10Sstevel@tonic-gate /*
26422Sqiao * CDDL HEADER START
36422Sqiao *
46422Sqiao * The contents of this file are subject to the terms of the
56422Sqiao * Common Development and Distribution License (the "License").
66422Sqiao * You may not use this file except in compliance with the License.
76422Sqiao *
86422Sqiao * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96422Sqiao * or http://www.opensolaris.org/os/licensing.
106422Sqiao * See the License for the specific language governing permissions
116422Sqiao * and limitations under the License.
126422Sqiao *
136422Sqiao * When distributing Covered Code, include this CDDL HEADER in each
146422Sqiao * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156422Sqiao * If applicable, add the following below this CDDL HEADER, with the
166422Sqiao * fields enclosed by brackets "[]" replaced with your own identifying
176422Sqiao * information: Portions Copyright [yyyy] [name of copyright owner]
186422Sqiao *
196422Sqiao * CDDL HEADER END
206422Sqiao */
219870SRoger.Faulkner@Sun.COM
226422Sqiao /*
23*11752STrevor.Thompson@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate * Copyright (c) 1982, 1986 Regents of the University of California.
290Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
300Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
310Sstevel@tonic-gate */
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include <sys/param.h>
340Sstevel@tonic-gate #include <sys/user.h>
350Sstevel@tonic-gate #include <sys/vnode.h>
360Sstevel@tonic-gate #include <sys/proc.h>
370Sstevel@tonic-gate #include <sys/time.h>
380Sstevel@tonic-gate #include <sys/systm.h>
390Sstevel@tonic-gate #include <sys/kmem.h>
400Sstevel@tonic-gate #include <sys/cmn_err.h>
410Sstevel@tonic-gate #include <sys/cpuvar.h>
420Sstevel@tonic-gate #include <sys/timer.h>
430Sstevel@tonic-gate #include <sys/debug.h>
440Sstevel@tonic-gate #include <sys/sysmacros.h>
450Sstevel@tonic-gate #include <sys/cyclic.h>
460Sstevel@tonic-gate
470Sstevel@tonic-gate static void realitexpire(void *);
480Sstevel@tonic-gate static void realprofexpire(void *);
490Sstevel@tonic-gate static void timeval_advance(struct timeval *, struct timeval *);
500Sstevel@tonic-gate
510Sstevel@tonic-gate kmutex_t tod_lock; /* protects time-of-day stuff */
520Sstevel@tonic-gate
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate * Constant to define the minimum interval value of the ITIMER_REALPROF timer.
550Sstevel@tonic-gate * Value is in microseconds; defaults to 500 usecs. Setting this value
560Sstevel@tonic-gate * significantly lower may allow for denial-of-service attacks.
570Sstevel@tonic-gate */
580Sstevel@tonic-gate int itimer_realprof_minimum = 500;
590Sstevel@tonic-gate
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate * macro to compare a timeval to a timestruc
620Sstevel@tonic-gate */
630Sstevel@tonic-gate
640Sstevel@tonic-gate #define TVTSCMP(tvp, tsp, cmp) \
650Sstevel@tonic-gate /* CSTYLED */ \
660Sstevel@tonic-gate ((tvp)->tv_sec cmp (tsp)->tv_sec || \
670Sstevel@tonic-gate ((tvp)->tv_sec == (tsp)->tv_sec && \
680Sstevel@tonic-gate /* CSTYLED */ \
690Sstevel@tonic-gate (tvp)->tv_usec * 1000 cmp (tsp)->tv_nsec))
700Sstevel@tonic-gate
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate * Time of day and interval timer support.
730Sstevel@tonic-gate *
740Sstevel@tonic-gate * These routines provide the kernel entry points to get and set
750Sstevel@tonic-gate * the time-of-day and per-process interval timers. Subroutines
760Sstevel@tonic-gate * here provide support for adding and subtracting timeval structures
770Sstevel@tonic-gate * and decrementing interval timers, optionally reloading the interval
780Sstevel@tonic-gate * timers when they expire.
790Sstevel@tonic-gate */
800Sstevel@tonic-gate
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate * SunOS function to generate monotonically increasing time values.
830Sstevel@tonic-gate */
840Sstevel@tonic-gate void
uniqtime(struct timeval * tv)850Sstevel@tonic-gate uniqtime(struct timeval *tv)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate static struct timeval last;
887900SBirva.Shah@Sun.COM static int last_timechanged;
890Sstevel@tonic-gate timestruc_t ts;
900Sstevel@tonic-gate time_t sec;
910Sstevel@tonic-gate int usec, nsec;
920Sstevel@tonic-gate
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate * protect modification of last
950Sstevel@tonic-gate */
960Sstevel@tonic-gate mutex_enter(&tod_lock);
970Sstevel@tonic-gate gethrestime(&ts);
980Sstevel@tonic-gate
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate * Fast algorithm to convert nsec to usec -- see hrt2ts()
1010Sstevel@tonic-gate * in common/os/timers.c for a full description.
1020Sstevel@tonic-gate */
1030Sstevel@tonic-gate nsec = ts.tv_nsec;
1040Sstevel@tonic-gate usec = nsec + (nsec >> 2);
1050Sstevel@tonic-gate usec = nsec + (usec >> 1);
1060Sstevel@tonic-gate usec = nsec + (usec >> 2);
1070Sstevel@tonic-gate usec = nsec + (usec >> 4);
1080Sstevel@tonic-gate usec = nsec - (usec >> 3);
1090Sstevel@tonic-gate usec = nsec + (usec >> 2);
1100Sstevel@tonic-gate usec = nsec + (usec >> 3);
1110Sstevel@tonic-gate usec = nsec + (usec >> 4);
1120Sstevel@tonic-gate usec = nsec + (usec >> 1);
1130Sstevel@tonic-gate usec = nsec + (usec >> 6);
1140Sstevel@tonic-gate usec = usec >> 10;
1150Sstevel@tonic-gate sec = ts.tv_sec;
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate /*
1187900SBirva.Shah@Sun.COM * If the system hres time has been changed since the last time
1197900SBirva.Shah@Sun.COM * we are called. then all bets are off; just update our
1207900SBirva.Shah@Sun.COM * local copy of timechanged and accept the reported time as is.
1217900SBirva.Shah@Sun.COM */
1227900SBirva.Shah@Sun.COM if (last_timechanged != timechanged) {
1237900SBirva.Shah@Sun.COM last_timechanged = timechanged;
1247900SBirva.Shah@Sun.COM }
1257900SBirva.Shah@Sun.COM /*
1260Sstevel@tonic-gate * Try to keep timestamps unique, but don't be obsessive about
1270Sstevel@tonic-gate * it in the face of large differences.
1280Sstevel@tonic-gate */
1297900SBirva.Shah@Sun.COM else if ((sec <= last.tv_sec) && /* same or lower seconds, and */
1300Sstevel@tonic-gate ((sec != last.tv_sec) || /* either different second or */
1310Sstevel@tonic-gate (usec <= last.tv_usec)) && /* lower microsecond, and */
1320Sstevel@tonic-gate ((last.tv_sec - sec) <= 5)) { /* not way back in time */
1330Sstevel@tonic-gate sec = last.tv_sec;
1340Sstevel@tonic-gate usec = last.tv_usec + 1;
1350Sstevel@tonic-gate if (usec >= MICROSEC) {
1360Sstevel@tonic-gate usec -= MICROSEC;
1370Sstevel@tonic-gate sec++;
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate last.tv_sec = sec;
1410Sstevel@tonic-gate last.tv_usec = usec;
1420Sstevel@tonic-gate mutex_exit(&tod_lock);
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate tv->tv_sec = sec;
1450Sstevel@tonic-gate tv->tv_usec = usec;
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate * Timestamps are exported from the kernel in several places.
1500Sstevel@tonic-gate * Such timestamps are commonly used for either uniqueness or for
1510Sstevel@tonic-gate * sequencing - truncation to 32-bits is fine for uniqueness,
1520Sstevel@tonic-gate * but sequencing is going to take more work as we get closer to 2038!
1530Sstevel@tonic-gate */
1540Sstevel@tonic-gate void
uniqtime32(struct timeval32 * tv32p)1550Sstevel@tonic-gate uniqtime32(struct timeval32 *tv32p)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate struct timeval tv;
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate uniqtime(&tv);
1600Sstevel@tonic-gate TIMEVAL_TO_TIMEVAL32(tv32p, &tv);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate int
gettimeofday(struct timeval * tp)1640Sstevel@tonic-gate gettimeofday(struct timeval *tp)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate struct timeval atv;
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate if (tp) {
1690Sstevel@tonic-gate uniqtime(&atv);
1700Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE) {
1710Sstevel@tonic-gate if (copyout(&atv, tp, sizeof (atv)))
1720Sstevel@tonic-gate return (set_errno(EFAULT));
1730Sstevel@tonic-gate } else {
1740Sstevel@tonic-gate struct timeval32 tv32;
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate if (TIMEVAL_OVERFLOW(&atv))
1770Sstevel@tonic-gate return (set_errno(EOVERFLOW));
1780Sstevel@tonic-gate TIMEVAL_TO_TIMEVAL32(&tv32, &atv);
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate if (copyout(&tv32, tp, sizeof (tv32)))
1810Sstevel@tonic-gate return (set_errno(EFAULT));
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate return (0);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate int
getitimer(uint_t which,struct itimerval * itv)1880Sstevel@tonic-gate getitimer(uint_t which, struct itimerval *itv)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate int error;
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE)
1930Sstevel@tonic-gate error = xgetitimer(which, itv, 0);
1940Sstevel@tonic-gate else {
1950Sstevel@tonic-gate struct itimerval kitv;
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate if ((error = xgetitimer(which, &kitv, 1)) == 0) {
1980Sstevel@tonic-gate if (ITIMERVAL_OVERFLOW(&kitv)) {
1990Sstevel@tonic-gate error = EOVERFLOW;
2000Sstevel@tonic-gate } else {
2010Sstevel@tonic-gate struct itimerval32 itv32;
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate ITIMERVAL_TO_ITIMERVAL32(&itv32, &kitv);
2040Sstevel@tonic-gate if (copyout(&itv32, itv, sizeof (itv32)) != 0)
2050Sstevel@tonic-gate error = EFAULT;
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate return (error ? (set_errno(error)) : 0);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate int
xgetitimer(uint_t which,struct itimerval * itv,int iskaddr)2140Sstevel@tonic-gate xgetitimer(uint_t which, struct itimerval *itv, int iskaddr)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate struct proc *p = curproc;
2170Sstevel@tonic-gate struct timeval now;
2180Sstevel@tonic-gate struct itimerval aitv;
2190Sstevel@tonic-gate hrtime_t ts, first, interval, remain;
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate mutex_enter(&p->p_lock);
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate switch (which) {
2240Sstevel@tonic-gate case ITIMER_VIRTUAL:
2250Sstevel@tonic-gate case ITIMER_PROF:
2260Sstevel@tonic-gate aitv = ttolwp(curthread)->lwp_timer[which];
2270Sstevel@tonic-gate break;
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate case ITIMER_REAL:
2300Sstevel@tonic-gate uniqtime(&now);
2310Sstevel@tonic-gate aitv = p->p_realitimer;
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate if (timerisset(&aitv.it_value)) {
2340Sstevel@tonic-gate /*CSTYLED*/
2350Sstevel@tonic-gate if (timercmp(&aitv.it_value, &now, <)) {
2360Sstevel@tonic-gate timerclear(&aitv.it_value);
2370Sstevel@tonic-gate } else {
2380Sstevel@tonic-gate timevalsub(&aitv.it_value, &now);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate break;
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate case ITIMER_REALPROF:
2440Sstevel@tonic-gate if (curproc->p_rprof_cyclic == CYCLIC_NONE) {
2450Sstevel@tonic-gate bzero(&aitv, sizeof (aitv));
2460Sstevel@tonic-gate break;
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate aitv = curproc->p_rprof_timer;
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate first = tv2hrt(&aitv.it_value);
2520Sstevel@tonic-gate interval = tv2hrt(&aitv.it_interval);
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate if ((ts = gethrtime()) < first) {
2550Sstevel@tonic-gate /*
2560Sstevel@tonic-gate * We haven't gone off for the first time; the time
2570Sstevel@tonic-gate * remaining is simply the first time we will go
2580Sstevel@tonic-gate * off minus the current time.
2590Sstevel@tonic-gate */
2600Sstevel@tonic-gate remain = first - ts;
2610Sstevel@tonic-gate } else {
2620Sstevel@tonic-gate if (interval == 0) {
2630Sstevel@tonic-gate /*
2640Sstevel@tonic-gate * This was set as a one-shot, and we've
2650Sstevel@tonic-gate * already gone off; there is no time
2660Sstevel@tonic-gate * remaining.
2670Sstevel@tonic-gate */
2680Sstevel@tonic-gate remain = 0;
2690Sstevel@tonic-gate } else {
2700Sstevel@tonic-gate /*
2710Sstevel@tonic-gate * We have a non-zero interval; we need to
2720Sstevel@tonic-gate * determine how far we are into the current
2730Sstevel@tonic-gate * interval, and subtract that from the
2740Sstevel@tonic-gate * interval to determine the time remaining.
2750Sstevel@tonic-gate */
2760Sstevel@tonic-gate remain = interval - ((ts - first) % interval);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate hrt2tv(remain, &aitv.it_value);
2810Sstevel@tonic-gate break;
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate default:
2840Sstevel@tonic-gate mutex_exit(&p->p_lock);
2850Sstevel@tonic-gate return (EINVAL);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate mutex_exit(&p->p_lock);
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate if (iskaddr) {
2910Sstevel@tonic-gate bcopy(&aitv, itv, sizeof (*itv));
2920Sstevel@tonic-gate } else {
2930Sstevel@tonic-gate ASSERT(get_udatamodel() == DATAMODEL_NATIVE);
2940Sstevel@tonic-gate if (copyout(&aitv, itv, sizeof (*itv)))
2950Sstevel@tonic-gate return (EFAULT);
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate return (0);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate int
setitimer(uint_t which,struct itimerval * itv,struct itimerval * oitv)3030Sstevel@tonic-gate setitimer(uint_t which, struct itimerval *itv, struct itimerval *oitv)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate int error;
3060Sstevel@tonic-gate
3070Sstevel@tonic-gate if (oitv != NULL)
3080Sstevel@tonic-gate if ((error = getitimer(which, oitv)) != 0)
3090Sstevel@tonic-gate return (error);
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate if (itv == NULL)
3120Sstevel@tonic-gate return (0);
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate if (get_udatamodel() == DATAMODEL_NATIVE)
3150Sstevel@tonic-gate error = xsetitimer(which, itv, 0);
3160Sstevel@tonic-gate else {
3170Sstevel@tonic-gate struct itimerval32 itv32;
3180Sstevel@tonic-gate struct itimerval kitv;
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate if (copyin(itv, &itv32, sizeof (itv32)))
3210Sstevel@tonic-gate error = EFAULT;
3220Sstevel@tonic-gate ITIMERVAL32_TO_ITIMERVAL(&kitv, &itv32);
3230Sstevel@tonic-gate error = xsetitimer(which, &kitv, 1);
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate return (error ? (set_errno(error)) : 0);
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate int
xsetitimer(uint_t which,struct itimerval * itv,int iskaddr)3300Sstevel@tonic-gate xsetitimer(uint_t which, struct itimerval *itv, int iskaddr)
3310Sstevel@tonic-gate {
3320Sstevel@tonic-gate struct itimerval aitv;
3330Sstevel@tonic-gate struct timeval now;
3340Sstevel@tonic-gate struct proc *p = curproc;
3350Sstevel@tonic-gate kthread_t *t;
3360Sstevel@tonic-gate timeout_id_t tmp_id;
3370Sstevel@tonic-gate cyc_handler_t hdlr;
3380Sstevel@tonic-gate cyc_time_t when;
3390Sstevel@tonic-gate cyclic_id_t cyclic;
3400Sstevel@tonic-gate hrtime_t ts;
3410Sstevel@tonic-gate int min;
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate if (itv == NULL)
3440Sstevel@tonic-gate return (0);
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate if (iskaddr) {
3470Sstevel@tonic-gate bcopy(itv, &aitv, sizeof (aitv));
3480Sstevel@tonic-gate } else {
3490Sstevel@tonic-gate ASSERT(get_udatamodel() == DATAMODEL_NATIVE);
3500Sstevel@tonic-gate if (copyin(itv, &aitv, sizeof (aitv)))
3510Sstevel@tonic-gate return (EFAULT);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate if (which == ITIMER_REALPROF) {
3550Sstevel@tonic-gate min = MAX((int)(cyclic_getres() / (NANOSEC / MICROSEC)),
3560Sstevel@tonic-gate itimer_realprof_minimum);
3570Sstevel@tonic-gate } else {
3580Sstevel@tonic-gate min = usec_per_tick;
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate
3610Sstevel@tonic-gate if (itimerfix(&aitv.it_value, min) ||
3620Sstevel@tonic-gate (itimerfix(&aitv.it_interval, min) && timerisset(&aitv.it_value)))
3630Sstevel@tonic-gate return (EINVAL);
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate mutex_enter(&p->p_lock);
3660Sstevel@tonic-gate switch (which) {
3670Sstevel@tonic-gate case ITIMER_REAL:
3680Sstevel@tonic-gate /*
3690Sstevel@tonic-gate * The SITBUSY flag prevents conflicts with multiple
3700Sstevel@tonic-gate * threads attempting to perform setitimer(ITIMER_REAL)
3710Sstevel@tonic-gate * at the same time, even when we drop p->p_lock below.
3720Sstevel@tonic-gate * Any blocked thread returns successfully because the
3730Sstevel@tonic-gate * effect is the same as if it got here first, finished,
3740Sstevel@tonic-gate * and the other thread then came through and destroyed
3750Sstevel@tonic-gate * what it did. We are just protecting the system from
3760Sstevel@tonic-gate * malfunctioning due to the race condition.
3770Sstevel@tonic-gate */
3780Sstevel@tonic-gate if (p->p_flag & SITBUSY) {
3790Sstevel@tonic-gate mutex_exit(&p->p_lock);
3800Sstevel@tonic-gate return (0);
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate p->p_flag |= SITBUSY;
3830Sstevel@tonic-gate while ((tmp_id = p->p_itimerid) != 0) {
3840Sstevel@tonic-gate /*
3850Sstevel@tonic-gate * Avoid deadlock in callout_delete (called from
3860Sstevel@tonic-gate * untimeout) which may go to sleep (while holding
3870Sstevel@tonic-gate * p_lock). Drop p_lock and re-acquire it after
3880Sstevel@tonic-gate * untimeout returns. Need to clear p_itimerid
3890Sstevel@tonic-gate * while holding p_lock.
3900Sstevel@tonic-gate */
3910Sstevel@tonic-gate p->p_itimerid = 0;
3920Sstevel@tonic-gate mutex_exit(&p->p_lock);
3930Sstevel@tonic-gate (void) untimeout(tmp_id);
3940Sstevel@tonic-gate mutex_enter(&p->p_lock);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate if (timerisset(&aitv.it_value)) {
3970Sstevel@tonic-gate uniqtime(&now);
3980Sstevel@tonic-gate timevaladd(&aitv.it_value, &now);
3990Sstevel@tonic-gate p->p_itimerid = realtime_timeout(realitexpire,
4000Sstevel@tonic-gate p, hzto(&aitv.it_value));
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate p->p_realitimer = aitv;
4030Sstevel@tonic-gate p->p_flag &= ~SITBUSY;
4040Sstevel@tonic-gate break;
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate case ITIMER_REALPROF:
4070Sstevel@tonic-gate cyclic = p->p_rprof_cyclic;
4080Sstevel@tonic-gate p->p_rprof_cyclic = CYCLIC_NONE;
4090Sstevel@tonic-gate
4100Sstevel@tonic-gate mutex_exit(&p->p_lock);
4110Sstevel@tonic-gate
4120Sstevel@tonic-gate /*
4130Sstevel@tonic-gate * We're now going to acquire cpu_lock, remove the old cyclic
4140Sstevel@tonic-gate * if necessary, and add our new cyclic.
4150Sstevel@tonic-gate */
4160Sstevel@tonic-gate mutex_enter(&cpu_lock);
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate if (cyclic != CYCLIC_NONE)
4190Sstevel@tonic-gate cyclic_remove(cyclic);
4200Sstevel@tonic-gate
4210Sstevel@tonic-gate if (!timerisset(&aitv.it_value)) {
4220Sstevel@tonic-gate /*
4230Sstevel@tonic-gate * If we were passed a value of 0, we're done.
4240Sstevel@tonic-gate */
4250Sstevel@tonic-gate mutex_exit(&cpu_lock);
4260Sstevel@tonic-gate return (0);
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate
4290Sstevel@tonic-gate hdlr.cyh_func = realprofexpire;
4300Sstevel@tonic-gate hdlr.cyh_arg = p;
4310Sstevel@tonic-gate hdlr.cyh_level = CY_LOW_LEVEL;
4320Sstevel@tonic-gate
4330Sstevel@tonic-gate when.cyt_when = (ts = gethrtime() + tv2hrt(&aitv.it_value));
4340Sstevel@tonic-gate when.cyt_interval = tv2hrt(&aitv.it_interval);
4350Sstevel@tonic-gate
4360Sstevel@tonic-gate if (when.cyt_interval == 0) {
4370Sstevel@tonic-gate /*
4380Sstevel@tonic-gate * Using the same logic as for CLOCK_HIGHRES timers, we
4390Sstevel@tonic-gate * set the interval to be INT64_MAX - when.cyt_when to
4400Sstevel@tonic-gate * effect a one-shot; see the comment in clock_highres.c
4410Sstevel@tonic-gate * for more details on why this works.
4420Sstevel@tonic-gate */
4430Sstevel@tonic-gate when.cyt_interval = INT64_MAX - when.cyt_when;
4440Sstevel@tonic-gate }
4450Sstevel@tonic-gate
4460Sstevel@tonic-gate cyclic = cyclic_add(&hdlr, &when);
4470Sstevel@tonic-gate
4480Sstevel@tonic-gate mutex_exit(&cpu_lock);
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate /*
4510Sstevel@tonic-gate * We have now successfully added the cyclic. Reacquire
4520Sstevel@tonic-gate * p_lock, and see if anyone has snuck in.
4530Sstevel@tonic-gate */
4540Sstevel@tonic-gate mutex_enter(&p->p_lock);
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate if (p->p_rprof_cyclic != CYCLIC_NONE) {
4570Sstevel@tonic-gate /*
4580Sstevel@tonic-gate * We're racing with another thread establishing an
4590Sstevel@tonic-gate * ITIMER_REALPROF interval timer. We'll let the other
4600Sstevel@tonic-gate * thread win (this is a race at the application level,
4610Sstevel@tonic-gate * so letting the other thread win is acceptable).
4620Sstevel@tonic-gate */
4630Sstevel@tonic-gate mutex_exit(&p->p_lock);
4640Sstevel@tonic-gate mutex_enter(&cpu_lock);
4650Sstevel@tonic-gate cyclic_remove(cyclic);
4660Sstevel@tonic-gate mutex_exit(&cpu_lock);
4670Sstevel@tonic-gate
4680Sstevel@tonic-gate return (0);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate
4710Sstevel@tonic-gate /*
4720Sstevel@tonic-gate * Success. Set our tracking variables in the proc structure,
4730Sstevel@tonic-gate * cancel any outstanding ITIMER_PROF, and allocate the
4740Sstevel@tonic-gate * per-thread SIGPROF buffers, if possible.
4750Sstevel@tonic-gate */
4760Sstevel@tonic-gate hrt2tv(ts, &aitv.it_value);
4770Sstevel@tonic-gate p->p_rprof_timer = aitv;
4780Sstevel@tonic-gate p->p_rprof_cyclic = cyclic;
4790Sstevel@tonic-gate
4800Sstevel@tonic-gate t = p->p_tlist;
4810Sstevel@tonic-gate do {
4820Sstevel@tonic-gate struct itimerval *itvp;
4830Sstevel@tonic-gate
4840Sstevel@tonic-gate itvp = &ttolwp(t)->lwp_timer[ITIMER_PROF];
4850Sstevel@tonic-gate timerclear(&itvp->it_interval);
4860Sstevel@tonic-gate timerclear(&itvp->it_value);
4870Sstevel@tonic-gate
4880Sstevel@tonic-gate if (t->t_rprof != NULL)
4890Sstevel@tonic-gate continue;
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate t->t_rprof =
4920Sstevel@tonic-gate kmem_zalloc(sizeof (struct rprof), KM_NOSLEEP);
4930Sstevel@tonic-gate aston(t);
4940Sstevel@tonic-gate } while ((t = t->t_forw) != p->p_tlist);
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate break;
4970Sstevel@tonic-gate
4980Sstevel@tonic-gate case ITIMER_VIRTUAL:
4990Sstevel@tonic-gate ttolwp(curthread)->lwp_timer[ITIMER_VIRTUAL] = aitv;
5000Sstevel@tonic-gate break;
5010Sstevel@tonic-gate
5020Sstevel@tonic-gate case ITIMER_PROF:
5030Sstevel@tonic-gate if (p->p_rprof_cyclic != CYCLIC_NONE) {
5040Sstevel@tonic-gate /*
5050Sstevel@tonic-gate * Silently ignore ITIMER_PROF if ITIMER_REALPROF
5060Sstevel@tonic-gate * is in effect.
5070Sstevel@tonic-gate */
5080Sstevel@tonic-gate break;
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate
5110Sstevel@tonic-gate ttolwp(curthread)->lwp_timer[ITIMER_PROF] = aitv;
5120Sstevel@tonic-gate break;
5130Sstevel@tonic-gate
5140Sstevel@tonic-gate default:
5150Sstevel@tonic-gate mutex_exit(&p->p_lock);
5160Sstevel@tonic-gate return (EINVAL);
5170Sstevel@tonic-gate }
5180Sstevel@tonic-gate mutex_exit(&p->p_lock);
5190Sstevel@tonic-gate return (0);
5200Sstevel@tonic-gate }
5210Sstevel@tonic-gate
5220Sstevel@tonic-gate /*
5239870SRoger.Faulkner@Sun.COM * Delete the ITIMER_REALPROF interval timer.
5249870SRoger.Faulkner@Sun.COM * Called only from exec_args() when exec occurs.
5259870SRoger.Faulkner@Sun.COM * The other ITIMER_* interval timers are specified
5269870SRoger.Faulkner@Sun.COM * to be inherited across exec(), so leave them alone.
5279870SRoger.Faulkner@Sun.COM */
5289870SRoger.Faulkner@Sun.COM void
delete_itimer_realprof(void)5299870SRoger.Faulkner@Sun.COM delete_itimer_realprof(void)
5309870SRoger.Faulkner@Sun.COM {
5319870SRoger.Faulkner@Sun.COM kthread_t *t = curthread;
5329870SRoger.Faulkner@Sun.COM struct proc *p = ttoproc(t);
5339870SRoger.Faulkner@Sun.COM klwp_t *lwp = ttolwp(t);
5349870SRoger.Faulkner@Sun.COM cyclic_id_t cyclic;
5359870SRoger.Faulkner@Sun.COM
5369870SRoger.Faulkner@Sun.COM mutex_enter(&p->p_lock);
5379870SRoger.Faulkner@Sun.COM
5389870SRoger.Faulkner@Sun.COM /* we are performing execve(); assert we are single-threaded */
5399870SRoger.Faulkner@Sun.COM ASSERT(t == p->p_tlist && t == t->t_forw);
5409870SRoger.Faulkner@Sun.COM
5419870SRoger.Faulkner@Sun.COM if ((cyclic = p->p_rprof_cyclic) == CYCLIC_NONE) {
5429870SRoger.Faulkner@Sun.COM mutex_exit(&p->p_lock);
5439870SRoger.Faulkner@Sun.COM } else {
5449870SRoger.Faulkner@Sun.COM p->p_rprof_cyclic = CYCLIC_NONE;
5459870SRoger.Faulkner@Sun.COM /*
5469870SRoger.Faulkner@Sun.COM * Delete any current instance of SIGPROF.
5479870SRoger.Faulkner@Sun.COM */
5489870SRoger.Faulkner@Sun.COM if (lwp->lwp_cursig == SIGPROF) {
5499870SRoger.Faulkner@Sun.COM lwp->lwp_cursig = 0;
5509870SRoger.Faulkner@Sun.COM lwp->lwp_extsig = 0;
5519870SRoger.Faulkner@Sun.COM if (lwp->lwp_curinfo) {
5529870SRoger.Faulkner@Sun.COM siginfofree(lwp->lwp_curinfo);
5539870SRoger.Faulkner@Sun.COM lwp->lwp_curinfo = NULL;
5549870SRoger.Faulkner@Sun.COM }
5559870SRoger.Faulkner@Sun.COM }
5569870SRoger.Faulkner@Sun.COM /*
5579870SRoger.Faulkner@Sun.COM * Delete any pending instances of SIGPROF.
5589870SRoger.Faulkner@Sun.COM */
5599870SRoger.Faulkner@Sun.COM sigdelset(&p->p_sig, SIGPROF);
5609870SRoger.Faulkner@Sun.COM sigdelset(&p->p_extsig, SIGPROF);
5619870SRoger.Faulkner@Sun.COM sigdelq(p, NULL, SIGPROF);
5629870SRoger.Faulkner@Sun.COM sigdelset(&t->t_sig, SIGPROF);
5639870SRoger.Faulkner@Sun.COM sigdelset(&t->t_extsig, SIGPROF);
5649870SRoger.Faulkner@Sun.COM sigdelq(p, t, SIGPROF);
5659870SRoger.Faulkner@Sun.COM
5669870SRoger.Faulkner@Sun.COM mutex_exit(&p->p_lock);
5679870SRoger.Faulkner@Sun.COM
5689870SRoger.Faulkner@Sun.COM /*
5699870SRoger.Faulkner@Sun.COM * Remove the ITIMER_REALPROF cyclic.
5709870SRoger.Faulkner@Sun.COM */
5719870SRoger.Faulkner@Sun.COM mutex_enter(&cpu_lock);
5729870SRoger.Faulkner@Sun.COM cyclic_remove(cyclic);
5739870SRoger.Faulkner@Sun.COM mutex_exit(&cpu_lock);
5749870SRoger.Faulkner@Sun.COM }
5759870SRoger.Faulkner@Sun.COM }
5769870SRoger.Faulkner@Sun.COM
5779870SRoger.Faulkner@Sun.COM /*
5780Sstevel@tonic-gate * Real interval timer expired:
5790Sstevel@tonic-gate * send process whose timer expired an alarm signal.
5800Sstevel@tonic-gate * If time is not set up to reload, then just return.
5810Sstevel@tonic-gate * Else compute next time timer should go off which is > current time.
5820Sstevel@tonic-gate * This is where delay in processing this timeout causes multiple
5830Sstevel@tonic-gate * SIGALRM calls to be compressed into one.
5840Sstevel@tonic-gate */
5850Sstevel@tonic-gate static void
realitexpire(void * arg)5860Sstevel@tonic-gate realitexpire(void *arg)
5870Sstevel@tonic-gate {
5880Sstevel@tonic-gate struct proc *p = arg;
5890Sstevel@tonic-gate struct timeval *valp = &p->p_realitimer.it_value;
5900Sstevel@tonic-gate struct timeval *intervalp = &p->p_realitimer.it_interval;
5910Sstevel@tonic-gate #if !defined(_LP64)
5920Sstevel@tonic-gate clock_t ticks;
5930Sstevel@tonic-gate #endif
5940Sstevel@tonic-gate
5950Sstevel@tonic-gate mutex_enter(&p->p_lock);
5960Sstevel@tonic-gate #if !defined(_LP64)
5970Sstevel@tonic-gate if ((ticks = hzto(valp)) > 1) {
5980Sstevel@tonic-gate /*
5990Sstevel@tonic-gate * If we are executing before we were meant to, it must be
6000Sstevel@tonic-gate * because of an overflow in a prior hzto() calculation.
6010Sstevel@tonic-gate * In this case, we want to go to sleep for the recalculated
6020Sstevel@tonic-gate * number of ticks. For the special meaning of the value "1"
6030Sstevel@tonic-gate * see comment in timespectohz().
6040Sstevel@tonic-gate */
6050Sstevel@tonic-gate p->p_itimerid = realtime_timeout(realitexpire, p, ticks);
6060Sstevel@tonic-gate mutex_exit(&p->p_lock);
6070Sstevel@tonic-gate return;
6080Sstevel@tonic-gate }
6090Sstevel@tonic-gate #endif
6100Sstevel@tonic-gate sigtoproc(p, NULL, SIGALRM);
6110Sstevel@tonic-gate if (!timerisset(intervalp)) {
6120Sstevel@tonic-gate timerclear(valp);
6130Sstevel@tonic-gate p->p_itimerid = 0;
6140Sstevel@tonic-gate } else {
6150Sstevel@tonic-gate /* advance timer value past current time */
6160Sstevel@tonic-gate timeval_advance(valp, intervalp);
6170Sstevel@tonic-gate p->p_itimerid = realtime_timeout(realitexpire, p, hzto(valp));
6180Sstevel@tonic-gate }
6190Sstevel@tonic-gate mutex_exit(&p->p_lock);
6200Sstevel@tonic-gate }
6210Sstevel@tonic-gate
6220Sstevel@tonic-gate /*
6230Sstevel@tonic-gate * Real time profiling interval timer expired:
6240Sstevel@tonic-gate * Increment microstate counters for each lwp in the process
6250Sstevel@tonic-gate * and ensure that running lwps are kicked into the kernel.
6260Sstevel@tonic-gate * If time is not set up to reload, then just return.
6270Sstevel@tonic-gate * Else compute next time timer should go off which is > current time,
6280Sstevel@tonic-gate * as above.
6290Sstevel@tonic-gate */
6300Sstevel@tonic-gate static void
realprofexpire(void * arg)6310Sstevel@tonic-gate realprofexpire(void *arg)
6320Sstevel@tonic-gate {
6330Sstevel@tonic-gate struct proc *p = arg;
6340Sstevel@tonic-gate kthread_t *t;
6350Sstevel@tonic-gate
6360Sstevel@tonic-gate mutex_enter(&p->p_lock);
6379870SRoger.Faulkner@Sun.COM if (p->p_rprof_cyclic == CYCLIC_NONE ||
6389870SRoger.Faulkner@Sun.COM (t = p->p_tlist) == NULL) {
6390Sstevel@tonic-gate mutex_exit(&p->p_lock);
6400Sstevel@tonic-gate return;
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate do {
6430Sstevel@tonic-gate int mstate;
6440Sstevel@tonic-gate
6450Sstevel@tonic-gate /*
6460Sstevel@tonic-gate * Attempt to allocate the SIGPROF buffer, but don't sleep.
6470Sstevel@tonic-gate */
6480Sstevel@tonic-gate if (t->t_rprof == NULL)
6490Sstevel@tonic-gate t->t_rprof = kmem_zalloc(sizeof (struct rprof),
6500Sstevel@tonic-gate KM_NOSLEEP);
6510Sstevel@tonic-gate if (t->t_rprof == NULL)
6520Sstevel@tonic-gate continue;
6530Sstevel@tonic-gate
6540Sstevel@tonic-gate thread_lock(t);
6550Sstevel@tonic-gate switch (t->t_state) {
6560Sstevel@tonic-gate case TS_SLEEP:
6570Sstevel@tonic-gate /*
6580Sstevel@tonic-gate * Don't touch the lwp is it is swapped out.
6590Sstevel@tonic-gate */
6600Sstevel@tonic-gate if (!(t->t_schedflag & TS_LOAD)) {
6610Sstevel@tonic-gate mstate = LMS_SLEEP;
6620Sstevel@tonic-gate break;
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate switch (mstate = ttolwp(t)->lwp_mstate.ms_prev) {
6650Sstevel@tonic-gate case LMS_TFAULT:
6660Sstevel@tonic-gate case LMS_DFAULT:
6670Sstevel@tonic-gate case LMS_KFAULT:
6680Sstevel@tonic-gate case LMS_USER_LOCK:
6690Sstevel@tonic-gate break;
6700Sstevel@tonic-gate default:
6710Sstevel@tonic-gate mstate = LMS_SLEEP;
6720Sstevel@tonic-gate break;
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate break;
6750Sstevel@tonic-gate case TS_RUN:
6763792Sakolb case TS_WAIT:
6770Sstevel@tonic-gate mstate = LMS_WAIT_CPU;
6780Sstevel@tonic-gate break;
6790Sstevel@tonic-gate case TS_ONPROC:
6800Sstevel@tonic-gate switch (mstate = t->t_mstate) {
6810Sstevel@tonic-gate case LMS_USER:
6820Sstevel@tonic-gate case LMS_SYSTEM:
6830Sstevel@tonic-gate case LMS_TRAP:
6840Sstevel@tonic-gate break;
6850Sstevel@tonic-gate default:
6860Sstevel@tonic-gate mstate = LMS_SYSTEM;
6870Sstevel@tonic-gate break;
6880Sstevel@tonic-gate }
6890Sstevel@tonic-gate break;
6900Sstevel@tonic-gate default:
6910Sstevel@tonic-gate mstate = t->t_mstate;
6920Sstevel@tonic-gate break;
6930Sstevel@tonic-gate }
6940Sstevel@tonic-gate t->t_rprof->rp_anystate = 1;
6950Sstevel@tonic-gate t->t_rprof->rp_state[mstate]++;
6960Sstevel@tonic-gate aston(t);
6970Sstevel@tonic-gate /*
6980Sstevel@tonic-gate * force the thread into the kernel
6990Sstevel@tonic-gate * if it is not already there.
7000Sstevel@tonic-gate */
7010Sstevel@tonic-gate if (t->t_state == TS_ONPROC && t->t_cpu != CPU)
7020Sstevel@tonic-gate poke_cpu(t->t_cpu->cpu_id);
7030Sstevel@tonic-gate thread_unlock(t);
7040Sstevel@tonic-gate } while ((t = t->t_forw) != p->p_tlist);
7050Sstevel@tonic-gate
7060Sstevel@tonic-gate mutex_exit(&p->p_lock);
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate
7090Sstevel@tonic-gate /*
7100Sstevel@tonic-gate * Advances timer value past the current time of day. See the detailed
7110Sstevel@tonic-gate * comment for this logic in realitsexpire(), above.
7120Sstevel@tonic-gate */
7130Sstevel@tonic-gate static void
timeval_advance(struct timeval * valp,struct timeval * intervalp)7140Sstevel@tonic-gate timeval_advance(struct timeval *valp, struct timeval *intervalp)
7150Sstevel@tonic-gate {
7160Sstevel@tonic-gate int cnt2nth;
7170Sstevel@tonic-gate struct timeval interval2nth;
7180Sstevel@tonic-gate
7190Sstevel@tonic-gate for (;;) {
7200Sstevel@tonic-gate interval2nth = *intervalp;
7210Sstevel@tonic-gate for (cnt2nth = 0; ; cnt2nth++) {
7220Sstevel@tonic-gate timevaladd(valp, &interval2nth);
7230Sstevel@tonic-gate /*CSTYLED*/
7240Sstevel@tonic-gate if (TVTSCMP(valp, &hrestime, >))
7250Sstevel@tonic-gate break;
7260Sstevel@tonic-gate timevaladd(&interval2nth, &interval2nth);
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate if (cnt2nth == 0)
7290Sstevel@tonic-gate break;
7300Sstevel@tonic-gate timevalsub(valp, &interval2nth);
7310Sstevel@tonic-gate }
7320Sstevel@tonic-gate }
7330Sstevel@tonic-gate
7340Sstevel@tonic-gate /*
7350Sstevel@tonic-gate * Check that a proposed value to load into the .it_value or .it_interval
7360Sstevel@tonic-gate * part of an interval timer is acceptable, and set it to at least a
7370Sstevel@tonic-gate * specified minimal value.
7380Sstevel@tonic-gate */
7390Sstevel@tonic-gate int
itimerfix(struct timeval * tv,int minimum)7400Sstevel@tonic-gate itimerfix(struct timeval *tv, int minimum)
7410Sstevel@tonic-gate {
7420Sstevel@tonic-gate if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
7430Sstevel@tonic-gate tv->tv_usec < 0 || tv->tv_usec >= MICROSEC)
7440Sstevel@tonic-gate return (EINVAL);
7450Sstevel@tonic-gate if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < minimum)
7460Sstevel@tonic-gate tv->tv_usec = minimum;
7470Sstevel@tonic-gate return (0);
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate
7500Sstevel@tonic-gate /*
7510Sstevel@tonic-gate * Same as itimerfix, except a) it takes a timespec instead of a timeval and
7520Sstevel@tonic-gate * b) it doesn't truncate based on timeout granularity; consumers of this
7530Sstevel@tonic-gate * interface (e.g. timer_settime()) depend on the passed timespec not being
7540Sstevel@tonic-gate * modified implicitly.
7550Sstevel@tonic-gate */
7560Sstevel@tonic-gate int
itimerspecfix(timespec_t * tv)7570Sstevel@tonic-gate itimerspecfix(timespec_t *tv)
7580Sstevel@tonic-gate {
7590Sstevel@tonic-gate if (tv->tv_sec < 0 || tv->tv_nsec < 0 || tv->tv_nsec >= NANOSEC)
7600Sstevel@tonic-gate return (EINVAL);
7610Sstevel@tonic-gate return (0);
7620Sstevel@tonic-gate }
7630Sstevel@tonic-gate
7640Sstevel@tonic-gate /*
7650Sstevel@tonic-gate * Decrement an interval timer by a specified number
7660Sstevel@tonic-gate * of microseconds, which must be less than a second,
7670Sstevel@tonic-gate * i.e. < 1000000. If the timer expires, then reload
7680Sstevel@tonic-gate * it. In this case, carry over (usec - old value) to
7690Sstevel@tonic-gate * reducint the value reloaded into the timer so that
7700Sstevel@tonic-gate * the timer does not drift. This routine assumes
7710Sstevel@tonic-gate * that it is called in a context where the timers
7720Sstevel@tonic-gate * on which it is operating cannot change in value.
7730Sstevel@tonic-gate */
7740Sstevel@tonic-gate int
itimerdecr(struct itimerval * itp,int usec)7750Sstevel@tonic-gate itimerdecr(struct itimerval *itp, int usec)
7760Sstevel@tonic-gate {
7770Sstevel@tonic-gate if (itp->it_value.tv_usec < usec) {
7780Sstevel@tonic-gate if (itp->it_value.tv_sec == 0) {
7790Sstevel@tonic-gate /* expired, and already in next interval */
7800Sstevel@tonic-gate usec -= itp->it_value.tv_usec;
7810Sstevel@tonic-gate goto expire;
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate itp->it_value.tv_usec += MICROSEC;
7840Sstevel@tonic-gate itp->it_value.tv_sec--;
7850Sstevel@tonic-gate }
7860Sstevel@tonic-gate itp->it_value.tv_usec -= usec;
7870Sstevel@tonic-gate usec = 0;
7880Sstevel@tonic-gate if (timerisset(&itp->it_value))
7890Sstevel@tonic-gate return (1);
7900Sstevel@tonic-gate /* expired, exactly at end of interval */
7910Sstevel@tonic-gate expire:
7920Sstevel@tonic-gate if (timerisset(&itp->it_interval)) {
7930Sstevel@tonic-gate itp->it_value = itp->it_interval;
7940Sstevel@tonic-gate itp->it_value.tv_usec -= usec;
7950Sstevel@tonic-gate if (itp->it_value.tv_usec < 0) {
7960Sstevel@tonic-gate itp->it_value.tv_usec += MICROSEC;
7970Sstevel@tonic-gate itp->it_value.tv_sec--;
7980Sstevel@tonic-gate }
7990Sstevel@tonic-gate } else
8000Sstevel@tonic-gate itp->it_value.tv_usec = 0; /* sec is already 0 */
8010Sstevel@tonic-gate return (0);
8020Sstevel@tonic-gate }
8030Sstevel@tonic-gate
8040Sstevel@tonic-gate /*
8050Sstevel@tonic-gate * Add and subtract routines for timevals.
8060Sstevel@tonic-gate * N.B.: subtract routine doesn't deal with
8070Sstevel@tonic-gate * results which are before the beginning,
8080Sstevel@tonic-gate * it just gets very confused in this case.
8090Sstevel@tonic-gate * Caveat emptor.
8100Sstevel@tonic-gate */
8110Sstevel@tonic-gate void
timevaladd(struct timeval * t1,struct timeval * t2)8120Sstevel@tonic-gate timevaladd(struct timeval *t1, struct timeval *t2)
8130Sstevel@tonic-gate {
8140Sstevel@tonic-gate t1->tv_sec += t2->tv_sec;
8150Sstevel@tonic-gate t1->tv_usec += t2->tv_usec;
8160Sstevel@tonic-gate timevalfix(t1);
8170Sstevel@tonic-gate }
8180Sstevel@tonic-gate
8190Sstevel@tonic-gate void
timevalsub(struct timeval * t1,struct timeval * t2)8200Sstevel@tonic-gate timevalsub(struct timeval *t1, struct timeval *t2)
8210Sstevel@tonic-gate {
8220Sstevel@tonic-gate t1->tv_sec -= t2->tv_sec;
8230Sstevel@tonic-gate t1->tv_usec -= t2->tv_usec;
8240Sstevel@tonic-gate timevalfix(t1);
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate
8270Sstevel@tonic-gate void
timevalfix(struct timeval * t1)8280Sstevel@tonic-gate timevalfix(struct timeval *t1)
8290Sstevel@tonic-gate {
8300Sstevel@tonic-gate if (t1->tv_usec < 0) {
8310Sstevel@tonic-gate t1->tv_sec--;
8320Sstevel@tonic-gate t1->tv_usec += MICROSEC;
8330Sstevel@tonic-gate }
8340Sstevel@tonic-gate if (t1->tv_usec >= MICROSEC) {
8350Sstevel@tonic-gate t1->tv_sec++;
8360Sstevel@tonic-gate t1->tv_usec -= MICROSEC;
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate }
8390Sstevel@tonic-gate
8400Sstevel@tonic-gate /*
8410Sstevel@tonic-gate * Same as the routines above. These routines take a timespec instead
8420Sstevel@tonic-gate * of a timeval.
8430Sstevel@tonic-gate */
8440Sstevel@tonic-gate void
timespecadd(timespec_t * t1,timespec_t * t2)8450Sstevel@tonic-gate timespecadd(timespec_t *t1, timespec_t *t2)
8460Sstevel@tonic-gate {
8470Sstevel@tonic-gate t1->tv_sec += t2->tv_sec;
8480Sstevel@tonic-gate t1->tv_nsec += t2->tv_nsec;
8490Sstevel@tonic-gate timespecfix(t1);
8500Sstevel@tonic-gate }
8510Sstevel@tonic-gate
8520Sstevel@tonic-gate void
timespecsub(timespec_t * t1,timespec_t * t2)8530Sstevel@tonic-gate timespecsub(timespec_t *t1, timespec_t *t2)
8540Sstevel@tonic-gate {
8550Sstevel@tonic-gate t1->tv_sec -= t2->tv_sec;
8560Sstevel@tonic-gate t1->tv_nsec -= t2->tv_nsec;
8570Sstevel@tonic-gate timespecfix(t1);
8580Sstevel@tonic-gate }
8590Sstevel@tonic-gate
8600Sstevel@tonic-gate void
timespecfix(timespec_t * t1)8610Sstevel@tonic-gate timespecfix(timespec_t *t1)
8620Sstevel@tonic-gate {
8630Sstevel@tonic-gate if (t1->tv_nsec < 0) {
8640Sstevel@tonic-gate t1->tv_sec--;
8650Sstevel@tonic-gate t1->tv_nsec += NANOSEC;
8660Sstevel@tonic-gate } else {
8670Sstevel@tonic-gate if (t1->tv_nsec >= NANOSEC) {
8680Sstevel@tonic-gate t1->tv_sec++;
8690Sstevel@tonic-gate t1->tv_nsec -= NANOSEC;
8700Sstevel@tonic-gate }
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate }
8730Sstevel@tonic-gate
8740Sstevel@tonic-gate /*
8750Sstevel@tonic-gate * Compute number of hz until specified time.
8760Sstevel@tonic-gate * Used to compute third argument to timeout() from an absolute time.
8770Sstevel@tonic-gate */
8780Sstevel@tonic-gate clock_t
hzto(struct timeval * tv)8790Sstevel@tonic-gate hzto(struct timeval *tv)
8800Sstevel@tonic-gate {
8810Sstevel@tonic-gate timespec_t ts, now;
8820Sstevel@tonic-gate
8830Sstevel@tonic-gate ts.tv_sec = tv->tv_sec;
8840Sstevel@tonic-gate ts.tv_nsec = tv->tv_usec * 1000;
8850Sstevel@tonic-gate gethrestime_lasttick(&now);
8860Sstevel@tonic-gate
8870Sstevel@tonic-gate return (timespectohz(&ts, now));
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate
8900Sstevel@tonic-gate /*
8910Sstevel@tonic-gate * Compute number of hz until specified time for a given timespec value.
8920Sstevel@tonic-gate * Used to compute third argument to timeout() from an absolute time.
8930Sstevel@tonic-gate */
8940Sstevel@tonic-gate clock_t
timespectohz(timespec_t * tv,timespec_t now)8950Sstevel@tonic-gate timespectohz(timespec_t *tv, timespec_t now)
8960Sstevel@tonic-gate {
8970Sstevel@tonic-gate clock_t ticks;
8980Sstevel@tonic-gate time_t sec;
8990Sstevel@tonic-gate int nsec;
9000Sstevel@tonic-gate
9010Sstevel@tonic-gate /*
9020Sstevel@tonic-gate * Compute number of ticks we will see between now and
9030Sstevel@tonic-gate * the target time; returns "1" if the destination time
9040Sstevel@tonic-gate * is before the next tick, so we always get some delay,
9050Sstevel@tonic-gate * and returns LONG_MAX ticks if we would overflow.
9060Sstevel@tonic-gate */
9070Sstevel@tonic-gate sec = tv->tv_sec - now.tv_sec;
9080Sstevel@tonic-gate nsec = tv->tv_nsec - now.tv_nsec + nsec_per_tick - 1;
9090Sstevel@tonic-gate
9100Sstevel@tonic-gate if (nsec < 0) {
9110Sstevel@tonic-gate sec--;
9120Sstevel@tonic-gate nsec += NANOSEC;
9130Sstevel@tonic-gate } else if (nsec >= NANOSEC) {
9140Sstevel@tonic-gate sec++;
9150Sstevel@tonic-gate nsec -= NANOSEC;
9160Sstevel@tonic-gate }
9170Sstevel@tonic-gate
9180Sstevel@tonic-gate ticks = NSEC_TO_TICK(nsec);
9190Sstevel@tonic-gate
9200Sstevel@tonic-gate /*
9210Sstevel@tonic-gate * Compute ticks, accounting for negative and overflow as above.
9220Sstevel@tonic-gate * Overflow protection kicks in at about 70 weeks for hz=50
9230Sstevel@tonic-gate * and at about 35 weeks for hz=100. (Rather longer for the 64-bit
9240Sstevel@tonic-gate * kernel :-)
9250Sstevel@tonic-gate */
9260Sstevel@tonic-gate if (sec < 0 || (sec == 0 && ticks < 1))
9270Sstevel@tonic-gate ticks = 1; /* protect vs nonpositive */
9280Sstevel@tonic-gate else if (sec > (LONG_MAX - ticks) / hz)
9290Sstevel@tonic-gate ticks = LONG_MAX; /* protect vs overflow */
9300Sstevel@tonic-gate else
9310Sstevel@tonic-gate ticks += sec * hz; /* common case */
9320Sstevel@tonic-gate
9330Sstevel@tonic-gate return (ticks);
9340Sstevel@tonic-gate }
9350Sstevel@tonic-gate
9360Sstevel@tonic-gate /*
9376422Sqiao * Compute number of hz with the timespec tv specified.
9386422Sqiao * The return type must be 64 bit integer.
9394123Sdm120769 */
9406422Sqiao int64_t
timespectohz64(timespec_t * tv)9416422Sqiao timespectohz64(timespec_t *tv)
9424123Sdm120769 {
9436422Sqiao int64_t ticks;
9446422Sqiao int64_t sec;
9456422Sqiao int64_t nsec;
9466422Sqiao
9476422Sqiao sec = tv->tv_sec;
9486422Sqiao nsec = tv->tv_nsec + nsec_per_tick - 1;
9494123Sdm120769
9506422Sqiao if (nsec < 0) {
9516422Sqiao sec--;
9526422Sqiao nsec += NANOSEC;
9536422Sqiao } else if (nsec >= NANOSEC) {
9546422Sqiao sec++;
9556422Sqiao nsec -= NANOSEC;
9566422Sqiao }
9576422Sqiao
9586422Sqiao ticks = NSEC_TO_TICK(nsec);
9596422Sqiao
9606422Sqiao /*
9616422Sqiao * Compute ticks, accounting for negative and overflow as above.
9626422Sqiao * Overflow protection kicks in at about 70 weeks for hz=50
9636422Sqiao * and at about 35 weeks for hz=100. (Rather longer for the 64-bit
9646422Sqiao * kernel
9656422Sqiao */
9666422Sqiao if (sec < 0 || (sec == 0 && ticks < 1))
9676422Sqiao ticks = 1; /* protect vs nonpositive */
9686422Sqiao else if (sec > (((~0ULL) >> 1) - ticks) / hz)
9696422Sqiao ticks = (~0ULL) >> 1; /* protect vs overflow */
9706422Sqiao else
9716422Sqiao ticks += sec * hz; /* common case */
9726422Sqiao
9736422Sqiao return (ticks);
9744123Sdm120769 }
9754123Sdm120769
9764123Sdm120769 /*
9770Sstevel@tonic-gate * hrt2ts(): convert from hrtime_t to timestruc_t.
9780Sstevel@tonic-gate *
9790Sstevel@tonic-gate * All this routine really does is:
9800Sstevel@tonic-gate *
9810Sstevel@tonic-gate * tsp->sec = hrt / NANOSEC;
9820Sstevel@tonic-gate * tsp->nsec = hrt % NANOSEC;
9830Sstevel@tonic-gate *
9840Sstevel@tonic-gate * The black magic below avoids doing a 64-bit by 32-bit integer divide,
9850Sstevel@tonic-gate * which is quite expensive. There's actually much more going on here than
9860Sstevel@tonic-gate * it might first appear -- don't try this at home.
9870Sstevel@tonic-gate *
9880Sstevel@tonic-gate * For the adventuresome, here's an explanation of how it works.
9890Sstevel@tonic-gate *
9900Sstevel@tonic-gate * Multiplication by a fixed constant is easy -- you just do the appropriate
9910Sstevel@tonic-gate * shifts and adds. For example, to multiply by 10, we observe that
9920Sstevel@tonic-gate *
9930Sstevel@tonic-gate * x * 10 = x * (8 + 2)
9940Sstevel@tonic-gate * = (x * 8) + (x * 2)
9950Sstevel@tonic-gate * = (x << 3) + (x << 1).
9960Sstevel@tonic-gate *
9970Sstevel@tonic-gate * In general, you can read the algorithm right off the bits: the number 10
9980Sstevel@tonic-gate * is 1010 in binary; bits 1 and 3 are ones, so x * 10 = (x << 1) + (x << 3).
9990Sstevel@tonic-gate *
10000Sstevel@tonic-gate * Sometimes you can do better. For example, 15 is 1111 binary, so the normal
10010Sstevel@tonic-gate * shift/add computation is x * 15 = (x << 0) + (x << 1) + (x << 2) + (x << 3).
10020Sstevel@tonic-gate * But, it's cheaper if you capitalize on the fact that you have a run of ones:
10030Sstevel@tonic-gate * 1111 = 10000 - 1, hence x * 15 = (x << 4) - (x << 0). [You would never
10040Sstevel@tonic-gate * actually perform the operation << 0, since it's a no-op; I'm just writing
10050Sstevel@tonic-gate * it that way for clarity.]
10060Sstevel@tonic-gate *
10070Sstevel@tonic-gate * The other way you can win is if you get lucky with the prime factorization
10080Sstevel@tonic-gate * of your constant. The number 1,000,000,000, which we have to multiply
10090Sstevel@tonic-gate * by below, is a good example. One billion is 111011100110101100101000000000
10100Sstevel@tonic-gate * in binary. If you apply the bit-grouping trick, it doesn't buy you very
10110Sstevel@tonic-gate * much, because it's only a win for groups of three or more equal bits:
10120Sstevel@tonic-gate *
10130Sstevel@tonic-gate * 111011100110101100101000000000 = 1000000000000000000000000000000
10140Sstevel@tonic-gate * - 000100011001010011011000000000
10150Sstevel@tonic-gate *
10160Sstevel@tonic-gate * Thus, instead of the 13 shift/add pairs (26 operations) implied by the LHS,
10170Sstevel@tonic-gate * we have reduced this to 10 shift/add pairs (20 operations) on the RHS.
10180Sstevel@tonic-gate * This is better, but not great.
10190Sstevel@tonic-gate *
10200Sstevel@tonic-gate * However, we can factor 1,000,000,000 = 2^9 * 5^9 = 2^9 * 125 * 125 * 125,
10210Sstevel@tonic-gate * and multiply by each factor. Multiplication by 125 is particularly easy,
10220Sstevel@tonic-gate * since 128 is nearby: x * 125 = (x << 7) - x - x - x, which is just four
10230Sstevel@tonic-gate * operations. So, to multiply by 1,000,000,000, we perform three multipli-
10240Sstevel@tonic-gate * cations by 125, then << 9, a total of only 3 * 4 + 1 = 13 operations.
10250Sstevel@tonic-gate * This is the algorithm we actually use in both hrt2ts() and ts2hrt().
10260Sstevel@tonic-gate *
10270Sstevel@tonic-gate * Division is harder; there is no equivalent of the simple shift-add algorithm
10280Sstevel@tonic-gate * we used for multiplication. However, we can convert the division problem
10290Sstevel@tonic-gate * into a multiplication problem by pre-computing the binary representation
10300Sstevel@tonic-gate * of the reciprocal of the divisor. For the case of interest, we have
10310Sstevel@tonic-gate *
10320Sstevel@tonic-gate * 1 / 1,000,000,000 = 1.0001001011100000101111101000001B-30,
10330Sstevel@tonic-gate *
10340Sstevel@tonic-gate * to 32 bits of precision. (The notation B-30 means "* 2^-30", just like
10350Sstevel@tonic-gate * E-18 means "* 10^-18".)
10360Sstevel@tonic-gate *
10370Sstevel@tonic-gate * So, to compute x / 1,000,000,000, we just multiply x by the 32-bit
10380Sstevel@tonic-gate * integer 10001001011100000101111101000001, then normalize (shift) the
10390Sstevel@tonic-gate * result. This constant has several large bits runs, so the multiply
10400Sstevel@tonic-gate * is relatively cheap:
10410Sstevel@tonic-gate *
10420Sstevel@tonic-gate * 10001001011100000101111101000001 = 10001001100000000110000001000001
10430Sstevel@tonic-gate * - 00000000000100000000000100000000
10440Sstevel@tonic-gate *
10450Sstevel@tonic-gate * Again, you can just read the algorithm right off the bits:
10460Sstevel@tonic-gate *
10470Sstevel@tonic-gate * sec = hrt;
10480Sstevel@tonic-gate * sec += (hrt << 6);
10490Sstevel@tonic-gate * sec -= (hrt << 8);
10500Sstevel@tonic-gate * sec += (hrt << 13);
10510Sstevel@tonic-gate * sec += (hrt << 14);
10520Sstevel@tonic-gate * sec -= (hrt << 20);
10530Sstevel@tonic-gate * sec += (hrt << 23);
10540Sstevel@tonic-gate * sec += (hrt << 24);
10550Sstevel@tonic-gate * sec += (hrt << 27);
10560Sstevel@tonic-gate * sec += (hrt << 31);
10570Sstevel@tonic-gate * sec >>= (32 + 30);
10580Sstevel@tonic-gate *
10590Sstevel@tonic-gate * Voila! The only problem is, since hrt is 64 bits, we need to use 96-bit
10600Sstevel@tonic-gate * arithmetic to perform this calculation. That's a waste, because ultimately
10610Sstevel@tonic-gate * we only need the highest 32 bits of the result.
10620Sstevel@tonic-gate *
10630Sstevel@tonic-gate * The first thing we do is to realize that we don't need to use all of hrt
10640Sstevel@tonic-gate * in the calculation. The lowest 30 bits can contribute at most 1 to the
10650Sstevel@tonic-gate * quotient (2^30 / 1,000,000,000 = 1.07...), so we'll deal with them later.
10660Sstevel@tonic-gate * The highest 2 bits have to be zero, or hrt won't fit in a timestruc_t.
10670Sstevel@tonic-gate * Thus, the only bits of hrt that matter for division are bits 30..61.
10680Sstevel@tonic-gate * These 32 bits are just the lower-order word of (hrt >> 30). This brings
10690Sstevel@tonic-gate * us down from 96-bit math to 64-bit math, and our algorithm becomes:
10700Sstevel@tonic-gate *
10710Sstevel@tonic-gate * tmp = (uint32_t) (hrt >> 30);
10720Sstevel@tonic-gate * sec = tmp;
10730Sstevel@tonic-gate * sec += (tmp << 6);
10740Sstevel@tonic-gate * sec -= (tmp << 8);
10750Sstevel@tonic-gate * sec += (tmp << 13);
10760Sstevel@tonic-gate * sec += (tmp << 14);
10770Sstevel@tonic-gate * sec -= (tmp << 20);
10780Sstevel@tonic-gate * sec += (tmp << 23);
10790Sstevel@tonic-gate * sec += (tmp << 24);
10800Sstevel@tonic-gate * sec += (tmp << 27);
10810Sstevel@tonic-gate * sec += (tmp << 31);
10820Sstevel@tonic-gate * sec >>= 32;
10830Sstevel@tonic-gate *
10840Sstevel@tonic-gate * Next, we're going to reduce this 64-bit computation to a 32-bit
10850Sstevel@tonic-gate * computation. We begin by rewriting the above algorithm to use relative
10860Sstevel@tonic-gate * shifts instead of absolute shifts. That is, instead of computing
10870Sstevel@tonic-gate * tmp << 6, tmp << 8, tmp << 13, etc, we'll just shift incrementally:
10880Sstevel@tonic-gate * tmp <<= 6, tmp <<= 2 (== 8 - 6), tmp <<= 5 (== 13 - 8), etc:
10890Sstevel@tonic-gate *
10900Sstevel@tonic-gate * tmp = (uint32_t) (hrt >> 30);
10910Sstevel@tonic-gate * sec = tmp;
10920Sstevel@tonic-gate * tmp <<= 6; sec += tmp;
10930Sstevel@tonic-gate * tmp <<= 2; sec -= tmp;
10940Sstevel@tonic-gate * tmp <<= 5; sec += tmp;
10950Sstevel@tonic-gate * tmp <<= 1; sec += tmp;
10960Sstevel@tonic-gate * tmp <<= 6; sec -= tmp;
10970Sstevel@tonic-gate * tmp <<= 3; sec += tmp;
10980Sstevel@tonic-gate * tmp <<= 1; sec += tmp;
10990Sstevel@tonic-gate * tmp <<= 3; sec += tmp;
11000Sstevel@tonic-gate * tmp <<= 4; sec += tmp;
11010Sstevel@tonic-gate * sec >>= 32;
11020Sstevel@tonic-gate *
11030Sstevel@tonic-gate * Now for the final step. Instead of throwing away the low 32 bits at
11040Sstevel@tonic-gate * the end, we can throw them away as we go, only keeping the high 32 bits
11050Sstevel@tonic-gate * of the product at each step. So, for example, where we now have
11060Sstevel@tonic-gate *
11070Sstevel@tonic-gate * tmp <<= 6; sec = sec + tmp;
11080Sstevel@tonic-gate * we will instead have
11090Sstevel@tonic-gate * tmp <<= 6; sec = (sec + tmp) >> 6;
11100Sstevel@tonic-gate * which is equivalent to
11110Sstevel@tonic-gate * sec = (sec >> 6) + tmp;
11120Sstevel@tonic-gate *
11130Sstevel@tonic-gate * The final shift ("sec >>= 32") goes away.
11140Sstevel@tonic-gate *
11150Sstevel@tonic-gate * All we're really doing here is long multiplication, just like we learned in
11160Sstevel@tonic-gate * grade school, except that at each step, we only look at the leftmost 32
11170Sstevel@tonic-gate * columns. The cumulative error is, at most, the sum of all the bits we
11180Sstevel@tonic-gate * throw away, which is 2^-32 + 2^-31 + ... + 2^-2 + 2^-1 == 1 - 2^-32.
11190Sstevel@tonic-gate * Thus, the final result ("sec") is correct to +/- 1.
11200Sstevel@tonic-gate *
11210Sstevel@tonic-gate * It turns out to be important to keep "sec" positive at each step, because
11220Sstevel@tonic-gate * we don't want to have to explicitly extend the sign bit. Therefore,
11230Sstevel@tonic-gate * starting with the last line of code above, each line that would have read
11240Sstevel@tonic-gate * "sec = (sec >> n) - tmp" must be changed to "sec = tmp - (sec >> n)", and
11250Sstevel@tonic-gate * the operators (+ or -) in all previous lines must be toggled accordingly.
11260Sstevel@tonic-gate * Thus, we end up with:
11270Sstevel@tonic-gate *
11280Sstevel@tonic-gate * tmp = (uint32_t) (hrt >> 30);
11290Sstevel@tonic-gate * sec = tmp + (sec >> 6);
11300Sstevel@tonic-gate * sec = tmp - (tmp >> 2);
11310Sstevel@tonic-gate * sec = tmp - (sec >> 5);
11320Sstevel@tonic-gate * sec = tmp + (sec >> 1);
11330Sstevel@tonic-gate * sec = tmp - (sec >> 6);
11340Sstevel@tonic-gate * sec = tmp - (sec >> 3);
11350Sstevel@tonic-gate * sec = tmp + (sec >> 1);
11360Sstevel@tonic-gate * sec = tmp + (sec >> 3);
11370Sstevel@tonic-gate * sec = tmp + (sec >> 4);
11380Sstevel@tonic-gate *
11390Sstevel@tonic-gate * This yields a value for sec that is accurate to +1/-1, so we have two
11400Sstevel@tonic-gate * cases to deal with. The mysterious-looking "+ 7" in the code below biases
11410Sstevel@tonic-gate * the rounding toward zero, so that sec is always less than or equal to
11420Sstevel@tonic-gate * the correct value. With this modified code, sec is accurate to +0/-2, with
11430Sstevel@tonic-gate * the -2 case being very rare in practice. With this change, we only have to
11440Sstevel@tonic-gate * deal with one case (sec too small) in the cleanup code.
11450Sstevel@tonic-gate *
11460Sstevel@tonic-gate * The other modification we make is to delete the second line above
11470Sstevel@tonic-gate * ("sec = tmp + (sec >> 6);"), since it only has an effect when bit 31 is
11480Sstevel@tonic-gate * set, and the cleanup code can handle that rare case. This reduces the
11490Sstevel@tonic-gate * *guaranteed* accuracy of sec to +0/-3, but speeds up the common cases.
11500Sstevel@tonic-gate *
11510Sstevel@tonic-gate * Finally, we compute nsec = hrt - (sec * 1,000,000,000). nsec will always
11520Sstevel@tonic-gate * be positive (since sec is never too large), and will at most be equal to
11530Sstevel@tonic-gate * the error in sec (times 1,000,000,000) plus the low-order 30 bits of hrt.
11540Sstevel@tonic-gate * Thus, nsec < 3 * 1,000,000,000 + 2^30, which is less than 2^32, so we can
11550Sstevel@tonic-gate * safely assume that nsec fits in 32 bits. Consequently, when we compute
11560Sstevel@tonic-gate * sec * 1,000,000,000, we only need the low 32 bits, so we can just do 32-bit
11570Sstevel@tonic-gate * arithmetic and let the high-order bits fall off the end.
11580Sstevel@tonic-gate *
11590Sstevel@tonic-gate * Since nsec < 3 * 1,000,000,000 + 2^30 == 4,073,741,824, the cleanup loop:
11600Sstevel@tonic-gate *
11610Sstevel@tonic-gate * while (nsec >= NANOSEC) {
11620Sstevel@tonic-gate * nsec -= NANOSEC;
11630Sstevel@tonic-gate * sec++;
11640Sstevel@tonic-gate * }
11650Sstevel@tonic-gate *
11660Sstevel@tonic-gate * is guaranteed to complete in at most 4 iterations. In practice, the loop
11670Sstevel@tonic-gate * completes in 0 or 1 iteration over 95% of the time.
11680Sstevel@tonic-gate *
11690Sstevel@tonic-gate * On an SS2, this implementation of hrt2ts() takes 1.7 usec, versus about
11700Sstevel@tonic-gate * 35 usec for software division -- about 20 times faster.
11710Sstevel@tonic-gate */
11720Sstevel@tonic-gate void
hrt2ts(hrtime_t hrt,timestruc_t * tsp)11730Sstevel@tonic-gate hrt2ts(hrtime_t hrt, timestruc_t *tsp)
11740Sstevel@tonic-gate {
11750Sstevel@tonic-gate uint32_t sec, nsec, tmp;
11760Sstevel@tonic-gate
11770Sstevel@tonic-gate tmp = (uint32_t)(hrt >> 30);
11780Sstevel@tonic-gate sec = tmp - (tmp >> 2);
11790Sstevel@tonic-gate sec = tmp - (sec >> 5);
11800Sstevel@tonic-gate sec = tmp + (sec >> 1);
11810Sstevel@tonic-gate sec = tmp - (sec >> 6) + 7;
11820Sstevel@tonic-gate sec = tmp - (sec >> 3);
11830Sstevel@tonic-gate sec = tmp + (sec >> 1);
11840Sstevel@tonic-gate sec = tmp + (sec >> 3);
11850Sstevel@tonic-gate sec = tmp + (sec >> 4);
11860Sstevel@tonic-gate tmp = (sec << 7) - sec - sec - sec;
11870Sstevel@tonic-gate tmp = (tmp << 7) - tmp - tmp - tmp;
11880Sstevel@tonic-gate tmp = (tmp << 7) - tmp - tmp - tmp;
11890Sstevel@tonic-gate nsec = (uint32_t)hrt - (tmp << 9);
11900Sstevel@tonic-gate while (nsec >= NANOSEC) {
11910Sstevel@tonic-gate nsec -= NANOSEC;
11920Sstevel@tonic-gate sec++;
11930Sstevel@tonic-gate }
11940Sstevel@tonic-gate tsp->tv_sec = (time_t)sec;
11950Sstevel@tonic-gate tsp->tv_nsec = nsec;
11960Sstevel@tonic-gate }
11970Sstevel@tonic-gate
11980Sstevel@tonic-gate /*
11990Sstevel@tonic-gate * Convert from timestruc_t to hrtime_t.
12000Sstevel@tonic-gate *
12010Sstevel@tonic-gate * The code below is equivalent to:
12020Sstevel@tonic-gate *
12030Sstevel@tonic-gate * hrt = tsp->tv_sec * NANOSEC + tsp->tv_nsec;
12040Sstevel@tonic-gate *
12050Sstevel@tonic-gate * but requires no integer multiply.
12060Sstevel@tonic-gate */
12070Sstevel@tonic-gate hrtime_t
ts2hrt(const timestruc_t * tsp)12080Sstevel@tonic-gate ts2hrt(const timestruc_t *tsp)
12090Sstevel@tonic-gate {
12100Sstevel@tonic-gate hrtime_t hrt;
12110Sstevel@tonic-gate
12120Sstevel@tonic-gate hrt = tsp->tv_sec;
12130Sstevel@tonic-gate hrt = (hrt << 7) - hrt - hrt - hrt;
12140Sstevel@tonic-gate hrt = (hrt << 7) - hrt - hrt - hrt;
12150Sstevel@tonic-gate hrt = (hrt << 7) - hrt - hrt - hrt;
12160Sstevel@tonic-gate hrt = (hrt << 9) + tsp->tv_nsec;
12170Sstevel@tonic-gate return (hrt);
12180Sstevel@tonic-gate }
12190Sstevel@tonic-gate
12200Sstevel@tonic-gate /*
12210Sstevel@tonic-gate * For the various 32-bit "compatibility" paths in the system.
12220Sstevel@tonic-gate */
12230Sstevel@tonic-gate void
hrt2ts32(hrtime_t hrt,timestruc32_t * ts32p)12240Sstevel@tonic-gate hrt2ts32(hrtime_t hrt, timestruc32_t *ts32p)
12250Sstevel@tonic-gate {
12260Sstevel@tonic-gate timestruc_t ts;
12270Sstevel@tonic-gate
12280Sstevel@tonic-gate hrt2ts(hrt, &ts);
12290Sstevel@tonic-gate TIMESPEC_TO_TIMESPEC32(ts32p, &ts);
12300Sstevel@tonic-gate }
12310Sstevel@tonic-gate
12320Sstevel@tonic-gate /*
12330Sstevel@tonic-gate * If this ever becomes performance critical (ha!), we can borrow the
12340Sstevel@tonic-gate * code from ts2hrt(), above, to multiply tv_sec by 1,000,000 and the
12350Sstevel@tonic-gate * straightforward (x << 10) - (x << 5) + (x << 3) to multiply tv_usec by
12360Sstevel@tonic-gate * 1,000. For now, we'll opt for readability (besides, the compiler does
12370Sstevel@tonic-gate * a passable job of optimizing constant multiplication into shifts and adds).
12380Sstevel@tonic-gate */
12390Sstevel@tonic-gate hrtime_t
tv2hrt(struct timeval * tvp)12400Sstevel@tonic-gate tv2hrt(struct timeval *tvp)
12410Sstevel@tonic-gate {
12420Sstevel@tonic-gate return ((hrtime_t)tvp->tv_sec * NANOSEC +
12430Sstevel@tonic-gate (hrtime_t)tvp->tv_usec * (NANOSEC / MICROSEC));
12440Sstevel@tonic-gate }
12450Sstevel@tonic-gate
12460Sstevel@tonic-gate void
hrt2tv(hrtime_t hrt,struct timeval * tvp)12471432Sandyb hrt2tv(hrtime_t hrt, struct timeval *tvp)
12480Sstevel@tonic-gate {
12491432Sandyb uint32_t sec, nsec, tmp;
12501432Sandyb uint32_t q, r, t;
12511432Sandyb
12521432Sandyb tmp = (uint32_t)(hrt >> 30);
12531432Sandyb sec = tmp - (tmp >> 2);
12541432Sandyb sec = tmp - (sec >> 5);
12551432Sandyb sec = tmp + (sec >> 1);
12561432Sandyb sec = tmp - (sec >> 6) + 7;
12571432Sandyb sec = tmp - (sec >> 3);
12581432Sandyb sec = tmp + (sec >> 1);
12591432Sandyb sec = tmp + (sec >> 3);
12601432Sandyb sec = tmp + (sec >> 4);
12611432Sandyb tmp = (sec << 7) - sec - sec - sec;
12621432Sandyb tmp = (tmp << 7) - tmp - tmp - tmp;
12631432Sandyb tmp = (tmp << 7) - tmp - tmp - tmp;
12641432Sandyb nsec = (uint32_t)hrt - (tmp << 9);
12651432Sandyb while (nsec >= NANOSEC) {
12661432Sandyb nsec -= NANOSEC;
12671432Sandyb sec++;
12681432Sandyb }
12691432Sandyb tvp->tv_sec = (time_t)sec;
12701432Sandyb /*
12711432Sandyb * this routine is very similar to hr2ts, but requires microseconds
12721432Sandyb * instead of nanoseconds, so an interger divide by 1000 routine
12731432Sandyb * completes the conversion
12741432Sandyb */
12751432Sandyb t = (nsec >> 7) + (nsec >> 8) + (nsec >> 12);
12761432Sandyb q = (nsec >> 1) + t + (nsec >> 15) + (t >> 11) + (t >> 14);
12771432Sandyb q = q >> 9;
12781432Sandyb r = nsec - q*1000;
12791432Sandyb tvp->tv_usec = q + ((r + 24) >> 10);
12801432Sandyb
12810Sstevel@tonic-gate }
12820Sstevel@tonic-gate
12830Sstevel@tonic-gate int
nanosleep(timespec_t * rqtp,timespec_t * rmtp)12840Sstevel@tonic-gate nanosleep(timespec_t *rqtp, timespec_t *rmtp)
12850Sstevel@tonic-gate {
12860Sstevel@tonic-gate timespec_t rqtime;
12870Sstevel@tonic-gate timespec_t rmtime;
12880Sstevel@tonic-gate timespec_t now;
12894123Sdm120769 int timecheck;
12900Sstevel@tonic-gate int ret = 1;
12910Sstevel@tonic-gate model_t datamodel = get_udatamodel();
12920Sstevel@tonic-gate
12937982SDonghai.Qiao@Sun.COM timecheck = timechanged;
12947982SDonghai.Qiao@Sun.COM gethrestime(&now);
12957982SDonghai.Qiao@Sun.COM
12960Sstevel@tonic-gate if (datamodel == DATAMODEL_NATIVE) {
12970Sstevel@tonic-gate if (copyin(rqtp, &rqtime, sizeof (rqtime)))
12980Sstevel@tonic-gate return (set_errno(EFAULT));
12990Sstevel@tonic-gate } else {
13000Sstevel@tonic-gate timespec32_t rqtime32;
13010Sstevel@tonic-gate
13020Sstevel@tonic-gate if (copyin(rqtp, &rqtime32, sizeof (rqtime32)))
13030Sstevel@tonic-gate return (set_errno(EFAULT));
13040Sstevel@tonic-gate TIMESPEC32_TO_TIMESPEC(&rqtime, &rqtime32);
13050Sstevel@tonic-gate }
13060Sstevel@tonic-gate
13070Sstevel@tonic-gate if (rqtime.tv_sec < 0 || rqtime.tv_nsec < 0 ||
13080Sstevel@tonic-gate rqtime.tv_nsec >= NANOSEC)
13090Sstevel@tonic-gate return (set_errno(EINVAL));
13100Sstevel@tonic-gate
13110Sstevel@tonic-gate if (timerspecisset(&rqtime)) {
13120Sstevel@tonic-gate timespecadd(&rqtime, &now);
13130Sstevel@tonic-gate mutex_enter(&curthread->t_delay_lock);
13140Sstevel@tonic-gate while ((ret = cv_waituntil_sig(&curthread->t_delay_cv,
13154123Sdm120769 &curthread->t_delay_lock, &rqtime, timecheck)) > 0)
13160Sstevel@tonic-gate continue;
13170Sstevel@tonic-gate mutex_exit(&curthread->t_delay_lock);
13180Sstevel@tonic-gate }
13190Sstevel@tonic-gate
13200Sstevel@tonic-gate if (rmtp) {
13210Sstevel@tonic-gate /*
13220Sstevel@tonic-gate * If cv_waituntil_sig() returned due to a signal, and
13230Sstevel@tonic-gate * there is time remaining, then set the time remaining.
13240Sstevel@tonic-gate * Else set time remaining to zero
13250Sstevel@tonic-gate */
13260Sstevel@tonic-gate rmtime.tv_sec = rmtime.tv_nsec = 0;
13270Sstevel@tonic-gate if (ret == 0) {
13283346Svb160487 timespec_t delta = rqtime;
13293346Svb160487
13300Sstevel@tonic-gate gethrestime(&now);
13313346Svb160487 timespecsub(&delta, &now);
13323346Svb160487 if (delta.tv_sec > 0 || (delta.tv_sec == 0 &&
13333346Svb160487 delta.tv_nsec > 0))
13343346Svb160487 rmtime = delta;
13350Sstevel@tonic-gate }
13360Sstevel@tonic-gate
13370Sstevel@tonic-gate if (datamodel == DATAMODEL_NATIVE) {
13380Sstevel@tonic-gate if (copyout(&rmtime, rmtp, sizeof (rmtime)))
13390Sstevel@tonic-gate return (set_errno(EFAULT));
13400Sstevel@tonic-gate } else {
13410Sstevel@tonic-gate timespec32_t rmtime32;
13420Sstevel@tonic-gate
13430Sstevel@tonic-gate TIMESPEC_TO_TIMESPEC32(&rmtime32, &rmtime);
13440Sstevel@tonic-gate if (copyout(&rmtime32, rmtp, sizeof (rmtime32)))
13450Sstevel@tonic-gate return (set_errno(EFAULT));
13460Sstevel@tonic-gate }
13470Sstevel@tonic-gate }
13480Sstevel@tonic-gate
13490Sstevel@tonic-gate if (ret == 0)
13500Sstevel@tonic-gate return (set_errno(EINTR));
13510Sstevel@tonic-gate return (0);
13520Sstevel@tonic-gate }
13530Sstevel@tonic-gate
13540Sstevel@tonic-gate /*
13550Sstevel@tonic-gate * Routines to convert standard UNIX time (seconds since Jan 1, 1970)
13560Sstevel@tonic-gate * into year/month/day/hour/minute/second format, and back again.
13570Sstevel@tonic-gate * Note: these routines require tod_lock held to protect cached state.
13580Sstevel@tonic-gate */
13590Sstevel@tonic-gate static int days_thru_month[64] = {
13600Sstevel@tonic-gate 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366, 0, 0,
13610Sstevel@tonic-gate 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0,
13620Sstevel@tonic-gate 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0,
13630Sstevel@tonic-gate 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365, 0, 0,
13640Sstevel@tonic-gate };
13650Sstevel@tonic-gate
13660Sstevel@tonic-gate todinfo_t saved_tod;
13670Sstevel@tonic-gate int saved_utc = -60;
13680Sstevel@tonic-gate
13690Sstevel@tonic-gate todinfo_t
utc_to_tod(time_t utc)13700Sstevel@tonic-gate utc_to_tod(time_t utc)
13710Sstevel@tonic-gate {
13720Sstevel@tonic-gate long dse, day, month, year;
13730Sstevel@tonic-gate todinfo_t tod;
13740Sstevel@tonic-gate
13750Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock));
13760Sstevel@tonic-gate
1377*11752STrevor.Thompson@Sun.COM /*
1378*11752STrevor.Thompson@Sun.COM * Note that tod_set_prev() assumes utc will be set to zero in
1379*11752STrevor.Thompson@Sun.COM * the case of it being negative. Consequently, any change made
1380*11752STrevor.Thompson@Sun.COM * to this behavior would have to be reflected in that function
1381*11752STrevor.Thompson@Sun.COM * as well.
1382*11752STrevor.Thompson@Sun.COM */
13830Sstevel@tonic-gate if (utc < 0) /* should never happen */
13840Sstevel@tonic-gate utc = 0;
13850Sstevel@tonic-gate
13860Sstevel@tonic-gate saved_tod.tod_sec += utc - saved_utc;
13870Sstevel@tonic-gate saved_utc = utc;
13880Sstevel@tonic-gate if (saved_tod.tod_sec >= 0 && saved_tod.tod_sec < 60)
13890Sstevel@tonic-gate return (saved_tod); /* only the seconds changed */
13900Sstevel@tonic-gate
13910Sstevel@tonic-gate dse = utc / 86400; /* days since epoch */
13920Sstevel@tonic-gate
13930Sstevel@tonic-gate tod.tod_sec = utc % 60;
13940Sstevel@tonic-gate tod.tod_min = (utc % 3600) / 60;
13950Sstevel@tonic-gate tod.tod_hour = (utc % 86400) / 3600;
13960Sstevel@tonic-gate tod.tod_dow = (dse + 4) % 7 + 1; /* epoch was a Thursday */
13970Sstevel@tonic-gate
13980Sstevel@tonic-gate year = dse / 365 + 72; /* first guess -- always a bit too large */
13990Sstevel@tonic-gate do {
14000Sstevel@tonic-gate year--;
14010Sstevel@tonic-gate day = dse - 365 * (year - 70) - ((year - 69) >> 2);
14020Sstevel@tonic-gate } while (day < 0);
14030Sstevel@tonic-gate
14040Sstevel@tonic-gate month = ((year & 3) << 4) + 1;
14050Sstevel@tonic-gate while (day >= days_thru_month[month + 1])
14060Sstevel@tonic-gate month++;
14070Sstevel@tonic-gate
14080Sstevel@tonic-gate tod.tod_day = day - days_thru_month[month] + 1;
14090Sstevel@tonic-gate tod.tod_month = month & 15;
14100Sstevel@tonic-gate tod.tod_year = year;
14110Sstevel@tonic-gate
14120Sstevel@tonic-gate saved_tod = tod;
14130Sstevel@tonic-gate return (tod);
14140Sstevel@tonic-gate }
14150Sstevel@tonic-gate
14160Sstevel@tonic-gate time_t
tod_to_utc(todinfo_t tod)14170Sstevel@tonic-gate tod_to_utc(todinfo_t tod)
14180Sstevel@tonic-gate {
14190Sstevel@tonic-gate time_t utc;
14200Sstevel@tonic-gate int year = tod.tod_year;
14210Sstevel@tonic-gate int month = tod.tod_month + ((year & 3) << 4);
14220Sstevel@tonic-gate #ifdef DEBUG
14230Sstevel@tonic-gate /* only warn once, not each time called */
14240Sstevel@tonic-gate static int year_warn = 1;
14250Sstevel@tonic-gate static int month_warn = 1;
14260Sstevel@tonic-gate static int day_warn = 1;
14270Sstevel@tonic-gate static int hour_warn = 1;
14280Sstevel@tonic-gate static int min_warn = 1;
14290Sstevel@tonic-gate static int sec_warn = 1;
14300Sstevel@tonic-gate int days_diff = days_thru_month[month + 1] - days_thru_month[month];
14310Sstevel@tonic-gate #endif
14320Sstevel@tonic-gate
14330Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock));
14340Sstevel@tonic-gate
14350Sstevel@tonic-gate #ifdef DEBUG
14360Sstevel@tonic-gate if (year_warn && (year < 70 || year > 8029)) {
14370Sstevel@tonic-gate cmn_err(CE_WARN,
14386422Sqiao "The hardware real-time clock appears to have the "
14396422Sqiao "wrong years value %d -- time needs to be reset\n",
14406422Sqiao year);
14410Sstevel@tonic-gate year_warn = 0;
14420Sstevel@tonic-gate }
14430Sstevel@tonic-gate
14440Sstevel@tonic-gate if (month_warn && (tod.tod_month < 1 || tod.tod_month > 12)) {
14450Sstevel@tonic-gate cmn_err(CE_WARN,
14466422Sqiao "The hardware real-time clock appears to have the "
14476422Sqiao "wrong months value %d -- time needs to be reset\n",
14486422Sqiao tod.tod_month);
14490Sstevel@tonic-gate month_warn = 0;
14500Sstevel@tonic-gate }
14510Sstevel@tonic-gate
14520Sstevel@tonic-gate if (day_warn && (tod.tod_day < 1 || tod.tod_day > days_diff)) {
14530Sstevel@tonic-gate cmn_err(CE_WARN,
14546422Sqiao "The hardware real-time clock appears to have the "
14556422Sqiao "wrong days value %d -- time needs to be reset\n",
14566422Sqiao tod.tod_day);
14570Sstevel@tonic-gate day_warn = 0;
14580Sstevel@tonic-gate }
14590Sstevel@tonic-gate
14600Sstevel@tonic-gate if (hour_warn && (tod.tod_hour < 0 || tod.tod_hour > 23)) {
14610Sstevel@tonic-gate cmn_err(CE_WARN,
14626422Sqiao "The hardware real-time clock appears to have the "
14636422Sqiao "wrong hours value %d -- time needs to be reset\n",
14646422Sqiao tod.tod_hour);
14650Sstevel@tonic-gate hour_warn = 0;
14660Sstevel@tonic-gate }
14670Sstevel@tonic-gate
14680Sstevel@tonic-gate if (min_warn && (tod.tod_min < 0 || tod.tod_min > 59)) {
14690Sstevel@tonic-gate cmn_err(CE_WARN,
14706422Sqiao "The hardware real-time clock appears to have the "
14716422Sqiao "wrong minutes value %d -- time needs to be reset\n",
14726422Sqiao tod.tod_min);
14730Sstevel@tonic-gate min_warn = 0;
14740Sstevel@tonic-gate }
14750Sstevel@tonic-gate
14760Sstevel@tonic-gate if (sec_warn && (tod.tod_sec < 0 || tod.tod_sec > 59)) {
14770Sstevel@tonic-gate cmn_err(CE_WARN,
14786422Sqiao "The hardware real-time clock appears to have the "
14796422Sqiao "wrong seconds value %d -- time needs to be reset\n",
14806422Sqiao tod.tod_sec);
14810Sstevel@tonic-gate sec_warn = 0;
14820Sstevel@tonic-gate }
14830Sstevel@tonic-gate #endif
14840Sstevel@tonic-gate
14850Sstevel@tonic-gate utc = (year - 70); /* next 3 lines: utc = 365y + y/4 */
14860Sstevel@tonic-gate utc += (utc << 3) + (utc << 6);
14870Sstevel@tonic-gate utc += (utc << 2) + ((year - 69) >> 2);
14880Sstevel@tonic-gate utc += days_thru_month[month] + tod.tod_day - 1;
14890Sstevel@tonic-gate utc = (utc << 3) + (utc << 4) + tod.tod_hour; /* 24 * day + hour */
14900Sstevel@tonic-gate utc = (utc << 6) - (utc << 2) + tod.tod_min; /* 60 * hour + min */
14910Sstevel@tonic-gate utc = (utc << 6) - (utc << 2) + tod.tod_sec; /* 60 * min + sec */
14920Sstevel@tonic-gate
14930Sstevel@tonic-gate return (utc);
14940Sstevel@tonic-gate }
1495