#
c3196306 |
|
20-Jan-2022 |
Mark Johnston <markj@FreeBSD.org> |
clockcalib: Fix an overflow bug tc_counter_mask is an unsigned int and in the TSC timecounter is equal to UINT_MAX, so the addition tc->tc_counter_mask + 1 can overflow to 0, resulting in a hang during boot. Fixes: c2705ceaeb09 ("x86: Speed up clock calibration") Reviewed by: cperciva Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D33956
|
#
c2705cea |
|
09-Jan-2022 |
Colin Percival <cperciva@FreeBSD.org> |
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
|