diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_prof/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/ra_prof/src/stop_watch.rs | 29 |
2 files changed, 25 insertions, 6 deletions
diff --git a/crates/ra_prof/Cargo.toml b/crates/ra_prof/Cargo.toml index e41cb5f52..c82b9f76d 100644 --- a/crates/ra_prof/Cargo.toml +++ b/crates/ra_prof/Cargo.toml | |||
@@ -15,6 +15,8 @@ once_cell = "1.3.1" | |||
15 | backtrace = { version = "0.3.44", optional = true } | 15 | backtrace = { version = "0.3.44", optional = true } |
16 | cfg-if = "0.1.10" | 16 | cfg-if = "0.1.10" |
17 | libc = "0.2.73" | 17 | libc = "0.2.73" |
18 | |||
19 | [target.'cfg(target_os = "linux")'.dependencies] | ||
18 | perf-event = "0.4" | 20 | perf-event = "0.4" |
19 | 21 | ||
20 | [features] | 22 | [features] |
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 @@ | |||
1 | use crate::MemoryUsage; | ||
2 | use std::{ | 1 | use std::{ |
3 | fmt, | 2 | fmt, |
4 | time::{Duration, Instant}, | 3 | time::{Duration, Instant}, |
5 | }; | 4 | }; |
6 | 5 | ||
6 | use crate::MemoryUsage; | ||
7 | |||
7 | pub struct StopWatch { | 8 | pub 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 | ||
19 | impl StopWatch { | 21 | impl 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 |
66 | impl Drop for StopWatch { | 82 | impl 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 | } |