Re: Missing CLOCK_BOOTTIME_RAW?

From: Thomas Gleixner
Date: Fri May 08 2020 - 17:00:09 EST


Pali,

Pali RohÃr <pali@xxxxxxxxxx> writes:

> I'm looking at clocks which are provided by kernel for userspace
> applications via clock_gettime() syscall and I think that there is
> missing clock variant CLOCK_BOOTTIME_RAW.
>
> If userspace application wants to measure time difference between two
> events then I think all of available clocks CLOCK_REALTIME,
> CLOCK_MONOTONIC, CLOCK_MONOTONIC_RAW and CLOCK_BOOTTIME have some issues
> for such task.
...
> CLOCK_BOOTTIME is affected by NTP jumps but provides correct time
> difference when system was suspended during measurement.

What do you mean with NTP jumps?

Neither CLOCK_BOOTTIME nor CLOCK_MONOTONIC jump. They are frequency
corrected when NTP, PTP or PPS are in use. The frequency correction is
incremental an smothed. They really don't jump and they give you proper
time in nanoseconds which is as close to the real nanoseconds as it
gets.

CLOCK_MONOTONIC_RAW converts some assumed clock frequency into something
which looks like time. But it's raw and subject to drift. The only
reason why it's exposed is because NTP/PTP need it to calculate the
frequency difference between the hardware counter and the master clock.

> So for me it looks like that there is missing CLOCK_BOOTTIME_RAW clock
> which would not be affected by NTP jumps (like CLOCK_MONOTONIC_RAW) and
> also would not be affected by system suspend (like CLOCK_BOOTTIME).
>
> Please correct me if I'm wrong in some of my above assumptions. It is
> how I understood documentation for clock_gettime() function from Linux
> manpage.

I don't know how you read jumps into this:

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.

And CLOCK_BOOTTIME is the same except that it accounts time in suspend
while MONOTONIC stops in suspend.

> Is there any reason why kernel does not provide such CLOCK_BOOTTIME_RAW
> clock for userspace applications which would be interested in measuring
> time difference which occurred between two events?

Nobody needs it and there is even no guarantee that it can be
reconstructed on resume.

If you want accurate time deltas then use either CLOCK_MONOTONIC or
CLOCK_BOOTTIME depending on your interest in suspend time.

Thanks,

tglx