NULL-out two stack-declared variables that were incorrectly assumed to
[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 $
48590578 73 * $DragonFly: src/sys/kern/kern_clock.c,v 1.34 2005/04/14 11:15:52 joerg 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
MD
129 *
130 * basetime is used to calculate the compensated real time of day. Chunky
131 * changes to the time, aka settimeofday(), are made by modifying basetime.
132 *
133 * The gd_time_seconds and gd_cpuclock_base fields remain fairly monotonic.
134 * Slight adjustments to gd_cpuclock_base are made to phase-lock it to
135 * the real time.
136 */
137struct timespec boottime; /* boot time (realtime) for reference only */
138struct timespec basetime; /* base time adjusts uptime -> realtime */
139time_t time_second; /* read-only 'passive' uptime in seconds */
984263bc 140
984263bc
MD
141SYSCTL_STRUCT(_kern, KERN_BOOTTIME, boottime, CTLFLAG_RD,
142 &boottime, timeval, "System boottime");
88c4d2f6
MD
143SYSCTL_STRUCT(_kern, OID_AUTO, basetime, CTLFLAG_RD,
144 &basetime, timeval, "System basetime");
984263bc 145
88c4d2f6
MD
146static void hardclock(systimer_t info, struct intrframe *frame);
147static void statclock(systimer_t info, struct intrframe *frame);
148static void schedclock(systimer_t info, struct intrframe *frame);
149
150int ticks; /* system master ticks at hz */
da3639ef 151int clocks_running; /* tsleep/timeout clocks operational */
88c4d2f6
MD
152int64_t nsec_adj; /* ntpd per-tick adjustment in nsec << 32 */
153int64_t nsec_acc; /* accumulator */
984263bc 154
4026c000
JS
155/* NTPD time correction fields */
156int64_t ntp_tick_permanent; /* per-tick adjustment in nsec << 32 */
157int64_t ntp_tick_acc; /* accumulator for per-tick adjustment */
158int64_t ntp_delta; /* one-time correction in nsec */
159int64_t ntp_big_delta = 1000000000;
160int32_t ntp_tick_delta; /* current adjustment rate */
161int32_t ntp_default_tick_delta; /* adjustment rate for ntp_delta */
48590578
JS
162time_t ntp_leap_second; /* time of next leap second */
163int ntp_leap_insert; /* whether to insert or remove a second */
4026c000 164
984263bc 165/*
88c4d2f6 166 * Finish initializing clock frequencies and start all clocks running.
984263bc 167 */
88c4d2f6
MD
168/* ARGSUSED*/
169static void
170initclocks(void *dummy)
984263bc 171{
88c4d2f6
MD
172 cpu_initclocks();
173#ifdef DEVICE_POLLING
174 init_device_poll();
175#endif
176 /*psratio = profhz / stathz;*/
177 initclocks_pcpu();
da3639ef 178 clocks_running = 1;
984263bc
MD
179}
180
88c4d2f6
MD
181/*
182 * Called on a per-cpu basis
183 */
184void
185initclocks_pcpu(void)
186{
187 struct globaldata *gd = mycpu;
984263bc 188
88c4d2f6
MD
189 crit_enter();
190 if (gd->gd_cpuid == 0) {
191 gd->gd_time_seconds = 1;
192 gd->gd_cpuclock_base = cputimer_count();
193 } else {
194 /* XXX */
195 gd->gd_time_seconds = globaldata_find(0)->gd_time_seconds;
196 gd->gd_cpuclock_base = globaldata_find(0)->gd_cpuclock_base;
197 }
0d1dffdf
MD
198
199 /*
200 * Use a non-queued periodic systimer to prevent multiple ticks from
201 * building up if the sysclock jumps forward (8254 gets reset). The
202 * sysclock will never jump backwards. Our time sync is based on
203 * the actual sysclock, not the ticks count.
204 */
205 systimer_init_periodic_nq(&gd->gd_hardclock, hardclock, NULL, hz);
206 systimer_init_periodic_nq(&gd->gd_statclock, statclock, NULL, stathz);
88c4d2f6 207 /* XXX correct the frequency for scheduler / estcpu tests */
0d1dffdf 208 systimer_init_periodic_nq(&gd->gd_schedclock, schedclock,
8478264a 209 NULL, ESTCPUFREQ);
88c4d2f6
MD
210 crit_exit();
211}
984263bc
MD
212
213/*
88c4d2f6
MD
214 * This sets the current real time of day. Timespecs are in seconds and
215 * nanoseconds. We do not mess with gd_time_seconds and gd_cpuclock_base,
216 * instead we adjust basetime so basetime + gd_* results in the current
217 * time of day. This way the gd_* fields are guarenteed to represent
218 * a monotonically increasing 'uptime' value.
984263bc 219 */
88c4d2f6
MD
220void
221set_timeofday(struct timespec *ts)
222{
223 struct timespec ts2;
984263bc 224
88c4d2f6
MD
225 /*
226 * XXX SMP / non-atomic basetime updates
227 */
228 crit_enter();
229 nanouptime(&ts2);
230 basetime.tv_sec = ts->tv_sec - ts2.tv_sec;
231 basetime.tv_nsec = ts->tv_nsec - ts2.tv_nsec;
232 if (basetime.tv_nsec < 0) {
233 basetime.tv_nsec += 1000000000;
234 --basetime.tv_sec;
235 }
a81931cc
MD
236
237 /*
238 * Note that basetime diverges from boottime as the clock drift is
239 * compensated for, so we cannot do away with boottime. When setting
240 * the absolute time of day the drift is 0 (for an instant) and we
241 * can simply assign boottime to basetime.
242 *
243 * Note that nanouptime() is based on gd_time_seconds which is drift
244 * compensated up to a point (it is guarenteed to remain monotonically
245 * increasing). gd_time_seconds is thus our best uptime guess and
246 * suitable for use in the boottime calculation. It is already taken
247 * into account in the basetime calculation above.
248 */
249 boottime.tv_sec = basetime.tv_sec;
4026c000 250 ntp_delta = 0;
88c4d2f6
MD
251 crit_exit();
252}
253
984263bc 254/*
88c4d2f6
MD
255 * Each cpu has its own hardclock, but we only increments ticks and softticks
256 * on cpu #0.
257 *
258 * NOTE! systimer! the MP lock might not be held here. We can only safely
259 * manipulate objects owned by the current cpu.
984263bc 260 */
984263bc 261static void
88c4d2f6 262hardclock(systimer_t info, struct intrframe *frame)
984263bc 263{
88c4d2f6
MD
264 sysclock_t cputicks;
265 struct proc *p;
266 struct pstats *pstats;
267 struct globaldata *gd = mycpu;
984263bc
MD
268
269 /*
88c4d2f6
MD
270 * Realtime updates are per-cpu. Note that timer corrections as
271 * returned by microtime() and friends make an additional adjustment
272 * using a system-wise 'basetime', but the running time is always
273 * taken from the per-cpu globaldata area. Since the same clock
274 * is distributing (XXX SMP) to all cpus, the per-cpu timebases
275 * stay in synch.
276 *
277 * Note that we never allow info->time (aka gd->gd_hardclock.time)
fad57d0e
MD
278 * to reverse index gd_cpuclock_base, but that it is possible for
279 * it to temporarily get behind in the seconds if something in the
280 * system locks interrupts for a long period of time. Since periodic
281 * timers count events, though everything should resynch again
282 * immediately.
984263bc 283 */
88c4d2f6 284 cputicks = info->time - gd->gd_cpuclock_base;
fad57d0e 285 if (cputicks >= cputimer_freq) {
88c4d2f6
MD
286 ++gd->gd_time_seconds;
287 gd->gd_cpuclock_base += cputimer_freq;
288 }
984263bc
MD
289
290 /*
92b561b7
MD
291 * The system-wide ticks counter and NTP related timedelta/tickdelta
292 * adjustments only occur on cpu #0. NTP adjustments are accomplished
293 * by updating basetime.
984263bc 294 */
88c4d2f6
MD
295 if (gd->gd_cpuid == 0) {
296 struct timespec nts;
297 int leap;
984263bc 298
88c4d2f6 299 ++ticks;
984263bc 300
88c4d2f6
MD
301#ifdef DEVICE_POLLING
302 hardclock_device_poll(); /* mpsafe, short and quick */
303#endif /* DEVICE_POLLING */
984263bc 304
88c4d2f6
MD
305#if 0
306 if (tco->tc_poll_pps)
307 tco->tc_poll_pps(tco);
308#endif
309 /*
310 * Apply adjtime corrections. At the moment only do this if
311 * we can get the MP lock to interlock with adjtime's modification
312 * of these variables. Note that basetime adjustments are not
313 * MP safe either XXX.
314 */
4026c000
JS
315 if (ntp_delta != 0) {
316 basetime.tv_nsec += ntp_tick_delta;
317 ntp_delta -= ntp_tick_delta;
318 if ((ntp_delta > 0 && ntp_delta < ntp_tick_delta) ||
319 (ntp_delta < 0 && ntp_delta > ntp_tick_delta)) {
320 ntp_tick_delta = ntp_delta;
321 }
322 }
323
324 if (ntp_tick_permanent != 0) {
325 ntp_tick_acc += ntp_tick_permanent;
326 if (ntp_tick_acc >= (1LL << 32)) {
327 basetime.tv_nsec += (-ntp_tick_acc) >> 32;
328 ntp_tick_acc &= (1LL << 32) - 1;
329 } else if (ntp_tick_acc <= -(1LL << 32)) {
330 basetime.tv_nsec -= (-ntp_tick_acc) >> 32;
331 ntp_tick_acc = -((-ntp_tick_acc) & ((1LL << 32) - 1));
332 }
333 }
334
335 if (basetime.tv_nsec >= 1000000000) {
336 basetime.tv_sec++;
88c4d2f6 337 basetime.tv_nsec -= 1000000000;
4026c000
JS
338 } else if (basetime.tv_nsec < 0) {
339 basetime.tv_sec--;
88c4d2f6 340 basetime.tv_nsec += 1000000000;
4026c000
JS
341 }
342
48590578 343 if (ntp_leap_second) {
4026c000
JS
344 struct timespec tsp;
345 nanotime(&tsp);
346
48590578
JS
347 if (ntp_leap_second == tsp.tv_sec) {
348 if (ntp_leap_insert)
4026c000
JS
349 basetime.tv_sec++;
350 else
351 basetime.tv_sec--;
48590578 352 ntp_leap_second--;
88c4d2f6 353 }
88c4d2f6
MD
354 }
355
356 /*
357 * Apply per-tick compensation. ticks_adj adjusts for both
358 * offset and frequency, and could be negative.
359 */
360 if (nsec_adj != 0 && try_mplock()) {
361 nsec_acc += nsec_adj;
362 if (nsec_acc >= 0x100000000LL) {
363 basetime.tv_nsec += nsec_acc >> 32;
364 nsec_acc = (nsec_acc & 0xFFFFFFFFLL);
365 } else if (nsec_acc <= -0x100000000LL) {
366 basetime.tv_nsec -= -nsec_acc >> 32;
367 nsec_acc = -(-nsec_acc & 0xFFFFFFFFLL);
368 }
369 if (basetime.tv_nsec >= 1000000000) {
370 basetime.tv_nsec -= 1000000000;
371 ++basetime.tv_sec;
372 } else if (basetime.tv_nsec < 0) {
373 basetime.tv_nsec += 1000000000;
374 --basetime.tv_sec;
375 }
376 rel_mplock();
377 }
378
379 /*
380 * If the realtime-adjusted seconds hand rolls over then tell
381 * ntp_update_second() what we did in the last second so it can
382 * calculate what to do in the next second. It may also add
383 * or subtract a leap second.
384 */
385 getnanotime(&nts);
386 if (time_second != nts.tv_sec) {
387 leap = ntp_update_second(time_second, &nsec_adj);
388 basetime.tv_sec += leap;
389 time_second = nts.tv_sec + leap;
390 nsec_adj /= hz;
391 }
392 }
393
92b561b7
MD
394 /*
395 * softticks are handled for all cpus
396 */
397 hardclock_softtick(gd);
398
88c4d2f6
MD
399 /*
400 * ITimer handling is per-tick, per-cpu. I don't think psignal()
401 * is mpsafe on curproc, so XXX get the mplock.
402 */
403 if ((p = curproc) != NULL && try_mplock()) {
984263bc 404 pstats = p->p_stats;
88c4d2f6 405 if (frame && CLKF_USERMODE(frame) &&
984263bc
MD
406 timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
407 itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0)
408 psignal(p, SIGVTALRM);
409 if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
410 itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0)
411 psignal(p, SIGPROF);
88c4d2f6 412 rel_mplock();
984263bc 413 }
604e1e09 414 setdelayed();
88c4d2f6 415}
984263bc 416
88c4d2f6
MD
417/*
418 * The statistics clock typically runs at a 125Hz rate, and is intended
419 * to be frequency offset from the hardclock (typ 100Hz). It is per-cpu.
420 *
421 * NOTE! systimer! the MP lock might not be held here. We can only safely
422 * manipulate objects owned by the current cpu.
423 *
424 * The stats clock is responsible for grabbing a profiling sample.
425 * Most of the statistics are only used by user-level statistics programs.
426 * The main exceptions are p->p_uticks, p->p_sticks, p->p_iticks, and
427 * p->p_estcpu.
428 *
429 * Like the other clocks, the stat clock is called from what is effectively
430 * a fast interrupt, so the context should be the thread/process that got
431 * interrupted.
432 */
433static void
434statclock(systimer_t info, struct intrframe *frame)
435{
436#ifdef GPROF
437 struct gmonparam *g;
438 int i;
984263bc 439#endif
88c4d2f6
MD
440 thread_t td;
441 struct proc *p;
442 int bump;
443 struct timeval tv;
444 struct timeval *stv;
984263bc
MD
445
446 /*
88c4d2f6 447 * How big was our timeslice relative to the last time?
984263bc 448 */
88c4d2f6
MD
449 microuptime(&tv); /* mpsafe */
450 stv = &mycpu->gd_stattv;
451 if (stv->tv_sec == 0) {
452 bump = 1;
453 } else {
454 bump = tv.tv_usec - stv->tv_usec +
455 (tv.tv_sec - stv->tv_sec) * 1000000;
456 if (bump < 0)
457 bump = 0;
458 if (bump > 1000000)
459 bump = 1000000;
460 }
461 *stv = tv;
984263bc 462
88c4d2f6
MD
463 td = curthread;
464 p = td->td_proc;
984263bc 465
88c4d2f6
MD
466 if (frame && CLKF_USERMODE(frame)) {
467 /*
468 * Came from userland, handle user time and deal with
469 * possible process.
470 */
471 if (p && (p->p_flag & P_PROFIL))
472 addupc_intr(p, CLKF_PC(frame), 1);
473 td->td_uticks += bump;
984263bc 474
88c4d2f6
MD
475 /*
476 * Charge the time as appropriate
477 */
478 if (p && p->p_nice > NZERO)
f5d21610 479 cp_time.cp_nice += bump;
88c4d2f6 480 else
f5d21610 481 cp_time.cp_user += bump;
88c4d2f6
MD
482 } else {
483#ifdef GPROF
484 /*
485 * Kernel statistics are just like addupc_intr, only easier.
486 */
487 g = &_gmonparam;
488 if (g->state == GMON_PROF_ON && frame) {
489 i = CLKF_PC(frame) - g->lowpc;
490 if (i < g->textsize) {
491 i /= HISTFRACTION * sizeof(*g->kcount);
492 g->kcount[i]++;
493 }
494 }
495#endif
496 /*
497 * Came from kernel mode, so we were:
498 * - handling an interrupt,
499 * - doing syscall or trap work on behalf of the current
500 * user process, or
501 * - spinning in the idle loop.
502 * Whichever it is, charge the time as appropriate.
503 * Note that we charge interrupts to the current process,
504 * regardless of whether they are ``for'' that process,
505 * so that we know how much of its real time was spent
506 * in ``non-process'' (i.e., interrupt) work.
507 *
508 * XXX assume system if frame is NULL. A NULL frame
509 * can occur if ipi processing is done from an splx().
510 */
511 if (frame && CLKF_INTR(frame))
512 td->td_iticks += bump;
513 else
514 td->td_sticks += bump;
515
516 if (frame && CLKF_INTR(frame)) {
f5d21610 517 cp_time.cp_intr += bump;
88c4d2f6
MD
518 } else {
519 if (td == &mycpu->gd_idlethread)
f5d21610 520 cp_time.cp_idle += bump;
88c4d2f6 521 else
f5d21610 522 cp_time.cp_sys += bump;
88c4d2f6
MD
523 }
524 }
525}
526
527/*
0a3f9b47 528 * The scheduler clock typically runs at a 20Hz rate. NOTE! systimer,
88c4d2f6
MD
529 * the MP lock might not be held. We can safely manipulate parts of curproc
530 * but that's about it.
531 */
532static void
533schedclock(systimer_t info, struct intrframe *frame)
534{
535 struct proc *p;
536 struct pstats *pstats;
537 struct rusage *ru;
538 struct vmspace *vm;
539 long rss;
540
541 schedulerclock(NULL); /* mpsafe */
542 if ((p = curproc) != NULL) {
543 /* Update resource usage integrals and maximums. */
544 if ((pstats = p->p_stats) != NULL &&
545 (ru = &pstats->p_ru) != NULL &&
546 (vm = p->p_vmspace) != NULL) {
547 ru->ru_ixrss += pgtok(vm->vm_tsize);
548 ru->ru_idrss += pgtok(vm->vm_dsize);
549 ru->ru_isrss += pgtok(vm->vm_ssize);
550 rss = pgtok(vmspace_resident_count(vm));
551 if (ru->ru_maxrss < rss)
552 ru->ru_maxrss = rss;
553 }
b68b7282 554 }
984263bc
MD
555}
556
557/*
a94976ad
MD
558 * Compute number of ticks for the specified amount of time. The
559 * return value is intended to be used in a clock interrupt timed
560 * operation and guarenteed to meet or exceed the requested time.
561 * If the representation overflows, return INT_MAX. The minimum return
562 * value is 1 ticks and the function will average the calculation up.
563 * If any value greater then 0 microseconds is supplied, a value
564 * of at least 2 will be returned to ensure that a near-term clock
565 * interrupt does not cause the timeout to occur (degenerately) early.
566 *
567 * Note that limit checks must take into account microseconds, which is
568 * done simply by using the smaller signed long maximum instead of
569 * the unsigned long maximum.
570 *
571 * If ints have 32 bits, then the maximum value for any timeout in
572 * 10ms ticks is 248 days.
984263bc
MD
573 */
574int
a94976ad 575tvtohz_high(struct timeval *tv)
984263bc 576{
a94976ad 577 int ticks;
1fd87d54 578 long sec, usec;
984263bc 579
984263bc
MD
580 sec = tv->tv_sec;
581 usec = tv->tv_usec;
582 if (usec < 0) {
583 sec--;
584 usec += 1000000;
585 }
586 if (sec < 0) {
587#ifdef DIAGNOSTIC
588 if (usec > 0) {
589 sec++;
590 usec -= 1000000;
591 }
592 printf("tvotohz: negative time difference %ld sec %ld usec\n",
593 sec, usec);
594#endif
595 ticks = 1;
a94976ad
MD
596 } else if (sec <= INT_MAX / hz) {
597 ticks = (int)(sec * hz +
598 ((u_long)usec + (tick - 1)) / tick) + 1;
599 } else {
600 ticks = INT_MAX;
601 }
602 return (ticks);
603}
604
605/*
606 * Compute number of ticks for the specified amount of time, erroring on
607 * the side of it being too low to ensure that sleeping the returned number
608 * of ticks will not result in a late return.
609 *
610 * The supplied timeval may not be negative and should be normalized. A
611 * return value of 0 is possible if the timeval converts to less then
612 * 1 tick.
613 *
614 * If ints have 32 bits, then the maximum value for any timeout in
615 * 10ms ticks is 248 days.
616 */
617int
618tvtohz_low(struct timeval *tv)
619{
620 int ticks;
621 long sec;
622
623 sec = tv->tv_sec;
624 if (sec <= INT_MAX / hz)
625 ticks = (int)(sec * hz + (u_long)tv->tv_usec / tick);
984263bc 626 else
984263bc 627 ticks = INT_MAX;
a94976ad 628 return (ticks);
984263bc
MD
629}
630
a94976ad 631
984263bc
MD
632/*
633 * Start profiling on a process.
634 *
635 * Kernel profiling passes proc0 which never exits and hence
636 * keeps the profile clock running constantly.
637 */
638void
88c4d2f6 639startprofclock(struct proc *p)
984263bc 640{
984263bc
MD
641 if ((p->p_flag & P_PROFIL) == 0) {
642 p->p_flag |= P_PROFIL;
88c4d2f6 643#if 0 /* XXX */
984263bc
MD
644 if (++profprocs == 1 && stathz != 0) {
645 s = splstatclock();
6ad39cae 646 psdiv = psratio;
984263bc
MD
647 setstatclockrate(profhz);
648 splx(s);
649 }
88c4d2f6 650#endif
984263bc
MD
651 }
652}
653
654/*
655 * Stop profiling on a process.
656 */
657void
88c4d2f6 658stopprofclock(struct proc *p)
984263bc 659{
984263bc
MD
660 if (p->p_flag & P_PROFIL) {
661 p->p_flag &= ~P_PROFIL;
88c4d2f6 662#if 0 /* XXX */
984263bc
MD
663 if (--profprocs == 0 && stathz != 0) {
664 s = splstatclock();
6ad39cae 665 psdiv = 1;
984263bc
MD
666 setstatclockrate(stathz);
667 splx(s);
668 }
984263bc 669#endif
984263bc
MD
670 }
671}
672
673/*
674 * Return information about system clocks.
675 */
676static int
677sysctl_kern_clockrate(SYSCTL_HANDLER_ARGS)
678{
f5d21610 679 struct kinfo_clockinfo clkinfo;
984263bc
MD
680 /*
681 * Construct clockinfo structure.
682 */
f5d21610
JS
683 clkinfo.ci_hz = hz;
684 clkinfo.ci_tick = tick;
4026c000 685 clkinfo.ci_tickadj = ntp_default_tick_delta / 1000;
f5d21610
JS
686 clkinfo.ci_profhz = profhz;
687 clkinfo.ci_stathz = stathz ? stathz : hz;
984263bc
MD
688 return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req));
689}
690
691SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, CTLTYPE_STRUCT|CTLFLAG_RD,
692 0, 0, sysctl_kern_clockrate, "S,clockinfo","");
693
984263bc
MD
694/*
695 * We have eight functions for looking at the clock, four for
696 * microseconds and four for nanoseconds. For each there is fast
697 * but less precise version "get{nano|micro}[up]time" which will
698 * return a time which is up to 1/HZ previous to the call, whereas
699 * the raw version "{nano|micro}[up]time" will return a timestamp
700 * which is as precise as possible. The "up" variants return the
701 * time relative to system boot, these are well suited for time
702 * interval measurements.
88c4d2f6
MD
703 *
704 * Each cpu independantly maintains the current time of day, so all
705 * we need to do to protect ourselves from changes is to do a loop
706 * check on the seconds field changing out from under us.
fad57d0e
MD
707 *
708 * The system timer maintains a 32 bit count and due to various issues
709 * it is possible for the calculated delta to occassionally exceed
710 * cputimer_freq. If this occurs the cputimer_freq64_nsec multiplication
711 * can easily overflow, so we deal with the case. For uniformity we deal
712 * with the case in the usec case too.
984263bc 713 */
984263bc
MD
714void
715getmicrouptime(struct timeval *tvp)
716{
88c4d2f6
MD
717 struct globaldata *gd = mycpu;
718 sysclock_t delta;
719
720 do {
721 tvp->tv_sec = gd->gd_time_seconds;
722 delta = gd->gd_hardclock.time - gd->gd_cpuclock_base;
723 } while (tvp->tv_sec != gd->gd_time_seconds);
fad57d0e
MD
724
725 if (delta >= cputimer_freq) {
726 tvp->tv_sec += delta / cputimer_freq;
727 delta %= cputimer_freq;
728 }
88c4d2f6
MD
729 tvp->tv_usec = (cputimer_freq64_usec * delta) >> 32;
730 if (tvp->tv_usec >= 1000000) {
731 tvp->tv_usec -= 1000000;
732 ++tvp->tv_sec;
984263bc
MD
733 }
734}
735
736void
737getnanouptime(struct timespec *tsp)
738{
88c4d2f6
MD
739 struct globaldata *gd = mycpu;
740 sysclock_t delta;
741
742 do {
743 tsp->tv_sec = gd->gd_time_seconds;
744 delta = gd->gd_hardclock.time - gd->gd_cpuclock_base;
745 } while (tsp->tv_sec != gd->gd_time_seconds);
fad57d0e
MD
746
747 if (delta >= cputimer_freq) {
748 tsp->tv_sec += delta / cputimer_freq;
749 delta %= cputimer_freq;
984263bc 750 }
fad57d0e 751 tsp->tv_nsec = (cputimer_freq64_nsec * delta) >> 32;
984263bc
MD
752}
753
754void
88c4d2f6 755microuptime(struct timeval *tvp)
984263bc 756{
88c4d2f6
MD
757 struct globaldata *gd = mycpu;
758 sysclock_t delta;
759
760 do {
761 tvp->tv_sec = gd->gd_time_seconds;
762 delta = cputimer_count() - gd->gd_cpuclock_base;
763 } while (tvp->tv_sec != gd->gd_time_seconds);
fad57d0e
MD
764
765 if (delta >= cputimer_freq) {
766 tvp->tv_sec += delta / cputimer_freq;
767 delta %= cputimer_freq;
984263bc 768 }
fad57d0e 769 tvp->tv_usec = (cputimer_freq64_usec * delta) >> 32;
984263bc
MD
770}
771
772void
88c4d2f6 773nanouptime(struct timespec *tsp)
984263bc 774{
88c4d2f6
MD
775 struct globaldata *gd = mycpu;
776 sysclock_t delta;
777
778 do {
779 tsp->tv_sec = gd->gd_time_seconds;
780 delta = cputimer_count() - gd->gd_cpuclock_base;
781 } while (tsp->tv_sec != gd->gd_time_seconds);
fad57d0e
MD
782
783 if (delta >= cputimer_freq) {
784 tsp->tv_sec += delta / cputimer_freq;
785 delta %= cputimer_freq;
984263bc 786 }
fad57d0e 787 tsp->tv_nsec = (cputimer_freq64_nsec * delta) >> 32;
984263bc
MD
788}
789
88c4d2f6
MD
790/*
791 * realtime routines
792 */
984263bc
MD
793
794void
88c4d2f6 795getmicrotime(struct timeval *tvp)
984263bc 796{
88c4d2f6
MD
797 struct globaldata *gd = mycpu;
798 sysclock_t delta;
984263bc 799
88c4d2f6
MD
800 do {
801 tvp->tv_sec = gd->gd_time_seconds;
802 delta = gd->gd_hardclock.time - gd->gd_cpuclock_base;
803 } while (tvp->tv_sec != gd->gd_time_seconds);
fad57d0e
MD
804
805 if (delta >= cputimer_freq) {
806 tvp->tv_sec += delta / cputimer_freq;
807 delta %= cputimer_freq;
808 }
88c4d2f6 809 tvp->tv_usec = (cputimer_freq64_usec * delta) >> 32;
984263bc 810
88c4d2f6
MD
811 tvp->tv_sec += basetime.tv_sec;
812 tvp->tv_usec += basetime.tv_nsec / 1000;
813 while (tvp->tv_usec >= 1000000) {
814 tvp->tv_usec -= 1000000;
815 ++tvp->tv_sec;
984263bc 816 }
984263bc
MD
817}
818
819void
88c4d2f6 820getnanotime(struct timespec *tsp)
984263bc 821{
88c4d2f6
MD
822 struct globaldata *gd = mycpu;
823 sysclock_t delta;
984263bc 824
88c4d2f6
MD
825 do {
826 tsp->tv_sec = gd->gd_time_seconds;
827 delta = gd->gd_hardclock.time - gd->gd_cpuclock_base;
828 } while (tsp->tv_sec != gd->gd_time_seconds);
fad57d0e
MD
829
830 if (delta >= cputimer_freq) {
831 tsp->tv_sec += delta / cputimer_freq;
832 delta %= cputimer_freq;
833 }
88c4d2f6 834 tsp->tv_nsec = (cputimer_freq64_nsec * delta) >> 32;
984263bc 835
88c4d2f6
MD
836 tsp->tv_sec += basetime.tv_sec;
837 tsp->tv_nsec += basetime.tv_nsec;
838 while (tsp->tv_nsec >= 1000000000) {
839 tsp->tv_nsec -= 1000000000;
840 ++tsp->tv_sec;
984263bc 841 }
984263bc
MD
842}
843
88c4d2f6
MD
844void
845microtime(struct timeval *tvp)
984263bc 846{
88c4d2f6
MD
847 struct globaldata *gd = mycpu;
848 sysclock_t delta;
984263bc 849
88c4d2f6
MD
850 do {
851 tvp->tv_sec = gd->gd_time_seconds;
852 delta = cputimer_count() - gd->gd_cpuclock_base;
853 } while (tvp->tv_sec != gd->gd_time_seconds);
fad57d0e
MD
854
855 if (delta >= cputimer_freq) {
856 tvp->tv_sec += delta / cputimer_freq;
857 delta %= cputimer_freq;
858 }
88c4d2f6 859 tvp->tv_usec = (cputimer_freq64_usec * delta) >> 32;
984263bc 860
88c4d2f6
MD
861 tvp->tv_sec += basetime.tv_sec;
862 tvp->tv_usec += basetime.tv_nsec / 1000;
863 while (tvp->tv_usec >= 1000000) {
864 tvp->tv_usec -= 1000000;
865 ++tvp->tv_sec;
984263bc 866 }
984263bc
MD
867}
868
88c4d2f6
MD
869void
870nanotime(struct timespec *tsp)
871{
872 struct globaldata *gd = mycpu;
873 sysclock_t delta;
984263bc 874
88c4d2f6
MD
875 do {
876 tsp->tv_sec = gd->gd_time_seconds;
877 delta = cputimer_count() - gd->gd_cpuclock_base;
878 } while (tsp->tv_sec != gd->gd_time_seconds);
fad57d0e
MD
879
880 if (delta >= cputimer_freq) {
881 tsp->tv_sec += delta / cputimer_freq;
882 delta %= cputimer_freq;
883 }
88c4d2f6 884 tsp->tv_nsec = (cputimer_freq64_nsec * delta) >> 32;
984263bc 885
88c4d2f6
MD
886 tsp->tv_sec += basetime.tv_sec;
887 tsp->tv_nsec += basetime.tv_nsec;
888 while (tsp->tv_nsec >= 1000000000) {
889 tsp->tv_nsec -= 1000000000;
890 ++tsp->tv_sec;
984263bc 891 }
984263bc
MD
892}
893
25b804e7
MD
894/*
895 * note: this is not exactly synchronized with real time. To do that we
896 * would have to do what microtime does and check for a nanoseconds overflow.
897 */
898time_t
899get_approximate_time_t(void)
900{
901 struct globaldata *gd = mycpu;
902 return(gd->gd_time_seconds + basetime.tv_sec);
903}
904
984263bc
MD
905int
906pps_ioctl(u_long cmd, caddr_t data, struct pps_state *pps)
907{
908 pps_params_t *app;
909 struct pps_fetch_args *fapi;
910#ifdef PPS_SYNC
911 struct pps_kcbind_args *kapi;
912#endif
913
914 switch (cmd) {
915 case PPS_IOC_CREATE:
916 return (0);
917 case PPS_IOC_DESTROY:
918 return (0);
919 case PPS_IOC_SETPARAMS:
920 app = (pps_params_t *)data;
921 if (app->mode & ~pps->ppscap)
922 return (EINVAL);
923 pps->ppsparam = *app;
924 return (0);
925 case PPS_IOC_GETPARAMS:
926 app = (pps_params_t *)data;
927 *app = pps->ppsparam;
928 app->api_version = PPS_API_VERS_1;
929 return (0);
930 case PPS_IOC_GETCAP:
931 *(int*)data = pps->ppscap;
932 return (0);
933 case PPS_IOC_FETCH:
934 fapi = (struct pps_fetch_args *)data;
935 if (fapi->tsformat && fapi->tsformat != PPS_TSFMT_TSPEC)
936 return (EINVAL);
937 if (fapi->timeout.tv_sec || fapi->timeout.tv_nsec)
938 return (EOPNOTSUPP);
939 pps->ppsinfo.current_mode = pps->ppsparam.mode;
940 fapi->pps_info_buf = pps->ppsinfo;
941 return (0);
942 case PPS_IOC_KCBIND:
943#ifdef PPS_SYNC
944 kapi = (struct pps_kcbind_args *)data;
945 /* XXX Only root should be able to do this */
946 if (kapi->tsformat && kapi->tsformat != PPS_TSFMT_TSPEC)
947 return (EINVAL);
948 if (kapi->kernel_consumer != PPS_KC_HARDPPS)
949 return (EINVAL);
950 if (kapi->edge & ~pps->ppscap)
951 return (EINVAL);
952 pps->kcmode = kapi->edge;
953 return (0);
954#else
955 return (EOPNOTSUPP);
956#endif
957 default:
958 return (ENOTTY);
959 }
960}
961
962void
963pps_init(struct pps_state *pps)
964{
965 pps->ppscap |= PPS_TSFMT_TSPEC;
966 if (pps->ppscap & PPS_CAPTUREASSERT)
967 pps->ppscap |= PPS_OFFSETASSERT;
968 if (pps->ppscap & PPS_CAPTURECLEAR)
969 pps->ppscap |= PPS_OFFSETCLEAR;
970}
971
972void
88c4d2f6 973pps_event(struct pps_state *pps, sysclock_t count, int event)
984263bc 974{
88c4d2f6
MD
975 struct globaldata *gd;
976 struct timespec *tsp;
977 struct timespec *osp;
978 struct timespec ts;
979 sysclock_t *pcount;
980#ifdef PPS_SYNC
981 sysclock_t tcount;
982#endif
983 sysclock_t delta;
984 pps_seq_t *pseq;
985 int foff;
986 int fhard;
987
988 gd = mycpu;
984263bc
MD
989
990 /* Things would be easier with arrays... */
991 if (event == PPS_CAPTUREASSERT) {
992 tsp = &pps->ppsinfo.assert_timestamp;
993 osp = &pps->ppsparam.assert_offset;
994 foff = pps->ppsparam.mode & PPS_OFFSETASSERT;
995 fhard = pps->kcmode & PPS_CAPTUREASSERT;
996 pcount = &pps->ppscount[0];
997 pseq = &pps->ppsinfo.assert_sequence;
998 } else {
999 tsp = &pps->ppsinfo.clear_timestamp;
1000 osp = &pps->ppsparam.clear_offset;
1001 foff = pps->ppsparam.mode & PPS_OFFSETCLEAR;
1002 fhard = pps->kcmode & PPS_CAPTURECLEAR;
1003 pcount = &pps->ppscount[1];
1004 pseq = &pps->ppsinfo.clear_sequence;
1005 }
1006
984263bc
MD
1007 /* Nothing really happened */
1008 if (*pcount == count)
1009 return;
1010
1011 *pcount = count;
1012
88c4d2f6
MD
1013 do {
1014 ts.tv_sec = gd->gd_time_seconds;
1015 delta = count - gd->gd_cpuclock_base;
1016 } while (ts.tv_sec != gd->gd_time_seconds);
fad57d0e
MD
1017
1018 if (delta >= cputimer_freq) {
88c4d2f6
MD
1019 ts.tv_sec += delta / cputimer_freq;
1020 delta %= cputimer_freq;
1021 }
1022 ts.tv_nsec = (cputimer_freq64_nsec * delta) >> 32;
1023 ts.tv_sec += basetime.tv_sec;
1024 ts.tv_nsec += basetime.tv_nsec;
1025 while (ts.tv_nsec >= 1000000000) {
1026 ts.tv_nsec -= 1000000000;
1027 ++ts.tv_sec;
984263bc 1028 }
984263bc
MD
1029
1030 (*pseq)++;
1031 *tsp = ts;
1032
1033 if (foff) {
1034 timespecadd(tsp, osp);
1035 if (tsp->tv_nsec < 0) {
1036 tsp->tv_nsec += 1000000000;
1037 tsp->tv_sec -= 1;
1038 }
1039 }
1040#ifdef PPS_SYNC
1041 if (fhard) {
1042 /* magic, at its best... */
1043 tcount = count - pps->ppscount[2];
1044 pps->ppscount[2] = count;
fad57d0e 1045 if (tcount >= cputimer_freq) {
64642171
JS
1046 delta = (1000000000 * (tcount / cputimer_freq) +
1047 cputimer_freq64_nsec *
fad57d0e
MD
1048 (tcount % cputimer_freq)) >> 32;
1049 } else {
1050 delta = (cputimer_freq64_nsec * tcount) >> 32;
1051 }
984263bc
MD
1052 hardpps(tsp, delta);
1053 }
1054#endif
1055}
88c4d2f6 1056