aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-07-30 09:40:55 +0100
committerAleksey Kladov <[email protected]>2020-07-30 09:40:55 +0100
commit5e498546e8275c86e0b3f711b069b2787c6385f2 (patch)
treedd5c7e319b1a9cb3ac77572e20a93b089be9d12b
parente9983f98a964a940bfe5283b02f0f2dc16d3095a (diff)
Fix non-linux compilation
-rw-r--r--crates/ra_prof/Cargo.toml2
-rw-r--r--crates/ra_prof/src/stop_watch.rs29
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"
15backtrace = { version = "0.3.44", optional = true } 15backtrace = { version = "0.3.44", optional = true }
16cfg-if = "0.1.10" 16cfg-if = "0.1.10"
17libc = "0.2.73" 17libc = "0.2.73"
18
19[target.'cfg(target_os = "linux")'.dependencies]
18perf-event = "0.4" 20perf-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 @@
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 }