mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-01 03:03:40 +00:00
Fix non-linux compilation
This commit is contained in:
parent
e9983f98a9
commit
5e498546e8
@ -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]
|
||||
|
@ -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<perf_event::Counter>,
|
||||
memory: Option<MemoryUsage>,
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user