aboutsummaryrefslogtreecommitdiff
path: root/contrib/ntp/ntpd/ntp_loopfilter.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/ntp/ntpd/ntp_loopfilter.c')
-rw-r--r--contrib/ntp/ntpd/ntp_loopfilter.c651
1 files changed, 379 insertions, 272 deletions
diff --git a/contrib/ntp/ntpd/ntp_loopfilter.c b/contrib/ntp/ntpd/ntp_loopfilter.c
index 15b625de6f20..c8a86cf12b66 100644
--- a/contrib/ntp/ntpd/ntp_loopfilter.c
+++ b/contrib/ntp/ntpd/ntp_loopfilter.c
@@ -6,19 +6,17 @@
# include <config.h>
#endif
+#include "ntpd.h"
+#include "ntp_io.h"
+#include "ntp_unixtime.h"
+#include "ntp_stdlib.h"
+
#include <stdio.h>
#include <ctype.h>
-#include <sys/time.h>
-
#include <signal.h>
#include <setjmp.h>
-#include "ntpd.h"
-#include "ntp_io.h"
-#include "ntp_unixtime.h"
-#include "ntp_stdlib.h"
-
#if defined(VMS) && defined(VMS_LOCALUNIT) /*wjm*/
#include "ntp_refclock.h"
#endif /* VMS */
@@ -36,8 +34,9 @@
*/
#define CLOCK_MAX .128 /* default max offset (s) */
#define CLOCK_PANIC 1000. /* default panic offset (s) */
-#define CLOCK_MAXSTAB 2e-6 /* max frequency stability */
+#define CLOCK_MAXSTAB 2e-6 /* max frequency stability (s/s) */
#define CLOCK_MAXERR 1e-2 /* max phase jitter (s) */
+#define CLOCK_PHI 15e-6 /* max frequency error (s/s) */
#define SHIFT_PLL 4 /* PLL loop gain (shift) */
#define CLOCK_AVG 4. /* FLL loop gain */
#define CLOCK_MINSEC 256. /* min FLL update interval (s) */
@@ -51,7 +50,26 @@
/*
* Clock discipline state machine. This is used to control the
* synchronization behavior during initialization and following a
- * timewarp.
+ * timewarp.
+ *
+ * State < max > max Comments
+ * ====================================================
+ * NSET FREQ FREQ no ntp.drift
+ *
+ * FSET TSET if (allow) TSET, ntp.drift
+ * else FREQ
+ *
+ * TSET SYNC FREQ time set
+ *
+ * FREQ SYNC if (mu < 900) FREQ calculate frequency
+ * else if (allow) TSET
+ * else FREQ
+ *
+ * SYNC SYNC if (mu < 900) SYNC normal state
+ * else SPIK
+ *
+ * SPIK SYNC if (allow) TSET spike detector
+ * else FREQ
*/
#define S_NSET 0 /* clock never set */
#define S_FSET 1 /* frequency set from the drift file */
@@ -72,10 +90,10 @@
* support is used as described above; if false, the kernel is bypassed
* entirely and the daemon PLL used instead.
*
- * Each update to a prefer peer sets pps_update if it survives the
+ * Each update to a prefer peer sets pps_stratum if it survives the
* intersection algorithm and its time is within range. The PPS time
* discipline is enabled (STA_PPSTIME bit set in the status word) when
- * pps_update is true and the PPS frequency discipline is enabled. If
+ * pps_stratum is true and the PPS frequency discipline is enabled. If
* the PPS time discipline is enabled and the kernel reports a PPS
* signal is present, the pps_control variable is set to the current
* time. If the current time is later than pps_control by PPS_MAXAGE
@@ -91,18 +109,27 @@
#define PPS_MAXAGE 120 /* kernel pps signal timeout (s) */
/*
+ * Program variables that can be tinkered.
+ */
+double clock_max = CLOCK_MAX; /* max offset before step (s) */
+double clock_panic = CLOCK_PANIC; /* max offset before panic (s) */
+double clock_phi = CLOCK_PHI; /* dispersion rate (s/s) */
+double clock_minstep = CLOCK_MINSTEP; /* step timeout (s) */
+double allan_xpt = CLOCK_ALLAN; /* minimum Allan intercept (s) */
+
+/*
* Program variables
*/
static double clock_offset; /* clock offset adjustment (s) */
-double drift_comp; /* clock frequency (ppm) */
-double clock_stability; /* clock stability (ppm) */
-double clock_max = CLOCK_MAX; /* max offset allowed before step (s) */
-static double clock_panic = CLOCK_PANIC; /* max offset allowed before panic */
+double drift_comp; /* clock frequency (s/s) */
+double clock_stability; /* clock stability (s/s) */
u_long pps_control; /* last pps sample time */
-static void rstclock P((int)); /* state transition function */
+static void rstclock P((int, double, double)); /* transition function */
#ifdef KERNEL_PLL
-int pll_status; /* status bits for kernel pll */
+struct timex ntv; /* kernel API parameters */
+int pll_status; /* status bits for kernel pll */
+int pll_nano; /* nanosecond kernel switch */
#endif /* KERNEL_PLL */
/*
@@ -111,32 +138,38 @@ int pll_status; /* status bits for kernel pll */
int ntp_enable; /* clock discipline enabled */
int pll_control; /* kernel support available */
int kern_enable; /* kernel support enabled */
+int pps_enable; /* kernel PPS discipline enabled */
int ext_enable; /* external clock enabled */
-int pps_update; /* pps update valid */
-int allow_set_backward = TRUE; /* step corrections allowed */
-int correct_any = FALSE; /* corrections > 1000 s allowed */
-
-#ifdef STA_NANO
-int pll_nano; /* nanosecond kernel switch */
-#endif /* STA_NANO */
+int pps_stratum; /* pps stratum */
+int allow_step = TRUE; /* allow step correction */
+int allow_panic = FALSE; /* allow panic correction */
+int mode_ntpdate = FALSE; /* exit on first clock set */
/*
* Clock state machine variables
*/
-u_char sys_poll; /* log2 of system poll interval */
+u_char sys_minpoll = NTP_MINDPOLL; /* min sys poll interval (log2 s) */
+u_char sys_poll = NTP_MINDPOLL; /* system poll interval (log2 s) */
int state; /* clock discipline state */
int tc_counter; /* poll-adjust counter */
u_long last_time; /* time of last clock update (s) */
double last_offset; /* last clock offset (s) */
-double allan_xpt; /* Allan intercept (s) */
-double sys_error; /* system standard error (s) */
+double sys_jitter; /* system RMS jitter (s) */
+
+/*
+ * Huff-n'-puff filter variables
+ */
+static double *sys_huffpuff; /* huff-n'-puff filter */
+static int sys_hufflen; /* huff-n'-puff filter stages */
+static int sys_huffptr; /* huff-n'-puff filter pointer */
+static double sys_mindly; /* huff-n'-puff filter min delay */
#if defined(KERNEL_PLL)
/* Emacs cc-mode goes nuts if we split the next line... */
#define MOD_BITS (MOD_OFFSET | MOD_MAXERROR | MOD_ESTERROR | \
MOD_STATUS | MOD_TIMECONST)
-static void pll_trap P((int)); /* configuration trap */
#ifdef SIGSYS
+static void pll_trap P((int)); /* configuration trap */
static struct sigaction sigsys; /* current sigaction status */
static struct sigaction newsigsys; /* new sigaction status */
static sigjmp_buf env; /* environment var. for pll_trap() */
@@ -153,7 +186,7 @@ init_loopfilter(void)
* Initialize state variables. Initially, we expect no drift
* file, so set the state to S_NSET.
*/
- rstclock(S_NSET);
+ rstclock(S_NSET, current_time, 0);
}
/*
@@ -175,30 +208,70 @@ local_clock(
double dtemp, etemp; /* double temps */
int retval; /* return value */
-#if defined(KERNEL_PLL)
- struct timex ntv; /* kernel interface structure */
-#endif /* KERNEL_PLL */
-
+ /*
+ * If the loop is opened, monitor and record the offsets
+ * anyway in order to determine the open-loop response.
+ */
#ifdef DEBUG
if (debug)
printf(
- "local_clock: offset %.6f jitter %.6f state %d\n",
- fp_offset, SQRT(epsil), state);
+ "local_clock: assocID %d off %.6f jit %.6f sta %d\n",
+ peer->associd, fp_offset, SQRT(epsil), state);
#endif
- if (!ntp_enable)
- return(0);
+ if (!ntp_enable) {
+ record_loop_stats(fp_offset, drift_comp, SQRT(epsil),
+ clock_stability, sys_poll);
+ return (0);
+ }
/*
- * If the clock is way off, don't tempt fate by correcting it.
+ * If the clock is way off, panic is declared. The clock_panic
+ * defaults to 1000 s; if set to zero, the panic will never
+ * occur. The allow_panic defaults to FALSE, so the first panic
+ * will exit. It can be set TRUE by a command line option, in
+ * which case the clock will be set anyway and time marches on.
+ * But, allow_panic will be set it FALSE when the update is
+ * within the step range; so, subsequent panics will exit.
*/
-#ifndef SYS_WINNT
- if (fabs(fp_offset) >= clock_panic && !correct_any) {
+ if (fabs(fp_offset) > clock_panic && clock_panic > 0 &&
+ !allow_panic) {
msyslog(LOG_ERR,
- "time error %.0f over %d seconds; set clock manually",
- fp_offset, (int)clock_panic);
+ "time correction of %.0f seconds exceeds sanity limit (%.0f); set clock manually to the correct UTC time.",
+ fp_offset, clock_panic);
return (-1);
}
-#endif
+
+ /*
+ * If simulating ntpdate, set the clock directly, rather than
+ * using the discipline. The clock_max defines the step
+ * threshold, above which the clock will be stepped instead of
+ * slewed. The value defaults to 128 ms, but can be set to even
+ * unreasonable values. If set to zero, the clock will never be
+ * stepped.
+ *
+ * Note that if ntpdate is active, the terminal does not detach,
+ * so the termination comments print directly to the console.
+ */
+ if (mode_ntpdate) {
+ if (allow_step && fabs(fp_offset) > clock_max &&
+ clock_max > 0) {
+ step_systime(fp_offset);
+ NLOG(NLOG_SYNCEVENT|NLOG_SYSEVENT)
+ msyslog(LOG_NOTICE, "time reset %.6f s",
+ fp_offset);
+ printf("ntpd: time reset %.6fs\n", fp_offset);
+ } else {
+ adj_systime(fp_offset);
+ NLOG(NLOG_SYNCEVENT|NLOG_SYSEVENT)
+ msyslog(LOG_NOTICE, "time slew %.6f s",
+ fp_offset);
+ printf("ntpd: time slew %.6fs\n", fp_offset);
+ }
+ record_loop_stats(fp_offset, drift_comp, SQRT(epsil),
+ clock_stability, sys_poll);
+ exit (0);
+ }
+
/*
* If the clock has never been set, set it and initialize the
* discipline parameters. We then switch to frequency mode to
@@ -210,29 +283,60 @@ local_clock(
step_systime(fp_offset);
NLOG(NLOG_SYNCEVENT|NLOG_SYSEVENT)
msyslog(LOG_NOTICE, "time set %.6f s", fp_offset);
- rstclock(S_TSET);
- rstclock(S_FREQ);
+ rstclock(S_FREQ, peer->epoch, fp_offset);
return (1);
}
/*
* Update the jitter estimate.
*/
- oerror = sys_error;
- dtemp = SQUARE(sys_error);
- sys_error = SQRT(dtemp + (epsil - dtemp) / CLOCK_AVG);
+ oerror = sys_jitter;
+ dtemp = SQUARE(sys_jitter);
+ sys_jitter = SQRT(dtemp + (epsil - dtemp) / CLOCK_AVG);
+
+ /*
+ * The huff-n'-puff filter finds the lowest delay in the recent
+ * interval. This is used to correct the offset by one-half the
+ * difference between the sample delay and minimum delay. This
+ * is most effective if the delays are highly assymetric and
+ * clockhopping is avoided and the clock frequency wander is
+ * relatively small.
+ */
+ if (sys_huffpuff != NULL) {
+ if (peer->delay < sys_huffpuff[sys_huffptr])
+ sys_huffpuff[sys_huffptr] = peer->delay;
+ if (peer->delay < sys_mindly)
+ sys_mindly = peer->delay;
+ if (fp_offset > 0)
+ dtemp = -(peer->delay - sys_mindly) / 2;
+ else
+ dtemp = (peer->delay - sys_mindly) / 2;
+ fp_offset += dtemp;
+#ifdef DEBUG
+ if (debug)
+ printf(
+ "local_clock: size %d mindly %.6f huffpuff %.6f\n",
+ sys_hufflen, sys_mindly, dtemp);
+#endif
+ }
/*
* Clock state machine transition function. This is where the
* action is and defines how the system reacts to large phase
* and frequency errors. There are two main regimes: when the
- * phase error exceeds the maximum allowed for ordinary tracking
- * and otherwise when it does not.
+ * offset exceeds the step threshold and when it does not.
+ * However, if the step threshold is set to zero, a step will
+ * never occur. See the instruction manual for the details how
+ * these actions interact with the command line options.
*/
retval = 0;
+ if (sys_poll > peer->maxpoll)
+ sys_poll = peer->maxpoll;
+ else if (sys_poll < peer->minpoll)
+ sys_poll = peer->minpoll;
clock_frequency = flladj = plladj = 0;
- mu = current_time - last_time;
- if (fabs(fp_offset) > clock_max) {
+ mu = peer->epoch - last_time;
+ if (fabs(fp_offset) > clock_max && clock_max > 0) {
switch (state) {
/*
@@ -243,32 +347,29 @@ local_clock(
* to S_FREQ state.
*/
case S_TSET:
- rstclock(S_FREQ);
- last_offset = clock_offset = fp_offset;
- return (0);
+ state = S_FREQ;
+ break;
/*
* In S_SYNC state we ignore outlyers. At the first
- * outlyer after CLOCK_MINSTEP (900 s), switch to S_SPIK
+ * outlyer after the stepout threshold, switch to S_SPIK
* state.
*/
case S_SYNC:
- if (mu < CLOCK_MINSTEP)
+ if (mu < clock_minstep)
return (0);
- rstclock(S_SPIK);
+ state = S_SPIK;
return (0);
/*
* In S_FREQ state we ignore outlyers. At the first
- * outlyer after CLOCK_MINSTEP (900 s), compute the
- * apparent phase and frequency correction.
+ * outlyer after 900 s, compute the apparent phase and
+ * frequency correction.
*/
case S_FREQ:
- if (mu < CLOCK_MINSTEP)
+ if (mu < clock_minstep)
return (0);
- clock_frequency = (fp_offset - clock_offset) /
- mu;
- /* fall through to default */
+ /* fall through to S_SPIK */
/*
* In S_SPIK state a large correction is necessary.
@@ -286,19 +387,19 @@ local_clock(
* reset or shaken, but never stirred.
*/
default:
- if (allow_set_backward | correct_any) {
+ if (allow_step) {
step_systime(fp_offset);
NLOG(NLOG_SYNCEVENT|NLOG_SYSEVENT)
msyslog(LOG_NOTICE, "time reset %.6f s",
fp_offset);
- rstclock(S_TSET);
+ 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);
- last_offset = clock_offset = fp_offset;
+ rstclock(S_FREQ, peer->epoch,
+ fp_offset);
}
break;
}
@@ -306,28 +407,25 @@ local_clock(
switch (state) {
/*
- * If this is the first update, initialize the
- * discipline parameters and pretend we had just set the
- * clock. We don't want to step the clock unless we have
- * to.
+ * In S_FSET state this is the first update. Adjust the
+ * phase, but don't adjust the frequency until the next
+ * update.
*/
case S_FSET:
- rstclock(S_TSET);
- last_offset = clock_offset = fp_offset;
- return (0);
+ rstclock(S_TSET, peer->epoch, fp_offset);
+ break;
/*
- * In S_FREQ state we ignore updates until CLOCK_MINSTEP
- * (900 s). After that, correct the phase and frequency
- * and switch to S_SYNC state.
+ * In S_FREQ state ignore updates until the stepout
+ * threshold. After that, correct the phase and
+ * frequency and switch to S_SYNC state.
*/
case S_FREQ:
- if (mu < CLOCK_MINSTEP)
+ if (mu < clock_minstep)
return (0);
clock_frequency = (fp_offset - clock_offset) /
mu;
- clock_offset = fp_offset;
- rstclock(S_SYNC);
+ rstclock(S_SYNC, peer->epoch, fp_offset);
break;
/*
@@ -337,7 +435,7 @@ local_clock(
*/
case S_TSET:
case S_SPIK:
- rstclock(S_SYNC);
+ state = S_SYNC;
/* fall through to default */
/*
@@ -349,14 +447,17 @@ local_clock(
* and ignore it.
*/
default:
+ allow_panic = TRUE;
if (fabs(fp_offset - last_offset) >
CLOCK_SGATE * oerror && mu <
ULOGTOD(sys_poll + 1)) {
#ifdef DEBUG
if (debug)
printf(
- "local_clock: popcorn %.6f %.6f\n",
- fp_offset, last_offset);
+ "local_clock: popcorn %.6f %.6f\n",
+ fabs(fp_offset -
+ last_offset), CLOCK_SGATE *
+ oerror);
#endif
last_offset = fp_offset;
return (0);
@@ -366,29 +467,32 @@ local_clock(
* Compute the FLL and PLL frequency adjustments
* conditioned on intricate weighting factors.
* For the FLL, the averaging interval is
- * clamped not to decrease below the Allan
- * intercept and the gain is decreased from
- * unity for mu above CLOCK_MINSEC (1024 s) to
- * zero below CLOCK_MINSEC (256 s). For the PLL,
- * the averaging interval is clamped not to
- * exceed the sustem poll interval. These
- * measures insure stability of the clock
- * discipline even when the rules of fair
- * engagement are broken.
+ * clamped to a minimum of 1024 s and the gain
+ * is decreased from unity for mu above 1024 s
+ * to zero below 256 s. For the PLL, the
+ * averaging interval is clamped not to exceed
+ * the sustem poll interval. No gain factor is
+ * necessary, since the frequency steering above
+ * 1024 s 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.
*/
dtemp = max(mu, allan_xpt);
etemp = min(max(0, mu - CLOCK_MINSEC) /
- CLOCK_ALLAN, 1.);
+ allan_xpt, 1.);
flladj = fp_offset * etemp / (dtemp *
CLOCK_AVG);
dtemp = ULOGTOD(SHIFT_PLL + 2 + sys_poll);
etemp = min(mu, ULOGTOD(sys_poll));
plladj = fp_offset * etemp / (dtemp * dtemp);
- clock_offset = fp_offset;
+ last_time = peer->epoch;
+ last_offset = clock_offset = fp_offset;
break;
}
}
+#if defined(KERNEL_PLL)
/*
* This code segment works when clock adjustments are made using
* precision time kernel support and the ntp_adjtime() system
@@ -399,7 +503,6 @@ local_clock(
* modifications provide a true microsecond clock and nanosecond
* clock, respectively.
*/
-#if defined(KERNEL_PLL)
if (pll_control && kern_enable) {
/*
@@ -413,7 +516,7 @@ local_clock(
* frequency offsets for jitter and stability values and
* to update the drift file.
*/
- memset((char *)&ntv, 0, sizeof ntv);
+ memset(&ntv, 0, sizeof(ntv));
if (ext_enable) {
ntv.modes = MOD_STATUS;
} else {
@@ -422,25 +525,21 @@ local_clock(
dtemp = -.5;
else
dtemp = .5;
-#ifdef STA_NANO
- if (pll_nano)
+ if (pll_nano) {
ntv.offset = (int32)(clock_offset *
1e9 + dtemp);
- else
-#endif /* STA_NANO */
+ ntv.constant = sys_poll;
+ } else {
ntv.offset = (int32)(clock_offset *
1e6 + dtemp);
+ ntv.constant = sys_poll - 4;
+ }
if (clock_frequency != 0) {
ntv.modes |= MOD_FREQUENCY;
ntv.freq = (int32)((clock_frequency +
drift_comp) * 65536e6);
}
-#ifdef STA_NANO
- ntv.constant = sys_poll;
-#else
- ntv.constant = sys_poll - 4;
-#endif /* STA_NANO */
- ntv.esterror = (u_int32)(sys_error * 1e6);
+ ntv.esterror = (u_int32)(sys_jitter * 1e6);
ntv.maxerror = (u_int32)((sys_rootdelay / 2 +
sys_rootdispersion) * 1e6);
ntv.status = STA_PLL;
@@ -467,63 +566,53 @@ local_clock(
*/
if (sys_poll > NTP_MAXDPOLL)
ntv.status |= STA_FLL;
- }
- /*
- * Wiggle the PPS bits according to the health of the
- * prefer peer.
- */
- if (pll_status & STA_PPSSIGNAL)
- ntv.status |= STA_PPSFREQ;
- if (pll_status & STA_PPSFREQ && pps_update)
- ntv.status |= STA_PPSTIME;
+ /*
+ * If the PPS signal is up and enabled, light
+ * the frequency bit. If the PPS driver is
+ * working, light the phase bit as well. If not,
+ * douse the lights, since somebody else may
+ * have left the switch on.
+ */
+ if (pps_enable && pll_status & STA_PPSSIGNAL) {
+ ntv.status |= STA_PPSFREQ;
+ if (pps_stratum < STRATUM_UNSPEC)
+ ntv.status |= STA_PPSTIME;
+ } else {
+ ntv.status &= ~(STA_PPSFREQ |
+ STA_PPSTIME);
+ }
+ }
/*
- * Update the offset and frequency from the kernel
- * variables.
+ * Pass the stuff to the kernel. If it squeals, turn off
+ * the pigs. In any case, fetch the kernel offset and
+ * frequency and pretend we did it here.
*/
if (ntp_adjtime(&ntv) == TIME_ERROR) {
if (ntv.status != pll_status)
msyslog(LOG_ERR,
- "kernel pll status change %x",
+ "kernel time discipline status change %x",
ntv.status);
+ ntv.status &= ~(STA_PPSFREQ | STA_PPSTIME);
}
pll_status = ntv.status;
-#ifdef STA_NANO
if (pll_nano)
clock_offset = ntv.offset / 1e9;
else
-#endif /* STA_NANO */
clock_offset = ntv.offset / 1e6;
-#ifdef STA_NANO
- sys_poll = ntv.constant;
-#else
- sys_poll = ntv.constant + 4;
-#endif /* STA_NANO */
clock_frequency = ntv.freq / 65536e6 - drift_comp;
flladj = plladj = 0;
/*
- * If the kernel pps discipline is working, monitor its
- * performance.
+ * If the kernel PPS is lit, monitor its performance.
*/
if (ntv.status & STA_PPSTIME) {
- if (!pps_control)
- NLOG(NLOG_SYSEVENT)msyslog(LOG_INFO,
- "pps sync enabled");
pps_control = current_time;
-#ifdef STA_NANO
if (pll_nano)
- record_peer_stats(
- &loopback_interface->sin,
- ctlsysstatus(), ntv.offset / 1e9,
- 0., ntv.jitter / 1e9, 0.);
+ sys_jitter = ntv.jitter / 1e9;
else
-#endif /* STA_NANO */
- record_peer_stats(
- &loopback_interface->sin,
- ctlsysstatus(), ntv.offset / 1e6,
- 0., ntv.jitter / 1e6, 0.);
+ sys_jitter = ntv.jitter / 1e6;
}
}
#endif /* KERNEL_PLL */
@@ -537,21 +626,26 @@ local_clock(
*/
etemp = clock_frequency + flladj + plladj;
drift_comp += etemp;
- if (drift_comp > sys_maxfreq)
- drift_comp = sys_maxfreq;
- else if (drift_comp <= -sys_maxfreq)
- drift_comp = -sys_maxfreq;
+ if (drift_comp > NTP_MAXFREQ)
+ drift_comp = NTP_MAXFREQ;
+ else if (drift_comp <= -NTP_MAXFREQ)
+ drift_comp = -NTP_MAXFREQ;
dtemp = SQUARE(clock_stability);
etemp = SQUARE(etemp) - dtemp;
clock_stability = SQRT(dtemp + etemp / CLOCK_AVG);
- allan_xpt = max(CLOCK_ALLAN, clock_stability * CLOCK_ADF);
/*
- * In SYNC state, adjust the poll interval.
+ * In SYNC state, adjust the poll interval. The trick here is to
+ * compare the apparent frequency change induced by the system
+ * jitter over the poll interval, or fritter, to the frequency
+ * stability. If the fritter is greater than the stability,
+ * phase noise predominates and the averaging interval is
+ * increased; otherwise, it is decreased. A bit of hysteresis
+ * helps calm the dance. Works best using burst mode.
*/
if (state == S_SYNC) {
- if (clock_stability < CLOCK_MAXSTAB &&
- fabs(clock_offset) < CLOCK_PGATE * sys_error) {
+ if (sys_jitter / ULOGTOD(sys_poll) > clock_stability &&
+ fabs(clock_offset) < CLOCK_PGATE * sys_jitter) {
tc_counter += sys_poll;
if (tc_counter > CLOCK_LIMIT) {
tc_counter = CLOCK_LIMIT;
@@ -575,25 +669,17 @@ local_clock(
/*
* Update the system time variables.
*/
- last_time = current_time;
- last_offset = clock_offset;
- dtemp = peer->disp + SQRT(peer->variance + SQUARE(sys_error));
+ dtemp = peer->disp + sys_jitter;
if ((peer->flags & FLAG_REFCLOCK) == 0 && dtemp < MINDISPERSE)
dtemp = MINDISPERSE;
sys_rootdispersion = peer->rootdispersion + dtemp;
- (void)record_loop_stats();
-#ifdef DEBUG
- if (debug)
- printf(
- "local_clock: mu %.0f allan %.0f fadj %.3f fll %.3f pll %.3f\n",
- mu, allan_xpt, clock_frequency * 1e6, flladj * 1e6,
- plladj * 1e6);
-#endif /* DEBUG */
+ record_loop_stats(last_offset, drift_comp, sys_jitter,
+ clock_stability, sys_poll);
#ifdef DEBUG
if (debug)
printf(
- "local_clock: jitter %.6f freq %.3f stab %.3f poll %d count %d\n",
- sys_error, drift_comp * 1e6, clock_stability * 1e6,
+ "local_clock: mu %.0f noi %.3f stb %.3f pol %d cnt %d\n",
+ mu, sys_jitter * 1e6 / mu, clock_stability * 1e6,
sys_poll, tc_counter);
#endif /* DEBUG */
return (retval);
@@ -620,7 +706,7 @@ adj_host_clock(
* maximum error and the local clock driver will pick it up and
* pass to the common refclock routines. Very elegant.
*/
- sys_rootdispersion += CLOCK_PHI;
+ sys_rootdispersion += clock_phi;
/*
* Declare PPS kernel unsync if the pps signal has not been
@@ -665,51 +751,35 @@ adj_host_clock(
*/
static void
rstclock(
- int trans /* new state */
+ int trans, /* new state */
+ double epoch, /* last time */
+ double offset /* last offset */
)
{
- correct_any = FALSE;
+ tc_counter = 0;
+ sys_poll = NTP_MINPOLL;
state = trans;
- switch (state) {
-
- /*
- * Frequency mode. The clock has ben set, but the frequency has
- * not yet been determined. Note that the Allan intercept is set
- * insure the clock filter considers only the most recent
- * measurements.
- */
- case S_FREQ:
- sys_poll = NTP_MINDPOLL;
- allan_xpt = CLOCK_ALLAN;
- last_time = current_time;
- break;
+ last_time = epoch;
+ last_offset = clock_offset = offset;
+}
- /*
- * Synchronized mode. Discipline the poll interval.
- */
- case S_SYNC:
- sys_poll = NTP_MINDPOLL;
- allan_xpt = CLOCK_ALLAN;
- tc_counter = 0;
- break;
- /*
- * Don't do anything in S_SPIK state; just continue from S_SYNC
- * state.
- */
- case S_SPIK:
- break;
+/*
+ * huff-n'-puff filter
+ */
+void
+huffpuff()
+{
+ int i;
- /*
- * S_NSET, S_FSET and S_TSET states. These transient states set
- * the time reference for future frequency updates.
- */
- default:
- sys_poll = NTP_MINDPOLL;
- allan_xpt = CLOCK_ALLAN;
- last_time = current_time;
- last_offset = clock_offset = 0;
- break;
+ if (sys_huffpuff == NULL)
+ return;
+ sys_huffptr = (sys_huffptr + 1) % sys_hufflen;
+ sys_huffpuff[sys_huffptr] = 1e9;
+ sys_mindly = 1e9;
+ for (i = 0; i < sys_hufflen; i++) {
+ if (sys_huffpuff[i] < sys_mindly)
+ sys_mindly = sys_huffpuff[i];
}
}
@@ -723,111 +793,148 @@ loop_config(
double freq
)
{
-#if defined(KERNEL_PLL)
- struct timex ntv;
-#endif /* KERNEL_PLL */
+ int i;
-#ifdef DEBUG
- if (debug)
- printf("loop_config: state %d freq %.3f\n", item, freq *
- 1e6);
-#endif
switch (item) {
- case LOOP_DRIFTINIT:
- case LOOP_DRIFTCOMP:
+ case LOOP_DRIFTINIT:
- /*
- * The drift file is present and the initial frequency
- * is available, so set the state to S_FSET
- */
- rstclock(S_FSET);
- drift_comp = freq;
- if (drift_comp > sys_maxfreq)
- drift_comp = sys_maxfreq;
- if (drift_comp < -sys_maxfreq)
- drift_comp = -sys_maxfreq;
#ifdef KERNEL_PLL
/*
- * If the phase-lock code is implemented in the kernel,
- * give the time_constant and saved frequency offset to
- * the kernel. If not, no harm is done. Note the initial
- * time constant is zero, but the first clock update
- * will fix that.
+ * Assume the kernel supports the ntp_adjtime() syscall.
+ * If that syscall works, initialize the kernel
+ * variables. Otherwise, continue leaving no harm
+ * behind. While at it, ask to set nanosecond mode. If
+ * the kernel agrees, rejoice; othewise, it does only
+ * microseconds.
*/
- memset((char *)&ntv, 0, sizeof ntv);
pll_control = 1;
-#ifdef MOD_NANO
- ntv.modes = MOD_NANO;
-#endif /* MOD_NANO */
+ memset(&ntv, 0, sizeof(ntv));
+#ifdef STA_NANO
+ ntv.modes = MOD_BITS | MOD_NANO;
+#else
+ ntv.modes = MOD_BITS;
+#endif /* STA_NANO */
+ ntv.maxerror = MAXDISPERSE;
+ ntv.esterror = MAXDISPERSE;
+ ntv.status = STA_UNSYNC;
#ifdef SIGSYS
+ /*
+ * Use sigsetjmp() to save state and then call
+ * ntp_adjtime(); if it fails, then siglongjmp() is used
+ * to return control
+ */
newsigsys.sa_handler = pll_trap;
newsigsys.sa_flags = 0;
if (sigaction(SIGSYS, &newsigsys, &sigsys)) {
msyslog(LOG_ERR,
"sigaction() fails to save SIGSYS trap: %m");
pll_control = 0;
- return;
}
-
- /*
- * Use sigsetjmp() to save state and then call
- * ntp_adjtime(); if it fails, then siglongjmp() is used
- * to return control
- */
if (sigsetjmp(env, 1) == 0)
- (void)ntp_adjtime(&ntv);
+ ntp_adjtime(&ntv);
if ((sigaction(SIGSYS, &sigsys,
(struct sigaction *)NULL))) {
msyslog(LOG_ERR,
"sigaction() fails to restore SIGSYS trap: %m");
pll_control = 0;
- return;
}
#else /* SIGSYS */
- if (ntp_adjtime(&ntv) < 0) {
- msyslog(LOG_ERR,
- "loop_config: ntp_adjtime() failed: %m");
- pll_control = 0;
- }
+ ntp_adjtime(&ntv);
#endif /* SIGSYS */
-
- /*
- * If the kernel support is available and enabled,
- * initialize the parameters, but only if the external
- * clock is not present.
- */
- if (pll_control && kern_enable) {
- msyslog(LOG_NOTICE,
- "using kernel phase-lock loop %04x",
- ntv.status);
+ pll_status = ntv.status;
+ if (pll_control) {
#ifdef STA_NANO
- if (ntv.status & STA_NANO)
+ if (pll_status & STA_NANO)
pll_nano = 1;
+ if (pll_status & STA_CLK)
+ ext_enable = 1;
#endif /* STA_NANO */
-#ifdef STA_CLK
+ msyslog(LOG_NOTICE,
+ "kernel time discipline status %04x",
+ pll_status);
+ }
+#endif /* KERNEL_PLL */
+ break;
- if (ntv.status & STA_CLK) {
- ext_enable = 1;
- } else {
- ntv.modes = MOD_BITS | MOD_FREQUENCY;
+ case LOOP_DRIFTCOMP:
+
+ /*
+ * 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.
+ */
+ 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;
+
+#ifdef KERNEL_PLL
+ /*
+ * Sanity check. If the kernel is enabled, load the
+ * frequency and light up the loop. If not, set the
+ * kernel frequency to zero and leave the loop dark. In
+ * either case set the time to zero to cancel any
+ * previous nonsense.
+ */
+ if (pll_control) {
+ memset((char *)&ntv, 0, sizeof(ntv));
+ ntv.modes = MOD_OFFSET | MOD_FREQUENCY;
+ if (kern_enable) {
+ ntv.modes |= MOD_STATUS;
+ ntv.status = STA_PLL;
ntv.freq = (int32)(drift_comp *
65536e6);
- ntv.maxerror = MAXDISPERSE;
- ntv.esterror = MAXDISPERSE;
- ntv.status = STA_UNSYNC | STA_PLL;
- (void)ntp_adjtime(&ntv);
}
-#else
- ntv.modes = MOD_BITS | MOD_FREQUENCY;
- ntv.freq = (int32)(drift_comp * 65536e6);
- ntv.maxerror = MAXDISPERSE;
- ntv.esterror = MAXDISPERSE;
- ntv.status = STA_UNSYNC | STA_PLL;
(void)ntp_adjtime(&ntv);
-#endif /* STA_CLK */
}
#endif /* KERNEL_PLL */
+ break;
+
+ /*
+ * Special tinker variables for Ulrich Windl. Very dangerous.
+ */
+ case LOOP_MAX: /* step threshold */
+ clock_max = freq;
+ break;
+
+ case LOOP_PANIC: /* panic exit threshold */
+ clock_panic = freq;
+ break;
+
+ case LOOP_PHI: /* dispersion rate */
+ clock_phi = freq;
+ break;
+
+ case LOOP_MINSTEP: /* watchdog bark */
+ clock_minstep = freq;
+ break;
+
+ case LOOP_MINPOLL: /* ephemeral association poll */
+ if (freq < NTP_MINPOLL)
+ freq = NTP_MINPOLL;
+ sys_minpoll = (u_char)freq;
+ break;
+
+ case LOOP_ALLAN: /* minimum Allan intercept */
+ if (freq < CLOCK_ALLAN)
+ freq = CLOCK_ALLAN;
+ allan_xpt = freq;
+ break;
+
+ case LOOP_HUFFPUFF: /* huff-n'-puff filter length */
+ if (freq < HUFFPUFF)
+ freq = HUFFPUFF;
+ sys_hufflen = (int)(freq / HUFFPUFF);
+ sys_huffpuff = (double *)emalloc(sizeof(double) *
+ sys_hufflen);
+ for (i = 0; i < sys_hufflen; i++)
+ sys_huffpuff[i] = 1e9;
+ sys_mindly = 1e9;
+ break;
}
}