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 int tc_min_ticktock_freq = 1;
92 time_t time_second = 1;
93 time_t time_uptime = 1;
95 struct bintime boottimebin;
96 struct timeval boottime;
97 static int sysctl_kern_boottime(SYSCTL_HANDLER_ARGS);
98 SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime, CTLTYPE_STRUCT|CTLFLAG_RD,
99 NULL, 0, sysctl_kern_boottime, "S,timeval", "System boottime");
101 SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
102 SYSCTL_NODE(_kern_timecounter, OID_AUTO, tc, CTLFLAG_RW, 0, "");
104 static int timestepwarnings;
105 SYSCTL_INT(_kern_timecounter, OID_AUTO, stepwarnings, CTLFLAG_RW,
106 ×tepwarnings, 0, "Log time steps");
108 static void tc_windup(void);
109 static void cpu_tick_calibrate(int);
112 sysctl_kern_boottime(SYSCTL_HANDLER_ARGS)
117 if (req->flags & SCTL_MASK32) {
118 tv[0] = boottime.tv_sec;
119 tv[1] = boottime.tv_usec;
120 return SYSCTL_OUT(req, tv, sizeof(tv));
123 return SYSCTL_OUT(req, &boottime, sizeof(boottime));
127 sysctl_kern_timecounter_get(SYSCTL_HANDLER_ARGS)
130 struct timecounter *tc = arg1;
132 ncount = tc->tc_get_timecount(tc);
133 return sysctl_handle_int(oidp, &ncount, 0, req);
137 sysctl_kern_timecounter_freq(SYSCTL_HANDLER_ARGS)
140 struct timecounter *tc = arg1;
142 freq = tc->tc_frequency;
143 return sysctl_handle_64(oidp, &freq, 0, req);
147 * Return the difference between the timehands' counter value now and what
148 * was when we copied it to the timehands' offset_count.
150 static __inline u_int
151 tc_delta(struct timehands *th)
153 struct timecounter *tc;
156 return ((tc->tc_get_timecount(tc) - th->th_offset_count) &
157 tc->tc_counter_mask);
161 * Functions for reading the time. We have to loop until we are sure that
162 * the timehands that we operated on was not updated under our feet. See
163 * the comment in <sys/time.h> for a description of these 12 functions.
167 binuptime(struct bintime *bt)
169 struct timehands *th;
174 gen = th->th_generation;
176 bintime_addx(bt, th->th_scale * tc_delta(th));
177 } while (gen == 0 || gen != th->th_generation);
181 nanouptime(struct timespec *tsp)
186 bintime2timespec(&bt, tsp);
190 microuptime(struct timeval *tvp)
195 bintime2timeval(&bt, tvp);
199 bintime(struct bintime *bt)
203 bintime_add(bt, &boottimebin);
207 nanotime(struct timespec *tsp)
212 bintime2timespec(&bt, tsp);
216 microtime(struct timeval *tvp)
221 bintime2timeval(&bt, tvp);
225 getbinuptime(struct bintime *bt)
227 struct timehands *th;
232 gen = th->th_generation;
234 } while (gen == 0 || gen != th->th_generation);
238 getnanouptime(struct timespec *tsp)
240 struct timehands *th;
245 gen = th->th_generation;
246 bintime2timespec(&th->th_offset, tsp);
247 } while (gen == 0 || gen != th->th_generation);
251 getmicrouptime(struct timeval *tvp)
253 struct timehands *th;
258 gen = th->th_generation;
259 bintime2timeval(&th->th_offset, tvp);
260 } while (gen == 0 || gen != th->th_generation);
264 getbintime(struct bintime *bt)
266 struct timehands *th;
271 gen = th->th_generation;
273 } while (gen == 0 || gen != th->th_generation);
274 bintime_add(bt, &boottimebin);
278 getnanotime(struct timespec *tsp)
280 struct timehands *th;
285 gen = th->th_generation;
286 *tsp = th->th_nanotime;
287 } while (gen == 0 || gen != th->th_generation);
291 getmicrotime(struct timeval *tvp)
293 struct timehands *th;
298 gen = th->th_generation;
299 *tvp = th->th_microtime;
300 } while (gen == 0 || gen != th->th_generation);
304 * Initialize a new timecounter and possibly use it.
307 tc_init(struct timecounter *tc)
310 struct sysctl_oid *tc_root;
312 u = tc->tc_frequency / tc->tc_counter_mask;
313 /* XXX: We need some margin here, 10% is a guess */
316 if (u > hz && tc->tc_quality >= 0) {
317 tc->tc_quality = -2000;
319 printf("Timecounter \"%s\" frequency %ju Hz",
320 tc->tc_name, (uintmax_t)tc->tc_frequency);
321 printf(" -- Insufficient hz, needs at least %u\n", u);
323 } else if (tc->tc_quality >= 0 || bootverbose) {
324 printf("Timecounter \"%s\" frequency %ju Hz quality %d\n",
325 tc->tc_name, (uintmax_t)tc->tc_frequency,
329 tc->tc_next = timecounters;
332 * Set up sysctl tree for this counter.
334 tc_root = SYSCTL_ADD_NODE(NULL,
335 SYSCTL_STATIC_CHILDREN(_kern_timecounter_tc), OID_AUTO, tc->tc_name,
336 CTLFLAG_RW, 0, "timecounter description");
337 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
338 "mask", CTLFLAG_RD, &(tc->tc_counter_mask), 0,
339 "mask for implemented bits");
340 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
341 "counter", CTLTYPE_UINT | CTLFLAG_RD, tc, sizeof(*tc),
342 sysctl_kern_timecounter_get, "IU", "current timecounter value");
343 SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
344 "frequency", CTLTYPE_U64 | CTLFLAG_RD, tc, sizeof(*tc),
345 sysctl_kern_timecounter_freq, "QU", "timecounter frequency");
346 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(tc_root), OID_AUTO,
347 "quality", CTLFLAG_RD, &(tc->tc_quality), 0,
348 "goodness of time counter");
350 * Never automatically use a timecounter with negative quality.
351 * Even though we run on the dummy counter, switching here may be
352 * worse since this timecounter may not be monotonous.
354 if (tc->tc_quality < 0)
356 if (tc->tc_quality < timecounter->tc_quality)
358 if (tc->tc_quality == timecounter->tc_quality &&
359 tc->tc_frequency < timecounter->tc_frequency)
361 (void)tc->tc_get_timecount(tc);
362 (void)tc->tc_get_timecount(tc);
366 /* Report the frequency of the current timecounter. */
368 tc_getfrequency(void)
371 return (timehands->th_counter->tc_frequency);
375 * Step our concept of UTC. This is done by modifying our estimate of
380 tc_setclock(struct timespec *ts)
382 struct timespec tbef, taft;
383 struct bintime bt, bt2;
385 cpu_tick_calibrate(1);
387 timespec2bintime(ts, &bt);
389 bintime_sub(&bt, &bt2);
390 bintime_add(&bt2, &boottimebin);
392 bintime2timeval(&bt, &boottime);
394 /* XXX fiddle all the little crinkly bits around the fiords... */
397 if (timestepwarnings) {
399 "Time stepped from %jd.%09ld to %jd.%09ld (%jd.%09ld)\n",
400 (intmax_t)tbef.tv_sec, tbef.tv_nsec,
401 (intmax_t)taft.tv_sec, taft.tv_nsec,
402 (intmax_t)ts->tv_sec, ts->tv_nsec);
404 cpu_tick_calibrate(1);
408 * Initialize the next struct timehands in the ring and make
409 * it the active timehands. Along the way we might switch to a different
410 * timecounter and/or do seconds processing in NTP. Slightly magic.
416 struct timehands *th, *tho;
418 u_int delta, ncount, ogen;
423 * Make the next timehands a copy of the current one, but do not
424 * overwrite the generation or next pointer. While we update
425 * the contents, the generation must be zero.
429 ogen = th->th_generation;
430 th->th_generation = 0;
431 bcopy(tho, th, offsetof(struct timehands, th_generation));
434 * Capture a timecounter delta on the current timecounter and if
435 * changing timecounters, a counter value from the new timecounter.
436 * Update the offset fields accordingly.
438 delta = tc_delta(th);
439 if (th->th_counter != timecounter)
440 ncount = timecounter->tc_get_timecount(timecounter);
443 th->th_offset_count += delta;
444 th->th_offset_count &= th->th_counter->tc_counter_mask;
445 while (delta > th->th_counter->tc_frequency) {
446 /* Eat complete unadjusted seconds. */
447 delta -= th->th_counter->tc_frequency;
450 if ((delta > th->th_counter->tc_frequency / 2) &&
451 (th->th_scale * delta < ((uint64_t)1 << 63))) {
452 /* The product th_scale * delta just barely overflows. */
455 bintime_addx(&th->th_offset, th->th_scale * delta);
458 * Hardware latching timecounters may not generate interrupts on
459 * PPS events, so instead we poll them. There is a finite risk that
460 * the hardware might capture a count which is later than the one we
461 * got above, and therefore possibly in the next NTP second which might
462 * have a different rate than the current NTP second. It doesn't
463 * matter in practice.
465 if (tho->th_counter->tc_poll_pps)
466 tho->th_counter->tc_poll_pps(tho->th_counter);
469 * Deal with NTP second processing. The for loop normally
470 * iterates at most once, but in extreme situations it might
471 * keep NTP sane if timeouts are not run for several seconds.
472 * At boot, the time step can be large when the TOD hardware
473 * has been read, so on really large steps, we call
474 * ntp_update_second only twice. We need to call it twice in
475 * case we missed a leap second.
478 bintime_add(&bt, &boottimebin);
479 i = bt.sec - tho->th_microtime.tv_sec;
484 ntp_update_second(&th->th_adjustment, &bt.sec);
486 boottimebin.sec += bt.sec - t;
488 /* Update the UTC timestamps used by the get*() functions. */
489 /* XXX shouldn't do this here. Should force non-`get' versions. */
490 bintime2timeval(&bt, &th->th_microtime);
491 bintime2timespec(&bt, &th->th_nanotime);
493 /* Now is a good time to change timecounters. */
494 if (th->th_counter != timecounter) {
495 th->th_counter = timecounter;
496 th->th_offset_count = ncount;
497 tc_min_ticktock_freq = max(1, timecounter->tc_frequency /
498 (((uint64_t)timecounter->tc_counter_mask + 1) / 3));
502 * Recalculate the scaling factor. We want the number of 1/2^64
503 * fractions of a second per period of the hardware counter, taking
504 * into account the th_adjustment factor which the NTP PLL/adjtime(2)
505 * processing provides us with.
507 * The th_adjustment is nanoseconds per second with 32 bit binary
508 * fraction and we want 64 bit binary fraction of second:
510 * x = a * 2^32 / 10^9 = a * 4.294967296
512 * The range of th_adjustment is +/- 5000PPM so inside a 64bit int
513 * we can only multiply by about 850 without overflowing, that
514 * leaves no suitably precise fractions for multiply before divide.
516 * Divide before multiply with a fraction of 2199/512 results in a
517 * systematic undercompensation of 10PPM of th_adjustment. On a
518 * 5000PPM adjustment this is a 0.05PPM error. This is acceptable.
520 * We happily sacrifice the lowest of the 64 bits of our result
521 * to the goddess of code clarity.
524 scale = (uint64_t)1 << 63;
525 scale += (th->th_adjustment / 1024) * 2199;
526 scale /= th->th_counter->tc_frequency;
527 th->th_scale = scale * 2;
530 * Now that the struct timehands is again consistent, set the new
531 * generation number, making sure to not make it zero.
535 th->th_generation = ogen;
537 /* Go live with the new struct timehands. */
538 time_second = th->th_microtime.tv_sec;
539 time_uptime = th->th_offset.sec;
543 /* Report or change the active timecounter hardware. */
545 sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
548 struct timecounter *newtc, *tc;
552 strlcpy(newname, tc->tc_name, sizeof(newname));
554 error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
555 if (error != 0 || req->newptr == NULL ||
556 strcmp(newname, tc->tc_name) == 0)
558 for (newtc = timecounters; newtc != NULL; newtc = newtc->tc_next) {
559 if (strcmp(newname, newtc->tc_name) != 0)
562 /* Warm up new timecounter. */
563 (void)newtc->tc_get_timecount(newtc);
564 (void)newtc->tc_get_timecount(newtc);
572 SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW,
573 0, 0, sysctl_kern_timecounter_hardware, "A",
574 "Timecounter hardware selected");
577 /* Report or change the active timecounter hardware. */
579 sysctl_kern_timecounter_choice(SYSCTL_HANDLER_ARGS)
582 struct timecounter *tc;
587 for (tc = timecounters; error == 0 && tc != NULL; tc = tc->tc_next) {
588 sprintf(buf, "%s%s(%d)",
589 spc, tc->tc_name, tc->tc_quality);
590 error = SYSCTL_OUT(req, buf, strlen(buf));
596 SYSCTL_PROC(_kern_timecounter, OID_AUTO, choice, CTLTYPE_STRING | CTLFLAG_RD,
597 0, 0, sysctl_kern_timecounter_choice, "A", "Timecounter hardware detected");
600 * RFC 2783 PPS-API implementation.
604 pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
607 struct pps_fetch_args *fapi;
609 struct pps_kcbind_args *kapi;
612 KASSERT(pps != NULL, ("NULL pps pointer in pps_ioctl"));
616 case PPS_IOC_DESTROY:
618 case PPS_IOC_SETPARAMS:
619 app = (pps_params_t *)data;
620 if (app->mode & ~pps->ppscap)
622 pps->ppsparam = *app;
624 case PPS_IOC_GETPARAMS:
625 app = (pps_params_t *)data;
626 *app = pps->ppsparam;
627 app->api_version = PPS_API_VERS_1;
630 *(int*)data = pps->ppscap;
633 fapi = (struct pps_fetch_args *)data;
634 if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
636 if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec)
638 pps->ppsinfo.current_mode = pps->ppsparam.mode;
639 fapi->pps_info_buf = pps->ppsinfo;
643 kapi = (struct pps_kcbind_args *)data;
644 /* XXX Only root should be able to do this */
645 if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
647 if (kapi->kernel_consumer != PPS_KC_HARDPPS)
649 if (kapi->edge & ~pps->ppscap)
651 pps->kcmode = kapi->edge;
662 pps_init(struct pps_state *pps)
664 pps->ppscap |= PPS_TSFMT_TSPEC;
665 if (pps->ppscap & PPS_CAPTUREASSERT)
666 pps->ppscap |= PPS_OFFSETASSERT;
667 if (pps->ppscap & PPS_CAPTURECLEAR)
668 pps->ppscap |= PPS_OFFSETCLEAR;
672 pps_capture(struct pps_state *pps)
674 struct timehands *th;
676 KASSERT(pps != NULL, ("NULL pps pointer in pps_capture"));
678 pps->capgen = th->th_generation;
680 pps->capcount = th->th_counter->tc_get_timecount(th->th_counter);
681 if (pps->capgen != th->th_generation)
686 pps_event(struct pps_state *pps, int event)
689 struct timespec ts, *tsp, *osp;
690 u_int tcount, *pcount;
694 KASSERT(pps != NULL, ("NULL pps pointer in pps_event"));
695 /* If the timecounter was wound up underneath us, bail out. */
696 if (pps->capgen == 0 || pps->capgen != pps->capth->th_generation)
699 /* Things would be easier with arrays. */
700 if (event == PPS_CAPTUREASSERT) {
701 tsp = &pps->ppsinfo.assert_timestamp;
702 osp = &pps->ppsparam.assert_offset;
703 foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
704 fhard = pps->kcmode & PPS_CAPTUREASSERT;
705 pcount = &pps->ppscount[0];
706 pseq = &pps->ppsinfo.assert_sequence;
708 tsp = &pps->ppsinfo.clear_timestamp;
709 osp = &pps->ppsparam.clear_offset;
710 foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
711 fhard = pps->kcmode & PPS_CAPTURECLEAR;
712 pcount = &pps->ppscount[1];
713 pseq = &pps->ppsinfo.clear_sequence;
717 * If the timecounter changed, we cannot compare the count values, so
718 * we have to drop the rest of the PPS-stuff until the next event.
720 if (pps->ppstc != pps->capth->th_counter) {
721 pps->ppstc = pps->capth->th_counter;
722 *pcount = pps->capcount;
723 pps->ppscount[2] = pps->capcount;
727 /* Convert the count to a timespec. */
728 tcount = pps->capcount - pps->capth->th_offset_count;
729 tcount &= pps->capth->th_counter->tc_counter_mask;
730 bt = pps->capth->th_offset;
731 bintime_addx(&bt, pps->capth->th_scale * tcount);
732 bintime_add(&bt, &boottimebin);
733 bintime2timespec(&bt, &ts);
735 /* If the timecounter was wound up underneath us, bail out. */
736 if (pps->capgen != pps->capth->th_generation)
739 *pcount = pps->capcount;
744 timespecadd(tsp, osp);
745 if (tsp->tv_nsec < 0) {
746 tsp->tv_nsec += 1000000000;
755 * Feed the NTP PLL/FLL.
756 * The FLL wants to know how many (hardware) nanoseconds
757 * elapsed since the previous event.
759 tcount = pps->capcount - pps->ppscount[2];
760 pps->ppscount[2] = pps->capcount;
761 tcount &= pps->capth->th_counter->tc_counter_mask;
762 scale = (uint64_t)1 << 63;
763 scale /= pps->capth->th_counter->tc_frequency;
767 bintime_addx(&bt, scale * tcount);
768 bintime2timespec(&bt, &ts);
769 hardpps(tsp, ts.tv_nsec + 1000000000 * ts.tv_sec);
775 * Timecounters need to be updated every so often to prevent the hardware
776 * counter from overflowing. Updating also recalculates the cached values
777 * used by the get*() family of functions, so their precision depends on
778 * the update frequency.
782 SYSCTL_INT(_kern_timecounter, OID_AUTO, tick, CTLFLAG_RD, &tc_tick, 0,
783 "Approximate number of hardclock ticks in a millisecond");
798 inittimecounter(void *dummy)
803 * Set the initial timeout to
804 * max(1, <approx. number of hardclock ticks in a millisecond>).
805 * People should probably not use the sysctl to set the timeout
806 * to smaller than its inital value, since that value is the
807 * smallest reasonable one. If they want better timestamps they
808 * should use the non-"get"* functions.
811 tc_tick = (hz + 500) / 1000;
814 p = (tc_tick * 1000000) / hz;
815 printf("Timecounters tick every %d.%03u msec\n", p / 1000, p % 1000);
817 /* warm up new timecounter (again) and get rolling. */
818 (void)timecounter->tc_get_timecount(timecounter);
819 (void)timecounter->tc_get_timecount(timecounter);
823 SYSINIT(timecounter, SI_SUB_CLOCKS, SI_ORDER_SECOND, inittimecounter, NULL);
825 /* Cpu tick handling -------------------------------------------------*/
827 static int cpu_tick_variable;
828 static uint64_t cpu_tick_frequency;
833 static uint64_t base;
834 static unsigned last;
836 struct timecounter *tc;
838 tc = timehands->th_counter;
839 u = tc->tc_get_timecount(tc) & tc->tc_counter_mask;
841 base += (uint64_t)tc->tc_counter_mask + 1;
847 cpu_tick_calibration(void)
849 static time_t last_calib;
851 if (time_uptime != last_calib && !(time_uptime & 0xf)) {
852 cpu_tick_calibrate(0);
853 last_calib = time_uptime;
858 * This function gets called every 16 seconds on only one designated
859 * CPU in the system from hardclock() via cpu_tick_calibration()().
861 * Whenever the real time clock is stepped we get called with reset=1
862 * to make sure we handle suspend/resume and similar events correctly.
866 cpu_tick_calibrate(int reset)
868 static uint64_t c_last;
869 uint64_t c_this, c_delta;
870 static struct bintime t_last;
871 struct bintime t_this, t_delta;
875 /* The clock was stepped, abort & reset */
880 /* we don't calibrate fixed rate cputicks */
881 if (!cpu_tick_variable)
884 getbinuptime(&t_this);
885 c_this = cpu_ticks();
886 if (t_last.sec != 0) {
887 c_delta = c_this - c_last;
889 bintime_sub(&t_delta, &t_last);
892 * 2^(64-20) / 16[s] =
894 * 17.592.186.044.416 / 16 =
895 * 1.099.511.627.776 [Hz]
897 divi = t_delta.sec << 20;
898 divi |= t_delta.frac >> (64 - 20);
901 if (c_delta > cpu_tick_frequency) {
902 if (0 && bootverbose)
903 printf("cpu_tick increased to %ju Hz\n",
905 cpu_tick_frequency = c_delta;
913 set_cputicker(cpu_tick_f *func, uint64_t freq, unsigned var)
917 cpu_ticks = tc_cpu_ticks;
919 cpu_tick_frequency = freq;
920 cpu_tick_variable = var;
929 if (cpu_ticks == tc_cpu_ticks)
930 return (tc_getfrequency());
931 return (cpu_tick_frequency);
935 * We need to be slightly careful converting cputicks to microseconds.
936 * There is plenty of margin in 64 bits of microseconds (half a million
937 * years) and in 64 bits at 4 GHz (146 years), but if we do a multiply
938 * before divide conversion (to retain precision) we find that the
939 * margin shrinks to 1.5 hours (one millionth of 146y).
940 * With a three prong approach we never lose significant bits, no
941 * matter what the cputick rate and length of timeinterval is.
945 cputick2usec(uint64_t tick)
948 if (tick > 18446744073709551LL) /* floor(2^64 / 1000) */
949 return (tick / (cpu_tickrate() / 1000000LL));
950 else if (tick > 18446744073709LL) /* floor(2^64 / 1000000) */
951 return ((tick * 1000LL) / (cpu_tickrate() / 1000LL));
953 return ((tick * 1000000LL) / cpu_tickrate());
956 cpu_tick_f *cpu_ticks = tc_cpu_ticks;