diff options
Diffstat (limited to 'crates/profile/src/stop_watch.rs')
-rw-r--r-- | crates/profile/src/stop_watch.rs | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/crates/profile/src/stop_watch.rs b/crates/profile/src/stop_watch.rs new file mode 100644 index 000000000..5e276190e --- /dev/null +++ b/crates/profile/src/stop_watch.rs | |||
@@ -0,0 +1,86 @@ | |||
1 | //! Like `std::time::Instant`, but also measures memory & CPU cycles. | ||
2 | use std::{ | ||
3 | fmt, | ||
4 | time::{Duration, Instant}, | ||
5 | }; | ||
6 | |||
7 | use crate::MemoryUsage; | ||
8 | |||
9 | pub struct StopWatch { | ||
10 | time: Instant, | ||
11 | #[cfg(target_os = "linux")] | ||
12 | counter: Option<perf_event::Counter>, | ||
13 | memory: Option<MemoryUsage>, | ||
14 | } | ||
15 | |||
16 | pub struct StopWatchSpan { | ||
17 | pub time: Duration, | ||
18 | pub instructions: Option<u64>, | ||
19 | pub memory: Option<MemoryUsage>, | ||
20 | } | ||
21 | |||
22 | impl StopWatch { | ||
23 | pub fn start() -> StopWatch { | ||
24 | #[cfg(target_os = "linux")] | ||
25 | let counter = { | ||
26 | let mut counter = perf_event::Builder::new() | ||
27 | .build() | ||
28 | .map_err(|err| eprintln!("Failed to create perf counter: {}", err)) | ||
29 | .ok(); | ||
30 | if let Some(counter) = &mut counter { | ||
31 | if let Err(err) = counter.enable() { | ||
32 | eprintln!("Failed to start perf counter: {}", err) | ||
33 | } | ||
34 | } | ||
35 | counter | ||
36 | }; | ||
37 | let time = Instant::now(); | ||
38 | StopWatch { | ||
39 | time, | ||
40 | #[cfg(target_os = "linux")] | ||
41 | counter, | ||
42 | memory: None, | ||
43 | } | ||
44 | } | ||
45 | pub fn memory(mut self, yes: bool) -> StopWatch { | ||
46 | if yes { | ||
47 | self.memory = Some(MemoryUsage::current()); | ||
48 | } | ||
49 | self | ||
50 | } | ||
51 | pub fn elapsed(&mut self) -> StopWatchSpan { | ||
52 | let time = self.time.elapsed(); | ||
53 | |||
54 | #[cfg(target_os = "linux")] | ||
55 | let instructions = self.counter.as_mut().and_then(|it| { | ||
56 | it.read().map_err(|err| eprintln!("Failed to read perf counter: {}", err)).ok() | ||
57 | }); | ||
58 | #[cfg(not(target_os = "linux"))] | ||
59 | let instructions = None; | ||
60 | |||
61 | let memory = self.memory.map(|it| MemoryUsage::current() - it); | ||
62 | StopWatchSpan { time, instructions, memory } | ||
63 | } | ||
64 | } | ||
65 | |||
66 | impl fmt::Display for StopWatchSpan { | ||
67 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
68 | write!(f, "{:.2?}", self.time)?; | ||
69 | if let Some(mut instructions) = self.instructions { | ||
70 | let mut prefix = ""; | ||
71 | if instructions > 10000 { | ||
72 | instructions /= 1000; | ||
73 | prefix = "k" | ||
74 | } | ||
75 | if instructions > 10000 { | ||
76 | instructions /= 1000; | ||
77 | prefix = "m" | ||
78 | } | ||
79 | write!(f, ", {}{}i", instructions, prefix)?; | ||
80 | } | ||
81 | if let Some(memory) = self.memory { | ||
82 | write!(f, ", {}", memory)?; | ||
83 | } | ||
84 | Ok(()) | ||
85 | } | ||
86 | } | ||