subr_time.c revision 1.1 1 /* $NetBSD: subr_time.c,v 1.1 2007/08/09 07:36:19 pooka 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.1 2007/08/09 07:36:19 pooka 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
44 #ifdef __HAVE_TIMECOUNTER
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 hzto(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 #endif /* __HAVE_TIMECOUNTER */
60
61 /*
62 * Compute number of ticks in the specified amount of time.
63 */
64 int
65 tvtohz(struct timeval *tv)
66 {
67 unsigned long ticks;
68 long sec, usec;
69
70 /*
71 * If the number of usecs in the whole seconds part of the time
72 * difference fits in a long, then the total number of usecs will
73 * fit in an unsigned long. Compute the total and convert it to
74 * ticks, rounding up and adding 1 to allow for the current tick
75 * to expire. Rounding also depends on unsigned long arithmetic
76 * to avoid overflow.
77 *
78 * Otherwise, if the number of ticks in the whole seconds part of
79 * the time difference fits in a long, then convert the parts to
80 * ticks separately and add, using similar rounding methods and
81 * overflow avoidance. This method would work in the previous
82 * case, but it is slightly slower and assumes that hz is integral.
83 *
84 * Otherwise, round the time difference down to the maximum
85 * representable value.
86 *
87 * If ints are 32-bit, then the maximum value for any timeout in
88 * 10ms ticks is 248 days.
89 */
90 sec = tv->tv_sec;
91 usec = tv->tv_usec;
92
93 if (usec < 0) {
94 sec--;
95 usec += 1000000;
96 }
97
98 if (sec < 0 || (sec == 0 && usec <= 0)) {
99 /*
100 * Would expire now or in the past. Return 0 ticks.
101 * This is different from the legacy hzto() interface,
102 * and callers need to check for it.
103 */
104 ticks = 0;
105 } else if (sec <= (LONG_MAX / 1000000))
106 ticks = (((sec * 1000000) + (unsigned long)usec + (tick - 1))
107 / tick) + 1;
108 else if (sec <= (LONG_MAX / hz))
109 ticks = (sec * hz) +
110 (((unsigned long)usec + (tick - 1)) / tick) + 1;
111 else
112 ticks = LONG_MAX;
113
114 if (ticks > INT_MAX)
115 ticks = INT_MAX;
116
117 return ((int)ticks);
118 }
119
120 #ifndef __HAVE_TIMECOUNTER
121 /*
122 * Compute number of hz until specified time. Used to compute second
123 * argument to callout_reset() from an absolute time.
124 */
125 int
126 hzto(struct timeval *tv)
127 {
128 unsigned long ticks;
129 long sec, usec;
130 int s;
131
132 /*
133 * If the number of usecs in the whole seconds part of the time
134 * difference fits in a long, then the total number of usecs will
135 * fit in an unsigned long. Compute the total and convert it to
136 * ticks, rounding up and adding 1 to allow for the current tick
137 * to expire. Rounding also depends on unsigned long arithmetic
138 * to avoid overflow.
139 *
140 * Otherwise, if the number of ticks in the whole seconds part of
141 * the time difference fits in a long, then convert the parts to
142 * ticks separately and add, using similar rounding methods and
143 * overflow avoidance. This method would work in the previous
144 * case, but it is slightly slower and assume that hz is integral.
145 *
146 * Otherwise, round the time difference down to the maximum
147 * representable value.
148 *
149 * If ints are 32-bit, then the maximum value for any timeout in
150 * 10ms ticks is 248 days.
151 */
152 s = splclock();
153 sec = tv->tv_sec - time.tv_sec;
154 usec = tv->tv_usec - time.tv_usec;
155 splx(s);
156
157 if (usec < 0) {
158 sec--;
159 usec += 1000000;
160 }
161
162 if (sec < 0 || (sec == 0 && usec <= 0)) {
163 /*
164 * Would expire now or in the past. Return 0 ticks.
165 * This is different from the legacy hzto() interface,
166 * and callers need to check for it.
167 */
168 ticks = 0;
169 } else if (sec <= (LONG_MAX / 1000000))
170 ticks = (((sec * 1000000) + (unsigned long)usec + (tick - 1))
171 / tick) + 1;
172 else if (sec <= (LONG_MAX / hz))
173 ticks = (sec * hz) +
174 (((unsigned long)usec + (tick - 1)) / tick) + 1;
175 else
176 ticks = LONG_MAX;
177
178 if (ticks > INT_MAX)
179 ticks = INT_MAX;
180
181 return ((int)ticks);
182 }
183 #endif /* !__HAVE_TIMECOUNTER */
184
185 /*
186 * Compute number of ticks in the specified amount of time.
187 */
188 int
189 tstohz(struct timespec *ts)
190 {
191 struct timeval tv;
192
193 /*
194 * usec has great enough resolution for hz, so convert to a
195 * timeval and use tvtohz() above.
196 */
197 TIMESPEC_TO_TIMEVAL(&tv, ts);
198 return tvtohz(&tv);
199 }
200
201 /*
202 * Check that a proposed value to load into the .it_value or
203 * .it_interval part of an interval timer is acceptable, and
204 * fix it to have at least minimal value (i.e. if it is less
205 * than the resolution of the clock, round it up.)
206 */
207 int
208 itimerfix(struct timeval *tv)
209 {
210
211 if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
212 return (EINVAL);
213 if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
214 tv->tv_usec = tick;
215 return (0);
216 }
217
218 int
219 itimespecfix(struct timespec *ts)
220 {
221
222 if (ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
223 return (EINVAL);
224 if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
225 ts->tv_nsec = tick * 1000;
226 return (0);
227 }
228