2 chronyd/chronyc - Programs for keeping computer clocks accurate.
4 **********************************************************************
5 * Copyright (C) Miroslav Lichvar 2015
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 **********************************************************************
22 =======================================================================
24 Routines implementing time smoothing.
35 #include "reference.h"
40 Time smoothing determines an offset that needs to be applied to the cooked
41 time to make it smooth for external observers. Observed offset and frequency
42 change slowly and there are no discontinuities. This can be used on an NTP
43 server to make it easier for the clients to track the time and keep their
44 clocks close together even when large offset or frequency corrections are
45 applied to the server's clock (e.g. after being offline for longer time).
47 Accumulated offset and frequency are smoothed out in three stages. In the
48 first stage, the frequency is changed at a constant rate (wander) up to a
49 maximum, in the second stage the frequency stays at the maximum for as long
50 as needed and in the third stage the frequency is brought back to zero.
53 max_freq +-------/--------\-------------
58 0 +--/----+--------+----\--------
64 Integral of this function is the smoothed out offset. It's a continuous
65 piecewise polynomial with two quadratic parts and one linear.
75 static struct stage stages
[NUM_STAGES
];
77 /* Enabled/disabled smoothing */
80 /* Enabled/disabled mode where only leap seconds are smoothed out and normal
81 offset/frequency changes are ignored */
82 static int leap_only_mode
;
84 /* Maximum skew/max_wander ratio to start updating offset and frequency */
85 #define UNLOCK_SKEW_WANDER_RATIO 10000
89 /* Maximum wander and frequency offset */
90 static double max_wander
;
91 static double max_freq
;
93 /* Frequency offset, time offset and the time of the last smoothing update */
94 static double smooth_freq
;
95 static double smooth_offset
;
96 static struct timespec last_update
;
100 get_smoothing(struct timespec
*now
, double *poffset
, double *pfreq
,
103 double elapsed
, length
, offset
, freq
, wander
;
106 elapsed
= UTI_DiffTimespecsToDouble(now
, &last_update
);
108 offset
= smooth_offset
;
112 for (i
= 0; i
< NUM_STAGES
; i
++) {
116 length
= stages
[i
].length
;
117 if (length
>= elapsed
)
120 wander
= stages
[i
].wander
;
121 offset
-= length
* (2.0 * freq
+ wander
* length
) / 2.0;
122 freq
+= wander
* length
;
128 offset
-= elapsed
* freq
;
140 double s1
, s2
, s
, l1
, l2
, l3
, lc
, f
, f2
, l1t
[2], l3t
[2], err
[2];
143 /* Prepare the three stages so that the integral of the frequency offset
144 is equal to the offset that should be smoothed out */
146 s1
= smooth_offset
/ max_wander
;
147 s2
= SQUARE(smooth_freq
) / (2.0 * SQUARE(max_wander
));
149 /* Calculate the lengths of the 1st and 3rd stage assuming there is no
150 frequency limit. The direction of the 1st stage is selected so that
151 the lengths will not be negative. With extremely small offsets both
152 directions may give a negative length due to numerical errors, so select
153 the one which gives a smaller error. */
155 for (i
= 0, dir
= -1; i
<= 1; i
++, dir
+= 2) {
165 l1t
[i
] = l3t
[i
] - dir
* smooth_freq
/ max_wander
;
168 err
[i
] += l1t
[i
] * l1t
[i
];
173 if (err
[0] < err
[1]) {
185 /* If the limit was reached, shorten 1st+3rd stages and set a 2nd stage */
186 f
= dir
* smooth_freq
+ l1
* max_wander
- max_freq
;
190 /* No 1st stage if the frequency is already above the maximum */
193 f2
= dir
* smooth_freq
;
198 l2
= lc
* (2.0 + f
/ f2
);
203 stages
[0].wander
= dir
* max_wander
;
204 stages
[0].length
= l1
;
205 stages
[1].wander
= 0.0;
206 stages
[1].length
= l2
;
207 stages
[2].wander
= -dir
* max_wander
;
208 stages
[2].length
= l3
;
210 for (i
= 0; i
< NUM_STAGES
; i
++) {
211 DEBUG_LOG("Smooth stage %d wander %e length %f",
212 i
+ 1, stages
[i
].wander
, stages
[i
].length
);
217 update_smoothing(struct timespec
*now
, double offset
, double freq
)
219 /* Don't accept offset/frequency until the clock has stabilized */
221 if (REF_GetSkew() / max_wander
< UNLOCK_SKEW_WANDER_RATIO
|| leap_only_mode
)
226 get_smoothing(now
, &smooth_offset
, &smooth_freq
, NULL
);
227 smooth_offset
+= offset
;
228 smooth_freq
= (smooth_freq
- freq
) / (1.0 - freq
);
233 DEBUG_LOG("Smooth offset %e freq %e", smooth_offset
, smooth_freq
);
237 handle_slew(struct timespec
*raw
, struct timespec
*cooked
, double dfreq
,
238 double doffset
, LCL_ChangeType change_type
, void *anything
)
242 if (change_type
== LCL_ChangeAdjust
) {
244 update_smoothing(cooked
, 0.0, 0.0);
246 update_smoothing(cooked
, doffset
, dfreq
);
249 if (!UTI_IsZeroTimespec(&last_update
))
250 UTI_AdjustTimespec(&last_update
, cooked
, &last_update
, &delta
, dfreq
, doffset
);
253 void SMT_Initialise(void)
255 CNF_GetSmooth(&max_freq
, &max_wander
, &leap_only_mode
);
256 if (max_freq
<= 0.0 || max_wander
<= 0.0) {
264 /* Convert from ppm */
268 UTI_ZeroTimespec(&last_update
);
270 LCL_AddParameterChangeHandler(handle_slew
, NULL
);
273 void SMT_Finalise(void)
278 LCL_RemoveParameterChangeHandler(handle_slew
, NULL
);
281 int SMT_IsEnabled(void)
287 SMT_GetOffset(struct timespec
*now
)
294 get_smoothing(now
, &offset
, &freq
, NULL
);
300 SMT_Activate(struct timespec
*now
)
302 if (!enabled
|| !locked
)
305 LOG(LOGS_INFO
, "Time smoothing activated%s", leap_only_mode
?
306 " (leap seconds only)" : "");
312 SMT_Reset(struct timespec
*now
)
323 for (i
= 0; i
< NUM_STAGES
; i
++)
324 stages
[i
].wander
= stages
[i
].length
= 0.0;
328 SMT_Leap(struct timespec
*now
, int leap
)
330 /* When the leap-only mode is disabled, the leap second will be accumulated
331 in handle_slew() as a normal offset */
332 if (!enabled
|| !leap_only_mode
)
335 update_smoothing(now
, leap
, 0.0);
339 SMT_GetSmoothingReport(RPT_SmoothingReport
*report
, struct timespec
*now
)
341 double length
, elapsed
;
347 report
->active
= !locked
;
348 report
->leap_only
= leap_only_mode
;
350 get_smoothing(now
, &report
->offset
, &report
->freq_ppm
, &report
->wander_ppm
);
352 /* Convert to ppm and negate (positive values mean faster/speeding up) */
353 report
->freq_ppm
*= -1.0e6
;
354 report
->wander_ppm
*= -1.0e6
;
356 elapsed
= UTI_DiffTimespecsToDouble(now
, &last_update
);
357 if (!locked
&& elapsed
>= 0.0) {
358 for (i
= 0, length
= 0.0; i
< NUM_STAGES
; i
++)
359 length
+= stages
[i
].length
;
360 report
->last_update_ago
= elapsed
;
361 report
->remaining_time
= elapsed
< length
? length
- elapsed
: 0.0;
363 report
->last_update_ago
= 0.0;
364 report
->remaining_time
= 0.0;