2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)kern_time.c 8.1 (Berkeley) 6/10/93
30 * $FreeBSD: src/sys/kern/kern_time.c,v 1.68.2.1 2002/10/01 08:00:41 bde Exp $
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/sysproto.h>
37 #include <sys/resourcevar.h>
38 #include <sys/signalvar.h>
39 #include <sys/kernel.h>
40 #include <sys/sysent.h>
41 #include <sys/sysunion.h>
45 #include <sys/vnode.h>
46 #include <sys/sysctl.h>
47 #include <sys/kern_syscall.h>
49 #include <vm/vm_extern.h>
51 #include <sys/msgport2.h>
52 #include <sys/spinlock2.h>
53 #include <sys/thread2.h>
55 extern struct spinlock ntp_spin;
60 * Time of day and interval timer support.
62 * These routines provide the kernel entry points to get and set
63 * the time-of-day and per-process interval timers. Subroutines
64 * here provide support for adding and subtracting timeval structures
65 * and decrementing interval timers, optionally reloading the interval
66 * timers when they expire.
69 static int settime(struct timeval *);
70 static void timevalfix(struct timeval *);
71 static void realitexpire(void *arg);
74 * Nanosleep tries very hard to sleep for a precisely requested time
75 * interval, down to 1uS. The administrator can impose a minimum delay
76 * and a delay below which we hard-loop instead of initiate a timer
77 * interrupt and sleep.
79 * For machines under high loads it might be beneficial to increase min_us
80 * to e.g. 1000uS (1ms) so spining processes sleep meaningfully.
82 static int nanosleep_min_us = 10;
83 static int nanosleep_hard_us = 100;
84 static int gettimeofday_quick = 0;
85 SYSCTL_INT(_kern, OID_AUTO, nanosleep_min_us, CTLFLAG_RW,
86 &nanosleep_min_us, 0, "");
87 SYSCTL_INT(_kern, OID_AUTO, nanosleep_hard_us, CTLFLAG_RW,
88 &nanosleep_hard_us, 0, "");
89 SYSCTL_INT(_kern, OID_AUTO, gettimeofday_quick, CTLFLAG_RW,
90 &gettimeofday_quick, 0, "");
92 static struct lock masterclock_lock = LOCK_INITIALIZER("mstrclk", 0, 0);
95 settime(struct timeval *tv)
97 struct timeval delta, tv1, tv2;
98 static struct timeval maxtime, laststep;
102 if ((origcpu = mycpu->gd_cpuid) != 0)
103 lwkt_setcpu_self(globaldata_find(0));
108 timevalsub(&delta, &tv1);
111 * If the system is secure, we do not allow the time to be
112 * set to a value earlier than 1 second less than the highest
113 * time we have yet seen. The worst a miscreant can do in
114 * this circumstance is "freeze" time. He couldn't go
117 * We similarly do not allow the clock to be stepped more
118 * than one second, nor more than once per second. This allows
119 * a miscreant to make the clock march double-time, but no worse.
121 if (securelevel > 1) {
122 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
124 * Update maxtime to latest time we've seen.
126 if (tv1.tv_sec > maxtime.tv_sec)
129 timevalsub(&tv2, &maxtime);
130 if (tv2.tv_sec < -1) {
131 tv->tv_sec = maxtime.tv_sec - 1;
132 kprintf("Time adjustment clamped to -1 second\n");
135 if (tv1.tv_sec == laststep.tv_sec) {
139 if (delta.tv_sec > 1) {
140 tv->tv_sec = tv1.tv_sec + 1;
141 kprintf("Time adjustment clamped to +1 second\n");
147 ts.tv_sec = tv->tv_sec;
148 ts.tv_nsec = tv->tv_usec * 1000;
153 lwkt_setcpu_self(globaldata_find(origcpu));
160 get_process_cputime(struct proc *p, struct timespec *ats)
164 lwkt_gettoken(&p->p_token);
166 lwkt_reltoken(&p->p_token);
167 timevaladd(&ru.ru_utime, &ru.ru_stime);
168 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, ats);
172 get_process_usertime(struct proc *p, struct timespec *ats)
176 lwkt_gettoken(&p->p_token);
178 lwkt_reltoken(&p->p_token);
179 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, ats);
183 get_thread_cputime(struct thread *td, struct timespec *ats)
185 struct timeval sys, user;
187 calcru(td->td_lwp, &user, &sys);
188 timevaladd(&user, &sys);
189 TIMEVAL_TO_TIMESPEC(&user, ats);
196 kern_clock_gettime(clockid_t clock_id, struct timespec *ats)
203 case CLOCK_REALTIME_PRECISE:
206 case CLOCK_REALTIME_FAST:
209 case CLOCK_MONOTONIC:
210 case CLOCK_MONOTONIC_PRECISE:
212 case CLOCK_UPTIME_PRECISE:
215 case CLOCK_MONOTONIC_FAST:
216 case CLOCK_UPTIME_FAST:
220 get_process_usertime(p, ats);
223 case CLOCK_PROCESS_CPUTIME_ID:
224 get_process_cputime(p, ats);
227 ats->tv_sec = time_second;
230 case CLOCK_THREAD_CPUTIME_ID:
231 get_thread_cputime(curthread, ats);
243 sys_clock_gettime(struct clock_gettime_args *uap)
248 error = kern_clock_gettime(uap->clock_id, &ats);
250 error = copyout(&ats, uap->tp, sizeof(ats));
256 kern_clock_settime(clockid_t clock_id, struct timespec *ats)
258 struct thread *td = curthread;
262 if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
264 if (clock_id != CLOCK_REALTIME)
266 if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
269 lockmgr(&masterclock_lock, LK_EXCLUSIVE);
270 TIMESPEC_TO_TIMEVAL(&atv, ats);
271 error = settime(&atv);
272 lockmgr(&masterclock_lock, LK_RELEASE);
281 sys_clock_settime(struct clock_settime_args *uap)
286 if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
289 error = kern_clock_settime(uap->clock_id, &ats);
298 kern_clock_getres(clockid_t clock_id, struct timespec *ts)
303 case CLOCK_REALTIME_FAST:
304 case CLOCK_REALTIME_PRECISE:
305 case CLOCK_MONOTONIC:
306 case CLOCK_MONOTONIC_FAST:
307 case CLOCK_MONOTONIC_PRECISE:
309 case CLOCK_UPTIME_FAST:
310 case CLOCK_UPTIME_PRECISE:
312 * Round up the result of the division cheaply
313 * by adding 1. Rounding up is especially important
314 * if rounding down would give 0. Perfect rounding
317 ts->tv_nsec = 1000000000 / sys_cputimer->freq + 1;
321 /* Accurately round up here because we can do so cheaply. */
322 ts->tv_nsec = (1000000000 + hz - 1) / hz;
328 case CLOCK_THREAD_CPUTIME_ID:
329 case CLOCK_PROCESS_CPUTIME_ID:
343 sys_clock_getres(struct clock_getres_args *uap)
348 error = kern_clock_getres(uap->clock_id, &ts);
350 error = copyout(&ts, uap->tp, sizeof(ts));
358 * This is a general helper function for nanosleep() (aka sleep() aka
361 * If there is less then one tick's worth of time left and
362 * we haven't done a yield, or the remaining microseconds is
363 * ridiculously low, do a yield. This avoids having
364 * to deal with systimer overheads when the system is under
365 * heavy loads. If we have done a yield already then use
366 * a systimer and an uninterruptable thread wait.
368 * If there is more then a tick's worth of time left,
369 * calculate the baseline ticks and use an interruptable
370 * tsleep, then handle the fine-grained delay on the next
371 * loop. This usually results in two sleeps occuring, a long one
377 ns1_systimer(systimer_t info, int in_ipi __unused,
378 struct intrframe *frame __unused)
380 lwkt_schedule(info->data);
384 nanosleep1(struct timespec *rqt, struct timespec *rmt)
387 struct timespec ts, ts2, ts3;
391 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
393 /* XXX: imho this should return EINVAL at least for tv_sec < 0 */
394 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
397 timespecadd(&ts, rqt); /* ts = target timestamp compare */
398 TIMESPEC_TO_TIMEVAL(&tv, rqt); /* tv = sleep interval */
402 struct systimer info;
404 ticks = tv.tv_usec / ustick; /* approximate */
406 if (tv.tv_sec == 0 && ticks == 0) {
407 thread_t td = curthread;
408 if (tv.tv_usec > 0 && tv.tv_usec < nanosleep_min_us)
409 tv.tv_usec = nanosleep_min_us;
410 if (tv.tv_usec < nanosleep_hard_us) {
414 crit_enter_quick(td);
415 systimer_init_oneshot(&info, ns1_systimer,
417 lwkt_deschedule_self(td);
420 systimer_del(&info); /* make sure it's gone */
422 error = iscaught(td->td_lwp);
423 } else if (tv.tv_sec == 0) {
424 error = tsleep(&nanowait, PCATCH, "nanslp", ticks);
426 ticks = tvtohz_low(&tv); /* also handles overflow */
427 error = tsleep(&nanowait, PCATCH, "nanslp", ticks);
430 if (error && error != EWOULDBLOCK) {
431 if (error == ERESTART)
434 timespecsub(&ts, &ts2);
441 if (timespeccmp(&ts2, &ts, >=))
444 timespecsub(&ts3, &ts2);
445 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
453 sys_nanosleep(struct nanosleep_args *uap)
459 error = copyin(uap->rqtp, &rqt, sizeof(rqt));
463 error = nanosleep1(&rqt, &rmt);
466 * copyout the residual if nanosleep was interrupted.
468 if (error && uap->rmtp) {
471 error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
479 * The gettimeofday() system call is supposed to return a fine-grained
480 * realtime stamp. However, acquiring a fine-grained stamp can create a
481 * bottleneck when multiple cpu cores are trying to accessing e.g. the
482 * HPET hardware timer all at the same time, so we have a sysctl that
483 * allows its behavior to be changed to a more coarse-grained timestamp
484 * which does not have to access a hardware timer.
487 sys_gettimeofday(struct gettimeofday_args *uap)
493 if (gettimeofday_quick)
497 if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
502 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
511 sys_settimeofday(struct settimeofday_args *uap)
513 struct thread *td = curthread;
518 if ((error = priv_check(td, PRIV_SETTIMEOFDAY)))
521 * Verify all parameters before changing time.
523 * XXX: We do not allow the time to be set to 0.0, which also by
524 * happy coincidence works around a pkgsrc bulk build bug.
527 if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
530 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
532 if (atv.tv_sec == 0 && atv.tv_usec == 0)
536 (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
539 lockmgr(&masterclock_lock, LK_EXCLUSIVE);
540 if (uap->tv && (error = settime(&atv))) {
541 lockmgr(&masterclock_lock, LK_RELEASE);
544 lockmgr(&masterclock_lock, LK_RELEASE);
552 * WARNING! Run with ntp_spin held
555 kern_adjtime_common(void)
557 if ((ntp_delta >= 0 && ntp_delta < ntp_default_tick_delta) ||
558 (ntp_delta < 0 && ntp_delta > -ntp_default_tick_delta))
559 ntp_tick_delta = ntp_delta;
560 else if (ntp_delta > ntp_big_delta)
561 ntp_tick_delta = 10 * ntp_default_tick_delta;
562 else if (ntp_delta < -ntp_big_delta)
563 ntp_tick_delta = -10 * ntp_default_tick_delta;
564 else if (ntp_delta > 0)
565 ntp_tick_delta = ntp_default_tick_delta;
567 ntp_tick_delta = -ntp_default_tick_delta;
571 kern_adjtime(int64_t delta, int64_t *odelta)
573 spin_lock(&ntp_spin);
576 kern_adjtime_common();
577 spin_unlock(&ntp_spin);
581 kern_get_ntp_delta(int64_t *delta)
587 kern_reladjtime(int64_t delta)
589 spin_lock(&ntp_spin);
591 kern_adjtime_common();
592 spin_unlock(&ntp_spin);
596 kern_adjfreq(int64_t rate)
598 spin_lock(&ntp_spin);
599 ntp_tick_permanent = rate;
600 spin_unlock(&ntp_spin);
607 sys_adjtime(struct adjtime_args *uap)
609 struct thread *td = curthread;
611 int64_t ndelta, odelta;
614 if ((error = priv_check(td, PRIV_ADJTIME)))
616 error = copyin(uap->delta, &atv, sizeof(struct timeval));
621 * Compute the total correction and the rate at which to apply it.
622 * Round the adjustment down to a whole multiple of the per-tick
623 * delta, so that after some number of incremental changes in
624 * hardclock(), tickdelta will become zero, lest the correction
625 * overshoot and start taking us away from the desired final time.
627 ndelta = (int64_t)atv.tv_sec * 1000000000 + atv.tv_usec * 1000;
628 kern_adjtime(ndelta, &odelta);
631 atv.tv_sec = odelta / 1000000000;
632 atv.tv_usec = odelta % 1000000000 / 1000;
633 copyout(&atv, uap->olddelta, sizeof(struct timeval));
639 sysctl_adjtime(SYSCTL_HANDLER_ARGS)
644 if (req->newptr != NULL) {
645 if (priv_check(curthread, PRIV_ROOT))
647 error = SYSCTL_IN(req, &delta, sizeof(delta));
650 kern_reladjtime(delta);
654 kern_get_ntp_delta(&delta);
655 error = SYSCTL_OUT(req, &delta, sizeof(delta));
660 * delta is in nanoseconds.
663 sysctl_delta(SYSCTL_HANDLER_ARGS)
665 int64_t delta, old_delta;
668 if (req->newptr != NULL) {
669 if (priv_check(curthread, PRIV_ROOT))
671 error = SYSCTL_IN(req, &delta, sizeof(delta));
674 kern_adjtime(delta, &old_delta);
677 if (req->oldptr != NULL)
678 kern_get_ntp_delta(&old_delta);
679 error = SYSCTL_OUT(req, &old_delta, sizeof(old_delta));
684 * frequency is in nanoseconds per second shifted left 32.
685 * kern_adjfreq() needs it in nanoseconds per tick shifted left 32.
688 sysctl_adjfreq(SYSCTL_HANDLER_ARGS)
693 if (req->newptr != NULL) {
694 if (priv_check(curthread, PRIV_ROOT))
696 error = SYSCTL_IN(req, &freqdelta, sizeof(freqdelta));
701 kern_adjfreq(freqdelta);
704 if (req->oldptr != NULL)
705 freqdelta = ntp_tick_permanent * hz;
706 error = SYSCTL_OUT(req, &freqdelta, sizeof(freqdelta));
713 SYSCTL_NODE(_kern, OID_AUTO, ntp, CTLFLAG_RW, 0, "NTP related controls");
714 SYSCTL_PROC(_kern_ntp, OID_AUTO, permanent,
715 CTLTYPE_QUAD|CTLFLAG_RW, 0, 0,
716 sysctl_adjfreq, "Q", "permanent correction per second");
717 SYSCTL_PROC(_kern_ntp, OID_AUTO, delta,
718 CTLTYPE_QUAD|CTLFLAG_RW, 0, 0,
719 sysctl_delta, "Q", "one-time delta");
720 SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, big_delta, CTLFLAG_RD,
721 &ntp_big_delta, sizeof(ntp_big_delta), "Q",
722 "threshold for fast adjustment");
723 SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, tick_delta, CTLFLAG_RD,
724 &ntp_tick_delta, sizeof(ntp_tick_delta), "LU",
725 "per-tick adjustment");
726 SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, default_tick_delta, CTLFLAG_RD,
727 &ntp_default_tick_delta, sizeof(ntp_default_tick_delta), "LU",
728 "default per-tick adjustment");
729 SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, next_leap_second, CTLFLAG_RW,
730 &ntp_leap_second, sizeof(ntp_leap_second), "LU",
732 SYSCTL_INT(_kern_ntp, OID_AUTO, insert_leap_second, CTLFLAG_RW,
733 &ntp_leap_insert, 0, "insert or remove leap second");
734 SYSCTL_PROC(_kern_ntp, OID_AUTO, adjust,
735 CTLTYPE_QUAD|CTLFLAG_RW, 0, 0,
736 sysctl_adjtime, "Q", "relative adjust for delta");
739 * Get value of an interval timer. The process virtual and
740 * profiling virtual time timers are kept in the p_stats area, since
741 * they can be swapped out. These are kept internally in the
742 * way they are specified externally: in time until they expire.
744 * The real time interval timer is kept in the process table slot
745 * for the process, and its value (it_value) is kept as an
746 * absolute time rather than as a delta, so that it is easy to keep
747 * periodic real-time signals from drifting.
749 * Virtual time timers are processed in the hardclock() routine of
750 * kern_clock.c. The real time timer is processed by a timeout
751 * routine, called from the softclock() routine. Since a callout
752 * may be delayed in real time due to interrupt processing in the system,
753 * it is possible for the real time timeout routine (realitexpire, given below),
754 * to be delayed in real time past when it is supposed to occur. It
755 * does not suffice, therefore, to reload the real timer .it_value from the
756 * real time timers .it_interval. Rather, we compute the next time in
757 * absolute time the timer should go off.
762 sys_getitimer(struct getitimer_args *uap)
764 struct proc *p = curproc;
766 struct itimerval aitv;
768 if (uap->which > ITIMER_PROF)
770 lwkt_gettoken(&p->p_token);
771 if (uap->which == ITIMER_REAL) {
773 * Convert from absolute to relative time in .it_value
774 * part of real time timer. If time for real time timer
775 * has passed return 0, else return difference between
776 * current time and time for the timer to go off.
778 aitv = p->p_realtimer;
779 if (timevalisset(&aitv.it_value)) {
780 getmicrouptime(&ctv);
781 if (timevalcmp(&aitv.it_value, &ctv, <))
782 timevalclear(&aitv.it_value);
784 timevalsub(&aitv.it_value, &ctv);
787 aitv = p->p_timer[uap->which];
789 lwkt_reltoken(&p->p_token);
790 return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
797 sys_setitimer(struct setitimer_args *uap)
799 struct itimerval aitv;
801 struct itimerval *itvp;
802 struct proc *p = curproc;
805 if (uap->which > ITIMER_PROF)
808 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
809 sizeof(struct itimerval))))
811 if ((uap->itv = uap->oitv) &&
812 (error = sys_getitimer((struct getitimer_args *)uap)))
816 if (itimerfix(&aitv.it_value))
818 if (!timevalisset(&aitv.it_value))
819 timevalclear(&aitv.it_interval);
820 else if (itimerfix(&aitv.it_interval))
822 lwkt_gettoken(&p->p_token);
823 if (uap->which == ITIMER_REAL) {
824 if (timevalisset(&p->p_realtimer.it_value))
825 callout_stop_sync(&p->p_ithandle);
826 if (timevalisset(&aitv.it_value))
827 callout_reset(&p->p_ithandle,
828 tvtohz_high(&aitv.it_value), realitexpire, p);
829 getmicrouptime(&ctv);
830 timevaladd(&aitv.it_value, &ctv);
831 p->p_realtimer = aitv;
833 p->p_timer[uap->which] = aitv;
836 p->p_flags &= ~P_SIGVTALRM;
839 p->p_flags &= ~P_SIGPROF;
843 lwkt_reltoken(&p->p_token);
848 * Real interval timer expired:
849 * send process whose timer expired an alarm signal.
850 * If time is not set up to reload, then just return.
851 * Else compute next time timer should go off which is > current time.
852 * This is where delay in processing this timeout causes multiple
853 * SIGALRM calls to be compressed into one.
854 * tvtohz_high() always adds 1 to allow for the time until the next clock
855 * interrupt being strictly less than 1 clock tick, but we don't want
856 * that here since we want to appear to be in sync with the clock
857 * interrupt even when we're delayed.
861 realitexpire(void *arg)
864 struct timeval ctv, ntv;
866 p = (struct proc *)arg;
868 lwkt_gettoken(&p->p_token);
870 if (!timevalisset(&p->p_realtimer.it_interval)) {
871 timevalclear(&p->p_realtimer.it_value);
875 timevaladd(&p->p_realtimer.it_value,
876 &p->p_realtimer.it_interval);
877 getmicrouptime(&ctv);
878 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
879 ntv = p->p_realtimer.it_value;
880 timevalsub(&ntv, &ctv);
881 callout_reset(&p->p_ithandle, tvtohz_low(&ntv),
887 lwkt_reltoken(&p->p_token);
892 * Used to validate itimer timeouts and utimes*() timespecs.
895 itimerfix(struct timeval *tv)
897 if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
899 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < ustick)
900 tv->tv_usec = ustick;
905 * Used to validate timeouts and utimes*() timespecs.
908 itimespecfix(struct timespec *ts)
910 if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000ULL)
912 if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < nstick)
913 ts->tv_nsec = nstick;
918 * Decrement an interval timer by a specified number
919 * of microseconds, which must be less than a second,
920 * i.e. < 1000000. If the timer expires, then reload
921 * it. In this case, carry over (usec - old value) to
922 * reduce the value reloaded into the timer so that
923 * the timer does not drift. This routine assumes
924 * that it is called in a context where the timers
925 * on which it is operating cannot change in value.
928 itimerdecr(struct itimerval *itp, int usec)
931 if (itp->it_value.tv_usec < usec) {
932 if (itp->it_value.tv_sec == 0) {
933 /* expired, and already in next interval */
934 usec -= itp->it_value.tv_usec;
937 itp->it_value.tv_usec += 1000000;
938 itp->it_value.tv_sec--;
940 itp->it_value.tv_usec -= usec;
942 if (timevalisset(&itp->it_value))
944 /* expired, exactly at end of interval */
946 if (timevalisset(&itp->it_interval)) {
947 itp->it_value = itp->it_interval;
948 itp->it_value.tv_usec -= usec;
949 if (itp->it_value.tv_usec < 0) {
950 itp->it_value.tv_usec += 1000000;
951 itp->it_value.tv_sec--;
954 itp->it_value.tv_usec = 0; /* sec is already 0 */
959 * Add and subtract routines for timevals.
960 * N.B.: subtract routine doesn't deal with
961 * results which are before the beginning,
962 * it just gets very confused in this case.
966 timevaladd(struct timeval *t1, const struct timeval *t2)
969 t1->tv_sec += t2->tv_sec;
970 t1->tv_usec += t2->tv_usec;
975 timevalsub(struct timeval *t1, const struct timeval *t2)
978 t1->tv_sec -= t2->tv_sec;
979 t1->tv_usec -= t2->tv_usec;
984 timevalfix(struct timeval *t1)
987 if (t1->tv_usec < 0) {
989 t1->tv_usec += 1000000;
991 if (t1->tv_usec >= 1000000) {
993 t1->tv_usec -= 1000000;
998 * ratecheck(): simple time-based rate-limit checking.
1001 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
1003 struct timeval tv, delta;
1006 getmicrouptime(&tv); /* NB: 10ms precision */
1008 timevalsub(&delta, lasttime);
1011 * check for 0,0 is so that the message will be seen at least once,
1012 * even if interval is huge.
1014 if (timevalcmp(&delta, mininterval, >=) ||
1015 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1024 * ppsratecheck(): packets (or events) per second limitation.
1026 * Return 0 if the limit is to be enforced (e.g. the caller
1027 * should drop a packet because of the rate limitation).
1029 * maxpps of 0 always causes zero to be returned. maxpps of -1
1030 * always causes 1 to be returned; this effectively defeats rate
1033 * Note that we maintain the struct timeval for compatibility
1034 * with other bsd systems. We reuse the storage and just monitor
1035 * clock ticks for minimal overhead.
1038 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
1043 * Reset the last time and counter if this is the first call
1044 * or more than a second has passed since the last update of
1048 if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
1049 lasttime->tv_sec = now;
1051 return (maxpps != 0);
1053 (*curpps)++; /* NB: ignore potential overflow */
1054 return (maxpps < 0 || *curpps < maxpps);