-
Notifications
You must be signed in to change notification settings - Fork 0
/
hc_clock.c
297 lines (258 loc) · 9.95 KB
/
hc_clock.c
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
/* houseclock - A simple GPS Time Server with Web console
*
* Copyright 2019, Pascal Martin
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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.
*
*
* hc_clock.c - The module that controls the system clock synchronization.
*
* This module interface with the OS to correct the indicated drift.
*
* SYNOPSYS:
*
* const char *hc_clock_help (int level)
*
* Prints the help information, two levels:
* 0: short help for one-line argument information.
* 1: multi-line description of each argument.
*
* void hc_clock_initialize (int argc, const char **argv)
*
* Reset the clock synchronization status, retrieve and store the clock
* options from the program's command line arguments.
*
* The command line options processed here are:
* -precision=<N> The clock accuracy target for synchronization (ms).
* -drift Print the measured drift (debug)
*
* void hc_clock_synchronize(const struct timeval *source,
* const struct timeval *local, int latency);
*
* Called to synchronize the local time based on a source clock.
* The local time parameter represents an estimate of the exact moment
* when the source time was received. The latency represents an estimate
* of the transmission delay, i.e. the delta between the moment the
* time was sampled at the source and the moment when it was received
* by this machine. This function calculates a drift between the two
* times and corrects the local time if needed.
*
* int hc_clock_synchronized (void)
*
* Return 1 when the local system time was synchronized with
* the source clock.
*
* void hc_clock_reference (struct timeval *reference);
* int hc_clock_dispersion (void);
*
* These two functions are intended for supporting the NTP module.
* The reference time is the time of the latest clock adjustment.
* The dispersion is the average drift from the source clock
* for the latest period. (This does not use the maximum drift
* because this is too influenced by the OS response time, which
* is unrelated to the accuracy of the local clock.)
*/
#include <time.h>
#include <errno.h>
#include "houseclock.h"
#include "hc_clock.h"
#include "hc_db.h"
#define HC_CLOCK_LEARNING_PERIOD 10
static int clockShowDrift = 0;
#define HC_CLOCK_DRIFT_DEPTH 120
static hc_clock_status *hc_clock_status_db = 0;
static int *hc_clock_drift_db = 0;
const char *hc_clock_help (int level) {
static const char *clockHelp[] = {
" [-drift] [-precision=N]",
"-drift Print the measured drift (test mode).\n"
"-precision=N: precision of the time synchronization in milliseconds.",
NULL
};
return clockHelp[level];
}
static void hc_clock_start_learning (const struct timeval *local) {
hc_clock_status_db->count = 0;
hc_clock_status_db->accumulator = 0;
hc_clock_status_db->cycle = *local;
}
void hc_clock_initialize (int argc, const char **argv) {
int i;
int precision;
const char *precision_option = "10"; // ms
clockShowDrift = 0;
for (i = 1; i < argc; ++i) {
echttp_option_match ("-precision=", argv[i], &precision_option);
clockShowDrift |= echttp_option_present ("-drift", argv[i]);
}
precision = atoi(precision_option);
i = hc_db_new (HC_CLOCK_DRIFT, sizeof(int), HC_CLOCK_DRIFT_DEPTH);
if (i != 0) {
fprintf (stderr, "[%s %d] cannot create %s: %s\n",
__FILE__, __LINE__, HC_CLOCK_DRIFT, strerror(i));
exit (1);
}
hc_clock_drift_db = (int *) hc_db_get (HC_CLOCK_DRIFT);
for (i = 0; i < HC_CLOCK_DRIFT_DEPTH; ++i) hc_clock_drift_db[i] = 0;
i = hc_db_new (HC_CLOCK_STATUS, sizeof(hc_clock_status), 1);
if (i != 0) {
fprintf (stderr, "[%s %d] cannot create %s: %s\n",
__FILE__, __LINE__, HC_CLOCK_STATUS, strerror(i));
exit (1);
}
hc_clock_status_db = (hc_clock_status *)hc_db_get (HC_CLOCK_STATUS);
hc_clock_status_db->synchronized = 0;
hc_clock_status_db->precision = precision;
hc_clock_status_db->drift = 0;
struct timeval now;
gettimeofday (&now, NULL);
hc_clock_start_learning (&now);
}
static void hc_clock_force (const struct timeval *source,
const struct timeval *local, int latency) {
struct timeval now;
struct timeval corrected = *source;
gettimeofday (&now, NULL);
// Correct the source time to adjust for the time spent since it was
// acquired, as estimated using the local clock (now).
//
corrected.tv_sec += (now.tv_sec - local->tv_sec);
corrected.tv_usec += (now.tv_usec - local->tv_usec) + (latency * 1000);
if (corrected.tv_usec > 1000000) {
corrected.tv_sec += 1;
corrected.tv_usec -= 1000000;
} else if (corrected.tv_usec < 0) {
corrected.tv_sec -= 1;
corrected.tv_usec += 1000000;
}
DEBUG {
printf ("Forcing time from %ld.%03.3d to %ld.%03.3d, "
"based on source clock %ld.%03.3d & latency %d\n",
(long)(now.tv_sec), (int)(now.tv_usec/1000),
(long)(corrected.tv_sec), (int)(corrected.tv_usec/1000),
(long)(source->tv_sec), (int)(source->tv_usec/1000), latency);
}
if (settimeofday (&corrected, NULL) != 0) {
printf ("settimeofday() error %d\n", errno);
return;
}
DEBUG {
gettimeofday (&corrected, 0);
printf ("Time set to %ld.%03.3d\n",
(long)(corrected.tv_sec), (int)(corrected.tv_usec/1000));
}
hc_clock_status_db->reference = corrected;
hc_clock_status_db->synchronized = 1;
}
static void hc_clock_adjust (time_t drift) {
struct timeval delta;
delta.tv_sec = (drift / 1000);
delta.tv_usec = (drift % 1000) * 1000;
if (delta.tv_usec < 0) {
// Per the GNU libc documentation, tv_usec must be positive, and
// microsecond time = (tv_sec * 1000000) + tv_usec.
delta.tv_sec -= 1;
delta.tv_usec += 1000000;
}
if (adjtime (&delta, NULL) != 0) {
printf ("adjtime() error %d\n", errno);
}
gettimeofday (&hc_clock_status_db->reference, NULL);
}
void hc_clock_synchronize(const struct timeval *source,
const struct timeval *local, int latency) {
static int FirstCall = 1;
if (hc_clock_drift_db == 0) return;
if (hc_clock_status_db == 0) return;
time_t drift = ((source->tv_sec - local->tv_sec) * 1000)
+ ((source->tv_usec - local->tv_usec) / 1000) + latency;
time_t absdrift = (drift < 0)? (0 - drift) : drift;
hc_clock_drift_db[source->tv_sec%HC_CLOCK_DRIFT_DEPTH] = (int)drift;
hc_clock_status_db->drift = (int)drift;
if (clockShowDrift || hc_test_mode()) {
printf ("[%d] %8.3f\n",
local->tv_sec%HC_CLOCK_DRIFT_DEPTH, drift/1000.0);
if (hc_test_mode()) {
if (absdrift < hc_clock_status_db->precision) {
hc_clock_status_db->synchronized = 1;
} else {
hc_clock_status_db->synchronized = 0;
}
return;
}
}
if (FirstCall || absdrift >= 10000) {
// Too much of a difference: force system time.
hc_clock_force (source, local, latency);
hc_clock_start_learning(source);
FirstCall = 0;
return;
}
// Accumulate an average drift, to eliminate one-time issues.
// (Do this only if the latency is greater than 0: this indicates
// a local clock source, sensitive to OS delays.)
//
hc_clock_status_db->accumulator += (int)drift;
hc_clock_status_db->count += 1;
if ((latency > 0) &&
(hc_clock_status_db->count < HC_CLOCK_LEARNING_PERIOD)) return;
// We reached the end of a learning period.
// At this point we consider only the average drift
// calculated over the past learning period.
//
drift = hc_clock_status_db->accumulator / hc_clock_status_db->count;
absdrift = (drift < 0)? (0 - drift) : drift;
hc_clock_status_db->avgdrift = (int)drift;
if (clockShowDrift)
printf ("Average drift: %d ms\n", drift);
if (absdrift < hc_clock_status_db->precision) {
DEBUG printf ("Clock is synchronized.\n");
hc_clock_status_db->synchronized = 1;
} else {
// Source and local system time have drifted apart
// by a small difference: adjust the time progressively.
//
DEBUG {
printf ("Time adjust at %ld.%3.3d (local), drift=%d ms\n",
(long)local->tv_sec, (int)local->tv_usec/1000, drift);
}
if (absdrift > 50 * hc_clock_status_db->precision) {
DEBUG printf ("Synchronization was lost.\n");
hc_clock_status_db->synchronized = 0; // Lost it, for now.
}
hc_clock_adjust (drift);
}
hc_clock_start_learning(local);
}
int hc_clock_synchronized (void) {
if (hc_clock_status_db == 0) return 0;
return hc_clock_status_db->synchronized;
}
void hc_clock_reference (struct timeval *reference) {
static struct timeval zero = {0, 0};
if (hc_clock_status_db == 0) {
*reference = zero;
return;
}
*reference = hc_clock_status_db->reference;
}
int hc_clock_dispersion (void) {
int drift;
if (hc_clock_status_db == 0) return 0;
drift = (int)(hc_clock_status_db->avgdrift);
if (drift < 0) return 0 - drift;
return drift;
}