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>
48 #include <sys/upmap.h>
50 #include <vm/vm_extern.h>
52 #include <sys/msgport2.h>
53 #include <sys/spinlock2.h>
54 #include <sys/thread2.h>
56 extern struct spinlock ntp_spin;
58 #define CPUCLOCK_BIT 0x80000000
59 #define CPUCLOCK_ID_MASK ~CPUCLOCK_BIT
60 #define CPUCLOCK2LWPID(clock_id) (((clockid_t)(clock_id) >> 32) & CPUCLOCK_ID_MASK)
61 #define CPUCLOCK2PID(clock_id) ((clock_id) & CPUCLOCK_ID_MASK)
62 #define MAKE_CPUCLOCK(pid, lwp_id) ((clockid_t)(lwp_id) << 32 | (pid) | CPUCLOCK_BIT)
67 * Time of day and interval timer support.
69 * These routines provide the kernel entry points to get and set
70 * the time-of-day and per-process interval timers. Subroutines
71 * here provide support for adding and subtracting timeval structures
72 * and decrementing interval timers, optionally reloading the interval
73 * timers when they expire.
76 static int settime(struct timeval *);
77 static void timevalfix(struct timeval *);
78 static void realitexpire(void *arg);
80 static int sysctl_gettimeofday_quick(SYSCTL_HANDLER_ARGS);
84 * Nanosleep tries very hard to sleep for a precisely requested time
85 * interval, down to 1uS. The administrator can impose a minimum delay
86 * and a delay below which we hard-loop instead of initiate a timer
87 * interrupt and sleep.
89 * For machines under high loads it might be beneficial to increase min_us
90 * to e.g. 1000uS (1ms) so spining processes sleep meaningfully.
92 static int nanosleep_min_us = 10;
93 static int nanosleep_hard_us = 100;
94 static int gettimeofday_quick = 0;
95 SYSCTL_INT(_kern, OID_AUTO, nanosleep_min_us, CTLFLAG_RW,
96 &nanosleep_min_us, 0, "");
97 SYSCTL_INT(_kern, OID_AUTO, nanosleep_hard_us, CTLFLAG_RW,
98 &nanosleep_hard_us, 0, "");
99 SYSCTL_PROC(_kern, OID_AUTO, gettimeofday_quick, CTLTYPE_INT | CTLFLAG_RW,
100 0, 0, sysctl_gettimeofday_quick, "I", "Quick mode gettimeofday");
102 static struct lock masterclock_lock = LOCK_INITIALIZER("mstrclk", 0, 0);
105 settime(struct timeval *tv)
107 struct timeval delta, tv1, tv2;
108 static struct timeval maxtime, laststep;
112 if ((origcpu = mycpu->gd_cpuid) != 0)
113 lwkt_setcpu_self(globaldata_find(0));
118 timevalsub(&delta, &tv1);
121 * If the system is secure, we do not allow the time to be
122 * set to a value earlier than 1 second less than the highest
123 * time we have yet seen. The worst a miscreant can do in
124 * this circumstance is "freeze" time. He couldn't go
127 * We similarly do not allow the clock to be stepped more
128 * than one second, nor more than once per second. This allows
129 * a miscreant to make the clock march double-time, but no worse.
131 if (securelevel > 1) {
132 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
134 * Update maxtime to latest time we've seen.
136 if (tv1.tv_sec > maxtime.tv_sec)
139 timevalsub(&tv2, &maxtime);
140 if (tv2.tv_sec < -1) {
141 tv->tv_sec = maxtime.tv_sec - 1;
142 kprintf("Time adjustment clamped to -1 second\n");
145 if (tv1.tv_sec == laststep.tv_sec) {
149 if (delta.tv_sec > 1) {
150 tv->tv_sec = tv1.tv_sec + 1;
151 kprintf("Time adjustment clamped to +1 second\n");
157 ts.tv_sec = tv->tv_sec;
158 ts.tv_nsec = tv->tv_usec * 1000;
163 lwkt_setcpu_self(globaldata_find(origcpu));
170 get_process_cputime(struct proc *p, struct timespec *ats)
174 lwkt_gettoken(&p->p_token);
176 lwkt_reltoken(&p->p_token);
177 timevaladd(&ru.ru_utime, &ru.ru_stime);
178 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, ats);
182 get_process_usertime(struct proc *p, struct timespec *ats)
186 lwkt_gettoken(&p->p_token);
188 lwkt_reltoken(&p->p_token);
189 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, ats);
193 get_thread_cputime(struct thread *td, struct timespec *ats)
195 struct timeval sys, user;
197 calcru(td->td_lwp, &user, &sys);
198 timevaladd(&user, &sys);
199 TIMEVAL_TO_TIMESPEC(&user, ats);
206 kern_clock_gettime(clockid_t clock_id, struct timespec *ats)
215 case CLOCK_REALTIME_PRECISE:
218 case CLOCK_REALTIME_FAST:
221 case CLOCK_MONOTONIC:
222 case CLOCK_MONOTONIC_PRECISE:
224 case CLOCK_UPTIME_PRECISE:
227 case CLOCK_MONOTONIC_FAST:
228 case CLOCK_UPTIME_FAST:
232 get_process_usertime(p, ats);
235 case CLOCK_PROCESS_CPUTIME_ID:
236 get_process_cputime(p, ats);
239 ats->tv_sec = time_second;
242 case CLOCK_THREAD_CPUTIME_ID:
243 get_thread_cputime(curthread, ats);
246 if ((clock_id & CPUCLOCK_BIT) == 0)
248 if ((p = pfind(CPUCLOCK2PID(clock_id))) == NULL)
250 lwp_id = CPUCLOCK2LWPID(clock_id);
252 get_process_cputime(p, ats);
254 lwkt_gettoken(&p->p_token);
255 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, lwp_id);
257 lwkt_reltoken(&p->p_token);
261 get_thread_cputime(lp->lwp_thread, ats);
262 lwkt_reltoken(&p->p_token);
273 sys_clock_gettime(struct clock_gettime_args *uap)
278 error = kern_clock_gettime(uap->clock_id, &ats);
280 error = copyout(&ats, uap->tp, sizeof(ats));
286 kern_clock_settime(clockid_t clock_id, struct timespec *ats)
288 struct thread *td = curthread;
292 if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
294 if (clock_id != CLOCK_REALTIME)
296 if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
299 lockmgr(&masterclock_lock, LK_EXCLUSIVE);
300 TIMESPEC_TO_TIMEVAL(&atv, ats);
301 error = settime(&atv);
302 lockmgr(&masterclock_lock, LK_RELEASE);
311 sys_clock_settime(struct clock_settime_args *uap)
316 if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
319 error = kern_clock_settime(uap->clock_id, &ats);
328 kern_clock_getres(clockid_t clock_id, struct timespec *ts)
333 case CLOCK_REALTIME_FAST:
334 case CLOCK_REALTIME_PRECISE:
335 case CLOCK_MONOTONIC:
336 case CLOCK_MONOTONIC_FAST:
337 case CLOCK_MONOTONIC_PRECISE:
339 case CLOCK_UPTIME_FAST:
340 case CLOCK_UPTIME_PRECISE:
342 * Round up the result of the division cheaply
343 * by adding 1. Rounding up is especially important
344 * if rounding down would give 0. Perfect rounding
347 ts->tv_nsec = 1000000000 / sys_cputimer->freq + 1;
351 /* Accurately round up here because we can do so cheaply. */
352 ts->tv_nsec = (1000000000 + hz - 1) / hz;
358 case CLOCK_THREAD_CPUTIME_ID:
359 case CLOCK_PROCESS_CPUTIME_ID:
363 if ((clock_id & CPUCLOCK_BIT) != 0)
376 sys_clock_getres(struct clock_getres_args *uap)
381 error = kern_clock_getres(uap->clock_id, &ts);
383 error = copyout(&ts, uap->tp, sizeof(ts));
389 kern_getcpuclockid(pid_t pid, lwpid_t lwp_id, clockid_t *clock_id)
403 /* lwp_id can be 0 when called by clock_getcpuclockid() */
408 lwkt_gettoken(&p->p_token);
410 lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, lwp_id) == NULL) {
411 lwkt_reltoken(&p->p_token);
415 *clock_id = MAKE_CPUCLOCK(pid, lwp_id);
416 lwkt_reltoken(&p->p_token);
423 sys_getcpuclockid(struct getcpuclockid_args *uap)
428 error = kern_getcpuclockid(uap->pid, uap->lwp_id, &clk_id);
430 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
438 * This is a general helper function for nanosleep() (aka sleep() aka
441 * If there is less then one tick's worth of time left and
442 * we haven't done a yield, or the remaining microseconds is
443 * ridiculously low, do a yield. This avoids having
444 * to deal with systimer overheads when the system is under
445 * heavy loads. If we have done a yield already then use
446 * a systimer and an uninterruptable thread wait.
448 * If there is more then a tick's worth of time left,
449 * calculate the baseline ticks and use an interruptable
450 * tsleep, then handle the fine-grained delay on the next
451 * loop. This usually results in two sleeps occuring, a long one
457 ns1_systimer(systimer_t info, int in_ipi __unused,
458 struct intrframe *frame __unused)
460 lwkt_schedule(info->data);
464 nanosleep1(struct timespec *rqt, struct timespec *rmt)
467 struct timespec ts, ts2, ts3;
471 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
473 /* XXX: imho this should return EINVAL at least for tv_sec < 0 */
474 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
477 timespecadd(&ts, rqt, &ts); /* ts = target timestamp compare */
478 TIMESPEC_TO_TIMEVAL(&tv, rqt); /* tv = sleep interval */
482 struct systimer info;
484 ticks = tv.tv_usec / ustick; /* approximate */
486 if (tv.tv_sec == 0 && ticks == 0) {
487 thread_t td = curthread;
488 if (tv.tv_usec > 0 && tv.tv_usec < nanosleep_min_us)
489 tv.tv_usec = nanosleep_min_us;
490 if (tv.tv_usec < nanosleep_hard_us) {
494 crit_enter_quick(td);
495 systimer_init_oneshot(&info, ns1_systimer,
497 lwkt_deschedule_self(td);
500 systimer_del(&info); /* make sure it's gone */
502 error = iscaught(td->td_lwp);
503 } else if (tv.tv_sec == 0) {
504 error = tsleep(&nanowait, PCATCH, "nanslp", ticks);
506 ticks = tvtohz_low(&tv); /* also handles overflow */
507 error = tsleep(&nanowait, PCATCH, "nanslp", ticks);
510 if (error && error != EWOULDBLOCK) {
511 if (error == ERESTART)
514 timespecsub(&ts, &ts2, &ts);
521 if (timespeccmp(&ts2, &ts, >=))
523 timespecsub(&ts, &ts2, &ts3);
524 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
532 sys_nanosleep(struct nanosleep_args *uap)
538 error = copyin(uap->rqtp, &rqt, sizeof(rqt));
542 error = nanosleep1(&rqt, &rmt);
545 * copyout the residual if nanosleep was interrupted.
547 if (error && uap->rmtp) {
550 error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
558 * The gettimeofday() system call is supposed to return a fine-grained
559 * realtime stamp. However, acquiring a fine-grained stamp can create a
560 * bottleneck when multiple cpu cores are trying to accessing e.g. the
561 * HPET hardware timer all at the same time, so we have a sysctl that
562 * allows its behavior to be changed to a more coarse-grained timestamp
563 * which does not have to access a hardware timer.
566 sys_gettimeofday(struct gettimeofday_args *uap)
572 if (gettimeofday_quick)
576 if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
581 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
590 sys_settimeofday(struct settimeofday_args *uap)
592 struct thread *td = curthread;
597 if ((error = priv_check(td, PRIV_SETTIMEOFDAY)))
600 * Verify all parameters before changing time.
602 * XXX: We do not allow the time to be set to 0.0, which also by
603 * happy coincidence works around a pkgsrc bulk build bug.
606 if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
609 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
611 if (atv.tv_sec == 0 && atv.tv_usec == 0)
615 (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
618 lockmgr(&masterclock_lock, LK_EXCLUSIVE);
619 if (uap->tv && (error = settime(&atv))) {
620 lockmgr(&masterclock_lock, LK_RELEASE);
623 lockmgr(&masterclock_lock, LK_RELEASE);
631 * WARNING! Run with ntp_spin held
634 kern_adjtime_common(void)
636 if ((ntp_delta >= 0 && ntp_delta < ntp_default_tick_delta) ||
637 (ntp_delta < 0 && ntp_delta > -ntp_default_tick_delta))
638 ntp_tick_delta = ntp_delta;
639 else if (ntp_delta > ntp_big_delta)
640 ntp_tick_delta = 10 * ntp_default_tick_delta;
641 else if (ntp_delta < -ntp_big_delta)
642 ntp_tick_delta = -10 * ntp_default_tick_delta;
643 else if (ntp_delta > 0)
644 ntp_tick_delta = ntp_default_tick_delta;
646 ntp_tick_delta = -ntp_default_tick_delta;
650 kern_adjtime(int64_t delta, int64_t *odelta)
652 spin_lock(&ntp_spin);
655 kern_adjtime_common();
656 spin_unlock(&ntp_spin);
660 kern_get_ntp_delta(int64_t *delta)
666 kern_reladjtime(int64_t delta)
668 spin_lock(&ntp_spin);
670 kern_adjtime_common();
671 spin_unlock(&ntp_spin);
675 kern_adjfreq(int64_t rate)
677 spin_lock(&ntp_spin);
678 ntp_tick_permanent = rate;
679 spin_unlock(&ntp_spin);
686 sys_adjtime(struct adjtime_args *uap)
688 struct thread *td = curthread;
690 int64_t ndelta, odelta;
693 if ((error = priv_check(td, PRIV_ADJTIME)))
695 error = copyin(uap->delta, &atv, sizeof(struct timeval));
700 * Compute the total correction and the rate at which to apply it.
701 * Round the adjustment down to a whole multiple of the per-tick
702 * delta, so that after some number of incremental changes in
703 * hardclock(), tickdelta will become zero, lest the correction
704 * overshoot and start taking us away from the desired final time.
706 ndelta = (int64_t)atv.tv_sec * 1000000000 + atv.tv_usec * 1000;
707 kern_adjtime(ndelta, &odelta);
710 atv.tv_sec = odelta / 1000000000;
711 atv.tv_usec = odelta % 1000000000 / 1000;
712 copyout(&atv, uap->olddelta, sizeof(struct timeval));
718 sysctl_adjtime(SYSCTL_HANDLER_ARGS)
723 if (req->newptr != NULL) {
724 if (priv_check(curthread, PRIV_ROOT))
726 error = SYSCTL_IN(req, &delta, sizeof(delta));
729 kern_reladjtime(delta);
733 kern_get_ntp_delta(&delta);
734 error = SYSCTL_OUT(req, &delta, sizeof(delta));
739 * delta is in nanoseconds.
742 sysctl_delta(SYSCTL_HANDLER_ARGS)
744 int64_t delta, old_delta;
747 if (req->newptr != NULL) {
748 if (priv_check(curthread, PRIV_ROOT))
750 error = SYSCTL_IN(req, &delta, sizeof(delta));
753 kern_adjtime(delta, &old_delta);
756 if (req->oldptr != NULL)
757 kern_get_ntp_delta(&old_delta);
758 error = SYSCTL_OUT(req, &old_delta, sizeof(old_delta));
763 * frequency is in nanoseconds per second shifted left 32.
764 * kern_adjfreq() needs it in nanoseconds per tick shifted left 32.
767 sysctl_adjfreq(SYSCTL_HANDLER_ARGS)
772 if (req->newptr != NULL) {
773 if (priv_check(curthread, PRIV_ROOT))
775 error = SYSCTL_IN(req, &freqdelta, sizeof(freqdelta));
780 kern_adjfreq(freqdelta);
783 if (req->oldptr != NULL)
784 freqdelta = ntp_tick_permanent * hz;
785 error = SYSCTL_OUT(req, &freqdelta, sizeof(freqdelta));
792 SYSCTL_NODE(_kern, OID_AUTO, ntp, CTLFLAG_RW, 0, "NTP related controls");
793 SYSCTL_PROC(_kern_ntp, OID_AUTO, permanent,
794 CTLTYPE_QUAD|CTLFLAG_RW, 0, 0,
795 sysctl_adjfreq, "Q", "permanent correction per second");
796 SYSCTL_PROC(_kern_ntp, OID_AUTO, delta,
797 CTLTYPE_QUAD|CTLFLAG_RW, 0, 0,
798 sysctl_delta, "Q", "one-time delta");
799 SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, big_delta, CTLFLAG_RD,
800 &ntp_big_delta, sizeof(ntp_big_delta), "Q",
801 "threshold for fast adjustment");
802 SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, tick_delta, CTLFLAG_RD,
803 &ntp_tick_delta, sizeof(ntp_tick_delta), "LU",
804 "per-tick adjustment");
805 SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, default_tick_delta, CTLFLAG_RD,
806 &ntp_default_tick_delta, sizeof(ntp_default_tick_delta), "LU",
807 "default per-tick adjustment");
808 SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, next_leap_second, CTLFLAG_RW,
809 &ntp_leap_second, sizeof(ntp_leap_second), "LU",
811 SYSCTL_INT(_kern_ntp, OID_AUTO, insert_leap_second, CTLFLAG_RW,
812 &ntp_leap_insert, 0, "insert or remove leap second");
813 SYSCTL_PROC(_kern_ntp, OID_AUTO, adjust,
814 CTLTYPE_QUAD|CTLFLAG_RW, 0, 0,
815 sysctl_adjtime, "Q", "relative adjust for delta");
818 * Get value of an interval timer. The process virtual and
819 * profiling virtual time timers are kept in the p_stats area, since
820 * they can be swapped out. These are kept internally in the
821 * way they are specified externally: in time until they expire.
823 * The real time interval timer is kept in the process table slot
824 * for the process, and its value (it_value) is kept as an
825 * absolute time rather than as a delta, so that it is easy to keep
826 * periodic real-time signals from drifting.
828 * Virtual time timers are processed in the hardclock() routine of
829 * kern_clock.c. The real time timer is processed by a timeout
830 * routine, called from the softclock() routine. Since a callout
831 * may be delayed in real time due to interrupt processing in the system,
832 * it is possible for the real time timeout routine (realitexpire, given below),
833 * to be delayed in real time past when it is supposed to occur. It
834 * does not suffice, therefore, to reload the real timer .it_value from the
835 * real time timers .it_interval. Rather, we compute the next time in
836 * absolute time the timer should go off.
841 sys_getitimer(struct getitimer_args *uap)
843 struct proc *p = curproc;
845 struct itimerval aitv;
847 if (uap->which > ITIMER_PROF)
849 lwkt_gettoken(&p->p_token);
850 if (uap->which == ITIMER_REAL) {
852 * Convert from absolute to relative time in .it_value
853 * part of real time timer. If time for real time timer
854 * has passed return 0, else return difference between
855 * current time and time for the timer to go off.
857 aitv = p->p_realtimer;
858 if (timevalisset(&aitv.it_value)) {
859 getmicrouptime(&ctv);
860 if (timevalcmp(&aitv.it_value, &ctv, <))
861 timevalclear(&aitv.it_value);
863 timevalsub(&aitv.it_value, &ctv);
866 aitv = p->p_timer[uap->which];
868 lwkt_reltoken(&p->p_token);
869 return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
876 sys_setitimer(struct setitimer_args *uap)
878 struct itimerval aitv;
880 struct itimerval *itvp;
881 struct proc *p = curproc;
884 if (uap->which > ITIMER_PROF)
887 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
888 sizeof(struct itimerval))))
890 if ((uap->itv = uap->oitv) &&
891 (error = sys_getitimer((struct getitimer_args *)uap)))
895 if (itimerfix(&aitv.it_value))
897 if (!timevalisset(&aitv.it_value))
898 timevalclear(&aitv.it_interval);
899 else if (itimerfix(&aitv.it_interval))
901 lwkt_gettoken(&p->p_token);
902 if (uap->which == ITIMER_REAL) {
903 if (timevalisset(&p->p_realtimer.it_value))
904 callout_cancel(&p->p_ithandle);
905 if (timevalisset(&aitv.it_value))
906 callout_reset(&p->p_ithandle,
907 tvtohz_high(&aitv.it_value), realitexpire, p);
908 getmicrouptime(&ctv);
909 timevaladd(&aitv.it_value, &ctv);
910 p->p_realtimer = aitv;
912 p->p_timer[uap->which] = aitv;
915 p->p_flags &= ~P_SIGVTALRM;
918 p->p_flags &= ~P_SIGPROF;
922 lwkt_reltoken(&p->p_token);
927 * Real interval timer expired:
928 * send process whose timer expired an alarm signal.
929 * If time is not set up to reload, then just return.
930 * Else compute next time timer should go off which is > current time.
931 * This is where delay in processing this timeout causes multiple
932 * SIGALRM calls to be compressed into one.
933 * tvtohz_high() always adds 1 to allow for the time until the next clock
934 * interrupt being strictly less than 1 clock tick, but we don't want
935 * that here since we want to appear to be in sync with the clock
936 * interrupt even when we're delayed.
940 realitexpire(void *arg)
943 struct timeval ctv, ntv;
945 p = (struct proc *)arg;
947 lwkt_gettoken(&p->p_token);
949 if (!timevalisset(&p->p_realtimer.it_interval)) {
950 timevalclear(&p->p_realtimer.it_value);
954 timevaladd(&p->p_realtimer.it_value,
955 &p->p_realtimer.it_interval);
956 getmicrouptime(&ctv);
957 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
958 ntv = p->p_realtimer.it_value;
959 timevalsub(&ntv, &ctv);
960 callout_reset(&p->p_ithandle, tvtohz_low(&ntv),
966 lwkt_reltoken(&p->p_token);
971 * Used to validate itimer timeouts and utimes*() timespecs.
974 itimerfix(struct timeval *tv)
976 if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
978 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < ustick)
979 tv->tv_usec = ustick;
984 * Used to validate timeouts and utimes*() timespecs.
987 itimespecfix(struct timespec *ts)
989 if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000ULL)
991 if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < nstick)
992 ts->tv_nsec = nstick;
997 * Decrement an interval timer by a specified number
998 * of microseconds, which must be less than a second,
999 * i.e. < 1000000. If the timer expires, then reload
1000 * it. In this case, carry over (usec - old value) to
1001 * reduce the value reloaded into the timer so that
1002 * the timer does not drift. This routine assumes
1003 * that it is called in a context where the timers
1004 * on which it is operating cannot change in value.
1007 itimerdecr(struct itimerval *itp, int usec)
1010 if (itp->it_value.tv_usec < usec) {
1011 if (itp->it_value.tv_sec == 0) {
1012 /* expired, and already in next interval */
1013 usec -= itp->it_value.tv_usec;
1016 itp->it_value.tv_usec += 1000000;
1017 itp->it_value.tv_sec--;
1019 itp->it_value.tv_usec -= usec;
1021 if (timevalisset(&itp->it_value))
1023 /* expired, exactly at end of interval */
1025 if (timevalisset(&itp->it_interval)) {
1026 itp->it_value = itp->it_interval;
1027 itp->it_value.tv_usec -= usec;
1028 if (itp->it_value.tv_usec < 0) {
1029 itp->it_value.tv_usec += 1000000;
1030 itp->it_value.tv_sec--;
1033 itp->it_value.tv_usec = 0; /* sec is already 0 */
1038 * Add and subtract routines for timevals.
1039 * N.B.: subtract routine doesn't deal with
1040 * results which are before the beginning,
1041 * it just gets very confused in this case.
1045 timevaladd(struct timeval *t1, const struct timeval *t2)
1048 t1->tv_sec += t2->tv_sec;
1049 t1->tv_usec += t2->tv_usec;
1054 timevalsub(struct timeval *t1, const struct timeval *t2)
1057 t1->tv_sec -= t2->tv_sec;
1058 t1->tv_usec -= t2->tv_usec;
1063 timevalfix(struct timeval *t1)
1066 if (t1->tv_usec < 0) {
1068 t1->tv_usec += 1000000;
1070 if (t1->tv_usec >= 1000000) {
1072 t1->tv_usec -= 1000000;
1077 * ratecheck(): simple time-based rate-limit checking.
1080 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
1082 struct timeval tv, delta;
1085 getmicrouptime(&tv); /* NB: 10ms precision */
1087 timevalsub(&delta, lasttime);
1090 * check for 0,0 is so that the message will be seen at least once,
1091 * even if interval is huge.
1093 if (timevalcmp(&delta, mininterval, >=) ||
1094 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1103 * ppsratecheck(): packets (or events) per second limitation.
1105 * Return 0 if the limit is to be enforced (e.g. the caller
1106 * should drop a packet because of the rate limitation).
1108 * maxpps of 0 always causes zero to be returned. maxpps of -1
1109 * always causes 1 to be returned; this effectively defeats rate
1112 * Note that we maintain the struct timeval for compatibility
1113 * with other bsd systems. We reuse the storage and just monitor
1114 * clock ticks for minimal overhead.
1117 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
1122 * Reset the last time and counter if this is the first call
1123 * or more than a second has passed since the last update of
1127 if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
1128 lasttime->tv_sec = now;
1130 return (maxpps != 0);
1132 (*curpps)++; /* NB: ignore potential overflow */
1133 return (maxpps < 0 || *curpps < maxpps);
1138 sysctl_gettimeofday_quick(SYSCTL_HANDLER_ARGS)
1143 gtod = gettimeofday_quick;
1144 error = sysctl_handle_int(oidp, >od, 0, req);
1145 if (error || req->newptr == NULL)
1147 gettimeofday_quick = gtod;
1149 kpmap->fast_gtod = gtod;