FreeBSD/Linux Kernel Cross Reference
sys/sys/timevar.h
1 /* $NetBSD: timevar.h,v 1.49 2022/10/26 23:23:52 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 2005, 2008, 2020 The NetBSD Foundation, Inc.
5 * 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Copyright (c) 1982, 1986, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
57 * @(#)time.h 8.5 (Berkeley) 5/4/95
58 */
59
60 #ifndef _SYS_TIMEVAR_H_
61 #define _SYS_TIMEVAR_H_
62
63 #include <sys/callout.h>
64 #include <sys/queue.h>
65 #include <sys/signal.h>
66 #include <sys/systm.h>
67
68 struct itimer;
69 LIST_HEAD(itlist, itimer);
70
71 /*
72 * Interval timer operations vector.
73 *
74 * Required fields:
75 *
76 * - ito_fire: A function to be called when the itimer fires.
77 * The timer implementation should perform whatever processing
78 * is necessary for that timer type.
79 *
80 * Optional fields:
81 *
82 * - ito_realtime_changed: A function that is called when the system
83 * time (CLOCK_REALTIME) is called.
84 */
85 struct itimer_ops {
86 void (*ito_fire)(struct itimer *);
87 void (*ito_realtime_changed)(struct itimer *);
88 };
89
90 /*
91 * Common interval timer data.
92 */
93 struct itimer {
94 union {
95 struct {
96 callout_t it_ch;
97 LIST_ENTRY(itimer) it_rtchgq;
98 } it_real;
99 struct {
100 struct itlist *it_vlist;
101 LIST_ENTRY(itimer) it_list;
102 bool it_active;
103 } it_virtual;
104 };
105 const struct itimer_ops *it_ops;
106 struct itimerspec it_time;
107 clockid_t it_clockid;
108 int it_overruns; /* Overruns currently accumulating */
109 bool it_dying;
110 };
111
112 #define it_ch it_real.it_ch
113 #define it_rtchgq it_real.it_rtchgq
114
115 #define it_vlist it_virtual.it_vlist
116 #define it_list it_virtual.it_list
117 #define it_active it_virtual.it_active
118
119 /*
120 * Structure used to manage timers in a process.
121 */
122 struct ptimer {
123 struct itimer pt_itimer;/* common interval timer data */
124
125 TAILQ_ENTRY(ptimer) pt_chain; /* link in signalling queue */
126 struct sigevent pt_ev; /* event notification info */
127 int pt_poverruns; /* Overruns associated w/ a delivery */
128 int pt_entry; /* slot in proc's timer table */
129 struct proc *pt_proc; /* associated process */
130 bool pt_queued; /* true if linked into signalling queue */
131 };
132
133 #define TIMER_MIN 4 /* [0..3] are reserved for setitimer(2) */
134 /* REAL=0,VIRTUAL=1,PROF=2,MONOTONIC=3 */
135 #define TIMER_MAX 36 /* 32 is minimum user timers per POSIX */
136 #define TIMERS_ALL 0
137 #define TIMERS_POSIX 1
138
139 struct ptimers {
140 struct itlist pts_virtual;
141 struct itlist pts_prof;
142 struct itimer *pts_timers[TIMER_MAX];
143 };
144
145 /*
146 * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
147 *
148 * Functions without the "get" prefix returns the best timestamp
149 * we can produce in the given format.
150 *
151 * "bin" == struct bintime == seconds + 64 bit fraction of seconds.
152 * "nano" == struct timespec == seconds + nanoseconds.
153 * "micro" == struct timeval == seconds + microseconds.
154 *
155 * Functions containing "up" returns time relative to boot and
156 * should be used for calculating time intervals.
157 *
158 * Functions without "up" returns GMT time.
159 *
160 * Functions with the "get" prefix returns a less precise result
161 * much faster than the functions without "get" prefix and should
162 * be used where a precision of 1/HZ (eg 10 msec on a 100HZ machine)
163 * is acceptable or where performance is priority.
164 * (NB: "precision", _not_ "resolution" !)
165 *
166 */
167
168 void binuptime(struct bintime *);
169 void nanouptime(struct timespec *);
170 void microuptime(struct timeval *);
171
172 void bintime(struct bintime *);
173 void nanotime(struct timespec *);
174 void microtime(struct timeval *);
175
176 void getbinuptime(struct bintime *);
177 void getnanouptime(struct timespec *);
178 void getmicrouptime(struct timeval *);
179
180 void getbintime(struct bintime *);
181 void getnanotime(struct timespec *);
182 void getmicrotime(struct timeval *);
183
184 void getbinboottime(struct bintime *);
185 void getnanoboottime(struct timespec *);
186 void getmicroboottime(struct timeval *);
187
188 /* Other functions */
189 int ts2timo(clockid_t, int, struct timespec *, int *, struct timespec *);
190 void adjtime1(const struct timeval *, struct timeval *, struct proc *);
191 int clock_getres1(clockid_t, struct timespec *);
192 int clock_gettime1(clockid_t, struct timespec *);
193 int clock_settime1(struct proc *, clockid_t, const struct timespec *, bool);
194 void clock_timeleft(clockid_t, struct timespec *, struct timespec *);
195 int dogetitimer(struct proc *, int, struct itimerval *);
196 int dosetitimer(struct proc *, int, struct itimerval *);
197 int dotimer_gettime(int, struct proc *, struct itimerspec *);
198 int dotimer_settime(int, struct itimerspec *, struct itimerspec *, int,
199 struct proc *);
200 int tshzto(const struct timespec *);
201 int tshztoup(const struct timespec *);
202 int tvhzto(const struct timeval *);
203 void inittimecounter(void);
204 int itimerfix(struct timeval *);
205 int itimespecfix(struct timespec *);
206 int ppsratecheck(struct timeval *, int *, int);
207 int ratecheck(struct timeval *, const struct timeval *);
208 int settime(struct proc *p, struct timespec *);
209 int nanosleep1(struct lwp *, clockid_t, int, struct timespec *,
210 struct timespec *);
211 int settimeofday1(const struct timeval *, bool,
212 const void *, struct lwp *, bool);
213 int timer_create1(timer_t *, clockid_t, struct sigevent *, copyin_t,
214 struct lwp *);
215 int tstohz(const struct timespec *);
216 int tvtohz(const struct timeval *);
217 int inittimeleft(struct timespec *, struct timespec *);
218 int gettimeleft(struct timespec *, struct timespec *);
219 void timerupcall(struct lwp *);
220 void time_init(void);
221 bool time_wraps(struct timespec *, struct timespec *);
222
223 void itimer_init(struct itimer *, const struct itimer_ops *,
224 clockid_t, struct itlist *);
225 void itimer_poison(struct itimer *);
226 void itimer_fini(struct itimer *);
227
228 void itimer_lock(void);
229 void itimer_unlock(void);
230 bool itimer_lock_held(void); /* for diagnostic assertions only */
231 int itimer_settime(struct itimer *);
232 void itimer_gettime(const struct itimer *, struct itimerspec *);
233
234 void ptimer_tick(struct lwp *, bool);
235 void ptimers_free(struct proc *, int);
236
237 extern volatile time_t time_second; /* current second in the epoch */
238 extern volatile time_t time_uptime; /* system uptime in seconds */
239
240 extern int time_adjusted;
241
242 #define DEFAULT_TIMEOUT_EPSILON \
243 (&(const struct bintime) { \
244 .sec = 0, \
245 .frac = ((uint64_t)1 << 32)/hz << 32, \
246 })
247
248 static __inline time_t time_mono_to_wall(time_t t)
249 {
250
251 return t - time_uptime + time_second;
252 }
253
254 static __inline time_t time_wall_to_mono(time_t t)
255 {
256
257 return t - time_second + time_uptime;
258 }
259
260 #endif /* !_SYS_TIMEVAR_H_ */
Cache object: b15f17f29bf12f55626cccfb21b11c57
|