summaryrefslogtreecommitdiffstats
path: root/smooth.c
blob: 4c350e91c060fb301df85118c437827e55e10fd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
  chronyd/chronyc - Programs for keeping computer clocks accurate.

 **********************************************************************
 * Copyright (C) Miroslav Lichvar  2015
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 **********************************************************************

  =======================================================================

  Routines implementing time smoothing.

  */

#include "config.h"

#include "sysincl.h"

#include "conf.h"
#include "local.h"
#include "logging.h"
#include "reference.h"
#include "smooth.h"
#include "util.h"

/*
  Time smoothing determines an offset that needs to be applied to the cooked
  time to make it smooth for external observers.  Observed offset and frequency
  change slowly and there are no discontinuities.  This can be used on an NTP
  server to make it easier for the clients to track the time and keep their
  clocks close together even when large offset or frequency corrections are
  applied to the server's clock (e.g. after being offline for longer time).

  Accumulated offset and frequency are smoothed out in three stages.  In the
  first stage, the frequency is changed at a constant rate (wander) up to a
  maximum, in the second stage the frequency stays at the maximum for as long
  as needed and in the third stage the frequency is brought back to zero.

              |
    max_freq  +-------/--------\-------------
              |      /|        |\
        freq  |     / |        | \
              |    /  |        |  \
              |   /   |        |   \
           0  +--/----+--------+----\--------
              | /     |        |    |    time
              |/      |        |    |

        stage     1       2      3

  Integral of this function is the smoothed out offset.  It's a continuous
  piecewise polynomial with two quadratic parts and one linear.
*/

struct stage {
  double wander;
  double length;
};

#define NUM_STAGES 3

static struct stage stages[NUM_STAGES];

/* Enabled/disabled smoothing */
static int enabled;

/* Enabled/disabled mode where only leap seconds are smoothed out and normal
   offset/frequency changes are ignored */
static int leap_only_mode;

/* Maximum skew/max_wander ratio to start updating offset and frequency */
#define UNLOCK_SKEW_WANDER_RATIO 10000

static int locked;

/* Maximum wander and frequency offset */
static double max_wander;
static double max_freq;

/* Frequency offset, time offset and the time of the last smoothing update */
static double smooth_freq;
static double smooth_offset;
static struct timespec last_update;


static void
get_smoothing(struct timespec *now, double *poffset, double *pfreq,
              double *pwander)
{
  double elapsed, length, offset, freq, wander;
  int i;

  elapsed = UTI_DiffTimespecsToDouble(now, &last_update);

  offset = smooth_offset;
  freq = smooth_freq;
  wander = 0.0;

  for (i = 0; i < NUM_STAGES; i++) {
    if (elapsed <= 0.0)
      break;

    length = stages[i].length;
    if (length >= elapsed)
      length = elapsed;

    wander = stages[i].wander;
    offset -= length * (2.0 * freq + wander * length) / 2.0;
    freq += wander * length;
    elapsed -= length;
  }

  if (elapsed > 0.0) {
    wander = 0.0;
    offset -= elapsed * freq;
  }

  *poffset = offset;
  *pfreq = freq;
  if (pwander)
    *pwander = wander;
}

static void
update_stages(void)
{
  double s1, s2, s, l1, l2, l3, lc, f, f2, l1t[2], l3t[2], err[2];
  int i, dir;

  /* Prepare the three stages so that the integral of the frequency offset
     is equal to the offset that should be smoothed out */

  s1 = smooth_offset / max_wander;
  s2 = SQUARE(smooth_freq) / (2.0 * SQUARE(max_wander));
  
  /* Calculate the lengths of the 1st and 3rd stage assuming there is no
     frequency limit.  The direction of the 1st stage is selected so that
     the lengths will not be negative.  With extremely small offsets both
     directions may give a negative length due to numerical errors, so select
     the one which gives a smaller error. */

  for (i = 0, dir = -1; i <= 1; i++, dir += 2) {
    err[i] = 0.0;
    s = dir * s1 + s2;

    if (s < 0.0) {
      err[i] += -s;
      s = 0.0;
    }

    l3t[i] = sqrt(s);
    l1t[i] = l3t[i] - dir * smooth_freq / max_wander;

    if (l1t[i] < 0.0) {
      err[i] += l1t[i] * l1t[i];
      l1t[i] = 0.0;
    }
  }

  if (err[0] < err[1]) {
    l1 = l1t[0];
    l3 = l3t[0];
    dir = -1;
  } else {
    l1 = l1t[1];
    l3 = l3t[1];
    dir = 1;
  }

  l2 = 0.0;

  /* If the limit was reached, shorten 1st+3rd stages and set a 2nd stage */
  f = dir * smooth_freq + l1 * max_wander - max_freq;
  if (f > 0.0) {
    lc = f / max_wander;

    /* No 1st stage if the frequency is already above the maximum */
    if (lc > l1) {
      lc = l1;
      f2 = dir * smooth_freq;
    } else {
      f2 = max_freq;
    }

    l2 = lc * (2.0 + f / f2);
    l1 -= lc;
    l3 -= lc;
  }

  stages[0].wander = dir * max_wander;
  stages[0].length = l1;
  stages[1].wander = 0.0;
  stages[1].length = l2;
  stages[2].wander = -dir * max_wander;
  stages[2].length = l3;

  for (i = 0; i < NUM_STAGES; i++) {
    DEBUG_LOG("Smooth stage %d wander %e length %f",
              i + 1, stages[i].wander, stages[i].length);
  }
}

static void
update_smoothing(struct timespec *now, double offset, double freq)
{
  /* Don't accept offset/frequency until the clock has stabilized */
  if (locked) {
    if (REF_GetSkew() / max_wander < UNLOCK_SKEW_WANDER_RATIO || leap_only_mode)
      SMT_Activate(now);
    return;
  }

  get_smoothing(now, &smooth_offset, &smooth_freq, NULL);
  smooth_offset += offset;
  smooth_freq = (smooth_freq - freq) / (1.0 - freq);
  last_update = *now;

  update_stages();

  DEBUG_LOG("Smooth offset %e freq %e", smooth_offset, smooth_freq);
}

static void
handle_slew(struct timespec *raw, struct timespec *cooked, double dfreq,
            double doffset, LCL_ChangeType change_type, void *anything)
{
  double delta;

  if (change_type == LCL_ChangeAdjust) {
    if (leap_only_mode)
      update_smoothing(cooked, 0.0, 0.0);
    else
      update_smoothing(cooked, doffset, dfreq);
  }

  if (!UTI_IsZeroTimespec(&last_update))
    UTI_AdjustTimespec(&last_update, cooked, &last_update, &delta, dfreq, doffset);
}

void SMT_Initialise(void)
{
  CNF_GetSmooth(&max_freq, &max_wander, &leap_only_mode);
  if (max_freq <= 0.0 || max_wander <= 0.0) {
      enabled = 0;
      return;
  }

  enabled = 1;
  locked = 1;

  /* Convert from ppm */
  max_freq *= 1e-6;
  max_wander *= 1e-6;

  UTI_ZeroTimespec(&last_update);

  LCL_AddParameterChangeHandler(handle_slew, NULL);
}

void SMT_Finalise(void)
{
  if (!enabled)
    return;

  LCL_RemoveParameterChangeHandler(handle_slew, NULL);
}

int SMT_IsEnabled(void)
{
  return enabled;
}

double
SMT_GetOffset(struct timespec *now)
{
  double offset, freq;

  if (!enabled)
    return 0.0;
  
  get_smoothing(now, &offset, &freq, NULL);

  return offset;
}

void
SMT_Activate(struct timespec *now)
{
  if (!enabled || !locked)
    return;

  LOG(LOGS_INFO, "Time smoothing activated%s", leap_only_mode ?
      " (leap seconds only)" : "");
  locked = 0;
  last_update = *now;
}

void
SMT_Reset(struct timespec *now)
{
  int i;

  if (!enabled)
    return;

  smooth_offset = 0.0;
  smooth_freq = 0.0;
  last_update = *now;

  for (i = 0; i < NUM_STAGES; i++)
    stages[i].wander = stages[i].length = 0.0;
}

void
SMT_Leap(struct timespec *now, int leap)
{
  /* When the leap-only mode is disabled, the leap second will be accumulated
     in handle_slew() as a normal offset */
  if (!enabled || !leap_only_mode)
    return;

  update_smoothing(now, leap, 0.0);
}

int
SMT_GetSmoothingReport(RPT_SmoothingReport *report, struct timespec *now)
{
  double length, elapsed;
  int i;

  if (!enabled)
    return 0;

  report->active = !locked;
  report->leap_only = leap_only_mode;

  get_smoothing(now, &report->offset, &report->freq_ppm, &report->wander_ppm);

  /* Convert to ppm and negate (positive values mean faster/speeding up) */
  report->freq_ppm *= -1.0e6;
  report->wander_ppm *= -1.0e6;

  elapsed = UTI_DiffTimespecsToDouble(now, &last_update);
  if (!locked && elapsed >= 0.0) {
    for (i = 0, length = 0.0; i < NUM_STAGES; i++)
      length += stages[i].length;
    report->last_update_ago = elapsed;
    report->remaining_time = elapsed < length ? length - elapsed : 0.0;
  } else {
    report->last_update_ago = 0.0;
    report->remaining_time = 0.0;
  }

  return 1;
}