]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/commit
x86: Speed up clock calibration
authorColin Percival <cperciva@FreeBSD.org>
Mon, 10 Jan 2022 01:22:20 +0000 (17:22 -0800)
committerColin Percival <cperciva@FreeBSD.org>
Wed, 12 Jan 2022 20:34:07 +0000 (12:34 -0800)
commitc2705ceaeb09d8579661097fd358ffb5defb5624
tree746ab22dd8ded9efcc773b7eb7aa6289c9c3bc38
parent12f747e6ff675edfc1f2f95f7fc435dc01e0c29c
x86: Speed up clock calibration

Prior to this commit, the TSC and local APIC frequencies were calibrated
at boot time by measuring the clocks before and after a one-second sleep.
This was simple and effective, but had the disadvantage of *requiring a
one-second sleep*.

Rather than making two clock measurements (before and after sleeping) we
now perform many measurements; and rather than simply subtracting the
starting count from the ending count, we calculate a best-fit regression
between the target clock and the reference clock (for which the current
best available timecounter is used). While we do this, we keep track
of an estimate of the uncertainty in the regression slope (aka. the ratio
of clock speeds), and stop measuring when we believe the uncertainty is
less than 1 PPM.

In order to avoid the risk of aliasing resulting from the data-gathering
loop synchronizing with (a multiple of) the frequency of the reference
clock, we add some additional spinning depending upon the iteration number.

For numerical stability and simplicity of implementation, we make use of
floating-point arithmetic for the statistical calculations.

On the author's Dell laptop, this reduces the time spent in calibration
from 2000 ms to 29 ms; on an EC2 c5.xlarge instance, it is reduced from
2000 ms to 2.5 ms.

Reviewed by: bde (previous version), kib
MFC after: 1 month
Sponsored by: https://www.patreon.com/cperciva
Differential Revision: https://reviews.freebsd.org/D33802
sys/conf/files.amd64
sys/conf/files.i386
sys/kern/subr_clockcalib.c [new file with mode: 0644]
sys/sys/timetc.h
sys/x86/x86/local_apic.c
sys/x86/x86/tsc.c