Skip to content

Commit 6a2eb72

Browse files
committed
Auto merge of rust-lang#2816 - oli-obk:💤, r=RalfJung
Update the virtual clock in isolation mode to step forward with around the same speed as the host system. Before this, the 1s sleep test took around 4 minutes on my machine.
2 parents 7b1c5b6 + abc824f commit 6a2eb72

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

src/tools/miri/src/clock.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ use std::time::{Duration, Instant as StdInstant};
33

44
/// When using a virtual clock, this defines how many nanoseconds we pretend are passing for each
55
/// basic block.
6-
const NANOSECONDS_PER_BASIC_BLOCK: u64 = 10;
6+
/// This number is pretty random, but it has been shown to approximately cause
7+
/// some sample programs to run within an order of magnitude of real time on desktop CPUs.
8+
/// (See `tests/pass/shims/time-with-isolation*.rs`.)
9+
const NANOSECONDS_PER_BASIC_BLOCK: u64 = 5000;
710

811
#[derive(Debug)]
912
pub struct Instant {

src/tools/miri/tests/pass/concurrency/thread_park_isolated.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ fn main() {
77

88
thread::park_timeout(Duration::from_millis(200));
99

10-
// Thanks to deterministic execution, this will wiat *exactly* 200ms (rounded to 1ms).
11-
assert!((200..201).contains(&start.elapsed().as_millis()));
10+
// Thanks to deterministic execution, this will wait *exactly* 200ms, plus the time for the surrounding code.
11+
assert!((200..210).contains(&start.elapsed().as_millis()), "{}", start.elapsed().as_millis());
1212
}

src/tools/miri/tests/pass/shims/time-with-isolation.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,23 @@ fn test_time_passes() {
2222
let diff = now2.duration_since(now1);
2323
assert_eq!(now1 + diff, now2);
2424
assert_eq!(now2 - diff, now1);
25-
// The virtual clock is deterministic and I got 29us on a 64-bit Linux machine. However, this
25+
// The virtual clock is deterministic and I got 15ms on a 64-bit Linux machine. However, this
2626
// changes according to the platform so we use an interval to be safe. This should be updated
2727
// if `NANOSECONDS_PER_BASIC_BLOCK` changes.
28-
assert!(diff.as_micros() > 10);
29-
assert!(diff.as_micros() < 40);
28+
assert!(diff.as_millis() > 5);
29+
assert!(diff.as_millis() < 20);
30+
}
31+
32+
fn test_block_for_one_second() {
33+
let end = Instant::now() + Duration::from_secs(1);
34+
// This takes a long time, as we only increment when statements are executed.
35+
// When we sleep on all threads, we fast forward to the sleep duration, which we can't
36+
// do with busy waiting.
37+
while Instant::now() < end {}
3038
}
3139

3240
fn main() {
3341
test_time_passes();
42+
test_block_for_one_second();
3443
test_sleep();
3544
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use std::time::Instant;
2+
3+
fn main() {
4+
let begin = Instant::now();
5+
for _ in 0..100_000 {}
6+
let time = begin.elapsed();
7+
println!("The loop took around {}s", time.as_secs());
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The loop took around 13s

0 commit comments

Comments
 (0)