2 * Copyright (c) 2022 Colin Percival
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/timetc.h>
30 #include <sys/tslog.h>
31 #include <machine/cpu.h>
34 * clockcalib(clk, clkname):
35 * Return the frequency of the provided timer, as calibrated against the
36 * current best-available timecounter.
39 clockcalib(uint64_t (*clk)(void), const char *clkname)
41 struct timecounter *tc = atomic_load_ptr(&timecounter);
42 uint64_t clk0, clk1, clk_delay, n, passes = 0;
43 uint64_t t0, t1, tadj, tlast;
55 * The idea here is to compute a best-fit linear regression between
56 * the clock we're calibrating and the reference clock; the slope of
57 * that line multiplied by the frequency of the reference clock gives
58 * us the frequency we're looking for.
60 * To do this, we calculate the
61 * (a) mean of the target clock measurements,
62 * (b) variance of the target clock measurements,
63 * (c) mean of the reference clock measurements,
64 * (d) variance of the reference clock measurements, and
65 * (e) covariance of the target clock and reference clock measurements
66 * on an ongoing basis, updating all five values after each new data
67 * point arrives, stopping when we're confident that we've accurately
68 * measured the target clock frequency.
70 * Given those five values, the important formulas to remember from
71 * introductory statistics are:
72 * 1. slope of regression line = covariance(x, y) / variance(x)
73 * 2. (relative uncertainty in slope)^2 =
74 * (variance(x) * variance(y) - covariance(x, y)^2)
75 * ------------------------------------------------
76 * covariance(x, y)^2 * (N - 2)
78 * We adjust the second formula slightly, adding a term to each of
79 * the variance values to reflect the measurement quantization.
81 * Finally, we need to determine when to stop gathering data. We
82 * can't simply stop as soon as the computed uncertainty estimate
83 * is below our threshold; this would make us overconfident since it
84 * would introduce a multiple-comparisons problem (cf. sequential
85 * analysis in clinical trials). Instead, we stop with N data points
86 * if the estimated uncertainty of the first k data points meets our
87 * target for all N/2 < k <= N; this is not theoretically optimal,
88 * but in practice works well enough.
92 * Initial values for clocks; we'll subtract these off from values
93 * we measure later in order to reduce floating-point rounding errors.
94 * We keep track of an adjustment for values read from the reference
95 * timecounter, since it can wrap.
98 t0 = tc->tc_get_timecount(tc) & tc->tc_counter_mask;
102 /* Loop until we give up or decide that we're calibrated. */
104 /* Get a new data point. */
106 t1 = tc->tc_get_timecount(tc) & tc->tc_counter_mask;
107 while (t1 + tadj < tlast)
108 tadj += (uint64_t)tc->tc_counter_mask + 1;
112 /* If we spent too long, bail. */
113 if (t1 > tc->tc_frequency) {
114 printf("Statistical %s calibration failed! "
115 "Clocks might be ticking at variable rates.\n",
117 printf("Falling back to slow %s calibration.\n",
119 freq = (double)(tc->tc_frequency) * clk1 / t1;
123 /* Precompute to save on divisions later. */
126 /* Update mean and variance of recorded TSC values. */
128 mu_clk += d1 * inv_n;
129 d2 = d1 * (clk1 - mu_clk);
130 va_clk += (d2 - va_clk) * inv_n;
132 /* Update mean and variance of recorded time values. */
135 d2 = d1 * (t1 - mu_t);
136 va_t += (d2 - va_t) * inv_n;
138 /* Update covariance. */
139 d2 = d1 * (clk1 - mu_clk);
140 cva += (d2 - cva) * inv_n;
143 * Count low-uncertainty iterations. This is a rearrangement
144 * of "relative uncertainty < 1 PPM" avoiding division.
146 #define TSC_PPM_UNCERTAINTY 1
147 #define TSC_UNCERTAINTY TSC_PPM_UNCERTAINTY * 0.000001
148 #define TSC_UNCERTAINTY_SQR TSC_UNCERTAINTY * TSC_UNCERTAINTY
149 if (TSC_UNCERTAINTY_SQR * (n - 2) * cva * cva >
150 (va_t + 4) * (va_clk + 4) - cva * cva)
155 /* Break if we're consistently certain. */
156 if (passes * 2 > n) {
157 freq = (double)(tc->tc_frequency) * cva / va_t;
159 printf("Statistical %s calibration took"
160 " %lu us and %lu data points\n",
161 clkname, (unsigned long)(t1 *
162 1000000.0 / tc->tc_frequency),
168 * Add variable delay to avoid theoretical risk of aliasing
169 * resulting from this loop synchronizing with the frequency
170 * of the reference clock. On the nth iteration, we spend
171 * O(1 / n) time here -- long enough to avoid aliasing, but
172 * short enough to be insignificant as n grows.
174 clk_delay = clk() + (clk() - clk0) / (n * n);
175 while (clk() < clk_delay)
176 cpu_spinwait(); /* Do nothing. */