-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a new metric type designed to more efficiently represent the per-core metrics by reducing the number of metric entries.
- Loading branch information
Showing
9 changed files
with
285 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use metriken::Metric; | ||
use parking_lot::RwLock; | ||
use std::sync::OnceLock; | ||
use thiserror::Error; | ||
use metriken::Value; | ||
|
||
#[derive(Error, Debug, PartialEq)] | ||
pub enum CounterGroupError { | ||
#[error("the index is higher than the counter group size")] | ||
InvalidIndex, | ||
} | ||
|
||
/// A group of counters that's protected by a reader-writer lock. | ||
pub struct CounterGroup { | ||
inner: OnceLock<RwLock<Vec<u64>>>, | ||
entries: usize, | ||
} | ||
|
||
impl Metric for CounterGroup { | ||
fn as_any(&self) -> std::option::Option<&(dyn std::any::Any + 'static)> { | ||
Some(self) | ||
} | ||
|
||
fn value(&self) -> std::option::Option<metriken::Value<'_>> { | ||
Some(Value::Other(self)) | ||
} | ||
} | ||
|
||
impl CounterGroup { | ||
/// Create a new counter group | ||
pub const fn new(entries: usize) -> Self { | ||
Self { | ||
inner: OnceLock::new(), | ||
entries, | ||
} | ||
} | ||
|
||
/// Sets the counter at a given index to the provided value | ||
pub fn set(&self, idx: usize, value: u64) -> Result<(), CounterGroupError> { | ||
if idx >= self.entries { | ||
Err(CounterGroupError::InvalidIndex) | ||
} else { | ||
let mut inner = self.get_or_init().write(); | ||
|
||
inner[idx] = value; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
/// Load the counter values | ||
pub fn load(&self) -> Option<Vec<u64>> { | ||
self.inner.get().map(|v| v.read().clone()) | ||
} | ||
|
||
pub fn len(&self) -> usize { | ||
self.entries | ||
} | ||
|
||
fn get_or_init(&self) -> &RwLock<Vec<u64>> { | ||
self.inner.get_or_init(|| vec![0; self.entries].into()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.