Home | History | Annotate | Line # | Download | only in kern
subr_time.c revision 1.9.6.2
      1  1.9.6.1       tls /*	$NetBSD: subr_time.c,v 1.9.6.2 2017/12/03 11:38:45 jdolecek Exp $	*/
      2      1.1     pooka 
      3      1.1     pooka /*
      4      1.1     pooka  * Copyright (c) 1982, 1986, 1989, 1993
      5      1.1     pooka  *	The Regents of the University of California.  All rights reserved.
      6      1.1     pooka  *
      7      1.1     pooka  * Redistribution and use in source and binary forms, with or without
      8      1.1     pooka  * modification, are permitted provided that the following conditions
      9      1.1     pooka  * are met:
     10      1.1     pooka  * 1. Redistributions of source code must retain the above copyright
     11      1.1     pooka  *    notice, this list of conditions and the following disclaimer.
     12      1.1     pooka  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1     pooka  *    notice, this list of conditions and the following disclaimer in the
     14      1.1     pooka  *    documentation and/or other materials provided with the distribution.
     15      1.1     pooka  * 3. Neither the name of the University nor the names of its contributors
     16      1.1     pooka  *    may be used to endorse or promote products derived from this software
     17      1.1     pooka  *    without specific prior written permission.
     18      1.1     pooka  *
     19      1.1     pooka  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20      1.1     pooka  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21      1.1     pooka  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22      1.1     pooka  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23      1.1     pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24      1.1     pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25      1.1     pooka  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26      1.1     pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27      1.1     pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28      1.1     pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29      1.1     pooka  * SUCH DAMAGE.
     30      1.1     pooka  *
     31      1.1     pooka  *	@(#)kern_clock.c	8.5 (Berkeley) 1/21/94
     32      1.1     pooka  *	@(#)kern_time.c 8.4 (Berkeley) 5/26/95
     33      1.1     pooka  */
     34      1.1     pooka 
     35      1.1     pooka #include <sys/cdefs.h>
     36  1.9.6.1       tls __KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.9.6.2 2017/12/03 11:38:45 jdolecek Exp $");
     37      1.1     pooka 
     38      1.1     pooka #include <sys/param.h>
     39      1.1     pooka #include <sys/kernel.h>
     40  1.9.6.2  jdolecek #include <sys/proc.h>
     41  1.9.6.2  jdolecek #include <sys/kauth.h>
     42  1.9.6.2  jdolecek #include <sys/lwp.h>
     43      1.1     pooka #include <sys/timex.h>
     44      1.1     pooka #include <sys/time.h>
     45      1.1     pooka #include <sys/timetc.h>
     46      1.2        ad #include <sys/intr.h>
     47      1.1     pooka 
     48  1.9.6.2  jdolecek #ifdef DEBUG_STICKS
     49  1.9.6.2  jdolecek #define DPRINTF(a) uprintf a
     50  1.9.6.2  jdolecek #else
     51  1.9.6.2  jdolecek #define DPRINTF(a)
     52  1.9.6.2  jdolecek #endif
     53  1.9.6.2  jdolecek 
     54      1.1     pooka /*
     55      1.1     pooka  * Compute number of hz until specified time.  Used to compute second
     56      1.1     pooka  * argument to callout_reset() from an absolute time.
     57      1.1     pooka  */
     58      1.1     pooka int
     59      1.4  christos tvhzto(const struct timeval *tvp)
     60      1.1     pooka {
     61      1.1     pooka 	struct timeval now, tv;
     62      1.1     pooka 
     63      1.1     pooka 	tv = *tvp;	/* Don't modify original tvp. */
     64      1.1     pooka 	getmicrotime(&now);
     65      1.1     pooka 	timersub(&tv, &now, &tv);
     66      1.1     pooka 	return tvtohz(&tv);
     67      1.1     pooka }
     68      1.1     pooka 
     69      1.1     pooka /*
     70      1.1     pooka  * Compute number of ticks in the specified amount of time.
     71      1.1     pooka  */
     72      1.1     pooka int
     73      1.4  christos tvtohz(const struct timeval *tv)
     74      1.1     pooka {
     75      1.1     pooka 	unsigned long ticks;
     76      1.1     pooka 	long sec, usec;
     77      1.1     pooka 
     78      1.1     pooka 	/*
     79      1.1     pooka 	 * If the number of usecs in the whole seconds part of the time
     80      1.1     pooka 	 * difference fits in a long, then the total number of usecs will
     81      1.1     pooka 	 * fit in an unsigned long.  Compute the total and convert it to
     82      1.1     pooka 	 * ticks, rounding up and adding 1 to allow for the current tick
     83      1.1     pooka 	 * to expire.  Rounding also depends on unsigned long arithmetic
     84      1.1     pooka 	 * to avoid overflow.
     85      1.1     pooka 	 *
     86      1.1     pooka 	 * Otherwise, if the number of ticks in the whole seconds part of
     87      1.1     pooka 	 * the time difference fits in a long, then convert the parts to
     88      1.1     pooka 	 * ticks separately and add, using similar rounding methods and
     89      1.1     pooka 	 * overflow avoidance.  This method would work in the previous
     90      1.1     pooka 	 * case, but it is slightly slower and assumes that hz is integral.
     91      1.1     pooka 	 *
     92      1.1     pooka 	 * Otherwise, round the time difference down to the maximum
     93      1.1     pooka 	 * representable value.
     94      1.1     pooka 	 *
     95      1.1     pooka 	 * If ints are 32-bit, then the maximum value for any timeout in
     96      1.1     pooka 	 * 10ms ticks is 248 days.
     97      1.1     pooka 	 */
     98      1.1     pooka 	sec = tv->tv_sec;
     99      1.1     pooka 	usec = tv->tv_usec;
    100      1.1     pooka 
    101      1.8  drochner 	KASSERT(usec >= 0 && usec < 1000000);
    102      1.8  drochner 
    103      1.8  drochner 	/* catch overflows in conversion time_t->int */
    104      1.8  drochner 	if (tv->tv_sec > INT_MAX)
    105      1.8  drochner 		return INT_MAX;
    106      1.8  drochner 	if (tv->tv_sec < 0)
    107      1.8  drochner 		return 0;
    108      1.1     pooka 
    109      1.8  drochner 	if (sec < 0 || (sec == 0 && usec == 0)) {
    110      1.1     pooka 		/*
    111      1.1     pooka 		 * Would expire now or in the past.  Return 0 ticks.
    112      1.4  christos 		 * This is different from the legacy tvhzto() interface,
    113      1.1     pooka 		 * and callers need to check for it.
    114      1.1     pooka 		 */
    115      1.1     pooka 		ticks = 0;
    116      1.1     pooka 	} else if (sec <= (LONG_MAX / 1000000))
    117      1.1     pooka 		ticks = (((sec * 1000000) + (unsigned long)usec + (tick - 1))
    118      1.1     pooka 		    / tick) + 1;
    119      1.1     pooka 	else if (sec <= (LONG_MAX / hz))
    120      1.1     pooka 		ticks = (sec * hz) +
    121      1.1     pooka 		    (((unsigned long)usec + (tick - 1)) / tick) + 1;
    122      1.1     pooka 	else
    123      1.1     pooka 		ticks = LONG_MAX;
    124      1.1     pooka 
    125      1.1     pooka 	if (ticks > INT_MAX)
    126      1.1     pooka 		ticks = INT_MAX;
    127      1.1     pooka 
    128      1.1     pooka 	return ((int)ticks);
    129      1.1     pooka }
    130      1.1     pooka 
    131      1.4  christos int
    132      1.4  christos tshzto(const struct timespec *tsp)
    133      1.4  christos {
    134      1.4  christos 	struct timespec now, ts;
    135      1.4  christos 
    136      1.4  christos 	ts = *tsp;	/* Don't modify original tsp. */
    137      1.4  christos 	getnanotime(&now);
    138      1.4  christos 	timespecsub(&ts, &now, &ts);
    139      1.4  christos 	return tstohz(&ts);
    140      1.4  christos }
    141      1.9  christos 
    142      1.9  christos int
    143      1.9  christos tshztoup(const struct timespec *tsp)
    144      1.9  christos {
    145      1.9  christos 	struct timespec now, ts;
    146      1.9  christos 
    147      1.9  christos 	ts = *tsp;	/* Don't modify original tsp. */
    148      1.9  christos 	getnanouptime(&now);
    149      1.9  christos 	timespecsub(&ts, &now, &ts);
    150      1.9  christos 	return tstohz(&ts);
    151      1.9  christos }
    152      1.9  christos 
    153      1.1     pooka /*
    154      1.1     pooka  * Compute number of ticks in the specified amount of time.
    155      1.1     pooka  */
    156      1.1     pooka int
    157      1.4  christos tstohz(const struct timespec *ts)
    158      1.1     pooka {
    159      1.1     pooka 	struct timeval tv;
    160      1.1     pooka 
    161      1.1     pooka 	/*
    162      1.1     pooka 	 * usec has great enough resolution for hz, so convert to a
    163      1.1     pooka 	 * timeval and use tvtohz() above.
    164      1.1     pooka 	 */
    165      1.1     pooka 	TIMESPEC_TO_TIMEVAL(&tv, ts);
    166      1.1     pooka 	return tvtohz(&tv);
    167      1.1     pooka }
    168      1.1     pooka 
    169      1.1     pooka /*
    170      1.1     pooka  * Check that a proposed value to load into the .it_value or
    171      1.1     pooka  * .it_interval part of an interval timer is acceptable, and
    172      1.1     pooka  * fix it to have at least minimal value (i.e. if it is less
    173  1.9.6.1       tls  * than the resolution of the clock, round it up.). We don't
    174  1.9.6.1       tls  * timeout the 0,0 value because this means to disable the
    175  1.9.6.1       tls  * timer or the interval.
    176      1.1     pooka  */
    177      1.1     pooka int
    178      1.1     pooka itimerfix(struct timeval *tv)
    179      1.1     pooka {
    180      1.1     pooka 
    181  1.9.6.1       tls 	if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
    182  1.9.6.1       tls 		return EINVAL;
    183  1.9.6.1       tls 	if (tv->tv_sec < 0)
    184  1.9.6.1       tls 		return ETIMEDOUT;
    185      1.1     pooka 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
    186      1.1     pooka 		tv->tv_usec = tick;
    187  1.9.6.1       tls 	return 0;
    188      1.1     pooka }
    189      1.1     pooka 
    190      1.1     pooka int
    191      1.1     pooka itimespecfix(struct timespec *ts)
    192      1.1     pooka {
    193      1.1     pooka 
    194  1.9.6.1       tls 	if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
    195  1.9.6.1       tls 		return EINVAL;
    196  1.9.6.1       tls 	if (ts->tv_sec < 0)
    197  1.9.6.1       tls 		return ETIMEDOUT;
    198      1.1     pooka 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
    199      1.1     pooka 		ts->tv_nsec = tick * 1000;
    200  1.9.6.1       tls 	return 0;
    201      1.1     pooka }
    202      1.5     rmind 
    203      1.5     rmind int
    204      1.5     rmind inittimeleft(struct timespec *ts, struct timespec *sleepts)
    205      1.5     rmind {
    206      1.5     rmind 
    207      1.5     rmind 	if (itimespecfix(ts)) {
    208      1.5     rmind 		return -1;
    209      1.5     rmind 	}
    210      1.5     rmind 	getnanouptime(sleepts);
    211      1.5     rmind 	return 0;
    212      1.5     rmind }
    213      1.5     rmind 
    214      1.5     rmind int
    215      1.5     rmind gettimeleft(struct timespec *ts, struct timespec *sleepts)
    216      1.5     rmind {
    217      1.5     rmind 	struct timespec sleptts;
    218      1.5     rmind 
    219      1.5     rmind 	/*
    220      1.5     rmind 	 * Reduce ts by elapsed time based on monotonic time scale.
    221      1.5     rmind 	 */
    222      1.5     rmind 	getnanouptime(&sleptts);
    223      1.5     rmind 	timespecadd(ts, sleepts, ts);
    224      1.5     rmind 	timespecsub(ts, &sleptts, ts);
    225      1.5     rmind 	*sleepts = sleptts;
    226      1.5     rmind 
    227      1.5     rmind 	return tstohz(ts);
    228      1.5     rmind }
    229      1.5     rmind 
    230  1.9.6.2  jdolecek static void
    231  1.9.6.2  jdolecek ticks2ts(uint64_t ticks, struct timespec *ts)
    232  1.9.6.2  jdolecek {
    233  1.9.6.2  jdolecek 	ts->tv_sec = ticks / hz;
    234  1.9.6.2  jdolecek 	uint64_t sticks = ticks - ts->tv_sec * hz;
    235  1.9.6.2  jdolecek 	if (sticks > BINTIME_SCALE_MS)	/* floor(2^64 / 1000) */
    236  1.9.6.2  jdolecek 		ts->tv_nsec = sticks / hz * 1000000000LL;
    237  1.9.6.2  jdolecek    	else if (sticks > BINTIME_SCALE_US)	/* floor(2^64 / 1000000) */
    238  1.9.6.2  jdolecek    		ts->tv_nsec = sticks * 1000LL / hz * 1000000LL;
    239  1.9.6.2  jdolecek 	else
    240  1.9.6.2  jdolecek    		ts->tv_nsec = sticks * 1000000000LL / hz;
    241  1.9.6.2  jdolecek 	DPRINTF(("%s: %ju/%ju -> %ju.%ju\n", __func__,
    242  1.9.6.2  jdolecek 	    (uintmax_t)ticks, (uintmax_t)sticks,
    243  1.9.6.2  jdolecek 	    (uintmax_t)ts->tv_sec, (uintmax_t)ts->tv_nsec));
    244  1.9.6.2  jdolecek }
    245  1.9.6.2  jdolecek 
    246  1.9.6.1       tls int
    247  1.9.6.1       tls clock_gettime1(clockid_t clock_id, struct timespec *ts)
    248  1.9.6.1       tls {
    249  1.9.6.2  jdolecek 	int error;
    250  1.9.6.2  jdolecek 	uint64_t ticks;
    251  1.9.6.2  jdolecek 	struct proc *p;
    252  1.9.6.2  jdolecek 
    253  1.9.6.2  jdolecek #define CPUCLOCK_ID_MASK (~(CLOCK_THREAD_CPUTIME_ID|CLOCK_PROCESS_CPUTIME_ID))
    254  1.9.6.2  jdolecek 	if (clock_id & CLOCK_PROCESS_CPUTIME_ID) {
    255  1.9.6.2  jdolecek 		pid_t pid = clock_id & CPUCLOCK_ID_MASK;
    256  1.9.6.2  jdolecek 
    257  1.9.6.2  jdolecek 		mutex_enter(proc_lock);
    258  1.9.6.2  jdolecek 		p = pid == 0 ? curproc : proc_find(pid);
    259  1.9.6.2  jdolecek 		if (p == NULL) {
    260  1.9.6.2  jdolecek 			mutex_exit(proc_lock);
    261  1.9.6.2  jdolecek 			return ESRCH;
    262  1.9.6.2  jdolecek 		}
    263  1.9.6.2  jdolecek 		ticks = p->p_uticks + p->p_sticks + p->p_iticks;
    264  1.9.6.2  jdolecek 		DPRINTF(("%s: u=%ju, s=%ju, i=%ju\n", __func__,
    265  1.9.6.2  jdolecek 		    (uintmax_t)p->p_uticks, (uintmax_t)p->p_sticks,
    266  1.9.6.2  jdolecek 		    (uintmax_t)p->p_iticks));
    267  1.9.6.2  jdolecek 		mutex_exit(proc_lock);
    268  1.9.6.2  jdolecek 
    269  1.9.6.2  jdolecek 		// XXX: Perhaps create a special kauth type
    270  1.9.6.2  jdolecek 		error = kauth_authorize_process(curlwp->l_cred,
    271  1.9.6.2  jdolecek 		    KAUTH_PROCESS_PTRACE, p,
    272  1.9.6.2  jdolecek 		    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
    273  1.9.6.2  jdolecek 		if (error)
    274  1.9.6.2  jdolecek 			return error;
    275  1.9.6.2  jdolecek 	} else if (clock_id & CLOCK_THREAD_CPUTIME_ID) {
    276  1.9.6.2  jdolecek 		struct lwp *l;
    277  1.9.6.2  jdolecek 		lwpid_t lid = clock_id & CPUCLOCK_ID_MASK;
    278  1.9.6.2  jdolecek 		p = curproc;
    279  1.9.6.2  jdolecek 		mutex_enter(p->p_lock);
    280  1.9.6.2  jdolecek 		l = lid == 0 ? curlwp : lwp_find(p, lid);
    281  1.9.6.2  jdolecek 		if (l == NULL) {
    282  1.9.6.2  jdolecek 			mutex_exit(p->p_lock);
    283  1.9.6.2  jdolecek 			return ESRCH;
    284  1.9.6.2  jdolecek 		}
    285  1.9.6.2  jdolecek 		ticks = l->l_rticksum + l->l_slpticksum;
    286  1.9.6.2  jdolecek 		DPRINTF(("%s: r=%ju, s=%ju\n", __func__,
    287  1.9.6.2  jdolecek 		    (uintmax_t)l->l_rticksum, (uintmax_t)l->l_slpticksum));
    288  1.9.6.2  jdolecek 		mutex_exit(p->p_lock);
    289  1.9.6.2  jdolecek         } else
    290  1.9.6.2  jdolecek 		ticks = (uint64_t)-1;
    291  1.9.6.2  jdolecek 
    292  1.9.6.2  jdolecek 	if (ticks != (uint64_t)-1) {
    293  1.9.6.2  jdolecek 		ticks2ts(ticks, ts);
    294  1.9.6.2  jdolecek 		return 0;
    295  1.9.6.2  jdolecek 	}
    296  1.9.6.1       tls 
    297  1.9.6.1       tls 	switch (clock_id) {
    298  1.9.6.1       tls 	case CLOCK_REALTIME:
    299  1.9.6.1       tls 		nanotime(ts);
    300  1.9.6.1       tls 		break;
    301  1.9.6.1       tls 	case CLOCK_MONOTONIC:
    302  1.9.6.1       tls 		nanouptime(ts);
    303  1.9.6.1       tls 		break;
    304  1.9.6.1       tls 	default:
    305  1.9.6.1       tls 		return EINVAL;
    306  1.9.6.1       tls 	}
    307  1.9.6.1       tls 
    308  1.9.6.1       tls 	return 0;
    309  1.9.6.1       tls }
    310  1.9.6.1       tls 
    311      1.5     rmind /*
    312      1.5     rmind  * Calculate delta and convert from struct timespec to the ticks.
    313      1.5     rmind  */
    314      1.5     rmind int
    315  1.9.6.1       tls ts2timo(clockid_t clock_id, int flags, struct timespec *ts,
    316  1.9.6.1       tls     int *timo, struct timespec *start)
    317      1.5     rmind {
    318      1.5     rmind 	int error;
    319  1.9.6.1       tls 	struct timespec tsd;
    320      1.5     rmind 
    321  1.9.6.1       tls 	flags &= TIMER_ABSTIME;
    322  1.9.6.1       tls 	if (start == NULL)
    323  1.9.6.1       tls 		start = &tsd;
    324  1.9.6.1       tls 
    325  1.9.6.1       tls 	if (flags || start != &tsd)
    326  1.9.6.1       tls 		if ((error = clock_gettime1(clock_id, start)) != 0)
    327  1.9.6.1       tls 			return error;
    328  1.9.6.1       tls 
    329  1.9.6.1       tls 	if (flags)
    330  1.9.6.1       tls 		timespecsub(ts, start, ts);
    331  1.9.6.1       tls 
    332  1.9.6.1       tls 	if ((error = itimespecfix(ts)) != 0)
    333      1.5     rmind 		return error;
    334  1.9.6.1       tls 
    335  1.9.6.1       tls 	if (ts->tv_sec == 0 && ts->tv_nsec == 0)
    336  1.9.6.1       tls 		return ETIMEDOUT;
    337  1.9.6.1       tls 
    338  1.9.6.1       tls 	*timo = tstohz(ts);
    339  1.9.6.1       tls 	KASSERT(*timo > 0);
    340      1.5     rmind 
    341      1.5     rmind 	return 0;
    342      1.5     rmind }
    343