Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[Windows] Work around non-monotonic clocks in the self-profiler
On Windows, the high-resolution timestamp api doesn't seem to always be
monotonic. This can cause panics when the self-profiler uses the
`Instant` api to find elapsed time.

Work around this by detecting the case where now is less than the start
time and just use 0 elapsed ticks as the measurement.

Fixes #51648
  • Loading branch information
wesleywiser committed Nov 25, 2018
commit dce1c4530e2707c338fe56b26a36797377f11514
17 changes: 15 additions & 2 deletions src/librustc/util/profiling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use session::config::Options;

use std::fs;
use std::io::{self, StdoutLock, Write};
use std::time::Instant;
use std::time::{Duration, Instant};

macro_rules! define_categories {
($($name:ident,)*) => {
Expand Down Expand Up @@ -197,7 +197,20 @@ impl SelfProfiler {
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this line (209) be changed to do now - self.current_timer instead? I think right now it's susceptible to the same error the workaround is meant to prevent. Since Instant::elapsed will call Instant::now again, which could be inaccurate and won't be double-checked like the variable now just was.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems like a reasonable change to me

}
} else {
self.current_timer.elapsed()
};

self.current_timer = Instant::now();

Expand Down