-
Notifications
You must be signed in to change notification settings - Fork 77
Make it easy to add more stat types to work packet statistics #324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+242
−92
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5477da3
Add perfmon to Stats
caizixian e6aa5d3
Refactor worker stat type
caizixian ca1bf18
Revert "Add perfmon to Stats"
caizixian 6fccad5
Implement as trait object
caizixian dfd64e3
Fix test
caizixian fcb7dc7
Fix clippy
caizixian 973503b
Refactor out work counters
caizixian c39b824
Make counter name a String
caizixian ffb1f70
Don't insert counters when stat is disabled
caizixian 61bd95c
Fix rustfmt
caizixian 55fcf85
Document work counters
caizixian fc44c4b
Revert "Fix test"
caizixian 63e257d
More documentation
caizixian 76c5b6e
Fix typo
caizixian 9b78056
Merge branch 'master' into worker-stat
qinsoon 10a80e1
Better documentation for `work_counters`
caizixian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,135 @@ | ||
//! Counter for work packets | ||
//! | ||
//! Provides an abstraction and implementations of counters for collecting | ||
//! work-packet level statistics | ||
//! | ||
//! See [`crate::util::statistics`] for collecting statistics over a GC cycle | ||
use std::time::SystemTime; | ||
|
||
/// Common struct for different work counters | ||
/// | ||
/// Stores the total, min and max of counter readings | ||
#[derive(Copy, Clone, Debug)] | ||
pub(super) struct WorkCounterBase { | ||
pub(super) total: f64, | ||
pub(super) min: f64, | ||
pub(super) max: f64, | ||
} | ||
|
||
/// Make [`WorkCounter`] trait objects cloneable | ||
pub(super) trait WorkCounterClone { | ||
caizixian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// Clone the object | ||
fn clone_box(&self) -> Box<dyn WorkCounter>; | ||
} | ||
|
||
impl<T: 'static + WorkCounter + Clone> WorkCounterClone for T { | ||
fn clone_box(&self) -> Box<dyn WorkCounter> { | ||
Box::new(self.clone()) | ||
} | ||
} | ||
|
||
/// An abstraction of work counters | ||
/// | ||
/// Use for trait objects, as we have might have types of work counters for | ||
/// the same work packet and the types are not statically known. | ||
/// The overhead should be negligible compared with the cost of executing | ||
/// a work packet. | ||
pub(super) trait WorkCounter: WorkCounterClone + std::fmt::Debug { | ||
// TODO: consolidate with crate::util::statistics::counter::Counter; | ||
/// Start the counter | ||
fn start(&mut self); | ||
/// Stop the counter | ||
fn stop(&mut self); | ||
/// Name of counter | ||
fn name(&self) -> String; | ||
/// Return a reference to [`WorkCounterBase`] | ||
fn get_base(&self) -> &WorkCounterBase; | ||
/// Return a mutatable reference to [`WorkCounterBase`] | ||
fn get_base_mut(&mut self) -> &mut WorkCounterBase; | ||
} | ||
|
||
impl Clone for Box<dyn WorkCounter> { | ||
fn clone(&self) -> Box<dyn WorkCounter> { | ||
self.clone_box() | ||
} | ||
} | ||
|
||
impl Default for WorkCounterBase { | ||
fn default() -> Self { | ||
WorkCounterBase { | ||
total: 0.0, | ||
min: f64::INFINITY, | ||
max: f64::NEG_INFINITY, | ||
} | ||
} | ||
} | ||
|
||
impl WorkCounterBase { | ||
/// Merge two [`WorkCounterBase`], keep the semantics of the fields, | ||
/// and return a new object | ||
pub(super) fn merge(&self, other: &Self) -> Self { | ||
let min = self.min.min(other.min); | ||
let max = self.max.max(other.max); | ||
let total = self.total + other.total; | ||
WorkCounterBase { total, min, max } | ||
} | ||
|
||
/// Merge two [`WorkCounterBase`], modify the current object in place, | ||
/// and keep the semantics of the fields | ||
pub(super) fn merge_inplace(&mut self, other: &Self) { | ||
self.min = self.min.min(other.min); | ||
self.max = self.max.max(other.max); | ||
self.total += other.total; | ||
} | ||
|
||
/// Update the object based on a single value | ||
pub(super) fn merge_val(&mut self, val: f64) { | ||
self.min = self.min.min(val); | ||
self.max = self.max.max(val); | ||
self.total += val; | ||
} | ||
} | ||
|
||
/// Measure the durations of work packets | ||
/// | ||
/// Timing is based on [`SystemTime`] | ||
#[derive(Copy, Clone, Debug)] | ||
pub(super) struct WorkDuration { | ||
base: WorkCounterBase, | ||
start_value: Option<SystemTime>, | ||
running: bool, | ||
} | ||
|
||
impl WorkDuration { | ||
pub(super) fn new() -> Self { | ||
WorkDuration { | ||
base: Default::default(), | ||
start_value: None, | ||
running: false, | ||
} | ||
} | ||
} | ||
|
||
impl WorkCounter for WorkDuration { | ||
fn start(&mut self) { | ||
self.start_value = Some(SystemTime::now()); | ||
self.running = true; | ||
} | ||
|
||
fn stop(&mut self) { | ||
let duration = self.start_value.unwrap().elapsed().unwrap().as_nanos() as f64; | ||
self.base.merge_val(duration); | ||
} | ||
|
||
fn name(&self) -> String { | ||
"time".to_owned() | ||
} | ||
|
||
fn get_base(&self) -> &WorkCounterBase { | ||
&self.base | ||
} | ||
|
||
fn get_base_mut(&mut self) -> &mut WorkCounterBase { | ||
&mut self.base | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.