2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
9 * Copyright (c) 2011 The FreeBSD Foundation
10 * All rights reserved.
12 * Portions of this software were developed by Julien Ridoux at the University
13 * of Melbourne under sponsorship from the FreeBSD Foundation.
16 #include <sys/cdefs.h>
17 __FBSDID("$FreeBSD$");
19 #include "opt_compat.h"
21 #include "opt_ffclock.h"
23 #include <sys/param.h>
24 #include <sys/kernel.h>
25 #include <sys/limits.h>
27 #include <sys/mutex.h>
28 #include <sys/sysctl.h>
29 #include <sys/syslog.h>
30 #include <sys/systm.h>
31 #include <sys/timeffc.h>
32 #include <sys/timepps.h>
33 #include <sys/timetc.h>
34 #include <sys/timex.h>
38 * A large step happens on boot. This constant detects such steps.
39 * It is relatively small so that ntp_update_second gets called enough
40 * in the typical 'missed a couple of seconds' case, but doesn't loop
41 * forever when the time step is large.
43 #define LARGE_STEP 200
46 * Implement a dummy timecounter which we can use until we get a real one
47 * in the air. This allows the console and other early stuff to use
52 dummy_get_timecount(struct timecounter *tc)
59 static struct timecounter dummy_timecounter = {
60 dummy_get_timecount, 0, ~0u, 1000000, "dummy", -1000000
64 /* These fields must be initialized by the driver. */
65 struct timecounter *th_counter;
66 int64_t th_adjustment;
68 u_int th_offset_count;
69 struct bintime th_offset;
70 struct timeval th_microtime;
71 struct timespec th_nanotime;
72 /* Fields not to be copied in tc_windup start with th_generation. */
73 volatile u_int th_generation;
74 struct timehands *th_next;
77 static struct timehands th0;
78 static struct timehands th9 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th0};
79 static struct timehands th8 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th9};
80 static struct timehands th7 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th8};
81 static struct timehands th6 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th7};
82 static struct timehands th5 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th6};
83 static struct timehands th4 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th5};
84 static struct timehands th3 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th4};
85 static struct timehands th2 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th3};
86 static struct timehands th1 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th2};
87 static struct timehands th0 = {
90 (uint64_t)-1 / 1000000,
99 static struct timehands *volatile timehands = &th0;
100 struct timecounter *timecounter = &dummy_timecounter;
101 static struct timecounter *timecounters = &dummy_timecounter;
103 int tc_min_ticktock_freq = 1;
105 volatile time_t time_second = 1;
106 volatile time_t time_uptime = 1;
108 struct bintime boottimebin;
109 struct timeval boottime;
110 static int sysctl_kern_boottime(SYSCTL_HANDLER_ARGS);
111 SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime, CTLTYPE_STRUCT|CTLFLAG_RD,
112 NULL, 0, sysctl_kern_boottime, "S,timeval", "System boottime");
114 SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
115 static SYSCTL_NODE(_kern_timecounter, OID_AUTO, tc, CTLFLAG_RW, 0, "");
117 static int timestepwarnings;
118 SYSCTL_INT(_kern_timecounter, OID_AUTO, stepwarnings, CTLFLAG_RW,
119 ×tepwarnings, 0, "Log time steps");
121 struct bintime bt_timethreshold;
122 struct bintime bt_tickthreshold;
123 sbintime_t sbt_timethreshold;
124 sbintime_t sbt_tickthreshold;
125 struct bintime tc_tick_bt;
126 sbintime_t tc_tick_sbt;
128 int tc_timepercentage = TC_DEFAULTPERC;
129 static int sysctl_kern_timecounter_adjprecision(SYSCTL_HANDLER_ARGS);
130 SYSCTL_PROC(_kern_timecounter, OID_AUTO, alloweddeviation,
131 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 0, 0,
132 sysctl_kern_timecounter_adjprecision, "I",
133 "Allowed time interval deviation in percents");
135 static void tc_windup(void);
136 static void cpu_tick_calibrate(int);
138 void dtrace_getnanotime(struct timespec *tsp);
141 sysctl_kern_boottime(SYSCTL_HANDLER_ARGS)
147 if (req->flags & SCTL_MASK32) {
148 tv[0] = boottime.tv_sec;
149 tv[1] = boottime.tv_usec;
150 return SYSCTL_OUT(req, tv, sizeof(tv));
154 return SYSCTL_OUT(req, &boottime, sizeof(boottime));
158 sysctl_kern_timecounter_get(SYSCTL_HANDLER_ARGS)
161 struct timecounter *tc = arg1;
163 ncount = tc->tc_get_timecount(tc);
164 return sysctl_handle_int(oidp, &ncount, 0, req);
168 sysctl_kern_timecounter_freq(SYSCTL_HANDLER_ARGS)
171 struct timecounter *tc = arg1;
173 freq = tc->tc_frequency;
174 return sysctl_handle_64(oidp, &freq, 0, req);
178 * Return the difference between the timehands' counter value now and what
179 * was when we copied it to the timehands' offset_count.
181 static __inline u_int
182 tc_delta(struct timehands *th)
184 struct timecounter *tc;
187 return ((tc->tc_get_timecount(tc) - th->th_offset_count) &
188 tc->tc_counter_mask);
192 * Functions for reading the time. We have to loop until we are sure that
193 * the timehands that we operated on was not updated under our feet. See
194 * the comment in <sys/time.h> for a description of these 12 functions.
199 fbclock_binuptime(struct bintime *bt)
201 struct timehands *th;
206 gen = th->th_generation;
208 bintime_addx(bt, th->th_scale * tc_delta(th));
209 } while (gen == 0 || gen != th->th_generation);
213 fbclock_nanouptime(struct timespec *tsp)
217 fbclock_binuptime(&bt);
218 bintime2timespec(&bt, tsp);
222 fbclock_microuptime(struct timeval *tvp)
226 fbclock_binuptime(&bt);
227 bintime2timeval(&bt, tvp);
231 fbclock_bintime(struct bintime *bt)
234 fbclock_binuptime(bt);
235 bintime_add(bt, &boottimebin);
239 fbclock_nanotime(struct timespec *tsp)
243 fbclock_bintime(&bt);
244 bintime2timespec(&bt, tsp);
248 fbclock_microtime(struct timeval *tvp)
252 fbclock_bintime(&bt);
253 bintime2timeval(&bt, tvp);
257 fbclock_getbinuptime(struct bintime *bt)
259 struct timehands *th;
264 gen = th->th_generation;
266 } while (gen == 0 || gen != th->th_generation);
270 fbclock_getnanouptime(struct timespec *tsp)
272 struct timehands *th;
277 gen = th->th_generation;
278 bintime2timespec(&th->th_offset, tsp);
279 } while (gen == 0 || gen != th->th_generation);
283 fbclock_getmicrouptime(struct timeval *tvp)
285 struct timehands *th;
290 gen = th->th_generation;
291 bintime2timeval(&th->th_offset, tvp);
292 } while (gen == 0 || gen != th->th_generation);
296 fbclock_getbintime(struct bintime *bt)
298 struct timehands *th;
303 gen = th->th_generation;
305 } while (gen == 0 || gen != th->th_generation);
306 bintime_add(bt, &boottimebin);
310 fbclock_getnanotime(struct timespec *tsp)
312 struct timehands *th;
317 gen = th->th_generation;
318 *tsp = th->th_nanotime;
319 } while (gen == 0 || gen != th->th_generation);
323 fbclock_getmicrotime(struct timeval *tvp)
325 struct timehands *th;
330 gen = th->th_generation;
331 *tvp = th->th_microtime;
332 } while (gen == 0 || gen != th->th_generation);
336 binuptime(struct bintime *bt)
338 struct timehands *th;
343 gen = th->th_generation;
345 bintime_addx(bt, th->th_scale * tc_delta(th));
346 } while (gen == 0 || gen != th->th_generation);
350 nanouptime(struct timespec *tsp)
355 bintime2timespec(&bt, tsp);
359 microuptime(struct timeval *tvp)
364 bintime2timeval(&bt, tvp);
368 bintime(struct bintime *bt)
372 bintime_add(bt, &boottimebin);
376 nanotime(struct timespec *tsp)
381 bintime2timespec(&bt, tsp);
385 microtime(struct timeval *tvp)
390 bintime2timeval(&bt, tvp);
394 getbinuptime(struct bintime *bt)
396 struct timehands *th;
401 gen = th->th_generation;
403 } while (gen == 0 || gen != th->th_generation);
407 getnanouptime(struct timespec *tsp)
409 struct timehands *th;
414 gen = th->th_generation;
415 bintime2timespec(&th->th_offset, tsp);
416 } while (gen == 0 || gen != th->th_generation);
420 getmicrouptime(struct timeval *tvp)
422 struct timehands *th;
427 gen = th->th_generation;
428 bintime2timeval(&th->th_offset, tvp);
429 } while (gen == 0 || gen != th->th_generation);
433 getbintime(struct bintime *bt)
435 struct timehands *th;
440 gen = th->th_generation;
442 } while (gen == 0 || gen != th->th_generation);
443 bintime_add(bt, &boottimebin);
447 getnanotime(struct timespec *tsp)
449 struct timehands *th;
454 gen = th->th_generation;
455 *tsp = th->th_nanotime;
456 } while (gen == 0 || gen != th->th_generation);
460 getmicrotime(struct timeval *tvp)
462 struct timehands *th;
467 gen = th->th_generation;
468 *tvp = th->th_microtime;
469 } while (gen == 0 || gen != th->th_generation);
475 * Support for feed-forward synchronization algorithms. This is heavily inspired
476 * by the timehands mechanism but kept independent from it. *_windup() functions
477 * have some connection to avoid accessing the timecounter hardware more than
481 /* Feed-forward clock estimates kept updated by the synchronization daemon. */
482 struct ffclock_estimate ffclock_estimate;
483 struct bintime ffclock_boottime; /* Feed-forward boot time estimate. */
484 uint32_t ffclock_status; /* Feed-forward clock status. */
485 int8_t ffclock_updated; /* New estimates are available. */
486 struct mtx ffclock_mtx; /* Mutex on ffclock_estimate. */
489 struct ffclock_estimate cest;
490 struct bintime tick_time;
491 struct bintime tick_time_lerp;
492 ffcounter tick_ffcount;
493 uint64_t period_lerp;
494 volatile uint8_t gen;
495 struct fftimehands *next;
498 #define NUM_ELEMENTS(x) (sizeof(x) / sizeof(*x))
500 static struct fftimehands ffth[10];
501 static struct fftimehands *volatile fftimehands = ffth;
506 struct fftimehands *cur;
507 struct fftimehands *last;
509 memset(ffth, 0, sizeof(ffth));
511 last = ffth + NUM_ELEMENTS(ffth) - 1;
512 for (cur = ffth; cur < last; cur++)
517 ffclock_status = FFCLOCK_STA_UNSYNC;
518 mtx_init(&ffclock_mtx, "ffclock lock", NULL, MTX_DEF);
522 * Reset the feed-forward clock estimates. Called from inittodr() to get things
523 * kick started and uses the timecounter nominal frequency as a first period
524 * estimate. Note: this function may be called several time just after boot.
525 * Note: this is the only function that sets the value of boot time for the
526 * monotonic (i.e. uptime) version of the feed-forward clock.
529 ffclock_reset_clock(struct timespec *ts)
531 struct timecounter *tc;
532 struct ffclock_estimate cest;
534 tc = timehands->th_counter;
535 memset(&cest, 0, sizeof(struct ffclock_estimate));
537 timespec2bintime(ts, &ffclock_boottime);
538 timespec2bintime(ts, &(cest.update_time));
539 ffclock_read_counter(&cest.update_ffcount);
540 cest.leapsec_next = 0;
541 cest.period = ((1ULL << 63) / tc->tc_frequency) << 1;
544 cest.status = FFCLOCK_STA_UNSYNC;
545 cest.leapsec_total = 0;
548 mtx_lock(&ffclock_mtx);
549 bcopy(&cest, &ffclock_estimate, sizeof(struct ffclock_estimate));
550 ffclock_updated = INT8_MAX;
551 mtx_unlock(&ffclock_mtx);
553 printf("ffclock reset: %s (%llu Hz), time = %ld.%09lu\n", tc->tc_name,
554 (unsigned long long)tc->tc_frequency, (long)ts->tv_sec,
555 (unsigned long)ts->tv_nsec);
559 * Sub-routine to convert a time interval measured in RAW counter units to time
560 * in seconds stored in bintime format.
561 * NOTE: bintime_mul requires u_int, but the value of the ffcounter may be
562 * larger than the max value of u_int (on 32 bit architecture). Loop to consume
566 ffclock_convert_delta(ffcounter ffdelta, uint64_t period, struct bintime *bt)
569 ffcounter delta, delta_max;
571 delta_max = (1ULL << (8 * sizeof(unsigned int))) - 1;
574 if (ffdelta > delta_max)
580 bintime_mul(&bt2, (unsigned int)delta);
581 bintime_add(bt, &bt2);
583 } while (ffdelta > 0);
587 * Update the fftimehands.
588 * Push the tick ffcount and time(s) forward based on current clock estimate.
589 * The conversion from ffcounter to bintime relies on the difference clock
590 * principle, whose accuracy relies on computing small time intervals. If a new
591 * clock estimate has been passed by the synchronisation daemon, make it
592 * current, and compute the linear interpolation for monotonic time if needed.
595 ffclock_windup(unsigned int delta)
597 struct ffclock_estimate *cest;
598 struct fftimehands *ffth;
599 struct bintime bt, gap_lerp;
602 unsigned int polling;
603 uint8_t forward_jump, ogen;
606 * Pick the next timehand, copy current ffclock estimates and move tick
607 * times and counter forward.
610 ffth = fftimehands->next;
614 bcopy(&fftimehands->cest, cest, sizeof(struct ffclock_estimate));
615 ffdelta = (ffcounter)delta;
616 ffth->period_lerp = fftimehands->period_lerp;
618 ffth->tick_time = fftimehands->tick_time;
619 ffclock_convert_delta(ffdelta, cest->period, &bt);
620 bintime_add(&ffth->tick_time, &bt);
622 ffth->tick_time_lerp = fftimehands->tick_time_lerp;
623 ffclock_convert_delta(ffdelta, ffth->period_lerp, &bt);
624 bintime_add(&ffth->tick_time_lerp, &bt);
626 ffth->tick_ffcount = fftimehands->tick_ffcount + ffdelta;
629 * Assess the status of the clock, if the last update is too old, it is
630 * likely the synchronisation daemon is dead and the clock is free
633 if (ffclock_updated == 0) {
634 ffdelta = ffth->tick_ffcount - cest->update_ffcount;
635 ffclock_convert_delta(ffdelta, cest->period, &bt);
636 if (bt.sec > 2 * FFCLOCK_SKM_SCALE)
637 ffclock_status |= FFCLOCK_STA_UNSYNC;
641 * If available, grab updated clock estimates and make them current.
642 * Recompute time at this tick using the updated estimates. The clock
643 * estimates passed the feed-forward synchronisation daemon may result
644 * in time conversion that is not monotonically increasing (just after
645 * the update). time_lerp is a particular linear interpolation over the
646 * synchronisation algo polling period that ensures monotonicity for the
647 * clock ids requesting it.
649 if (ffclock_updated > 0) {
650 bcopy(&ffclock_estimate, cest, sizeof(struct ffclock_estimate));
651 ffdelta = ffth->tick_ffcount - cest->update_ffcount;
652 ffth->tick_time = cest->update_time;
653 ffclock_convert_delta(ffdelta, cest->period, &bt);
654 bintime_add(&ffth->tick_time, &bt);
656 /* ffclock_reset sets ffclock_updated to INT8_MAX */
657 if (ffclock_updated == INT8_MAX)
658 ffth->tick_time_lerp = ffth->tick_time;
660 if (bintime_cmp(&ffth->tick_time, &ffth->tick_time_lerp, >))
665 bintime_clear(&gap_lerp);
667 gap_lerp = ffth->tick_time;
668 bintime_sub(&gap_lerp, &ffth->tick_time_lerp);
670 gap_lerp = ffth->tick_time_lerp;
671 bintime_sub(&gap_lerp, &ffth->tick_time);
675 * The reset from the RTC clock may be far from accurate, and
676 * reducing the gap between real time and interpolated time
677 * could take a very long time if the interpolated clock insists
678 * on strict monotonicity. The clock is reset under very strict
679 * conditions (kernel time is known to be wrong and
680 * synchronization daemon has been restarted recently.
681 * ffclock_boottime absorbs the jump to ensure boot time is
682 * correct and uptime functions stay consistent.
684 if (((ffclock_status & FFCLOCK_STA_UNSYNC) == FFCLOCK_STA_UNSYNC) &&
685 ((cest->status & FFCLOCK_STA_UNSYNC) == 0) &&
686 ((cest->status & FFCLOCK_STA_WARMUP) == FFCLOCK_STA_WARMUP)) {
688 bintime_add(&ffclock_boottime, &gap_lerp);
690 bintime_sub(&ffclock_boottime, &gap_lerp);
691 ffth->tick_time_lerp = ffth->tick_time;
692 bintime_clear(&gap_lerp);
695 ffclock_status = cest->status;
696 ffth->period_lerp = cest->period;
699 * Compute corrected period used for the linear interpolation of
700 * time. The rate of linear interpolation is capped to 5000PPM
703 if (bintime_isset(&gap_lerp)) {
704 ffdelta = cest->update_ffcount;
705 ffdelta -= fftimehands->cest.update_ffcount;
706 ffclock_convert_delta(ffdelta, cest->period, &bt);
709 bt.frac = 5000000 * (uint64_t)18446744073LL;
710 bintime_mul(&bt, polling);
711 if (bintime_cmp(&gap_lerp, &bt, >))
714 /* Approximate 1 sec by 1-(1/2^64) to ease arithmetic */
716 if (gap_lerp.sec > 0) {
718 frac /= ffdelta / gap_lerp.sec;
720 frac += gap_lerp.frac / ffdelta;
723 ffth->period_lerp += frac;
725 ffth->period_lerp -= frac;
737 * Adjust the fftimehands when the timecounter is changed. Stating the obvious,
738 * the old and new hardware counter cannot be read simultaneously. tc_windup()
739 * does read the two counters 'back to back', but a few cycles are effectively
740 * lost, and not accumulated in tick_ffcount. This is a fairly radical
741 * operation for a feed-forward synchronization daemon, and it is its job to not
742 * pushing irrelevant data to the kernel. Because there is no locking here,
743 * simply force to ignore pending or next update to give daemon a chance to
744 * realize the counter has changed.
747 ffclock_change_tc(struct timehands *th)
749 struct fftimehands *ffth;
750 struct ffclock_estimate *cest;
751 struct timecounter *tc;
755 ffth = fftimehands->next;
760 bcopy(&(fftimehands->cest), cest, sizeof(struct ffclock_estimate));
761 cest->period = ((1ULL << 63) / tc->tc_frequency ) << 1;
764 cest->status |= FFCLOCK_STA_UNSYNC;
766 ffth->tick_ffcount = fftimehands->tick_ffcount;
767 ffth->tick_time_lerp = fftimehands->tick_time_lerp;
768 ffth->tick_time = fftimehands->tick_time;
769 ffth->period_lerp = cest->period;
771 /* Do not lock but ignore next update from synchronization daemon. */
781 * Retrieve feed-forward counter and time of last kernel tick.
784 ffclock_last_tick(ffcounter *ffcount, struct bintime *bt, uint32_t flags)
786 struct fftimehands *ffth;
790 * No locking but check generation has not changed. Also need to make
791 * sure ffdelta is positive, i.e. ffcount > tick_ffcount.
796 if ((flags & FFCLOCK_LERP) == FFCLOCK_LERP)
797 *bt = ffth->tick_time_lerp;
799 *bt = ffth->tick_time;
800 *ffcount = ffth->tick_ffcount;
801 } while (gen == 0 || gen != ffth->gen);
805 * Absolute clock conversion. Low level function to convert ffcounter to
806 * bintime. The ffcounter is converted using the current ffclock period estimate
807 * or the "interpolated period" to ensure monotonicity.
808 * NOTE: this conversion may have been deferred, and the clock updated since the
809 * hardware counter has been read.
812 ffclock_convert_abs(ffcounter ffcount, struct bintime *bt, uint32_t flags)
814 struct fftimehands *ffth;
820 * No locking but check generation has not changed. Also need to make
821 * sure ffdelta is positive, i.e. ffcount > tick_ffcount.
826 if (ffcount > ffth->tick_ffcount)
827 ffdelta = ffcount - ffth->tick_ffcount;
829 ffdelta = ffth->tick_ffcount - ffcount;
831 if ((flags & FFCLOCK_LERP) == FFCLOCK_LERP) {
832 *bt = ffth->tick_time_lerp;
833 ffclock_convert_delta(ffdelta, ffth->period_lerp, &bt2);
835 *bt = ffth->tick_time;
836 ffclock_convert_delta(ffdelta, ffth->cest.period, &bt2);
839 if (ffcount > ffth->tick_ffcount)
840 bintime_add(bt, &bt2);
842 bintime_sub(bt, &bt2);
843 } while (gen == 0 || gen != ffth->gen);
847 * Difference clock conversion.
848 * Low level function to Convert a time interval measured in RAW counter units
849 * into bintime. The difference clock allows measuring small intervals much more
850 * reliably than the absolute clock.
853 ffclock_convert_diff(ffcounter ffdelta, struct bintime *bt)
855 struct fftimehands *ffth;
858 /* No locking but check generation has not changed. */
862 ffclock_convert_delta(ffdelta, ffth->cest.period, bt);
863 } while (gen == 0 || gen != ffth->gen);
867 * Access to current ffcounter value.
870 ffclock_read_counter(ffcounter *ffcount)
872 struct timehands *th;
873 struct fftimehands *ffth;
874 unsigned int gen, delta;
877 * ffclock_windup() called from tc_windup(), safe to rely on
878 * th->th_generation only, for correct delta and ffcounter.
882 gen = th->th_generation;
884 delta = tc_delta(th);
885 *ffcount = ffth->tick_ffcount;
886 } while (gen == 0 || gen != th->th_generation);
892 binuptime(struct bintime *bt)
895 binuptime_fromclock(bt, sysclock_active);
899 nanouptime(struct timespec *tsp)
902 nanouptime_fromclock(tsp, sysclock_active);
906 microuptime(struct timeval *tvp)
909 microuptime_fromclock(tvp, sysclock_active);
913 bintime(struct bintime *bt)
916 bintime_fromclock(bt, sysclock_active);
920 nanotime(struct timespec *tsp)
923 nanotime_fromclock(tsp, sysclock_active);
927 microtime(struct timeval *tvp)
930 microtime_fromclock(tvp, sysclock_active);
934 getbinuptime(struct bintime *bt)
937 getbinuptime_fromclock(bt, sysclock_active);
941 getnanouptime(struct timespec *tsp)
944 getnanouptime_fromclock(tsp, sysclock_active);
948 getmicrouptime(struct timeval *tvp)
951 getmicrouptime_fromclock(tvp, sysclock_active);
955 getbintime(struct bintime *bt)
958 getbintime_fromclock(bt, sysclock_active);
962 getnanotime(struct timespec *tsp)
965 getnanotime_fromclock(tsp, sysclock_active);
969 getmicrotime(struct timeval *tvp)
972 getmicrouptime_fromclock(tvp, sysclock_active);
978 * This is a clone of getnanotime and used for walltimestamps.
979 * The dtrace_ prefix prevents fbt from creating probes for
980 * it so walltimestamp can be safely used in all fbt probes.
983 dtrace_getnanotime(struct timespec *tsp)
985 struct timehands *th;
990 gen = th->th_generation;
991 *tsp = th->th_nanotime;
992 } while (gen == 0 || gen != th->th_generation);
996 * System clock currently providing time to the system. Modifiable via sysctl
997 * when the FFCLOCK option is defined.
999 int sysclock_active = SYSCLOCK_FBCK;
1001 /* Internal NTP status and error estimates. */
1002 extern int time_status;
1003 extern long time_esterror;
1006 * Take a snapshot of sysclock data which can be used to compare system clocks
1007 * and generate timestamps after the fact.
1010 sysclock_getsnapshot(struct sysclock_snap *clock_snap, int fast)
1012 struct fbclock_info *fbi;
1013 struct timehands *th;
1015 unsigned int delta, gen;
1018 struct fftimehands *ffth;
1019 struct ffclock_info *ffi;
1020 struct ffclock_estimate cest;
1022 ffi = &clock_snap->ff_info;
1025 fbi = &clock_snap->fb_info;
1030 gen = th->th_generation;
1031 fbi->th_scale = th->th_scale;
1032 fbi->tick_time = th->th_offset;
1035 ffi->tick_time = ffth->tick_time_lerp;
1036 ffi->tick_time_lerp = ffth->tick_time_lerp;
1037 ffi->period = ffth->cest.period;
1038 ffi->period_lerp = ffth->period_lerp;
1039 clock_snap->ffcount = ffth->tick_ffcount;
1043 delta = tc_delta(th);
1044 } while (gen == 0 || gen != th->th_generation);
1046 clock_snap->delta = delta;
1047 clock_snap->sysclock_active = sysclock_active;
1049 /* Record feedback clock status and error. */
1050 clock_snap->fb_info.status = time_status;
1051 /* XXX: Very crude estimate of feedback clock error. */
1052 bt.sec = time_esterror / 1000000;
1053 bt.frac = ((time_esterror - bt.sec) * 1000000) *
1054 (uint64_t)18446744073709ULL;
1055 clock_snap->fb_info.error = bt;
1059 clock_snap->ffcount += delta;
1061 /* Record feed-forward clock leap second adjustment. */
1062 ffi->leapsec_adjustment = cest.leapsec_total;
1063 if (clock_snap->ffcount > cest.leapsec_next)
1064 ffi->leapsec_adjustment -= cest.leapsec;
1066 /* Record feed-forward clock status and error. */
1067 clock_snap->ff_info.status = cest.status;
1068 ffcount = clock_snap->ffcount - cest.update_ffcount;
1069 ffclock_convert_delta(ffcount, cest.period, &bt);
1070 /* 18446744073709 = int(2^64/1e12), err_bound_rate in [ps/s]. */
1071 bintime_mul(&bt, cest.errb_rate * (uint64_t)18446744073709ULL);
1072 /* 18446744073 = int(2^64 / 1e9), since err_abs in [ns]. */
1073 bintime_addx(&bt, cest.errb_abs * (uint64_t)18446744073ULL);
1074 clock_snap->ff_info.error = bt;
1079 * Convert a sysclock snapshot into a struct bintime based on the specified
1080 * clock source and flags.
1083 sysclock_snap2bintime(struct sysclock_snap *cs, struct bintime *bt,
1084 int whichclock, uint32_t flags)
1091 switch (whichclock) {
1093 *bt = cs->fb_info.tick_time;
1095 /* If snapshot was created with !fast, delta will be >0. */
1097 bintime_addx(bt, cs->fb_info.th_scale * cs->delta);
1099 if ((flags & FBCLOCK_UPTIME) == 0)
1100 bintime_add(bt, &boottimebin);
1104 if (flags & FFCLOCK_LERP) {
1105 *bt = cs->ff_info.tick_time_lerp;
1106 period = cs->ff_info.period_lerp;
1108 *bt = cs->ff_info.tick_time;
1109 period = cs->ff_info.period;
1112 /* If snapshot was created with !fast, delta will be >0. */
1113 if (cs->delta > 0) {
1114 ffclock_convert_delta(cs->delta, period, &bt2);
1115 bintime_add(bt, &bt2);
1118 /* Leap second adjustment. */
1119 if (flags & FFCLOCK_LEAPSEC)
1120 bt->sec -= cs->ff_info.leapsec_adjustment;
1122 /* Boot time adjustment, for uptime/monotonic clocks. */
1123 if (flags & FFCLOCK_UPTIME)
1124 bintime_sub(bt, &ffclock_boottime);
1136 * Initialize a new timecounter and possibly use it.
1139 tc_init(struct timecounter *tc)
1142 struct sysctl_oid *tc_root;
1144 u = tc->tc_frequency / tc->tc_counter_mask;
1145 /* XXX: We need some margin here, 10% is a guess */
1148 if (u > hz && tc->tc_quality >= 0) {
1149 tc->tc_quality = -2000;
1151 printf("Timecounter \"%s\" frequency %ju Hz",
1152 tc->tc_name, (uintmax_t)tc->tc_frequency);
1153 printf(" -- Insufficient hz, needs at least %u\n", u);
1155 } else if (tc->tc_quality >= 0 || bootverbose) {
1156 printf("Timecounter \"%s\" frequency %ju Hz quality %d\n",
1157 tc->tc_name, (uintmax_t)tc->tc_frequency,
1161 tc->tc_next = timecounters;
1164 * Set up sysctl tree for this counter.
1166 tc_root = SYSCTL_ADD_NODE(NULL,
1167 SYSCTL_STATIC_CHILDREN(_kern_timecounter_tc), OID_AUTO, tc->tc_name,
1168 CTLFLAG_RW, 0, "timecounter description");
1169 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1170 "mask", CTLFLAG_RD, &(tc->tc_counter_mask), 0,
1171 "mask for implemented bits");
1172 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1173 "counter", CTLTYPE_UINT | CTLFLAG_RD, tc, sizeof(*tc),
1174 sysctl_kern_timecounter_get, "IU", "current timecounter value");
1175 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1176 "frequency", CTLTYPE_U64 | CTLFLAG_RD, tc, sizeof(*tc),
1177 sysctl_kern_timecounter_freq, "QU", "timecounter frequency");
1178 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1179 "quality", CTLFLAG_RD, &(tc->tc_quality), 0,
1180 "goodness of time counter");
1182 * Never automatically use a timecounter with negative quality.
1183 * Even though we run on the dummy counter, switching here may be
1184 * worse since this timecounter may not be monotonous.
1186 if (tc->tc_quality < 0)
1188 if (tc->tc_quality < timecounter->tc_quality)
1190 if (tc->tc_quality == timecounter->tc_quality &&
1191 tc->tc_frequency < timecounter->tc_frequency)
1193 (void)tc->tc_get_timecount(tc);
1194 (void)tc->tc_get_timecount(tc);
1198 /* Report the frequency of the current timecounter. */
1200 tc_getfrequency(void)
1203 return (timehands->th_counter->tc_frequency);
1207 * Step our concept of UTC. This is done by modifying our estimate of
1212 tc_setclock(struct timespec *ts)
1214 struct timespec tbef, taft;
1215 struct bintime bt, bt2;
1217 cpu_tick_calibrate(1);
1219 timespec2bintime(ts, &bt);
1221 bintime_sub(&bt, &bt2);
1222 bintime_add(&bt2, &boottimebin);
1224 bintime2timeval(&bt, &boottime);
1226 /* XXX fiddle all the little crinkly bits around the fiords... */
1229 if (timestepwarnings) {
1231 "Time stepped from %jd.%09ld to %jd.%09ld (%jd.%09ld)\n",
1232 (intmax_t)tbef.tv_sec, tbef.tv_nsec,
1233 (intmax_t)taft.tv_sec, taft.tv_nsec,
1234 (intmax_t)ts->tv_sec, ts->tv_nsec);
1236 cpu_tick_calibrate(1);
1240 * Initialize the next struct timehands in the ring and make
1241 * it the active timehands. Along the way we might switch to a different
1242 * timecounter and/or do seconds processing in NTP. Slightly magic.
1248 struct timehands *th, *tho;
1250 u_int delta, ncount, ogen;
1255 * Make the next timehands a copy of the current one, but do not
1256 * overwrite the generation or next pointer. While we update
1257 * the contents, the generation must be zero.
1261 ogen = th->th_generation;
1262 th->th_generation = 0;
1263 bcopy(tho, th, offsetof(struct timehands, th_generation));
1266 * Capture a timecounter delta on the current timecounter and if
1267 * changing timecounters, a counter value from the new timecounter.
1268 * Update the offset fields accordingly.
1270 delta = tc_delta(th);
1271 if (th->th_counter != timecounter)
1272 ncount = timecounter->tc_get_timecount(timecounter);
1276 ffclock_windup(delta);
1278 th->th_offset_count += delta;
1279 th->th_offset_count &= th->th_counter->tc_counter_mask;
1280 while (delta > th->th_counter->tc_frequency) {
1281 /* Eat complete unadjusted seconds. */
1282 delta -= th->th_counter->tc_frequency;
1283 th->th_offset.sec++;
1285 if ((delta > th->th_counter->tc_frequency / 2) &&
1286 (th->th_scale * delta < ((uint64_t)1 << 63))) {
1287 /* The product th_scale * delta just barely overflows. */
1288 th->th_offset.sec++;
1290 bintime_addx(&th->th_offset, th->th_scale * delta);
1293 * Hardware latching timecounters may not generate interrupts on
1294 * PPS events, so instead we poll them. There is a finite risk that
1295 * the hardware might capture a count which is later than the one we
1296 * got above, and therefore possibly in the next NTP second which might
1297 * have a different rate than the current NTP second. It doesn't
1298 * matter in practice.
1300 if (tho->th_counter->tc_poll_pps)
1301 tho->th_counter->tc_poll_pps(tho->th_counter);
1304 * Deal with NTP second processing. The for loop normally
1305 * iterates at most once, but in extreme situations it might
1306 * keep NTP sane if timeouts are not run for several seconds.
1307 * At boot, the time step can be large when the TOD hardware
1308 * has been read, so on really large steps, we call
1309 * ntp_update_second only twice. We need to call it twice in
1310 * case we missed a leap second.
1313 bintime_add(&bt, &boottimebin);
1314 i = bt.sec - tho->th_microtime.tv_sec;
1317 for (; i > 0; i--) {
1319 ntp_update_second(&th->th_adjustment, &bt.sec);
1321 boottimebin.sec += bt.sec - t;
1323 /* Update the UTC timestamps used by the get*() functions. */
1324 /* XXX shouldn't do this here. Should force non-`get' versions. */
1325 bintime2timeval(&bt, &th->th_microtime);
1326 bintime2timespec(&bt, &th->th_nanotime);
1328 /* Now is a good time to change timecounters. */
1329 if (th->th_counter != timecounter) {
1331 if ((timecounter->tc_flags & TC_FLAGS_C2STOP) != 0)
1332 cpu_disable_c2_sleep++;
1333 if ((th->th_counter->tc_flags & TC_FLAGS_C2STOP) != 0)
1334 cpu_disable_c2_sleep--;
1336 th->th_counter = timecounter;
1337 th->th_offset_count = ncount;
1338 tc_min_ticktock_freq = max(1, timecounter->tc_frequency /
1339 (((uint64_t)timecounter->tc_counter_mask + 1) / 3));
1341 ffclock_change_tc(th);
1346 * Recalculate the scaling factor. We want the number of 1/2^64
1347 * fractions of a second per period of the hardware counter, taking
1348 * into account the th_adjustment factor which the NTP PLL/adjtime(2)
1349 * processing provides us with.
1351 * The th_adjustment is nanoseconds per second with 32 bit binary
1352 * fraction and we want 64 bit binary fraction of second:
1354 * x = a * 2^32 / 10^9 = a * 4.294967296
1356 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
1357 * we can only multiply by about 850 without overflowing, that
1358 * leaves no suitably precise fractions for multiply before divide.
1360 * Divide before multiply with a fraction of 2199/512 results in a
1361 * systematic undercompensation of 10PPM of th_adjustment. On a
1362 * 5000PPM adjustment this is a 0.05PPM error. This is acceptable.
1364 * We happily sacrifice the lowest of the 64 bits of our result
1365 * to the goddess of code clarity.
1368 scale = (uint64_t)1 << 63;
1369 scale += (th->th_adjustment / 1024) * 2199;
1370 scale /= th->th_counter->tc_frequency;
1371 th->th_scale = scale * 2;
1374 * Now that the struct timehands is again consistent, set the new
1375 * generation number, making sure to not make it zero.
1379 th->th_generation = ogen;
1381 /* Go live with the new struct timehands. */
1383 switch (sysclock_active) {
1386 time_second = th->th_microtime.tv_sec;
1387 time_uptime = th->th_offset.sec;
1391 time_second = fftimehands->tick_time_lerp.sec;
1392 time_uptime = fftimehands->tick_time_lerp.sec - ffclock_boottime.sec;
1398 timekeep_push_vdso();
1401 /* Report or change the active timecounter hardware. */
1403 sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
1406 struct timecounter *newtc, *tc;
1410 strlcpy(newname, tc->tc_name, sizeof(newname));
1412 error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
1413 if (error != 0 || req->newptr == NULL ||
1414 strcmp(newname, tc->tc_name) == 0)
1416 for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) {
1417 if (strcmp(newname, newtc->tc_name) != 0)
1420 /* Warm up new timecounter. */
1421 (void)newtc->tc_get_timecount(newtc);
1422 (void)newtc->tc_get_timecount(newtc);
1424 timecounter = newtc;
1427 * The vdso timehands update is deferred until the next
1430 * This is prudent given that 'timekeep_push_vdso()' does not
1431 * use any locking and that it can be called in hard interrupt
1432 * context via 'tc_windup()'.
1439 SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW,
1440 0, 0, sysctl_kern_timecounter_hardware, "A",
1441 "Timecounter hardware selected");
1444 /* Report or change the active timecounter hardware. */
1446 sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS)
1449 struct timecounter *tc;
1454 for (tc = timecounters; error == 0 && tc != NULL; tc = tc->tc_next) {
1455 sprintf(buf, "%s%s(%d)",
1456 spc, tc->tc_name, tc->tc_quality);
1457 error = SYSCTL_OUT(req, buf, strlen(buf));
1463 SYSCTL_PROC(_kern_timecounter, OID_AUTO, choice, CTLTYPE_STRING | CTLFLAG_RD,
1464 0, 0, sysctl_kern_timecounter_choice, "A", "Timecounter hardware detected");
1467 * RFC 2783 PPS-API implementation.
1471 pps_fetch(struct pps_fetch_args *fapi, struct pps_state *pps)
1474 pps_seq_t aseq, cseq;
1477 if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
1481 * If no timeout is requested, immediately return whatever values were
1482 * most recently captured. If timeout seconds is -1, that's a request
1483 * to block without a timeout. WITNESS won't let us sleep forever
1484 * without a lock (we really don't need a lock), so just repeatedly
1485 * sleep a long time.
1487 if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec) {
1488 if (fapi->timeout.tv_sec == -1)
1491 tv.tv_sec = fapi->timeout.tv_sec;
1492 tv.tv_usec = fapi->timeout.tv_nsec / 1000;
1495 aseq = pps->ppsinfo.assert_sequence;
1496 cseq = pps->ppsinfo.clear_sequence;
1497 while (aseq == pps->ppsinfo.assert_sequence &&
1498 cseq == pps->ppsinfo.clear_sequence) {
1499 if (pps->mtx != NULL)
1500 err = msleep(pps, pps->mtx, PCATCH, "ppsfch", timo);
1502 err = tsleep(pps, PCATCH, "ppsfch", timo);
1503 if (err == EWOULDBLOCK && fapi->timeout.tv_sec == -1) {
1505 } else if (err != 0) {
1511 pps->ppsinfo.current_mode = pps->ppsparam.mode;
1512 fapi->pps_info_buf = pps->ppsinfo;
1518 pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
1521 struct pps_fetch_args *fapi;
1523 struct pps_fetch_ffc_args *fapi_ffc;
1526 struct pps_kcbind_args *kapi;
1529 KASSERT(pps != NULL, ("NULL pps pointer in pps_ioctl"));
1531 case PPS_IOC_CREATE:
1533 case PPS_IOC_DESTROY:
1535 case PPS_IOC_SETPARAMS:
1536 app = (pps_params_t *)data;
1537 if (app->mode & ~pps->ppscap)
1540 /* Ensure only a single clock is selected for ffc timestamp. */
1541 if ((app->mode & PPS_TSCLK_MASK) == PPS_TSCLK_MASK)
1544 pps->ppsparam = *app;
1546 case PPS_IOC_GETPARAMS:
1547 app = (pps_params_t *)data;
1548 *app = pps->ppsparam;
1549 app->api_version = PPS_API_VERS_1;
1551 case PPS_IOC_GETCAP:
1552 *(int*)data = pps->ppscap;
1555 fapi = (struct pps_fetch_args *)data;
1556 return (pps_fetch(fapi, pps));
1558 case PPS_IOC_FETCH_FFCOUNTER:
1559 fapi_ffc = (struct pps_fetch_ffc_args *)data;
1560 if (fapi_ffc->tsformat && fapi_ffc->tsformat !=
1563 if (fapi_ffc->timeout.tv_sec || fapi_ffc->timeout.tv_nsec)
1564 return (EOPNOTSUPP);
1565 pps->ppsinfo_ffc.current_mode = pps->ppsparam.mode;
1566 fapi_ffc->pps_info_buf_ffc = pps->ppsinfo_ffc;
1567 /* Overwrite timestamps if feedback clock selected. */
1568 switch (pps->ppsparam.mode & PPS_TSCLK_MASK) {
1569 case PPS_TSCLK_FBCK:
1570 fapi_ffc->pps_info_buf_ffc.assert_timestamp =
1571 pps->ppsinfo.assert_timestamp;
1572 fapi_ffc->pps_info_buf_ffc.clear_timestamp =
1573 pps->ppsinfo.clear_timestamp;
1575 case PPS_TSCLK_FFWD:
1581 #endif /* FFCLOCK */
1582 case PPS_IOC_KCBIND:
1584 kapi = (struct pps_kcbind_args *)data;
1585 /* XXX Only root should be able to do this */
1586 if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
1588 if (kapi->kernel_consumer != PPS_KC_HARDPPS)
1590 if (kapi->edge & ~pps->ppscap)
1592 pps->kcmode = kapi->edge;
1595 return (EOPNOTSUPP);
1603 pps_init(struct pps_state *pps)
1605 pps->ppscap |= PPS_TSFMT_TSPEC | PPS_CANWAIT;
1606 if (pps->ppscap & PPS_CAPTUREASSERT)
1607 pps->ppscap |= PPS_OFFSETASSERT;
1608 if (pps->ppscap & PPS_CAPTURECLEAR)
1609 pps->ppscap |= PPS_OFFSETCLEAR;
1611 pps->ppscap |= PPS_TSCLK_MASK;
1616 pps_capture(struct pps_state *pps)
1618 struct timehands *th;
1620 KASSERT(pps != NULL, ("NULL pps pointer in pps_capture"));
1622 pps->capgen = th->th_generation;
1625 pps->capffth = fftimehands;
1627 pps->capcount = th->th_counter->tc_get_timecount(th->th_counter);
1628 if (pps->capgen != th->th_generation)
1633 pps_event(struct pps_state *pps, int event)
1636 struct timespec ts, *tsp, *osp;
1637 u_int tcount, *pcount;
1641 struct timespec *tsp_ffc;
1642 pps_seq_t *pseq_ffc;
1646 KASSERT(pps != NULL, ("NULL pps pointer in pps_event"));
1647 /* If the timecounter was wound up underneath us, bail out. */
1648 if (pps->capgen == 0 || pps->capgen != pps->capth->th_generation)
1651 /* Things would be easier with arrays. */
1652 if (event == PPS_CAPTUREASSERT) {
1653 tsp = &pps->ppsinfo.assert_timestamp;
1654 osp = &pps->ppsparam.assert_offset;
1655 foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
1656 fhard = pps->kcmode & PPS_CAPTUREASSERT;
1657 pcount = &pps->ppscount[0];
1658 pseq = &pps->ppsinfo.assert_sequence;
1660 ffcount = &pps->ppsinfo_ffc.assert_ffcount;
1661 tsp_ffc = &pps->ppsinfo_ffc.assert_timestamp;
1662 pseq_ffc = &pps->ppsinfo_ffc.assert_sequence;
1665 tsp = &pps->ppsinfo.clear_timestamp;
1666 osp = &pps->ppsparam.clear_offset;
1667 foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
1668 fhard = pps->kcmode & PPS_CAPTURECLEAR;
1669 pcount = &pps->ppscount[1];
1670 pseq = &pps->ppsinfo.clear_sequence;
1672 ffcount = &pps->ppsinfo_ffc.clear_ffcount;
1673 tsp_ffc = &pps->ppsinfo_ffc.clear_timestamp;
1674 pseq_ffc = &pps->ppsinfo_ffc.clear_sequence;
1679 * If the timecounter changed, we cannot compare the count values, so
1680 * we have to drop the rest of the PPS-stuff until the next event.
1682 if (pps->ppstc != pps->capth->th_counter) {
1683 pps->ppstc = pps->capth->th_counter;
1684 *pcount = pps->capcount;
1685 pps->ppscount[2] = pps->capcount;
1689 /* Convert the count to a timespec. */
1690 tcount = pps->capcount - pps->capth->th_offset_count;
1691 tcount &= pps->capth->th_counter->tc_counter_mask;
1692 bt = pps->capth->th_offset;
1693 bintime_addx(&bt, pps->capth->th_scale * tcount);
1694 bintime_add(&bt, &boottimebin);
1695 bintime2timespec(&bt, &ts);
1697 /* If the timecounter was wound up underneath us, bail out. */
1698 if (pps->capgen != pps->capth->th_generation)
1701 *pcount = pps->capcount;
1706 timespecadd(tsp, osp);
1707 if (tsp->tv_nsec < 0) {
1708 tsp->tv_nsec += 1000000000;
1714 *ffcount = pps->capffth->tick_ffcount + tcount;
1715 bt = pps->capffth->tick_time;
1716 ffclock_convert_delta(tcount, pps->capffth->cest.period, &bt);
1717 bintime_add(&bt, &pps->capffth->tick_time);
1718 bintime2timespec(&bt, &ts);
1728 * Feed the NTP PLL/FLL.
1729 * The FLL wants to know how many (hardware) nanoseconds
1730 * elapsed since the previous event.
1732 tcount = pps->capcount - pps->ppscount[2];
1733 pps->ppscount[2] = pps->capcount;
1734 tcount &= pps->capth->th_counter->tc_counter_mask;
1735 scale = (uint64_t)1 << 63;
1736 scale /= pps->capth->th_counter->tc_frequency;
1740 bintime_addx(&bt, scale * tcount);
1741 bintime2timespec(&bt, &ts);
1742 hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec);
1746 /* Wakeup anyone sleeping in pps_fetch(). */
1751 * Timecounters need to be updated every so often to prevent the hardware
1752 * counter from overflowing. Updating also recalculates the cached values
1753 * used by the get*() family of functions, so their precision depends on
1754 * the update frequency.
1758 SYSCTL_INT(_kern_timecounter, OID_AUTO, tick, CTLFLAG_RD, &tc_tick, 0,
1759 "Approximate number of hardclock ticks in a millisecond");
1762 tc_ticktock(int cnt)
1767 if (count < tc_tick)
1773 static void __inline
1774 tc_adjprecision(void)
1778 if (tc_timepercentage > 0) {
1779 t = (99 + tc_timepercentage) / tc_timepercentage;
1780 tc_precexp = fls(t + (t >> 1)) - 1;
1781 FREQ2BT(hz / tc_tick, &bt_timethreshold);
1782 FREQ2BT(hz, &bt_tickthreshold);
1783 bintime_shift(&bt_timethreshold, tc_precexp);
1784 bintime_shift(&bt_tickthreshold, tc_precexp);
1787 bt_timethreshold.sec = INT_MAX;
1788 bt_timethreshold.frac = ~(uint64_t)0;
1789 bt_tickthreshold = bt_timethreshold;
1791 sbt_timethreshold = bttosbt(bt_timethreshold);
1792 sbt_tickthreshold = bttosbt(bt_tickthreshold);
1796 sysctl_kern_timecounter_adjprecision(SYSCTL_HANDLER_ARGS)
1800 val = tc_timepercentage;
1801 error = sysctl_handle_int(oidp, &val, 0, req);
1802 if (error != 0 || req->newptr == NULL)
1804 tc_timepercentage = val;
1813 inittimecounter(void *dummy)
1819 * Set the initial timeout to
1820 * max(1, <approx. number of hardclock ticks in a millisecond>).
1821 * People should probably not use the sysctl to set the timeout
1822 * to smaller than its inital value, since that value is the
1823 * smallest reasonable one. If they want better timestamps they
1824 * should use the non-"get"* functions.
1827 tc_tick = (hz + 500) / 1000;
1831 FREQ2BT(hz, &tick_bt);
1832 tick_sbt = bttosbt(tick_bt);
1833 tick_rate = hz / tc_tick;
1834 FREQ2BT(tick_rate, &tc_tick_bt);
1835 tc_tick_sbt = bttosbt(tc_tick_bt);
1836 p = (tc_tick * 1000000) / hz;
1837 printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000);
1842 /* warm up new timecounter (again) and get rolling. */
1843 (void)timecounter->tc_get_timecount(timecounter);
1844 (void)timecounter->tc_get_timecount(timecounter);
1848 SYSINIT(timecounter, SI_SUB_CLOCKS, SI_ORDER_SECOND, inittimecounter, NULL);
1850 /* Cpu tick handling -------------------------------------------------*/
1852 static int cpu_tick_variable;
1853 static uint64_t cpu_tick_frequency;
1858 static uint64_t base;
1859 static unsigned last;
1861 struct timecounter *tc;
1863 tc = timehands->th_counter;
1864 u = tc->tc_get_timecount(tc) & tc->tc_counter_mask;
1866 base += (uint64_t)tc->tc_counter_mask + 1;
1872 cpu_tick_calibration(void)
1874 static time_t last_calib;
1876 if (time_uptime != last_calib && !(time_uptime & 0xf)) {
1877 cpu_tick_calibrate(0);
1878 last_calib = time_uptime;
1883 * This function gets called every 16 seconds on only one designated
1884 * CPU in the system from hardclock() via cpu_tick_calibration()().
1886 * Whenever the real time clock is stepped we get called with reset=1
1887 * to make sure we handle suspend/resume and similar events correctly.
1891 cpu_tick_calibrate(int reset)
1893 static uint64_t c_last;
1894 uint64_t c_this, c_delta;
1895 static struct bintime t_last;
1896 struct bintime t_this, t_delta;
1900 /* The clock was stepped, abort & reset */
1905 /* we don't calibrate fixed rate cputicks */
1906 if (!cpu_tick_variable)
1909 getbinuptime(&t_this);
1910 c_this = cpu_ticks();
1911 if (t_last.sec != 0) {
1912 c_delta = c_this - c_last;
1914 bintime_sub(&t_delta, &t_last);
1917 * 2^(64-20) / 16[s] =
1919 * 17.592.186.044.416 / 16 =
1920 * 1.099.511.627.776 [Hz]
1922 divi = t_delta.sec << 20;
1923 divi |= t_delta.frac >> (64 - 20);
1926 if (c_delta > cpu_tick_frequency) {
1927 if (0 && bootverbose)
1928 printf("cpu_tick increased to %ju Hz\n",
1930 cpu_tick_frequency = c_delta;
1938 set_cputicker(cpu_tick_f *func, uint64_t freq, unsigned var)
1942 cpu_ticks = tc_cpu_ticks;
1944 cpu_tick_frequency = freq;
1945 cpu_tick_variable = var;
1954 if (cpu_ticks == tc_cpu_ticks)
1955 return (tc_getfrequency());
1956 return (cpu_tick_frequency);
1960 * We need to be slightly careful converting cputicks to microseconds.
1961 * There is plenty of margin in 64 bits of microseconds (half a million
1962 * years) and in 64 bits at 4 GHz (146 years), but if we do a multiply
1963 * before divide conversion (to retain precision) we find that the
1964 * margin shrinks to 1.5 hours (one millionth of 146y).
1965 * With a three prong approach we never lose significant bits, no
1966 * matter what the cputick rate and length of timeinterval is.
1970 cputick2usec(uint64_t tick)
1973 if (tick > 18446744073709551LL) /* floor(2^64 / 1000) */
1974 return (tick / (cpu_tickrate() / 1000000LL));
1975 else if (tick > 18446744073709LL) /* floor(2^64 / 1000000) */
1976 return ((tick * 1000LL) / (cpu_tickrate() / 1000LL));
1978 return ((tick * 1000000LL) / cpu_tickrate());
1981 cpu_tick_f *cpu_ticks = tc_cpu_ticks;
1983 static int vdso_th_enable = 1;
1985 sysctl_fast_gettime(SYSCTL_HANDLER_ARGS)
1987 int old_vdso_th_enable, error;
1989 old_vdso_th_enable = vdso_th_enable;
1990 error = sysctl_handle_int(oidp, &old_vdso_th_enable, 0, req);
1993 vdso_th_enable = old_vdso_th_enable;
1996 SYSCTL_PROC(_kern_timecounter, OID_AUTO, fast_gettime,
1997 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1998 NULL, 0, sysctl_fast_gettime, "I", "Enable fast time of day");
2001 tc_fill_vdso_timehands(struct vdso_timehands *vdso_th)
2003 struct timehands *th;
2007 vdso_th->th_algo = VDSO_TH_ALGO_1;
2008 vdso_th->th_scale = th->th_scale;
2009 vdso_th->th_offset_count = th->th_offset_count;
2010 vdso_th->th_counter_mask = th->th_counter->tc_counter_mask;
2011 vdso_th->th_offset = th->th_offset;
2012 vdso_th->th_boottime = boottimebin;
2013 enabled = cpu_fill_vdso_timehands(vdso_th, th->th_counter);
2014 if (!vdso_th_enable)
2019 #ifdef COMPAT_FREEBSD32
2021 tc_fill_vdso_timehands32(struct vdso_timehands32 *vdso_th32)
2023 struct timehands *th;
2027 vdso_th32->th_algo = VDSO_TH_ALGO_1;
2028 *(uint64_t *)&vdso_th32->th_scale[0] = th->th_scale;
2029 vdso_th32->th_offset_count = th->th_offset_count;
2030 vdso_th32->th_counter_mask = th->th_counter->tc_counter_mask;
2031 vdso_th32->th_offset.sec = th->th_offset.sec;
2032 *(uint64_t *)&vdso_th32->th_offset.frac[0] = th->th_offset.frac;
2033 vdso_th32->th_boottime.sec = boottimebin.sec;
2034 *(uint64_t *)&vdso_th32->th_boottime.frac[0] = boottimebin.frac;
2035 enabled = cpu_fill_vdso_timehands32(vdso_th32, th->th_counter);
2036 if (!vdso_th_enable)