Fix warnings, use strlcpy instead of strcpy + manual check.
[dragonfly.git] / sys / kern / kern_clock.c
CommitLineData
8c10bfcf
MD
1/*
2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
984263bc
MD
34 * Copyright (c) 1997, 1998 Poul-Henning Kamp <phk@FreeBSD.org>
35 * Copyright (c) 1982, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
37 * (c) UNIX System Laboratories, Inc.
38 * All or some portions of this file are derived from material licensed
39 * to the University of California by American Telephone and Telegraph
40 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
41 * the permission of UNIX System Laboratories, Inc.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
72 * $FreeBSD: src/sys/kern/kern_clock.c,v 1.105.2.10 2002/10/17 13:19:40 maxim Exp $
32040d57 73 * $DragonFly: src/sys/kern/kern_clock.c,v 1.39 2005/04/26 22:35:32 dillon Exp $
984263bc
MD
74 */
75
76#include "opt_ntp.h"
77
78#include <sys/param.h>
79#include <sys/systm.h>
80#include <sys/dkstat.h>
81#include <sys/callout.h>
82#include <sys/kernel.h>
f5d21610 83#include <sys/kinfo.h>
984263bc
MD
84#include <sys/proc.h>
85#include <sys/malloc.h>
86#include <sys/resourcevar.h>
87#include <sys/signalvar.h>
88#include <sys/timex.h>
89#include <sys/timepps.h>
90#include <vm/vm.h>
91#include <sys/lock.h>
92#include <vm/pmap.h>
93#include <vm/vm_map.h>
94#include <sys/sysctl.h>
2689779e 95#include <sys/thread2.h>
984263bc
MD
96
97#include <machine/cpu.h>
98#include <machine/limits.h>
99#include <machine/smp.h>
100
101#ifdef GPROF
102#include <sys/gmon.h>
103#endif
104
105#ifdef DEVICE_POLLING
106extern void init_device_poll(void);
107extern void hardclock_device_poll(void);
108#endif /* DEVICE_POLLING */
109
402ed7e1 110static void initclocks (void *dummy);
984263bc
MD
111SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL)
112
6ad39cae
MD
113/*
114 * Some of these don't belong here, but it's easiest to concentrate them.
f5d21610 115 * Note that cp_time counts in microseconds, but most userland programs
6ad39cae
MD
116 * just compare relative times against the total by delta.
117 */
f5d21610 118struct cp_time cp_time;
984263bc
MD
119
120SYSCTL_OPAQUE(_kern, OID_AUTO, cp_time, CTLFLAG_RD, &cp_time, sizeof(cp_time),
121 "LU", "CPU time statistics");
122
88c4d2f6
MD
123/*
124 * boottime is used to calculate the 'real' uptime. Do not confuse this with
125 * microuptime(). microtime() is not drift compensated. The real uptime
60b2809b
MD
126 * with compensation is nanotime() - bootime. boottime is recalculated
127 * whenever the real time is set based on the compensated elapsed time
128 * in seconds (gd->gd_time_seconds).
88c4d2f6 129 *
88c4d2f6
MD
130 * The gd_time_seconds and gd_cpuclock_base fields remain fairly monotonic.
131 * Slight adjustments to gd_cpuclock_base are made to phase-lock it to
132 * the real time.
133 */
134struct timespec boottime; /* boot time (realtime) for reference only */
88c4d2f6 135time_t time_second; /* read-only 'passive' uptime in seconds */
984263bc 136
5eb5a6bc
MD
137/*
138 * basetime is used to calculate the compensated real time of day. The
139 * basetime can be modified on a per-tick basis by the adjtime(),
140 * ntp_adjtime(), and sysctl-based time correction APIs.
141 *
142 * Note that frequency corrections can also be made by adjusting
143 * gd_cpuclock_base.
144 *
145 * basetime is a tail-chasing FIFO, updated only by cpu #0. The FIFO is
146 * used on both SMP and UP systems to avoid MP races between cpu's and
147 * interrupt races on UP systems.
148 */
149#define BASETIME_ARYSIZE 16
150#define BASETIME_ARYMASK (BASETIME_ARYSIZE - 1)
151static struct timespec basetime[BASETIME_ARYSIZE];
152static volatile int basetime_index;
153
154static int
155sysctl_get_basetime(SYSCTL_HANDLER_ARGS)
156{
157 struct timespec *bt;
158 int error;
159
160 bt = &basetime[basetime_index];
08f95c49 161 error = SYSCTL_OUT(req, bt, sizeof(*bt));
5eb5a6bc
MD
162 return (error);
163}
164
984263bc 165SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime, CTLFLAG_RD,
08f95c49 166 &boottime, timespec, "System boottime");
5eb5a6bc 167SYSCTL_PROC(_kern, OID_AUTO, basetime, CTLTYPE_STRUCT|CTLFLAG_RD, 0, 0,
08f95c49 168 sysctl_get_basetime, "S,timespec", "System basetime");
984263bc 169
88c4d2f6
MD
170static void hardclock(systimer_t info, struct intrframe *frame);
171static void statclock(systimer_t info, struct intrframe *frame);
172static void schedclock(systimer_t info, struct intrframe *frame);
5eb5a6bc 173static void getnanotime_nbt(struct timespec *nbt, struct timespec *tsp);
88c4d2f6
MD
174
175int ticks; /* system master ticks at hz */
da3639ef 176int clocks_running; /* tsleep/timeout clocks operational */
88c4d2f6
MD
177int64_t nsec_adj; /* ntpd per-tick adjustment in nsec << 32 */
178int64_t nsec_acc; /* accumulator */
984263bc 179
4026c000
JS
180/* NTPD time correction fields */
181int64_t ntp_tick_permanent; /* per-tick adjustment in nsec << 32 */
182int64_t ntp_tick_acc; /* accumulator for per-tick adjustment */
183int64_t ntp_delta; /* one-time correction in nsec */
184int64_t ntp_big_delta = 1000000000;
185int32_t ntp_tick_delta; /* current adjustment rate */
186int32_t ntp_default_tick_delta; /* adjustment rate for ntp_delta */
48590578
JS
187time_t ntp_leap_second; /* time of next leap second */
188int ntp_leap_insert; /* whether to insert or remove a second */
4026c000 189
984263bc 190/*
88c4d2f6 191 * Finish initializing clock frequencies and start all clocks running.
984263bc 192 */
88c4d2f6
MD
193/* ARGSUSED*/
194static void
195initclocks(void *dummy)
984263bc 196{
88c4d2f6
MD
197 cpu_initclocks();
198#ifdef DEVICE_POLLING
199 init_device_poll();
200#endif
201 /*psratio = profhz / stathz;*/
202 initclocks_pcpu();
da3639ef 203 clocks_running = 1;
984263bc
MD
204}
205
88c4d2f6
MD
206/*
207 * Called on a per-cpu basis
208 */
209void
210initclocks_pcpu(void)
211{
212 struct globaldata *gd = mycpu;
984263bc 213
88c4d2f6
MD
214 crit_enter();
215 if (gd->gd_cpuid == 0) {
216 gd->gd_time_seconds = 1;
217 gd->gd_cpuclock_base = cputimer_count();
218 } else {
219 /* XXX */
220 gd->gd_time_seconds = globaldata_find(0)->gd_time_seconds;
221 gd->gd_cpuclock_base = globaldata_find(0)->gd_cpuclock_base;
222 }
0d1dffdf
MD
223
224 /*
225 * Use a non-queued periodic systimer to prevent multiple ticks from
226 * building up if the sysclock jumps forward (8254 gets reset). The
227 * sysclock will never jump backwards. Our time sync is based on
228 * the actual sysclock, not the ticks count.
229 */
230 systimer_init_periodic_nq(&gd->gd_hardclock, hardclock, NULL, hz);
231 systimer_init_periodic_nq(&gd->gd_statclock, statclock, NULL, stathz);
88c4d2f6 232 /* XXX correct the frequency for scheduler / estcpu tests */
0d1dffdf 233 systimer_init_periodic_nq(&gd->gd_schedclock, schedclock,
8478264a 234 NULL, ESTCPUFREQ);
88c4d2f6
MD
235 crit_exit();
236}
984263bc
MD
237
238/*
88c4d2f6
MD
239 * This sets the current real time of day. Timespecs are in seconds and
240 * nanoseconds. We do not mess with gd_time_seconds and gd_cpuclock_base,
241 * instead we adjust basetime so basetime + gd_* results in the current
242 * time of day. This way the gd_* fields are guarenteed to represent
243 * a monotonically increasing 'uptime' value.
5eb5a6bc
MD
244 *
245 * When set_timeofday() is called from userland, the system call forces it
246 * onto cpu #0 since only cpu #0 can update basetime_index.
984263bc 247 */
88c4d2f6
MD
248void
249set_timeofday(struct timespec *ts)
250{
5eb5a6bc
MD
251 struct timespec *nbt;
252 int ni;
984263bc 253
88c4d2f6
MD
254 /*
255 * XXX SMP / non-atomic basetime updates
256 */
257 crit_enter();
5eb5a6bc
MD
258 ni = (basetime_index + 1) & BASETIME_ARYMASK;
259 nbt = &basetime[ni];
260 nanouptime(nbt);
261 nbt->tv_sec = ts->tv_sec - nbt->tv_sec;
262 nbt->tv_nsec = ts->tv_nsec - nbt->tv_nsec;
263 if (nbt->tv_nsec < 0) {
264 nbt->tv_nsec += 1000000000;
265 --nbt->tv_sec;
88c4d2f6 266 }
a81931cc
MD
267
268 /*
269 * Note that basetime diverges from boottime as the clock drift is
270 * compensated for, so we cannot do away with boottime. When setting
271 * the absolute time of day the drift is 0 (for an instant) and we
272 * can simply assign boottime to basetime.
273 *
274 * Note that nanouptime() is based on gd_time_seconds which is drift
275 * compensated up to a point (it is guarenteed to remain monotonically
276 * increasing). gd_time_seconds is thus our best uptime guess and
277 * suitable for use in the boottime calculation. It is already taken
278 * into account in the basetime calculation above.
279 */
5eb5a6bc 280 boottime.tv_sec = nbt->tv_sec;
4026c000 281 ntp_delta = 0;
5eb5a6bc
MD
282
283 /*
284 * We now have a new basetime, update the index.
285 */
286 cpu_mb1();
287 basetime_index = ni;
288
88c4d2f6
MD
289 crit_exit();
290}
291
984263bc 292/*
88c4d2f6
MD
293 * Each cpu has its own hardclock, but we only increments ticks and softticks
294 * on cpu #0.
295 *
296 * NOTE! systimer! the MP lock might not be held here. We can only safely
297 * manipulate objects owned by the current cpu.
984263bc 298 */
984263bc 299static void
88c4d2f6 300hardclock(systimer_t info, struct intrframe *frame)
984263bc 301{
88c4d2f6
MD
302 sysclock_t cputicks;
303 struct proc *p;
304 struct pstats *pstats;
305 struct globaldata *gd = mycpu;
984263bc
MD
306
307 /*
88c4d2f6
MD
308 * Realtime updates are per-cpu. Note that timer corrections as
309 * returned by microtime() and friends make an additional adjustment
310 * using a system-wise 'basetime', but the running time is always
311 * taken from the per-cpu globaldata area. Since the same clock
312 * is distributing (XXX SMP) to all cpus, the per-cpu timebases
313 * stay in synch.
314 *
315 * Note that we never allow info->time (aka gd->gd_hardclock.time)
fad57d0e
MD
316 * to reverse index gd_cpuclock_base, but that it is possible for
317 * it to temporarily get behind in the seconds if something in the
318 * system locks interrupts for a long period of time. Since periodic
319 * timers count events, though everything should resynch again
320 * immediately.
984263bc 321 */
88c4d2f6 322 cputicks = info->time - gd->gd_cpuclock_base;
fad57d0e 323 if (cputicks >= cputimer_freq) {
88c4d2f6
MD
324 ++gd->gd_time_seconds;
325 gd->gd_cpuclock_base += cputimer_freq;
326 }
984263bc
MD
327
328 /*
92b561b7
MD
329 * The system-wide ticks counter and NTP related timedelta/tickdelta
330 * adjustments only occur on cpu #0. NTP adjustments are accomplished
331 * by updating basetime.
984263bc 332 */
88c4d2f6 333 if (gd->gd_cpuid == 0) {
5eb5a6bc 334 struct timespec *nbt;
88c4d2f6
MD
335 struct timespec nts;
336 int leap;
5eb5a6bc 337 int ni;
984263bc 338
88c4d2f6 339 ++ticks;
984263bc 340
88c4d2f6
MD
341#ifdef DEVICE_POLLING
342 hardclock_device_poll(); /* mpsafe, short and quick */
343#endif /* DEVICE_POLLING */
984263bc 344
88c4d2f6
MD
345#if 0
346 if (tco->tc_poll_pps)
347 tco->tc_poll_pps(tco);
348#endif
5eb5a6bc 349
88c4d2f6 350 /*
5eb5a6bc
MD
351 * Calculate the new basetime index. We are in a critical section
352 * on cpu #0 and can safely play with basetime_index. Start
353 * with the current basetime and then make adjustments.
354 */
355 ni = (basetime_index + 1) & BASETIME_ARYMASK;
356 nbt = &basetime[ni];
357 *nbt = basetime[basetime_index];
358
359 /*
360 * Apply adjtime corrections. (adjtime() API)
361 *
362 * adjtime() only runs on cpu #0 so our critical section is
363 * sufficient to access these variables.
88c4d2f6 364 */
4026c000 365 if (ntp_delta != 0) {
5eb5a6bc 366 nbt->tv_nsec += ntp_tick_delta;
4026c000
JS
367 ntp_delta -= ntp_tick_delta;
368 if ((ntp_delta > 0 && ntp_delta < ntp_tick_delta) ||
369 (ntp_delta < 0 && ntp_delta > ntp_tick_delta)) {
5eb5a6bc 370 ntp_tick_delta = ntp_delta;
4026c000
JS
371 }
372 }
373
5eb5a6bc
MD
374 /*
375 * Apply permanent frequency corrections. (sysctl API)
376 */
4026c000
JS
377 if (ntp_tick_permanent != 0) {
378 ntp_tick_acc += ntp_tick_permanent;
379 if (ntp_tick_acc >= (1LL << 32)) {
5eb5a6bc 380 nbt->tv_nsec += ntp_tick_acc >> 32;
331bc6f8 381 ntp_tick_acc -= (ntp_tick_acc >> 32) << 32;
4026c000 382 } else if (ntp_tick_acc <= -(1LL << 32)) {
331bc6f8 383 /* Negate ntp_tick_acc to avoid shifting the sign bit. */
5eb5a6bc 384 nbt->tv_nsec -= (-ntp_tick_acc) >> 32;
331bc6f8 385 ntp_tick_acc += ((-ntp_tick_acc) >> 32) << 32;
4026c000
JS
386 }
387 }
388
5eb5a6bc
MD
389 if (nbt->tv_nsec >= 1000000000) {
390 nbt->tv_sec++;
391 nbt->tv_nsec -= 1000000000;
392 } else if (nbt->tv_nsec < 0) {
393 nbt->tv_sec--;
394 nbt->tv_nsec += 1000000000;
88c4d2f6
MD
395 }
396
397 /*
5eb5a6bc 398 * Another per-tick compensation. (for ntp_adjtime() API)
88c4d2f6 399 */
5eb5a6bc 400 if (nsec_adj != 0) {
88c4d2f6
MD
401 nsec_acc += nsec_adj;
402 if (nsec_acc >= 0x100000000LL) {
5eb5a6bc 403 nbt->tv_nsec += nsec_acc >> 32;
88c4d2f6
MD
404 nsec_acc = (nsec_acc & 0xFFFFFFFFLL);
405 } else if (nsec_acc <= -0x100000000LL) {
5eb5a6bc 406 nbt->tv_nsec -= -nsec_acc >> 32;
88c4d2f6
MD
407 nsec_acc = -(-nsec_acc & 0xFFFFFFFFLL);
408 }
5eb5a6bc
MD
409 if (nbt->tv_nsec >= 1000000000) {
410 nbt->tv_nsec -= 1000000000;
411 ++nbt->tv_sec;
412 } else if (nbt->tv_nsec < 0) {
413 nbt->tv_nsec += 1000000000;
414 --nbt->tv_sec;
415 }
416 }
417
418 /************************************************************
419 * LEAP SECOND CORRECTION *
420 ************************************************************
421 *
422 * Taking into account all the corrections made above, figure
423 * out the new real time. If the seconds field has changed
424 * then apply any pending leap-second corrections.
425 */
426 getnanotime_nbt(nbt, &nts);
427
32040d57
MD
428 if (time_second != nts.tv_sec) {
429 /*
430 * Apply leap second (sysctl API). Adjust nts for changes
431 * so we do not have to call getnanotime_nbt again.
432 */
433 if (ntp_leap_second) {
434 if (ntp_leap_second == nts.tv_sec) {
435 if (ntp_leap_insert) {
436 nbt->tv_sec++;
437 nts.tv_sec++;
438 } else {
439 nbt->tv_sec--;
440 nts.tv_sec--;
441 }
5eb5a6bc 442 ntp_leap_second--;
32040d57 443 }
88c4d2f6 444 }
88c4d2f6 445
32040d57
MD
446 /*
447 * Apply leap second (ntp_adjtime() API), calculate a new
448 * nsec_adj field. ntp_update_second() returns nsec_adj
449 * as a per-second value but we need it as a per-tick value.
450 */
88c4d2f6 451 leap = ntp_update_second(time_second, &nsec_adj);
88c4d2f6 452 nsec_adj /= hz;
32040d57
MD
453 nbt->tv_sec += leap;
454 nts.tv_sec += leap;
455
456 /*
457 * Update the time_second 'approximate time' global.
458 */
459 time_second = nts.tv_sec;
88c4d2f6 460 }
5eb5a6bc
MD
461
462 /*
463 * Finally, our new basetime is ready to go live!
464 */
465 cpu_mb1();
466 basetime_index = ni;
88c4d2f6
MD
467 }
468
92b561b7
MD
469 /*
470 * softticks are handled for all cpus
471 */
472 hardclock_softtick(gd);
473
88c4d2f6
MD
474 /*
475 * ITimer handling is per-tick, per-cpu. I don't think psignal()
476 * is mpsafe on curproc, so XXX get the mplock.
477 */
478 if ((p = curproc) != NULL && try_mplock()) {
984263bc 479 pstats = p->p_stats;
88c4d2f6 480 if (frame && CLKF_USERMODE(frame) &&
984263bc
MD
481 timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
482 itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0)
483 psignal(p, SIGVTALRM);
484 if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
485 itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0)
486 psignal(p, SIGPROF);
88c4d2f6 487 rel_mplock();
984263bc 488 }
604e1e09 489 setdelayed();
88c4d2f6 490}
984263bc 491
88c4d2f6
MD
492/*
493 * The statistics clock typically runs at a 125Hz rate, and is intended
494 * to be frequency offset from the hardclock (typ 100Hz). It is per-cpu.
495 *
496 * NOTE! systimer! the MP lock might not be held here. We can only safely
497 * manipulate objects owned by the current cpu.
498 *
499 * The stats clock is responsible for grabbing a profiling sample.
500 * Most of the statistics are only used by user-level statistics programs.
501 * The main exceptions are p->p_uticks, p->p_sticks, p->p_iticks, and
502 * p->p_estcpu.
503 *
504 * Like the other clocks, the stat clock is called from what is effectively
505 * a fast interrupt, so the context should be the thread/process that got
506 * interrupted.
507 */
508static void
509statclock(systimer_t info, struct intrframe *frame)
510{
511#ifdef GPROF
512 struct gmonparam *g;
513 int i;
984263bc 514#endif
88c4d2f6
MD
515 thread_t td;
516 struct proc *p;
517 int bump;
518 struct timeval tv;
519 struct timeval *stv;
984263bc
MD
520
521 /*
88c4d2f6 522 * How big was our timeslice relative to the last time?
984263bc 523 */
88c4d2f6
MD
524 microuptime(&tv); /* mpsafe */
525 stv = &mycpu->gd_stattv;
526 if (stv->tv_sec == 0) {
527 bump = 1;
528 } else {
529 bump = tv.tv_usec - stv->tv_usec +
530 (tv.tv_sec - stv->tv_sec) * 1000000;
531 if (bump < 0)
532 bump = 0;
533 if (bump > 1000000)
534 bump = 1000000;
535 }
536 *stv = tv;
984263bc 537
88c4d2f6
MD
538 td = curthread;
539 p = td->td_proc;
984263bc 540
88c4d2f6
MD
541 if (frame && CLKF_USERMODE(frame)) {
542 /*
543 * Came from userland, handle user time and deal with
544 * possible process.
545 */
546 if (p && (p->p_flag & P_PROFIL))
547 addupc_intr(p, CLKF_PC(frame), 1);
548 td->td_uticks += bump;
984263bc 549
88c4d2f6
MD
550 /*
551 * Charge the time as appropriate
552 */
553 if (p && p->p_nice > NZERO)
f5d21610 554 cp_time.cp_nice += bump;
88c4d2f6 555 else
f5d21610 556 cp_time.cp_user += bump;
88c4d2f6
MD
557 } else {
558#ifdef GPROF
559 /*
560 * Kernel statistics are just like addupc_intr, only easier.
561 */
562 g = &_gmonparam;
563 if (g->state == GMON_PROF_ON && frame) {
564 i = CLKF_PC(frame) - g->lowpc;
565 if (i < g->textsize) {
566 i /= HISTFRACTION * sizeof(*g->kcount);
567 g->kcount[i]++;
568 }
569 }
570#endif
571 /*
572 * Came from kernel mode, so we were:
573 * - handling an interrupt,
574 * - doing syscall or trap work on behalf of the current
575 * user process, or
576 * - spinning in the idle loop.
577 * Whichever it is, charge the time as appropriate.
578 * Note that we charge interrupts to the current process,
579 * regardless of whether they are ``for'' that process,
580 * so that we know how much of its real time was spent
581 * in ``non-process'' (i.e., interrupt) work.
582 *
583 * XXX assume system if frame is NULL. A NULL frame
584 * can occur if ipi processing is done from an splx().
585 */
586 if (frame && CLKF_INTR(frame))
587 td->td_iticks += bump;
588 else
589 td->td_sticks += bump;
590
591 if (frame && CLKF_INTR(frame)) {
f5d21610 592 cp_time.cp_intr += bump;
88c4d2f6
MD
593 } else {
594 if (td == &mycpu->gd_idlethread)
f5d21610 595 cp_time.cp_idle += bump;
88c4d2f6 596 else
f5d21610 597 cp_time.cp_sys += bump;
88c4d2f6
MD
598 }
599 }
600}
601
602/*
0a3f9b47 603 * The scheduler clock typically runs at a 20Hz rate. NOTE! systimer,
88c4d2f6
MD
604 * the MP lock might not be held. We can safely manipulate parts of curproc
605 * but that's about it.
606 */
607static void
608schedclock(systimer_t info, struct intrframe *frame)
609{
610 struct proc *p;
611 struct pstats *pstats;
612 struct rusage *ru;
613 struct vmspace *vm;
614 long rss;
615
616 schedulerclock(NULL); /* mpsafe */
617 if ((p = curproc) != NULL) {
618 /* Update resource usage integrals and maximums. */
619 if ((pstats = p->p_stats) != NULL &&
620 (ru = &pstats->p_ru) != NULL &&
621 (vm = p->p_vmspace) != NULL) {
622 ru->ru_ixrss += pgtok(vm->vm_tsize);
623 ru->ru_idrss += pgtok(vm->vm_dsize);
624 ru->ru_isrss += pgtok(vm->vm_ssize);
625 rss = pgtok(vmspace_resident_count(vm));
626 if (ru->ru_maxrss < rss)
627 ru->ru_maxrss = rss;
628 }
b68b7282 629 }
984263bc
MD
630}
631
632/*
a94976ad
MD
633 * Compute number of ticks for the specified amount of time. The
634 * return value is intended to be used in a clock interrupt timed
635 * operation and guarenteed to meet or exceed the requested time.
636 * If the representation overflows, return INT_MAX. The minimum return
637 * value is 1 ticks and the function will average the calculation up.
638 * If any value greater then 0 microseconds is supplied, a value
639 * of at least 2 will be returned to ensure that a near-term clock
640 * interrupt does not cause the timeout to occur (degenerately) early.
641 *
642 * Note that limit checks must take into account microseconds, which is
643 * done simply by using the smaller signed long maximum instead of
644 * the unsigned long maximum.
645 *
646 * If ints have 32 bits, then the maximum value for any timeout in
647 * 10ms ticks is 248 days.
984263bc
MD
648 */
649int
a94976ad 650tvtohz_high(struct timeval *tv)
984263bc 651{
a94976ad 652 int ticks;
1fd87d54 653 long sec, usec;
984263bc 654
984263bc
MD
655 sec = tv->tv_sec;
656 usec = tv->tv_usec;
657 if (usec < 0) {
658 sec--;
659 usec += 1000000;
660 }
661 if (sec < 0) {
662#ifdef DIAGNOSTIC
663 if (usec > 0) {
664 sec++;
665 usec -= 1000000;
666 }
667 printf("tvotohz: negative time difference %ld sec %ld usec\n",
668 sec, usec);
669#endif
670 ticks = 1;
a94976ad
MD
671 } else if (sec <= INT_MAX / hz) {
672 ticks = (int)(sec * hz +
673 ((u_long)usec + (tick - 1)) / tick) + 1;
674 } else {
675 ticks = INT_MAX;
676 }
677 return (ticks);
678}
679
680/*
681 * Compute number of ticks for the specified amount of time, erroring on
682 * the side of it being too low to ensure that sleeping the returned number
683 * of ticks will not result in a late return.
684 *
685 * The supplied timeval may not be negative and should be normalized. A
686 * return value of 0 is possible if the timeval converts to less then
687 * 1 tick.
688 *
689 * If ints have 32 bits, then the maximum value for any timeout in
690 * 10ms ticks is 248 days.
691 */
692int
693tvtohz_low(struct timeval *tv)
694{
695 int ticks;
696 long sec;
697
698 sec = tv->tv_sec;
699 if (sec <= INT_MAX / hz)
700 ticks = (int)(sec * hz + (u_long)tv->tv_usec / tick);
984263bc 701 else
984263bc 702 ticks = INT_MAX;
a94976ad 703 return (ticks);
984263bc
MD
704}
705
a94976ad 706
984263bc
MD
707/*
708 * Start profiling on a process.
709 *
710 * Kernel profiling passes proc0 which never exits and hence
711 * keeps the profile clock running constantly.
712 */
713void
88c4d2f6 714startprofclock(struct proc *p)
984263bc 715{
984263bc
MD
716 if ((p->p_flag & P_PROFIL) == 0) {
717 p->p_flag |= P_PROFIL;
88c4d2f6 718#if 0 /* XXX */
984263bc
MD
719 if (++profprocs == 1 && stathz != 0) {
720 s = splstatclock();
6ad39cae 721 psdiv = psratio;
984263bc
MD
722 setstatclockrate(profhz);
723 splx(s);
724 }
88c4d2f6 725#endif
984263bc
MD
726 }
727}
728
729/*
730 * Stop profiling on a process.
731 */
732void
88c4d2f6 733stopprofclock(struct proc *p)
984263bc 734{
984263bc
MD
735 if (p->p_flag & P_PROFIL) {
736 p->p_flag &= ~P_PROFIL;
88c4d2f6 737#if 0 /* XXX */
984263bc
MD
738 if (--profprocs == 0 && stathz != 0) {
739 s = splstatclock();
6ad39cae 740 psdiv = 1;
984263bc
MD
741 setstatclockrate(stathz);
742 splx(s);
743 }
984263bc 744#endif
984263bc
MD
745 }
746}
747
748/*
749 * Return information about system clocks.
750 */
751static int
752sysctl_kern_clockrate(SYSCTL_HANDLER_ARGS)
753{
f5d21610 754 struct kinfo_clockinfo clkinfo;
984263bc
MD
755 /*
756 * Construct clockinfo structure.
757 */
f5d21610
JS
758 clkinfo.ci_hz = hz;
759 clkinfo.ci_tick = tick;
4026c000 760 clkinfo.ci_tickadj = ntp_default_tick_delta / 1000;
f5d21610
JS
761 clkinfo.ci_profhz = profhz;
762 clkinfo.ci_stathz = stathz ? stathz : hz;
984263bc
MD
763 return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req));
764}
765
766SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, CTLTYPE_STRUCT|CTLFLAG_RD,
767 0, 0, sysctl_kern_clockrate, "S,clockinfo","");
768
984263bc
MD
769/*
770 * We have eight functions for looking at the clock, four for
771 * microseconds and four for nanoseconds. For each there is fast
772 * but less precise version "get{nano|micro}[up]time" which will
773 * return a time which is up to 1/HZ previous to the call, whereas
774 * the raw version "{nano|micro}[up]time" will return a timestamp
775 * which is as precise as possible. The "up" variants return the
776 * time relative to system boot, these are well suited for time
777 * interval measurements.
88c4d2f6
MD
778 *
779 * Each cpu independantly maintains the current time of day, so all
780 * we need to do to protect ourselves from changes is to do a loop
781 * check on the seconds field changing out from under us.
fad57d0e
MD
782 *
783 * The system timer maintains a 32 bit count and due to various issues
784 * it is possible for the calculated delta to occassionally exceed
785 * cputimer_freq. If this occurs the cputimer_freq64_nsec multiplication
786 * can easily overflow, so we deal with the case. For uniformity we deal
787 * with the case in the usec case too.
984263bc 788 */
984263bc
MD
789void
790getmicrouptime(struct timeval *tvp)
791{
88c4d2f6
MD
792 struct globaldata *gd = mycpu;
793 sysclock_t delta;
794
795 do {
796 tvp->tv_sec = gd->gd_time_seconds;
797 delta = gd->gd_hardclock.time - gd->gd_cpuclock_base;
798 } while (tvp->tv_sec != gd->gd_time_seconds);
fad57d0e
MD
799
800 if (delta >= cputimer_freq) {
801 tvp->tv_sec += delta / cputimer_freq;
802 delta %= cputimer_freq;
803 }
88c4d2f6
MD
804 tvp->tv_usec = (cputimer_freq64_usec * delta) >> 32;
805 if (tvp->tv_usec >= 1000000) {
806 tvp->tv_usec -= 1000000;
807 ++tvp->tv_sec;
984263bc
MD
808 }
809}
810
811void
812getnanouptime(struct timespec *tsp)
813{
88c4d2f6
MD
814 struct globaldata *gd = mycpu;
815 sysclock_t delta;
816
817 do {
818 tsp->tv_sec = gd->gd_time_seconds;
819 delta = gd->gd_hardclock.time - gd->gd_cpuclock_base;
820 } while (tsp->tv_sec != gd->gd_time_seconds);
fad57d0e
MD
821
822 if (delta >= cputimer_freq) {
823 tsp->tv_sec += delta / cputimer_freq;
824 delta %= cputimer_freq;
984263bc 825 }
fad57d0e 826 tsp->tv_nsec = (cputimer_freq64_nsec * delta) >> 32;
984263bc
MD
827}
828
829void
88c4d2f6 830microuptime(struct timeval *tvp)
984263bc 831{
88c4d2f6
MD
832 struct globaldata *gd = mycpu;
833 sysclock_t delta;
834
835 do {
836 tvp->tv_sec = gd->gd_time_seconds;
837 delta = cputimer_count() - gd->gd_cpuclock_base;
838 } while (tvp->tv_sec != gd->gd_time_seconds);
fad57d0e
MD
839
840 if (delta >= cputimer_freq) {
841 tvp->tv_sec += delta / cputimer_freq;
842 delta %= cputimer_freq;
984263bc 843 }
fad57d0e 844 tvp->tv_usec = (cputimer_freq64_usec * delta) >> 32;
984263bc
MD
845}
846
847void
88c4d2f6 848nanouptime(struct timespec *tsp)
984263bc 849{
88c4d2f6
MD
850 struct globaldata *gd = mycpu;
851 sysclock_t delta;
852
853 do {
854 tsp->tv_sec = gd->gd_time_seconds;
855 delta = cputimer_count() - gd->gd_cpuclock_base;
856 } while (tsp->tv_sec != gd->gd_time_seconds);
fad57d0e
MD
857
858 if (delta >= cputimer_freq) {
859 tsp->tv_sec += delta / cputimer_freq;
860 delta %= cputimer_freq;
984263bc 861 }
fad57d0e 862 tsp->tv_nsec = (cputimer_freq64_nsec * delta) >> 32;
984263bc
MD
863}
864
88c4d2f6
MD
865/*
866 * realtime routines
867 */
984263bc
MD
868
869void
88c4d2f6 870getmicrotime(struct timeval *tvp)
984263bc 871{
88c4d2f6 872 struct globaldata *gd = mycpu;
5eb5a6bc 873 struct timespec *bt;
88c4d2f6 874 sysclock_t delta;
984263bc 875
88c4d2f6
MD
876 do {
877 tvp->tv_sec = gd->gd_time_seconds;
878 delta = gd->gd_hardclock.time - gd->gd_cpuclock_base;
879 } while (tvp->tv_sec != gd->gd_time_seconds);
fad57d0e
MD
880
881 if (delta >= cputimer_freq) {
882 tvp->tv_sec += delta / cputimer_freq;
883 delta %= cputimer_freq;
884 }
88c4d2f6 885 tvp->tv_usec = (cputimer_freq64_usec * delta) >> 32;
984263bc 886
5eb5a6bc
MD
887 bt = &basetime[basetime_index];
888 tvp->tv_sec += bt->tv_sec;
889 tvp->tv_usec += bt->tv_nsec / 1000;
88c4d2f6
MD
890 while (tvp->tv_usec >= 1000000) {
891 tvp->tv_usec -= 1000000;
892 ++tvp->tv_sec;
984263bc 893 }
984263bc
MD
894}
895
896void
88c4d2f6 897getnanotime(struct timespec *tsp)
984263bc 898{
88c4d2f6 899 struct globaldata *gd = mycpu;
5eb5a6bc 900 struct timespec *bt;
88c4d2f6 901 sysclock_t delta;
984263bc 902
88c4d2f6
MD
903 do {
904 tsp->tv_sec = gd->gd_time_seconds;
905 delta = gd->gd_hardclock.time - gd->gd_cpuclock_base;
906 } while (tsp->tv_sec != gd->gd_time_seconds);
fad57d0e
MD
907
908 if (delta >= cputimer_freq) {
909 tsp->tv_sec += delta / cputimer_freq;
910 delta %= cputimer_freq;
911 }
88c4d2f6 912 tsp->tv_nsec = (cputimer_freq64_nsec * delta) >> 32;
984263bc 913
5eb5a6bc
MD
914 bt = &basetime[basetime_index];
915 tsp->tv_sec += bt->tv_sec;
916 tsp->tv_nsec += bt->tv_nsec;
88c4d2f6
MD
917 while (tsp->tv_nsec >= 1000000000) {
918 tsp->tv_nsec -= 1000000000;
919 ++tsp->tv_sec;
984263bc 920 }
984263bc
MD
921}
922
5eb5a6bc
MD
923static void
924getnanotime_nbt(struct timespec *nbt, struct timespec *tsp)
925{
926 struct globaldata *gd = mycpu;
927 sysclock_t delta;
928
929 do {
930 tsp->tv_sec = gd->gd_time_seconds;
931 delta = gd->gd_hardclock.time - gd->gd_cpuclock_base;
932 } while (tsp->tv_sec != gd->gd_time_seconds);
933
934 if (delta >= cputimer_freq) {
935 tsp->tv_sec += delta / cputimer_freq;
936 delta %= cputimer_freq;
937 }
938 tsp->tv_nsec = (cputimer_freq64_nsec * delta) >> 32;
939
940 tsp->tv_sec += nbt->tv_sec;
941 tsp->tv_nsec += nbt->tv_nsec;
942 while (tsp->tv_nsec >= 1000000000) {
943 tsp->tv_nsec -= 1000000000;
944 ++tsp->tv_sec;
945 }
946}
947
948
88c4d2f6
MD
949void
950microtime(struct timeval *tvp)
984263bc 951{
88c4d2f6 952 struct globaldata *gd = mycpu;
5eb5a6bc 953 struct timespec *bt;
88c4d2f6 954 sysclock_t delta;
984263bc 955
88c4d2f6
MD
956 do {
957 tvp->tv_sec = gd->gd_time_seconds;
958 delta = cputimer_count() - gd->gd_cpuclock_base;
959 } while (tvp->tv_sec != gd->gd_time_seconds);
fad57d0e
MD
960
961 if (delta >= cputimer_freq) {
962 tvp->tv_sec += delta / cputimer_freq;
963 delta %= cputimer_freq;
964 }
88c4d2f6 965 tvp->tv_usec = (cputimer_freq64_usec * delta) >> 32;
984263bc 966
5eb5a6bc
MD
967 bt = &basetime[basetime_index];
968 tvp->tv_sec += bt->tv_sec;
969 tvp->tv_usec += bt->tv_nsec / 1000;
88c4d2f6
MD
970 while (tvp->tv_usec >= 1000000) {
971 tvp->tv_usec -= 1000000;
972 ++tvp->tv_sec;
984263bc 973 }
984263bc
MD
974}
975
88c4d2f6
MD
976void
977nanotime(struct timespec *tsp)
978{
979 struct globaldata *gd = mycpu;
5eb5a6bc 980 struct timespec *bt;
88c4d2f6 981 sysclock_t delta;
984263bc 982
88c4d2f6
MD
983 do {
984 tsp->tv_sec = gd->gd_time_seconds;
985 delta = cputimer_count() - gd->gd_cpuclock_base;
986 } while (tsp->tv_sec != gd->gd_time_seconds);
fad57d0e
MD
987
988 if (delta >= cputimer_freq) {
989 tsp->tv_sec += delta / cputimer_freq;
990 delta %= cputimer_freq;
991 }
88c4d2f6 992 tsp->tv_nsec = (cputimer_freq64_nsec * delta) >> 32;
984263bc 993
5eb5a6bc
MD
994 bt = &basetime[basetime_index];
995 tsp->tv_sec += bt->tv_sec;
996 tsp->tv_nsec += bt->tv_nsec;
88c4d2f6
MD
997 while (tsp->tv_nsec >= 1000000000) {
998 tsp->tv_nsec -= 1000000000;
999 ++tsp->tv_sec;
984263bc 1000 }
984263bc
MD
1001}
1002
25b804e7
MD
1003/*
1004 * note: this is not exactly synchronized with real time. To do that we
1005 * would have to do what microtime does and check for a nanoseconds overflow.
1006 */
1007time_t
1008get_approximate_time_t(void)
1009{
1010 struct globaldata *gd = mycpu;
5eb5a6bc
MD
1011 struct timespec *bt;
1012
1013 bt = &basetime[basetime_index];
1014 return(gd->gd_time_seconds + bt->tv_sec);
25b804e7
MD
1015}
1016
984263bc
MD
1017int
1018pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
1019{
1020 pps_params_t *app;
1021 struct pps_fetch_args *fapi;
1022#ifdef PPS_SYNC
1023 struct pps_kcbind_args *kapi;
1024#endif
1025
1026 switch (cmd) {
1027 case PPS_IOC_CREATE:
1028 return (0);
1029 case PPS_IOC_DESTROY:
1030 return (0);
1031 case PPS_IOC_SETPARAMS:
1032 app = (pps_params_t *)data;
1033 if (app->mode & ~pps->ppscap)
1034 return (EINVAL);
1035 pps->ppsparam = *app;
1036 return (0);
1037 case PPS_IOC_GETPARAMS:
1038 app = (pps_params_t *)data;
1039 *app = pps->ppsparam;
1040 app->api_version = PPS_API_VERS_1;
1041 return (0);
1042 case PPS_IOC_GETCAP:
1043 *(int*)data = pps->ppscap;
1044 return (0);
1045 case PPS_IOC_FETCH:
1046 fapi = (struct pps_fetch_args *)data;
1047 if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
1048 return (EINVAL);
1049 if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec)
1050 return (EOPNOTSUPP);
1051 pps->ppsinfo.current_mode = pps->ppsparam.mode;
1052 fapi->pps_info_buf = pps->ppsinfo;
1053 return (0);
1054 case PPS_IOC_KCBIND:
1055#ifdef PPS_SYNC
1056 kapi = (struct pps_kcbind_args *)data;
1057 /* XXX Only root should be able to do this */
1058 if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
1059 return (EINVAL);
1060 if (kapi->kernel_consumer != PPS_KC_HARDPPS)
1061 return (EINVAL);
1062 if (kapi->edge & ~pps->ppscap)
1063 return (EINVAL);
1064 pps->kcmode = kapi->edge;
1065 return (0);
1066#else
1067 return (EOPNOTSUPP);
1068#endif
1069 default:
1070 return (ENOTTY);
1071 }
1072}
1073
1074void
1075pps_init(struct pps_state *pps)
1076{
1077 pps->ppscap |= PPS_TSFMT_TSPEC;
1078 if (pps->ppscap & PPS_CAPTUREASSERT)
1079 pps->ppscap |= PPS_OFFSETASSERT;
1080 if (pps->ppscap & PPS_CAPTURECLEAR)
1081 pps->ppscap |= PPS_OFFSETCLEAR;
1082}
1083
1084void
88c4d2f6 1085pps_event(struct pps_state *pps, sysclock_t count, int event)
984263bc 1086{
88c4d2f6
MD
1087 struct globaldata *gd;
1088 struct timespec *tsp;
1089 struct timespec *osp;
5eb5a6bc 1090 struct timespec *bt;
88c4d2f6
MD
1091 struct timespec ts;
1092 sysclock_t *pcount;
1093#ifdef PPS_SYNC
1094 sysclock_t tcount;
1095#endif
1096 sysclock_t delta;
1097 pps_seq_t *pseq;
1098 int foff;
1099 int fhard;
1100
1101 gd = mycpu;
984263bc
MD
1102
1103 /* Things would be easier with arrays... */
1104 if (event == PPS_CAPTUREASSERT) {
1105 tsp = &pps->ppsinfo.assert_timestamp;
1106 osp = &pps->ppsparam.assert_offset;
1107 foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
1108 fhard = pps->kcmode & PPS_CAPTUREASSERT;
1109 pcount = &pps->ppscount[0];
1110 pseq = &pps->ppsinfo.assert_sequence;
1111 } else {
1112 tsp = &pps->ppsinfo.clear_timestamp;
1113 osp = &pps->ppsparam.clear_offset;
1114 foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
1115 fhard = pps->kcmode & PPS_CAPTURECLEAR;
1116 pcount = &pps->ppscount[1];
1117 pseq = &pps->ppsinfo.clear_sequence;
1118 }
1119
984263bc
MD
1120 /* Nothing really happened */
1121 if (*pcount == count)
1122 return;
1123
1124 *pcount = count;
1125
88c4d2f6
MD
1126 do {
1127 ts.tv_sec = gd->gd_time_seconds;
1128 delta = count - gd->gd_cpuclock_base;
1129 } while (ts.tv_sec != gd->gd_time_seconds);
fad57d0e
MD
1130
1131 if (delta >= cputimer_freq) {
88c4d2f6
MD
1132 ts.tv_sec += delta / cputimer_freq;
1133 delta %= cputimer_freq;
1134 }
1135 ts.tv_nsec = (cputimer_freq64_nsec * delta) >> 32;
5eb5a6bc
MD
1136 bt = &basetime[basetime_index];
1137 ts.tv_sec += bt->tv_sec;
1138 ts.tv_nsec += bt->tv_nsec;
88c4d2f6
MD
1139 while (ts.tv_nsec >= 1000000000) {
1140 ts.tv_nsec -= 1000000000;
1141 ++ts.tv_sec;
984263bc 1142 }
984263bc
MD
1143
1144 (*pseq)++;
1145 *tsp = ts;
1146
1147 if (foff) {
1148 timespecadd(tsp, osp);
1149 if (tsp->tv_nsec < 0) {
1150 tsp->tv_nsec += 1000000000;
1151 tsp->tv_sec -= 1;
1152 }
1153 }
1154#ifdef PPS_SYNC
1155 if (fhard) {
1156 /* magic, at its best... */
1157 tcount = count - pps->ppscount[2];
1158 pps->ppscount[2] = count;
fad57d0e 1159 if (tcount >= cputimer_freq) {
64642171
JS
1160 delta = (1000000000 * (tcount / cputimer_freq) +
1161 cputimer_freq64_nsec *
fad57d0e
MD
1162 (tcount % cputimer_freq)) >> 32;
1163 } else {
1164 delta = (cputimer_freq64_nsec * tcount) >> 32;
1165 }
984263bc
MD
1166 hardpps(tsp, delta);
1167 }
1168#endif
1169}
88c4d2f6 1170