Deleted Added
full compact
3a4,5
> * ATTENTION: Get approval from Dave Mills on all changes to this file!
> *
35,36c37,39
< #define CLOCK_MAX .128 /* default step offset (s) */
< #define CLOCK_PANIC 1000. /* default panic offset (s) */
---
> #define CLOCK_MAX .128 /* default step threshold (s) */
> #define CLOCK_MINSTEP 900. /* default stepout threshold (s) */
> #define CLOCK_PANIC 1000. /* default panic threshold (s) */
38c41
< #define SHIFT_PLL 4 /* PLL loop gain (shift) */
---
> #define CLOCK_PLL 16. /* PLL loop gain */
41,43c44,45
< #define CLOCK_MINSEC 256. /* min FLL update interval (s) */
< #define CLOCK_MINSTEP 900. /* step-change timeout (s) */
< #define CLOCK_DAY 86400. /* one day of seconds (s) */
---
> #define CLOCK_ALLAN 1500. /* compromise Allan intercept (s) */
> #define CLOCK_DAY 86400. /* one day in seconds (s) */
46d47
< #define CLOCK_ALLAN 10 /* min Allan intercept (log2 s) */
111,112c112,114
< double clock_max = CLOCK_MAX; /* max offset before step (s) */
< double clock_panic = CLOCK_PANIC; /* max offset before panic (s) */
---
> double clock_max = CLOCK_MAX; /* step threshold (s) */
> double clock_minstep = CLOCK_MINSTEP; /* stepout threshold (s) */
> double clock_panic = CLOCK_PANIC; /* panic threshold (s) */
114,115c116
< double clock_minstep = CLOCK_MINSTEP; /* step timeout (s) */
< u_char allan_xpt = CLOCK_ALLAN; /* minimum Allan intercept (log2 s) */
---
> double allan_xpt = CLOCK_ALLAN; /* Allan intercept (s) */
118,128d118
< * Hybrid PLL/FLL parameters. These were chosen by experiment using a
< * MatLab program. The parameters were fudged to match a pure PLL at
< * poll intervals of 64 s and lower and a pure FLL at poll intervals of
< * 4096 s and higher. Between these extremes the parameters were chosen
< * as a geometric series of intervals while holding the overshoot to
< * less than 5 percent.
< */
< static double fll[] = {0., 1./64, 1./32, 1./16, 1./8, 1./4, 1.};
< static double pll[] = {1., 1.4, 2., 2.8, 4.1, 7., 12.};
<
< /*
135c125
< static void rstclock P((int, double, double)); /* transition function */
---
> static void rstclock P((int, u_long, double)); /* transition function */
152d141
< int allow_step = TRUE; /* allow step correction */
159d147
< u_char sys_minpoll = NTP_MINDPOLL; /* min sys poll interval (log2 s) */
162c150
< int tc_counter; /* poll-adjust counter */
---
> int tc_counter; /* hysteresis counter */
202a191,193
> *
> * LOCKCLOCK: The only thing this routine does is set the
> * sys_rootdispersion variable equal to the peer dispersion.
211c202
< double mu; /* interval since last update (s) */
---
> u_long mu; /* interval since last update (s) */
218d208
< int i;
227c217
< "local_clock: assocID %d off %.6f jit %.6f sta %d\n",
---
> "local_clock: assocID %d offset %.9f jitter %.9f state %d\n",
229a220,224
> #ifdef LOCKCLOCK
> sys_rootdispersion = peer->rootdispersion;
> return (0);
>
> #else /* LOCKCLOCK */
265,266c260
< if (allow_step && fabs(fp_offset) > clock_max &&
< clock_max > 0) {
---
> if (fabs(fp_offset) > clock_max && clock_max > 0) {
268,269c262
< NLOG(NLOG_SYNCEVENT|NLOG_SYSEVENT)
< msyslog(LOG_NOTICE, "time reset %.6f s",
---
> msyslog(LOG_NOTICE, "time reset %+.6f s",
271c264
< printf("ntpd: time reset %.6fs\n", fp_offset);
---
> printf("ntpd: time set %+.6fs\n", fp_offset);
274,275c267
< NLOG(NLOG_SYNCEVENT|NLOG_SYSEVENT)
< msyslog(LOG_NOTICE, "time slew %.6f s",
---
> msyslog(LOG_NOTICE, "time slew %+.6f s",
277c269
< printf("ntpd: time slew %.6fs\n", fp_offset);
---
> printf("ntpd: time slew %+.6fs\n", fp_offset);
292,294c284,289
< step_systime(fp_offset);
< NLOG(NLOG_SYNCEVENT|NLOG_SYSEVENT)
< msyslog(LOG_NOTICE, "time set %.6f s", fp_offset);
---
> if (fabs(fp_offset) > clock_max && clock_max > 0) {
> step_systime(fp_offset);
> msyslog(LOG_NOTICE, "time reset %+.6f s",
> fp_offset);
> reinit_timer();
> }
399,412c394,399
< if (allow_step) {
< step_systime(fp_offset);
< NLOG(NLOG_SYNCEVENT|NLOG_SYSEVENT)
< msyslog(LOG_NOTICE, "time reset %.6f s",
< fp_offset);
< rstclock(S_TSET, peer->epoch, 0);
< retval = 1;
< } else {
< NLOG(NLOG_SYNCEVENT|NLOG_SYSEVENT)
< msyslog(LOG_NOTICE, "time slew %.6f s",
< fp_offset);
< rstclock(S_FREQ, peer->epoch,
< fp_offset);
< }
---
> step_systime(fp_offset);
> msyslog(LOG_NOTICE, "time reset %+.6f s",
> fp_offset);
> reinit_timer();
> rstclock(S_TSET, peer->epoch, 0);
> retval = 1;
452,456c439,443
< * frequency adjustments. If the offset exceeds the
< * previous time error estimate by CLOCK_SGATE and the
< * interval since the last update is less than twice the
< * poll interval, consider the update a popcorn spike
< * and ignore it.
---
> * frequency adjustments. If the difference between the
> * last offset and the current one exceeds the jitter by
> * CLOCK_SGATE and the interval since the last update is
> * less than twice the system poll interval, consider
> * the update a popcorn spike and ignore it..
460,462c447,450
< if (fabs(fp_offset - last_offset) >
< CLOCK_SGATE * oerror && mu <
< ULOGTOD(sys_poll + 1)) {
---
> dtemp = fabs(fp_offset - last_offset);
> /*
> if (dtemp > CLOCK_SGATE * oerror && mu <
> (u_long) ULOGTOD(sys_poll + 1)) {
467,469c455
< fabs(fp_offset -
< last_offset), CLOCK_SGATE *
< oerror);
---
> dtemp, oerror);
473a460
> */
476,491c463,476
< * Compute the FLL and PLL frequency adjustments
< * conditioned on intricate weighting factors.
< * The gain factors depend on the poll interval
< * and Allan intercept. For the FLL, the
< * averaging interval is clamped to a minimum of
< * 1024 s and the gain increased in stages from
< * zero for poll intervals below half the Allan
< * intercept to unity above twice the Allan
< * intercept. For the PLL, the averaging
< * interval is clamped not to exceed the poll
< * interval. No gain factor is necessary, since
< * the frequency steering above the Allan
< * intercept is negligible. Particularly for the
< * PLL, these measures allow oversampling, but
< * not undersampling and insure stability even
< * when the rules of fair engagement are broken.
---
> * The FLL and PLL frequency gain constants
> * depend on the poll interval and Allan
> * intercept. The PLL constant is calculated
> * throughout the poll interval range, but the
> * update interval is clamped so as not to
> * exceed the poll interval. The FLL gain is
> * zero below one-half the Allan intercept and
> * unity at MAXPOLL. It decreases as 1 /
> * (MAXPOLL + 1 - poll interval) in a feeble
> * effort to match the loop stiffness to the
> * Allan wobble. Particularly for the PLL, these
> * measures allow oversampling, but not
> * undersampling and insure stability even when
> * the rules of fair engagement are broken.
493,503c478,484
< i = sys_poll - allan_xpt + 4;
< if (i < 0)
< i = 0;
< else if (i > 6)
< i = 6;
< etemp = fll[i];
< dtemp = max(mu, ULOGTOD(allan_xpt));
< flladj = (fp_offset - clock_offset) * etemp /
< (dtemp * CLOCK_FLL);
< dtemp = ULOGTOD(SHIFT_PLL + 2 + sys_poll);
< etemp = min(mu, ULOGTOD(sys_poll));
---
> if (ULOGTOD(sys_poll) > allan_xpt / 2) {
> dtemp = NTP_MAXPOLL + 1 - sys_poll;
> flladj = (fp_offset - clock_offset) /
> (max(mu, allan_xpt) * dtemp);
> }
> etemp = min(mu, (u_long)ULOGTOD(sys_poll));
> dtemp = 4 * CLOCK_PLL * ULOGTOD(sys_poll);
511c492
< #if defined(KERNEL_PLL)
---
> #ifdef KERNEL_PLL
610,611c591,593
< msyslog(LOG_ERR,
< "kernel time discipline status change %x",
---
> NLOG(NLOG_SYNCEVENT | NLOG_SYSEVENT)
> msyslog(LOG_NOTICE,
> "kernel time sync disabled %04x",
613a596,601
> } else {
> if (ntv.status != pll_status)
> NLOG(NLOG_SYNCEVENT | NLOG_SYSEVENT)
> msyslog(LOG_NOTICE,
> "kernel time sync enabled %04x",
> ntv.status);
643,645c631,633
< etemp = clock_frequency + flladj + plladj;
< drift_comp += etemp;
< if (drift_comp > NTP_MAXFREQ)
---
> dtemp = clock_frequency + flladj + plladj;
> etemp = drift_comp + dtemp;
> if (etemp > NTP_MAXFREQ)
647c635
< else if (drift_comp <= -NTP_MAXFREQ)
---
> else if (etemp <= -NTP_MAXFREQ)
649,651c637,643
< dtemp = SQUARE(clock_stability);
< etemp = SQUARE(etemp) - dtemp;
< clock_stability = SQRT(dtemp + etemp / CLOCK_AVG);
---
> else
> drift_comp = etemp;
> if (fabs(etemp) > NTP_MAXFREQ)
> NLOG(NLOG_SYNCEVENT | NLOG_SYSEVENT)
> msyslog(LOG_NOTICE,
> "frequency error %.0f PPM exceeds tolerance %.0f PPM",
> etemp * 1e6, NTP_MAXFREQ * 1e6);
652a645,648
> etemp = SQUARE(clock_stability);
> dtemp = SQUARE(dtemp);
> clock_stability = SQRT(etemp + (dtemp - etemp) / CLOCK_AVG);
>
663c659
< if (sys_jitter / ULOGTOD(sys_poll) > clock_stability &&
---
> if (sys_jitter > ULOGTOD(sys_poll) * clock_stability &&
688,689c684,686
< dtemp = peer->disp + sys_jitter;
< if ((peer->flags & FLAG_REFCLOCK) == 0 && dtemp < MINDISPERSE)
---
> dtemp = peer->disp + (current_time - peer->epoch) * clock_phi +
> sys_jitter + fabs(last_offset);
> if (!(peer->flags & FLAG_REFCLOCK) && dtemp < MINDISPERSE)
693a691
>
697,698c695,696
< "local_clock: mu %.0f noi %.3f stb %.3f pol %d cnt %d\n",
< mu, sys_jitter * 1e6, clock_stability * 1e6, sys_poll,
---
> "local_clock: mu %lu rootjit %.6f stab %.3f poll %d count %d\n",
> mu, dtemp, clock_stability * 1e6, sys_poll,
701a700
> #endif /* LOCKCLOCK */
706a706,708
> *
> * LOCKCLOCK: The only thing this routine does is increment the
> * sys_rootdispersion variable.
713,714c715
< double adjustment;
< int i;
---
> double adjustment;
727a729
> #ifndef LOCKCLOCK
734,735c736,737
< NLOG(NLOG_SYSEVENT) /* conditional if clause */
< msyslog(LOG_INFO, "pps sync disabled");
---
> NLOG(NLOG_SYNCEVENT | NLOG_SYSEVENT)
> msyslog(LOG_NOTICE, "pps sync disabled");
738,739d739
< if (!ntp_enable)
< return;
742,743c742,743
< * If the phase-lock loop is implemented in the kernel, we
< * have no business going further.
---
> * If NTP is disabled or ntpdate mode enabled or the kernel
> * discipline enabled, we have no business going further.
745c745
< if (pll_control && kern_enable)
---
> if (!ntp_enable || mode_ntpdate || (pll_control && kern_enable))
762,764c762,763
< * This ugly bit of business is necessary in order to move the
< * pole frequency higher in FLL mode. This is necessary for loop
< * stability.
---
> * Implement the phase and frequency adjustments. Note the
> * black art formerly practiced here has been whitewashed.
766,772c765
< i = sys_poll - allan_xpt + 4;
< if (i < 0)
< i = 0;
< else if (i > 6)
< i = 6;
< adjustment = clock_offset / (pll[i] * ULOGTOD(SHIFT_PLL +
< sys_poll));
---
> adjustment = clock_offset / (CLOCK_PLL * ULOGTOD(sys_poll));
774a768
> #endif /* LOCKCLOCK */
784c778
< double epoch, /* last time */
---
> u_long epoch, /* last time */
792a787,791
> #ifdef DEBUG
> if (debug)
> printf("local_clock: at %lu state %d\n", last_time,
> trans);
> #endif
817a817,818
> *
> * LOCKCLOCK: The LOOP_DRIFTINIT and LOOP_DRIFTCOMP cases are no-ops.
830a832
> #ifndef LOCKCLOCK
838a841,845
> *
> * Call out the safety patrol. If ntpdate mode or if the
> * step threshold has been changed by the -x option or
> * tinker command, kernel discipline is unsafe, so don't
> * do any of this stuff.
839a847,849
> if (mode_ntpdate || clock_max != CLOCK_MAX)
> break;
>
882,883c892,894
< msyslog(LOG_NOTICE,
< "kernel time discipline status %04x",
---
> NLOG(NLOG_SYNCEVENT | NLOG_SYSEVENT)
> msyslog(LOG_INFO,
> "kernel time sync status %04x",
886a898
> #endif /* LOCKCLOCK */
890a903
> #ifndef LOCKCLOCK
892,895c905,909
< * Initialize the kernel frequency and clamp to
< * reasonable value. Also set the initial state to
< * S_FSET to indicated the frequency has been
< * initialized from the previously saved drift file.
---
> * If the frequency value is reasonable, set the initial
> * frequency to the given value and the state to S_FSET.
> * Otherwise, the drift file may be missing or broken,
> * so set the frequency to zero. This erases past
> * history should somebody break something.
897,902c911,916
< rstclock(S_FSET, current_time, 0);
< drift_comp = freq;
< if (drift_comp > NTP_MAXFREQ)
< drift_comp = NTP_MAXFREQ;
< if (drift_comp < -NTP_MAXFREQ)
< drift_comp = -NTP_MAXFREQ;
---
> if (freq <= NTP_MAXFREQ && freq >= -NTP_MAXFREQ) {
> drift_comp = freq;
> rstclock(S_FSET, current_time, 0);
> } else {
> drift_comp = 0;
> }
923a938
> #endif /* LOCKCLOCK */
933c948
< case LOOP_PANIC: /* panic exit threshold */
---
> case LOOP_PANIC: /* panic threshold */
945,948c960,961
< case LOOP_MINPOLL: /* ephemeral association poll */
< if (freq < NTP_MINPOLL)
< freq = NTP_MINPOLL;
< sys_minpoll = (u_char)freq;
---
> case LOOP_ALLAN: /* Allan intercept */
> allan_xpt = freq;
950,955d962
<
< case LOOP_ALLAN: /* minimum Allan intercept */
< if (freq < CLOCK_ALLAN)
< freq = CLOCK_ALLAN;
< allan_xpt = (u_char)freq;
< break;
966a974,978
>
> case LOOP_FREQ: /* initial frequency */
> drift_comp = freq / 1e6;
> rstclock(S_FSET, current_time, 0);
> break;