about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPietro Albini <pietro@pietroalbini.org>2018-11-25 17:05:07 +0100
committerGitHub <noreply@github.com>2018-11-25 17:05:07 +0100
commite03fa3eec636bf66212e6457a8f451556b6fccf7 (patch)
tree7fbaba897acb1c70bbdc48bc087aa9df83cf22d3
parent989678e525a7f5db87a7fbfaedcf844c2b91d8d7 (diff)
parentdce1c4530e2707c338fe56b26a36797377f11514 (diff)
downloadrust-e03fa3eec636bf66212e6457a8f451556b6fccf7.tar.gz
rust-e03fa3eec636bf66212e6457a8f451556b6fccf7.zip
Rollup merge of #56170 - wesleywiser:fix_self_profiler_windows, r=estebank
Fix self profiler ICE on Windows

Fixes #51648
-rw-r--r--src/librustc/session/mod.rs6
-rw-r--r--src/librustc/util/profiling.rs17
2 files changed, 19 insertions, 4 deletions
diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs
index d688d93b808..1187c53305d 100644
--- a/src/librustc/session/mod.rs
+++ b/src/librustc/session/mod.rs
@@ -826,8 +826,10 @@ impl Session {
     }
 
     pub fn profiler<F: FnOnce(&mut SelfProfiler) -> ()>(&self, f: F) {
-        let mut profiler = self.self_profiling.borrow_mut();
-        f(&mut profiler);
+        if self.opts.debugging_opts.self_profile {
+            let mut profiler = self.self_profiling.borrow_mut();
+            f(&mut profiler);
+        }
     }
 
     pub fn print_profiler_results(&self) {
diff --git a/src/librustc/util/profiling.rs b/src/librustc/util/profiling.rs
index dfd3f2ee184..6540a09d877 100644
--- a/src/librustc/util/profiling.rs
+++ b/src/librustc/util/profiling.rs
@@ -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,)*) => {
@@ -208,7 +208,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()
+            }
+        } else {
+            self.current_timer.elapsed()
+        };
 
         self.current_timer = Instant::now();