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"
17 #ifdef HAVE_SYS_PARAM_H
18 # include <sys/param.h>
22 #endif /* HAVE_UTMP_H */
25 #endif /* HAVE_UTMPX_H */
28 * These routines (get_systime, step_systime, adj_systime) implement an
29 * interface between the system independent NTP clock and the Unix
30 * system clock in various architectures and operating systems.
32 * Time is a precious quantity in these routines and every effort is
33 * made to minimize errors by always rounding toward zero and amortizing
34 * adjustment residues. By default the adjustment quantum is 1 us for
35 * the usual Unix tickadj() system call, but this can be increased if
36 * necessary by a configuration command. For instance, when the
37 * adjtime() quantum is a clock tick for a 100-Hz clock, the quantum
40 double sys_tick = 1e-6; /* tickadj() quantum (s) */
41 double sys_residual = 0; /* adjustment residue (s) */
46 * get_systime - return system time in NTP timestamp format.
50 l_fp *now /* system time */
55 #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_GETCLOCK)
56 struct timespec ts; /* seconds and nanoseconds */
59 * Convert Unix clock from seconds and nanoseconds to seconds.
61 # ifdef HAVE_CLOCK_GETTIME
62 clock_gettime(CLOCK_REALTIME, &ts);
64 getclock(TIMEOFDAY, &ts);
66 now->l_i = ts.tv_sec + JAN_1970;
67 dtemp = ts.tv_nsec / 1e9;
69 #else /* HAVE_CLOCK_GETTIME || HAVE_GETCLOCK */
70 struct timeval tv; /* seconds and microseconds */
73 * Convert Unix clock from seconds and microseconds to seconds.
75 GETTIMEOFDAY(&tv, NULL);
76 now->l_i = tv.tv_sec + JAN_1970;
77 dtemp = tv.tv_usec / 1e6;
79 #endif /* HAVE_CLOCK_GETTIME || HAVE_GETCLOCK */
82 * Renormalize to seconds past 1900 and fraction.
84 dtemp += sys_residual;
88 } else if (dtemp < -1) {
93 now->l_uf = (u_int32)dtemp;
98 * adj_systime - adjust system time by the argument.
100 #if !defined SYS_WINNT
101 int /* 0 okay, 1 error */
103 double now /* adjustment (s) */
106 struct timeval adjtv; /* new adjustment */
107 struct timeval oadjtv; /* residual adjustment */
113 * Most Unix adjtime() implementations adjust the system clock
114 * in microsecond quanta, but some adjust in 10-ms quanta. We
115 * carefully round the adjustment to the nearest quantum, then
116 * adjust in quanta and keep the residue for later.
118 dtemp = now + sys_residual;
123 adjtv.tv_sec = (long)dtemp;
124 dtemp -= adjtv.tv_sec;
125 ticks = (long)(dtemp / sys_tick + .5);
126 adjtv.tv_usec = (long)(ticks * sys_tick * 1e6);
127 dtemp -= adjtv.tv_usec / 1e6;
128 sys_residual = dtemp;
131 * Convert to signed seconds and microseconds for the Unix
132 * adjtime() system call. Note we purposely lose the adjtime()
136 adjtv.tv_sec = -adjtv.tv_sec;
137 adjtv.tv_usec = -adjtv.tv_usec;
139 if (adjtime(&adjtv, &oadjtv) < 0) {
140 msyslog(LOG_ERR, "adj_systime: %m");
149 * step_systime - step the system clock.
156 struct timeval timetv, adjtv, oldtimetv;
159 #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_GETCLOCK)
163 dtemp = sys_residual + now;
167 adjtv.tv_sec = (int32)dtemp;
168 adjtv.tv_usec = (u_int32)((dtemp -
169 (double)adjtv.tv_sec) * 1e6 + .5);
171 adjtv.tv_sec = (int32)dtemp;
172 adjtv.tv_usec = (u_int32)((dtemp -
173 (double)adjtv.tv_sec) * 1e6 + .5);
175 #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_GETCLOCK)
176 #ifdef HAVE_CLOCK_GETTIME
177 (void) clock_gettime(CLOCK_REALTIME, &ts);
179 (void) getclock(TIMEOFDAY, &ts);
181 timetv.tv_sec = ts.tv_sec;
182 timetv.tv_usec = ts.tv_nsec / 1000;
183 #else /* not HAVE_GETCLOCK */
184 (void) GETTIMEOFDAY(&timetv, (struct timezone *)0);
185 #endif /* not HAVE_GETCLOCK */
191 printf("step_systime: step %.6f residual %.6f\n", now, sys_residual);
194 timetv.tv_sec -= adjtv.tv_sec;
195 timetv.tv_usec -= adjtv.tv_usec;
196 if (timetv.tv_usec < 0) {
198 timetv.tv_usec += 1000000;
201 timetv.tv_sec += adjtv.tv_sec;
202 timetv.tv_usec += adjtv.tv_usec;
203 if (timetv.tv_usec >= 1000000) {
205 timetv.tv_usec -= 1000000;
208 if (ntp_set_tod(&timetv, NULL) != 0) {
209 msyslog(LOG_ERR, "step-systime: %m");
214 #ifdef NEED_HPUX_ADJTIME
216 * CHECKME: is this correct when called by ntpdate?????
222 * FreeBSD, for example, has:
224 * char ut_line[UT_LINESIZE];
225 * char ut_name[UT_NAMESIZE];
226 * char ut_host[UT_HOSTSIZE];
229 * and appends line="|", name="date", host="", time for the OLD
230 * and appends line="{", name="date", host="", time for the NEW
233 * Some OSes have utmp, some have utmpx.
237 * Write old and new time entries in utmp and wtmp if step
238 * adjustment is greater than one second.
240 * This might become even Uglier...
242 if (oldtimetv.tv_sec != timetv.tv_sec)
252 memset((char *)&ut, 0, sizeof(ut));
255 memset((char *)&utx, 0, sizeof(utx));
261 # ifdef HAVE_PUTUTLINE
262 ut.ut_type = OLD_TIME;
263 (void)strcpy(ut.ut_line, OTIME_MSG);
264 ut.ut_time = oldtimetv.tv_sec;
267 ut.ut_type = NEW_TIME;
268 (void)strcpy(ut.ut_line, NTIME_MSG);
269 ut.ut_time = timetv.tv_sec;
272 # else /* not HAVE_PUTUTLINE */
273 # endif /* not HAVE_PUTUTLINE */
274 #endif /* UPDATE_UTMP */
279 # ifdef HAVE_PUTUTXLINE
280 utx.ut_type = OLD_TIME;
281 (void)strcpy(utx.ut_line, OTIME_MSG);
282 utx.ut_tv = oldtimetv;
285 utx.ut_type = NEW_TIME;
286 (void)strcpy(utx.ut_line, NTIME_MSG);
290 # else /* not HAVE_PUTUTXLINE */
291 # endif /* not HAVE_PUTUTXLINE */
292 #endif /* UPDATE_UTMPX */
297 # ifdef HAVE_PUTUTLINE
299 ut.ut_type = OLD_TIME;
300 (void)strcpy(ut.ut_line, OTIME_MSG);
301 ut.ut_time = oldtimetv.tv_sec;
303 ut.ut_type = NEW_TIME;
304 (void)strcpy(ut.ut_line, NTIME_MSG);
305 ut.ut_time = timetv.tv_sec;
308 # else /* not HAVE_PUTUTLINE */
309 # endif /* not HAVE_PUTUTLINE */
310 #endif /* UPDATE_WTMP */
315 # ifdef HAVE_PUTUTXLINE
316 utx.ut_type = OLD_TIME;
317 utx.ut_tv = oldtimetv;
318 (void)strcpy(utx.ut_line, OTIME_MSG);
319 # ifdef HAVE_UPDWTMPX
320 updwtmpx(WTMPX_FILE, &utx);
321 # else /* not HAVE_UPDWTMPX */
322 # endif /* not HAVE_UPDWTMPX */
323 # else /* not HAVE_PUTUTXLINE */
324 # endif /* not HAVE_PUTUTXLINE */
325 # ifdef HAVE_PUTUTXLINE
326 utx.ut_type = NEW_TIME;
328 (void)strcpy(utx.ut_line, NTIME_MSG);
329 # ifdef HAVE_UPDWTMPX
330 updwtmpx(WTMPX_FILE, &utx);
331 # else /* not HAVE_UPDWTMPX */
332 # endif /* not HAVE_UPDWTMPX */
333 # else /* not HAVE_PUTUTXLINE */
334 # endif /* not HAVE_PUTUTXLINE */
335 #endif /* UPDATE_WTMPX */
343 * Clock routines for the simulator - Harish Nair, with help
346 * get_systime - return the system time in NTP timestamp format
350 l_fp *now /* current system time in l_fp */ )
353 * To fool the code that determines the local clock precision,
354 * we advance the clock a minimum of 200 nanoseconds on every
355 * clock read. This is appropriate for a typical modern machine
356 * with nanosecond clocks. Note we make no attempt here to
357 * simulate reading error, since the error is so small. This may
358 * change when the need comes to implement picosecond clocks.
360 if (ntp_node.ntp_time == ntp_node.last_time)
361 ntp_node.ntp_time += 200e-9;
362 ntp_node.last_time = ntp_node.ntp_time;
363 DTOLFP(ntp_node.ntp_time, now);
368 * adj_systime - advance or retard the system clock exactly like the
371 int /* always succeeds */
373 double now /* time adjustment (s) */
376 struct timeval adjtv; /* new adjustment */
382 * Most Unix adjtime() implementations adjust the system clock
383 * in microsecond quanta, but some adjust in 10-ms quanta. We
384 * carefully round the adjustment to the nearest quantum, then
385 * adjust in quanta and keep the residue for later.
387 dtemp = now + sys_residual;
392 adjtv.tv_sec = (long)dtemp;
393 dtemp -= adjtv.tv_sec;
394 ticks = (long)(dtemp / sys_tick + .5);
395 adjtv.tv_usec = (long)(ticks * sys_tick * 1e6);
396 dtemp -= adjtv.tv_usec / 1e6;
397 sys_residual = dtemp;
400 * Convert to signed seconds and microseconds for the Unix
401 * adjtime() system call. Note we purposely lose the adjtime()
405 adjtv.tv_sec = -adjtv.tv_sec;
406 adjtv.tv_usec = -adjtv.tv_usec;
407 sys_residual = -sys_residual;
411 * We went to all the trouble just to be sure the emulation is
412 * precise. We now return to our regularly scheduled concert.
414 ntp_node.clk_time -= adjtv.tv_sec + adjtv.tv_usec / 1e6;
420 * step_systime - step the system clock. We are religious here.
422 int /* always succeeds */
424 double now /* step adjustment (s) */
432 * node_clock - update the clocks
434 int /* always succeeds */
436 Node *n, /* global node pointer */
437 double t /* node time */
443 * Advance client clock (ntp_time). Advance server clock
444 * (clk_time) adjusted for systematic and random frequency
445 * errors. The random error is a random walk computed as the
446 * integral of samples from a Gaussian distribution.
448 dtemp = t - n->ntp_time;
450 n->ntp_time += dtemp;
451 n->ferr += gauss(0, dtemp * n->fnse);
452 n->clk_time += dtemp * (1 + n->ferr);
455 * Perform the adjtime() function. If the adjustment completed
456 * in the previous interval, amortize the entire amount; if not,
457 * carry the leftover to the next interval.
460 if (dtemp < fabs(n->adj)) {
463 n->ntp_time -= dtemp;
466 n->ntp_time += dtemp;
469 n->ntp_time += n->adj;
477 * gauss() - returns samples from a gaussion distribution
479 double /* Gaussian sample */
481 double m, /* sample mean */
482 double s /* sample standard deviation (sigma) */
488 * Roll a sample from a Gaussian distribution with mean m and
489 * standard deviation s. For m = 0, s = 1, mean(y) = 0,
494 while ((q1 = drand48()) == 0);
496 return (m + s * sqrt(-2. * log(q1)) * cos(2. * PI * q2));
501 * poisson() - returns samples from a network delay distribution
503 double /* delay sample (s) */
505 double m, /* fixed propagation delay (s) */
506 double s /* exponential parameter (mu) */
512 * Roll a sample from a composite distribution with propagation
513 * delay m and exponential distribution time with parameter s.
514 * For m = 0, s = 1, mean(y) = std(y) = 1.
518 while ((q1 = drand48()) == 0);
519 return (m - s * log(q1 * s));