File: manual.c

package info (click to toggle)
chrony 1.14-7
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,480 kB
  • ctags: 2,091
  • sloc: ansic: 15,266; sh: 194; makefile: 113
file content (318 lines) | stat: -rw-r--r-- 7,544 bytes parent folder | download
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
/*
  $Header: /cvs/src/chrony/manual.c,v 1.17 1999/04/19 20:23:46 richard Exp $

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

  chronyd/chronyc - Programs for keeping computer clocks accurate.

  Copyright (C) 1997-1999 Richard P. Curnow
  All rights reserved.

  For conditions of use, refer to the file LICENCE.

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

  Routines for implementing manual input of real time.

  The daemon accepts manual time input over the control connection,
  and adjusts the system time to match.  Besides this, though, it can
  determine the average rate of time loss or gain of the local system
  and adjust the frequency accordingly.

  */

#include <stddef.h>

#include "manual.h"
#include "logging.h"
#include "local.h"
#include "conf.h"
#include "util.h"
#include "ntp.h"
#include "reference.h"
#include "regress.h"

static int enabled = 0;

/* More recent samples at highest indices */
typedef struct {
  struct timeval when; /* This is our 'cooked' time */
  double orig_offset; /*+ Not modified by slew samples */
  double offset; /*+ if we are fast of the supplied reference */
  double residual; /*+ regression residual (sign convention given by
                     (measured-predicted)) */
} Sample;

#define MAX_SAMPLES 16

static Sample samples[16];
static int n_samples;

static int replace_margin;
static int error;

/* Eventually these constants need to be user-defined in conf file */
#define REPLACE_MARGIN 300
#define ERROR_MARGIN 0.2

/* ================================================== */

static void
slew_samples(struct timeval *raw,
             struct timeval *cooked, 
             double dfreq,
             double afreq,
             double doffset,
             int is_step_change,
             void *not_used);

/* ================================================== */

void
MNL_Initialise(void)
{
  if (CNF_GetManualEnabled()) {
    enabled = 1;
  } else {
    enabled = 0;
  }

  n_samples = 0;

  replace_margin = REPLACE_MARGIN;
  error = ERROR_MARGIN;

  LCL_AddParameterChangeHandler(slew_samples, NULL);

  return;
}

/* ================================================== */

void
MNL_Finalise(void)
{
  return;
}

/* ================================================== */

static void
estimate_and_set_system(struct timeval *now, int offset_provided, double offset, long *offset_cs, double *dfreq_ppm, double *new_afreq_ppm)
{
  double agos[MAX_SAMPLES], offsets[MAX_SAMPLES];
  double b0, b1;
  int n_runs, best_start; /* Unused results from regression analyser */
  int i;
  double freq = 0.0;
  double skew = 0.099999999; /* All 9's when printed to log file */
  int found_freq;
  double slew_by;

  if (n_samples > 1) {
    for (i=0; i<n_samples; i++) {
      UTI_DiffTimevalsToDouble(&agos[i], &samples[n_samples-1].when, &samples[i].when);
      offsets[i] = samples[i].offset;
    }
    
    RGR_FindBestRobustRegression(agos, offsets, n_samples,
                                 1.0e-8, /* 0.01ppm easily good enough for this! */
                                 &b0, &b1, &n_runs, &best_start);
    
    
    /* Ignore b0 from regression; treat offset as being the most
       recently entered value.  (If the administrator knows he's put
       an outlier in, he will rerun the settime operation.)   However,
       the frequency estimate comes from the regression. */
    
    freq = -b1;
    found_freq = 1;
  } else {
    if (offset_provided) {
      b0 = offset;
    } else {
      b0 = 0.0;
    }
    b1 = freq = 0.0;
    found_freq = 0;
  }

  if (offset_provided) {
    slew_by = offset;
  } else {
    slew_by = b0;
  }
  
  if (found_freq) {
    LOG(LOGS_INFO, LOGF_Manual,
        "Making a frequency change of %.3fppm and a slew of %.6f\n",
        1.0e6 * freq, slew_by);
    
    REF_SetManualReference(now,
                           slew_by,
                           freq, skew);
  } else {
    LOG(LOGS_INFO, LOGF_Manual, "Making a slew of %.6f\n", slew_by);
    REF_SetManualReference(now,
                           slew_by,
                           0.0, skew);
  }
  
  if (offset_cs) *offset_cs = (long)(0.5 + 100.0 * b0);
  if (dfreq_ppm) *dfreq_ppm = 1.0e6 * freq;
  if (new_afreq_ppm) *new_afreq_ppm = LCL_ReadAbsoluteFrequency();
  
  /* Calculate residuals to store them */
  for (i=0; i<n_samples; i++) {
    samples[i].residual = offsets[i] - (b0 + agos[i] * b1);
  }
  
}

/* ================================================== */

int
MNL_AcceptTimestamp(struct timeval *ts, long *offset_cs, double *dfreq_ppm, double *new_afreq_ppm)
{
  struct timeval now;
  double local_clock_err;
  double offset;
  int i;

  if (enabled) {

    /* Check whether timestamp is within margin of old one */
    LCL_ReadCookedTime(&now, &local_clock_err);

    UTI_DiffTimevalsToDouble(&offset, &now, ts);

    /* Check if buffer full up */
    if (n_samples == MAX_SAMPLES) {
      /* Shift samples down */
      for (i=1; i<n_samples; i++) {
        samples[i-1] = samples[i];
      }
      --n_samples;
    }
    
    samples[n_samples].when = now;
    samples[n_samples].offset = offset;
    samples[n_samples].orig_offset = offset;
    ++n_samples;

    estimate_and_set_system(&now, 1, offset, offset_cs, dfreq_ppm, new_afreq_ppm);

    return 1;

  } else {
  
    return 0;

  }
}

/* ================================================== */

static void
slew_samples(struct timeval *raw,
             struct timeval *cooked, 
             double dfreq,
             double afreq,
             double doffset,
             int is_step_change,
             void *not_used)
{
  double elapsed, delta_time;
  int i;
  for (i=0; i<n_samples; i++) {
    UTI_DiffTimevalsToDouble(&elapsed, cooked, &samples[i].when);
    delta_time = elapsed * dfreq - doffset;
    UTI_AddDoubleToTimeval(&samples[i].when, delta_time, &samples[i].when);
    samples[i].offset += delta_time;
  }
  return;
}

/* ================================================== */

void
MNL_Enable(void)
{
  enabled = 1;
}


/* ================================================== */

void
MNL_Disable(void)
{
  enabled = 0;
}

/* ================================================== */

void
MNL_Reset(void)
{
  n_samples = 0;
}

/* ================================================== */
/* Generate report data for the REQ_MANUAL_LIST command/monitoring
   protocol */

void
MNL_ReportSamples(RPT_ManualSamplesReport *report, int max, int *n)
{
  int i;

  if (n_samples > max) {
    *n = max;
  } else {
    *n = n_samples;
  }

  for (i=0; i<n_samples && i<max; i++) {
    report[i].when = samples[i].when.tv_sec;
    report[i].slewed_offset = samples[i].offset;
    report[i].orig_offset = samples[i].orig_offset;
    report[i].residual = samples[i].residual;
  }
}

/* ================================================== */
/* Delete a sample if it's within range, re-estimate the error and
   drift and apply it to the system clock. */

int
MNL_DeleteSample(int index)
{
  int i;
  struct timeval now;
  double local_clock_err;

  if ((index < 0) || (index >= n_samples)) {
    return 0;
  }

  /* Crunch the samples down onto the one being deleted */

  for (i=index; i<(n_samples-1); i++) {
    samples[i] = samples[i+1];
  }
  
  n_samples -= 1;

  /* Now re-estimate.  NULLs because we don't want the parameters back
     in this case. */
  LCL_ReadCookedTime(&now, &local_clock_err);
  estimate_and_set_system(&now, 0, 0.0, NULL, NULL, NULL);

  return 1;

}

/* ================================================== */