2 * Copyright (c) 1997, 1998 Poul-Henning Kamp <phk@FreeBSD.org>
3 * Copyright (c) 1982, 1986, 1991, 1993
4 * The Regents of the University of California. All rights reserved.
5 * (c) UNIX System Laboratories, Inc.
6 * All or some portions of this file are derived from material licensed
7 * to the University of California by American Telephone and Telegraph
8 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
9 * the permission of UNIX System Laboratories, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
40 * $FreeBSD: src/sys/kern/kern_clock.c,v 1.105.2.10 2002/10/17 13:19:40 maxim Exp $
41 * $DragonFly: src/sys/kern/kern_clock.c,v 1.3 2003/06/23 23:36:11 dillon Exp $
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/dkstat.h>
49 #include <sys/callout.h>
50 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/resourcevar.h>
54 #include <sys/signalvar.h>
55 #include <sys/timex.h>
56 #include <sys/timepps.h>
60 #include <vm/vm_map.h>
61 #include <sys/sysctl.h>
63 #include <machine/cpu.h>
64 #include <machine/limits.h>
65 #include <machine/smp.h>
72 extern void init_device_poll(void);
73 extern void hardclock_device_poll(void);
74 #endif /* DEVICE_POLLING */
77 * Number of timecounters used to implement stable storage
80 #define NTIMECOUNTER 5
83 static MALLOC_DEFINE(M_TIMECOUNTER, "timecounter",
84 "Timecounter stable storage");
86 static void initclocks __P((void *dummy));
87 SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL)
89 static void tco_forward __P((int force));
90 static void tco_setscales __P((struct timecounter *tc));
91 static __inline unsigned tco_delta __P((struct timecounter *tc));
93 /* Some of these don't belong here, but it's easiest to concentrate them. */
94 long cp_time[CPUSTATES];
96 SYSCTL_OPAQUE(_kern, OID_AUTO, cp_time, CTLFLAG_RD, &cp_time, sizeof(cp_time),
97 "LU", "CPU time statistics");
106 struct timeval boottime;
107 SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime, CTLFLAG_RD,
108 &boottime, timeval, "System boottime");
111 * Which update policy to use.
112 * 0 - every tick, bad hardware may fail with "calcru negative..."
113 * 1 - more resistent to the above hardware, but less efficient.
115 static int tco_method;
118 * Implement a dummy timecounter which we can use until we get a real one
119 * in the air. This allows the console and other early stuff to use
124 dummy_get_timecount(struct timecounter *tc)
130 static struct timecounter dummy_timecounter = {
138 struct timecounter *timecounter = &dummy_timecounter;
141 * Clock handling routines.
143 * This code is written to operate with two timers that run independently of
146 * The main timer, running hz times per second, is used to trigger interval
147 * timers, timeouts and rescheduling as needed.
149 * The second timer handles kernel and user profiling,
150 * and does resource use estimation. If the second timer is programmable,
151 * it is randomized to avoid aliasing between the two clocks. For example,
152 * the randomization prevents an adversary from always giving up the cpu
153 * just before its quantum expires. Otherwise, it would never accumulate
154 * cpu ticks. The mean frequency of the second timer is stathz.
156 * If no second timer exists, stathz will be zero; in this case we drive
157 * profiling and statistics off the main clock. This WILL NOT be accurate;
158 * do not do it unless absolutely necessary.
160 * The statistics clock may (or may not) be run at a higher rate while
161 * profiling. This profile clock runs at profhz. We require that profhz
162 * be an integral multiple of stathz.
164 * If the statistics clock is running fast, it must be divided by the ratio
165 * profhz/stathz for statistics. (For profiling, every tick counts.)
167 * Time-of-day is maintained using a "timecounter", which may or may
168 * not be related to the hardware generating the above mentioned
174 static int profprocs;
176 static int psdiv, pscnt; /* prof => stat divider */
177 int psratio; /* ratio: prof / stat */
180 * Initialize clock frequencies and start both clocks running.
190 * Set divisors to 1 (normal case) and let the machine-specific
196 #ifdef DEVICE_POLLING
201 * Compute profhz/stathz, and fix profhz if needed.
203 i = stathz ? stathz : hz;
206 psratio = profhz / i;
210 * The real-time timer, interrupting hz times per second.
214 register struct clockframe *frame;
216 register struct proc *p;
220 register struct pstats *pstats;
223 * Run current process's virtual and profile time, as needed.
226 if (CLKF_USERMODE(frame) &&
227 timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
228 itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0)
229 psignal(p, SIGVTALRM);
230 if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
231 itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0)
235 #if defined(SMP) && defined(BETTER_CLOCK)
236 forward_hardclock(pscnt);
240 * If no separate statistics clock is available, run it from here.
248 #ifdef DEVICE_POLLING
249 hardclock_device_poll(); /* this is very short and quick */
250 #endif /* DEVICE_POLLING */
253 * Process callouts at a very low cpu priority, so we don't keep the
254 * relatively high clock interrupt priority any longer than necessary.
256 if (TAILQ_FIRST(&callwheel[ticks & callwheelmask]) != NULL) {
257 if (CLKF_BASEPRI(frame)) {
259 * Save the overhead of a software interrupt;
260 * it will happen as soon as we return, so do it now.
262 (void)splsoftclock();
266 } else if (softticks + 1 == ticks)
271 * Compute number of ticks in the specified amount of time.
277 register unsigned long ticks;
278 register long sec, usec;
281 * If the number of usecs in the whole seconds part of the time
282 * difference fits in a long, then the total number of usecs will
283 * fit in an unsigned long. Compute the total and convert it to
284 * ticks, rounding up and adding 1 to allow for the current tick
285 * to expire. Rounding also depends on unsigned long arithmetic
288 * Otherwise, if the number of ticks in the whole seconds part of
289 * the time difference fits in a long, then convert the parts to
290 * ticks separately and add, using similar rounding methods and
291 * overflow avoidance. This method would work in the previous
292 * case but it is slightly slower and assumes that hz is integral.
294 * Otherwise, round the time difference down to the maximum
295 * representable value.
297 * If ints have 32 bits, then the maximum value for any timeout in
298 * 10ms ticks is 248 days.
312 printf("tvotohz: negative time difference %ld sec %ld usec\n",
316 } else if (sec <= LONG_MAX / 1000000)
317 ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
319 else if (sec <= LONG_MAX / hz)
321 + ((unsigned long)usec + (tick - 1)) / tick + 1;
330 * Start profiling on a process.
332 * Kernel profiling passes proc0 which never exits and hence
333 * keeps the profile clock running constantly.
337 register struct proc *p;
341 if ((p->p_flag & P_PROFIL) == 0) {
342 p->p_flag |= P_PROFIL;
343 if (++profprocs == 1 && stathz != 0) {
345 psdiv = pscnt = psratio;
346 setstatclockrate(profhz);
353 * Stop profiling on a process.
357 register struct proc *p;
361 if (p->p_flag & P_PROFIL) {
362 p->p_flag &= ~P_PROFIL;
363 if (--profprocs == 0 && stathz != 0) {
366 setstatclockrate(stathz);
373 * Statistics clock. Grab profile sample, and if divider reaches 0,
374 * do process and kernel statistics. Most of the statistics are only
375 * used by user-level statistics programs. The main exceptions are
376 * p->p_uticks, p->p_sticks, p->p_iticks, and p->p_estcpu.
380 register struct clockframe *frame;
383 register struct gmonparam *g;
387 struct pstats *pstats;
397 * How big was our timeslice relative to the last time
400 stv = &mycpu->gd_stattv;
401 if (stv->tv_sec == 0) {
404 bump = tv.tv_usec - stv->tv_usec +
405 (tv.tv_sec - stv->tv_sec) * 1000000;
416 if (CLKF_USERMODE(frame)) {
418 * Came from userland, handle user time and deal with
421 if (p && (p->p_flag & P_PROFIL))
422 addupc_intr(p, CLKF_PC(frame), 1);
423 #if defined(SMP) && defined(BETTER_CLOCK)
425 forward_statclock(pscnt);
427 td->td_uticks += bump;
432 * Charge the time as appropriate
434 if (p && p->p_nice > NZERO)
441 * Kernel statistics are just like addupc_intr, only easier.
444 if (g->state == GMON_PROF_ON) {
445 i = CLKF_PC(frame) - g->lowpc;
446 if (i < g->textsize) {
447 i /= HISTFRACTION * sizeof(*g->kcount);
452 #if defined(SMP) && defined(BETTER_CLOCK)
454 forward_statclock(pscnt);
457 * Came from kernel mode, so we were:
458 * - handling an interrupt,
459 * - doing syscall or trap work on behalf of the current
461 * - spinning in the idle loop.
462 * Whichever it is, charge the time as appropriate.
463 * Note that we charge interrupts to the current process,
464 * regardless of whether they are ``for'' that process,
465 * so that we know how much of its real time was spent
466 * in ``non-process'' (i.e., interrupt) work.
468 if (CLKF_INTR(frame))
469 td->td_iticks += bump;
471 td->td_sticks += bump;
476 if (CLKF_INTR(frame)) {
479 if (td == &mycpu->gd_idlethread)
490 /* Update resource usage integrals and maximums. */
491 if ((pstats = p->p_stats) != NULL &&
492 (ru = &pstats->p_ru) != NULL &&
493 (vm = p->p_vmspace) != NULL) {
494 ru->ru_ixrss += pgtok(vm->vm_tsize);
495 ru->ru_idrss += pgtok(vm->vm_dsize);
496 ru->ru_isrss += pgtok(vm->vm_ssize);
497 rss = pgtok(vmspace_resident_count(vm));
498 if (ru->ru_maxrss < rss)
505 * Return information about system clocks.
508 sysctl_kern_clockrate(SYSCTL_HANDLER_ARGS)
510 struct clockinfo clkinfo;
512 * Construct clockinfo structure.
516 clkinfo.tickadj = tickadj;
517 clkinfo.profhz = profhz;
518 clkinfo.stathz = stathz ? stathz : hz;
519 return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req));
522 SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, CTLTYPE_STRUCT|CTLFLAG_RD,
523 0, 0, sysctl_kern_clockrate, "S,clockinfo","");
525 static __inline unsigned
526 tco_delta(struct timecounter *tc)
529 return ((tc->tc_get_timecount(tc) - tc->tc_offset_count) &
530 tc->tc_counter_mask);
534 * We have eight functions for looking at the clock, four for
535 * microseconds and four for nanoseconds. For each there is fast
536 * but less precise version "get{nano|micro}[up]time" which will
537 * return a time which is up to 1/HZ previous to the call, whereas
538 * the raw version "{nano|micro}[up]time" will return a timestamp
539 * which is as precise as possible. The "up" variants return the
540 * time relative to system boot, these are well suited for time
541 * interval measurements.
545 getmicrotime(struct timeval *tvp)
547 struct timecounter *tc;
551 *tvp = tc->tc_microtime;
558 getnanotime(struct timespec *tsp)
560 struct timecounter *tc;
564 *tsp = tc->tc_nanotime;
571 microtime(struct timeval *tv)
573 struct timecounter *tc;
576 tv->tv_sec = tc->tc_offset_sec;
577 tv->tv_usec = tc->tc_offset_micro;
578 tv->tv_usec += ((u_int64_t)tco_delta(tc) * tc->tc_scale_micro) >> 32;
579 tv->tv_usec += boottime.tv_usec;
580 tv->tv_sec += boottime.tv_sec;
581 while (tv->tv_usec < 0) {
582 tv->tv_usec += 1000000;
586 while (tv->tv_usec >= 1000000) {
587 tv->tv_usec -= 1000000;
593 nanotime(struct timespec *ts)
597 struct timecounter *tc;
600 ts->tv_sec = tc->tc_offset_sec;
601 count = tco_delta(tc);
602 delta = tc->tc_offset_nano;
603 delta += ((u_int64_t)count * tc->tc_scale_nano_f);
605 delta += ((u_int64_t)count * tc->tc_scale_nano_i);
606 delta += boottime.tv_usec * 1000;
607 ts->tv_sec += boottime.tv_sec;
613 while (delta >= 1000000000) {
621 getmicrouptime(struct timeval *tvp)
623 struct timecounter *tc;
627 tvp->tv_sec = tc->tc_offset_sec;
628 tvp->tv_usec = tc->tc_offset_micro;
635 getnanouptime(struct timespec *tsp)
637 struct timecounter *tc;
641 tsp->tv_sec = tc->tc_offset_sec;
642 tsp->tv_nsec = tc->tc_offset_nano >> 32;
649 microuptime(struct timeval *tv)
651 struct timecounter *tc;
654 tv->tv_sec = tc->tc_offset_sec;
655 tv->tv_usec = tc->tc_offset_micro;
656 tv->tv_usec += ((u_int64_t)tco_delta(tc) * tc->tc_scale_micro) >> 32;
657 while (tv->tv_usec < 0) {
658 tv->tv_usec += 1000000;
662 while (tv->tv_usec >= 1000000) {
663 tv->tv_usec -= 1000000;
669 nanouptime(struct timespec *ts)
673 struct timecounter *tc;
676 ts->tv_sec = tc->tc_offset_sec;
677 count = tco_delta(tc);
678 delta = tc->tc_offset_nano;
679 delta += ((u_int64_t)count * tc->tc_scale_nano_f);
681 delta += ((u_int64_t)count * tc->tc_scale_nano_i);
687 while (delta >= 1000000000) {
695 tco_setscales(struct timecounter *tc)
699 scale = 1000000000LL << 32;
700 scale += tc->tc_adjustment;
701 scale /= tc->tc_tweak->tc_frequency;
702 tc->tc_scale_micro = scale / 1000;
703 tc->tc_scale_nano_f = scale & 0xffffffff;
704 tc->tc_scale_nano_i = scale >> 32;
708 update_timecounter(struct timecounter *tc)
714 init_timecounter(struct timecounter *tc)
717 struct timecounter *t1, *t2, *t3;
721 u = tc->tc_frequency / tc->tc_counter_mask;
723 printf("Timecounter \"%s\" frequency %lu Hz"
724 " -- Insufficient hz, needs at least %u\n",
725 tc->tc_name, (u_long) tc->tc_frequency, u);
729 tc->tc_adjustment = 0;
732 tc->tc_offset_count = tc->tc_get_timecount(tc);
733 if (timecounter == &dummy_timecounter)
736 tc->tc_avail = timecounter->tc_tweak->tc_avail;
737 timecounter->tc_tweak->tc_avail = tc;
739 MALLOC(t1, struct timecounter *, sizeof *t1, M_TIMECOUNTER, M_WAITOK);
743 for (i = 1; i < NTIMECOUNTER; i++) {
744 MALLOC(t3, struct timecounter *, sizeof *t3,
745 M_TIMECOUNTER, M_WAITOK);
753 printf("Timecounter \"%s\" frequency %lu Hz\n",
754 tc->tc_name, (u_long)tc->tc_frequency);
756 /* XXX: For now always start using the counter. */
757 tc->tc_offset_count = tc->tc_get_timecount(tc);
759 tc->tc_offset_nano = (u_int64_t)ts1.tv_nsec << 32;
760 tc->tc_offset_micro = ts1.tv_nsec / 1000;
761 tc->tc_offset_sec = ts1.tv_sec;
766 set_timecounter(struct timespec *ts)
771 boottime.tv_sec = ts->tv_sec - ts2.tv_sec;
772 boottime.tv_usec = (ts->tv_nsec - ts2.tv_nsec) / 1000;
773 if (boottime.tv_usec < 0) {
774 boottime.tv_usec += 1000000;
777 /* fiddle all the little crinkly bits around the fiords... */
782 switch_timecounter(struct timecounter *newtc)
785 struct timecounter *tc;
790 if (newtc->tc_tweak == tc->tc_tweak) {
794 newtc = newtc->tc_tweak->tc_other;
796 newtc->tc_offset_sec = ts.tv_sec;
797 newtc->tc_offset_nano = (u_int64_t)ts.tv_nsec << 32;
798 newtc->tc_offset_micro = ts.tv_nsec / 1000;
799 newtc->tc_offset_count = newtc->tc_get_timecount(newtc);
800 tco_setscales(newtc);
805 static struct timecounter *
806 sync_other_counter(void)
808 struct timecounter *tc, *tcn, *tco;
816 delta = tco_delta(tc);
817 tc->tc_offset_count += delta;
818 tc->tc_offset_count &= tc->tc_counter_mask;
819 tc->tc_offset_nano += (u_int64_t)delta * tc->tc_scale_nano_f;
820 tc->tc_offset_nano += (u_int64_t)delta * tc->tc_scale_nano_i << 32;
825 tco_forward(int force)
827 struct timecounter *tc, *tco;
831 tc = sync_other_counter();
833 * We may be inducing a tiny error here, the tc_poll_pps() may
834 * process a latched count which happens after the tco_delta()
835 * in sync_other_counter(), which would extend the previous
836 * counters parameters into the domain of this new one.
837 * Since the timewindow is very small for this, the error is
838 * going to be only a few weenieseconds (as Dave Mills would
839 * say), so lets just not talk more about it, OK ?
841 if (tco->tc_poll_pps)
842 tco->tc_poll_pps(tco);
843 if (timedelta != 0) {
845 tvt.tv_usec += tickdelta;
846 if (tvt.tv_usec >= 1000000) {
848 tvt.tv_usec -= 1000000;
849 } else if (tvt.tv_usec < 0) {
851 tvt.tv_usec += 1000000;
854 timedelta -= tickdelta;
857 while (tc->tc_offset_nano >= 1000000000ULL << 32) {
858 tc->tc_offset_nano -= 1000000000ULL << 32;
860 ntp_update_second(tc); /* XXX only needed if xntpd runs */
865 if (tco_method && !force)
868 tc->tc_offset_micro = (tc->tc_offset_nano / 1000) >> 32;
870 /* Figure out the wall-clock time */
871 tc->tc_nanotime.tv_sec = tc->tc_offset_sec + boottime.tv_sec;
872 tc->tc_nanotime.tv_nsec =
873 (tc->tc_offset_nano >> 32) + boottime.tv_usec * 1000;
874 tc->tc_microtime.tv_usec = tc->tc_offset_micro + boottime.tv_usec;
875 while (tc->tc_nanotime.tv_nsec >= 1000000000) {
876 tc->tc_nanotime.tv_nsec -= 1000000000;
877 tc->tc_microtime.tv_usec -= 1000000;
878 tc->tc_nanotime.tv_sec++;
880 time_second = tc->tc_microtime.tv_sec = tc->tc_nanotime.tv_sec;
885 SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
887 SYSCTL_INT(_kern_timecounter, OID_AUTO, method, CTLFLAG_RW, &tco_method, 0,
888 "This variable determines the method used for updating timecounters. "
889 "If the default algorithm (0) fails with \"calcru negative...\" messages "
890 "try the alternate algorithm (1) which handles bad hardware better."
895 sysctl_kern_timecounter_hardware(SYSCTL_HANDLER_ARGS)
898 struct timecounter *newtc, *tc;
901 tc = timecounter->tc_tweak;
902 strncpy(newname, tc->tc_name, sizeof(newname));
903 error = sysctl_handle_string(oidp, &newname[0], sizeof(newname), req);
904 if (error == 0 && req->newptr != NULL &&
905 strcmp(newname, tc->tc_name) != 0) {
906 for (newtc = tc->tc_avail; newtc != tc;
907 newtc = newtc->tc_avail) {
908 if (strcmp(newname, newtc->tc_name) == 0) {
909 /* Warm up new timecounter. */
910 (void)newtc->tc_get_timecount(newtc);
912 switch_timecounter(newtc);
921 SYSCTL_PROC(_kern_timecounter, OID_AUTO, hardware, CTLTYPE_STRING | CTLFLAG_RW,
922 0, 0, sysctl_kern_timecounter_hardware, "A", "");
926 pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
929 struct pps_fetch_args *fapi;
931 struct pps_kcbind_args *kapi;
937 case PPS_IOC_DESTROY:
939 case PPS_IOC_SETPARAMS:
940 app = (pps_params_t *)data;
941 if (app->mode & ~pps->ppscap)
943 pps->ppsparam = *app;
945 case PPS_IOC_GETPARAMS:
946 app = (pps_params_t *)data;
947 *app = pps->ppsparam;
948 app->api_version = PPS_API_VERS_1;
951 *(int*)data = pps->ppscap;
954 fapi = (struct pps_fetch_args *)data;
955 if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
957 if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec)
959 pps->ppsinfo.current_mode = pps->ppsparam.mode;
960 fapi->pps_info_buf = pps->ppsinfo;
964 kapi = (struct pps_kcbind_args *)data;
965 /* XXX Only root should be able to do this */
966 if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
968 if (kapi->kernel_consumer != PPS_KC_HARDPPS)
970 if (kapi->edge & ~pps->ppscap)
972 pps->kcmode = kapi->edge;
983 pps_init(struct pps_state *pps)
985 pps->ppscap |= PPS_TSFMT_TSPEC;
986 if (pps->ppscap & PPS_CAPTUREASSERT)
987 pps->ppscap |= PPS_OFFSETASSERT;
988 if (pps->ppscap & PPS_CAPTURECLEAR)
989 pps->ppscap |= PPS_OFFSETCLEAR;
993 pps_event(struct pps_state *pps, struct timecounter *tc, unsigned count, int event)
995 struct timespec ts, *tsp, *osp;
997 unsigned tcount, *pcount;
1001 /* Things would be easier with arrays... */
1002 if (event == PPS_CAPTUREASSERT) {
1003 tsp = &pps->ppsinfo.assert_timestamp;
1004 osp = &pps->ppsparam.assert_offset;
1005 foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
1006 fhard = pps->kcmode & PPS_CAPTUREASSERT;
1007 pcount = &pps->ppscount[0];
1008 pseq = &pps->ppsinfo.assert_sequence;
1010 tsp = &pps->ppsinfo.clear_timestamp;
1011 osp = &pps->ppsparam.clear_offset;
1012 foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
1013 fhard = pps->kcmode & PPS_CAPTURECLEAR;
1014 pcount = &pps->ppscount[1];
1015 pseq = &pps->ppsinfo.clear_sequence;
1018 /* The timecounter changed: bail */
1020 pps->ppstc->tc_name != tc->tc_name ||
1021 tc->tc_name != timecounter->tc_name) {
1027 /* Nothing really happened */
1028 if (*pcount == count)
1033 /* Convert the count to timespec */
1034 ts.tv_sec = tc->tc_offset_sec;
1035 tcount = count - tc->tc_offset_count;
1036 tcount &= tc->tc_counter_mask;
1037 delta = tc->tc_offset_nano;
1038 delta += ((u_int64_t)tcount * tc->tc_scale_nano_f);
1040 delta += ((u_int64_t)tcount * tc->tc_scale_nano_i);
1041 delta += boottime.tv_usec * 1000;
1042 ts.tv_sec += boottime.tv_sec;
1043 while (delta >= 1000000000) {
1044 delta -= 1000000000;
1053 timespecadd(tsp, osp);
1054 if (tsp->tv_nsec < 0) {
1055 tsp->tv_nsec += 1000000000;
1061 /* magic, at its best... */
1062 tcount = count - pps->ppscount[2];
1063 pps->ppscount[2] = count;
1064 tcount &= tc->tc_counter_mask;
1065 delta = ((u_int64_t)tcount * tc->tc_tweak->tc_scale_nano_f);
1067 delta += ((u_int64_t)tcount * tc->tc_tweak->tc_scale_nano_i);
1068 hardpps(tsp, delta);