aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_prof/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_prof/src')
-rw-r--r--crates/ra_prof/src/stop_watch.rs29
1 files changed, 23 insertions, 6 deletions
diff --git a/crates/ra_prof/src/stop_watch.rs b/crates/ra_prof/src/stop_watch.rs
index 54bfb0559..c52c92ce5 100644
--- a/crates/ra_prof/src/stop_watch.rs
+++ b/crates/ra_prof/src/stop_watch.rs
@@ -1,11 +1,13 @@
1use crate::MemoryUsage;
2use std::{ 1use std::{
3 fmt, 2 fmt,
4 time::{Duration, Instant}, 3 time::{Duration, Instant},
5}; 4};
6 5
6use crate::MemoryUsage;
7
7pub struct StopWatch { 8pub struct StopWatch {
8 time: Instant, 9 time: Instant,
10 #[cfg(target_os = "linux")]
9 counter: Option<perf_event::Counter>, 11 counter: Option<perf_event::Counter>,
10 memory: Option<MemoryUsage>, 12 memory: Option<MemoryUsage>,
11} 13}
@@ -18,12 +20,21 @@ pub struct StopWatchSpan {
18 20
19impl StopWatch { 21impl StopWatch {
20 pub fn start() -> StopWatch { 22 pub fn start() -> StopWatch {
21 let mut counter = perf_event::Builder::new().build().ok(); 23 #[cfg(target_os = "linux")]
22 if let Some(counter) = &mut counter { 24 let counter = {
23 let _ = counter.enable(); 25 let mut counter = perf_event::Builder::new().build().ok();
24 } 26 if let Some(counter) = &mut counter {
27 let _ = counter.enable();
28 }
29 counter
30 };
25 let time = Instant::now(); 31 let time = Instant::now();
26 StopWatch { time, counter, memory: None } 32 StopWatch {
33 time,
34 #[cfg(target_os = "linux")]
35 counter,
36 memory: None,
37 }
27 } 38 }
28 pub fn memory(mut self, yes: bool) -> StopWatch { 39 pub fn memory(mut self, yes: bool) -> StopWatch {
29 if yes { 40 if yes {
@@ -33,7 +44,12 @@ impl StopWatch {
33 } 44 }
34 pub fn elapsed(&mut self) -> StopWatchSpan { 45 pub fn elapsed(&mut self) -> StopWatchSpan {
35 let time = self.time.elapsed(); 46 let time = self.time.elapsed();
47
48 #[cfg(target_os = "linux")]
36 let instructions = self.counter.as_mut().and_then(|it| it.read().ok()); 49 let instructions = self.counter.as_mut().and_then(|it| it.read().ok());
50 #[cfg(not(target_os = "linux"))]
51 let instructions = None;
52
37 let memory = self.memory.map(|it| MemoryUsage::current() - it); 53 let memory = self.memory.map(|it| MemoryUsage::current() - it);
38 StopWatchSpan { time, instructions, memory } 54 StopWatchSpan { time, instructions, memory }
39 } 55 }
@@ -65,6 +81,7 @@ impl fmt::Display for StopWatchSpan {
65// https://github.com/jimblandy/perf-event/issues/8 81// https://github.com/jimblandy/perf-event/issues/8
66impl Drop for StopWatch { 82impl Drop for StopWatch {
67 fn drop(&mut self) { 83 fn drop(&mut self) {
84 #[cfg(target_os = "linux")]
68 if let Some(mut counter) = self.counter.take() { 85 if let Some(mut counter) = self.counter.take() {
69 let _ = counter.disable(); 86 let _ = counter.disable();
70 } 87 }