diff options
author | Aleksey Kladov <[email protected]> | 2020-07-30 09:40:55 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-07-30 09:40:55 +0100 |
commit | 5e498546e8275c86e0b3f711b069b2787c6385f2 (patch) | |
tree | dd5c7e319b1a9cb3ac77572e20a93b089be9d12b /crates/ra_prof/src | |
parent | e9983f98a964a940bfe5283b02f0f2dc16d3095a (diff) |
Fix non-linux compilation
Diffstat (limited to 'crates/ra_prof/src')
-rw-r--r-- | crates/ra_prof/src/stop_watch.rs | 29 |
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 @@ | |||
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 | } |