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 * ----------------------------------------------------------------------------
10 #include <sys/cdefs.h>
11 __FBSDID("$FreeBSD$");
15 #include <sys/param.h>
16 #include <sys/kernel.h>
17 #include <sys/sysctl.h>
18 #include <sys/syslog.h>
19 #include <sys/systm.h>
20 #include <sys/timepps.h>
21 #include <sys/timetc.h>
22 #include <sys/timex.h>
25 * A large step happens on boot. This constant detects such steps.
26 * It is relatively small so that ntp_update_second gets called enough
27 * in the typical 'missed a couple of seconds' case, but doesn't loop
28 * forever when the time step is large.
30 #define LARGE_STEP 200
33 * Implement a dummy timecounter which we can use until we get a real one
34 * in the air. This allows the console and other early stuff to use
39 dummy_get_timecount(struct timecounter *tc)
46 static struct timecounter dummy_timecounter = {
47 dummy_get_timecount, 0, ~0u, 1000000, "dummy", -1000000
51 /* These fields must be initialized by the driver. */
52 struct timecounter *th_counter;
53 int64_t th_adjustment;
55 u_int th_offset_count;
56 struct bintime th_offset;
57 struct timeval th_microtime;
58 struct timespec th_nanotime;
59 /* Fields not to be copied in tc_windup start with th_generation. */
60 volatile u_int th_generation;
61 struct timehands *th_next;
64 static struct timehands th0;
65 static struct timehands th9 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th0};
66 static struct timehands th8 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th9};
67 static struct timehands th7 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th8};
68 static struct timehands th6 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th7};
69 static struct timehands th5 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th6};
70 static struct timehands th4 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th5};
71 static struct timehands th3 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th4};
72 static struct timehands th2 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th3};
73 static struct timehands th1 = { NULL, 0, 0, 0, {0, 0}, {0, 0}, {0, 0}, 0, &th2};
74 static struct timehands th0 = {
77 (uint64_t)-1 / 1000000,
86 static struct timehands *volatile timehands = &th0;
87 struct timecounter *timecounter = &dummy_timecounter;
88 static struct timecounter *timecounters = &dummy_timecounter;
90 time_t time_second = 1;
91 time_t time_uptime = 1;
93 static struct bintime boottimebin;
94 struct timeval boottime;
95 static int sysctl_kern_boottime(SYSCTL_HANDLER_ARGS);
96 SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime, CTLTYPE_STRUCT|CTLFLAG_RD,
97 NULL, 0, sysctl_kern_boottime, "S,timeval", "System boottime");
99 SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
100 SYSCTL_NODE(_kern_timecounter, OID_AUTO, tc, CTLFLAG_RW, 0, "");
102 static int timestepwarnings;
103 SYSCTL_INT(_kern_timecounter, OID_AUTO, stepwarnings, CTLFLAG_RW,
104 ×tepwarnings, 0, "");
106 static void tc_windup(void);
107 static void cpu_tick_calibrate(int);
110 sysctl_kern_boottime(SYSCTL_HANDLER_ARGS)
115 if (req->flags & SCTL_MASK32) {
116 tv[0] = boottime.tv_sec;
117 tv[1] = boottime.tv_usec;
118 return SYSCTL_OUT(req, tv, sizeof(tv));
121 return SYSCTL_OUT(req, &boottime, sizeof(boottime));
125 sysctl_kern_timecounter_get(SYSCTL_HANDLER_ARGS)
128 struct timecounter *tc = arg1;
130 ncount = tc->tc_get_timecount(tc);
131 return sysctl_handle_int(oidp, &ncount, 0, req);
135 sysctl_kern_timecounter_freq(SYSCTL_HANDLER_ARGS)
138 struct timecounter *tc = arg1;
140 freq = tc->tc_frequency;
141 return sysctl_handle_quad(oidp, &freq, 0, req);
145 * Return the difference between the timehands' counter value now and what
146 * was when we copied it to the timehands' offset_count.
148 static __inline u_int
149 tc_delta(struct timehands *th)
151 struct timecounter *tc;
154 return ((tc->tc_get_timecount(tc) - th->th_offset_count) &
155 tc->tc_counter_mask);
159 * Functions for reading the time. We have to loop until we are sure that
160 * the timehands that we operated on was not updated under our feet. See
161 * the comment in <sys/time.h> for a description of these 12 functions.
165 binuptime(struct bintime *bt)
167 struct timehands *th;
172 gen = th->th_generation;
174 bintime_addx(bt, th->th_scale * tc_delta(th));
175 } while (gen == 0 || gen != th->th_generation);
179 nanouptime(struct timespec *tsp)
184 bintime2timespec(&bt, tsp);
188 microuptime(struct timeval *tvp)
193 bintime2timeval(&bt, tvp);
197 bintime(struct bintime *bt)
201 bintime_add(bt, &boottimebin);
205 nanotime(struct timespec *tsp)
210 bintime2timespec(&bt, tsp);
214 microtime(struct timeval *tvp)
219 bintime2timeval(&bt, tvp);
223 getbinuptime(struct bintime *bt)
225 struct timehands *th;
230 gen = th->th_generation;
232 } while (gen == 0 || gen != th->th_generation);
236 getnanouptime(struct timespec *tsp)
238 struct timehands *th;
243 gen = th->th_generation;
244 bintime2timespec(&th->th_offset, tsp);
245 } while (gen == 0 || gen != th->th_generation);
249 getmicrouptime(struct timeval *tvp)
251 struct timehands *th;
256 gen = th->th_generation;
257 bintime2timeval(&th->th_offset, tvp);
258 } while (gen == 0 || gen != th->th_generation);
262 getbintime(struct bintime *bt)
264 struct timehands *th;
269 gen = th->th_generation;
271 } while (gen == 0 || gen != th->th_generation);
272 bintime_add(bt, &boottimebin);
276 getnanotime(struct timespec *tsp)
278 struct timehands *th;
283 gen = th->th_generation;
284 *tsp = th->th_nanotime;
285 } while (gen == 0 || gen != th->th_generation);
289 getmicrotime(struct timeval *tvp)
291 struct timehands *th;
296 gen = th->th_generation;
297 *tvp = th->th_microtime;
298 } while (gen == 0 || gen != th->th_generation);
302 * Initialize a new timecounter and possibly use it.
305 tc_init(struct timecounter *tc)
308 struct sysctl_oid *tc_root;
310 u = tc->tc_frequency / tc->tc_counter_mask;
311 /* XXX: We need some margin here, 10% is a guess */
314 if (u > hz && tc->tc_quality >= 0) {
315 tc->tc_quality = -2000;
317 printf("Timecounter \"%s\" frequency %ju Hz",
318 tc->tc_name, (uintmax_t)tc->tc_frequency);
319 printf(" -- Insufficient hz, needs at least %u\n", u);
321 } else if (tc->tc_quality >= 0 || bootverbose) {
322 printf("Timecounter \"%s\" frequency %ju Hz quality %d\n",
323 tc->tc_name, (uintmax_t)tc->tc_frequency,
327 tc->tc_next = timecounters;
330 * Set up sysctl tree for this counter.
332 tc_root = SYSCTL_ADD_NODE(NULL,
333 SYSCTL_STATIC_CHILDREN(_kern_timecounter_tc), OID_AUTO, tc->tc_name,
334 CTLFLAG_RW, 0, "timecounter description");
335 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
336 "mask", CTLFLAG_RD, &(tc->tc_counter_mask), 0,
337 "mask for implemented bits");
338 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
339 "counter", CTLTYPE_UINT | CTLFLAG_RD, tc, sizeof(*tc),
340 sysctl_kern_timecounter_get, "IU", "current timecounter value");
341 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
342 "frequency", CTLTYPE_QUAD | CTLFLAG_RD, tc, sizeof(*tc),
343 sysctl_kern_timecounter_freq, "QU", "timecounter frequency");
344 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
345 "quality", CTLFLAG_RD, &(tc->tc_quality), 0,
346 "goodness of time counter");
348 * Never automatically use a timecounter with negative quality.
349 * Even though we run on the dummy counter, switching here may be
350 * worse since this timecounter may not be monotonous.
352 if (tc->tc_quality < 0)
354 if (tc->tc_quality < timecounter->tc_quality)
356 if (tc->tc_quality == timecounter->tc_quality &&
357 tc->tc_frequency < timecounter->tc_frequency)
359 (void)tc->tc_get_timecount(tc);
360 (void)tc->tc_get_timecount(tc);
364 /* Report the frequency of the current timecounter. */
366 tc_getfrequency(void)
369 return (timehands->th_counter->tc_frequency);
373 * Step our concept of UTC. This is done by modifying our estimate of
378 tc_setclock(struct timespec *ts)
380 struct timespec tbef, taft;
381 struct bintime bt, bt2;
383 cpu_tick_calibrate(1);
385 timespec2bintime(ts, &bt);
387 bintime_sub(&bt, &bt2);
388 bintime_add(&bt2, &boottimebin);
390 bintime2timeval(&bt, &boottime);
392 /* XXX fiddle all the little crinkly bits around the fiords... */
395 if (timestepwarnings) {
397 "Time stepped from %jd.%09ld to %jd.%09ld (%jd.%09ld)\n",
398 (intmax_t)tbef.tv_sec, tbef.tv_nsec,
399 (intmax_t)taft.tv_sec, taft.tv_nsec,
400 (intmax_t)ts->tv_sec, ts->tv_nsec);
402 cpu_tick_calibrate(1);
406 * Initialize the next struct timehands in the ring and make
407 * it the active timehands. Along the way we might switch to a different
408 * timecounter and/or do seconds processing in NTP. Slightly magic.
414 struct timehands *th, *tho;
416 u_int delta, ncount, ogen;
421 * Make the next timehands a copy of the current one, but do not
422 * overwrite the generation or next pointer. While we update
423 * the contents, the generation must be zero.
427 ogen = th->th_generation;
428 th->th_generation = 0;
429 bcopy(tho, th, offsetof(struct timehands, th_generation));
432 * Capture a timecounter delta on the current timecounter and if
433 * changing timecounters, a counter value from the new timecounter.
434 * Update the offset fields accordingly.
436 delta = tc_delta(th);
437 if (th->th_counter != timecounter)
438 ncount = timecounter->tc_get_timecount(timecounter);
441 th->th_offset_count += delta;
442 th->th_offset_count &= th->th_counter->tc_counter_mask;
443 bintime_addx(&th->th_offset, th->th_scale * delta);
446 * Hardware latching timecounters may not generate interrupts on
447 * PPS events, so instead we poll them. There is a finite risk that
448 * the hardware might capture a count which is later than the one we
449 * got above, and therefore possibly in the next NTP second which might
450 * have a different rate than the current NTP second. It doesn't
451 * matter in practice.
453 if (tho->th_counter->tc_poll_pps)
454 tho->th_counter->tc_poll_pps(tho->th_counter);
457 * Deal with NTP second processing. The for loop normally
458 * iterates at most once, but in extreme situations it might
459 * keep NTP sane if timeouts are not run for several seconds.
460 * At boot, the time step can be large when the TOD hardware
461 * has been read, so on really large steps, we call
462 * ntp_update_second only twice. We need to call it twice in
463 * case we missed a leap second.
466 bintime_add(&bt, &boottimebin);
467 i = bt.sec - tho->th_microtime.tv_sec;
472 ntp_update_second(&th->th_adjustment, &bt.sec);
474 boottimebin.sec += bt.sec - t;
476 /* Update the UTC timestamps used by the get*() functions. */
477 /* XXX shouldn't do this here. Should force non-`get' versions. */
478 bintime2timeval(&bt, &th->th_microtime);
479 bintime2timespec(&bt, &th->th_nanotime);
481 /* Now is a good time to change timecounters. */
482 if (th->th_counter != timecounter) {
483 th->th_counter = timecounter;
484 th->th_offset_count = ncount;
488 * Recalculate the scaling factor. We want the number of 1/2^64
489 * fractions of a second per period of the hardware counter, taking
490 * into account the th_adjustment factor which the NTP PLL/adjtime(2)
491 * processing provides us with.
493 * The th_adjustment is nanoseconds per second with 32 bit binary
494 * fraction and we want 64 bit binary fraction of second:
496 * x = a * 2^32 / 10^9 = a * 4.294967296
498 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
499 * we can only multiply by about 850 without overflowing, that
500 * leaves no suitably precise fractions for multiply before divide.
502 * Divide before multiply with a fraction of 2199/512 results in a
503 * systematic undercompensation of 10PPM of th_adjustment. On a
504 * 5000PPM adjustment this is a 0.05PPM error. This is acceptable.
506 * We happily sacrifice the lowest of the 64 bits of our result
507 * to the goddess of code clarity.
510 scale = (u_int64_t)1 << 63;
511 scale += (th->th_adjustment / 1024) * 2199;
512 scale /= th->th_counter->tc_frequency;
513 th->th_scale = scale * 2;
516 * Now that the struct timehands is again consistent, set the new
517 * generation number, making sure to not make it zero.
521 th->th_generation = ogen;
523 /* Go live with the new struct timehands. */
524 time_second = th->th_microtime.tv_sec;
525 time_uptime = th->th_offset.sec;
529 /* Report or change the active timecounter hardware. */
531 sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
534 struct timecounter *newtc, *tc;
538 strlcpy(newname, tc->tc_name, sizeof(newname));
540 error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
541 if (error != 0 || req->newptr == NULL ||
542 strcmp(newname, tc->tc_name) == 0)
544 for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) {
545 if (strcmp(newname, newtc->tc_name) != 0)
548 /* Warm up new timecounter. */
549 (void)newtc->tc_get_timecount(newtc);
550 (void)newtc->tc_get_timecount(newtc);
558 SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW,
559 0, 0, sysctl_kern_timecounter_hardware, "A", "");
562 /* Report or change the active timecounter hardware. */
564 sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS)
567 struct timecounter *tc;
572 for (tc = timecounters; error == 0 && tc != NULL; tc = tc->tc_next) {
573 sprintf(buf, "%s%s(%d)",
574 spc, tc->tc_name, tc->tc_quality);
575 error = SYSCTL_OUT(req, buf, strlen(buf));
581 SYSCTL_PROC(_kern_timecounter, OID_AUTO, choice, CTLTYPE_STRING | CTLFLAG_RD,
582 0, 0, sysctl_kern_timecounter_choice, "A", "");
585 * RFC 2783 PPS-API implementation.
589 pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
592 struct pps_fetch_args *fapi;
594 struct pps_kcbind_args *kapi;
597 KASSERT(pps != NULL, ("NULL pps pointer in pps_ioctl"));
601 case PPS_IOC_DESTROY:
603 case PPS_IOC_SETPARAMS:
604 app = (pps_params_t *)data;
605 if (app->mode & ~pps->ppscap)
607 pps->ppsparam = *app;
609 case PPS_IOC_GETPARAMS:
610 app = (pps_params_t *)data;
611 *app = pps->ppsparam;
612 app->api_version = PPS_API_VERS_1;
615 *(int*)data = pps->ppscap;
618 fapi = (struct pps_fetch_args *)data;
619 if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
621 if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec)
623 pps->ppsinfo.current_mode = pps->ppsparam.mode;
624 fapi->pps_info_buf = pps->ppsinfo;
628 kapi = (struct pps_kcbind_args *)data;
629 /* XXX Only root should be able to do this */
630 if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
632 if (kapi->kernel_consumer != PPS_KC_HARDPPS)
634 if (kapi->edge & ~pps->ppscap)
636 pps->kcmode = kapi->edge;
647 pps_init(struct pps_state *pps)
649 pps->ppscap |= PPS_TSFMT_TSPEC;
650 if (pps->ppscap & PPS_CAPTUREASSERT)
651 pps->ppscap |= PPS_OFFSETASSERT;
652 if (pps->ppscap & PPS_CAPTURECLEAR)
653 pps->ppscap |= PPS_OFFSETCLEAR;
657 pps_capture(struct pps_state *pps)
659 struct timehands *th;
661 KASSERT(pps != NULL, ("NULL pps pointer in pps_capture"));
663 pps->capgen = th->th_generation;
665 pps->capcount = th->th_counter->tc_get_timecount(th->th_counter);
666 if (pps->capgen != th->th_generation)
671 pps_event(struct pps_state *pps, int event)
674 struct timespec ts, *tsp, *osp;
675 u_int tcount, *pcount;
679 KASSERT(pps != NULL, ("NULL pps pointer in pps_event"));
680 /* If the timecounter was wound up underneath us, bail out. */
681 if (pps->capgen == 0 || pps->capgen != pps->capth->th_generation)
684 /* Things would be easier with arrays. */
685 if (event == PPS_CAPTUREASSERT) {
686 tsp = &pps->ppsinfo.assert_timestamp;
687 osp = &pps->ppsparam.assert_offset;
688 foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
689 fhard = pps->kcmode & PPS_CAPTUREASSERT;
690 pcount = &pps->ppscount[0];
691 pseq = &pps->ppsinfo.assert_sequence;
693 tsp = &pps->ppsinfo.clear_timestamp;
694 osp = &pps->ppsparam.clear_offset;
695 foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
696 fhard = pps->kcmode & PPS_CAPTURECLEAR;
697 pcount = &pps->ppscount[1];
698 pseq = &pps->ppsinfo.clear_sequence;
702 * If the timecounter changed, we cannot compare the count values, so
703 * we have to drop the rest of the PPS-stuff until the next event.
705 if (pps->ppstc != pps->capth->th_counter) {
706 pps->ppstc = pps->capth->th_counter;
707 *pcount = pps->capcount;
708 pps->ppscount[2] = pps->capcount;
712 /* Convert the count to a timespec. */
713 tcount = pps->capcount - pps->capth->th_offset_count;
714 tcount &= pps->capth->th_counter->tc_counter_mask;
715 bt = pps->capth->th_offset;
716 bintime_addx(&bt, pps->capth->th_scale * tcount);
717 bintime_add(&bt, &boottimebin);
718 bintime2timespec(&bt, &ts);
720 /* If the timecounter was wound up underneath us, bail out. */
721 if (pps->capgen != pps->capth->th_generation)
724 *pcount = pps->capcount;
729 timespecadd(tsp, osp);
730 if (tsp->tv_nsec < 0) {
731 tsp->tv_nsec += 1000000000;
740 * Feed the NTP PLL/FLL.
741 * The FLL wants to know how many (hardware) nanoseconds
742 * elapsed since the previous event.
744 tcount = pps->capcount - pps->ppscount[2];
745 pps->ppscount[2] = pps->capcount;
746 tcount &= pps->capth->th_counter->tc_counter_mask;
747 scale = (u_int64_t)1 << 63;
748 scale /= pps->capth->th_counter->tc_frequency;
752 bintime_addx(&bt, scale * tcount);
753 bintime2timespec(&bt, &ts);
754 hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec);
760 * Timecounters need to be updated every so often to prevent the hardware
761 * counter from overflowing. Updating also recalculates the cached values
762 * used by the get*() family of functions, so their precision depends on
763 * the update frequency.
767 SYSCTL_INT(_kern_timecounter, OID_AUTO, tick, CTLFLAG_RD, &tc_tick, 0, "");
773 static time_t last_calib;
775 if (++count < tc_tick)
779 if (time_uptime != last_calib && !(time_uptime & 0xf)) {
780 cpu_tick_calibrate(0);
781 last_calib = time_uptime;
786 inittimecounter(void *dummy)
791 * Set the initial timeout to
792 * max(1, <approx. number of hardclock ticks in a millisecond>).
793 * People should probably not use the sysctl to set the timeout
794 * to smaller than its inital value, since that value is the
795 * smallest reasonable one. If they want better timestamps they
796 * should use the non-"get"* functions.
799 tc_tick = (hz + 500) / 1000;
802 p = (tc_tick * 1000000) / hz;
803 printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000);
805 /* warm up new timecounter (again) and get rolling. */
806 (void)timecounter->tc_get_timecount(timecounter);
807 (void)timecounter->tc_get_timecount(timecounter);
810 SYSINIT(timecounter, SI_SUB_CLOCKS, SI_ORDER_SECOND, inittimecounter, NULL);
812 /* Cpu tick handling -------------------------------------------------*/
814 static int cpu_tick_variable;
815 static uint64_t cpu_tick_frequency;
820 static uint64_t base;
821 static unsigned last;
823 struct timecounter *tc;
825 tc = timehands->th_counter;
826 u = tc->tc_get_timecount(tc) & tc->tc_counter_mask;
828 base += (uint64_t)tc->tc_counter_mask + 1;
834 * This function gets called every 16 seconds on only one designated
835 * CPU in the system from hardclock() via tc_ticktock().
837 * Whenever the real time clock is stepped we get called with reset=1
838 * to make sure we handle suspend/resume and similar events correctly.
842 cpu_tick_calibrate(int reset)
844 static uint64_t c_last;
845 uint64_t c_this, c_delta;
846 static struct bintime t_last;
847 struct bintime t_this, t_delta;
851 /* The clock was stepped, abort & reset */
856 /* we don't calibrate fixed rate cputicks */
857 if (!cpu_tick_variable)
860 getbinuptime(&t_this);
861 c_this = cpu_ticks();
862 if (t_last.sec != 0) {
863 c_delta = c_this - c_last;
865 bintime_sub(&t_delta, &t_last);
867 * Validate that 16 +/- 1/256 seconds passed.
868 * After division by 16 this gives us a precision of
869 * roughly 250PPM which is sufficient
871 if (t_delta.sec > 16 || (
872 t_delta.sec == 16 && t_delta.frac >= (0x01LL << 56))) {
875 printf("t_delta %ju.%016jx too long\n",
876 (uintmax_t)t_delta.sec,
877 (uintmax_t)t_delta.frac);
878 } else if (t_delta.sec < 15 ||
879 (t_delta.sec == 15 && t_delta.frac <= (0xffLL << 56))) {
882 printf("t_delta %ju.%016jx too short\n",
883 (uintmax_t)t_delta.sec,
884 (uintmax_t)t_delta.frac);
889 * 2^(64-20) / 16[s] =
891 * 17.592.186.044.416 / 16 =
892 * 1.099.511.627.776 [Hz]
894 divi = t_delta.sec << 20;
895 divi |= t_delta.frac >> (64 - 20);
898 if (c_delta > cpu_tick_frequency) {
899 if (0 && bootverbose)
900 printf("cpu_tick increased to %ju Hz\n",
902 cpu_tick_frequency = c_delta;
911 set_cputicker(cpu_tick_f *func, uint64_t freq, unsigned var)
915 cpu_ticks = tc_cpu_ticks;
917 cpu_tick_frequency = freq;
918 cpu_tick_variable = var;
927 if (cpu_ticks == tc_cpu_ticks)
928 return (tc_getfrequency());
929 return (cpu_tick_frequency);
933 * We need to be slightly careful converting cputicks to microseconds.
934 * There is plenty of margin in 64 bits of microseconds (half a million
935 * years) and in 64 bits at 4 GHz (146 years), but if we do a multiply
936 * before divide conversion (to retain precision) we find that the
937 * margin shrinks to 1.5 hours (one millionth of 146y).
938 * With a three prong approach we never lose significant bits, no
939 * matter what the cputick rate and length of timeinterval is.
943 cputick2usec(uint64_t tick)
946 if (tick > 18446744073709551LL) /* floor(2^64 / 1000) */
947 return (tick / (cpu_tickrate() / 1000000LL));
948 else if (tick > 18446744073709LL) /* floor(2^64 / 1000000) */
949 return ((tick * 1000LL) / (cpu_tickrate() / 1000LL));
951 return ((tick * 1000000LL) / cpu_tickrate());
954 cpu_tick_f *cpu_ticks = tc_cpu_ticks;