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