]> git.ipfire.org Git - thirdparty/chrony.git/commit
local: improve measurement of clock precision
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 8 Oct 2025 11:09:10 +0000 (13:09 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 9 Oct 2025 09:10:51 +0000 (11:10 +0200)
commit2e29935c548fe842e700ffef3ba79efc22a94a51
treec9b2b41117125a919088cfdda6f9a50d6d63cb47
parent8084961011a08a57ce09b1be4869be3296951b0c
local: improve measurement of clock precision

By default, the clock precision is set to the minimum measured time
needed to read the clock. This value is typically larger than the actual
resolution, which causes the NTP server to add more noise to NTP
timestamps than necessary. With HW timestamping and PTP corrections
enabled by the NTP-over-PTP transport that can be the limiting factor in
the stability of NTP measurements.

Try to determine the actual resolution of the clock. On non-Linux
systems use the clock_getres() function. On FreeBSD and NetBSD it seems
to provide expected values. On illumos it returns a large value (kernel
tick length?). On Linux it seems to be the internal timer resolution,
which is 1 ns with hrtimers, even when using a lower-resolution
clocksource like hpet or acpi_pm.

On Linux, try to measure the resolution as the minimum observed change
in differences between consecutive readings of the CLOCK_MONOTONIC_RAW
clock with a varying amount of busy work. Ignore 1ns changes due to
the kernel converting readings to timespec. This seems to work reliably.
In a test with the acpi_pm clocksource, differences of 3073, 3352, and
3631 ns were measured, which gives a resolution of 279 ns, matching the
clocksource frequency of ~3.58 MHz. With a tsc clocksource it gives
the minimum accepted resolution of 2 ns and with kvm-clock 10 ns.

As the final value of the precision, use the minimum value from the
measured or clock_getres() resolution and the original minimum time
needed to read the clock.
doc/chrony.conf.adoc
local.c