Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between CLOCK_MONOTONIC & CLOCK_MONOTONIC_RAW?

According to the Linux man page under Ubuntu

CLOCK_MONOTONIC       Clock that cannot be set and  represents  monotonic  time  since       some unspecified starting point.  CLOCK_MONOTONIC_RAW (since Linux 2.6.28; Linux-specific)       Similar  to  CLOCK_MONOTONIC, but provides access to a raw hard‐       ware-based time that is not subject to NTP adjustments. 

According to the webster online dictionary Monotonic means:

2: having the property either of never increasing or of never decreasing as the values of the independent variable or the subscripts of the terms increase.

In other words, it won't jump backwards. I can see that this would be an important property if you were timing some code.

However, the difference between the normal and raw version isn't clear. Can someone shed some light into how NTP can still affect CLOCK_MONOTONIC?

like image 768
hookenz Avatar asked Jan 11 '13 01:01

hookenz


People also ask

What is CLOCK_MONOTONIC?

CLOCK_MONOTONIC A nonsettable system-wide clock that represents monotonic time since—as described by POSIX—"some unspecified point in the past". On Linux, that point corresponds to the number of seconds that the system has been running since it was booted.

What is Clockid_t?

The type clockid_t is used for constants that indicate which of several system clocks one wishes to use. All systems that support this family of functions will define at least this clock constant: Macro: clockid_t CLOCK_REALTIME. This clock uses the POSIX epoch, 00:00:00 on January 1, 1970, Coordinated Universal Time.


1 Answers

CLOCK_MONOTONIC never experiences discontinuities due to NTP time adjustments, but it does change in frequency as NTP learns what error exists between the local oscillator and the upstream servers.

CLOCK_MONOTONIC_RAW is simply the local oscillator, not disciplined by NTP. This could be very useful if you want to implement some other time synchronization algorithm against a clock which is not fighting you due to NTP. While ntpd (the reference implementation of NTP protocol and the most widespread NTP daemon) is reputed to be "gentle" with time adjustments, it's more accurate to say it's gentle with the absolute time. It's willing to slew the clock by 500ppm which is pretty dramatic if you're in a position to measure your clock frequency against some other standard.

The utility of CLOCK_MONOTONIC_RAW is going to be limited until facilities like pthread_timedwait_monotonic offer an option to use that timebase.

like image 93
Ben Jackson Avatar answered Sep 26 '22 19:09

Ben Jackson