From 5e498546e8275c86e0b3f711b069b2787c6385f2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 10:40:55 +0200 Subject: Fix non-linux compilation --- crates/ra_prof/Cargo.toml | 2 ++ crates/ra_prof/src/stop_watch.rs | 29 +++++++++++++++++++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) (limited to 'crates') 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" backtrace = { version = "0.3.44", optional = true } cfg-if = "0.1.10" libc = "0.2.73" + +[target.'cfg(target_os = "linux")'.dependencies] perf-event = "0.4" [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 @@ -use crate::MemoryUsage; use std::{ fmt, time::{Duration, Instant}, }; +use crate::MemoryUsage; + pub struct StopWatch { time: Instant, + #[cfg(target_os = "linux")] counter: Option, memory: Option, } @@ -18,12 +20,21 @@ pub struct StopWatchSpan { impl StopWatch { pub fn start() -> StopWatch { - let mut counter = perf_event::Builder::new().build().ok(); - if let Some(counter) = &mut counter { - let _ = counter.enable(); - } + #[cfg(target_os = "linux")] + let counter = { + let mut counter = perf_event::Builder::new().build().ok(); + if let Some(counter) = &mut counter { + let _ = counter.enable(); + } + counter + }; let time = Instant::now(); - StopWatch { time, counter, memory: None } + StopWatch { + time, + #[cfg(target_os = "linux")] + counter, + memory: None, + } } pub fn memory(mut self, yes: bool) -> StopWatch { if yes { @@ -33,7 +44,12 @@ impl StopWatch { } pub fn elapsed(&mut self) -> StopWatchSpan { let time = self.time.elapsed(); + + #[cfg(target_os = "linux")] let instructions = self.counter.as_mut().and_then(|it| it.read().ok()); + #[cfg(not(target_os = "linux"))] + let instructions = None; + let memory = self.memory.map(|it| MemoryUsage::current() - it); StopWatchSpan { time, instructions, memory } } @@ -65,6 +81,7 @@ impl fmt::Display for StopWatchSpan { // https://github.com/jimblandy/perf-event/issues/8 impl Drop for StopWatch { fn drop(&mut self) { + #[cfg(target_os = "linux")] if let Some(mut counter) = self.counter.take() { let _ = counter.disable(); } -- cgit v1.2.3