Skip to content

Commit 0ee06ed

Browse files
committed
Refactor out work counters
1 parent 3bed26d commit 0ee06ed

File tree

3 files changed

+108
-105
lines changed

3 files changed

+108
-105
lines changed

src/scheduler/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ pub use gc_work::ProcessEdgesWork;
3535
// We should do some refactoring related to Scanning::SCAN_MUTATORS_IN_SAFEPOINT
3636
// to make sure this type is not exposed to the bindings.
3737
pub use gc_work::ScanStackRoot;
38+
39+
mod work_counter;

src/scheduler/stat.rs

+1-105
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use super::work_counter::{WorkCounter, WorkCounterBase, WorkDuration};
12
use std::any::TypeId;
23
use std::collections::HashMap;
34
use std::sync::atomic::{AtomicBool, Ordering};
4-
use std::time::SystemTime;
55

66
#[derive(Default)]
77
pub struct SchedulerStat {
@@ -10,110 +10,6 @@ pub struct SchedulerStat {
1010
work_counters: HashMap<TypeId, Vec<Vec<Box<dyn WorkCounter>>>>,
1111
}
1212

13-
#[derive(Copy, Clone)]
14-
struct WorkCounterBase {
15-
total: f64,
16-
min: f64,
17-
max: f64,
18-
}
19-
20-
impl Default for WorkCounterBase {
21-
fn default() -> Self {
22-
WorkCounterBase {
23-
total: 0.0,
24-
min: f64::INFINITY,
25-
max: f64::NEG_INFINITY,
26-
}
27-
}
28-
}
29-
30-
impl WorkCounterBase {
31-
fn merge(&self, other: &Self) -> Self {
32-
let min = self.min.min(other.min);
33-
let max = self.max.max(other.max);
34-
let total = self.total + other.total;
35-
WorkCounterBase { total, min, max }
36-
}
37-
38-
fn merge_inplace(&mut self, other: &Self) {
39-
self.min = self.min.min(other.min);
40-
self.max = self.max.max(other.max);
41-
self.total += other.total;
42-
}
43-
44-
fn merge_val(&mut self, val: f64) {
45-
self.min = self.min.min(val);
46-
self.max = self.max.max(val);
47-
self.total += val;
48-
}
49-
}
50-
51-
trait WorkCounter: WorkCounterClone {
52-
// TODO: consolidate with crate::util::statistics::counter::Counter;
53-
fn start(&mut self);
54-
fn stop(&mut self);
55-
fn name(&self) -> &'static str;
56-
fn get_base(&self) -> &WorkCounterBase;
57-
fn get_base_mut(&mut self) -> &mut WorkCounterBase;
58-
}
59-
60-
trait WorkCounterClone {
61-
fn clone_box(&self) -> Box<dyn WorkCounter>;
62-
}
63-
64-
impl<T: 'static + WorkCounter + Clone> WorkCounterClone for T {
65-
fn clone_box(&self) -> Box<dyn WorkCounter> {
66-
Box::new(self.clone())
67-
}
68-
}
69-
70-
impl Clone for Box<dyn WorkCounter> {
71-
fn clone(&self) -> Box<dyn WorkCounter> {
72-
self.clone_box()
73-
}
74-
}
75-
76-
#[derive(Copy, Clone)]
77-
struct WorkDuration {
78-
base: WorkCounterBase,
79-
start_value: Option<SystemTime>,
80-
running: bool,
81-
}
82-
83-
impl WorkDuration {
84-
fn new() -> Self {
85-
WorkDuration {
86-
base: Default::default(),
87-
start_value: None,
88-
running: false,
89-
}
90-
}
91-
}
92-
93-
impl WorkCounter for WorkDuration {
94-
fn start(&mut self) {
95-
self.start_value = Some(SystemTime::now());
96-
self.running = true;
97-
}
98-
99-
fn stop(&mut self) {
100-
let duration = self.start_value.unwrap().elapsed().unwrap().as_nanos() as f64;
101-
self.base.merge_val(duration);
102-
}
103-
104-
fn name(&self) -> &'static str {
105-
"time"
106-
}
107-
108-
fn get_base(&self) -> &WorkCounterBase {
109-
&self.base
110-
}
111-
112-
fn get_base_mut(&mut self) -> &mut WorkCounterBase {
113-
&mut self.base
114-
}
115-
}
116-
11713
impl SchedulerStat {
11814
/// Extract the work-packet name from the full type name.
11915
/// i.e. simplifies `crate::scheduler::gc_work::SomeWorkPacket<Semispace>` to `SomeWorkPacket`.

src/scheduler/work_counter.rs

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
use std::time::SystemTime;
2+
3+
#[derive(Copy, Clone)]
4+
pub(super) struct WorkCounterBase {
5+
pub(super) total: f64,
6+
pub(super) min: f64,
7+
pub(super) max: f64,
8+
}
9+
10+
pub(super) trait WorkCounterClone {
11+
fn clone_box(&self) -> Box<dyn WorkCounter>;
12+
}
13+
14+
impl<T: 'static + WorkCounter + Clone> WorkCounterClone for T {
15+
fn clone_box(&self) -> Box<dyn WorkCounter> {
16+
Box::new(self.clone())
17+
}
18+
}
19+
20+
pub(super) trait WorkCounter: WorkCounterClone {
21+
// TODO: consolidate with crate::util::statistics::counter::Counter;
22+
fn start(&mut self);
23+
fn stop(&mut self);
24+
fn name(&self) -> &'static str;
25+
fn get_base(&self) -> &WorkCounterBase;
26+
fn get_base_mut(&mut self) -> &mut WorkCounterBase;
27+
}
28+
29+
impl Clone for Box<dyn WorkCounter> {
30+
fn clone(&self) -> Box<dyn WorkCounter> {
31+
self.clone_box()
32+
}
33+
}
34+
35+
impl Default for WorkCounterBase {
36+
fn default() -> Self {
37+
WorkCounterBase {
38+
total: 0.0,
39+
min: f64::INFINITY,
40+
max: f64::NEG_INFINITY,
41+
}
42+
}
43+
}
44+
45+
impl WorkCounterBase {
46+
pub(super) fn merge(&self, other: &Self) -> Self {
47+
let min = self.min.min(other.min);
48+
let max = self.max.max(other.max);
49+
let total = self.total + other.total;
50+
WorkCounterBase { total, min, max }
51+
}
52+
53+
pub(super) fn merge_inplace(&mut self, other: &Self) {
54+
self.min = self.min.min(other.min);
55+
self.max = self.max.max(other.max);
56+
self.total += other.total;
57+
}
58+
59+
pub(super) fn merge_val(&mut self, val: f64) {
60+
self.min = self.min.min(val);
61+
self.max = self.max.max(val);
62+
self.total += val;
63+
}
64+
}
65+
66+
#[derive(Copy, Clone)]
67+
pub(super) struct WorkDuration {
68+
base: WorkCounterBase,
69+
start_value: Option<SystemTime>,
70+
running: bool,
71+
}
72+
73+
impl WorkDuration {
74+
pub(super) fn new() -> Self {
75+
WorkDuration {
76+
base: Default::default(),
77+
start_value: None,
78+
running: false,
79+
}
80+
}
81+
}
82+
83+
impl WorkCounter for WorkDuration {
84+
fn start(&mut self) {
85+
self.start_value = Some(SystemTime::now());
86+
self.running = true;
87+
}
88+
89+
fn stop(&mut self) {
90+
let duration = self.start_value.unwrap().elapsed().unwrap().as_nanos() as f64;
91+
self.base.merge_val(duration);
92+
}
93+
94+
fn name(&self) -> &'static str {
95+
"time"
96+
}
97+
98+
fn get_base(&self) -> &WorkCounterBase {
99+
&self.base
100+
}
101+
102+
fn get_base_mut(&mut self) -> &mut WorkCounterBase {
103+
&mut self.base
104+
}
105+
}

0 commit comments

Comments
 (0)