Lines Matching +full:min +full:- +full:sample +full:- +full:time +full:- +full:ns
1 /*-
4 * Copyright (c) David L. Mills 1993-2001 *
22 * Poul-Henning Kamp <phk@FreeBSD.org>.
45 #include <sys/time.h>
57 * Single-precision macros for 64-bit machines
61 #define L_SUB(v, u) ((v) -= (u))
63 #define L_NEG(v) ((v) = -(v))
67 (v) = -(-(v) >> (n)); \
77 ((v) = -((int64_t)(-(a)) << 32)); \
81 #define L_GINT(v) ((v) < 0 ? -(-(v) >> 32) : (v) >> 32)
86 * These routines constitute the Network Time Protocol (NTP) interfaces
88 * provides the time, maximum error (synch distance) and estimated error
91 * externally derived time. The time offset and related variables set by
96 * When the kernel time is reckoned directly in nanoseconds (NTP_NANO
97 * defined), the time at each tick interrupt is derived directly from
98 * the kernel time variable. When the kernel time is reckoned in
99 * microseconds, (NTP_NANO undefined), the time is derived from the
100 * kernel time variable together with a variable representing the
102 * current nanosecond time is reckoned from these values plus an
104 * architecture-specific module. The interpolation can use either a
111 * Phase/frequency-lock loop (PLL/FLL) definitions
113 * The nanosecond clock discipline uses two variable types, time
114 * variables and frequency variables. Both types are represented as 64-
115 * bit fixed-point quantities with the decimal point between two 32-bit
116 * halves. On a 32-bit machine, each half is represented as a single
117 * word and mathematical operations are done using multiple-precision
118 * arithmetic. On a 64-bit machine, ordinary computer arithmetic is
121 * A time variable is a signed 64-bit fixed-point number in ns and
122 * fraction. It represents the remaining time offset to be amortized
123 * over succeeding tick interrupts. The maximum time offset is about
124 * 0.5 s and the resolution is about 2.3e-10 ns.
128 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
129 * |s s s| ns |
130 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
132 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
134 * A frequency variable is a signed 64-bit fixed-point number in ns/s
135 * and fraction. It represents the ns and fraction to be added to the
136 * kernel time variable at each second. The maximum frequency offset is
137 * about +-500000 ns/s and the resolution is about 2.3e-10 ns/s.
141 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
142 * |s s s s s s s s s s s s s| ns/s |
143 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
149 * residual time and frequency offset of the local clock.
157 static long time_monitor; /* last time offset scaled (ns) */
159 static long time_precision = 1; /* clock precision (ns) */
163 static l_fp time_offset; /* time offset (ns) */
164 static l_fp time_freq; /* frequency offset (ns/s) */
165 static l_fp time_adj; /* tick adjust (ns/s) */
178 * The following variables are used when a pulse-per-second (PPS) signal
183 #define PPS_FAVG 2 /* min freq avg interval (s) (shift) */
188 #define PPS_MAXWANDER 100000 /* max PPS wander (ns/s) */
192 static l_fp pps_freq; /* scaled frequency offset (ns/s) */
194 static long pps_jitter; /* nominal jitter (ns) */
195 static long pps_stabil; /* nominal stability (scaled ns/s) */
196 static time_t pps_lastsec; /* time at last calibration (s) */
211 * End of phase/frequency-lock loop (PLL/FLL) definitions
233 * PPS signal lost when either time or frequency synchronization
240 * PPS jitter exceeded when time synchronization requested
258 struct timespec atv; /* nanosecond time */
263 ntvp->time.tv_sec = atv.tv_sec;
264 ntvp->time.tv_nsec = atv.tv_nsec;
265 ntvp->maxerror = time_maxerror;
266 ntvp->esterror = time_esterror;
267 ntvp->tai = time_tai;
268 ntvp->time_state = time_state;
271 ntvp->time_state = TIME_ERROR;
275 * ntp_gettime() - NTP user application interface
297 td->td_retval[0] = ntv.time_state;
298 return (copyout(&ntv, uap->ntvp, sizeof(ntv)));
327 &time_monitor, 0, "Last time offset scaled (ns)");
331 "Scaled frequency offset (ns/sec)");
334 "Frequency offset (ns/sec)");
338 * ntp_adjtime() - NTP daemon application interface
341 * the timex.constant structure member has a dual purpose to set the time
347 long freq; /* frequency ns/s) */
352 * Update selected clock variables - only the superuser can
355 * Note that either the time constant or TAI offset are loaded
360 modes = ntv->modes;
368 time_maxerror = ntv->maxerror;
370 time_esterror = ntv->esterror;
372 if (time_status & STA_PLL && !(ntv->status & STA_PLL)) {
380 time_status |= ntv->status & ~STA_RONLY;
383 if (ntv->constant < 0)
385 else if (ntv->constant > MAXTC)
388 time_constant = ntv->constant;
391 if (ntv->constant > 0) /* XXX zero & negative numbers ? */
392 time_tai = ntv->constant;
396 if (ntv->shift < PPS_FAVG)
398 else if (ntv->shift > PPS_FAVGMAX)
401 pps_shiftmax = ntv->shift;
413 freq = (ntv->freq * 1000LL) >> 16;
416 else if (freq < -MAXFREQ)
417 L_LINT(time_freq, -MAXFREQ);
420 * ntv->freq is [PPM * 2^16] = [us/s * 2^16]
421 * time_freq is [ns/s * 2^32]
423 time_freq = ntv->freq * 1000LL * 65536LL;
431 hardupdate(ntv->offset);
433 hardupdate(ntv->offset * 1000);
441 ntv->offset = L_GINT(time_offset);
443 ntv->offset = L_GINT(time_offset) / 1000; /* XXX rounding ? */
444 ntv->freq = L_GINT((time_freq / 1000LL) << 16);
445 ntv->maxerror = time_maxerror;
446 ntv->esterror = time_esterror;
447 ntv->status = time_status;
448 ntv->constant = time_constant;
450 ntv->precision = time_precision;
452 ntv->precision = time_precision / 1000;
453 ntv->tolerance = MAXFREQ * SCALE_PPM;
455 ntv->shift = pps_shift;
456 ntv->ppsfreq = L_GINT((pps_freq / 1000LL) << 16);
458 ntv->jitter = pps_jitter;
460 ntv->jitter = pps_jitter / 1000;
461 ntv->stabil = pps_stabil;
462 ntv->calcnt = pps_calcnt;
463 ntv->errcnt = pps_errcnt;
464 ntv->jitcnt = pps_jitcnt;
465 ntv->stbcnt = pps_stbcnt;
486 error = copyin(uap->tp, &ntv, sizeof(ntv));
490 error = copyout(&ntv, uap->tp, sizeof(ntv));
492 td->td_retval[0] = retval;
499 * second_overflow() - called after ntp_tick_adjust()
510 l_fp ftemp; /* 32/64-bit temporary */
524 * Leap second processing. If in leap-insert state at
526 * second; if in leap-delete state, the system clock is
528 * external clock driver will insure that reported time
550 (*newsec)--;
564 time_tai--;
585 * Compute the total time adjustment for the next second
586 * in ns. The offset is reduced by a factor depending on
614 else if (time_adjtime < -1000000)
615 tickrate = -5000;
618 else if (time_adjtime < -500)
619 tickrate = -500;
622 time_adjtime -= tickrate;
630 pps_valid--;
639 * hardupdate() - local clock update
642 * phase and frequency. The implementation is of an adaptive-parameter,
643 * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new
644 * time and frequency offset estimates for each call. If the kernel PPS
646 * determines the new time offset, instead of the calling argument.
648 * believes the local clock is valid within some bound (+-128 ms with
649 * NTP). If the caller's time is far different than the PPS time, an
653 * intervals less than 256 s, operation should be in phase-lock mode,
655 * than 1024 s, operation should be in frequency-lock mode, where the
660 hardupdate(long offset /* clock offset (ns) */)
670 * discipline the time, the PPS offset is used; otherwise, the
679 else if (offset < -MAXPHASE)
680 time_monitor = -MAXPHASE;
698 mtemp = time_uptime - time_reftime;
714 else if (L_GINT(time_freq) < -MAXFREQ)
715 L_LINT(time_freq, -MAXFREQ);
720 * hardpps() - discipline CPU clock oscillator to external PPS signal
724 * first-order feedback loops, one for the phase, the other for the
730 * time and architecture-dependent hardware counter values in
731 * nanoseconds at the on-time PPS signal transition.
736 * variables, except for the actual time and frequency variables, which
739 * tsp - time at current PPS event
740 * delta_nsec - time elapsed between the previous and current PPS event
754 * the range +-500 us. The frequency discriminator rejects input
755 * signals with apparent frequency outside the range 1 +-500
764 u_sec = tsp->tv_sec;
765 u_nsec = tsp->tv_nsec;
767 u_nsec -= NANOSECOND;
770 v_nsec = u_nsec - pps_tf[0].tv_nsec;
771 if (u_sec == pps_tf[0].tv_sec && v_nsec < NANOSECOND - MAXFREQ)
782 pps_fcount += delta_nsec - NANOSECOND;
783 if (v_nsec > MAXFREQ || v_nsec < -MAXFREQ)
788 * A three-stage median filter is used to help denoise the PPS
789 * time. The median sample becomes the time offset estimate; the
790 * difference between the other two samples becomes the time
796 u_nsec = pps_tf[0].tv_nsec - pps_tf[2].tv_nsec;
799 u_nsec = pps_tf[2].tv_nsec - pps_tf[1].tv_nsec;
802 u_nsec = pps_tf[0].tv_nsec - pps_tf[1].tv_nsec;
807 u_nsec = pps_tf[2].tv_nsec - pps_tf[0].tv_nsec;
810 u_nsec = pps_tf[1].tv_nsec - pps_tf[2].tv_nsec;
813 u_nsec = pps_tf[1].tv_nsec - pps_tf[0].tv_nsec;
819 * latency. If it exceeds the popcorn threshold, the sample is
820 * discarded. otherwise, if so enabled, the time offset is
822 * much degrading time accuracy.
827 * timecounter running faster than 1 GHz the lower bound is 2ns, just
835 time_monitor = -v_nsec;
838 pps_jitter += (u_nsec - pps_jitter) >> PPS_FAVG;
839 u_sec = pps_tf[0].tv_sec - pps_lastsec;
854 v_nsec = -pps_fcount;
858 if (v_nsec > u_nsec || v_nsec < -u_nsec || u_sec != (1 << pps_shift)) {
881 pps_intcnt--;
884 } else if (u_nsec < -PPS_MAXWANDER) {
885 L_LINT(ftemp, -PPS_MAXWANDER);
886 pps_intcnt--;
898 } else if (pps_intcnt <= -4 || pps_shift > pps_shiftmax) {
899 pps_intcnt = -4;
901 pps_shift--;
906 u_nsec = -u_nsec;
907 pps_stabil += (u_nsec * SCALE_PPM - pps_stabil) >> PPS_FAVG;
918 else if (u_nsec < -MAXFREQ)
919 L_LINT(pps_freq, -MAXFREQ);
941 if (uap->delta) {
942 error = copyin(uap->delta, &delta, sizeof(delta));
949 if (uap->olddelta && error == 0)
950 error = copyout(&olddelta, uap->olddelta, sizeof(olddelta));
965 ltw = (int64_t)delta->tv_sec * 1000000 + delta->tv_usec;
977 atv.tv_sec--;
992 * Read of time_status is lock-less, which is fine since
1016 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
1017 if (error || !req->newptr)
1032 "Save system time to RTC with this period (in seconds)");