-
Notifications
You must be signed in to change notification settings - Fork 384
Make sleep
work with isolation enabled
#2506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6a37643
make `sleep` work with isolation enabled
pvdrz ad69e0b
hide all enums inside kind types
pvdrz f5e2f73
move clock stuff to separate file
RalfJung bc307b4
organize clock arithmetic more like the stdlib
RalfJung 613a436
add extra sanity check against depending on system time with isolatio…
RalfJung b16d301
test fast sleeping
RalfJung c834637
address review comments
pvdrz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
use std::sync::atomic::{AtomicU64, Ordering}; | ||
use std::time::{Duration, Instant as StdInstant}; | ||
|
||
/// When using a virtual clock, this defines how many nanoseconds we pretend are passing for each | ||
/// basic block. | ||
const NANOSECONDS_PER_BASIC_BLOCK: u64 = 10; | ||
|
||
#[derive(Debug)] | ||
pub struct Instant { | ||
kind: InstantKind, | ||
} | ||
|
||
#[derive(Debug)] | ||
enum InstantKind { | ||
Host(StdInstant), | ||
Virtual { nanoseconds: u64 }, | ||
} | ||
|
||
impl Instant { | ||
pub fn checked_add(&self, duration: Duration) -> Option<Instant> { | ||
match self.kind { | ||
InstantKind::Host(instant) => | ||
instant.checked_add(duration).map(|i| Instant { kind: InstantKind::Host(i) }), | ||
InstantKind::Virtual { nanoseconds } => | ||
u128::from(nanoseconds) | ||
.checked_add(duration.as_nanos()) | ||
.and_then(|n| u64::try_from(n).ok()) | ||
.map(|nanoseconds| Instant { kind: InstantKind::Virtual { nanoseconds } }), | ||
} | ||
} | ||
|
||
pub fn duration_since(&self, earlier: Instant) -> Duration { | ||
match (&self.kind, earlier.kind) { | ||
(InstantKind::Host(instant), InstantKind::Host(earlier)) => | ||
instant.duration_since(earlier), | ||
( | ||
InstantKind::Virtual { nanoseconds }, | ||
InstantKind::Virtual { nanoseconds: earlier }, | ||
) => Duration::from_nanos(nanoseconds.saturating_sub(earlier)), | ||
_ => panic!("all `Instant` must be of the same kind"), | ||
} | ||
} | ||
} | ||
|
||
/// A monotone clock used for `Instant` simulation. | ||
#[derive(Debug)] | ||
pub struct Clock { | ||
kind: ClockKind, | ||
} | ||
|
||
#[derive(Debug)] | ||
enum ClockKind { | ||
Host { | ||
/// The "time anchor" for this machine's monotone clock. | ||
time_anchor: StdInstant, | ||
}, | ||
Virtual { | ||
/// The "current virtual time". | ||
nanoseconds: AtomicU64, | ||
}, | ||
} | ||
|
||
impl Clock { | ||
/// Create a new clock based on the availability of communication with the host. | ||
pub fn new(communicate: bool) -> Self { | ||
let kind = if communicate { | ||
ClockKind::Host { time_anchor: StdInstant::now() } | ||
} else { | ||
ClockKind::Virtual { nanoseconds: 0.into() } | ||
}; | ||
|
||
Self { kind } | ||
} | ||
|
||
/// Let the time pass for a small interval. | ||
pub fn tick(&self) { | ||
match &self.kind { | ||
ClockKind::Host { .. } => { | ||
// Time will pass without us doing anything. | ||
} | ||
ClockKind::Virtual { nanoseconds } => { | ||
nanoseconds.fetch_add(NANOSECONDS_PER_BASIC_BLOCK, Ordering::SeqCst); | ||
} | ||
} | ||
} | ||
|
||
/// Sleep for the desired duration. | ||
pub fn sleep(&self, duration: Duration) { | ||
match &self.kind { | ||
ClockKind::Host { .. } => std::thread::sleep(duration), | ||
ClockKind::Virtual { nanoseconds } => { | ||
// Just pretend that we have slept for some time. | ||
nanoseconds.fetch_add(duration.as_nanos().try_into().unwrap(), Ordering::SeqCst); | ||
} | ||
} | ||
} | ||
|
||
/// Return the `anchor` instant, to convert between monotone instants and durations relative to the anchor. | ||
pub fn anchor(&self) -> Instant { | ||
match &self.kind { | ||
ClockKind::Host { time_anchor } => Instant { kind: InstantKind::Host(*time_anchor) }, | ||
ClockKind::Virtual { .. } => Instant { kind: InstantKind::Virtual { nanoseconds: 0 } }, | ||
} | ||
} | ||
|
||
pub fn now(&self) -> Instant { | ||
match &self.kind { | ||
ClockKind::Host { .. } => Instant { kind: InstantKind::Host(StdInstant::now()) }, | ||
ClockKind::Virtual { nanoseconds } => | ||
Instant { | ||
kind: InstantKind::Virtual { nanoseconds: nanoseconds.load(Ordering::SeqCst) }, | ||
}, | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.