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