Home | History | Annotate | Line # | Download | only in kern
subr_time.c revision 1.15
      1  1.15  christos /*	$NetBSD: subr_time.c,v 1.15 2013/04/01 16:37:22 christos 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.15  christos __KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.15 2013/04/01 16:37:22 christos Exp $");
     37   1.1     pooka 
     38   1.1     pooka #include <sys/param.h>
     39   1.1     pooka #include <sys/kernel.h>
     40   1.1     pooka #include <sys/timex.h>
     41   1.1     pooka #include <sys/time.h>
     42   1.1     pooka #include <sys/timetc.h>
     43   1.2        ad #include <sys/intr.h>
     44   1.1     pooka 
     45   1.1     pooka /*
     46   1.1     pooka  * Compute number of hz until specified time.  Used to compute second
     47   1.1     pooka  * argument to callout_reset() from an absolute time.
     48   1.1     pooka  */
     49   1.1     pooka int
     50   1.4  christos tvhzto(const struct timeval *tvp)
     51   1.1     pooka {
     52   1.1     pooka 	struct timeval now, tv;
     53   1.1     pooka 
     54   1.1     pooka 	tv = *tvp;	/* Don't modify original tvp. */
     55   1.1     pooka 	getmicrotime(&now);
     56   1.1     pooka 	timersub(&tv, &now, &tv);
     57   1.1     pooka 	return tvtohz(&tv);
     58   1.1     pooka }
     59   1.1     pooka 
     60   1.1     pooka /*
     61   1.1     pooka  * Compute number of ticks in the specified amount of time.
     62   1.1     pooka  */
     63   1.1     pooka int
     64   1.4  christos tvtohz(const struct timeval *tv)
     65   1.1     pooka {
     66   1.1     pooka 	unsigned long ticks;
     67   1.1     pooka 	long sec, usec;
     68   1.1     pooka 
     69   1.1     pooka 	/*
     70   1.1     pooka 	 * If the number of usecs in the whole seconds part of the time
     71   1.1     pooka 	 * difference fits in a long, then the total number of usecs will
     72   1.1     pooka 	 * fit in an unsigned long.  Compute the total and convert it to
     73   1.1     pooka 	 * ticks, rounding up and adding 1 to allow for the current tick
     74   1.1     pooka 	 * to expire.  Rounding also depends on unsigned long arithmetic
     75   1.1     pooka 	 * to avoid overflow.
     76   1.1     pooka 	 *
     77   1.1     pooka 	 * Otherwise, if the number of ticks in the whole seconds part of
     78   1.1     pooka 	 * the time difference fits in a long, then convert the parts to
     79   1.1     pooka 	 * ticks separately and add, using similar rounding methods and
     80   1.1     pooka 	 * overflow avoidance.  This method would work in the previous
     81   1.1     pooka 	 * case, but it is slightly slower and assumes that hz is integral.
     82   1.1     pooka 	 *
     83   1.1     pooka 	 * Otherwise, round the time difference down to the maximum
     84   1.1     pooka 	 * representable value.
     85   1.1     pooka 	 *
     86   1.1     pooka 	 * If ints are 32-bit, then the maximum value for any timeout in
     87   1.1     pooka 	 * 10ms ticks is 248 days.
     88   1.1     pooka 	 */
     89   1.1     pooka 	sec = tv->tv_sec;
     90   1.1     pooka 	usec = tv->tv_usec;
     91   1.1     pooka 
     92   1.8  drochner 	KASSERT(usec >= 0 && usec < 1000000);
     93   1.8  drochner 
     94   1.8  drochner 	/* catch overflows in conversion time_t->int */
     95   1.8  drochner 	if (tv->tv_sec > INT_MAX)
     96   1.8  drochner 		return INT_MAX;
     97   1.8  drochner 	if (tv->tv_sec < 0)
     98   1.8  drochner 		return 0;
     99   1.1     pooka 
    100   1.8  drochner 	if (sec < 0 || (sec == 0 && usec == 0)) {
    101   1.1     pooka 		/*
    102   1.1     pooka 		 * Would expire now or in the past.  Return 0 ticks.
    103   1.4  christos 		 * This is different from the legacy tvhzto() interface,
    104   1.1     pooka 		 * and callers need to check for it.
    105   1.1     pooka 		 */
    106   1.1     pooka 		ticks = 0;
    107   1.1     pooka 	} else if (sec <= (LONG_MAX / 1000000))
    108   1.1     pooka 		ticks = (((sec * 1000000) + (unsigned long)usec + (tick - 1))
    109   1.1     pooka 		    / tick) + 1;
    110   1.1     pooka 	else if (sec <= (LONG_MAX / hz))
    111   1.1     pooka 		ticks = (sec * hz) +
    112   1.1     pooka 		    (((unsigned long)usec + (tick - 1)) / tick) + 1;
    113   1.1     pooka 	else
    114   1.1     pooka 		ticks = LONG_MAX;
    115   1.1     pooka 
    116   1.1     pooka 	if (ticks > INT_MAX)
    117   1.1     pooka 		ticks = INT_MAX;
    118   1.1     pooka 
    119   1.1     pooka 	return ((int)ticks);
    120   1.1     pooka }
    121   1.1     pooka 
    122   1.4  christos int
    123   1.4  christos tshzto(const struct timespec *tsp)
    124   1.4  christos {
    125   1.4  christos 	struct timespec now, ts;
    126   1.4  christos 
    127   1.4  christos 	ts = *tsp;	/* Don't modify original tsp. */
    128   1.4  christos 	getnanotime(&now);
    129   1.4  christos 	timespecsub(&ts, &now, &ts);
    130   1.4  christos 	return tstohz(&ts);
    131   1.4  christos }
    132   1.9  christos 
    133   1.9  christos int
    134   1.9  christos tshztoup(const struct timespec *tsp)
    135   1.9  christos {
    136   1.9  christos 	struct timespec now, ts;
    137   1.9  christos 
    138   1.9  christos 	ts = *tsp;	/* Don't modify original tsp. */
    139   1.9  christos 	getnanouptime(&now);
    140   1.9  christos 	timespecsub(&ts, &now, &ts);
    141   1.9  christos 	return tstohz(&ts);
    142   1.9  christos }
    143   1.9  christos 
    144   1.1     pooka /*
    145   1.1     pooka  * Compute number of ticks in the specified amount of time.
    146   1.1     pooka  */
    147   1.1     pooka int
    148   1.4  christos tstohz(const struct timespec *ts)
    149   1.1     pooka {
    150   1.1     pooka 	struct timeval tv;
    151   1.1     pooka 
    152   1.1     pooka 	/*
    153   1.1     pooka 	 * usec has great enough resolution for hz, so convert to a
    154   1.1     pooka 	 * timeval and use tvtohz() above.
    155   1.1     pooka 	 */
    156   1.1     pooka 	TIMESPEC_TO_TIMEVAL(&tv, ts);
    157   1.1     pooka 	return tvtohz(&tv);
    158   1.1     pooka }
    159   1.1     pooka 
    160   1.1     pooka /*
    161   1.1     pooka  * Check that a proposed value to load into the .it_value or
    162   1.1     pooka  * .it_interval part of an interval timer is acceptable, and
    163   1.1     pooka  * fix it to have at least minimal value (i.e. if it is less
    164  1.15  christos  * than the resolution of the clock, round it up.). We don't
    165  1.15  christos  * timeout the 0,0 value because this means to disable the
    166  1.15  christos  * timer or the interval.
    167   1.1     pooka  */
    168   1.1     pooka int
    169   1.1     pooka itimerfix(struct timeval *tv)
    170   1.1     pooka {
    171   1.1     pooka 
    172  1.12  christos 	if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
    173  1.12  christos 		return EINVAL;
    174  1.15  christos 	if (tv->tv_sec < 0)
    175  1.12  christos 		return ETIMEDOUT;
    176  1.15  christos 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
    177   1.1     pooka 		tv->tv_usec = tick;
    178  1.12  christos 	return 0;
    179   1.1     pooka }
    180   1.1     pooka 
    181   1.1     pooka int
    182   1.1     pooka itimespecfix(struct timespec *ts)
    183   1.1     pooka {
    184   1.1     pooka 
    185  1.12  christos 	if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
    186  1.12  christos 		return EINVAL;
    187  1.15  christos 	if (ts->tv_sec < 0)
    188  1.12  christos 		return ETIMEDOUT;
    189  1.15  christos 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
    190   1.1     pooka 		ts->tv_nsec = tick * 1000;
    191  1.12  christos 	return 0;
    192   1.1     pooka }
    193   1.5     rmind 
    194   1.5     rmind int
    195   1.5     rmind inittimeleft(struct timespec *ts, struct timespec *sleepts)
    196   1.5     rmind {
    197   1.5     rmind 
    198   1.5     rmind 	if (itimespecfix(ts)) {
    199   1.5     rmind 		return -1;
    200   1.5     rmind 	}
    201   1.5     rmind 	getnanouptime(sleepts);
    202   1.5     rmind 	return 0;
    203   1.5     rmind }
    204   1.5     rmind 
    205   1.5     rmind int
    206   1.5     rmind gettimeleft(struct timespec *ts, struct timespec *sleepts)
    207   1.5     rmind {
    208   1.5     rmind 	struct timespec sleptts;
    209   1.5     rmind 
    210   1.5     rmind 	/*
    211   1.5     rmind 	 * Reduce ts by elapsed time based on monotonic time scale.
    212   1.5     rmind 	 */
    213   1.5     rmind 	getnanouptime(&sleptts);
    214   1.5     rmind 	timespecadd(ts, sleepts, ts);
    215   1.5     rmind 	timespecsub(ts, &sleptts, ts);
    216   1.5     rmind 	*sleepts = sleptts;
    217   1.5     rmind 
    218   1.5     rmind 	return tstohz(ts);
    219   1.5     rmind }
    220   1.5     rmind 
    221  1.11    martin int
    222  1.11    martin clock_gettime1(clockid_t clock_id, struct timespec *ts)
    223  1.11    martin {
    224  1.11    martin 
    225  1.11    martin 	switch (clock_id) {
    226  1.11    martin 	case CLOCK_REALTIME:
    227  1.11    martin 		nanotime(ts);
    228  1.11    martin 		break;
    229  1.11    martin 	case CLOCK_MONOTONIC:
    230  1.11    martin 		nanouptime(ts);
    231  1.11    martin 		break;
    232  1.11    martin 	default:
    233  1.11    martin 		return EINVAL;
    234  1.11    martin 	}
    235  1.11    martin 
    236  1.11    martin 	return 0;
    237  1.11    martin }
    238  1.11    martin 
    239   1.5     rmind /*
    240   1.5     rmind  * Calculate delta and convert from struct timespec to the ticks.
    241   1.5     rmind  */
    242   1.5     rmind int
    243  1.10  christos ts2timo(clockid_t clock_id, int flags, struct timespec *ts,
    244  1.10  christos     int *timo, struct timespec *start)
    245   1.5     rmind {
    246  1.14  christos 	int error;
    247   1.5     rmind 	struct timespec tsd;
    248   1.5     rmind 
    249  1.10  christos 	flags &= TIMER_ABSTIME;
    250  1.10  christos 
    251  1.10  christos 	if (start == NULL || flags)
    252  1.10  christos 		start = &tsd;
    253  1.10  christos 
    254  1.10  christos 	if (start)
    255  1.10  christos 		if ((error = clock_gettime1(clock_id, start)) != 0)
    256  1.10  christos 			return error;
    257  1.10  christos 
    258  1.10  christos 	if (flags)
    259  1.10  christos 		timespecsub(ts, start, ts);
    260  1.10  christos 
    261  1.12  christos 	if ((error = itimespecfix(ts)) != 0)
    262   1.5     rmind 		return error;
    263  1.10  christos 
    264  1.15  christos 	if (ts->tv_sec == 0 && ts->tv_nsec == 0)
    265  1.15  christos 		return ETIMEDOUT;
    266  1.15  christos 
    267  1.14  christos 	*timo = tstohz(ts);
    268  1.14  christos 	KASSERT(*timo > 0);
    269   1.5     rmind 
    270   1.5     rmind 	return 0;
    271   1.5     rmind }
    272