-
Notifications
You must be signed in to change notification settings - Fork 12
/
clock.cc
407 lines (344 loc) · 9.5 KB
/
clock.cc
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* Copyright (C) 2010 Miroslav Lichvar <[email protected]>
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "clock.h"
#define MINSEC 256
#define MAXSEC 2048
#define MAXTIMECONST 10
#define MAXMAXERROR 16000000
#define SHIFT_FLL 2
#define SCALE_FREQ 65536.0e6
#define MAXFREQ_SCALED 32768000
#define MAX_SLEWRATE 500
#define MAX_TICK(base_tick) ((base_tick) * 11 / 10)
#define MIN_TICK(base_tick) ((base_tick) * 9 / 10)
#define MIN_FREQ 0.8
#define MAX_FREQ 1.2
Clock::Clock() {
time = 0.0;
mono_time = 0.0;
freq = 1.0;
freq_generator = NULL;
step_generator = NULL;
base_tick = sysconf(_SC_CLK_TCK);
assert(base_tick > 0);
base_tick = (1000000 + base_tick / 2) / base_tick;
memset(&ntp_timex, 0, sizeof(ntp_timex));
ntp_timex.tick = base_tick;
ntp_timex.tolerance = MAXFREQ_SCALED;
ntp_timex.precision = 1;
ntp_state = TIME_OK;
/* in Linux kernel SHIFT_PLL is 2 since 2.6.31 */
ntp_shift_pll = 2;
ntp_flags = 0;
ntp_update_interval = 0;
ntp_offset = 0.0;
ntp_slew = 0.0;
ss_offset = 0;
ss_slew = 0;
}
Clock::~Clock() {
if (freq_generator)
delete freq_generator;
if (step_generator)
delete step_generator;
}
double Clock::get_real_time() const {
return time;
}
double Clock::get_monotonic_time() const {
return mono_time;
}
double Clock::get_total_freq() const {
double timex_freq, adjtime_freq;
timex_freq = (double)ntp_timex.tick / base_tick + ntp_timex.freq / SCALE_FREQ + ntp_slew;
adjtime_freq = ss_slew / 1e6;
return freq * (timex_freq + adjtime_freq);
}
double Clock::get_raw_freq() const {
double timex_freq;
timex_freq = (double)ntp_timex.tick / base_tick + ntp_timex.freq / SCALE_FREQ;
return freq * timex_freq;
}
double Clock::get_true_interval(double local_interval) const {
return local_interval / get_total_freq();
}
double Clock::get_local_interval(double true_interval) const {
return true_interval * get_total_freq();
}
void Clock::set_freq_generator(Generator *gen) {
if (freq_generator)
delete freq_generator;
freq_generator = gen;
}
void Clock::set_step_generator(Generator *gen) {
if (step_generator)
delete step_generator;
step_generator = gen;
}
void Clock::set_freq(double freq) {
this->freq = freq + 1.0;
if (!(this->freq > MIN_FREQ && this->freq < MAX_FREQ)) {
fprintf(stderr, "frequency %e outside allowed range (%.2f, %.2f)\n", this->freq - 1.0, MIN_FREQ - 1.0, MAX_FREQ - 1.0);
exit(1);
}
}
void Clock::set_time(double time) {
this->time = time;
}
void Clock::step_time(double step) {
this->time += step;
}
void Clock::set_ntp_shift_pll(int shift) {
ntp_shift_pll = shift;
}
void Clock::set_ntp_flag(int enable, int flag) {
ntp_flags &= ~flag;
if (enable)
ntp_flags |= flag;
}
void Clock::advance(double real_interval) {
double local_interval = get_local_interval(real_interval);
time += local_interval;
mono_time += local_interval;
}
void Clock::update(bool second) {
if (freq_generator)
set_freq(freq_generator->generate(NULL));
if (step_generator)
step_time(step_generator->generate(NULL));
if (!second)
return;
if (ntp_timex.status & STA_PLL) {
ntp_update_interval++;
ntp_slew = ntp_offset / (1 << (ntp_shift_pll +
ntp_timex.constant));
#if 0
if (ntp_slew > MAX_SLEWRATE / 1e6)
ntp_slew = MAX_SLEWRATE / 1e6;
else if (ntp_slew < -MAX_SLEWRATE / 1e6)
ntp_slew = -MAX_SLEWRATE / 1e6;
#endif
ntp_offset -= ntp_slew;
if (ntp_timex.status & STA_NANO)
ntp_timex.offset = ntp_offset * 1e9;
else
ntp_timex.offset = ntp_offset * 1e6;
}
if (ss_offset) {
if (ss_offset > 0) {
if (ss_offset > MAX_SLEWRATE) {
ss_slew = MAX_SLEWRATE;
ss_offset -= MAX_SLEWRATE;
} else {
ss_slew = ss_offset;
ss_offset = 0;
}
} else {
if (ss_offset < -MAX_SLEWRATE) {
ss_slew = -MAX_SLEWRATE;
ss_offset -= -MAX_SLEWRATE;
} else {
ss_slew = ss_offset;
ss_offset = 0;
}
}
} else
ss_slew = 0;
switch (ntp_state) {
case TIME_OK:
if (ntp_timex.status & STA_INS)
ntp_state = TIME_INS;
else if (ntp_timex.status & STA_DEL)
ntp_state = TIME_DEL;
break;
case TIME_INS:
if ((time_t)(time + 0.5) % (24 * 3600) <= 1) {
time -= 1.0;
ntp_timex.tai += 1.0;
ntp_state = TIME_OOP;
} else if (!(ntp_timex.status & STA_INS)) {
ntp_state = TIME_OK;
}
break;
case TIME_DEL:
if ((time_t)(time + 1.0 + 0.5) % (24 * 3600) <= 1) {
time += 1.0;
ntp_timex.tai -= 1.0;
ntp_state = TIME_WAIT;
} else if (!(ntp_timex.status & STA_DEL)) {
ntp_state = TIME_OK;
}
break;
case TIME_OOP:
ntp_state = TIME_WAIT;
break;
case TIME_WAIT:
if (!(ntp_timex.status & (STA_INS | STA_DEL)))
ntp_state = TIME_OK;
break;
default:
assert(0);
}
}
void Clock::update_ntp_offset(long offset) {
double fll_adj, pll_adj, new_offset, old_offset, tc, t;
if (ntp_timex.status & STA_FREQHOLD)
ntp_update_interval = 0;
if (ntp_timex.status & STA_NANO)
new_offset = offset / 1e9;
else
new_offset = offset / 1e6;
tc = 1 << ntp_timex.constant;
ntp_timex.offset = offset;
old_offset = ntp_offset;
ntp_offset = new_offset;
if (!(ntp_timex.status & STA_PLL))
return;
if (old_offset && ntp_update_interval >= MINSEC &&
(ntp_timex.status & STA_FLL || ntp_update_interval > MAXSEC)) {
ntp_timex.status |= STA_MODE;
if (ntp_flags & CLOCK_NTP_FLL_MODE2)
fll_adj = (new_offset - old_offset) / (ntp_update_interval * (1 << SHIFT_FLL));
else
fll_adj = new_offset / (ntp_update_interval * (1 << SHIFT_FLL));
} else {
ntp_timex.status &= ~STA_MODE;
fll_adj = 0.0;
}
if (ntp_flags & CLOCK_NTP_PLL_CLAMP) {
if (ntp_update_interval > MAXSEC)
ntp_update_interval = MAXSEC;
if (ntp_update_interval > tc * (1 << (ntp_shift_pll + 1)))
ntp_update_interval = tc * (1 << (ntp_shift_pll + 1));
}
t = 4 * (1 << ntp_shift_pll) * tc;
pll_adj = new_offset * ntp_update_interval / (t * t);
ntp_timex.freq += (fll_adj + pll_adj) * SCALE_FREQ;
if (ntp_timex.freq > MAXFREQ_SCALED)
ntp_timex.freq = MAXFREQ_SCALED;
else if (ntp_timex.freq < -MAXFREQ_SCALED)
ntp_timex.freq = -MAXFREQ_SCALED;
ntp_update_interval = 0;
}
int Clock::adjtimex(struct timex *buf) {
int r = ntp_state;
struct timex t;
if (buf->modes & ADJ_FREQUENCY) {
ntp_timex.freq = buf->freq;
if (ntp_timex.freq > MAXFREQ_SCALED)
ntp_timex.freq = MAXFREQ_SCALED;
else if (ntp_timex.freq < -MAXFREQ_SCALED)
ntp_timex.freq = -MAXFREQ_SCALED;
}
if (buf->modes & ADJ_MAXERROR)
ntp_timex.maxerror = buf->maxerror;
if (buf->modes & ADJ_STATUS) {
if ((buf->status & STA_PLL) && !(ntp_timex.status & STA_PLL))
ntp_update_interval = 0;
ntp_timex.status = buf->status & 0xff;
}
if (buf->modes & ADJ_MICRO)
ntp_timex.status &= ~STA_NANO;
if (buf->modes & ADJ_NANO)
ntp_timex.status |= STA_NANO;
if (buf->modes & ADJ_TIMECONST) {
ntp_timex.constant = buf->constant;
if (!(ntp_timex.status & STA_NANO))
ntp_timex.constant += 4;
if (ntp_timex.constant > MAXTIMECONST)
ntp_timex.constant = MAXTIMECONST;
if (ntp_timex.constant < 0)
ntp_timex.constant = 0;
}
if (buf->modes & ADJ_TICK) {
if (buf->tick > MAX_TICK(base_tick) || buf->tick < MIN_TICK(base_tick)) {
r = -1;
} else
ntp_timex.tick = buf->tick;
}
if ((buf->modes & ADJ_OFFSET_SINGLESHOT) != ADJ_OFFSET_SINGLESHOT) {
if (buf->modes & ADJ_OFFSET) {
update_ntp_offset(buf->offset);
}
}
if (buf->modes & ADJ_SETOFFSET) {
if (ntp_timex.status & STA_NANO)
time += buf->time.tv_sec + buf->time.tv_usec * 1e-9;
else
time += buf->time.tv_sec + buf->time.tv_usec * 1e-6;
ntp_timex.maxerror = MAXMAXERROR;
}
if (buf->modes & ADJ_TAI) {
ntp_timex.tai = buf->constant;
}
t = ntp_timex;
if ((buf->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) {
if ((buf->modes & ADJ_OFFSET_SS_READ) == ADJ_OFFSET_SINGLESHOT) {
t.offset = ss_offset;
ss_offset = buf->offset;
} else {
t.offset = ss_offset;
}
}
*buf = t;
return r;
}
int Clock::adjtime(const struct timeval *delta, struct timeval *olddelta) {
if (olddelta) {
olddelta->tv_sec = ss_offset / 1000000;
olddelta->tv_usec = ss_offset % 1000000;
}
if (delta)
ss_offset = delta->tv_sec * 1000000 + delta->tv_usec;
return 0;
}
Refclock::Refclock() {
time = 0.0;
offset = 0.0;
generate = false;
valid = false;
offset_generator = NULL;
}
Refclock::~Refclock() {
if (offset_generator)
delete offset_generator;
}
void Refclock::set_offset_generator(Generator *gen) {
if (offset_generator)
delete offset_generator;
offset_generator = gen;
}
void Refclock::set_generation(bool enable) {
generate = enable;
}
void Refclock::update(double time, const Clock *clock) {
if (!generate || !offset_generator)
return;
this->time = clock->get_real_time();
offset = this->time - time + offset_generator->generate(NULL);
valid = true;
}
bool Refclock::get_sample(double *time, double *offset) const {
*time = this->time;
*offset = this->offset;
return valid;
}
void Refclock::get_offsets(double *offsets, int size) {
int i;
for (i = 0; i < size; i++)
offsets[i] = offset_generator ? offset_generator->generate(NULL) : 0.0;
}