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