Skip to content
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

use CLOCK_BOOTTIME in Instant::now #88714

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion library/std/src/sys/unix/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,13 @@ mod inner {
pub fn now() -> Instant {
#[cfg(target_os = "macos")]
const clock_id: libc::clockid_t = libc::CLOCK_UPTIME_RAW;
#[cfg(not(target_os = "macos"))]

#[cfg(target_os = "linux")]
const clock_id: libc::clockid_t = libc::CLOCK_BOOTTIME;
Copy link
Member

@RalfJung RalfJung Nov 30, 2022

Choose a reason for hiding this comment

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

Should we have documentation saying that Instant, when possible, will use a clock that keeps going when the system is asleep -- and list on which targets this is definitely the case?

Judging from this and this PR, that would be Windows and Linux. No idea about Android, the BSDs, iOS/watchOS, ...

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Okay so it seems like after this PR it's pretty much "everything except macOS"?

Copy link
Contributor

@CryZe CryZe Nov 30, 2022

Choose a reason for hiding this comment

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

Fuchsia is also problematic as they specifically defined their monotonic to behave like Linux's... but they didn't provide an alternative BOOTTIME one yet. Also the PR only checks for "linux" specifically and not Linux derivatives such as Android and l4re (not sure 100% on l4re, at least libc usually bundles it into the Linux-like #[cfg]s), which it probably should (just not emscripten as it errors out on BOOTTIME atm).

Copy link
Member

Choose a reason for hiding this comment

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

Okay, doesn't look like this actually closes #87906 quite yet then.


#[cfg(not(any(target_os = "macos", target_os = "linux")))]
const clock_id: libc::clockid_t = libc::CLOCK_MONOTONIC;

Instant { t: Timespec::now(clock_id) }
}

Expand Down
3 changes: 3 additions & 0 deletions src/tools/miri/src/shims/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
relative_clocks = vec![
this.eval_libc_i32("CLOCK_MONOTONIC")?,
this.eval_libc_i32("CLOCK_MONOTONIC_COARSE")?,
// This is the equivalent to `CLOCK_UPTIME_RAW` in the macos section
// We support it, because std relies on it
Copy link
Member

@RalfJung RalfJung Nov 30, 2022

Choose a reason for hiding this comment

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

This comment is backwards -- BOOTTIME you say does increment when the machine is asleep, but CLOCK_UPTIME_RAW does not.

If anything, it seems like CLOCK_BOOTTIME on linux is like CLOCK_MONOTONIC on macOS, and CLOCK_MONOTONIC on linux is like CLOCK_UPTIME_RAW (and mach_absolute_time) on macos?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah judging from a quick search, looks like MONOTONIC on Linux does not increment when sleeping, but on macos it does. It also looks before this PR we are actually consistent (both Linux and macos use clocks that do not count when the system is asleep), and this PR makes us inconsistent by changing only Linux.

Or maybe I am looking at the wrong docs? This seems to look reasonably official though for macos.

Comment on lines +48 to +49
Copy link
Member

@RalfJung RalfJung Nov 30, 2022

Choose a reason for hiding this comment

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

Suggested change
// This is the equivalent to `CLOCK_UPTIME_RAW` in the macos section
// We support it, because std relies on it
// Unlike `CLOCK_MONOTONIC` (which is weird on Linux in that it does not increment
// during system sleep), this *is* supposed to increment during system sleep...
// but that's not really something a program running inside Miri can tell, anyway.
// We support it because std relies on it.

this.eval_libc_i32("CLOCK_BOOTTIME")?,
];
}
"macos" => {
Expand Down