Skip to content

Commit 5e49854

Browse files
committed
Fix non-linux compilation
1 parent e9983f9 commit 5e49854

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

crates/ra_prof/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ once_cell = "1.3.1"
1515
backtrace = { version = "0.3.44", optional = true }
1616
cfg-if = "0.1.10"
1717
libc = "0.2.73"
18+
19+
[target.'cfg(target_os = "linux")'.dependencies]
1820
perf-event = "0.4"
1921

2022
[features]

crates/ra_prof/src/stop_watch.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
use crate::MemoryUsage;
21
use std::{
32
fmt,
43
time::{Duration, Instant},
54
};
65

6+
use crate::MemoryUsage;
7+
78
pub struct StopWatch {
89
time: Instant,
10+
#[cfg(target_os = "linux")]
911
counter: Option<perf_event::Counter>,
1012
memory: Option<MemoryUsage>,
1113
}
@@ -18,12 +20,21 @@ pub struct StopWatchSpan {
1820

1921
impl StopWatch {
2022
pub fn start() -> StopWatch {
21-
let mut counter = perf_event::Builder::new().build().ok();
22-
if let Some(counter) = &mut counter {
23-
let _ = counter.enable();
24-
}
23+
#[cfg(target_os = "linux")]
24+
let counter = {
25+
let mut counter = perf_event::Builder::new().build().ok();
26+
if let Some(counter) = &mut counter {
27+
let _ = counter.enable();
28+
}
29+
counter
30+
};
2531
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+
}
2738
}
2839
pub fn memory(mut self, yes: bool) -> StopWatch {
2940
if yes {
@@ -33,7 +44,12 @@ impl StopWatch {
3344
}
3445
pub fn elapsed(&mut self) -> StopWatchSpan {
3546
let time = self.time.elapsed();
47+
48+
#[cfg(target_os = "linux")]
3649
let instructions = self.counter.as_mut().and_then(|it| it.read().ok());
50+
#[cfg(not(target_os = "linux"))]
51+
let instructions = None;
52+
3753
let memory = self.memory.map(|it| MemoryUsage::current() - it);
3854
StopWatchSpan { time, instructions, memory }
3955
}
@@ -65,6 +81,7 @@ impl fmt::Display for StopWatchSpan {
6581
// https://github.com/jimblandy/perf-event/issues/8
6682
impl Drop for StopWatch {
6783
fn drop(&mut self) {
84+
#[cfg(target_os = "linux")]
6885
if let Some(mut counter) = self.counter.take() {
6986
let _ = counter.disable();
7087
}

0 commit comments

Comments
 (0)