2 * systime -- routines to fiddle a UNIX clock.
4 * ATTENTION: Get approval from Dave Mills on all changes to this file!
7 #include "ntp_machine.h"
9 #include "ntp_syslog.h"
10 #include "ntp_unixtime.h"
11 #include "ntp_stdlib.h"
12 #include "ntp_random.h"
13 #include "ntpd.h" /* for sys_precision */
19 #ifdef HAVE_SYS_PARAM_H
20 # include <sys/param.h>
24 #endif /* HAVE_UTMP_H */
27 #endif /* HAVE_UTMPX_H */
30 * These routines (get_systime, step_systime, adj_systime) implement an
31 * interface between the system independent NTP clock and the Unix
32 * system clock in various architectures and operating systems.
34 * Time is a precious quantity in these routines and every effort is
35 * made to minimize errors by always rounding toward zero and amortizing
36 * adjustment residues. By default the adjustment quantum is 1 us for
37 * the usual Unix tickadj() system call, but this can be increased if
38 * necessary by the tick configuration command. For instance, when the
39 * adjtime() quantum is a clock tick for a 100-Hz clock, the quantum
42 #if defined RELIANTUNIX_CLOCK || defined SCO5_CLOCK
43 double sys_tick = 10e-3; /* 10 ms tickadj() */
45 double sys_tick = 1e-6; /* 1 us tickadj() */
47 double sys_residual = 0; /* adjustment residue (s) */
52 * get_systime - return system time in NTP timestamp format.
56 l_fp *now /* system time */
61 #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_GETCLOCK)
62 struct timespec ts; /* seconds and nanoseconds */
65 * Convert Unix clock from seconds and nanoseconds to seconds.
66 * The bottom is only two bits down, so no need for fuzz.
67 * Some systems don't have that level of precision, however...
69 # ifdef HAVE_CLOCK_GETTIME
70 clock_gettime(CLOCK_REALTIME, &ts);
72 getclock(TIMEOFDAY, &ts);
74 now->l_i = ts.tv_sec + JAN_1970;
75 dtemp = ts.tv_nsec / 1e9;
77 #else /* HAVE_CLOCK_GETTIME || HAVE_GETCLOCK */
78 struct timeval tv; /* seconds and microseconds */
81 * Convert Unix clock from seconds and microseconds to seconds.
82 * Add in unbiased random fuzz beneath the microsecond.
84 GETTIMEOFDAY(&tv, NULL);
85 now->l_i = tv.tv_sec + JAN_1970;
86 dtemp = tv.tv_usec / 1e6;
88 #endif /* HAVE_CLOCK_GETTIME || HAVE_GETCLOCK */
91 * ntp_random() produces 31 bits (always nonnegative).
92 * This bit is done only after the precision has been
95 if (sys_precision != 0)
96 dtemp += (ntp_random() / FRAC - .5) / (1 <<
100 * Renormalize to seconds past 1900 and fraction.
102 dtemp += sys_residual;
106 } else if (dtemp < 0) {
111 now->l_uf = (u_int32)dtemp;
116 * adj_systime - adjust system time by the argument.
118 #if !defined SYS_WINNT
119 int /* 0 okay, 1 error */
121 double now /* adjustment (s) */
124 struct timeval adjtv; /* new adjustment */
125 struct timeval oadjtv; /* residual adjustment */
131 * Most Unix adjtime() implementations adjust the system clock
132 * in microsecond quanta, but some adjust in 10-ms quanta. We
133 * carefully round the adjustment to the nearest quantum, then
134 * adjust in quanta and keep the residue for later.
136 dtemp = now + sys_residual;
141 adjtv.tv_sec = (long)dtemp;
142 dtemp -= adjtv.tv_sec;
143 ticks = (long)(dtemp / sys_tick + .5);
144 adjtv.tv_usec = (long)(ticks * sys_tick * 1e6);
145 dtemp -= adjtv.tv_usec / 1e6;
146 sys_residual = dtemp;
149 * Convert to signed seconds and microseconds for the Unix
150 * adjtime() system call. Note we purposely lose the adjtime()
154 adjtv.tv_sec = -adjtv.tv_sec;
155 adjtv.tv_usec = -adjtv.tv_usec;
156 sys_residual = -sys_residual;
158 if (adjtv.tv_sec != 0 || adjtv.tv_usec != 0) {
159 if (adjtime(&adjtv, &oadjtv) < 0) {
160 msyslog(LOG_ERR, "adj_systime: %m");
170 * step_systime - step the system clock.
177 struct timeval timetv, adjtv, oldtimetv;
180 #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_GETCLOCK)
184 dtemp = sys_residual + now;
188 adjtv.tv_sec = (int32)dtemp;
189 adjtv.tv_usec = (u_int32)((dtemp -
190 (double)adjtv.tv_sec) * 1e6 + .5);
192 adjtv.tv_sec = (int32)dtemp;
193 adjtv.tv_usec = (u_int32)((dtemp -
194 (double)adjtv.tv_sec) * 1e6 + .5);
196 #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_GETCLOCK)
197 # ifdef HAVE_CLOCK_GETTIME
198 (void) clock_gettime(CLOCK_REALTIME, &ts);
200 (void) getclock(TIMEOFDAY, &ts);
202 timetv.tv_sec = ts.tv_sec;
203 timetv.tv_usec = ts.tv_nsec / 1000;
204 #else /* not HAVE_GETCLOCK */
205 (void) GETTIMEOFDAY(&timetv, (struct timezone *)0);
206 #endif /* not HAVE_GETCLOCK */
212 printf("step_systime: step %.6f residual %.6f\n", now, sys_residual);
215 timetv.tv_sec -= adjtv.tv_sec;
216 timetv.tv_usec -= adjtv.tv_usec;
217 if (timetv.tv_usec < 0) {
219 timetv.tv_usec += 1000000;
222 timetv.tv_sec += adjtv.tv_sec;
223 timetv.tv_usec += adjtv.tv_usec;
224 if (timetv.tv_usec >= 1000000) {
226 timetv.tv_usec -= 1000000;
229 if (ntp_set_tod(&timetv, NULL) != 0) {
230 msyslog(LOG_ERR, "step-systime: %m");
235 #ifdef NEED_HPUX_ADJTIME
237 * CHECKME: is this correct when called by ntpdate?????
243 * FreeBSD, for example, has:
245 * char ut_line[UT_LINESIZE];
246 * char ut_name[UT_NAMESIZE];
247 * char ut_host[UT_HOSTSIZE];
250 * and appends line="|", name="date", host="", time for the OLD
251 * and appends line="{", name="date", host="", time for the NEW
254 * Some OSes have utmp, some have utmpx.
258 * Write old and new time entries in utmp and wtmp if step
259 * adjustment is greater than one second.
261 * This might become even Uglier...
263 if (oldtimetv.tv_sec != timetv.tv_sec)
273 memset((char *)&ut, 0, sizeof(ut));
276 memset((char *)&utx, 0, sizeof(utx));
282 # ifdef HAVE_PUTUTLINE
283 ut.ut_type = OLD_TIME;
284 (void)strcpy(ut.ut_line, OTIME_MSG);
285 ut.ut_time = oldtimetv.tv_sec;
288 ut.ut_type = NEW_TIME;
289 (void)strcpy(ut.ut_line, NTIME_MSG);
290 ut.ut_time = timetv.tv_sec;
293 # else /* not HAVE_PUTUTLINE */
294 # endif /* not HAVE_PUTUTLINE */
295 #endif /* UPDATE_UTMP */
300 # ifdef HAVE_PUTUTXLINE
301 utx.ut_type = OLD_TIME;
302 (void)strcpy(utx.ut_line, OTIME_MSG);
303 utx.ut_tv = oldtimetv;
306 utx.ut_type = NEW_TIME;
307 (void)strcpy(utx.ut_line, NTIME_MSG);
311 # else /* not HAVE_PUTUTXLINE */
312 # endif /* not HAVE_PUTUTXLINE */
313 #endif /* UPDATE_UTMPX */
318 # ifdef HAVE_PUTUTLINE
320 ut.ut_type = OLD_TIME;
321 (void)strcpy(ut.ut_line, OTIME_MSG);
322 ut.ut_time = oldtimetv.tv_sec;
324 ut.ut_type = NEW_TIME;
325 (void)strcpy(ut.ut_line, NTIME_MSG);
326 ut.ut_time = timetv.tv_sec;
329 # else /* not HAVE_PUTUTLINE */
330 # endif /* not HAVE_PUTUTLINE */
331 #endif /* UPDATE_WTMP */
336 # ifdef HAVE_PUTUTXLINE
337 utx.ut_type = OLD_TIME;
338 utx.ut_tv = oldtimetv;
339 (void)strcpy(utx.ut_line, OTIME_MSG);
340 # ifdef HAVE_UPDWTMPX
341 updwtmpx(WTMPX_FILE, &utx);
342 # else /* not HAVE_UPDWTMPX */
343 # endif /* not HAVE_UPDWTMPX */
344 # else /* not HAVE_PUTUTXLINE */
345 # endif /* not HAVE_PUTUTXLINE */
346 # ifdef HAVE_PUTUTXLINE
347 utx.ut_type = NEW_TIME;
349 (void)strcpy(utx.ut_line, NTIME_MSG);
350 # ifdef HAVE_UPDWTMPX
351 updwtmpx(WTMPX_FILE, &utx);
352 # else /* not HAVE_UPDWTMPX */
353 # endif /* not HAVE_UPDWTMPX */
354 # else /* not HAVE_PUTUTXLINE */
355 # endif /* not HAVE_PUTUTXLINE */
356 #endif /* UPDATE_WTMPX */
364 * Clock routines for the simulator - Harish Nair, with help
367 * get_systime - return the system time in NTP timestamp format
371 l_fp *now /* current system time in l_fp */ )
374 * To fool the code that determines the local clock precision,
375 * we advance the clock a minimum of 200 nanoseconds on every
376 * clock read. This is appropriate for a typical modern machine
377 * with nanosecond clocks. Note we make no attempt here to
378 * simulate reading error, since the error is so small. This may
379 * change when the need comes to implement picosecond clocks.
381 if (ntp_node.ntp_time == ntp_node.last_time)
382 ntp_node.ntp_time += 200e-9;
383 ntp_node.last_time = ntp_node.ntp_time;
384 DTOLFP(ntp_node.ntp_time, now);
389 * adj_systime - advance or retard the system clock exactly like the
392 int /* always succeeds */
394 double now /* time adjustment (s) */
397 struct timeval adjtv; /* new adjustment */
403 * Most Unix adjtime() implementations adjust the system clock
404 * in microsecond quanta, but some adjust in 10-ms quanta. We
405 * carefully round the adjustment to the nearest quantum, then
406 * adjust in quanta and keep the residue for later.
408 dtemp = now + sys_residual;
413 adjtv.tv_sec = (long)dtemp;
414 dtemp -= adjtv.tv_sec;
415 ticks = (long)(dtemp / sys_tick + .5);
416 adjtv.tv_usec = (long)(ticks * sys_tick * 1e6);
417 dtemp -= adjtv.tv_usec / 1e6;
418 sys_residual = dtemp;
421 * Convert to signed seconds and microseconds for the Unix
422 * adjtime() system call. Note we purposely lose the adjtime()
426 adjtv.tv_sec = -adjtv.tv_sec;
427 adjtv.tv_usec = -adjtv.tv_usec;
428 sys_residual = -sys_residual;
436 * step_systime - step the system clock. We are religious here.
438 int /* always succeeds */
440 double now /* step adjustment (s) */
445 printf("step_systime: time %.6f adj %.6f\n",
446 ntp_node.ntp_time, now);
448 ntp_node.ntp_time += now;
453 * node_clock - update the clocks
455 int /* always succeeds */
457 Node *n, /* global node pointer */
458 double t /* node time */
464 * Advance client clock (ntp_time). Advance server clock
465 * (clk_time) adjusted for systematic and random frequency
466 * errors. The random error is a random walk computed as the
467 * integral of samples from a Gaussian distribution.
469 dtemp = t - n->ntp_time;
471 n->ntp_time += dtemp;
472 n->ferr += gauss(0, dtemp * n->fnse);
473 n->clk_time += dtemp * (1 + n->ferr);
476 * Perform the adjtime() function. If the adjustment completed
477 * in the previous interval, amortize the entire amount; if not,
478 * carry the leftover to the next interval.
481 if (dtemp < fabs(n->adj)) {
484 n->ntp_time -= dtemp;
487 n->ntp_time += dtemp;
490 n->ntp_time += n->adj;
498 * gauss() - returns samples from a gaussion distribution
500 double /* Gaussian sample */
502 double m, /* sample mean */
503 double s /* sample standard deviation (sigma) */
509 * Roll a sample from a Gaussian distribution with mean m and
510 * standard deviation s. For m = 0, s = 1, mean(y) = 0,
515 while ((q1 = drand48()) == 0);
517 return (m + s * sqrt(-2. * log(q1)) * cos(2. * PI * q2));
522 * poisson() - returns samples from a network delay distribution
524 double /* delay sample (s) */
526 double m, /* fixed propagation delay (s) */
527 double s /* exponential parameter (mu) */
533 * Roll a sample from a composite distribution with propagation
534 * delay m and exponential distribution time with parameter s.
535 * For m = 0, s = 1, mean(y) = std(y) = 1.
539 while ((q1 = drand48()) == 0);
540 return (m - s * log(q1 * s));