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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)kern_time.c 8.1 (Berkeley) 6/10/93
34 * $FreeBSD: src/sys/kern/kern_time.c,v 1.68.2.1 2002/10/01 08:00:41 bde Exp $
35 * $DragonFly: src/sys/kern/kern_time.c,v 1.29 2005/06/01 17:43:42 dillon Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/sysproto.h>
42 #include <sys/resourcevar.h>
43 #include <sys/signalvar.h>
44 #include <sys/kernel.h>
45 #include <sys/systm.h>
46 #include <sys/sysent.h>
47 #include <sys/sysunion.h>
50 #include <sys/vnode.h>
51 #include <sys/sysctl.h>
53 #include <vm/vm_extern.h>
54 #include <sys/msgport2.h>
55 #include <sys/thread2.h>
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 nanosleep1 (struct timespec
*rqt
,
70 struct timespec
*rmt
);
71 static int settime (struct timeval
*);
72 static void timevalfix (struct timeval
*);
73 static void no_lease_updatetime (int);
75 static int sleep_hard_us
= 100;
76 SYSCTL_INT(_kern
, OID_AUTO
, sleep_hard_us
, CTLFLAG_RW
, &sleep_hard_us
, 0, "")
79 no_lease_updatetime(deltat
)
84 void (*lease_updatetime
) (int) = no_lease_updatetime
;
90 struct timeval delta
, tv1
, tv2
;
91 static struct timeval maxtime
, laststep
;
95 if ((origcpu
= mycpu
->gd_cpuid
) != 0)
96 lwkt_setcpu_self(globaldata_find(0));
101 timevalsub(&delta
, &tv1
);
104 * If the system is secure, we do not allow the time to be
105 * set to a value earlier than 1 second less than the highest
106 * time we have yet seen. The worst a miscreant can do in
107 * this circumstance is "freeze" time. He couldn't go
110 * We similarly do not allow the clock to be stepped more
111 * than one second, nor more than once per second. This allows
112 * a miscreant to make the clock march double-time, but no worse.
114 if (securelevel
> 1) {
115 if (delta
.tv_sec
< 0 || delta
.tv_usec
< 0) {
117 * Update maxtime to latest time we've seen.
119 if (tv1
.tv_sec
> maxtime
.tv_sec
)
122 timevalsub(&tv2
, &maxtime
);
123 if (tv2
.tv_sec
< -1) {
124 tv
->tv_sec
= maxtime
.tv_sec
- 1;
125 printf("Time adjustment clamped to -1 second\n");
128 if (tv1
.tv_sec
== laststep
.tv_sec
) {
132 if (delta
.tv_sec
> 1) {
133 tv
->tv_sec
= tv1
.tv_sec
+ 1;
134 printf("Time adjustment clamped to +1 second\n");
140 ts
.tv_sec
= tv
->tv_sec
;
141 ts
.tv_nsec
= tv
->tv_usec
* 1000;
143 lease_updatetime(delta
.tv_sec
);
147 lwkt_setcpu_self(globaldata_find(origcpu
));
155 clock_gettime(struct clock_gettime_args
*uap
)
159 switch(uap
->clock_id
) {
162 return (copyout(&ats
, uap
->tp
, sizeof(ats
)));
163 case CLOCK_MONOTONIC
:
165 return (copyout(&ats
, uap
->tp
, sizeof(ats
)));
173 clock_settime(struct clock_settime_args
*uap
)
175 struct thread
*td
= curthread
;
180 if ((error
= suser(td
)) != 0)
182 switch(uap
->clock_id
) {
184 if ((error
= copyin(uap
->tp
, &ats
, sizeof(ats
))) != 0)
186 if (ats
.tv_nsec
< 0 || ats
.tv_nsec
>= 1000000000)
188 /* XXX Don't convert nsec->usec and back */
189 TIMESPEC_TO_TIMEVAL(&atv
, &ats
);
190 error
= settime(&atv
);
198 clock_getres(struct clock_getres_args
*uap
)
202 switch(uap
->clock_id
) {
204 case CLOCK_MONOTONIC
:
206 * Round up the result of the division cheaply
207 * by adding 1. Rounding up is especially important
208 * if rounding down would give 0. Perfect rounding
212 ts
.tv_nsec
= 1000000000 / sys_cputimer
->freq
+ 1;
213 return(copyout(&ts
, uap
->tp
, sizeof(ts
)));
222 * This is a general helper function for nanosleep() (aka sleep() aka
225 * If there is less then one tick's worth of time left and
226 * we haven't done a yield, or the remaining microseconds is
227 * ridiculously low, do a yield. This avoids having
228 * to deal with systimer overheads when the system is under
229 * heavy loads. If we have done a yield already then use
230 * a systimer and an uninterruptable thread wait.
232 * If there is more then a tick's worth of time left,
233 * calculate the baseline ticks and use an interruptable
234 * tsleep, then handle the fine-grained delay on the next
235 * loop. This usually results in two sleeps occuring, a long one
239 ns1_systimer(systimer_t info
)
241 lwkt_schedule(info
->data
);
245 nanosleep1(struct timespec
*rqt
, struct timespec
*rmt
)
248 struct timespec ts
, ts2
, ts3
;
253 if (rqt
->tv_nsec
< 0 || rqt
->tv_nsec
>= 1000000000)
255 if (rqt
->tv_sec
< 0 || (rqt
->tv_sec
== 0 && rqt
->tv_nsec
== 0))
258 timespecadd(&ts
, rqt
); /* ts = target timestamp compare */
259 TIMESPEC_TO_TIMEVAL(&tv
, rqt
); /* tv = sleep interval */
264 struct systimer info
;
266 ticks
= tv
.tv_usec
/ tick
; /* approximate */
268 if (tv
.tv_sec
== 0 && ticks
== 0) {
269 thread_t td
= curthread
;
270 if (tried_yield
|| tv
.tv_usec
< sleep_hard_us
) {
274 crit_enter_quick(td
);
275 systimer_init_oneshot(&info
, ns1_systimer
,
277 lwkt_deschedule_self(td
);
280 systimer_del(&info
); /* make sure it's gone */
282 error
= iscaught(td
->td_proc
);
283 } else if (tv
.tv_sec
== 0) {
284 error
= tsleep(&nanowait
, PCATCH
, "nanslp", ticks
);
286 ticks
= tvtohz_low(&tv
); /* also handles overflow */
287 error
= tsleep(&nanowait
, PCATCH
, "nanslp", ticks
);
290 if (error
&& error
!= EWOULDBLOCK
) {
291 if (error
== ERESTART
)
294 timespecsub(&ts
, &ts2
);
301 if (timespeccmp(&ts2
, &ts
, >=))
304 timespecsub(&ts3
, &ts2
);
305 TIMESPEC_TO_TIMEVAL(&tv
, &ts3
);
309 static void nanosleep_done(void *arg
);
310 static void nanosleep_copyout(union sysunion
*sysun
);
314 nanosleep(struct nanosleep_args
*uap
)
317 struct sysmsg_sleep
*smsleep
= &uap
->sysmsg
.sm
.sleep
;
319 error
= copyin(uap
->rqtp
, &smsleep
->rqt
, sizeof(smsleep
->rqt
));
323 * YYY clean this up to always use the callout, note that an abort
324 * implementation should record the residual in the async case.
326 if (uap
->sysmsg
.lmsg
.ms_flags
& MSGF_ASYNC
) {
329 ticks
= (quad_t
)smsleep
->rqt
.tv_nsec
* hz
/ 1000000000LL;
330 if (smsleep
->rqt
.tv_sec
)
331 ticks
+= (quad_t
)smsleep
->rqt
.tv_sec
* hz
;
338 uap
->sysmsg
.copyout
= nanosleep_copyout
;
339 uap
->sysmsg
.lmsg
.ms_flags
&= ~MSGF_DONE
;
340 callout_init(&smsleep
->timer
);
341 callout_reset(&smsleep
->timer
, ticks
, nanosleep_done
, uap
);
346 * Old synchronous sleep code, copyout the residual if
347 * nanosleep was interrupted.
349 error
= nanosleep1(&smsleep
->rqt
, &smsleep
->rmt
);
350 if (error
&& uap
->rmtp
)
351 error
= copyout(&smsleep
->rmt
, uap
->rmtp
, sizeof(smsleep
->rmt
));
357 * Asynch completion for the nanosleep() syscall. This function may be
358 * called from any context and cannot legally access the originating
359 * thread, proc, or its user space.
361 * YYY change the callout interface API so we can simply assign the replymsg
362 * function to it directly.
365 nanosleep_done(void *arg
)
367 struct nanosleep_args
*uap
= arg
;
368 lwkt_msg_t msg
= &uap
->sysmsg
.lmsg
;
370 lwkt_replymsg(msg
, 0);
374 * Asynch return for the nanosleep() syscall, called in the context of the
375 * originating thread when it pulls the message off the reply port. This
376 * function is responsible for any copyouts to userland. Kernel threads
377 * which do their own internal system calls will not usually call the return
381 nanosleep_copyout(union sysunion
*sysun
)
383 struct nanosleep_args
*uap
= &sysun
->nanosleep
;
384 struct sysmsg_sleep
*smsleep
= &uap
->sysmsg
.sm
.sleep
;
386 if (sysun
->lmsg
.ms_error
&& uap
->rmtp
) {
387 sysun
->lmsg
.ms_error
=
388 copyout(&smsleep
->rmt
, uap
->rmtp
, sizeof(smsleep
->rmt
));
394 gettimeofday(struct gettimeofday_args
*uap
)
401 if ((error
= copyout((caddr_t
)&atv
, (caddr_t
)uap
->tp
,
406 error
= copyout((caddr_t
)&tz
, (caddr_t
)uap
->tzp
,
413 settimeofday(struct settimeofday_args
*uap
)
415 struct thread
*td
= curthread
;
420 if ((error
= suser(td
)))
422 /* Verify all parameters before changing time. */
424 if ((error
= copyin((caddr_t
)uap
->tv
, (caddr_t
)&atv
,
427 if (atv
.tv_usec
< 0 || atv
.tv_usec
>= 1000000)
431 (error
= copyin((caddr_t
)uap
->tzp
, (caddr_t
)&atz
, sizeof(atz
))))
433 if (uap
->tv
&& (error
= settime(&atv
)))
441 kern_adjtime_common(void)
443 if ((ntp_delta
>= 0 && ntp_delta
< ntp_default_tick_delta
) ||
444 (ntp_delta
< 0 && ntp_delta
> ntp_default_tick_delta
))
445 ntp_tick_delta
= ntp_delta
;
446 else if (ntp_delta
> ntp_big_delta
)
447 ntp_tick_delta
= 10 * ntp_default_tick_delta
;
448 else if (ntp_delta
< -ntp_big_delta
)
449 ntp_tick_delta
= -10 * ntp_default_tick_delta
;
450 else if (ntp_delta
> 0)
451 ntp_tick_delta
= ntp_default_tick_delta
;
453 ntp_tick_delta
= -ntp_default_tick_delta
;
457 kern_adjtime(int64_t delta
, int64_t *odelta
)
461 if ((origcpu
= mycpu
->gd_cpuid
) != 0)
462 lwkt_setcpu_self(globaldata_find(0));
467 kern_adjtime_common();
471 lwkt_setcpu_self(globaldata_find(origcpu
));
475 kern_get_ntp_delta(int64_t *delta
)
479 if ((origcpu
= mycpu
->gd_cpuid
) != 0)
480 lwkt_setcpu_self(globaldata_find(0));
487 lwkt_setcpu_self(globaldata_find(origcpu
));
491 kern_reladjtime(int64_t delta
)
495 if ((origcpu
= mycpu
->gd_cpuid
) != 0)
496 lwkt_setcpu_self(globaldata_find(0));
500 kern_adjtime_common();
504 lwkt_setcpu_self(globaldata_find(origcpu
));
508 kern_adjfreq(int64_t rate
)
512 if ((origcpu
= mycpu
->gd_cpuid
) != 0)
513 lwkt_setcpu_self(globaldata_find(0));
516 ntp_tick_permanent
= rate
;
520 lwkt_setcpu_self(globaldata_find(origcpu
));
525 adjtime(struct adjtime_args
*uap
)
527 struct thread
*td
= curthread
;
529 int64_t ndelta
, odelta
;
532 if ((error
= suser(td
)))
535 copyin((caddr_t
)uap
->delta
, (caddr_t
)&atv
, sizeof(struct timeval
))))
539 * Compute the total correction and the rate at which to apply it.
540 * Round the adjustment down to a whole multiple of the per-tick
541 * delta, so that after some number of incremental changes in
542 * hardclock(), tickdelta will become zero, lest the correction
543 * overshoot and start taking us away from the desired final time.
545 ndelta
= (int64_t)atv
.tv_sec
* 1000000000 + atv
.tv_usec
* 1000;
546 kern_adjtime(ndelta
, &odelta
);
549 atv
.tv_sec
= odelta
/ 1000000000;
550 atv
.tv_usec
= odelta
% 1000000 / 1000;
551 (void) copyout((caddr_t
)&atv
, (caddr_t
)uap
->olddelta
,
552 sizeof(struct timeval
));
558 sysctl_adjtime(SYSCTL_HANDLER_ARGS
)
563 if (req
->newptr
!= NULL
) {
564 if (suser(curthread
))
566 error
= SYSCTL_IN(req
, &delta
, sizeof(delta
));
569 kern_reladjtime(delta
);
573 kern_get_ntp_delta(&delta
);
574 error
= SYSCTL_OUT(req
, &delta
, sizeof(delta
));
579 * delta is in nanoseconds.
582 sysctl_delta(SYSCTL_HANDLER_ARGS
)
584 int64_t delta
, old_delta
;
587 if (req
->newptr
!= NULL
) {
588 if (suser(curthread
))
590 error
= SYSCTL_IN(req
, &delta
, sizeof(delta
));
593 kern_adjtime(delta
, &old_delta
);
596 if (req
->oldptr
!= NULL
)
597 kern_get_ntp_delta(&old_delta
);
598 error
= SYSCTL_OUT(req
, &old_delta
, sizeof(old_delta
));
603 * frequency is in nanoseconds per second shifted left 32.
604 * kern_adjfreq() needs it in nanoseconds per tick shifted left 32.
607 sysctl_adjfreq(SYSCTL_HANDLER_ARGS
)
612 if (req
->newptr
!= NULL
) {
613 if (suser(curthread
))
615 error
= SYSCTL_IN(req
, &freqdelta
, sizeof(freqdelta
));
620 kern_adjfreq(freqdelta
);
623 if (req
->oldptr
!= NULL
)
624 freqdelta
= ntp_tick_permanent
* hz
;
625 error
= SYSCTL_OUT(req
, &freqdelta
, sizeof(freqdelta
));
632 SYSCTL_NODE(_kern
, OID_AUTO
, ntp
, CTLFLAG_RW
, 0, "NTP related controls");
633 SYSCTL_PROC(_kern_ntp
, OID_AUTO
, permanent
,
634 CTLTYPE_QUAD
|CTLFLAG_RW
, 0, 0,
635 sysctl_adjfreq
, "Q", "permanent correction per second");
636 SYSCTL_PROC(_kern_ntp
, OID_AUTO
, delta
,
637 CTLTYPE_QUAD
|CTLFLAG_RW
, 0, 0,
638 sysctl_delta
, "Q", "one-time delta");
639 SYSCTL_OPAQUE(_kern_ntp
, OID_AUTO
, big_delta
, CTLFLAG_RD
,
640 &ntp_big_delta
, sizeof(ntp_big_delta
), "Q",
641 "threshold for fast adjustment");
642 SYSCTL_OPAQUE(_kern_ntp
, OID_AUTO
, tick_delta
, CTLFLAG_RD
,
643 &ntp_tick_delta
, sizeof(ntp_tick_delta
), "LU",
644 "per-tick adjustment");
645 SYSCTL_OPAQUE(_kern_ntp
, OID_AUTO
, default_tick_delta
, CTLFLAG_RD
,
646 &ntp_default_tick_delta
, sizeof(ntp_default_tick_delta
), "LU",
647 "default per-tick adjustment");
648 SYSCTL_OPAQUE(_kern_ntp
, OID_AUTO
, next_leap_second
, CTLFLAG_RW
,
649 &ntp_leap_second
, sizeof(ntp_leap_second
), "LU",
651 SYSCTL_INT(_kern_ntp
, OID_AUTO
, insert_leap_second
, CTLFLAG_RW
,
652 &ntp_leap_insert
, 0, "insert or remove leap second");
653 SYSCTL_PROC(_kern_ntp
, OID_AUTO
, adjust
,
654 CTLTYPE_QUAD
|CTLFLAG_RW
, 0, 0,
655 sysctl_adjtime
, "Q", "relative adjust for delta");
658 * Get value of an interval timer. The process virtual and
659 * profiling virtual time timers are kept in the p_stats area, since
660 * they can be swapped out. These are kept internally in the
661 * way they are specified externally: in time until they expire.
663 * The real time interval timer is kept in the process table slot
664 * for the process, and its value (it_value) is kept as an
665 * absolute time rather than as a delta, so that it is easy to keep
666 * periodic real-time signals from drifting.
668 * Virtual time timers are processed in the hardclock() routine of
669 * kern_clock.c. The real time timer is processed by a timeout
670 * routine, called from the softclock() routine. Since a callout
671 * may be delayed in real time due to interrupt processing in the system,
672 * it is possible for the real time timeout routine (realitexpire, given below),
673 * to be delayed in real time past when it is supposed to occur. It
674 * does not suffice, therefore, to reload the real timer .it_value from the
675 * real time timers .it_interval. Rather, we compute the next time in
676 * absolute time the timer should go off.
680 getitimer(struct getitimer_args
*uap
)
682 struct proc
*p
= curproc
;
684 struct itimerval aitv
;
686 if (uap
->which
> ITIMER_PROF
)
689 if (uap
->which
== ITIMER_REAL
) {
691 * Convert from absolute to relative time in .it_value
692 * part of real time timer. If time for real time timer
693 * has passed return 0, else return difference between
694 * current time and time for the timer to go off.
696 aitv
= p
->p_realtimer
;
697 if (timevalisset(&aitv
.it_value
)) {
698 getmicrouptime(&ctv
);
699 if (timevalcmp(&aitv
.it_value
, &ctv
, <))
700 timevalclear(&aitv
.it_value
);
702 timevalsub(&aitv
.it_value
, &ctv
);
705 aitv
= p
->p_stats
->p_timer
[uap
->which
];
708 return (copyout((caddr_t
)&aitv
, (caddr_t
)uap
->itv
,
709 sizeof (struct itimerval
)));
714 setitimer(struct setitimer_args
*uap
)
716 struct itimerval aitv
;
718 struct itimerval
*itvp
;
719 struct proc
*p
= curproc
;
722 if (uap
->which
> ITIMER_PROF
)
725 if (itvp
&& (error
= copyin((caddr_t
)itvp
, (caddr_t
)&aitv
,
726 sizeof(struct itimerval
))))
728 if ((uap
->itv
= uap
->oitv
) &&
729 (error
= getitimer((struct getitimer_args
*)uap
)))
733 if (itimerfix(&aitv
.it_value
))
735 if (!timevalisset(&aitv
.it_value
))
736 timevalclear(&aitv
.it_interval
);
737 else if (itimerfix(&aitv
.it_interval
))
740 if (uap
->which
== ITIMER_REAL
) {
741 if (timevalisset(&p
->p_realtimer
.it_value
))
742 callout_stop(&p
->p_ithandle
);
743 if (timevalisset(&aitv
.it_value
))
744 callout_reset(&p
->p_ithandle
,
745 tvtohz_high(&aitv
.it_value
), realitexpire
, p
);
746 getmicrouptime(&ctv
);
747 timevaladd(&aitv
.it_value
, &ctv
);
748 p
->p_realtimer
= aitv
;
750 p
->p_stats
->p_timer
[uap
->which
] = aitv
;
757 * Real interval timer expired:
758 * send process whose timer expired an alarm signal.
759 * If time is not set up to reload, then just return.
760 * Else compute next time timer should go off which is > current time.
761 * This is where delay in processing this timeout causes multiple
762 * SIGALRM calls to be compressed into one.
763 * tvtohz_high() always adds 1 to allow for the time until the next clock
764 * interrupt being strictly less than 1 clock tick, but we don't want
765 * that here since we want to appear to be in sync with the clock
766 * interrupt even when we're delayed.
773 struct timeval ctv
, ntv
;
775 p
= (struct proc
*)arg
;
777 if (!timevalisset(&p
->p_realtimer
.it_interval
)) {
778 timevalclear(&p
->p_realtimer
.it_value
);
783 timevaladd(&p
->p_realtimer
.it_value
,
784 &p
->p_realtimer
.it_interval
);
785 getmicrouptime(&ctv
);
786 if (timevalcmp(&p
->p_realtimer
.it_value
, &ctv
, >)) {
787 ntv
= p
->p_realtimer
.it_value
;
788 timevalsub(&ntv
, &ctv
);
789 callout_reset(&p
->p_ithandle
, tvtohz_low(&ntv
),
799 * Check that a proposed value to load into the .it_value or
800 * .it_interval part of an interval timer is acceptable, and
801 * fix it to have at least minimal value (i.e. if it is less
802 * than the resolution of the clock, round it up.)
809 if (tv
->tv_sec
< 0 || tv
->tv_sec
> 100000000 ||
810 tv
->tv_usec
< 0 || tv
->tv_usec
>= 1000000)
812 if (tv
->tv_sec
== 0 && tv
->tv_usec
!= 0 && tv
->tv_usec
< tick
)
818 * Decrement an interval timer by a specified number
819 * of microseconds, which must be less than a second,
820 * i.e. < 1000000. If the timer expires, then reload
821 * it. In this case, carry over (usec - old value) to
822 * reduce the value reloaded into the timer so that
823 * the timer does not drift. This routine assumes
824 * that it is called in a context where the timers
825 * on which it is operating cannot change in value.
828 itimerdecr(itp
, usec
)
829 struct itimerval
*itp
;
833 if (itp
->it_value
.tv_usec
< usec
) {
834 if (itp
->it_value
.tv_sec
== 0) {
835 /* expired, and already in next interval */
836 usec
-= itp
->it_value
.tv_usec
;
839 itp
->it_value
.tv_usec
+= 1000000;
840 itp
->it_value
.tv_sec
--;
842 itp
->it_value
.tv_usec
-= usec
;
844 if (timevalisset(&itp
->it_value
))
846 /* expired, exactly at end of interval */
848 if (timevalisset(&itp
->it_interval
)) {
849 itp
->it_value
= itp
->it_interval
;
850 itp
->it_value
.tv_usec
-= usec
;
851 if (itp
->it_value
.tv_usec
< 0) {
852 itp
->it_value
.tv_usec
+= 1000000;
853 itp
->it_value
.tv_sec
--;
856 itp
->it_value
.tv_usec
= 0; /* sec is already 0 */
861 * Add and subtract routines for timevals.
862 * N.B.: subtract routine doesn't deal with
863 * results which are before the beginning,
864 * it just gets very confused in this case.
869 struct timeval
*t1
, *t2
;
872 t1
->tv_sec
+= t2
->tv_sec
;
873 t1
->tv_usec
+= t2
->tv_usec
;
879 struct timeval
*t1
, *t2
;
882 t1
->tv_sec
-= t2
->tv_sec
;
883 t1
->tv_usec
-= t2
->tv_usec
;
892 if (t1
->tv_usec
< 0) {
894 t1
->tv_usec
+= 1000000;
896 if (t1
->tv_usec
>= 1000000) {
898 t1
->tv_usec
-= 1000000;
903 * ratecheck(): simple time-based rate-limit checking.
906 ratecheck(struct timeval
*lasttime
, const struct timeval
*mininterval
)
908 struct timeval tv
, delta
;
911 getmicrouptime(&tv
); /* NB: 10ms precision */
913 timevalsub(&delta
, lasttime
);
916 * check for 0,0 is so that the message will be seen at least once,
917 * even if interval is huge.
919 if (timevalcmp(&delta
, mininterval
, >=) ||
920 (lasttime
->tv_sec
== 0 && lasttime
->tv_usec
== 0)) {
929 * ppsratecheck(): packets (or events) per second limitation.
931 * Return 0 if the limit is to be enforced (e.g. the caller
932 * should drop a packet because of the rate limitation).
934 * maxpps of 0 always causes zero to be returned. maxpps of -1
935 * always causes 1 to be returned; this effectively defeats rate
938 * Note that we maintain the struct timeval for compatibility
939 * with other bsd systems. We reuse the storage and just monitor
940 * clock ticks for minimal overhead.
943 ppsratecheck(struct timeval
*lasttime
, int *curpps
, int maxpps
)
948 * Reset the last time and counter if this is the first call
949 * or more than a second has passed since the last update of
953 if (lasttime
->tv_sec
== 0 || (u_int
)(now
- lasttime
->tv_sec
) >= hz
) {
954 lasttime
->tv_sec
= now
;
956 return (maxpps
!= 0);
958 (*curpps
)++; /* NB: ignore potential overflow */
959 return (maxpps
< 0 || *curpps
< maxpps
);