kern_ntptime.c (30994) | kern_ntptime.c (32513) |
---|---|
1/****************************************************************************** 2 * * 3 * Copyright (c) David L. Mills 1993, 1994 * 4 * * 5 * Permission to use, copy, modify, and distribute this software and its * 6 * documentation for any purpose and without fee is hereby granted, provided * 7 * that the above copyright notice appears in all copies and that both the * 8 * copyright notice and this permission notice appear in supporting * --- 41 unchanged lines hidden (view full) --- 50#include <sys/systm.h> 51#include <sys/sysproto.h> 52#include <sys/kernel.h> 53#include <sys/proc.h> 54#include <sys/timex.h> 55#include <sys/sysctl.h> 56 57/* | 1/****************************************************************************** 2 * * 3 * Copyright (c) David L. Mills 1993, 1994 * 4 * * 5 * Permission to use, copy, modify, and distribute this software and its * 6 * documentation for any purpose and without fee is hereby granted, provided * 7 * that the above copyright notice appears in all copies and that both the * 8 * copyright notice and this permission notice appear in supporting * --- 41 unchanged lines hidden (view full) --- 50#include <sys/systm.h> 51#include <sys/sysproto.h> 52#include <sys/kernel.h> 53#include <sys/proc.h> 54#include <sys/timex.h> 55#include <sys/sysctl.h> 56 57/* |
58 * The following variables are used by the hardclock() routine in the 59 * kern_clock.c module and are described in that module. | 58 * Phase/frequency-lock loop (PLL/FLL) definitions 59 * 60 * The following variables are read and set by the ntp_adjtime() system 61 * call. 62 * 63 * time_state shows the state of the system clock, with values defined 64 * in the timex.h header file. 65 * 66 * time_status shows the status of the system clock, with bits defined 67 * in the timex.h header file. 68 * 69 * time_offset is used by the PLL/FLL to adjust the system time in small 70 * increments. 71 * 72 * time_constant determines the bandwidth or "stiffness" of the PLL. 73 * 74 * time_tolerance determines maximum frequency error or tolerance of the 75 * CPU clock oscillator and is a property of the architecture; however, 76 * in principle it could change as result of the presence of external 77 * discipline signals, for instance. 78 * 79 * time_precision is usually equal to the kernel tick variable; however, 80 * in cases where a precision clock counter or external clock is 81 * available, the resolution can be much less than this and depend on 82 * whether the external clock is working or not. 83 * 84 * time_maxerror is initialized by a ntp_adjtime() call and increased by 85 * the kernel once each second to reflect the maximum error 86 * bound growth. 87 * 88 * time_esterror is set and read by the ntp_adjtime() call, but 89 * otherwise not used by the kernel. |
60 */ | 90 */ |
61extern int time_state; /* clock state */ 62extern int time_status; /* clock status bits */ 63extern long time_offset; /* time adjustment (us) */ 64extern long time_freq; /* frequency offset (scaled ppm) */ 65extern long time_maxerror; /* maximum error (us) */ 66extern long time_esterror; /* estimated error (us) */ 67extern long time_constant; /* pll time constant */ 68extern long time_precision; /* clock precision (us) */ 69extern long time_tolerance; /* frequency tolerance (scaled ppm) */ | 91static int time_status = STA_UNSYNC; /* clock status bits */ 92static int time_state = TIME_OK; /* clock state */ 93static long time_offset = 0; /* time offset (us) */ 94static long time_constant = 0; /* pll time constant */ 95static long time_tolerance = MAXFREQ; /* frequency tolerance (scaled ppm) */ 96static long time_precision = 1; /* clock precision (us) */ 97static long time_maxerror = MAXPHASE; /* maximum error (us) */ 98static long time_esterror = MAXPHASE; /* estimated error (us) */ |
70 | 99 |
100/* 101 * The following variables establish the state of the PLL/FLL and the 102 * residual time and frequency offset of the local clock. The scale 103 * factors are defined in the timex.h header file. 104 * 105 * time_phase and time_freq are the phase increment and the frequency 106 * increment, respectively, of the kernel time variable at each tick of 107 * the clock. 108 * 109 * time_freq is set via ntp_adjtime() from a value stored in a file when 110 * the synchronization daemon is first started. Its value is retrieved 111 * via ntp_adjtime() and written to the file about once per hour by the 112 * daemon. 113 * 114 * time_adj is the adjustment added to the value of tick at each timer 115 * interrupt and is recomputed from time_phase and time_freq at each 116 * seconds rollover. 117 * 118 * time_reftime is the second's portion of the system time on the last 119 * call to ntp_adjtime(). It is used to adjust the time_freq variable 120 * and to increase the time_maxerror as the time since last update 121 * increases. 122 */ 123long time_phase = 0; /* phase offset (scaled us) */ 124static long time_freq = 0; /* frequency offset (scaled ppm) */ 125long time_adj = 0; /* tick adjust (scaled 1 / hz) */ 126static long time_reftime = 0; /* time at last adjustment (s) */ 127 |
|
71#ifdef PPS_SYNC 72/* | 128#ifdef PPS_SYNC 129/* |
73 * The following variables are used only if the PPS signal discipline 74 * is configured in the kernel. | 130 * The following variables are used only if the kernel PPS discipline 131 * code is configured (PPS_SYNC). The scale factors are defined in the 132 * timex.h header file. 133 * 134 * pps_time contains the time at each calibration interval, as read by 135 * microtime(). pps_count counts the seconds of the calibration 136 * interval, the duration of which is nominally pps_shift in powers of 137 * two. 138 * 139 * pps_offset is the time offset produced by the time median filter 140 * pps_tf[], while pps_jitter is the dispersion (jitter) measured by 141 * this filter. 142 * 143 * pps_freq is the frequency offset produced by the frequency median 144 * filter pps_ff[], while pps_stabil is the dispersion (wander) measured 145 * by this filter. 146 * 147 * pps_usec is latched from a high resolution counter or external clock 148 * at pps_time. Here we want the hardware counter contents only, not the 149 * contents plus the time_tv.usec as usual. 150 * 151 * pps_valid counts the number of seconds since the last PPS update. It 152 * is used as a watchdog timer to disable the PPS discipline should the 153 * PPS signal be lost. 154 * 155 * pps_glitch counts the number of seconds since the beginning of an 156 * offset burst more than tick/2 from current nominal offset. It is used 157 * mainly to suppress error bursts due to priority conflicts between the 158 * PPS interrupt and timer interrupt. 159 * 160 * pps_intcnt counts the calibration intervals for use in the interval- 161 * adaptation algorithm. It's just too complicated for words. |
75 */ | 162 */ |
76extern int pps_shift; /* interval duration (s) (shift) */ 77extern long pps_freq; /* pps frequency offset (scaled ppm) */ 78extern long pps_jitter; /* pps jitter (us) */ 79extern long pps_stabil; /* pps stability (scaled ppm) */ 80extern long pps_jitcnt; /* jitter limit exceeded */ 81extern long pps_calcnt; /* calibration intervals */ 82extern long pps_errcnt; /* calibration errors */ 83extern long pps_stbcnt; /* stability limit exceeded */ | 163static struct timeval pps_time; /* kernel time at last interval */ 164static long pps_offset = 0; /* pps time offset (us) */ 165static long pps_jitter = MAXTIME; /* pps time dispersion (jitter) (us) */ 166static long pps_tf[] = {0, 0, 0}; /* pps time offset median filter (us) */ 167static long pps_freq = 0; /* frequency offset (scaled ppm) */ 168static long pps_stabil = MAXFREQ; /* frequency dispersion (scaled ppm) */ 169static long pps_ff[] = {0, 0, 0}; /* frequency offset median filter */ 170static long pps_usec = 0; /* microsec counter at last interval */ 171static long pps_valid = PPS_VALID; /* pps signal watchdog counter */ 172static int pps_glitch = 0; /* pps signal glitch counter */ 173static int pps_count = 0; /* calibration interval counter (s) */ 174static int pps_shift = PPS_SHIFT; /* interval duration (s) (shift) */ 175static int pps_intcnt = 0; /* intervals at current duration */ 176 177/* 178 * PPS signal quality monitors 179 * 180 * pps_jitcnt counts the seconds that have been discarded because the 181 * jitter measured by the time median filter exceeds the limit MAXTIME 182 * (100 us). 183 * 184 * pps_calcnt counts the frequency calibration intervals, which are 185 * variable from 4 s to 256 s. 186 * 187 * pps_errcnt counts the calibration intervals which have been discarded 188 * because the wander exceeds the limit MAXFREQ (100 ppm) or where the 189 * calibration interval jitter exceeds two ticks. 190 * 191 * pps_stbcnt counts the calibration intervals that have been discarded 192 * because the frequency wander exceeds the limit MAXFREQ / 4 (25 us). 193 */ 194static long pps_jitcnt = 0; /* jitter limit exceeded */ 195static long pps_calcnt = 0; /* calibration intervals */ 196static long pps_errcnt = 0; /* calibration errors */ 197static long pps_stbcnt = 0; /* stability limit exceeded */ |
84#endif /* PPS_SYNC */ 85 | 198#endif /* PPS_SYNC */ 199 |
200static void hardupdate __P((long offset)); 201 202/* 203 * hardupdate() - local clock update 204 * 205 * This routine is called by ntp_adjtime() to update the local clock 206 * phase and frequency. The implementation is of an adaptive-parameter, 207 * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new 208 * time and frequency offset estimates for each call. If the kernel PPS 209 * discipline code is configured (PPS_SYNC), the PPS signal itself 210 * determines the new time offset, instead of the calling argument. 211 * Presumably, calls to ntp_adjtime() occur only when the caller 212 * believes the local clock is valid within some bound (+-128 ms with 213 * NTP). If the caller's time is far different than the PPS time, an 214 * argument will ensue, and it's not clear who will lose. 215 * 216 * For uncompensated quartz crystal oscillatores and nominal update 217 * intervals less than 1024 s, operation should be in phase-lock mode 218 * (STA_FLL = 0), where the loop is disciplined to phase. For update 219 * intervals greater than thiss, operation should be in frequency-lock 220 * mode (STA_FLL = 1), where the loop is disciplined to frequency. 221 * 222 * Note: splclock() is in effect. 223 */ 224static void 225hardupdate(offset) 226 long offset; 227{ 228 long ltemp, mtemp; 229 230 if (!(time_status & STA_PLL) && !(time_status & STA_PPSTIME)) 231 return; 232 ltemp = offset; 233#ifdef PPS_SYNC 234 if (time_status & STA_PPSTIME && time_status & STA_PPSSIGNAL) 235 ltemp = pps_offset; 236#endif /* PPS_SYNC */ 237 238 /* 239 * Scale the phase adjustment and clamp to the operating range. 240 */ 241 if (ltemp > MAXPHASE) 242 time_offset = MAXPHASE << SHIFT_UPDATE; 243 else if (ltemp < -MAXPHASE) 244 time_offset = -(MAXPHASE << SHIFT_UPDATE); 245 else 246 time_offset = ltemp << SHIFT_UPDATE; 247 248 /* 249 * Select whether the frequency is to be controlled and in which 250 * mode (PLL or FLL). Clamp to the operating range. Ugly 251 * multiply/divide should be replaced someday. 252 */ 253 if (time_status & STA_FREQHOLD || time_reftime == 0) 254 time_reftime = time.tv_sec; 255 mtemp = time.tv_sec - time_reftime; 256 time_reftime = time.tv_sec; 257 if (time_status & STA_FLL) { 258 if (mtemp >= MINSEC) { 259 ltemp = ((time_offset / mtemp) << (SHIFT_USEC - 260 SHIFT_UPDATE)); 261 if (ltemp < 0) 262 time_freq -= -ltemp >> SHIFT_KH; 263 else 264 time_freq += ltemp >> SHIFT_KH; 265 } 266 } else { 267 if (mtemp < MAXSEC) { 268 ltemp *= mtemp; 269 if (ltemp < 0) 270 time_freq -= -ltemp >> (time_constant + 271 time_constant + SHIFT_KF - 272 SHIFT_USEC); 273 else 274 time_freq += ltemp >> (time_constant + 275 time_constant + SHIFT_KF - 276 SHIFT_USEC); 277 } 278 } 279 if (time_freq > time_tolerance) 280 time_freq = time_tolerance; 281 else if (time_freq < -time_tolerance) 282 time_freq = -time_tolerance; 283} 284 285void 286ntp_update_second(long *newsec) 287{ 288 long ltemp; 289 290 time_maxerror += time_tolerance >> SHIFT_USEC; 291 292 /* 293 * Compute the phase adjustment for the next second. In 294 * PLL mode, the offset is reduced by a fixed factor 295 * times the time constant. In FLL mode the offset is 296 * used directly. In either mode, the maximum phase 297 * adjustment for each second is clamped so as to spread 298 * the adjustment over not more than the number of 299 * seconds between updates. 300 */ 301 if (time_offset < 0) { 302 ltemp = -time_offset; 303 if (!(time_status & STA_FLL)) 304 ltemp >>= SHIFT_KG + time_constant; 305 if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE) 306 ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE; 307 time_offset += ltemp; 308 time_adj = -ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE); 309 } else { 310 ltemp = time_offset; 311 if (!(time_status & STA_FLL)) 312 ltemp >>= SHIFT_KG + time_constant; 313 if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE) 314 ltemp = (MAXPHASE / MINSEC) << SHIFT_UPDATE; 315 time_offset -= ltemp; 316 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE); 317 } 318 319 /* 320 * Compute the frequency estimate and additional phase 321 * adjustment due to frequency error for the next 322 * second. When the PPS signal is engaged, gnaw on the 323 * watchdog counter and update the frequency computed by 324 * the pll and the PPS signal. 325 */ 326#ifdef PPS_SYNC 327 pps_valid++; 328 if (pps_valid == PPS_VALID) { 329 pps_jitter = MAXTIME; 330 pps_stabil = MAXFREQ; 331 time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER | 332 STA_PPSWANDER | STA_PPSERROR); 333 } 334 ltemp = time_freq + pps_freq; 335#else 336 ltemp = time_freq; 337#endif /* PPS_SYNC */ 338 if (ltemp < 0) 339 time_adj -= -ltemp >> (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE); 340 else 341 time_adj += ltemp >> (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE); 342 343#if SHIFT_HZ == 7 344 /* 345 * When the CPU clock oscillator frequency is not a 346 * power of two in Hz, the SHIFT_HZ is only an 347 * approximate scale factor. In the SunOS kernel, this 348 * results in a PLL gain factor of 1/1.28 = 0.78 what it 349 * should be. In the following code the overall gain is 350 * increased by a factor of 1.25, which results in a 351 * residual error less than 3 percent. 352 */ 353 /* Same thing applies for FreeBSD --GAW */ 354 if (hz == 100) { 355 if (time_adj < 0) 356 time_adj -= -time_adj >> 2; 357 else 358 time_adj += time_adj >> 2; 359 } 360#endif /* SHIFT_HZ */ 361 362 /* XXX - this is really bogus, but can't be fixed until 363 xntpd's idea of the system clock is fixed to know how 364 the user wants leap seconds handled; in the mean time, 365 we assume that users of NTP are running without proper 366 leap second support (this is now the default anyway) */ 367 /* 368 * Leap second processing. If in leap-insert state at 369 * the end of the day, the system clock is set back one 370 * second; if in leap-delete state, the system clock is 371 * set ahead one second. The microtime() routine or 372 * external clock driver will insure that reported time 373 * is always monotonic. The ugly divides should be 374 * replaced. 375 */ 376 switch (time_state) { 377 378 case TIME_OK: 379 if (time_status & STA_INS) 380 time_state = TIME_INS; 381 else if (time_status & STA_DEL) 382 time_state = TIME_DEL; 383 break; 384 385 case TIME_INS: 386 if ((*newsec) % 86400 == 0) { 387 (*newsec)--; 388 time_state = TIME_OOP; 389 } 390 break; 391 392 case TIME_DEL: 393 if (((*newsec) + 1) % 86400 == 0) { 394 (*newsec)++; 395 time_state = TIME_WAIT; 396 } 397 break; 398 399 case TIME_OOP: 400 time_state = TIME_WAIT; 401 break; 402 403 case TIME_WAIT: 404 if (!(time_status & (STA_INS | STA_DEL))) 405 time_state = TIME_OK; 406 break; 407 } 408} |
|
86static int 87ntp_sysctl SYSCTL_HANDLER_ARGS 88{ 89 struct timeval atv; 90 struct ntptimeval ntv; 91 int s; 92 93 s = splclock(); --- 167 unchanged lines hidden (view full) --- 261 p->p_retval[0] = TIME_ERROR; 262 if (time_status & STA_PPSFREQ && 263 time_status & (STA_PPSWANDER | STA_PPSERROR)) 264 p->p_retval[0] = TIME_ERROR; 265 } 266 return error; 267} 268 | 409static int 410ntp_sysctl SYSCTL_HANDLER_ARGS 411{ 412 struct timeval atv; 413 struct ntptimeval ntv; 414 int s; 415 416 s = splclock(); --- 167 unchanged lines hidden (view full) --- 584 p->p_retval[0] = TIME_ERROR; 585 if (time_status & STA_PPSFREQ && 586 time_status & (STA_PPSWANDER | STA_PPSERROR)) 587 p->p_retval[0] = TIME_ERROR; 588 } 589 return error; 590} 591 |
592#ifdef PPS_SYNC |
|
269 | 593 |
594/* We need this ugly monster twice, so lets macroize it... */ 595 596#define MEDIAN3X(a, m, s, i1, i2, i3) \ 597 do { \ 598 m = a[i2]; \ 599 s = a[i1] - a[i3]; \ 600 } while (0) 601 602#define MEDIAN3(a, m, s) \ 603 do { \ 604 if (a[0] > a[1]) { \ 605 if (a[1] > a[2]) \ 606 MEDIAN3X(a, m, s, 0, 1, 2); \ 607 else if (a[2] > a[0]) \ 608 MEDIAN3X(a, m, s, 2, 0, 1); \ 609 else \ 610 MEDIAN3X(a, m, s, 0, 2, 1); \ 611 } else { \ 612 if (a[2] > a[1]) \ 613 MEDIAN3X(a, m, s, 2, 1, 0); \ 614 else if (a[0] > a[2]) \ 615 MEDIAN3X(a, m, s, 1, 0, 2); \ 616 else \ 617 MEDIAN3X(a, m, s, 1, 2, 0); \ 618 } \ 619 } while (0) 620 621/* 622 * hardpps() - discipline CPU clock oscillator to external PPS signal 623 * 624 * This routine is called at each PPS interrupt in order to discipline 625 * the CPU clock oscillator to the PPS signal. It measures the PPS phase 626 * and leaves it in a handy spot for the hardclock() routine. It 627 * integrates successive PPS phase differences and calculates the 628 * frequency offset. This is used in hardclock() to discipline the CPU 629 * clock oscillator so that intrinsic frequency error is cancelled out. 630 * The code requires the caller to capture the time and hardware counter 631 * value at the on-time PPS signal transition. 632 * 633 * Note that, on some Unix systems, this routine runs at an interrupt 634 * priority level higher than the timer interrupt routine hardclock(). 635 * Therefore, the variables used are distinct from the hardclock() 636 * variables, except for certain exceptions: The PPS frequency pps_freq 637 * and phase pps_offset variables are determined by this routine and 638 * updated atomically. The time_tolerance variable can be considered a 639 * constant, since it is infrequently changed, and then only when the 640 * PPS signal is disabled. The watchdog counter pps_valid is updated 641 * once per second by hardclock() and is atomically cleared in this 642 * routine. 643 */ 644void 645hardpps(tvp, p_usec) 646 struct timeval *tvp; /* time at PPS */ 647 long p_usec; /* hardware counter at PPS */ 648{ 649 long u_usec, v_usec, bigtick; 650 long cal_sec, cal_usec; 651 652 /* 653 * An occasional glitch can be produced when the PPS interrupt 654 * occurs in the hardclock() routine before the time variable is 655 * updated. Here the offset is discarded when the difference 656 * between it and the last one is greater than tick/2, but not 657 * if the interval since the first discard exceeds 30 s. 658 */ 659 time_status |= STA_PPSSIGNAL; 660 time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR); 661 pps_valid = 0; 662 u_usec = -tvp->tv_usec; 663 if (u_usec < -500000) 664 u_usec += 1000000; 665 v_usec = pps_offset - u_usec; 666 if (v_usec < 0) 667 v_usec = -v_usec; 668 if (v_usec > (tick >> 1)) { 669 if (pps_glitch > MAXGLITCH) { 670 pps_glitch = 0; 671 pps_tf[2] = u_usec; 672 pps_tf[1] = u_usec; 673 } else { 674 pps_glitch++; 675 u_usec = pps_offset; 676 } 677 } else 678 pps_glitch = 0; 679 680 /* 681 * A three-stage median filter is used to help deglitch the pps 682 * time. The median sample becomes the time offset estimate; the 683 * difference between the other two samples becomes the time 684 * dispersion (jitter) estimate. 685 */ 686 pps_tf[2] = pps_tf[1]; 687 pps_tf[1] = pps_tf[0]; 688 pps_tf[0] = u_usec; 689 690 MEDIAN3(pps_tf, pps_offset, v_usec); 691 692 if (v_usec > MAXTIME) 693 pps_jitcnt++; 694 v_usec = (v_usec << PPS_AVG) - pps_jitter; 695 if (v_usec < 0) 696 pps_jitter -= -v_usec >> PPS_AVG; 697 else 698 pps_jitter += v_usec >> PPS_AVG; 699 if (pps_jitter > (MAXTIME >> 1)) 700 time_status |= STA_PPSJITTER; 701 702 /* 703 * During the calibration interval adjust the starting time when 704 * the tick overflows. At the end of the interval compute the 705 * duration of the interval and the difference of the hardware 706 * counters at the beginning and end of the interval. This code 707 * is deliciously complicated by the fact valid differences may 708 * exceed the value of tick when using long calibration 709 * intervals and small ticks. Note that the counter can be 710 * greater than tick if caught at just the wrong instant, but 711 * the values returned and used here are correct. 712 */ 713 bigtick = (long)tick << SHIFT_USEC; 714 pps_usec -= pps_freq; 715 if (pps_usec >= bigtick) 716 pps_usec -= bigtick; 717 if (pps_usec < 0) 718 pps_usec += bigtick; 719 pps_time.tv_sec++; 720 pps_count++; 721 if (pps_count < (1 << pps_shift)) 722 return; 723 pps_count = 0; 724 pps_calcnt++; 725 u_usec = p_usec << SHIFT_USEC; 726 v_usec = pps_usec - u_usec; 727 if (v_usec >= bigtick >> 1) 728 v_usec -= bigtick; 729 if (v_usec < -(bigtick >> 1)) 730 v_usec += bigtick; 731 if (v_usec < 0) 732 v_usec = -(-v_usec >> pps_shift); 733 else 734 v_usec = v_usec >> pps_shift; 735 pps_usec = u_usec; 736 cal_sec = tvp->tv_sec; 737 cal_usec = tvp->tv_usec; 738 cal_sec -= pps_time.tv_sec; 739 cal_usec -= pps_time.tv_usec; 740 if (cal_usec < 0) { 741 cal_usec += 1000000; 742 cal_sec--; 743 } 744 pps_time = *tvp; 745 746 /* 747 * Check for lost interrupts, noise, excessive jitter and 748 * excessive frequency error. The number of timer ticks during 749 * the interval may vary +-1 tick. Add to this a margin of one 750 * tick for the PPS signal jitter and maximum frequency 751 * deviation. If the limits are exceeded, the calibration 752 * interval is reset to the minimum and we start over. 753 */ 754 u_usec = (long)tick << 1; 755 if (!((cal_sec == -1 && cal_usec > (1000000 - u_usec)) 756 || (cal_sec == 0 && cal_usec < u_usec)) 757 || v_usec > time_tolerance || v_usec < -time_tolerance) { 758 pps_errcnt++; 759 pps_shift = PPS_SHIFT; 760 pps_intcnt = 0; 761 time_status |= STA_PPSERROR; 762 return; 763 } 764 765 /* 766 * A three-stage median filter is used to help deglitch the pps 767 * frequency. The median sample becomes the frequency offset 768 * estimate; the difference between the other two samples 769 * becomes the frequency dispersion (stability) estimate. 770 */ 771 pps_ff[2] = pps_ff[1]; 772 pps_ff[1] = pps_ff[0]; 773 pps_ff[0] = v_usec; 774 775 MEDIAN3(pps_ff, u_usec, v_usec); 776 777 /* 778 * Here the frequency dispersion (stability) is updated. If it 779 * is less than one-fourth the maximum (MAXFREQ), the frequency 780 * offset is updated as well, but clamped to the tolerance. It 781 * will be processed later by the hardclock() routine. 782 */ 783 v_usec = (v_usec >> 1) - pps_stabil; 784 if (v_usec < 0) 785 pps_stabil -= -v_usec >> PPS_AVG; 786 else 787 pps_stabil += v_usec >> PPS_AVG; 788 if (pps_stabil > MAXFREQ >> 2) { 789 pps_stbcnt++; 790 time_status |= STA_PPSWANDER; 791 return; 792 } 793 if (time_status & STA_PPSFREQ) { 794 if (u_usec < 0) { 795 pps_freq -= -u_usec >> PPS_AVG; 796 if (pps_freq < -time_tolerance) 797 pps_freq = -time_tolerance; 798 u_usec = -u_usec; 799 } else { 800 pps_freq += u_usec >> PPS_AVG; 801 if (pps_freq > time_tolerance) 802 pps_freq = time_tolerance; 803 } 804 } 805 806 /* 807 * Here the calibration interval is adjusted. If the maximum 808 * time difference is greater than tick / 4, reduce the interval 809 * by half. If this is not the case for four consecutive 810 * intervals, double the interval. 811 */ 812 if (u_usec << pps_shift > bigtick >> 2) { 813 pps_intcnt = 0; 814 if (pps_shift > PPS_SHIFT) 815 pps_shift--; 816 } else if (pps_intcnt >= 4) { 817 pps_intcnt = 0; 818 if (pps_shift < PPS_SHIFTMAX) 819 pps_shift++; 820 } else 821 pps_intcnt++; 822} 823#endif /* PPS_SYNC */ |
|