mmtk/util/statistics/counter/
event_counter.rs1use super::*;
2use crate::util::statistics::stats::{SharedStats, DEFAULT_NUM_PHASES};
3use std::sync::Arc;
4
5pub struct EventCounter {
10 name: String,
11 pub implicitly_start: bool,
12 merge_phases: bool,
13 pub(super) count: Vec<u64>,
16 current_count: u64,
17 running: bool,
18 stats: Arc<SharedStats>,
19}
20
21impl EventCounter {
22 pub fn new(
23 name: String,
24 stats: Arc<SharedStats>,
25 implicitly_start: bool,
26 merge_phases: bool,
27 ) -> Self {
28 EventCounter {
29 name,
30 implicitly_start,
31 merge_phases,
32 count: Vec::with_capacity(DEFAULT_NUM_PHASES),
33 current_count: 0,
34 running: false,
35 stats,
36 }
37 }
38
39 pub fn inc(&mut self) {
43 if self.running {
44 self.inc_by(1);
45 }
46 }
47
48 pub fn inc_by(&mut self, value: u64) {
52 if self.running {
53 self.current_count += value;
54 }
55 }
56
57 pub fn print_current(&self) {
58 self.print_value(self.current_count);
59 }
60
61 fn print_value(&self, value: u64) {
62 print!("{}", value);
63 }
64}
65
66impl Counter for EventCounter {
67 fn start(&mut self) {
68 if !self.stats.get_gathering_stats() {
69 return;
70 }
71 debug_assert!(!self.running);
72 self.current_count = 0;
73 self.running = true;
74 }
75
76 fn stop(&mut self) {
77 if !self.stats.get_gathering_stats() {
78 return;
79 }
80 debug_assert!(self.running);
81 self.count.push(self.current_count);
82 debug_assert_eq!(self.count[self.stats.get_phase()], self.current_count);
83 self.running = false;
84 }
85
86 fn phase_change(&mut self, old_phase: usize) {
87 if self.running {
88 self.count.push(self.current_count);
89 debug_assert_eq!(self.count[old_phase], self.current_count);
90 self.current_count = 0;
91 }
92 }
93
94 fn print_count(&self, phase: usize) {
95 if self.merge_phases() {
96 debug_assert!((phase | 1) == (phase + 1));
97 self.print_value(self.count[phase] + self.count[phase + 1]);
98 } else {
99 self.print_value(self.count[phase]);
100 }
101 }
102
103 fn get_total(&self, other: Option<bool>) -> u64 {
104 let (start, step) = match other {
105 None => (0, 1),
106 Some(m) => (!m as usize, 2),
107 };
108 self.count
109 .iter()
110 .take(self.stats.get_phase() + 1)
111 .skip(start)
112 .step_by(step)
113 .sum()
114 }
115
116 fn print_total(&self, other: Option<bool>) {
117 self.print_value(self.get_total(other));
118 }
119
120 fn print_min(&self, other: bool) {
121 let mut p = !other as usize;
122 let mut min = self.count[p];
123 while p < self.stats.get_phase() {
124 if self.count[p] < min {
125 min = self.count[p];
126 p += 2;
127 }
128 }
129 self.print_value(min);
130 }
131
132 fn print_max(&self, other: bool) {
133 let mut p = !other as usize;
134 let mut max = self.count[p];
135 while p < self.stats.get_phase() {
136 if self.count[p] > max {
137 max = self.count[p];
138 p += 2;
139 }
140 }
141 self.print_value(max);
142 }
143
144 fn print_last(&self) {
145 let phase = self.stats.get_phase();
146 if phase > 0 {
147 self.print_count(phase - 1);
148 }
149 }
150
151 fn merge_phases(&self) -> bool {
152 self.merge_phases
153 }
154
155 fn implicitly_start(&self) -> bool {
156 self.implicitly_start
157 }
158
159 fn name(&self) -> &String {
160 &self.name
161 }
162}