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$");
20 #include "opt_ffclock.h"
22 #include <sys/param.h>
23 #include <sys/kernel.h>
26 #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>
37 * A large step happens on boot. This constant detects such steps.
38 * It is relatively small so that ntp_update_second gets called enough
39 * in the typical 'missed a couple of seconds' case, but doesn't loop
40 * forever when the time step is large.
42 #define LARGE_STEP 200
45 * Implement a dummy timecounter which we can use until we get a real one
46 * in the air. This allows the console and other early stuff to use
51 dummy_get_timecount(struct timecounter *tc)
58 static struct timecounter dummy_timecounter = {
59 dummy_get_timecount, 0, ~0u, 1000000, "dummy", -1000000
63 /* These fields must be initialized by the driver. */
64 struct timecounter *th_counter;
65 int64_t th_adjustment;
67 u_int th_offset_count;
68 struct bintime th_offset;
69 struct timeval th_microtime;
70 struct timespec th_nanotime;
71 /* Fields not to be copied in tc_windup start with th_generation. */
72 volatile u_int th_generation;
73 struct timehands *th_next;
76 static struct timehands th0;
77 static struct timehands th9 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th0};
78 static struct timehands th8 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th9};
79 static struct timehands th7 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th8};
80 static struct timehands th6 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th7};
81 static struct timehands th5 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th6};
82 static struct timehands th4 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th5};
83 static struct timehands th3 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th4};
84 static struct timehands th2 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th3};
85 static struct timehands th1 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th2};
86 static struct timehands th0 = {
89 (uint64_t)-1 / 1000000,
98 static struct timehands *volatile timehands = &th0;
99 struct timecounter *timecounter = &dummy_timecounter;
100 static struct timecounter *timecounters = &dummy_timecounter;
102 int tc_min_ticktock_freq = 1;
104 time_t time_second = 1;
105 time_t time_uptime = 1;
107 struct bintime boottimebin;
108 struct timeval boottime;
109 static int sysctl_kern_boottime(SYSCTL_HANDLER_ARGS);
110 SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime, CTLTYPE_STRUCT|CTLFLAG_RD,
111 NULL, 0, sysctl_kern_boottime, "S,timeval", "System boottime");
113 SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
114 static SYSCTL_NODE(_kern_timecounter, OID_AUTO, tc, CTLFLAG_RW, 0, "");
116 static int timestepwarnings;
117 SYSCTL_INT(_kern_timecounter, OID_AUTO, stepwarnings, CTLFLAG_RW,
118 ×tepwarnings, 0, "Log time steps");
120 static void tc_windup(void);
121 static void cpu_tick_calibrate(int);
124 sysctl_kern_boottime(SYSCTL_HANDLER_ARGS)
130 if (req->flags & SCTL_MASK32) {
131 tv[0] = boottime.tv_sec;
132 tv[1] = boottime.tv_usec;
133 return SYSCTL_OUT(req, tv, sizeof(tv));
137 return SYSCTL_OUT(req, &boottime, sizeof(boottime));
141 sysctl_kern_timecounter_get(SYSCTL_HANDLER_ARGS)
144 struct timecounter *tc = arg1;
146 ncount = tc->tc_get_timecount(tc);
147 return sysctl_handle_int(oidp, &ncount, 0, req);
151 sysctl_kern_timecounter_freq(SYSCTL_HANDLER_ARGS)
154 struct timecounter *tc = arg1;
156 freq = tc->tc_frequency;
157 return sysctl_handle_64(oidp, &freq, 0, req);
161 * Return the difference between the timehands' counter value now and what
162 * was when we copied it to the timehands' offset_count.
164 static __inline u_int
165 tc_delta(struct timehands *th)
167 struct timecounter *tc;
170 return ((tc->tc_get_timecount(tc) - th->th_offset_count) &
171 tc->tc_counter_mask);
175 * Functions for reading the time. We have to loop until we are sure that
176 * the timehands that we operated on was not updated under our feet. See
177 * the comment in <sys/time.h> for a description of these 12 functions.
182 fbclock_binuptime(struct bintime *bt)
184 struct timehands *th;
189 gen = th->th_generation;
191 bintime_addx(bt, th->th_scale * tc_delta(th));
192 } while (gen == 0 || gen != th->th_generation);
196 fbclock_nanouptime(struct timespec *tsp)
200 fbclock_binuptime(&bt);
201 bintime2timespec(&bt, tsp);
205 fbclock_microuptime(struct timeval *tvp)
209 fbclock_binuptime(&bt);
210 bintime2timeval(&bt, tvp);
214 fbclock_bintime(struct bintime *bt)
217 fbclock_binuptime(bt);
218 bintime_add(bt, &boottimebin);
222 fbclock_nanotime(struct timespec *tsp)
226 fbclock_bintime(&bt);
227 bintime2timespec(&bt, tsp);
231 fbclock_microtime(struct timeval *tvp)
235 fbclock_bintime(&bt);
236 bintime2timeval(&bt, tvp);
240 fbclock_getbinuptime(struct bintime *bt)
242 struct timehands *th;
247 gen = th->th_generation;
249 } while (gen == 0 || gen != th->th_generation);
253 fbclock_getnanouptime(struct timespec *tsp)
255 struct timehands *th;
260 gen = th->th_generation;
261 bintime2timespec(&th->th_offset, tsp);
262 } while (gen == 0 || gen != th->th_generation);
266 fbclock_getmicrouptime(struct timeval *tvp)
268 struct timehands *th;
273 gen = th->th_generation;
274 bintime2timeval(&th->th_offset, tvp);
275 } while (gen == 0 || gen != th->th_generation);
279 fbclock_getbintime(struct bintime *bt)
281 struct timehands *th;
286 gen = th->th_generation;
288 } while (gen == 0 || gen != th->th_generation);
289 bintime_add(bt, &boottimebin);
293 fbclock_getnanotime(struct timespec *tsp)
295 struct timehands *th;
300 gen = th->th_generation;
301 *tsp = th->th_nanotime;
302 } while (gen == 0 || gen != th->th_generation);
306 fbclock_getmicrotime(struct timeval *tvp)
308 struct timehands *th;
313 gen = th->th_generation;
314 *tvp = th->th_microtime;
315 } while (gen == 0 || gen != th->th_generation);
319 binuptime(struct bintime *bt)
321 struct timehands *th;
326 gen = th->th_generation;
328 bintime_addx(bt, th->th_scale * tc_delta(th));
329 } while (gen == 0 || gen != th->th_generation);
333 nanouptime(struct timespec *tsp)
338 bintime2timespec(&bt, tsp);
342 microuptime(struct timeval *tvp)
347 bintime2timeval(&bt, tvp);
351 bintime(struct bintime *bt)
355 bintime_add(bt, &boottimebin);
359 nanotime(struct timespec *tsp)
364 bintime2timespec(&bt, tsp);
368 microtime(struct timeval *tvp)
373 bintime2timeval(&bt, tvp);
377 getbinuptime(struct bintime *bt)
379 struct timehands *th;
384 gen = th->th_generation;
386 } while (gen == 0 || gen != th->th_generation);
390 getnanouptime(struct timespec *tsp)
392 struct timehands *th;
397 gen = th->th_generation;
398 bintime2timespec(&th->th_offset, tsp);
399 } while (gen == 0 || gen != th->th_generation);
403 getmicrouptime(struct timeval *tvp)
405 struct timehands *th;
410 gen = th->th_generation;
411 bintime2timeval(&th->th_offset, tvp);
412 } while (gen == 0 || gen != th->th_generation);
416 getbintime(struct bintime *bt)
418 struct timehands *th;
423 gen = th->th_generation;
425 } while (gen == 0 || gen != th->th_generation);
426 bintime_add(bt, &boottimebin);
430 getnanotime(struct timespec *tsp)
432 struct timehands *th;
437 gen = th->th_generation;
438 *tsp = th->th_nanotime;
439 } while (gen == 0 || gen != th->th_generation);
443 getmicrotime(struct timeval *tvp)
445 struct timehands *th;
450 gen = th->th_generation;
451 *tvp = th->th_microtime;
452 } while (gen == 0 || gen != th->th_generation);
458 * Support for feed-forward synchronization algorithms. This is heavily inspired
459 * by the timehands mechanism but kept independent from it. *_windup() functions
460 * have some connection to avoid accessing the timecounter hardware more than
464 /* Feed-forward clock estimates kept updated by the synchronization daemon. */
465 struct ffclock_estimate ffclock_estimate;
466 struct bintime ffclock_boottime; /* Feed-forward boot time estimate. */
467 uint32_t ffclock_status; /* Feed-forward clock status. */
468 int8_t ffclock_updated; /* New estimates are available. */
469 struct mtx ffclock_mtx; /* Mutex on ffclock_estimate. */
472 struct ffclock_estimate cest;
473 struct bintime tick_time;
474 struct bintime tick_time_lerp;
475 ffcounter tick_ffcount;
476 uint64_t period_lerp;
477 volatile uint8_t gen;
478 struct fftimehands *next;
481 #define NUM_ELEMENTS(x) (sizeof(x) / sizeof(*x))
483 static struct fftimehands ffth[10];
484 static struct fftimehands *volatile fftimehands = ffth;
489 struct fftimehands *cur;
490 struct fftimehands *last;
492 memset(ffth, 0, sizeof(ffth));
494 last = ffth + NUM_ELEMENTS(ffth) - 1;
495 for (cur = ffth; cur < last; cur++)
500 ffclock_status = FFCLOCK_STA_UNSYNC;
501 mtx_init(&ffclock_mtx, "ffclock lock", NULL, MTX_DEF);
505 * Reset the feed-forward clock estimates. Called from inittodr() to get things
506 * kick started and uses the timecounter nominal frequency as a first period
507 * estimate. Note: this function may be called several time just after boot.
508 * Note: this is the only function that sets the value of boot time for the
509 * monotonic (i.e. uptime) version of the feed-forward clock.
512 ffclock_reset_clock(struct timespec *ts)
514 struct timecounter *tc;
515 struct ffclock_estimate cest;
517 tc = timehands->th_counter;
518 memset(&cest, 0, sizeof(struct ffclock_estimate));
520 timespec2bintime(ts, &ffclock_boottime);
521 timespec2bintime(ts, &(cest.update_time));
522 ffclock_read_counter(&cest.update_ffcount);
523 cest.leapsec_next = 0;
524 cest.period = ((1ULL << 63) / tc->tc_frequency) << 1;
527 cest.status = FFCLOCK_STA_UNSYNC;
528 cest.leapsec_total = 0;
531 mtx_lock(&ffclock_mtx);
532 bcopy(&cest, &ffclock_estimate, sizeof(struct ffclock_estimate));
533 ffclock_updated = INT8_MAX;
534 mtx_unlock(&ffclock_mtx);
536 printf("ffclock reset: %s (%llu Hz), time = %ld.%09lu\n", tc->tc_name,
537 (unsigned long long)tc->tc_frequency, (long)ts->tv_sec,
538 (unsigned long)ts->tv_nsec);
542 * Sub-routine to convert a time interval measured in RAW counter units to time
543 * in seconds stored in bintime format.
544 * NOTE: bintime_mul requires u_int, but the value of the ffcounter may be
545 * larger than the max value of u_int (on 32 bit architecture). Loop to consume
549 ffclock_convert_delta(ffcounter ffdelta, uint64_t period, struct bintime *bt)
552 ffcounter delta, delta_max;
554 delta_max = (1ULL << (8 * sizeof(unsigned int))) - 1;
557 if (ffdelta > delta_max)
563 bintime_mul(&bt2, (unsigned int)delta);
564 bintime_add(bt, &bt2);
566 } while (ffdelta > 0);
570 * Update the fftimehands.
571 * Push the tick ffcount and time(s) forward based on current clock estimate.
572 * The conversion from ffcounter to bintime relies on the difference clock
573 * principle, whose accuracy relies on computing small time intervals. If a new
574 * clock estimate has been passed by the synchronisation daemon, make it
575 * current, and compute the linear interpolation for monotonic time if needed.
578 ffclock_windup(unsigned int delta)
580 struct ffclock_estimate *cest;
581 struct fftimehands *ffth;
582 struct bintime bt, gap_lerp;
585 unsigned int polling;
586 uint8_t forward_jump, ogen;
589 * Pick the next timehand, copy current ffclock estimates and move tick
590 * times and counter forward.
593 ffth = fftimehands->next;
597 bcopy(&fftimehands->cest, cest, sizeof(struct ffclock_estimate));
598 ffdelta = (ffcounter)delta;
599 ffth->period_lerp = fftimehands->period_lerp;
601 ffth->tick_time = fftimehands->tick_time;
602 ffclock_convert_delta(ffdelta, cest->period, &bt);
603 bintime_add(&ffth->tick_time, &bt);
605 ffth->tick_time_lerp = fftimehands->tick_time_lerp;
606 ffclock_convert_delta(ffdelta, ffth->period_lerp, &bt);
607 bintime_add(&ffth->tick_time_lerp, &bt);
609 ffth->tick_ffcount = fftimehands->tick_ffcount + ffdelta;
612 * Assess the status of the clock, if the last update is too old, it is
613 * likely the synchronisation daemon is dead and the clock is free
616 if (ffclock_updated == 0) {
617 ffdelta = ffth->tick_ffcount - cest->update_ffcount;
618 ffclock_convert_delta(ffdelta, cest->period, &bt);
619 if (bt.sec > 2 * FFCLOCK_SKM_SCALE)
620 ffclock_status |= FFCLOCK_STA_UNSYNC;
624 * If available, grab updated clock estimates and make them current.
625 * Recompute time at this tick using the updated estimates. The clock
626 * estimates passed the feed-forward synchronisation daemon may result
627 * in time conversion that is not monotonically increasing (just after
628 * the update). time_lerp is a particular linear interpolation over the
629 * synchronisation algo polling period that ensures monotonicity for the
630 * clock ids requesting it.
632 if (ffclock_updated > 0) {
633 bcopy(&ffclock_estimate, cest, sizeof(struct ffclock_estimate));
634 ffdelta = ffth->tick_ffcount - cest->update_ffcount;
635 ffth->tick_time = cest->update_time;
636 ffclock_convert_delta(ffdelta, cest->period, &bt);
637 bintime_add(&ffth->tick_time, &bt);
639 /* ffclock_reset sets ffclock_updated to INT8_MAX */
640 if (ffclock_updated == INT8_MAX)
641 ffth->tick_time_lerp = ffth->tick_time;
643 if (bintime_cmp(&ffth->tick_time, &ffth->tick_time_lerp, >))
648 bintime_clear(&gap_lerp);
650 gap_lerp = ffth->tick_time;
651 bintime_sub(&gap_lerp, &ffth->tick_time_lerp);
653 gap_lerp = ffth->tick_time_lerp;
654 bintime_sub(&gap_lerp, &ffth->tick_time);
658 * The reset from the RTC clock may be far from accurate, and
659 * reducing the gap between real time and interpolated time
660 * could take a very long time if the interpolated clock insists
661 * on strict monotonicity. The clock is reset under very strict
662 * conditions (kernel time is known to be wrong and
663 * synchronization daemon has been restarted recently.
664 * ffclock_boottime absorbs the jump to ensure boot time is
665 * correct and uptime functions stay consistent.
667 if (((ffclock_status & FFCLOCK_STA_UNSYNC) == FFCLOCK_STA_UNSYNC) &&
668 ((cest->status & FFCLOCK_STA_UNSYNC) == 0) &&
669 ((cest->status & FFCLOCK_STA_WARMUP) == FFCLOCK_STA_WARMUP)) {
671 bintime_add(&ffclock_boottime, &gap_lerp);
673 bintime_sub(&ffclock_boottime, &gap_lerp);
674 ffth->tick_time_lerp = ffth->tick_time;
675 bintime_clear(&gap_lerp);
678 ffclock_status = cest->status;
679 ffth->period_lerp = cest->period;
682 * Compute corrected period used for the linear interpolation of
683 * time. The rate of linear interpolation is capped to 5000PPM
686 if (bintime_isset(&gap_lerp)) {
687 ffdelta = cest->update_ffcount;
688 ffdelta -= fftimehands->cest.update_ffcount;
689 ffclock_convert_delta(ffdelta, cest->period, &bt);
692 bt.frac = 5000000 * (uint64_t)18446744073LL;
693 bintime_mul(&bt, polling);
694 if (bintime_cmp(&gap_lerp, &bt, >))
697 /* Approximate 1 sec by 1-(1/2^64) to ease arithmetic */
699 if (gap_lerp.sec > 0) {
701 frac /= ffdelta / gap_lerp.sec;
703 frac += gap_lerp.frac / ffdelta;
706 ffth->period_lerp += frac;
708 ffth->period_lerp -= frac;
720 * Adjust the fftimehands when the timecounter is changed. Stating the obvious,
721 * the old and new hardware counter cannot be read simultaneously. tc_windup()
722 * does read the two counters 'back to back', but a few cycles are effectively
723 * lost, and not accumulated in tick_ffcount. This is a fairly radical
724 * operation for a feed-forward synchronization daemon, and it is its job to not
725 * pushing irrelevant data to the kernel. Because there is no locking here,
726 * simply force to ignore pending or next update to give daemon a chance to
727 * realize the counter has changed.
730 ffclock_change_tc(struct timehands *th)
732 struct fftimehands *ffth;
733 struct ffclock_estimate *cest;
734 struct timecounter *tc;
738 ffth = fftimehands->next;
743 bcopy(&(fftimehands->cest), cest, sizeof(struct ffclock_estimate));
744 cest->period = ((1ULL << 63) / tc->tc_frequency ) << 1;
747 cest->status |= FFCLOCK_STA_UNSYNC;
749 ffth->tick_ffcount = fftimehands->tick_ffcount;
750 ffth->tick_time_lerp = fftimehands->tick_time_lerp;
751 ffth->tick_time = fftimehands->tick_time;
752 ffth->period_lerp = cest->period;
754 /* Do not lock but ignore next update from synchronization daemon. */
764 * Retrieve feed-forward counter and time of last kernel tick.
767 ffclock_last_tick(ffcounter *ffcount, struct bintime *bt, uint32_t flags)
769 struct fftimehands *ffth;
773 * No locking but check generation has not changed. Also need to make
774 * sure ffdelta is positive, i.e. ffcount > tick_ffcount.
779 if ((flags & FFCLOCK_LERP) == FFCLOCK_LERP)
780 *bt = ffth->tick_time_lerp;
782 *bt = ffth->tick_time;
783 *ffcount = ffth->tick_ffcount;
784 } while (gen == 0 || gen != ffth->gen);
788 * Absolute clock conversion. Low level function to convert ffcounter to
789 * bintime. The ffcounter is converted using the current ffclock period estimate
790 * or the "interpolated period" to ensure monotonicity.
791 * NOTE: this conversion may have been deferred, and the clock updated since the
792 * hardware counter has been read.
795 ffclock_convert_abs(ffcounter ffcount, struct bintime *bt, uint32_t flags)
797 struct fftimehands *ffth;
803 * No locking but check generation has not changed. Also need to make
804 * sure ffdelta is positive, i.e. ffcount > tick_ffcount.
809 if (ffcount > ffth->tick_ffcount)
810 ffdelta = ffcount - ffth->tick_ffcount;
812 ffdelta = ffth->tick_ffcount - ffcount;
814 if ((flags & FFCLOCK_LERP) == FFCLOCK_LERP) {
815 *bt = ffth->tick_time_lerp;
816 ffclock_convert_delta(ffdelta, ffth->period_lerp, &bt2);
818 *bt = ffth->tick_time;
819 ffclock_convert_delta(ffdelta, ffth->cest.period, &bt2);
822 if (ffcount > ffth->tick_ffcount)
823 bintime_add(bt, &bt2);
825 bintime_sub(bt, &bt2);
826 } while (gen == 0 || gen != ffth->gen);
830 * Difference clock conversion.
831 * Low level function to Convert a time interval measured in RAW counter units
832 * into bintime. The difference clock allows measuring small intervals much more
833 * reliably than the absolute clock.
836 ffclock_convert_diff(ffcounter ffdelta, struct bintime *bt)
838 struct fftimehands *ffth;
841 /* No locking but check generation has not changed. */
845 ffclock_convert_delta(ffdelta, ffth->cest.period, bt);
846 } while (gen == 0 || gen != ffth->gen);
850 * Access to current ffcounter value.
853 ffclock_read_counter(ffcounter *ffcount)
855 struct timehands *th;
856 struct fftimehands *ffth;
857 unsigned int gen, delta;
860 * ffclock_windup() called from tc_windup(), safe to rely on
861 * th->th_generation only, for correct delta and ffcounter.
865 gen = th->th_generation;
867 delta = tc_delta(th);
868 *ffcount = ffth->tick_ffcount;
869 } while (gen == 0 || gen != th->th_generation);
875 binuptime(struct bintime *bt)
878 binuptime_fromclock(bt, sysclock_active);
882 nanouptime(struct timespec *tsp)
885 nanouptime_fromclock(tsp, sysclock_active);
889 microuptime(struct timeval *tvp)
892 microuptime_fromclock(tvp, sysclock_active);
896 bintime(struct bintime *bt)
899 bintime_fromclock(bt, sysclock_active);
903 nanotime(struct timespec *tsp)
906 nanotime_fromclock(tsp, sysclock_active);
910 microtime(struct timeval *tvp)
913 microtime_fromclock(tvp, sysclock_active);
917 getbinuptime(struct bintime *bt)
920 getbinuptime_fromclock(bt, sysclock_active);
924 getnanouptime(struct timespec *tsp)
927 getnanouptime_fromclock(tsp, sysclock_active);
931 getmicrouptime(struct timeval *tvp)
934 getmicrouptime_fromclock(tvp, sysclock_active);
938 getbintime(struct bintime *bt)
941 getbintime_fromclock(bt, sysclock_active);
945 getnanotime(struct timespec *tsp)
948 getnanotime_fromclock(tsp, sysclock_active);
952 getmicrotime(struct timeval *tvp)
955 getmicrouptime_fromclock(tvp, sysclock_active);
961 * System clock currently providing time to the system. Modifiable via sysctl
962 * when the FFCLOCK option is defined.
964 int sysclock_active = SYSCLOCK_FBCK;
966 /* Internal NTP status and error estimates. */
967 extern int time_status;
968 extern long time_esterror;
971 * Take a snapshot of sysclock data which can be used to compare system clocks
972 * and generate timestamps after the fact.
975 sysclock_getsnapshot(struct sysclock_snap *clock_snap, int fast)
977 struct fbclock_info *fbi;
978 struct timehands *th;
980 unsigned int delta, gen;
983 struct fftimehands *ffth;
984 struct ffclock_info *ffi;
985 struct ffclock_estimate cest;
987 ffi = &clock_snap->ff_info;
990 fbi = &clock_snap->fb_info;
995 gen = th->th_generation;
996 fbi->th_scale = th->th_scale;
997 fbi->tick_time = th->th_offset;
1000 ffi->tick_time = ffth->tick_time_lerp;
1001 ffi->tick_time_lerp = ffth->tick_time_lerp;
1002 ffi->period = ffth->cest.period;
1003 ffi->period_lerp = ffth->period_lerp;
1004 clock_snap->ffcount = ffth->tick_ffcount;
1008 delta = tc_delta(th);
1009 } while (gen == 0 || gen != th->th_generation);
1011 clock_snap->delta = delta;
1012 clock_snap->sysclock_active = sysclock_active;
1014 /* Record feedback clock status and error. */
1015 clock_snap->fb_info.status = time_status;
1016 /* XXX: Very crude estimate of feedback clock error. */
1017 bt.sec = time_esterror / 1000000;
1018 bt.frac = ((time_esterror - bt.sec) * 1000000) *
1019 (uint64_t)18446744073709ULL;
1020 clock_snap->fb_info.error = bt;
1024 clock_snap->ffcount += delta;
1026 /* Record feed-forward clock leap second adjustment. */
1027 ffi->leapsec_adjustment = cest.leapsec_total;
1028 if (clock_snap->ffcount > cest.leapsec_next)
1029 ffi->leapsec_adjustment -= cest.leapsec;
1031 /* Record feed-forward clock status and error. */
1032 clock_snap->ff_info.status = cest.status;
1033 ffcount = clock_snap->ffcount - cest.update_ffcount;
1034 ffclock_convert_delta(ffcount, cest.period, &bt);
1035 /* 18446744073709 = int(2^64/1e12), err_bound_rate in [ps/s]. */
1036 bintime_mul(&bt, cest.errb_rate * (uint64_t)18446744073709ULL);
1037 /* 18446744073 = int(2^64 / 1e9), since err_abs in [ns]. */
1038 bintime_addx(&bt, cest.errb_abs * (uint64_t)18446744073ULL);
1039 clock_snap->ff_info.error = bt;
1044 * Convert a sysclock snapshot into a struct bintime based on the specified
1045 * clock source and flags.
1048 sysclock_snap2bintime(struct sysclock_snap *cs, struct bintime *bt,
1049 int whichclock, uint32_t flags)
1056 switch (whichclock) {
1058 *bt = cs->fb_info.tick_time;
1060 /* If snapshot was created with !fast, delta will be >0. */
1062 bintime_addx(bt, cs->fb_info.th_scale * cs->delta);
1064 if ((flags & FBCLOCK_UPTIME) == 0)
1065 bintime_add(bt, &boottimebin);
1069 if (flags & FFCLOCK_LERP) {
1070 *bt = cs->ff_info.tick_time_lerp;
1071 period = cs->ff_info.period_lerp;
1073 *bt = cs->ff_info.tick_time;
1074 period = cs->ff_info.period;
1077 /* If snapshot was created with !fast, delta will be >0. */
1078 if (cs->delta > 0) {
1079 ffclock_convert_delta(cs->delta, period, &bt2);
1080 bintime_add(bt, &bt2);
1083 /* Leap second adjustment. */
1084 if (flags & FFCLOCK_LEAPSEC)
1085 bt->sec -= cs->ff_info.leapsec_adjustment;
1087 /* Boot time adjustment, for uptime/monotonic clocks. */
1088 if (flags & FFCLOCK_UPTIME)
1089 bintime_sub(bt, &ffclock_boottime);
1101 * Initialize a new timecounter and possibly use it.
1104 tc_init(struct timecounter *tc)
1107 struct sysctl_oid *tc_root;
1109 u = tc->tc_frequency / tc->tc_counter_mask;
1110 /* XXX: We need some margin here, 10% is a guess */
1113 if (u > hz && tc->tc_quality >= 0) {
1114 tc->tc_quality = -2000;
1116 printf("Timecounter \"%s\" frequency %ju Hz",
1117 tc->tc_name, (uintmax_t)tc->tc_frequency);
1118 printf(" -- Insufficient hz, needs at least %u\n", u);
1120 } else if (tc->tc_quality >= 0 || bootverbose) {
1121 printf("Timecounter \"%s\" frequency %ju Hz quality %d\n",
1122 tc->tc_name, (uintmax_t)tc->tc_frequency,
1126 tc->tc_next = timecounters;
1129 * Set up sysctl tree for this counter.
1131 tc_root = SYSCTL_ADD_NODE(NULL,
1132 SYSCTL_STATIC_CHILDREN(_kern_timecounter_tc), OID_AUTO, tc->tc_name,
1133 CTLFLAG_RW, 0, "timecounter description");
1134 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1135 "mask", CTLFLAG_RD, &(tc->tc_counter_mask), 0,
1136 "mask for implemented bits");
1137 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1138 "counter", CTLTYPE_UINT | CTLFLAG_RD, tc, sizeof(*tc),
1139 sysctl_kern_timecounter_get, "IU", "current timecounter value");
1140 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1141 "frequency", CTLTYPE_U64 | CTLFLAG_RD, tc, sizeof(*tc),
1142 sysctl_kern_timecounter_freq, "QU", "timecounter frequency");
1143 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
1144 "quality", CTLFLAG_RD, &(tc->tc_quality), 0,
1145 "goodness of time counter");
1147 * Never automatically use a timecounter with negative quality.
1148 * Even though we run on the dummy counter, switching here may be
1149 * worse since this timecounter may not be monotonous.
1151 if (tc->tc_quality < 0)
1153 if (tc->tc_quality < timecounter->tc_quality)
1155 if (tc->tc_quality == timecounter->tc_quality &&
1156 tc->tc_frequency < timecounter->tc_frequency)
1158 (void)tc->tc_get_timecount(tc);
1159 (void)tc->tc_get_timecount(tc);
1163 /* Report the frequency of the current timecounter. */
1165 tc_getfrequency(void)
1168 return (timehands->th_counter->tc_frequency);
1172 * Step our concept of UTC. This is done by modifying our estimate of
1177 tc_setclock(struct timespec *ts)
1179 struct timespec tbef, taft;
1180 struct bintime bt, bt2;
1182 cpu_tick_calibrate(1);
1184 timespec2bintime(ts, &bt);
1186 bintime_sub(&bt, &bt2);
1187 bintime_add(&bt2, &boottimebin);
1189 bintime2timeval(&bt, &boottime);
1191 /* XXX fiddle all the little crinkly bits around the fiords... */
1194 if (timestepwarnings) {
1196 "Time stepped from %jd.%09ld to %jd.%09ld (%jd.%09ld)\n",
1197 (intmax_t)tbef.tv_sec, tbef.tv_nsec,
1198 (intmax_t)taft.tv_sec, taft.tv_nsec,
1199 (intmax_t)ts->tv_sec, ts->tv_nsec);
1201 cpu_tick_calibrate(1);
1205 * Initialize the next struct timehands in the ring and make
1206 * it the active timehands. Along the way we might switch to a different
1207 * timecounter and/or do seconds processing in NTP. Slightly magic.
1213 struct timehands *th, *tho;
1215 u_int delta, ncount, ogen;
1220 * Make the next timehands a copy of the current one, but do not
1221 * overwrite the generation or next pointer. While we update
1222 * the contents, the generation must be zero.
1226 ogen = th->th_generation;
1227 th->th_generation = 0;
1228 bcopy(tho, th, offsetof(struct timehands, th_generation));
1231 * Capture a timecounter delta on the current timecounter and if
1232 * changing timecounters, a counter value from the new timecounter.
1233 * Update the offset fields accordingly.
1235 delta = tc_delta(th);
1236 if (th->th_counter != timecounter)
1237 ncount = timecounter->tc_get_timecount(timecounter);
1241 ffclock_windup(delta);
1243 th->th_offset_count += delta;
1244 th->th_offset_count &= th->th_counter->tc_counter_mask;
1245 while (delta > th->th_counter->tc_frequency) {
1246 /* Eat complete unadjusted seconds. */
1247 delta -= th->th_counter->tc_frequency;
1248 th->th_offset.sec++;
1250 if ((delta > th->th_counter->tc_frequency / 2) &&
1251 (th->th_scale * delta < ((uint64_t)1 << 63))) {
1252 /* The product th_scale * delta just barely overflows. */
1253 th->th_offset.sec++;
1255 bintime_addx(&th->th_offset, th->th_scale * delta);
1258 * Hardware latching timecounters may not generate interrupts on
1259 * PPS events, so instead we poll them. There is a finite risk that
1260 * the hardware might capture a count which is later than the one we
1261 * got above, and therefore possibly in the next NTP second which might
1262 * have a different rate than the current NTP second. It doesn't
1263 * matter in practice.
1265 if (tho->th_counter->tc_poll_pps)
1266 tho->th_counter->tc_poll_pps(tho->th_counter);
1269 * Deal with NTP second processing. The for loop normally
1270 * iterates at most once, but in extreme situations it might
1271 * keep NTP sane if timeouts are not run for several seconds.
1272 * At boot, the time step can be large when the TOD hardware
1273 * has been read, so on really large steps, we call
1274 * ntp_update_second only twice. We need to call it twice in
1275 * case we missed a leap second.
1278 bintime_add(&bt, &boottimebin);
1279 i = bt.sec - tho->th_microtime.tv_sec;
1282 for (; i > 0; i--) {
1284 ntp_update_second(&th->th_adjustment, &bt.sec);
1286 boottimebin.sec += bt.sec - t;
1288 /* Update the UTC timestamps used by the get*() functions. */
1289 /* XXX shouldn't do this here. Should force non-`get' versions. */
1290 bintime2timeval(&bt, &th->th_microtime);
1291 bintime2timespec(&bt, &th->th_nanotime);
1293 /* Now is a good time to change timecounters. */
1294 if (th->th_counter != timecounter) {
1296 if ((timecounter->tc_flags & TC_FLAGS_C3STOP) != 0)
1297 cpu_disable_deep_sleep++;
1298 if ((th->th_counter->tc_flags & TC_FLAGS_C3STOP) != 0)
1299 cpu_disable_deep_sleep--;
1301 th->th_counter = timecounter;
1302 th->th_offset_count = ncount;
1303 tc_min_ticktock_freq = max(1, timecounter->tc_frequency /
1304 (((uint64_t)timecounter->tc_counter_mask + 1) / 3));
1306 ffclock_change_tc(th);
1311 * Recalculate the scaling factor. We want the number of 1/2^64
1312 * fractions of a second per period of the hardware counter, taking
1313 * into account the th_adjustment factor which the NTP PLL/adjtime(2)
1314 * processing provides us with.
1316 * The th_adjustment is nanoseconds per second with 32 bit binary
1317 * fraction and we want 64 bit binary fraction of second:
1319 * x = a * 2^32 / 10^9 = a * 4.294967296
1321 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
1322 * we can only multiply by about 850 without overflowing, that
1323 * leaves no suitably precise fractions for multiply before divide.
1325 * Divide before multiply with a fraction of 2199/512 results in a
1326 * systematic undercompensation of 10PPM of th_adjustment. On a
1327 * 5000PPM adjustment this is a 0.05PPM error. This is acceptable.
1329 * We happily sacrifice the lowest of the 64 bits of our result
1330 * to the goddess of code clarity.
1333 scale = (uint64_t)1 << 63;
1334 scale += (th->th_adjustment / 1024) * 2199;
1335 scale /= th->th_counter->tc_frequency;
1336 th->th_scale = scale * 2;
1339 * Now that the struct timehands is again consistent, set the new
1340 * generation number, making sure to not make it zero.
1344 th->th_generation = ogen;
1346 /* Go live with the new struct timehands. */
1348 switch (sysclock_active) {
1351 time_second = th->th_microtime.tv_sec;
1352 time_uptime = th->th_offset.sec;
1356 time_second = fftimehands->tick_time_lerp.sec;
1357 time_uptime = fftimehands->tick_time_lerp.sec - ffclock_boottime.sec;
1365 /* Report or change the active timecounter hardware. */
1367 sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
1370 struct timecounter *newtc, *tc;
1374 strlcpy(newname, tc->tc_name, sizeof(newname));
1376 error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
1377 if (error != 0 || req->newptr == NULL ||
1378 strcmp(newname, tc->tc_name) == 0)
1380 for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) {
1381 if (strcmp(newname, newtc->tc_name) != 0)
1384 /* Warm up new timecounter. */
1385 (void)newtc->tc_get_timecount(newtc);
1386 (void)newtc->tc_get_timecount(newtc);
1388 timecounter = newtc;
1394 SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW,
1395 0, 0, sysctl_kern_timecounter_hardware, "A",
1396 "Timecounter hardware selected");
1399 /* Report or change the active timecounter hardware. */
1401 sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS)
1404 struct timecounter *tc;
1409 for (tc = timecounters; error == 0 && tc != NULL; tc = tc->tc_next) {
1410 sprintf(buf, "%s%s(%d)",
1411 spc, tc->tc_name, tc->tc_quality);
1412 error = SYSCTL_OUT(req, buf, strlen(buf));
1418 SYSCTL_PROC(_kern_timecounter, OID_AUTO, choice, CTLTYPE_STRING | CTLFLAG_RD,
1419 0, 0, sysctl_kern_timecounter_choice, "A", "Timecounter hardware detected");
1422 * RFC 2783 PPS-API implementation.
1426 pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
1429 struct pps_fetch_args *fapi;
1431 struct pps_fetch_ffc_args *fapi_ffc;
1434 struct pps_kcbind_args *kapi;
1437 KASSERT(pps != NULL, ("NULL pps pointer in pps_ioctl"));
1439 case PPS_IOC_CREATE:
1441 case PPS_IOC_DESTROY:
1443 case PPS_IOC_SETPARAMS:
1444 app = (pps_params_t *)data;
1445 if (app->mode & ~pps->ppscap)
1448 /* Ensure only a single clock is selected for ffc timestamp. */
1449 if ((app->mode & PPS_TSCLK_MASK) == PPS_TSCLK_MASK)
1452 pps->ppsparam = *app;
1454 case PPS_IOC_GETPARAMS:
1455 app = (pps_params_t *)data;
1456 *app = pps->ppsparam;
1457 app->api_version = PPS_API_VERS_1;
1459 case PPS_IOC_GETCAP:
1460 *(int*)data = pps->ppscap;
1463 fapi = (struct pps_fetch_args *)data;
1464 if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
1466 if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec)
1467 return (EOPNOTSUPP);
1468 pps->ppsinfo.current_mode = pps->ppsparam.mode;
1469 fapi->pps_info_buf = pps->ppsinfo;
1472 case PPS_IOC_FETCH_FFCOUNTER:
1473 fapi_ffc = (struct pps_fetch_ffc_args *)data;
1474 if (fapi_ffc->tsformat && fapi_ffc->tsformat !=
1477 if (fapi_ffc->timeout.tv_sec || fapi_ffc->timeout.tv_nsec)
1478 return (EOPNOTSUPP);
1479 pps->ppsinfo_ffc.current_mode = pps->ppsparam.mode;
1480 fapi_ffc->pps_info_buf_ffc = pps->ppsinfo_ffc;
1481 /* Overwrite timestamps if feedback clock selected. */
1482 switch (pps->ppsparam.mode & PPS_TSCLK_MASK) {
1483 case PPS_TSCLK_FBCK:
1484 fapi_ffc->pps_info_buf_ffc.assert_timestamp =
1485 pps->ppsinfo.assert_timestamp;
1486 fapi_ffc->pps_info_buf_ffc.clear_timestamp =
1487 pps->ppsinfo.clear_timestamp;
1489 case PPS_TSCLK_FFWD:
1495 #endif /* FFCLOCK */
1496 case PPS_IOC_KCBIND:
1498 kapi = (struct pps_kcbind_args *)data;
1499 /* XXX Only root should be able to do this */
1500 if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
1502 if (kapi->kernel_consumer != PPS_KC_HARDPPS)
1504 if (kapi->edge & ~pps->ppscap)
1506 pps->kcmode = kapi->edge;
1509 return (EOPNOTSUPP);
1517 pps_init(struct pps_state *pps)
1519 pps->ppscap |= PPS_TSFMT_TSPEC;
1520 if (pps->ppscap & PPS_CAPTUREASSERT)
1521 pps->ppscap |= PPS_OFFSETASSERT;
1522 if (pps->ppscap & PPS_CAPTURECLEAR)
1523 pps->ppscap |= PPS_OFFSETCLEAR;
1525 pps->ppscap |= PPS_TSCLK_MASK;
1530 pps_capture(struct pps_state *pps)
1532 struct timehands *th;
1534 KASSERT(pps != NULL, ("NULL pps pointer in pps_capture"));
1536 pps->capgen = th->th_generation;
1539 pps->capffth = fftimehands;
1541 pps->capcount = th->th_counter->tc_get_timecount(th->th_counter);
1542 if (pps->capgen != th->th_generation)
1547 pps_event(struct pps_state *pps, int event)
1550 struct timespec ts, *tsp, *osp;
1551 u_int tcount, *pcount;
1555 struct timespec *tsp_ffc;
1556 pps_seq_t *pseq_ffc;
1560 KASSERT(pps != NULL, ("NULL pps pointer in pps_event"));
1561 /* If the timecounter was wound up underneath us, bail out. */
1562 if (pps->capgen == 0 || pps->capgen != pps->capth->th_generation)
1565 /* Things would be easier with arrays. */
1566 if (event == PPS_CAPTUREASSERT) {
1567 tsp = &pps->ppsinfo.assert_timestamp;
1568 osp = &pps->ppsparam.assert_offset;
1569 foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
1570 fhard = pps->kcmode & PPS_CAPTUREASSERT;
1571 pcount = &pps->ppscount[0];
1572 pseq = &pps->ppsinfo.assert_sequence;
1574 ffcount = &pps->ppsinfo_ffc.assert_ffcount;
1575 tsp_ffc = &pps->ppsinfo_ffc.assert_timestamp;
1576 pseq_ffc = &pps->ppsinfo_ffc.assert_sequence;
1579 tsp = &pps->ppsinfo.clear_timestamp;
1580 osp = &pps->ppsparam.clear_offset;
1581 foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
1582 fhard = pps->kcmode & PPS_CAPTURECLEAR;
1583 pcount = &pps->ppscount[1];
1584 pseq = &pps->ppsinfo.clear_sequence;
1586 ffcount = &pps->ppsinfo_ffc.clear_ffcount;
1587 tsp_ffc = &pps->ppsinfo_ffc.clear_timestamp;
1588 pseq_ffc = &pps->ppsinfo_ffc.clear_sequence;
1593 * If the timecounter changed, we cannot compare the count values, so
1594 * we have to drop the rest of the PPS-stuff until the next event.
1596 if (pps->ppstc != pps->capth->th_counter) {
1597 pps->ppstc = pps->capth->th_counter;
1598 *pcount = pps->capcount;
1599 pps->ppscount[2] = pps->capcount;
1603 /* Convert the count to a timespec. */
1604 tcount = pps->capcount - pps->capth->th_offset_count;
1605 tcount &= pps->capth->th_counter->tc_counter_mask;
1606 bt = pps->capth->th_offset;
1607 bintime_addx(&bt, pps->capth->th_scale * tcount);
1608 bintime_add(&bt, &boottimebin);
1609 bintime2timespec(&bt, &ts);
1611 /* If the timecounter was wound up underneath us, bail out. */
1612 if (pps->capgen != pps->capth->th_generation)
1615 *pcount = pps->capcount;
1620 timespecadd(tsp, osp);
1621 if (tsp->tv_nsec < 0) {
1622 tsp->tv_nsec += 1000000000;
1628 *ffcount = pps->capffth->tick_ffcount + tcount;
1629 bt = pps->capffth->tick_time;
1630 ffclock_convert_delta(tcount, pps->capffth->cest.period, &bt);
1631 bintime_add(&bt, &pps->capffth->tick_time);
1632 bintime2timespec(&bt, &ts);
1642 * Feed the NTP PLL/FLL.
1643 * The FLL wants to know how many (hardware) nanoseconds
1644 * elapsed since the previous event.
1646 tcount = pps->capcount - pps->ppscount[2];
1647 pps->ppscount[2] = pps->capcount;
1648 tcount &= pps->capth->th_counter->tc_counter_mask;
1649 scale = (uint64_t)1 << 63;
1650 scale /= pps->capth->th_counter->tc_frequency;
1654 bintime_addx(&bt, scale * tcount);
1655 bintime2timespec(&bt, &ts);
1656 hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec);
1662 * Timecounters need to be updated every so often to prevent the hardware
1663 * counter from overflowing. Updating also recalculates the cached values
1664 * used by the get*() family of functions, so their precision depends on
1665 * the update frequency.
1669 SYSCTL_INT(_kern_timecounter, OID_AUTO, tick, CTLFLAG_RD, &tc_tick, 0,
1670 "Approximate number of hardclock ticks in a millisecond");
1673 tc_ticktock(int cnt)
1678 if (count < tc_tick)
1685 inittimecounter(void *dummy)
1690 * Set the initial timeout to
1691 * max(1, <approx. number of hardclock ticks in a millisecond>).
1692 * People should probably not use the sysctl to set the timeout
1693 * to smaller than its inital value, since that value is the
1694 * smallest reasonable one. If they want better timestamps they
1695 * should use the non-"get"* functions.
1698 tc_tick = (hz + 500) / 1000;
1701 p = (tc_tick * 1000000) / hz;
1702 printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000);
1707 /* warm up new timecounter (again) and get rolling. */
1708 (void)timecounter->tc_get_timecount(timecounter);
1709 (void)timecounter->tc_get_timecount(timecounter);
1713 SYSINIT(timecounter, SI_SUB_CLOCKS, SI_ORDER_SECOND, inittimecounter, NULL);
1715 /* Cpu tick handling -------------------------------------------------*/
1717 static int cpu_tick_variable;
1718 static uint64_t cpu_tick_frequency;
1723 static uint64_t base;
1724 static unsigned last;
1726 struct timecounter *tc;
1728 tc = timehands->th_counter;
1729 u = tc->tc_get_timecount(tc) & tc->tc_counter_mask;
1731 base += (uint64_t)tc->tc_counter_mask + 1;
1737 cpu_tick_calibration(void)
1739 static time_t last_calib;
1741 if (time_uptime != last_calib && !(time_uptime & 0xf)) {
1742 cpu_tick_calibrate(0);
1743 last_calib = time_uptime;
1748 * This function gets called every 16 seconds on only one designated
1749 * CPU in the system from hardclock() via cpu_tick_calibration()().
1751 * Whenever the real time clock is stepped we get called with reset=1
1752 * to make sure we handle suspend/resume and similar events correctly.
1756 cpu_tick_calibrate(int reset)
1758 static uint64_t c_last;
1759 uint64_t c_this, c_delta;
1760 static struct bintime t_last;
1761 struct bintime t_this, t_delta;
1765 /* The clock was stepped, abort & reset */
1770 /* we don't calibrate fixed rate cputicks */
1771 if (!cpu_tick_variable)
1774 getbinuptime(&t_this);
1775 c_this = cpu_ticks();
1776 if (t_last.sec != 0) {
1777 c_delta = c_this - c_last;
1779 bintime_sub(&t_delta, &t_last);
1782 * 2^(64-20) / 16[s] =
1784 * 17.592.186.044.416 / 16 =
1785 * 1.099.511.627.776 [Hz]
1787 divi = t_delta.sec << 20;
1788 divi |= t_delta.frac >> (64 - 20);
1791 if (c_delta > cpu_tick_frequency) {
1792 if (0 && bootverbose)
1793 printf("cpu_tick increased to %ju Hz\n",
1795 cpu_tick_frequency = c_delta;
1803 set_cputicker(cpu_tick_f *func, uint64_t freq, unsigned var)
1807 cpu_ticks = tc_cpu_ticks;
1809 cpu_tick_frequency = freq;
1810 cpu_tick_variable = var;
1819 if (cpu_ticks == tc_cpu_ticks)
1820 return (tc_getfrequency());
1821 return (cpu_tick_frequency);
1825 * We need to be slightly careful converting cputicks to microseconds.
1826 * There is plenty of margin in 64 bits of microseconds (half a million
1827 * years) and in 64 bits at 4 GHz (146 years), but if we do a multiply
1828 * before divide conversion (to retain precision) we find that the
1829 * margin shrinks to 1.5 hours (one millionth of 146y).
1830 * With a three prong approach we never lose significant bits, no
1831 * matter what the cputick rate and length of timeinterval is.
1835 cputick2usec(uint64_t tick)
1838 if (tick > 18446744073709551LL) /* floor(2^64 / 1000) */
1839 return (tick / (cpu_tickrate() / 1000000LL));
1840 else if (tick > 18446744073709LL) /* floor(2^64 / 1000000) */
1841 return ((tick * 1000LL) / (cpu_tickrate() / 1000LL));
1843 return ((tick * 1000000LL) / cpu_tickrate());
1846 cpu_tick_f *cpu_ticks = tc_cpu_ticks;