2 * ntp_loopfilter.c - implements the NTP loop filter algorithm
4 * ATTENTION: Get approval from Dave Mills on all changes to this file!
16 #include "ntp_unixtime.h"
17 #include "ntp_stdlib.h"
27 #include "ntp_syscall.h"
28 #endif /* KERNEL_PLL */
31 * This is an implementation of the clock discipline algorithm described
32 * in UDel TR 97-4-3, as amended. It operates as an adaptive parameter,
33 * hybrid phase/frequency-lock loop. A number of sanity checks are
34 * included to protect against timewarps, timespikes and general mayhem.
35 * All units are in s and s/s, unless noted otherwise.
37 #define CLOCK_MAX .128 /* default step threshold (s) */
38 #define CLOCK_MINSTEP 300. /* default stepout threshold (s) */
39 #define CLOCK_PANIC 1000. /* default panic threshold (s) */
40 #define CLOCK_PHI 15e-6 /* max frequency error (s/s) */
41 #define CLOCK_PLL 16. /* PLL loop gain (log2) */
42 #define CLOCK_AVG 8. /* parameter averaging constant */
43 #define CLOCK_FLL .25 /* FLL loop gain */
44 #define CLOCK_FLOOR .0005 /* startup offset floor (s) */
45 #define CLOCK_ALLAN 11 /* Allan intercept (log2 s) */
46 #define CLOCK_LIMIT 30 /* poll-adjust threshold */
47 #define CLOCK_PGATE 4. /* poll-adjust gate */
48 #define PPS_MAXAGE 120 /* kernel pps signal timeout (s) */
49 #define FREQTOD(x) ((x) / 65536e6) /* NTP to double */
50 #define DTOFREQ(x) ((int32)((x) * 65536e6)) /* double to NTP */
53 * Clock discipline state machine. This is used to control the
54 * synchronization behavior during initialization and following a
57 * State < step > step Comments
58 * ========================================================
59 * NSET FREQ step, FREQ freq not set
61 * FSET SYNC step, SYNC freq set
63 * FREQ if (mu < 900) if (mu < 900) set freq direct
66 * freq, SYNC freq, step, SYNC
68 * SYNC SYNC SPIK, ignore adjust phase/freq
70 * SPIK SYNC if (mu < 900) adjust phase/freq
75 * Kernel PLL/PPS state machine. This is used with the kernel PLL
76 * modifications described in the documentation.
78 * If kernel support for the ntp_adjtime() system call is available, the
79 * ntp_control flag is set. The ntp_enable and kern_enable flags can be
80 * set at configuration time or run time using ntpdc. If ntp_enable is
81 * false, the discipline loop is unlocked and no corrections of any kind
82 * are made. If both ntp_control and kern_enable are set, the kernel
83 * support is used as described above; if false, the kernel is bypassed
84 * entirely and the daemon discipline used instead.
86 * There have been three versions of the kernel discipline code. The
87 * first (microkernel) now in Solaris discipilnes the microseconds. The
88 * second and third (nanokernel) disciplines the clock in nanoseconds.
89 * These versions are identifed if the symbol STA_PLL is present in the
90 * header file /usr/include/sys/timex.h. The third and current version
91 * includes TAI offset and is identified by the symbol NTP_API with
94 * Each PPS time/frequency discipline can be enabled by the atom driver
95 * or another driver. If enabled, the STA_PPSTIME and STA_FREQ bits are
96 * set in the kernel status word; otherwise, these bits are cleared.
97 * These bits are also cleard if the kernel reports an error.
99 * If an external clock is present, the clock driver sets STA_CLK in the
100 * status word. When the local clock driver sees this bit, it updates
101 * via this routine, which then calls ntp_adjtime() with the STA_PLL bit
102 * set to zero, in which case the system clock is not adjusted. This is
103 * also a signal for the external clock driver to discipline the system
104 * clock. Unless specified otherwise, all times are in seconds.
107 * Program variables that can be tinkered.
109 double clock_max_back = CLOCK_MAX; /* step threshold */
110 double clock_max_fwd = CLOCK_MAX; /* step threshold */
111 double clock_minstep = CLOCK_MINSTEP; /* stepout threshold */
112 double clock_panic = CLOCK_PANIC; /* panic threshold */
113 double clock_phi = CLOCK_PHI; /* dispersion rate (s/s) */
114 u_char allan_xpt = CLOCK_ALLAN; /* Allan intercept (log2 s) */
119 static double clock_offset; /* offset */
120 double clock_jitter; /* offset jitter */
121 double drift_comp; /* frequency (s/s) */
122 static double init_drift_comp; /* initial frequency (PPM) */
123 double clock_stability; /* frequency stability (wander) (s/s) */
124 double clock_codec; /* audio codec frequency (samples/s) */
125 static u_long clock_epoch; /* last update */
126 u_int sys_tai; /* TAI offset from UTC */
127 static int loop_started; /* TRUE after LOOP_DRIFTINIT */
128 static void rstclock (int, double); /* transition function */
129 static double direct_freq(double); /* direct set frequency */
130 static void set_freq(double); /* set frequency */
132 # define PATH_MAX MAX_PATH
134 static char relative_path[PATH_MAX + 1]; /* relative path per recursive make */
135 static char *this_file = NULL;
138 static struct timex ntv; /* ntp_adjtime() parameters */
139 int pll_status; /* last kernel status bits */
140 #if defined(STA_NANO) && NTP_API == 4
141 static u_int loop_tai; /* last TAI offset */
142 #endif /* STA_NANO */
143 static void start_kern_loop(void);
144 static void stop_kern_loop(void);
145 #endif /* KERNEL_PLL */
148 * Clock state machine control flags
150 int ntp_enable = TRUE; /* clock discipline enabled */
151 int pll_control; /* kernel support available */
152 int kern_enable = TRUE; /* kernel support enabled */
153 int hardpps_enable; /* kernel PPS discipline enabled */
154 int ext_enable; /* external clock enabled */
155 int pps_stratum; /* pps stratum */
156 int kernel_status; /* from ntp_adjtime */
157 int force_step_once = FALSE; /* always step time once at startup (-G) */
158 int mode_ntpdate = FALSE; /* exit on first clock set (-q) */
159 int freq_cnt; /* initial frequency clamp */
160 int freq_set; /* initial set frequency switch */
163 * Clock state machine variables
165 int state = 0; /* clock discipline state */
166 u_char sys_poll; /* time constant/poll (log2 s) */
167 int tc_counter; /* jiggle counter */
168 double last_offset; /* last offset (s) */
171 * Huff-n'-puff filter variables
173 static double *sys_huffpuff; /* huff-n'-puff filter */
174 static int sys_hufflen; /* huff-n'-puff filter stages */
175 static int sys_huffptr; /* huff-n'-puff filter pointer */
176 static double sys_mindly; /* huff-n'-puff filter min delay */
178 #if defined(KERNEL_PLL)
179 /* Emacs cc-mode goes nuts if we split the next line... */
180 #define MOD_BITS (MOD_OFFSET | MOD_MAXERROR | MOD_ESTERROR | \
181 MOD_STATUS | MOD_TIMECONST)
183 static void pll_trap (int); /* configuration trap */
184 static struct sigaction sigsys; /* current sigaction status */
185 static struct sigaction newsigsys; /* new sigaction status */
186 static sigjmp_buf env; /* environment var. for pll_trap() */
188 #endif /* KERNEL_PLL */
191 sync_status(const char *what, int ostatus, int nstatus)
193 char obuf[256], nbuf[256], tbuf[1024];
194 #if defined(USE_SNPRINTB) && defined (STA_FMT)
195 snprintb(obuf, sizeof(obuf), STA_FMT, ostatus);
196 snprintb(nbuf, sizeof(nbuf), STA_FMT, nstatus);
198 snprintf(obuf, sizeof(obuf), "%04x", ostatus);
199 snprintf(nbuf, sizeof(nbuf), "%04x", nstatus);
201 snprintf(tbuf, sizeof(tbuf), "%s status: %s -> %s", what, obuf, nbuf);
202 report_event(EVNT_KERN, NULL, tbuf);
206 * file_name - return pointer to non-relative portion of this C file pathname
208 static char *file_name(void)
210 if (this_file == NULL) {
211 (void)strncpy(relative_path, __FILE__, PATH_MAX);
212 for (this_file=relative_path;
213 *this_file && ! isalnum((unsigned char)*this_file);
220 * init_loopfilter - initialize loop filter data
223 init_loopfilter(void)
226 * Initialize state variables.
228 sys_poll = ntp_minpoll;
229 clock_jitter = LOGTOD(sys_precision);
230 freq_cnt = (int)clock_minstep;
235 * ntp_adjtime_error_handler - process errors from ntp_adjtime
238 ntp_adjtime_error_handler(
239 const char *caller, /* name of calling function */
240 struct timex *ptimex, /* pointer to struct timex */
241 int ret, /* return value from ntp_adjtime */
242 int saved_errno, /* value of errno when ntp_adjtime returned */
243 int pps_call, /* ntp_adjtime call was PPS-related */
244 int tai_call, /* ntp_adjtime call was TAI-related */
245 int line /* line number of ntp_adjtime call */
248 char des[1024] = ""; /* Decoded Error Status */
252 switch (saved_errno) {
254 msyslog(LOG_ERR, "%s: %s line %d: invalid struct timex pointer: 0x%lx",
255 caller, file_name(), line,
256 (long)((void *)ptimex)
260 msyslog(LOG_ERR, "%s: %s line %d: invalid struct timex \"constant\" element value: %ld",
261 caller, file_name(), line,
262 (long)(ptimex->constant)
269 "%s: ntp_adjtime(TAI) failed: %m",
273 msyslog(LOG_ERR, "%s: %s line %d: ntp_adjtime: %m",
274 caller, file_name(), line
278 msyslog(LOG_NOTICE, "%s: %s line %d: unhandled errno value %d after failed ntp_adjtime call",
279 caller, file_name(), line,
286 case TIME_OK: /* 0: synchronized, no leap second warning */
287 /* msyslog(LOG_INFO, "kernel reports time is synchronized normally"); */
290 # warning TIME_OK is not defined
293 case TIME_INS: /* 1: positive leap second warning */
294 msyslog(LOG_INFO, "kernel reports leap second insertion scheduled");
297 # warning TIME_INS is not defined
300 case TIME_DEL: /* 2: negative leap second warning */
301 msyslog(LOG_INFO, "kernel reports leap second deletion scheduled");
304 # warning TIME_DEL is not defined
307 case TIME_OOP: /* 3: leap second in progress */
308 msyslog(LOG_INFO, "kernel reports leap second in progress");
311 # warning TIME_OOP is not defined
314 case TIME_WAIT: /* 4: leap second has occured */
315 msyslog(LOG_INFO, "kernel reports leap second has occurred");
318 # warning TIME_WAIT is not defined
323 from the reference implementation of ntp_gettime():
325 // Hardware or software error
326 if ((time_status & (STA_UNSYNC | STA_CLOCKERR))
329 * PPS signal lost when either time or frequency synchronization
332 || (time_status & (STA_PPSFREQ | STA_PPSTIME)
333 && !(time_status & STA_PPSSIGNAL))
336 * PPS jitter exceeded when time synchronization requested
338 || (time_status & STA_PPSTIME &&
339 time_status & STA_PPSJITTER)
342 * PPS wander exceeded or calibration error when frequency
343 * synchronization requested
345 || (time_status & STA_PPSFREQ &&
346 time_status & (STA_PPSWANDER | STA_PPSERROR)))
349 or, from ntp_adjtime():
351 if ( (time_status & (STA_UNSYNC | STA_CLOCKERR))
352 || (time_status & (STA_PPSFREQ | STA_PPSTIME)
353 && !(time_status & STA_PPSSIGNAL))
354 || (time_status & STA_PPSTIME
355 && time_status & STA_PPSJITTER)
356 || (time_status & STA_PPSFREQ
357 && time_status & (STA_PPSWANDER | STA_PPSERROR))
362 case TIME_ERROR: /* 5: unsynchronized, or loss of synchronization */
363 /* error (see status word) */
365 if (ptimex->status & STA_UNSYNC)
366 snprintf(des, sizeof(des), "%s%sClock Unsynchronized",
367 des, (*des) ? "; " : "");
369 if (ptimex->status & STA_CLOCKERR)
370 snprintf(des, sizeof(des), "%s%sClock Error",
371 des, (*des) ? "; " : "");
373 if (!(ptimex->status & STA_PPSSIGNAL)
374 && ptimex->status & STA_PPSFREQ)
375 snprintf(des, sizeof(des), "%s%sPPS Frequency Sync wanted but no PPS",
376 des, (*des) ? "; " : "");
378 if (!(ptimex->status & STA_PPSSIGNAL)
379 && ptimex->status & STA_PPSTIME)
380 snprintf(des, sizeof(des), "%s%sPPS Time Sync wanted but no PPS signal",
381 des, (*des) ? "; " : "");
383 if ( ptimex->status & STA_PPSTIME
384 && ptimex->status & STA_PPSJITTER)
385 snprintf(des, sizeof(des), "%s%sPPS Time Sync wanted but PPS Jitter exceeded",
386 des, (*des) ? "; " : "");
388 if ( ptimex->status & STA_PPSFREQ
389 && ptimex->status & STA_PPSWANDER)
390 snprintf(des, sizeof(des), "%s%sPPS Frequency Sync wanted but PPS Wander exceeded",
391 des, (*des) ? "; " : "");
393 if ( ptimex->status & STA_PPSFREQ
394 && ptimex->status & STA_PPSERROR)
395 snprintf(des, sizeof(des), "%s%sPPS Frequency Sync wanted but Calibration error detected",
396 des, (*des) ? "; " : "");
398 if (pps_call && !(ptimex->status & STA_PPSSIGNAL))
399 report_event(EVNT_KERN, NULL,
401 DPRINTF(1, ("kernel loop status %#x (%s)\n",
402 ptimex->status, des));
404 * This code may be returned when ntp_adjtime() has just
405 * been called for the first time, quite a while after
406 * startup, when ntpd just starts to discipline the kernel
407 * time. In this case the occurrence of this message
408 * can be pretty confusing.
410 * HMS: How about a message when we begin kernel processing:
411 * Determining kernel clock state...
412 * so an initial TIME_ERROR message is less confising,
413 * or skipping the first message (ugh),
415 * msyslog(LOG_INFO, "kernel reports time synchronization lost");
417 msyslog(LOG_INFO, "kernel reports TIME_ERROR: %#x: %s",
418 ptimex->status, des);
421 # warning TIME_ERROR is not defined
424 msyslog(LOG_NOTICE, "%s: %s line %d: unhandled return value %d from ntp_adjtime() in %s at line %d",
425 caller, file_name(), line,
436 * local_clock - the NTP logical clock loop filter.
439 * -1 update ignored: exceeds panic threshold
440 * 0 update ignored: popcorn or exceeds step threshold
442 * 2 clock was stepped
444 * LOCKCLOCK: The only thing this routine does is set the
445 * sys_rootdisp variable equal to the peer dispersion.
449 struct peer *peer, /* synch source peer structure */
450 double fp_offset /* clock offset (s) */
453 int rval; /* return code */
454 int osys_poll; /* old system poll */
455 int ntp_adj_ret; /* returned by ntp_adjtime */
456 double mu; /* interval since last update */
457 double clock_frequency; /* clock frequency */
458 double dtemp, etemp; /* double temps */
459 char tbuf[80]; /* report buffer */
461 (void)ntp_adj_ret; /* not always used below... */
463 * If the loop is opened or the NIST LOCKCLOCK is in use,
464 * monitor and record the offsets anyway in order to determine
465 * the open-loop response and then go home.
469 #endif /* not LOCKCLOCK */
471 record_loop_stats(fp_offset, drift_comp, clock_jitter,
472 clock_stability, sys_poll);
478 * If the clock is way off, panic is declared. The clock_panic
479 * defaults to 1000 s; if set to zero, the panic will never
480 * occur. The allow_panic defaults to FALSE, so the first panic
481 * will exit. It can be set TRUE by a command line option, in
482 * which case the clock will be set anyway and time marches on.
483 * But, allow_panic will be set FALSE when the update is less
484 * than the step threshold; so, subsequent panics will exit.
486 if (fabs(fp_offset) > clock_panic && clock_panic > 0 &&
488 snprintf(tbuf, sizeof(tbuf),
489 "%+.0f s; set clock manually within %.0f s.",
490 fp_offset, clock_panic);
491 report_event(EVNT_SYSFAULT, NULL, tbuf);
498 * This section simulates ntpdate. If the offset exceeds the
499 * step threshold (128 ms), step the clock to that time and
500 * exit. Otherwise, slew the clock to that time and exit. Note
501 * that the slew will persist and eventually complete beyond the
502 * life of this program. Note that while ntpdate is active, the
503 * terminal does not detach, so the termination message prints
504 * directly to the terminal.
507 if ( ( fp_offset > clock_max_fwd && clock_max_fwd > 0)
508 || (-fp_offset > clock_max_back && clock_max_back > 0)) {
509 step_systime(fp_offset);
510 msyslog(LOG_NOTICE, "ntpd: time set %+.6f s",
512 printf("ntpd: time set %+.6fs\n", fp_offset);
514 adj_systime(fp_offset);
515 msyslog(LOG_NOTICE, "ntpd: time slew %+.6f s",
517 printf("ntpd: time slew %+.6fs\n", fp_offset);
519 record_loop_stats(fp_offset, drift_comp, clock_jitter,
520 clock_stability, sys_poll);
525 * The huff-n'-puff filter finds the lowest delay in the recent
526 * interval. This is used to correct the offset by one-half the
527 * difference between the sample delay and minimum delay. This
528 * is most effective if the delays are highly assymetric and
529 * clockhopping is avoided and the clock frequency wander is
532 if (sys_huffpuff != NULL) {
533 if (peer->delay < sys_huffpuff[sys_huffptr])
534 sys_huffpuff[sys_huffptr] = peer->delay;
535 if (peer->delay < sys_mindly)
536 sys_mindly = peer->delay;
538 dtemp = -(peer->delay - sys_mindly) / 2;
540 dtemp = (peer->delay - sys_mindly) / 2;
542 DPRINTF(1, ("local_clock: size %d mindly %.6f huffpuff %.6f\n",
543 sys_hufflen, sys_mindly, dtemp));
547 * Clock state machine transition function which defines how the
548 * system reacts to large phase and frequency excursion. There
549 * are two main regimes: when the offset exceeds the step
550 * threshold (128 ms) and when it does not. Under certain
551 * conditions updates are suspended until the stepout theshold
552 * (900 s) is exceeded. See the documentation on how these
553 * thresholds interact with commands and command line options.
555 * Note the kernel is disabled if step is disabled or greater
556 * than 0.5 s or in ntpdate mode.
558 osys_poll = sys_poll;
559 if (sys_poll < peer->minpoll)
560 sys_poll = peer->minpoll;
561 if (sys_poll > peer->maxpoll)
562 sys_poll = peer->maxpoll;
563 mu = current_time - clock_epoch;
564 clock_frequency = drift_comp;
566 if ( ( fp_offset > clock_max_fwd && clock_max_fwd > 0)
567 || (-fp_offset > clock_max_back && clock_max_back > 0)
568 || force_step_once ) {
569 if (force_step_once) {
570 force_step_once = FALSE; /* we want this only once after startup */
571 msyslog(LOG_NOTICE, "Doing intital time step" );
577 * In SYNC state we ignore the first outlier and switch
581 snprintf(tbuf, sizeof(tbuf), "%+.6f s",
583 report_event(EVNT_SPIK, NULL, tbuf);
588 * In FREQ state we ignore outliers and inlyers. At the
589 * first outlier after the stepout threshold, compute
590 * the apparent frequency correction and step the phase.
593 if (mu < clock_minstep)
596 clock_frequency = direct_freq(fp_offset);
598 /* fall through to EVNT_SPIK */
601 * In SPIK state we ignore succeeding outliers until
602 * either an inlyer is found or the stepout threshold is
606 if (mu < clock_minstep)
609 /* fall through to default */
612 * We get here by default in NSET and FSET states and
613 * from above in FREQ or SPIK states.
615 * In NSET state an initial frequency correction is not
616 * available, usually because the frequency file has not
617 * yet been written. Since the time is outside the step
618 * threshold, the clock is stepped. The frequency will
619 * be set directly following the stepout interval.
621 * In FSET state the initial frequency has been set from
622 * the frequency file. Since the time is outside the
623 * step threshold, the clock is stepped immediately,
624 * rather than after the stepout interval. Guys get
625 * nervous if it takes 15 minutes to set the clock for
628 * In FREQ and SPIK states the stepout threshold has
629 * expired and the phase is still above the step
630 * threshold. Note that a single spike greater than the
631 * step threshold is always suppressed, even with a
632 * long time constant.
635 snprintf(tbuf, sizeof(tbuf), "%+.6f s",
637 report_event(EVNT_CLOCKRESET, NULL, tbuf);
638 step_systime(fp_offset);
641 clock_jitter = LOGTOD(sys_precision);
643 if (state == EVNT_NSET) {
644 rstclock(EVNT_FREQ, 0);
649 rstclock(EVNT_SYNC, 0);
652 * The offset is less than the step threshold. Calculate
653 * the jitter as the exponentially weighted offset
656 etemp = SQUARE(clock_jitter);
657 dtemp = SQUARE(max(fabs(fp_offset - last_offset),
658 LOGTOD(sys_precision)));
659 clock_jitter = SQRT(etemp + (dtemp - etemp) /
664 * In NSET state this is the first update received and
665 * the frequency has not been initialized. Adjust the
666 * phase, but do not adjust the frequency until after
667 * the stepout threshold.
670 adj_systime(fp_offset);
671 rstclock(EVNT_FREQ, fp_offset);
675 * In FREQ state ignore updates until the stepout
676 * threshold. After that, compute the new frequency, but
677 * do not adjust the frequency until the holdoff counter
678 * decrements to zero.
681 if (mu < clock_minstep)
684 clock_frequency = direct_freq(fp_offset);
688 * We get here by default in FSET, SPIK and SYNC states.
689 * Here compute the frequency update due to PLL and FLL
690 * contributions. Note, we avoid frequency discipline at
691 * startup until the initial transient has subsided.
697 * The FLL and PLL frequency gain constants
698 * depend on the time constant and Allan
699 * intercept. The PLL is always used, but
700 * becomes ineffective above the Allan intercept
701 * where the FLL becomes effective.
703 if (sys_poll >= allan_xpt)
705 (fp_offset - clock_offset)
706 / ( max(ULOGTOD(sys_poll), mu)
710 * The PLL frequency gain (numerator) depends on
711 * the minimum of the update interval and Allan
712 * intercept. This reduces the PLL gain when the
713 * FLL becomes effective.
715 etemp = min(ULOGTOD(allan_xpt), mu);
716 dtemp = 4 * CLOCK_PLL * ULOGTOD(sys_poll);
718 fp_offset * etemp / (dtemp * dtemp);
720 rstclock(EVNT_SYNC, fp_offset);
721 if (fabs(fp_offset) < CLOCK_FLOOR)
729 * This code segment works when clock adjustments are made using
730 * precision time kernel support and the ntp_adjtime() system
731 * call. This support is available in Solaris 2.6 and later,
732 * Digital Unix 4.0 and later, FreeBSD, Linux and specially
733 * modified kernels for HP-UX 9 and Ultrix 4. In the case of the
734 * DECstation 5000/240 and Alpha AXP, additional kernel
735 * modifications provide a true microsecond clock and nanosecond
736 * clock, respectively.
738 * Important note: The kernel discipline is used only if the
739 * step threshold is less than 0.5 s, as anything higher can
740 * lead to overflow problems. This might occur if some misguided
741 * lad set the step threshold to something ridiculous.
743 if (pll_control && kern_enable && freq_cnt == 0) {
746 * We initialize the structure for the ntp_adjtime()
747 * system call. We have to convert everything to
748 * microseconds or nanoseconds first. Do not update the
749 * system variables if the ext_enable flag is set. In
750 * this case, the external clock driver will update the
751 * variables, which will be read later by the local
752 * clock driver. Afterwards, remember the time and
753 * frequency offsets for jitter and stability values and
754 * to update the frequency file.
758 ntv.modes = MOD_STATUS;
761 ntv.modes = MOD_BITS | MOD_NANO;
763 ntv.modes = MOD_BITS;
764 #endif /* STA_NANO */
765 if (clock_offset < 0)
770 ntv.offset = (int32)(clock_offset * 1e9 +
772 ntv.constant = sys_poll;
774 ntv.offset = (int32)(clock_offset * 1e6 +
776 ntv.constant = sys_poll - 4;
777 #endif /* STA_NANO */
778 if (ntv.constant < 0)
781 ntv.esterror = (u_int32)(clock_jitter * 1e6);
782 ntv.maxerror = (u_int32)((sys_rootdelay / 2 +
783 sys_rootdisp) * 1e6);
784 ntv.status = STA_PLL;
787 * Enable/disable the PPS if requested.
789 if (hardpps_enable) {
790 ntv.status |= (STA_PPSTIME | STA_PPSFREQ);
791 if (!(pll_status & STA_PPSTIME))
792 sync_status("PPS enabled",
796 ntv.status &= ~(STA_PPSTIME | STA_PPSFREQ);
797 if (pll_status & STA_PPSTIME)
798 sync_status("PPS disabled",
802 if (sys_leap == LEAP_ADDSECOND)
803 ntv.status |= STA_INS;
804 else if (sys_leap == LEAP_DELSECOND)
805 ntv.status |= STA_DEL;
809 * Pass the stuff to the kernel. If it squeals, turn off
810 * the pps. In any case, fetch the kernel offset,
811 * frequency and jitter.
813 ntp_adj_ret = ntp_adjtime(&ntv);
815 * A squeal is a return status < 0, or a state change.
817 if ((0 > ntp_adj_ret) || (ntp_adj_ret != kernel_status)) {
818 kernel_status = ntp_adj_ret;
819 ntp_adjtime_error_handler(__func__, &ntv, ntp_adj_ret, errno, hardpps_enable, 0, __LINE__ - 1);
821 pll_status = ntv.status;
823 clock_offset = ntv.offset / 1e9;
825 clock_offset = ntv.offset / 1e6;
826 #endif /* STA_NANO */
827 clock_frequency = FREQTOD(ntv.freq);
830 * If the kernel PPS is lit, monitor its performance.
832 if (ntv.status & STA_PPSTIME) {
834 clock_jitter = ntv.jitter / 1e9;
836 clock_jitter = ntv.jitter / 1e6;
837 #endif /* STA_NANO */
840 #if defined(STA_NANO) && NTP_API == 4
842 * If the TAI changes, update the kernel TAI.
844 if (loop_tai != sys_tai) {
847 ntv.constant = sys_tai;
848 if ((ntp_adj_ret = ntp_adjtime(&ntv)) != 0) {
849 ntp_adjtime_error_handler(__func__, &ntv, ntp_adj_ret, errno, 0, 1, __LINE__ - 1);
852 #endif /* STA_NANO */
854 #endif /* KERNEL_PLL */
857 * Clamp the frequency within the tolerance range and calculate
858 * the frequency difference since the last update.
860 if (fabs(clock_frequency) > NTP_MAXFREQ)
862 "frequency error %.0f PPM exceeds tolerance %.0f PPM",
863 clock_frequency * 1e6, NTP_MAXFREQ * 1e6);
864 dtemp = SQUARE(clock_frequency - drift_comp);
865 if (clock_frequency > NTP_MAXFREQ)
866 drift_comp = NTP_MAXFREQ;
867 else if (clock_frequency < -NTP_MAXFREQ)
868 drift_comp = -NTP_MAXFREQ;
870 drift_comp = clock_frequency;
873 * Calculate the wander as the exponentially weighted RMS
874 * frequency differences. Record the change for the frequency
877 etemp = SQUARE(clock_stability);
878 clock_stability = SQRT(etemp + (dtemp - etemp) / CLOCK_AVG);
881 * Here we adjust the time constant by comparing the current
882 * offset with the clock jitter. If the offset is less than the
883 * clock jitter times a constant, then the averaging interval is
884 * increased, otherwise it is decreased. A bit of hysteresis
885 * helps calm the dance. Works best using burst mode. Don't
886 * fiddle with the poll during the startup clamp period.
890 } else if (fabs(clock_offset) < CLOCK_PGATE * clock_jitter) {
891 tc_counter += sys_poll;
892 if (tc_counter > CLOCK_LIMIT) {
893 tc_counter = CLOCK_LIMIT;
894 if (sys_poll < peer->maxpoll) {
900 tc_counter -= sys_poll << 1;
901 if (tc_counter < -CLOCK_LIMIT) {
902 tc_counter = -CLOCK_LIMIT;
903 if (sys_poll > peer->minpoll) {
911 * If the time constant has changed, update the poll variables.
913 if (osys_poll != sys_poll)
914 poll_update(peer, sys_poll);
917 * Yibbidy, yibbbidy, yibbidy; that'h all folks.
919 record_loop_stats(clock_offset, drift_comp, clock_jitter,
920 clock_stability, sys_poll);
921 DPRINTF(1, ("local_clock: offset %.9f jit %.9f freq %.3f stab %.3f poll %d\n",
922 clock_offset, clock_jitter, drift_comp * 1e6,
923 clock_stability * 1e6, sys_poll));
925 #endif /* not LOCKCLOCK */
930 * adj_host_clock - Called once every second to update the local clock.
932 * LOCKCLOCK: The only thing this routine does is increment the
933 * sys_rootdisp variable.
944 * Update the dispersion since the last update. In contrast to
945 * NTPv3, NTPv4 does not declare unsynchronized after one day,
946 * since the dispersion check serves this function. Also,
947 * since the poll interval can exceed one day, the old test
948 * would be counterproductive. During the startup clamp period, the
949 * time constant is clamped at 2.
951 sys_rootdisp += clock_phi;
953 if (!ntp_enable || mode_ntpdate)
956 * Determine the phase adjustment. The gain factor (denominator)
957 * increases with poll interval, so is dominated by the FLL
958 * above the Allan intercept. Note the reduced time constant at
961 if (state != EVNT_SYNC) {
963 } else if (freq_cnt > 0) {
964 offset_adj = clock_offset / (CLOCK_PLL * ULOGTOD(1));
967 } else if (pll_control && kern_enable) {
969 #endif /* KERNEL_PLL */
971 offset_adj = clock_offset / (CLOCK_PLL * ULOGTOD(sys_poll));
975 * If the kernel discipline is enabled the frequency correction
976 * drift_comp has already been engaged via ntp_adjtime() in
977 * set_freq(). Otherwise it is a component of the adj_systime()
981 if (pll_control && kern_enable)
984 #endif /* KERNEL_PLL */
985 freq_adj = drift_comp;
987 /* Bound absolute value of total adjustment to NTP_MAXFREQ. */
988 if (offset_adj + freq_adj > NTP_MAXFREQ)
989 offset_adj = NTP_MAXFREQ - freq_adj;
990 else if (offset_adj + freq_adj < -NTP_MAXFREQ)
991 offset_adj = -NTP_MAXFREQ - freq_adj;
993 clock_offset -= offset_adj;
995 * Windows port adj_systime() must be called each second,
996 * even if the argument is zero, to ease emulation of
997 * adjtime() using Windows' slew API which controls the rate
998 * but does not automatically stop slewing when an offset
999 * has decayed to zero.
1001 DEBUG_INSIST(enable_panic_check == TRUE);
1002 enable_panic_check = FALSE;
1003 adj_systime(offset_adj + freq_adj);
1004 enable_panic_check = TRUE;
1005 #endif /* LOCKCLOCK */
1010 * Clock state machine. Enter new state and set state variables.
1014 int trans, /* new state */
1015 double offset /* new offset */
1018 DPRINTF(2, ("rstclock: mu %lu state %d poll %d count %d\n",
1019 current_time - clock_epoch, trans, sys_poll,
1021 if (trans != state && trans != EVNT_FSET)
1022 report_event(trans, NULL, NULL);
1024 last_offset = clock_offset = offset;
1025 clock_epoch = current_time;
1030 * calc_freq - calculate frequency directly
1032 * This is very carefully done. When the offset is first computed at the
1033 * first update, a residual frequency component results. Subsequently,
1034 * updates are suppresed until the end of the measurement interval while
1035 * the offset is amortized. At the end of the interval the frequency is
1036 * calculated from the current offset, residual offset, length of the
1037 * interval and residual frequency component. At the same time the
1038 * frequenchy file is armed for update at the next hourly stats.
1045 set_freq(fp_offset / (current_time - clock_epoch));
1052 * set_freq - set clock frequency correction
1054 * Used to step the frequency correction at startup, possibly again once
1055 * the frequency is measured (that is, transitioning from EVNT_NSET to
1056 * EVNT_FSET), and finally to switch between daemon and kernel loop
1057 * discipline at runtime.
1059 * When the kernel loop discipline is available but the daemon loop is
1060 * in use, the kernel frequency correction is disabled (set to 0) to
1061 * ensure drift_comp is applied by only one of the loops.
1065 double freq /* frequency update */
1068 const char * loop_desc;
1071 (void)ntp_adj_ret; /* not always used below... */
1077 ntv.modes = MOD_FREQUENCY;
1079 loop_desc = "kernel";
1080 ntv.freq = DTOFREQ(drift_comp);
1082 if ((ntp_adj_ret = ntp_adjtime(&ntv)) != 0) {
1083 ntp_adjtime_error_handler(__func__, &ntv, ntp_adj_ret, errno, 0, 0, __LINE__ - 1);
1086 #endif /* KERNEL_PLL */
1087 mprintf_event(EVNT_FSET, NULL, "%s %.3f PPM", loop_desc,
1094 start_kern_loop(void)
1096 static int atexit_done;
1101 ntv.modes = MOD_BITS;
1102 ntv.status = STA_PLL;
1103 ntv.maxerror = MAXDISPERSE;
1104 ntv.esterror = MAXDISPERSE;
1105 ntv.constant = sys_poll; /* why is it that here constant is unconditionally set to sys_poll, whereas elsewhere is is modified depending on nanosecond vs. microsecond kernel? */
1108 * Use sigsetjmp() to save state and then call ntp_adjtime(); if
1109 * it fails, then pll_trap() will set pll_control FALSE before
1110 * returning control using siglogjmp().
1112 newsigsys.sa_handler = pll_trap;
1113 newsigsys.sa_flags = 0;
1114 if (sigaction(SIGSYS, &newsigsys, &sigsys)) {
1115 msyslog(LOG_ERR, "sigaction() trap SIGSYS: %m");
1116 pll_control = FALSE;
1118 if (sigsetjmp(env, 1) == 0) {
1119 if ((ntp_adj_ret = ntp_adjtime(&ntv)) != 0) {
1120 ntp_adjtime_error_handler(__func__, &ntv, ntp_adj_ret, errno, 0, 0, __LINE__ - 1);
1123 if (sigaction(SIGSYS, &sigsys, NULL)) {
1125 "sigaction() restore SIGSYS: %m");
1126 pll_control = FALSE;
1130 if ((ntp_adj_ret = ntp_adjtime(&ntv)) != 0) {
1131 ntp_adjtime_error_handler(__func__, &ntv, ntp_adj_ret, errno, 0, 0, __LINE__ - 1);
1136 * Save the result status and light up an external clock
1139 pll_status = ntv.status;
1143 atexit(&stop_kern_loop);
1146 if (pll_status & STA_CLK)
1148 #endif /* STA_NANO */
1149 report_event(EVNT_KERN, NULL,
1150 "kernel time sync enabled");
1153 #endif /* KERNEL_PLL */
1158 stop_kern_loop(void)
1160 if (pll_control && kern_enable)
1161 report_event(EVNT_KERN, NULL,
1162 "kernel time sync disabled");
1164 #endif /* KERNEL_PLL */
1168 * select_loop() - choose kernel or daemon loop discipline.
1175 if (kern_enable == use_kern_loop)
1178 if (pll_control && !use_kern_loop)
1181 kern_enable = use_kern_loop;
1183 if (pll_control && use_kern_loop)
1187 * If this loop selection change occurs after initial startup,
1188 * call set_freq() to switch the frequency compensation to or
1189 * from the kernel loop.
1192 if (pll_control && loop_started)
1193 set_freq(drift_comp);
1199 * huff-n'-puff filter
1206 if (sys_huffpuff == NULL)
1209 sys_huffptr = (sys_huffptr + 1) % sys_hufflen;
1210 sys_huffpuff[sys_huffptr] = 1e9;
1212 for (i = 0; i < sys_hufflen; i++) {
1213 if (sys_huffpuff[i] < sys_mindly)
1214 sys_mindly = sys_huffpuff[i];
1220 * loop_config - configure the loop filter
1222 * LOCKCLOCK: The LOOP_DRIFTINIT and LOOP_DRIFTCOMP cases are no-ops.
1233 DPRINTF(2, ("loop_config: item %d freq %f\n", item, freq));
1237 * We first assume the kernel supports the ntp_adjtime()
1238 * syscall. If that syscall works, initialize the kernel time
1239 * variables. Otherwise, continue leaving no harm behind.
1241 case LOOP_DRIFTINIT:
1248 #endif /* KERNEL_PLL */
1251 * Initialize frequency if given; otherwise, begin frequency
1252 * calibration phase.
1254 ftemp = init_drift_comp / 1e6;
1255 if (ftemp > NTP_MAXFREQ)
1256 ftemp = NTP_MAXFREQ;
1257 else if (ftemp < -NTP_MAXFREQ)
1258 ftemp = -NTP_MAXFREQ;
1261 rstclock(EVNT_FSET, 0);
1263 rstclock(EVNT_NSET, 0);
1264 loop_started = TRUE;
1265 #endif /* LOCKCLOCK */
1268 case LOOP_KERN_CLEAR:
1269 #if 0 /* XXX: needs more review, and how can we get here? */
1272 if (pll_control && kern_enable) {
1273 memset((char *)&ntv, 0, sizeof(ntv));
1274 ntv.modes = MOD_STATUS;
1275 ntv.status = STA_UNSYNC;
1277 sync_status("kernel time sync disabled",
1281 # endif /* KERNEL_PLL */
1282 #endif /* LOCKCLOCK */
1287 * Tinker command variables for Ulrich Windl. Very dangerous.
1289 case LOOP_ALLAN: /* Allan intercept (log2) (allan) */
1290 allan_xpt = (u_char)freq;
1293 case LOOP_CODEC: /* audio codec frequency (codec) */
1294 clock_codec = freq / 1e6;
1297 case LOOP_PHI: /* dispersion threshold (dispersion) */
1298 clock_phi = freq / 1e6;
1301 case LOOP_FREQ: /* initial frequency (freq) */
1302 init_drift_comp = freq;
1306 case LOOP_HUFFPUFF: /* huff-n'-puff length (huffpuff) */
1307 if (freq < HUFFPUFF)
1309 sys_hufflen = (int)(freq / HUFFPUFF);
1310 sys_huffpuff = eallocarray(sys_hufflen, sizeof(sys_huffpuff[0]));
1311 for (i = 0; i < sys_hufflen; i++)
1312 sys_huffpuff[i] = 1e9;
1316 case LOOP_PANIC: /* panic threshold (panic) */
1320 case LOOP_MAX: /* step threshold (step) */
1321 clock_max_fwd = clock_max_back = freq;
1322 if (freq == 0 || freq > 0.5)
1326 case LOOP_MAX_BACK: /* step threshold (step) */
1327 clock_max_back = freq;
1329 * Leave using the kernel discipline code unless both
1330 * limits are massive. This assumes the reason to stop
1331 * using it is that it's pointless, not that it goes wrong.
1333 if ( (clock_max_back == 0 || clock_max_back > 0.5)
1334 || (clock_max_fwd == 0 || clock_max_fwd > 0.5))
1338 case LOOP_MAX_FWD: /* step threshold (step) */
1339 clock_max_fwd = freq;
1340 if ( (clock_max_back == 0 || clock_max_back > 0.5)
1341 || (clock_max_fwd == 0 || clock_max_fwd > 0.5))
1345 case LOOP_MINSTEP: /* stepout threshold (stepout) */
1346 if (freq < CLOCK_MINSTEP)
1347 clock_minstep = CLOCK_MINSTEP;
1349 clock_minstep = freq;
1352 case LOOP_TICK: /* tick increment (tick) */
1353 set_sys_tick_precision(freq);
1356 case LOOP_LEAP: /* not used, fall through */
1359 "loop_config: unsupported option %d", item);
1364 #if defined(KERNEL_PLL) && defined(SIGSYS)
1366 * _trap - trap processor for undefined syscalls
1368 * This nugget is called by the kernel when the SYS_ntp_adjtime()
1369 * syscall bombs because the silly thing has not been implemented in
1370 * the kernel. In this case the phase-lock loop is emulated by
1371 * the stock adjtime() syscall and a lot of indelicate abuse.
1378 pll_control = FALSE;
1381 #endif /* KERNEL_PLL && SIGSYS */