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/sysmsg.h>
37 #include <sys/resourcevar.h>
38 #include <sys/signalvar.h>
39 #include <sys/kernel.h>
40 #include <sys/sysent.h>
44 #include <sys/vnode.h>
45 #include <sys/sysctl.h>
46 #include <sys/kern_syscall.h>
47 #include <sys/upmap.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;
57 #define CPUCLOCK_BIT 0x80000000
58 #define CPUCLOCK_ID_MASK ~CPUCLOCK_BIT
59 #define CPUCLOCK2LWPID(clock_id) (((clockid_t)(clock_id) >> 32) & CPUCLOCK_ID_MASK)
60 #define CPUCLOCK2PID(clock_id) ((clock_id) & CPUCLOCK_ID_MASK)
61 #define MAKE_CPUCLOCK(pid, lwp_id) ((clockid_t)(lwp_id) << 32 | (pid) | CPUCLOCK_BIT)
66 * Time of day and interval timer support.
68 * These routines provide the kernel entry points to get and set
69 * the time-of-day and per-process interval timers. Subroutines
70 * here provide support for adding and subtracting timeval structures
71 * and decrementing interval timers, optionally reloading the interval
72 * timers when they expire.
75 static int settime(struct timeval *);
76 static void timevalfix(struct timeval *);
77 static void realitexpire(void *arg);
79 static int sysctl_gettimeofday_quick(SYSCTL_HANDLER_ARGS);
83 * Nanosleep tries very hard to sleep for a precisely requested time
84 * interval, down to 1uS. The administrator can impose a minimum delay
85 * and a delay below which we hard-loop instead of initiate a timer
86 * interrupt and sleep.
88 * For machines under high loads it might be beneficial to increase min_us
89 * to e.g. 1000uS (1ms) so spining processes sleep meaningfully.
91 static int nanosleep_min_us = 10;
92 static int nanosleep_hard_us = 100;
93 static int gettimeofday_quick = 0;
94 SYSCTL_INT(_kern, OID_AUTO, nanosleep_min_us, CTLFLAG_RW,
95 &nanosleep_min_us, 0, "");
96 SYSCTL_INT(_kern, OID_AUTO, nanosleep_hard_us, CTLFLAG_RW,
97 &nanosleep_hard_us, 0, "");
98 SYSCTL_PROC(_kern, OID_AUTO, gettimeofday_quick, CTLTYPE_INT | CTLFLAG_RW,
99 0, 0, sysctl_gettimeofday_quick, "I", "Quick mode gettimeofday");
101 static struct lock masterclock_lock = LOCK_INITIALIZER("mstrclk", 0, 0);
104 settime(struct timeval *tv)
106 struct timeval delta, tv1, tv2;
107 static struct timeval maxtime, laststep;
111 if ((origcpu = mycpu->gd_cpuid) != 0)
112 lwkt_setcpu_self(globaldata_find(0));
117 timevalsub(&delta, &tv1);
120 * If the system is secure, we do not allow the time to be
121 * set to a value earlier than 1 second less than the highest
122 * time we have yet seen. The worst a miscreant can do in
123 * this circumstance is "freeze" time. He couldn't go
126 * We similarly do not allow the clock to be stepped more
127 * than one second, nor more than once per second. This allows
128 * a miscreant to make the clock march double-time, but no worse.
130 if (securelevel > 1) {
131 if (delta.tv_sec < 0 || delta.tv_usec < 0) {
133 * Update maxtime to latest time we've seen.
135 if (tv1.tv_sec > maxtime.tv_sec)
138 timevalsub(&tv2, &maxtime);
139 if (tv2.tv_sec < -1) {
140 tv->tv_sec = maxtime.tv_sec - 1;
141 kprintf("Time adjustment clamped to -1 second\n");
144 if (tv1.tv_sec == laststep.tv_sec) {
148 if (delta.tv_sec > 1) {
149 tv->tv_sec = tv1.tv_sec + 1;
150 kprintf("Time adjustment clamped to +1 second\n");
156 ts.tv_sec = tv->tv_sec;
157 ts.tv_nsec = tv->tv_usec * 1000;
162 lwkt_setcpu_self(globaldata_find(origcpu));
169 get_process_cputime(struct proc *p, struct timespec *ats)
173 lwkt_gettoken(&p->p_token);
175 lwkt_reltoken(&p->p_token);
176 timevaladd(&ru.ru_utime, &ru.ru_stime);
177 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, ats);
181 get_process_usertime(struct proc *p, struct timespec *ats)
185 lwkt_gettoken(&p->p_token);
187 lwkt_reltoken(&p->p_token);
188 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, ats);
192 get_thread_cputime(struct thread *td, struct timespec *ats)
194 struct timeval sys, user;
196 calcru(td->td_lwp, &user, &sys);
197 timevaladd(&user, &sys);
198 TIMEVAL_TO_TIMESPEC(&user, ats);
205 kern_clock_gettime(clockid_t clock_id, struct timespec *ats)
214 case CLOCK_REALTIME_PRECISE:
217 case CLOCK_REALTIME_FAST:
220 case CLOCK_MONOTONIC:
221 case CLOCK_MONOTONIC_PRECISE:
223 case CLOCK_UPTIME_PRECISE:
226 case CLOCK_MONOTONIC_FAST:
227 case CLOCK_UPTIME_FAST:
231 get_process_usertime(p, ats);
234 case CLOCK_PROCESS_CPUTIME_ID:
235 get_process_cputime(p, ats);
238 ats->tv_sec = time_second;
241 case CLOCK_THREAD_CPUTIME_ID:
242 get_thread_cputime(curthread, ats);
245 if ((clock_id & CPUCLOCK_BIT) == 0)
247 if ((p = pfind(CPUCLOCK2PID(clock_id))) == NULL)
249 lwp_id = CPUCLOCK2LWPID(clock_id);
251 get_process_cputime(p, ats);
253 lwkt_gettoken(&p->p_token);
254 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, lwp_id);
256 lwkt_reltoken(&p->p_token);
260 get_thread_cputime(lp->lwp_thread, ats);
261 lwkt_reltoken(&p->p_token);
272 sys_clock_gettime(struct sysmsg *sysmsg, const struct clock_gettime_args *uap)
277 error = kern_clock_gettime(uap->clock_id, &ats);
279 error = copyout(&ats, uap->tp, sizeof(ats));
285 kern_clock_settime(clockid_t clock_id, struct timespec *ats)
287 struct thread *td = curthread;
291 if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
293 if (clock_id != CLOCK_REALTIME)
295 if (ats->tv_nsec < 0 || ats->tv_nsec >= 1000000000)
298 lockmgr(&masterclock_lock, LK_EXCLUSIVE);
299 TIMESPEC_TO_TIMEVAL(&atv, ats);
300 error = settime(&atv);
301 lockmgr(&masterclock_lock, LK_RELEASE);
310 sys_clock_settime(struct sysmsg *sysmsg, const struct clock_settime_args *uap)
315 if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
318 error = kern_clock_settime(uap->clock_id, &ats);
327 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 * Minimum reportable resolution is 1ns. Rounding is
343 * otherwise unimportant.
345 ts->tv_nsec = 999999999 / sys_cputimer->freq + 1;
349 /* Accurately round up here because we can do so cheaply. */
350 ts->tv_nsec = (1000000000 + hz - 1) / hz;
356 case CLOCK_THREAD_CPUTIME_ID:
357 case CLOCK_PROCESS_CPUTIME_ID:
361 if ((clock_id & CPUCLOCK_BIT) != 0)
374 sys_clock_getres(struct sysmsg *sysmsg, const struct clock_getres_args *uap)
379 error = kern_clock_getres(uap->clock_id, &ts);
381 error = copyout(&ts, uap->tp, sizeof(ts));
387 kern_getcpuclockid(pid_t pid, lwpid_t lwp_id, clockid_t *clock_id)
401 /* lwp_id can be 0 when called by clock_getcpuclockid() */
406 lwkt_gettoken(&p->p_token);
408 lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, lwp_id) == NULL) {
409 lwkt_reltoken(&p->p_token);
413 *clock_id = MAKE_CPUCLOCK(pid, lwp_id);
414 lwkt_reltoken(&p->p_token);
421 sys_getcpuclockid(struct sysmsg *sysmsg, const struct getcpuclockid_args *uap)
426 error = kern_getcpuclockid(uap->pid, uap->lwp_id, &clk_id);
428 error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
436 * This is a general helper function for nanosleep() (aka sleep() aka
439 * If there is less then one tick's worth of time left and
440 * we haven't done a yield, or the remaining microseconds is
441 * ridiculously low, do a yield. This avoids having
442 * to deal with systimer overheads when the system is under
443 * heavy loads. If we have done a yield already then use
444 * a systimer and an uninterruptable thread wait.
446 * If there is more then a tick's worth of time left,
447 * calculate the baseline ticks and use an interruptable
448 * tsleep, then handle the fine-grained delay on the next
449 * loop. This usually results in two sleeps occuring, a long one
455 ns1_systimer(systimer_t info, int in_ipi __unused,
456 struct intrframe *frame __unused)
458 lwkt_schedule(info->data);
462 nanosleep1(struct timespec *rqt, struct timespec *rmt)
465 struct timespec ts, ts2, ts3;
469 if (rqt->tv_nsec < 0 || rqt->tv_nsec >= 1000000000)
471 /* XXX: imho this should return EINVAL at least for tv_sec < 0 */
472 if (rqt->tv_sec < 0 || (rqt->tv_sec == 0 && rqt->tv_nsec == 0))
475 timespecadd(&ts, rqt, &ts); /* ts = target timestamp compare */
476 TIMESPEC_TO_TIMEVAL(&tv, rqt); /* tv = sleep interval */
480 struct systimer info;
482 ticks = tv.tv_usec / ustick; /* approximate */
484 if (tv.tv_sec == 0 && ticks == 0) {
485 thread_t td = curthread;
486 if (tv.tv_usec > 0 && tv.tv_usec < nanosleep_min_us)
487 tv.tv_usec = nanosleep_min_us;
488 if (tv.tv_usec < nanosleep_hard_us) {
492 crit_enter_quick(td);
493 systimer_init_oneshot(&info, ns1_systimer,
495 lwkt_deschedule_self(td);
498 systimer_del(&info); /* make sure it's gone */
500 error = iscaught(td->td_lwp);
501 } else if (tv.tv_sec == 0) {
502 error = tsleep(&nanowait, PCATCH, "nanslp", ticks);
504 ticks = tvtohz_low(&tv); /* also handles overflow */
505 error = tsleep(&nanowait, PCATCH, "nanslp", ticks);
508 if (error && error != EWOULDBLOCK) {
509 if (error == ERESTART)
512 timespecsub(&ts, &ts2, &ts);
519 if (timespeccmp(&ts2, &ts, >=))
521 timespecsub(&ts, &ts2, &ts3);
522 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
530 sys_nanosleep(struct sysmsg *sysmsg, const struct nanosleep_args *uap)
536 error = copyin(uap->rqtp, &rqt, sizeof(rqt));
540 error = nanosleep1(&rqt, &rmt);
543 * copyout the residual if nanosleep was interrupted.
545 if (error && uap->rmtp) {
548 error2 = copyout(&rmt, uap->rmtp, sizeof(rmt));
556 * The gettimeofday() system call is supposed to return a fine-grained
557 * realtime stamp. However, acquiring a fine-grained stamp can create a
558 * bottleneck when multiple cpu cores are trying to accessing e.g. the
559 * HPET hardware timer all at the same time, so we have a sysctl that
560 * allows its behavior to be changed to a more coarse-grained timestamp
561 * which does not have to access a hardware timer.
564 sys_gettimeofday(struct sysmsg *sysmsg, const struct gettimeofday_args *uap)
570 if (gettimeofday_quick)
574 if ((error = copyout((caddr_t)&atv, (caddr_t)uap->tp,
579 error = copyout((caddr_t)&tz, (caddr_t)uap->tzp,
588 sys_settimeofday(struct sysmsg *sysmsg, const struct settimeofday_args *uap)
590 struct thread *td = curthread;
595 if ((error = priv_check(td, PRIV_SETTIMEOFDAY)))
598 * Verify all parameters before changing time.
600 * XXX: We do not allow the time to be set to 0.0, which also by
601 * happy coincidence works around a pkgsrc bulk build bug.
604 if ((error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
607 if (atv.tv_usec < 0 || atv.tv_usec >= 1000000)
609 if (atv.tv_sec == 0 && atv.tv_usec == 0)
613 (error = copyin((caddr_t)uap->tzp, (caddr_t)&atz, sizeof(atz))))
616 lockmgr(&masterclock_lock, LK_EXCLUSIVE);
617 if (uap->tv && (error = settime(&atv))) {
618 lockmgr(&masterclock_lock, LK_RELEASE);
621 lockmgr(&masterclock_lock, LK_RELEASE);
629 * WARNING! Run with ntp_spin held
632 kern_adjtime_common(void)
634 if ((ntp_delta >= 0 && ntp_delta < ntp_default_tick_delta) ||
635 (ntp_delta < 0 && ntp_delta > -ntp_default_tick_delta))
636 ntp_tick_delta = ntp_delta;
637 else if (ntp_delta > ntp_big_delta)
638 ntp_tick_delta = 10 * ntp_default_tick_delta;
639 else if (ntp_delta < -ntp_big_delta)
640 ntp_tick_delta = -10 * ntp_default_tick_delta;
641 else if (ntp_delta > 0)
642 ntp_tick_delta = ntp_default_tick_delta;
644 ntp_tick_delta = -ntp_default_tick_delta;
648 kern_adjtime(int64_t delta, int64_t *odelta)
650 spin_lock(&ntp_spin);
653 kern_adjtime_common();
654 spin_unlock(&ntp_spin);
658 kern_get_ntp_delta(int64_t *delta)
664 kern_reladjtime(int64_t delta)
666 spin_lock(&ntp_spin);
668 kern_adjtime_common();
669 spin_unlock(&ntp_spin);
673 kern_adjfreq(int64_t rate)
675 spin_lock(&ntp_spin);
676 ntp_tick_permanent = rate;
677 spin_unlock(&ntp_spin);
684 sys_adjtime(struct sysmsg *sysmsg, const struct adjtime_args *uap)
686 struct thread *td = curthread;
688 int64_t ndelta, odelta;
691 if ((error = priv_check(td, PRIV_ADJTIME)))
693 error = copyin(uap->delta, &atv, sizeof(struct timeval));
698 * Compute the total correction and the rate at which to apply it.
699 * Round the adjustment down to a whole multiple of the per-tick
700 * delta, so that after some number of incremental changes in
701 * hardclock(), tickdelta will become zero, lest the correction
702 * overshoot and start taking us away from the desired final time.
704 ndelta = (int64_t)atv.tv_sec * 1000000000 + atv.tv_usec * 1000;
705 kern_adjtime(ndelta, &odelta);
708 atv.tv_sec = odelta / 1000000000;
709 atv.tv_usec = odelta % 1000000000 / 1000;
710 copyout(&atv, uap->olddelta, sizeof(struct timeval));
716 sysctl_adjtime(SYSCTL_HANDLER_ARGS)
721 if (req->newptr != NULL) {
722 if (priv_check(curthread, PRIV_ROOT))
724 error = SYSCTL_IN(req, &delta, sizeof(delta));
727 kern_reladjtime(delta);
731 kern_get_ntp_delta(&delta);
732 error = SYSCTL_OUT(req, &delta, sizeof(delta));
737 * delta is in nanoseconds.
740 sysctl_delta(SYSCTL_HANDLER_ARGS)
742 int64_t delta, old_delta;
745 if (req->newptr != NULL) {
746 if (priv_check(curthread, PRIV_ROOT))
748 error = SYSCTL_IN(req, &delta, sizeof(delta));
751 kern_adjtime(delta, &old_delta);
754 if (req->oldptr != NULL)
755 kern_get_ntp_delta(&old_delta);
756 error = SYSCTL_OUT(req, &old_delta, sizeof(old_delta));
761 * frequency is in nanoseconds per second shifted left 32.
762 * kern_adjfreq() needs it in nanoseconds per tick shifted left 32.
765 sysctl_adjfreq(SYSCTL_HANDLER_ARGS)
770 if (req->newptr != NULL) {
771 if (priv_check(curthread, PRIV_ROOT))
773 error = SYSCTL_IN(req, &freqdelta, sizeof(freqdelta));
778 kern_adjfreq(freqdelta);
781 if (req->oldptr != NULL)
782 freqdelta = ntp_tick_permanent * hz;
783 error = SYSCTL_OUT(req, &freqdelta, sizeof(freqdelta));
790 SYSCTL_NODE(_kern, OID_AUTO, ntp, CTLFLAG_RW, 0, "NTP related controls");
791 SYSCTL_PROC(_kern_ntp, OID_AUTO, permanent,
792 CTLTYPE_QUAD|CTLFLAG_RW, 0, 0,
793 sysctl_adjfreq, "Q", "permanent correction per second");
794 SYSCTL_PROC(_kern_ntp, OID_AUTO, delta,
795 CTLTYPE_QUAD|CTLFLAG_RW, 0, 0,
796 sysctl_delta, "Q", "one-time delta");
797 SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, big_delta, CTLFLAG_RD,
798 &ntp_big_delta, sizeof(ntp_big_delta), "Q",
799 "threshold for fast adjustment");
800 SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, tick_delta, CTLFLAG_RD,
801 &ntp_tick_delta, sizeof(ntp_tick_delta), "LU",
802 "per-tick adjustment");
803 SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, default_tick_delta, CTLFLAG_RD,
804 &ntp_default_tick_delta, sizeof(ntp_default_tick_delta), "LU",
805 "default per-tick adjustment");
806 SYSCTL_OPAQUE(_kern_ntp, OID_AUTO, next_leap_second, CTLFLAG_RW,
807 &ntp_leap_second, sizeof(ntp_leap_second), "LU",
809 SYSCTL_INT(_kern_ntp, OID_AUTO, insert_leap_second, CTLFLAG_RW,
810 &ntp_leap_insert, 0, "insert or remove leap second");
811 SYSCTL_PROC(_kern_ntp, OID_AUTO, adjust,
812 CTLTYPE_QUAD|CTLFLAG_RW, 0, 0,
813 sysctl_adjtime, "Q", "relative adjust for delta");
816 * Get value of an interval timer. The process virtual and
817 * profiling virtual time timers are kept in the p_stats area, since
818 * they can be swapped out. These are kept internally in the
819 * way they are specified externally: in time until they expire.
821 * The real time interval timer is kept in the process table slot
822 * for the process, and its value (it_value) is kept as an
823 * absolute time rather than as a delta, so that it is easy to keep
824 * periodic real-time signals from drifting.
826 * Virtual time timers are processed in the hardclock() routine of
827 * kern_clock.c. The real time timer is processed by a timeout
828 * routine, called from the softclock() routine. Since a callout
829 * may be delayed in real time due to interrupt processing in the system,
830 * it is possible for the real time timeout routine (realitexpire, given below),
831 * to be delayed in real time past when it is supposed to occur. It
832 * does not suffice, therefore, to reload the real timer .it_value from the
833 * real time timers .it_interval. Rather, we compute the next time in
834 * absolute time the timer should go off.
839 sys_getitimer(struct sysmsg *sysmsg, const struct getitimer_args *uap)
841 struct proc *p = curproc;
843 struct itimerval aitv;
845 if (uap->which > ITIMER_PROF)
847 lwkt_gettoken(&p->p_token);
848 if (uap->which == ITIMER_REAL) {
850 * Convert from absolute to relative time in .it_value
851 * part of real time timer. If time for real time timer
852 * has passed return 0, else return difference between
853 * current time and time for the timer to go off.
855 aitv = p->p_realtimer;
856 if (timevalisset(&aitv.it_value)) {
857 getmicrouptime(&ctv);
858 if (timevalcmp(&aitv.it_value, &ctv, <))
859 timevalclear(&aitv.it_value);
861 timevalsub(&aitv.it_value, &ctv);
864 aitv = p->p_timer[uap->which];
866 lwkt_reltoken(&p->p_token);
867 return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
874 sys_setitimer(struct sysmsg *sysmsg, const struct setitimer_args *uap)
876 struct itimerval aitv;
878 struct itimerval *itvp;
879 struct proc *p = curproc;
880 struct getitimer_args gitargs;
883 if (uap->which > ITIMER_PROF)
886 if (itvp && (error = copyin((caddr_t)itvp, (caddr_t)&aitv,
887 sizeof(struct itimerval))))
891 gitargs.which = uap->which;
892 gitargs.itv = uap->oitv;
893 error = sys_getitimer(sysmsg, &gitargs);
899 if (itimerfix(&aitv.it_value))
901 if (!timevalisset(&aitv.it_value))
902 timevalclear(&aitv.it_interval);
903 else if (itimerfix(&aitv.it_interval))
905 lwkt_gettoken(&p->p_token);
906 if (uap->which == ITIMER_REAL) {
907 if (timevalisset(&p->p_realtimer.it_value))
908 callout_cancel(&p->p_ithandle);
909 if (timevalisset(&aitv.it_value))
910 callout_reset(&p->p_ithandle,
911 tvtohz_high(&aitv.it_value), realitexpire, p);
912 getmicrouptime(&ctv);
913 timevaladd(&aitv.it_value, &ctv);
914 p->p_realtimer = aitv;
916 p->p_timer[uap->which] = aitv;
919 p->p_flags &= ~P_SIGVTALRM;
922 p->p_flags &= ~P_SIGPROF;
926 lwkt_reltoken(&p->p_token);
931 * Real interval timer expired:
932 * send process whose timer expired an alarm signal.
933 * If time is not set up to reload, then just return.
934 * Else compute next time timer should go off which is > current time.
935 * This is where delay in processing this timeout causes multiple
936 * SIGALRM calls to be compressed into one.
937 * tvtohz_high() always adds 1 to allow for the time until the next clock
938 * interrupt being strictly less than 1 clock tick, but we don't want
939 * that here since we want to appear to be in sync with the clock
940 * interrupt even when we're delayed.
944 realitexpire(void *arg)
947 struct timeval ctv, ntv;
949 p = (struct proc *)arg;
951 lwkt_gettoken(&p->p_token);
953 if (!timevalisset(&p->p_realtimer.it_interval)) {
954 timevalclear(&p->p_realtimer.it_value);
958 timevaladd(&p->p_realtimer.it_value,
959 &p->p_realtimer.it_interval);
960 getmicrouptime(&ctv);
961 if (timevalcmp(&p->p_realtimer.it_value, &ctv, >)) {
962 ntv = p->p_realtimer.it_value;
963 timevalsub(&ntv, &ctv);
964 callout_reset(&p->p_ithandle, tvtohz_low(&ntv),
970 lwkt_reltoken(&p->p_token);
975 * Used to validate itimer timeouts and utimes*() timespecs.
978 itimerfix(struct timeval *tv)
980 if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
982 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < ustick)
983 tv->tv_usec = ustick;
988 * Used to validate timeouts and utimes*() timespecs.
991 itimespecfix(struct timespec *ts)
993 if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000ULL)
995 if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < nstick)
996 ts->tv_nsec = nstick;
1001 * Decrement an interval timer by a specified number
1002 * of microseconds, which must be less than a second,
1003 * i.e. < 1000000. If the timer expires, then reload
1004 * it. In this case, carry over (usec - old value) to
1005 * reduce the value reloaded into the timer so that
1006 * the timer does not drift. This routine assumes
1007 * that it is called in a context where the timers
1008 * on which it is operating cannot change in value.
1011 itimerdecr(struct itimerval *itp, int usec)
1014 if (itp->it_value.tv_usec < usec) {
1015 if (itp->it_value.tv_sec == 0) {
1016 /* expired, and already in next interval */
1017 usec -= itp->it_value.tv_usec;
1020 itp->it_value.tv_usec += 1000000;
1021 itp->it_value.tv_sec--;
1023 itp->it_value.tv_usec -= usec;
1025 if (timevalisset(&itp->it_value))
1027 /* expired, exactly at end of interval */
1029 if (timevalisset(&itp->it_interval)) {
1030 itp->it_value = itp->it_interval;
1031 itp->it_value.tv_usec -= usec;
1032 if (itp->it_value.tv_usec < 0) {
1033 itp->it_value.tv_usec += 1000000;
1034 itp->it_value.tv_sec--;
1037 itp->it_value.tv_usec = 0; /* sec is already 0 */
1042 * Add and subtract routines for timevals.
1043 * N.B.: subtract routine doesn't deal with
1044 * results which are before the beginning,
1045 * it just gets very confused in this case.
1049 timevaladd(struct timeval *t1, const struct timeval *t2)
1052 t1->tv_sec += t2->tv_sec;
1053 t1->tv_usec += t2->tv_usec;
1058 timevalsub(struct timeval *t1, const struct timeval *t2)
1061 t1->tv_sec -= t2->tv_sec;
1062 t1->tv_usec -= t2->tv_usec;
1067 timevalfix(struct timeval *t1)
1070 if (t1->tv_usec < 0) {
1072 t1->tv_usec += 1000000;
1074 if (t1->tv_usec >= 1000000) {
1076 t1->tv_usec -= 1000000;
1081 * ratecheck(): simple time-based rate-limit checking.
1084 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
1086 struct timeval tv, delta;
1089 getmicrouptime(&tv); /* NB: 10ms precision */
1091 timevalsub(&delta, lasttime);
1094 * check for 0,0 is so that the message will be seen at least once,
1095 * even if interval is huge.
1097 if (timevalcmp(&delta, mininterval, >=) ||
1098 (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1107 * ppsratecheck(): packets (or events) per second limitation.
1109 * Return 0 if the limit is to be enforced (e.g. the caller
1110 * should drop a packet because of the rate limitation).
1112 * maxpps of 0 always causes zero to be returned. maxpps of -1
1113 * always causes 1 to be returned; this effectively defeats rate
1116 * Note that we maintain the struct timeval for compatibility
1117 * with other bsd systems. We reuse the storage and just monitor
1118 * clock ticks for minimal overhead.
1121 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
1126 * Reset the last time and counter if this is the first call
1127 * or more than a second has passed since the last update of
1131 if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
1132 lasttime->tv_sec = now;
1134 return (maxpps != 0);
1136 (*curpps)++; /* NB: ignore potential overflow */
1137 return (maxpps < 0 || *curpps < maxpps);
1142 sysctl_gettimeofday_quick(SYSCTL_HANDLER_ARGS)
1147 gtod = gettimeofday_quick;
1148 error = sysctl_handle_int(oidp, >od, 0, req);
1149 if (error || req->newptr == NULL)
1151 gettimeofday_quick = gtod;
1153 kpmap->fast_gtod = gtod;