]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc/util/profiling.rs
Rollup merge of #56170 - wesleywiser:fix_self_profiler_windows, r=estebank
[rust.git] / src / librustc / util / profiling.rs
index dfd3f2ee184cebd87cb0490e06783bed66178cec..6540a09d87763c181788bd0e1ca162abe1023a8c 100644 (file)
@@ -12,7 +12,7 @@
 
 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 @@ pub fn end_activity(&mut self, category: ProfileCategory) {
     }
 
     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();