Skip to content

Use CLOCK_MONOTONIC_RAW instead of CLOCK_MONOTONIC for Instant #77807

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

Closed
maxbla opened this issue Oct 10, 2020 · 3 comments
Closed

Use CLOCK_MONOTONIC_RAW instead of CLOCK_MONOTONIC for Instant #77807

maxbla opened this issue Oct 10, 2020 · 3 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. O-linux Operating system: Linux T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@maxbla
Copy link
Contributor

maxbla commented Oct 10, 2020

According to man pages, CLOCK_MONOTONIC_RAW doesn't get adjustments from NTP or adjtime, whereas CLOCK_MONOTONIC does. This seems ideal for Instant. CLOCK_MONOTONIC_RAW appears to prevent slewing or jumping, closer matching user expectations about Instant (despite the fact that Instant is explicitly allowed to jump forward and slew)

This wasn't done earlier due to Rust's minimum supported Linux kernel version (now 2.6.32) since CLOCK_MONOTONIC_RAW was added in 2.6.28. See also #37902

I'd also like to change thread::sleep's implementation to use clock_nanosleep(CLOCK_MONOTONIC_RAW) from it's current nanosleep(). Note that

POSIX.1 specifies that nanosleep() should measure time against the CLOCK_REALTIME clock. However, Linux measures the time using the CLOCK_MONOTONIC clock.

from linux.die. This has negative implications for the implementation of thread::sleep on BSD, which should probably use CLOCK_MONOTONIC.

Of course, CLOCK_MONATONIC_RAW is Linux-specific, so other Unix platforms will still use CLOCK_MONATONIC

@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs Relevant to the library team, which will review and decide on the PR/issue. O-linux Operating system: Linux labels Oct 10, 2020
@maxbla maxbla changed the title Use CLOCK_MONATONIC_RAW instead of CLOCK_MONOTONIC for Instant Use CLOCK_MONOTONIC_RAW instead of CLOCK_MONOTONIC for Instant Oct 10, 2020
@sfackler
Copy link
Member

sfackler commented Oct 11, 2020

The kernel version is not the only reason to use CLOCK_MONOTONIC:

We probably don't want to use CLOCK_MONOTONIC_RAW for Instant since it is not guaranteed to follow real time (e.g. 1 second on CLOCK_MONOTONIC_RAW can be shorter/longer than 1 real second). CLOCK_MONOTONIC is sped up/slowed down by the kernel as necessary to match real time (through NTP).

#37902 (comment)

@maxbla
Copy link
Contributor Author

maxbla commented Oct 11, 2020

The kernel version is not the only reason to use CLOCK_MONOTONIC: ...

Sorry, I managed to not read that part of that issue.

That said, CLOCK_MONOTONIC is not guaranteed to follow real time either. For example, if NTP is significantly different from your hardware clock, it jumps. Also, if you don't use NTP, or adjtime, I suspect the two clocks are equivalent. But I'm not hell-bent on changing it.

I am convinced that thread::sleep should use clock_nanosleep() instead of nanosleep, as this will make the BSDs behave nicer.

@maxbla
Copy link
Contributor Author

maxbla commented Oct 14, 2020

thread::sleep() cannot use clock_nanosleep() because clock_nanosleep is not currently exposed by libc on bsd (see rust-lang/libc#1922). Further, FreeBSD 10 didn't even have clock_nanosleep.

@maxbla maxbla closed this as completed Oct 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. O-linux Operating system: Linux T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants