Home | History | Annotate | Line # | Download | only in kern
subr_time.c revision 1.2
      1 /*	$NetBSD: subr_time.c,v 1.2 2007/11/29 18:04:46 ad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)kern_clock.c	8.5 (Berkeley) 1/21/94
     32  *	@(#)kern_time.c 8.4 (Berkeley) 5/26/95
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.2 2007/11/29 18:04:46 ad Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/kernel.h>
     40 #include <sys/timex.h>
     41 #include <sys/time.h>
     42 #include <sys/timetc.h>
     43 #include <sys/intr.h>
     44 
     45 #ifdef __HAVE_TIMECOUNTER
     46 /*
     47  * Compute number of hz until specified time.  Used to compute second
     48  * argument to callout_reset() from an absolute time.
     49  */
     50 int
     51 hzto(struct timeval *tvp)
     52 {
     53 	struct timeval now, tv;
     54 
     55 	tv = *tvp;	/* Don't modify original tvp. */
     56 	getmicrotime(&now);
     57 	timersub(&tv, &now, &tv);
     58 	return tvtohz(&tv);
     59 }
     60 #endif /* __HAVE_TIMECOUNTER */
     61 
     62 /*
     63  * Compute number of ticks in the specified amount of time.
     64  */
     65 int
     66 tvtohz(struct timeval *tv)
     67 {
     68 	unsigned long ticks;
     69 	long sec, usec;
     70 
     71 	/*
     72 	 * If the number of usecs in the whole seconds part of the time
     73 	 * difference fits in a long, then the total number of usecs will
     74 	 * fit in an unsigned long.  Compute the total and convert it to
     75 	 * ticks, rounding up and adding 1 to allow for the current tick
     76 	 * to expire.  Rounding also depends on unsigned long arithmetic
     77 	 * to avoid overflow.
     78 	 *
     79 	 * Otherwise, if the number of ticks in the whole seconds part of
     80 	 * the time difference fits in a long, then convert the parts to
     81 	 * ticks separately and add, using similar rounding methods and
     82 	 * overflow avoidance.  This method would work in the previous
     83 	 * case, but it is slightly slower and assumes that hz is integral.
     84 	 *
     85 	 * Otherwise, round the time difference down to the maximum
     86 	 * representable value.
     87 	 *
     88 	 * If ints are 32-bit, then the maximum value for any timeout in
     89 	 * 10ms ticks is 248 days.
     90 	 */
     91 	sec = tv->tv_sec;
     92 	usec = tv->tv_usec;
     93 
     94 	if (usec < 0) {
     95 		sec--;
     96 		usec += 1000000;
     97 	}
     98 
     99 	if (sec < 0 || (sec == 0 && usec <= 0)) {
    100 		/*
    101 		 * Would expire now or in the past.  Return 0 ticks.
    102 		 * This is different from the legacy hzto() interface,
    103 		 * and callers need to check for it.
    104 		 */
    105 		ticks = 0;
    106 	} else if (sec <= (LONG_MAX / 1000000))
    107 		ticks = (((sec * 1000000) + (unsigned long)usec + (tick - 1))
    108 		    / tick) + 1;
    109 	else if (sec <= (LONG_MAX / hz))
    110 		ticks = (sec * hz) +
    111 		    (((unsigned long)usec + (tick - 1)) / tick) + 1;
    112 	else
    113 		ticks = LONG_MAX;
    114 
    115 	if (ticks > INT_MAX)
    116 		ticks = INT_MAX;
    117 
    118 	return ((int)ticks);
    119 }
    120 
    121 #ifndef __HAVE_TIMECOUNTER
    122 /*
    123  * Compute number of hz until specified time.  Used to compute second
    124  * argument to callout_reset() from an absolute time.
    125  */
    126 int
    127 hzto(struct timeval *tv)
    128 {
    129 	unsigned long ticks;
    130 	long sec, usec;
    131 	int s;
    132 
    133 	/*
    134 	 * If the number of usecs in the whole seconds part of the time
    135 	 * difference fits in a long, then the total number of usecs will
    136 	 * fit in an unsigned long.  Compute the total and convert it to
    137 	 * ticks, rounding up and adding 1 to allow for the current tick
    138 	 * to expire.  Rounding also depends on unsigned long arithmetic
    139 	 * to avoid overflow.
    140 	 *
    141 	 * Otherwise, if the number of ticks in the whole seconds part of
    142 	 * the time difference fits in a long, then convert the parts to
    143 	 * ticks separately and add, using similar rounding methods and
    144 	 * overflow avoidance.  This method would work in the previous
    145 	 * case, but it is slightly slower and assume that hz is integral.
    146 	 *
    147 	 * Otherwise, round the time difference down to the maximum
    148 	 * representable value.
    149 	 *
    150 	 * If ints are 32-bit, then the maximum value for any timeout in
    151 	 * 10ms ticks is 248 days.
    152 	 */
    153 	s = splclock();
    154 	sec = tv->tv_sec - time.tv_sec;
    155 	usec = tv->tv_usec - time.tv_usec;
    156 	splx(s);
    157 
    158 	if (usec < 0) {
    159 		sec--;
    160 		usec += 1000000;
    161 	}
    162 
    163 	if (sec < 0 || (sec == 0 && usec <= 0)) {
    164 		/*
    165 		 * Would expire now or in the past.  Return 0 ticks.
    166 		 * This is different from the legacy hzto() interface,
    167 		 * and callers need to check for it.
    168 		 */
    169 		ticks = 0;
    170 	} else if (sec <= (LONG_MAX / 1000000))
    171 		ticks = (((sec * 1000000) + (unsigned long)usec + (tick - 1))
    172 		    / tick) + 1;
    173 	else if (sec <= (LONG_MAX / hz))
    174 		ticks = (sec * hz) +
    175 		    (((unsigned long)usec + (tick - 1)) / tick) + 1;
    176 	else
    177 		ticks = LONG_MAX;
    178 
    179 	if (ticks > INT_MAX)
    180 		ticks = INT_MAX;
    181 
    182 	return ((int)ticks);
    183 }
    184 #endif /* !__HAVE_TIMECOUNTER */
    185 
    186 /*
    187  * Compute number of ticks in the specified amount of time.
    188  */
    189 int
    190 tstohz(struct timespec *ts)
    191 {
    192 	struct timeval tv;
    193 
    194 	/*
    195 	 * usec has great enough resolution for hz, so convert to a
    196 	 * timeval and use tvtohz() above.
    197 	 */
    198 	TIMESPEC_TO_TIMEVAL(&tv, ts);
    199 	return tvtohz(&tv);
    200 }
    201 
    202 /*
    203  * Check that a proposed value to load into the .it_value or
    204  * .it_interval part of an interval timer is acceptable, and
    205  * fix it to have at least minimal value (i.e. if it is less
    206  * than the resolution of the clock, round it up.)
    207  */
    208 int
    209 itimerfix(struct timeval *tv)
    210 {
    211 
    212 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
    213 		return (EINVAL);
    214 	if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
    215 		tv->tv_usec = tick;
    216 	return (0);
    217 }
    218 
    219 int
    220 itimespecfix(struct timespec *ts)
    221 {
    222 
    223 	if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
    224 		return (EINVAL);
    225 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
    226 		ts->tv_nsec = tick * 1000;
    227 	return (0);
    228 }
    229