[lkml]   [2021]   [Dec]   [15]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
SubjectRe: Time keeping while suspended in the presence of persistent clock drift
On 15/12/2021 15:42:05-0700, Joel Daniels wrote:
> Measuring RTC drift is hard. The standard PC RTC has only one second
> resolution so you have to wait for the "edge" of a tick and measure
> drift over an extended period of time.

Having a one second resolution and measuring drift is orthogonal. The
proper way to read the time from an RTC is not to read its time but to
program an alarm or an update interrupt.

> If you have some NTP daemon
> slewing your system clock while you try to measure RTC drift then
> you will get garbage.

Yeah, the issue is that CLOCK_MONOTONIC is affected by adjtime.

> If your motherboard gets hot enough then your
> RTC will run at a different rate while the machine is on than while
> it is off.

Indeed, you get about 1ppm per degree Celsius on a typical quartz. the
farther away you get from 25°C, the worse it gets.
You also lose about 5ppm per year due to aging.

> I know of three programs that measure RTC drift today:
> # hwclock: you must use it to set the RTC twice, the second time
> with the "--update-drift" argument. The manual suggests waiting
> one day between calls. The drift and offset information is
> stored in /etc/adjtime. On boot "hwclock --hctosys" will use this
> to set the system clock correctly.
> # adjtimex (program not syscall) when run with the "--compare"
> option. It uses a least squares estimate from multiple samples
> which by default are each 10 seconds apart.
> # chrony with the "rtcfile" directive. It tracks the RTC over time
> to measure its offset and drift similarly to how it tracks the
> system clock drift. Tracking information is saved into
> /var/lib/chrony/rtc
> and can be used (via "chronyd -s") to set the system clock
> correctly on next boot.
> Any method of measuring the drift is going to need to persist the
> drift coefficient to disk so that it can set the system clock
> correctly on boot. I think it would be best for the kernel to use this
> same coefficient.

I usually use chrony to measure the drift and then get the RTC to
correct its own drift when it is supported by the hardware. The value is
then stored directly on the RTC and nothing more is needed.

Anything that is not using NTP will actually measure the drift
difference between the clocksource and the RTC and I've seen systems
where the RTC was drifting less than the clocksource so you may be
overcompensating in the wrong direction.

Note that on any system using systemd, the kernel hctosys will be used
to the the system time at boot so you may be already more than a second
away from the actual time.

> > Alternatively I'd go very simple and just put the correction factor in
> > a boot argument.
> This works for my use case though it won't be useful to a general
> distro. Would you have one argument being used regardless of where the
> sleep injection was coming from or would you try to tie it to the
> persistent clock and/or a specific RTC?

Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering

 \ /
  Last update: 2021-12-16 00:28    [W:0.125 / U:1.088 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site