Skip to content

time.rs should consider using CLOCK_MONOTONIC_RAW instead of CLOCK_MONOTONIC on Linux #37902

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
marcust opened this issue Nov 20, 2016 · 5 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@marcust
Copy link

marcust commented Nov 20, 2016

I have reproducible compiler crashes on arm64:

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

thread 'rustc' panicked at 'other was less than the current instant', /buildslave/rust-buildbot/slave/beta-dist-rustc-cross-host-linux/build/src/libstd/sys/unix/time.rs:276
note: Run with `RUST_BACKTRACE=1` for a backtrace.

I'm not really sure where that behavior originates but I suspect the rather old Kernel in combination with the (obscure) hardware I'm running. longsleep/linux-pine64#44 has some more details, basically I see CLOCK_MONOTONIC jumping backwards from time to time. Looking somehow further I see other people used to have the same issue (http://stackoverflow.com/questions/3657289/linux-clock-gettimeclock-monotonic-strange-non-monotonic-behavior)

My Linux Kernel docs say:

       CLOCK_MONOTONIC
              Clock that cannot be set and represents monotonic time since some unspecified starting point.  This clock is not affected by
              discontinuous jumps in the system time (e.g., if the system administrator manually changes the clock), but  is  affected  by
              the incremental adjustments performed by adjtime(3) and NTP.

       CLOCK_MONOTONIC_RAW (since Linux 2.6.28; Linux-specific)
              Similar  to  CLOCK_MONOTONIC, but provides access to a raw hardware-based time that is not subject to NTP adjustments or the
              incremental adjustments performed by adjtime(3).

I see this behavior even without OpenNTPd running (so I still suspect that there is a Kernel/HW-Issue), but it might be better to use the Linux specific RAW version of the monotonic clock in order to get strict monotonic increasing values.

I just saw #37137 but as this is on real hardware that might be more relevant. Even when I'm not running anything that obviously does clock adjustments I see this behavior, RAW seems to be very consistently monotonically increasing. As rust is the only thing that I saw complaining so far you maybe want to consider changing this call.

@sfackler
Copy link
Member

We can't unconditionally use RAW since we support Linux all the way back to 2.6.18. We can probably use it when supported though.

@sfackler sfackler added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Nov 21, 2016
sfackler added a commit to sfackler/rust that referenced this issue Nov 21, 2016
@Amanieu
Copy link
Member

Amanieu commented Nov 21, 2016

This is definitely a kernel bug: CLOCK_MONOTONIC is definitely not supposed to jump backward. Have you tested CLOCK_MONOTONIC_RAW to see if it exhibits the same behavior on your hardware?

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).

@marcust
Copy link
Author

marcust commented Nov 21, 2016

Yes CLOCK_MONOTONIC_RAW is consistent, even under high loads in my environment.

@Mark-Simulacrum
Copy link
Member

It seems that we want to use CLOCK_MONOTONIC and it's usage is intentional. As such, I'm going to close. I think utilizing CLOCK_MONOTONIC_RAW could work, but it'd need to be separately exposed... which isn't too big a problem but would probably look odd API wise, and is best maintained out of std, most likely.

@gnzlbg
Copy link
Contributor

gnzlbg commented Aug 5, 2019

We can't unconditionally use RAW since we support Linux all the way back to 2.6.18.

Is this documented or tested anywhere ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants