kern_tc.c (32444) | kern_tc.c (32513) |
---|---|
1/*- 2 * Copyright (c) 1982, 1986, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. --- 22 unchanged lines hidden (view full) --- 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94 | 1/*- 2 * Copyright (c) 1982, 1986, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. --- 22 unchanged lines hidden (view full) --- 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94 |
39 * $Id: kern_clock.c,v 1.51 1998/01/11 00:44:27 phk Exp $ | 39 * $Id: kern_clock.c,v 1.52 1998/01/11 19:07:58 phk Exp $ |
40 */ 41 | 40 */ 41 |
42/* Portions of this software are covered by the following: */ 43/****************************************************************************** 44 * * 45 * Copyright (c) David L. Mills 1993, 1994 * 46 * * 47 * Permission to use, copy, modify, and distribute this software and its * 48 * documentation for any purpose and without fee is hereby granted, provided * 49 * that the above copyright notice appears in all copies and that both the * 50 * copyright notice and this permission notice appear in supporting * 51 * documentation, and that the name University of Delaware not be used in * 52 * advertising or publicity pertaining to distribution of the software * 53 * without specific, written prior permission. The University of Delaware * 54 * makes no representations about the suitability this software for any * 55 * purpose. It is provided "as is" without express or implied warranty. * 56 * * 57 *****************************************************************************/ 58 | |
59#include <sys/param.h> 60#include <sys/systm.h> 61#include <sys/dkstat.h> 62#include <sys/callout.h> 63#include <sys/kernel.h> 64#include <sys/proc.h> 65#include <sys/resourcevar.h> 66#include <sys/signalvar.h> --- 90 unchanged lines hidden (view full) --- 157int ticks; 158static int psdiv, pscnt; /* prof => stat divider */ 159int psratio; /* ratio: prof / stat */ 160 161volatile struct timeval time; 162volatile struct timeval mono_time; 163 164/* | 42#include <sys/param.h> 43#include <sys/systm.h> 44#include <sys/dkstat.h> 45#include <sys/callout.h> 46#include <sys/kernel.h> 47#include <sys/proc.h> 48#include <sys/resourcevar.h> 49#include <sys/signalvar.h> --- 90 unchanged lines hidden (view full) --- 140int ticks; 141static int psdiv, pscnt; /* prof => stat divider */ 142int psratio; /* ratio: prof / stat */ 143 144volatile struct timeval time; 145volatile struct timeval mono_time; 146 147/* |
165 * Phase/frequency-lock loop (PLL/FLL) definitions 166 * 167 * The following variables are read and set by the ntp_adjtime() system 168 * call. 169 * 170 * time_state shows the state of the system clock, with values defined 171 * in the timex.h header file. 172 * 173 * time_status shows the status of the system clock, with bits defined 174 * in the timex.h header file. 175 * 176 * time_offset is used by the PLL/FLL to adjust the system time in small 177 * increments. 178 * 179 * time_constant determines the bandwidth or "stiffness" of the PLL. 180 * 181 * time_tolerance determines maximum frequency error or tolerance of the 182 * CPU clock oscillator and is a property of the architecture; however, 183 * in principle it could change as result of the presence of external 184 * discipline signals, for instance. 185 * 186 * time_precision is usually equal to the kernel tick variable; however, 187 * in cases where a precision clock counter or external clock is 188 * available, the resolution can be much less than this and depend on 189 * whether the external clock is working or not. 190 * 191 * time_maxerror is initialized by a ntp_adjtime() call and increased by 192 * the kernel once each second to reflect the maximum error 193 * bound growth. 194 * 195 * time_esterror is set and read by the ntp_adjtime() call, but 196 * otherwise not used by the kernel. 197 */ 198int time_status = STA_UNSYNC; /* clock status bits */ 199int time_state = TIME_OK; /* clock state */ 200long time_offset = 0; /* time offset (us) */ 201long time_constant = 0; /* pll time constant */ 202long time_tolerance = MAXFREQ; /* frequency tolerance (scaled ppm) */ 203long time_precision = 1; /* clock precision (us) */ 204long time_maxerror = MAXPHASE; /* maximum error (us) */ 205long time_esterror = MAXPHASE; /* estimated error (us) */ 206 207/* 208 * The following variables establish the state of the PLL/FLL and the 209 * residual time and frequency offset of the local clock. The scale 210 * factors are defined in the timex.h header file. 211 * 212 * time_phase and time_freq are the phase increment and the frequency 213 * increment, respectively, of the kernel time variable at each tick of 214 * the clock. 215 * 216 * time_freq is set via ntp_adjtime() from a value stored in a file when 217 * the synchronization daemon is first started. Its value is retrieved 218 * via ntp_adjtime() and written to the file about once per hour by the 219 * daemon. 220 * 221 * time_adj is the adjustment added to the value of tick at each timer 222 * interrupt and is recomputed from time_phase and time_freq at each 223 * seconds rollover. 224 * 225 * time_reftime is the second's portion of the system time on the last 226 * call to ntp_adjtime(). It is used to adjust the time_freq variable 227 * and to increase the time_maxerror as the time since last update 228 * increases. 229 */ 230static long time_phase = 0; /* phase offset (scaled us) */ 231long time_freq = 0; /* frequency offset (scaled ppm) */ 232static long time_adj = 0; /* tick adjust (scaled 1 / hz) */ 233static long time_reftime = 0; /* time at last adjustment (s) */ 234 235#ifdef PPS_SYNC 236/* 237 * The following variables are used only if the kernel PPS discipline 238 * code is configured (PPS_SYNC). The scale factors are defined in the 239 * timex.h header file. 240 * 241 * pps_time contains the time at each calibration interval, as read by 242 * microtime(). pps_count counts the seconds of the calibration 243 * interval, the duration of which is nominally pps_shift in powers of 244 * two. 245 * 246 * pps_offset is the time offset produced by the time median filter 247 * pps_tf[], while pps_jitter is the dispersion (jitter) measured by 248 * this filter. 249 * 250 * pps_freq is the frequency offset produced by the frequency median 251 * filter pps_ff[], while pps_stabil is the dispersion (wander) measured 252 * by this filter. 253 * 254 * pps_usec is latched from a high resolution counter or external clock 255 * at pps_time. Here we want the hardware counter contents only, not the 256 * contents plus the time_tv.usec as usual. 257 * 258 * pps_valid counts the number of seconds since the last PPS update. It 259 * is used as a watchdog timer to disable the PPS discipline should the 260 * PPS signal be lost. 261 * 262 * pps_glitch counts the number of seconds since the beginning of an 263 * offset burst more than tick/2 from current nominal offset. It is used 264 * mainly to suppress error bursts due to priority conflicts between the 265 * PPS interrupt and timer interrupt. 266 * 267 * pps_intcnt counts the calibration intervals for use in the interval- 268 * adaptation algorithm. It's just too complicated for words. 269 */ 270struct timeval pps_time; /* kernel time at last interval */ 271long pps_offset = 0; /* pps time offset (us) */ 272long pps_jitter = MAXTIME; /* pps time dispersion (jitter) (us) */ 273long pps_tf[] = {0, 0, 0}; /* pps time offset median filter (us) */ 274long pps_freq = 0; /* frequency offset (scaled ppm) */ 275long pps_stabil = MAXFREQ; /* frequency dispersion (scaled ppm) */ 276long pps_ff[] = {0, 0, 0}; /* frequency offset median filter */ 277long pps_usec = 0; /* microsec counter at last interval */ 278long pps_valid = PPS_VALID; /* pps signal watchdog counter */ 279int pps_glitch = 0; /* pps signal glitch counter */ 280int pps_count = 0; /* calibration interval counter (s) */ 281int pps_shift = PPS_SHIFT; /* interval duration (s) (shift) */ 282int pps_intcnt = 0; /* intervals at current duration */ 283 284/* 285 * PPS signal quality monitors 286 * 287 * pps_jitcnt counts the seconds that have been discarded because the 288 * jitter measured by the time median filter exceeds the limit MAXTIME 289 * (100 us). 290 * 291 * pps_calcnt counts the frequency calibration intervals, which are 292 * variable from 4 s to 256 s. 293 * 294 * pps_errcnt counts the calibration intervals which have been discarded 295 * because the wander exceeds the limit MAXFREQ (100 ppm) or where the 296 * calibration interval jitter exceeds two ticks. 297 * 298 * pps_stbcnt counts the calibration intervals that have been discarded 299 * because the frequency wander exceeds the limit MAXFREQ / 4 (25 us). 300 */ 301long pps_jitcnt = 0; /* jitter limit exceeded */ 302long pps_calcnt = 0; /* calibration intervals */ 303long pps_errcnt = 0; /* calibration errors */ 304long pps_stbcnt = 0; /* stability limit exceeded */ 305#endif /* PPS_SYNC */ 306 307/* XXX none of this stuff works under FreeBSD */ 308 309/* 310 * hardupdate() - local clock update 311 * 312 * This routine is called by ntp_adjtime() to update the local clock 313 * phase and frequency. The implementation is of an adaptive-parameter, 314 * hybrid phase/frequency-lock loop (PLL/FLL). The routine computes new 315 * time and frequency offset estimates for each call. If the kernel PPS 316 * discipline code is configured (PPS_SYNC), the PPS signal itself 317 * determines the new time offset, instead of the calling argument. 318 * Presumably, calls to ntp_adjtime() occur only when the caller 319 * believes the local clock is valid within some bound (+-128 ms with 320 * NTP). If the caller's time is far different than the PPS time, an 321 * argument will ensue, and it's not clear who will lose. 322 * 323 * For uncompensated quartz crystal oscillatores and nominal update 324 * intervals less than 1024 s, operation should be in phase-lock mode 325 * (STA_FLL = 0), where the loop is disciplined to phase. For update 326 * intervals greater than thiss, operation should be in frequency-lock 327 * mode (STA_FLL = 1), where the loop is disciplined to frequency. 328 * 329 * Note: splclock() is in effect. 330 */ 331void 332hardupdate(offset) 333 long offset; 334{ 335 long ltemp, mtemp; 336 337 if (!(time_status & STA_PLL) && !(time_status & STA_PPSTIME)) 338 return; 339 ltemp = offset; 340#ifdef PPS_SYNC 341 if (time_status & STA_PPSTIME && time_status & STA_PPSSIGNAL) 342 ltemp = pps_offset; 343#endif /* PPS_SYNC */ 344 345 /* 346 * Scale the phase adjustment and clamp to the operating range. 347 */ 348 if (ltemp > MAXPHASE) 349 time_offset = MAXPHASE << SHIFT_UPDATE; 350 else if (ltemp < -MAXPHASE) 351 time_offset = -(MAXPHASE << SHIFT_UPDATE); 352 else 353 time_offset = ltemp << SHIFT_UPDATE; 354 355 /* 356 * Select whether the frequency is to be controlled and in which 357 * mode (PLL or FLL). Clamp to the operating range. Ugly 358 * multiply/divide should be replaced someday. 359 */ 360 if (time_status & STA_FREQHOLD || time_reftime == 0) 361 time_reftime = time.tv_sec; 362 mtemp = time.tv_sec - time_reftime; 363 time_reftime = time.tv_sec; 364 if (time_status & STA_FLL) { 365 if (mtemp >= MINSEC) { 366 ltemp = ((time_offset / mtemp) << (SHIFT_USEC - 367 SHIFT_UPDATE)); 368 if (ltemp < 0) 369 time_freq -= -ltemp >> SHIFT_KH; 370 else 371 time_freq += ltemp >> SHIFT_KH; 372 } 373 } else { 374 if (mtemp < MAXSEC) { 375 ltemp *= mtemp; 376 if (ltemp < 0) 377 time_freq -= -ltemp >> (time_constant + 378 time_constant + SHIFT_KF - 379 SHIFT_USEC); 380 else 381 time_freq += ltemp >> (time_constant + 382 time_constant + SHIFT_KF - 383 SHIFT_USEC); 384 } 385 } 386 if (time_freq > time_tolerance) 387 time_freq = time_tolerance; 388 else if (time_freq < -time_tolerance) 389 time_freq = -time_tolerance; 390} 391 392 393 394/* | |
395 * Initialize clock frequencies and start both clocks running. 396 */ 397/* ARGSUSED*/ 398static void 399initclocks(dummy) 400 void *dummy; 401{ 402 register int i; --- 17 unchanged lines hidden (view full) --- 420/* 421 * The real-time timer, interrupting hz times per second. 422 */ 423void 424hardclock(frame) 425 register struct clockframe *frame; 426{ 427 register struct proc *p; | 148 * Initialize clock frequencies and start both clocks running. 149 */ 150/* ARGSUSED*/ 151static void 152initclocks(dummy) 153 void *dummy; 154{ 155 register int i; --- 17 unchanged lines hidden (view full) --- 173/* 174 * The real-time timer, interrupting hz times per second. 175 */ 176void 177hardclock(frame) 178 register struct clockframe *frame; 179{ 180 register struct proc *p; |
181 int time_update; 182 struct timeval newtime = time; 183 long ltemp; |
|
428 429 p = curproc; 430 if (p) { 431 register struct pstats *pstats; 432 433 /* 434 * Run current process's virtual and profile time, as needed. 435 */ --- 15 unchanged lines hidden (view full) --- 451 */ 452 if (stathz == 0) 453 statclock(frame); 454 455 /* 456 * Increment the time-of-day. 457 */ 458 ticks++; | 184 185 p = curproc; 186 if (p) { 187 register struct pstats *pstats; 188 189 /* 190 * Run current process's virtual and profile time, as needed. 191 */ --- 15 unchanged lines hidden (view full) --- 207 */ 208 if (stathz == 0) 209 statclock(frame); 210 211 /* 212 * Increment the time-of-day. 213 */ 214 ticks++; |
459 { 460 int time_update; 461 struct timeval newtime = time; 462 long ltemp; | |
463 | 215 |
464 if (timedelta == 0) { 465 time_update = CPU_THISTICKLEN(tick); 466 } else { 467 time_update = CPU_THISTICKLEN(tick) + tickdelta; 468 timedelta -= tickdelta; 469 } 470 BUMPTIME(&mono_time, time_update); | 216 if (timedelta == 0) { 217 time_update = CPU_THISTICKLEN(tick); 218 } else { 219 time_update = CPU_THISTICKLEN(tick) + tickdelta; 220 timedelta -= tickdelta; 221 } 222 BUMPTIME(&mono_time, time_update); |
471 | 223 |
472 /* 473 * Compute the phase adjustment. If the low-order bits 474 * (time_phase) of the update overflow, bump the high-order bits 475 * (time_update). 476 */ 477 time_phase += time_adj; 478 if (time_phase <= -FINEUSEC) { 479 ltemp = -time_phase >> SHIFT_SCALE; 480 time_phase += ltemp << SHIFT_SCALE; 481 time_update -= ltemp; 482 } 483 else if (time_phase >= FINEUSEC) { 484 ltemp = time_phase >> SHIFT_SCALE; 485 time_phase -= ltemp << SHIFT_SCALE; 486 time_update += ltemp; 487 } | 224 /* 225 * Compute the phase adjustment. If the low-order bits 226 * (time_phase) of the update overflow, bump the high-order bits 227 * (time_update). 228 */ 229 time_phase += time_adj; 230 if (time_phase <= -FINEUSEC) { 231 ltemp = -time_phase >> SHIFT_SCALE; 232 time_phase += ltemp << SHIFT_SCALE; 233 time_update -= ltemp; 234 } 235 else if (time_phase >= FINEUSEC) { 236 ltemp = time_phase >> SHIFT_SCALE; 237 time_phase -= ltemp << SHIFT_SCALE; 238 time_update += ltemp; 239 } |
488 | 240 |
489 newtime.tv_usec += time_update; 490 /* 491 * On rollover of the second the phase adjustment to be used for 492 * the next second is calculated. Also, the maximum error is 493 * increased by the tolerance. If the PPS frequency discipline 494 * code is present, the phase is increased to compensate for the 495 * CPU clock oscillator frequency error. 496 * 497 * On a 32-bit machine and given parameters in the timex.h 498 * header file, the maximum phase adjustment is +-512 ms and 499 * maximum frequency offset is a tad less than) +-512 ppm. On a 500 * 64-bit machine, you shouldn't need to ask. 501 */ 502 if (newtime.tv_usec >= 1000000) { 503 newtime.tv_usec -= 1000000; 504 newtime.tv_sec++; 505 time_maxerror += time_tolerance >> SHIFT_USEC; 506 507 /* 508 * Compute the phase adjustment for the next second. In 509 * PLL mode, the offset is reduced by a fixed factor 510 * times the time constant. In FLL mode the offset is 511 * used directly. In either mode, the maximum phase 512 * adjustment for each second is clamped so as to spread 513 * the adjustment over not more than the number of 514 * seconds between updates. 515 */ 516 if (time_offset < 0) { 517 ltemp = -time_offset; 518 if (!(time_status & STA_FLL)) 519 ltemp >>= SHIFT_KG + time_constant; 520 if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE) 521 ltemp = (MAXPHASE / MINSEC) << 522 SHIFT_UPDATE; 523 time_offset += ltemp; 524 time_adj = -ltemp << (SHIFT_SCALE - SHIFT_HZ - 525 SHIFT_UPDATE); 526 } else { 527 ltemp = time_offset; 528 if (!(time_status & STA_FLL)) 529 ltemp >>= SHIFT_KG + time_constant; 530 if (ltemp > (MAXPHASE / MINSEC) << SHIFT_UPDATE) 531 ltemp = (MAXPHASE / MINSEC) << 532 SHIFT_UPDATE; 533 time_offset -= ltemp; 534 time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - 535 SHIFT_UPDATE); 536 } 537 538 /* 539 * Compute the frequency estimate and additional phase 540 * adjustment due to frequency error for the next 541 * second. When the PPS signal is engaged, gnaw on the 542 * watchdog counter and update the frequency computed by 543 * the pll and the PPS signal. 544 */ 545#ifdef PPS_SYNC 546 pps_valid++; 547 if (pps_valid == PPS_VALID) { 548 pps_jitter = MAXTIME; 549 pps_stabil = MAXFREQ; 550 time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER | 551 STA_PPSWANDER | STA_PPSERROR); 552 } 553 ltemp = time_freq + pps_freq; 554#else 555 ltemp = time_freq; 556#endif /* PPS_SYNC */ 557 if (ltemp < 0) 558 time_adj -= -ltemp >> 559 (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE); 560 else 561 time_adj += ltemp >> 562 (SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE); 563 564#if SHIFT_HZ == 7 565 /* 566 * When the CPU clock oscillator frequency is not a 567 * power of two in Hz, the SHIFT_HZ is only an 568 * approximate scale factor. In the SunOS kernel, this 569 * results in a PLL gain factor of 1/1.28 = 0.78 what it 570 * should be. In the following code the overall gain is 571 * increased by a factor of 1.25, which results in a 572 * residual error less than 3 percent. 573 */ 574 /* Same thing applies for FreeBSD --GAW */ 575 if (hz == 100) { 576 if (time_adj < 0) 577 time_adj -= -time_adj >> 2; 578 else 579 time_adj += time_adj >> 2; 580 } 581#endif /* SHIFT_HZ */ 582 583 /* XXX - this is really bogus, but can't be fixed until 584 xntpd's idea of the system clock is fixed to know how 585 the user wants leap seconds handled; in the mean time, 586 we assume that users of NTP are running without proper 587 leap second support (this is now the default anyway) */ 588 /* 589 * Leap second processing. If in leap-insert state at 590 * the end of the day, the system clock is set back one 591 * second; if in leap-delete state, the system clock is 592 * set ahead one second. The microtime() routine or 593 * external clock driver will insure that reported time 594 * is always monotonic. The ugly divides should be 595 * replaced. 596 */ 597 switch (time_state) { 598 599 case TIME_OK: 600 if (time_status & STA_INS) 601 time_state = TIME_INS; 602 else if (time_status & STA_DEL) 603 time_state = TIME_DEL; 604 break; 605 606 case TIME_INS: 607 if (newtime.tv_sec % 86400 == 0) { 608 newtime.tv_sec--; 609 time_state = TIME_OOP; 610 } 611 break; 612 613 case TIME_DEL: 614 if ((newtime.tv_sec + 1) % 86400 == 0) { 615 newtime.tv_sec++; 616 time_state = TIME_WAIT; 617 } 618 break; 619 620 case TIME_OOP: 621 time_state = TIME_WAIT; 622 break; 623 624 case TIME_WAIT: 625 if (!(time_status & (STA_INS | STA_DEL))) 626 time_state = TIME_OK; 627 } 628 } 629 CPU_CLOCKUPDATE(&time, &newtime); | 241 newtime.tv_usec += time_update; 242 /* 243 * On rollover of the second the phase adjustment to be used for 244 * the next second is calculated. Also, the maximum error is 245 * increased by the tolerance. If the PPS frequency discipline 246 * code is present, the phase is increased to compensate for the 247 * CPU clock oscillator frequency error. 248 * 249 * On a 32-bit machine and given parameters in the timex.h 250 * header file, the maximum phase adjustment is +-512 ms and 251 * maximum frequency offset is a tad less than) +-512 ppm. On a 252 * 64-bit machine, you shouldn't need to ask. 253 */ 254 if (newtime.tv_usec >= 1000000) { 255 newtime.tv_usec -= 1000000; 256 newtime.tv_sec++; 257 ntp_update_second(&newtime.tv_sec); |
630 } | 258 } |
631 632 if (TAILQ_FIRST(&callwheel[ticks & callwheelmask]) != NULL) { | 259 CPU_CLOCKUPDATE(&time, &newtime); 260 261 if (TAILQ_FIRST(&callwheel[ticks & callwheelmask]) != NULL) |
633 setsoftclock(); | 262 setsoftclock(); |
634 } | |
635} 636 637void 638gettime(struct timeval *tvp) 639{ 640 int s; 641 642 s = splclock(); --- 252 unchanged lines hidden (view full) --- 895 clkinfo.profhz = profhz; 896 clkinfo.stathz = stathz ? stathz : hz; 897 return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req)); 898} 899 900SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, CTLTYPE_STRUCT|CTLFLAG_RD, 901 0, 0, sysctl_kern_clockrate, "S,clockinfo",""); 902 | 263} 264 265void 266gettime(struct timeval *tvp) 267{ 268 int s; 269 270 s = splclock(); --- 252 unchanged lines hidden (view full) --- 523 clkinfo.profhz = profhz; 524 clkinfo.stathz = stathz ? stathz : hz; 525 return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req)); 526} 527 528SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, CTLTYPE_STRUCT|CTLFLAG_RD, 529 0, 0, sysctl_kern_clockrate, "S,clockinfo",""); 530 |
903#ifdef PPS_SYNC 904 905/* We need this ugly monster twice, so lets macroize it... */ 906 907#define MEDIAN3(a, m, s) \ 908 do { \ 909 if (a[0] > a[1]) { \ 910 if (a[1] > a[2]) { \ 911 /* 0 1 2 */ \ 912 m = a[1]; \ 913 s = a[0] - a[2]; \ 914 } else if (a[2] > a[0]) { \ 915 /* 2 0 1 */ \ 916 m = a[0]; \ 917 s = a[2] - a[1]; \ 918 } else { \ 919 /* 0 2 1 */ \ 920 m = a[2]; \ 921 s = a[0] - a[1]; \ 922 } \ 923 } else { \ 924 if (a[1] < a[2]) { \ 925 /* 2 1 0 */ \ 926 m = a[1]; \ 927 s = a[2] - a[0]; \ 928 } else if (a[2] < a[0]) { \ 929 /* 1 0 2 */ \ 930 m = a[0]; \ 931 s = a[1] - a[2]; \ 932 } else { \ 933 /* 1 2 0 */ \ 934 m = a[2]; \ 935 s = a[1] - a[0]; \ 936 } \ 937 } \ 938 } while (0) 939 940/* 941 * hardpps() - discipline CPU clock oscillator to external PPS signal 942 * 943 * This routine is called at each PPS interrupt in order to discipline 944 * the CPU clock oscillator to the PPS signal. It measures the PPS phase 945 * and leaves it in a handy spot for the hardclock() routine. It 946 * integrates successive PPS phase differences and calculates the 947 * frequency offset. This is used in hardclock() to discipline the CPU 948 * clock oscillator so that intrinsic frequency error is cancelled out. 949 * The code requires the caller to capture the time and hardware counter 950 * value at the on-time PPS signal transition. 951 * 952 * Note that, on some Unix systems, this routine runs at an interrupt 953 * priority level higher than the timer interrupt routine hardclock(). 954 * Therefore, the variables used are distinct from the hardclock() 955 * variables, except for certain exceptions: The PPS frequency pps_freq 956 * and phase pps_offset variables are determined by this routine and 957 * updated atomically. The time_tolerance variable can be considered a 958 * constant, since it is infrequently changed, and then only when the 959 * PPS signal is disabled. The watchdog counter pps_valid is updated 960 * once per second by hardclock() and is atomically cleared in this 961 * routine. 962 */ 963void 964hardpps(tvp, p_usec) 965 struct timeval *tvp; /* time at PPS */ 966 long p_usec; /* hardware counter at PPS */ 967{ 968 long u_usec, v_usec, bigtick; 969 long cal_sec, cal_usec; 970 971 /* 972 * An occasional glitch can be produced when the PPS interrupt 973 * occurs in the hardclock() routine before the time variable is 974 * updated. Here the offset is discarded when the difference 975 * between it and the last one is greater than tick/2, but not 976 * if the interval since the first discard exceeds 30 s. 977 */ 978 time_status |= STA_PPSSIGNAL; 979 time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR); 980 pps_valid = 0; 981 u_usec = -tvp->tv_usec; 982 if (u_usec < -500000) 983 u_usec += 1000000; 984 v_usec = pps_offset - u_usec; 985 if (v_usec < 0) 986 v_usec = -v_usec; 987 if (v_usec > (tick >> 1)) { 988 if (pps_glitch > MAXGLITCH) { 989 pps_glitch = 0; 990 pps_tf[2] = u_usec; 991 pps_tf[1] = u_usec; 992 } else { 993 pps_glitch++; 994 u_usec = pps_offset; 995 } 996 } else 997 pps_glitch = 0; 998 999 /* 1000 * A three-stage median filter is used to help deglitch the pps 1001 * time. The median sample becomes the time offset estimate; the 1002 * difference between the other two samples becomes the time 1003 * dispersion (jitter) estimate. 1004 */ 1005 pps_tf[2] = pps_tf[1]; 1006 pps_tf[1] = pps_tf[0]; 1007 pps_tf[0] = u_usec; 1008 1009 MEDIAN3(pps_tf, pps_offset, v_usec); 1010 1011 if (v_usec > MAXTIME) 1012 pps_jitcnt++; 1013 v_usec = (v_usec << PPS_AVG) - pps_jitter; 1014 if (v_usec < 0) 1015 pps_jitter -= -v_usec >> PPS_AVG; 1016 else 1017 pps_jitter += v_usec >> PPS_AVG; 1018 if (pps_jitter > (MAXTIME >> 1)) 1019 time_status |= STA_PPSJITTER; 1020 1021 /* 1022 * During the calibration interval adjust the starting time when 1023 * the tick overflows. At the end of the interval compute the 1024 * duration of the interval and the difference of the hardware 1025 * counters at the beginning and end of the interval. This code 1026 * is deliciously complicated by the fact valid differences may 1027 * exceed the value of tick when using long calibration 1028 * intervals and small ticks. Note that the counter can be 1029 * greater than tick if caught at just the wrong instant, but 1030 * the values returned and used here are correct. 1031 */ 1032 bigtick = (long)tick << SHIFT_USEC; 1033 pps_usec -= pps_freq; 1034 if (pps_usec >= bigtick) 1035 pps_usec -= bigtick; 1036 if (pps_usec < 0) 1037 pps_usec += bigtick; 1038 pps_time.tv_sec++; 1039 pps_count++; 1040 if (pps_count < (1 << pps_shift)) 1041 return; 1042 pps_count = 0; 1043 pps_calcnt++; 1044 u_usec = p_usec << SHIFT_USEC; 1045 v_usec = pps_usec - u_usec; 1046 if (v_usec >= bigtick >> 1) 1047 v_usec -= bigtick; 1048 if (v_usec < -(bigtick >> 1)) 1049 v_usec += bigtick; 1050 if (v_usec < 0) 1051 v_usec = -(-v_usec >> pps_shift); 1052 else 1053 v_usec = v_usec >> pps_shift; 1054 pps_usec = u_usec; 1055 cal_sec = tvp->tv_sec; 1056 cal_usec = tvp->tv_usec; 1057 cal_sec -= pps_time.tv_sec; 1058 cal_usec -= pps_time.tv_usec; 1059 if (cal_usec < 0) { 1060 cal_usec += 1000000; 1061 cal_sec--; 1062 } 1063 pps_time = *tvp; 1064 1065 /* 1066 * Check for lost interrupts, noise, excessive jitter and 1067 * excessive frequency error. The number of timer ticks during 1068 * the interval may vary +-1 tick. Add to this a margin of one 1069 * tick for the PPS signal jitter and maximum frequency 1070 * deviation. If the limits are exceeded, the calibration 1071 * interval is reset to the minimum and we start over. 1072 */ 1073 u_usec = (long)tick << 1; 1074 if (!((cal_sec == -1 && cal_usec > (1000000 - u_usec)) 1075 || (cal_sec == 0 && cal_usec < u_usec)) 1076 || v_usec > time_tolerance || v_usec < -time_tolerance) { 1077 pps_errcnt++; 1078 pps_shift = PPS_SHIFT; 1079 pps_intcnt = 0; 1080 time_status |= STA_PPSERROR; 1081 return; 1082 } 1083 1084 /* 1085 * A three-stage median filter is used to help deglitch the pps 1086 * frequency. The median sample becomes the frequency offset 1087 * estimate; the difference between the other two samples 1088 * becomes the frequency dispersion (stability) estimate. 1089 */ 1090 pps_ff[2] = pps_ff[1]; 1091 pps_ff[1] = pps_ff[0]; 1092 pps_ff[0] = v_usec; 1093 1094 MEDIAN3(pps_ff, u_usec, v_usec); 1095 1096 /* 1097 * Here the frequency dispersion (stability) is updated. If it 1098 * is less than one-fourth the maximum (MAXFREQ), the frequency 1099 * offset is updated as well, but clamped to the tolerance. It 1100 * will be processed later by the hardclock() routine. 1101 */ 1102 v_usec = (v_usec >> 1) - pps_stabil; 1103 if (v_usec < 0) 1104 pps_stabil -= -v_usec >> PPS_AVG; 1105 else 1106 pps_stabil += v_usec >> PPS_AVG; 1107 if (pps_stabil > MAXFREQ >> 2) { 1108 pps_stbcnt++; 1109 time_status |= STA_PPSWANDER; 1110 return; 1111 } 1112 if (time_status & STA_PPSFREQ) { 1113 if (u_usec < 0) { 1114 pps_freq -= -u_usec >> PPS_AVG; 1115 if (pps_freq < -time_tolerance) 1116 pps_freq = -time_tolerance; 1117 u_usec = -u_usec; 1118 } else { 1119 pps_freq += u_usec >> PPS_AVG; 1120 if (pps_freq > time_tolerance) 1121 pps_freq = time_tolerance; 1122 } 1123 } 1124 1125 /* 1126 * Here the calibration interval is adjusted. If the maximum 1127 * time difference is greater than tick / 4, reduce the interval 1128 * by half. If this is not the case for four consecutive 1129 * intervals, double the interval. 1130 */ 1131 if (u_usec << pps_shift > bigtick >> 2) { 1132 pps_intcnt = 0; 1133 if (pps_shift > PPS_SHIFT) 1134 pps_shift--; 1135 } else if (pps_intcnt >= 4) { 1136 pps_intcnt = 0; 1137 if (pps_shift < PPS_SHIFTMAX) 1138 pps_shift++; 1139 } else 1140 pps_intcnt++; 1141} 1142#endif /* PPS_SYNC */ 1143 | |