]> 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>
Fri, 11 Feb 2022 06:52:00 +0000 (22:52 -0800)
commitbaee6cc1814b8e851555d2caa6410eedcef2c6c8
treeb3e87516673c2dec1eb0aeda8496ab0f090aff15
parent5438c482cd6bc7145ba66202c4bf19e4ceddfdef
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
Sponsored by: https://www.patreon.com/cperciva
Differential Revision: https://reviews.freebsd.org/D33802

(cherry picked from commit c2705ceaeb09d8579661097fd358ffb5defb5624)
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