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