Skip to content

Commit e03fa3e

Browse files
authored
Rollup merge of rust-lang#56170 - wesleywiser:fix_self_profiler_windows, r=estebank
Fix self profiler ICE on Windows Fixes rust-lang#51648
2 parents 989678e + dce1c45 commit e03fa3e

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/librustc/session/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,10 @@ impl Session {
826826
}
827827

828828
pub fn profiler<F: FnOnce(&mut SelfProfiler) -> ()>(&self, f: F) {
829-
let mut profiler = self.self_profiling.borrow_mut();
830-
f(&mut profiler);
829+
if self.opts.debugging_opts.self_profile {
830+
let mut profiler = self.self_profiling.borrow_mut();
831+
f(&mut profiler);
832+
}
831833
}
832834

833835
pub fn print_profiler_results(&self) {

src/librustc/util/profiling.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use session::config::Options;
1212

1313
use std::fs;
1414
use std::io::{self, StdoutLock, Write};
15-
use std::time::Instant;
15+
use std::time::{Duration, Instant};
1616

1717
macro_rules! define_categories {
1818
($($name:ident,)*) => {
@@ -208,7 +208,20 @@ impl SelfProfiler {
208208
}
209209

210210
fn stop_timer(&mut self) -> u64 {
211-
let elapsed = self.current_timer.elapsed();
211+
let elapsed = if cfg!(windows) {
212+
// On Windows, timers don't always appear to be monotonic (see #51648)
213+
// which can lead to panics when calculating elapsed time.
214+
// Work around this by testing to see if the current time is less than
215+
// our recorded time, and if it is, just returning 0.
216+
let now = Instant::now();
217+
if self.current_timer >= now {
218+
Duration::new(0, 0)
219+
} else {
220+
self.current_timer.elapsed()
221+
}
222+
} else {
223+
self.current_timer.elapsed()
224+
};
212225

213226
self.current_timer = Instant::now();
214227

0 commit comments

Comments
 (0)