1 /* $NetBSD: timevar.h,v 1.36 2016/03/08 05:02:55 christos Exp $ */
2
3 /*
4 * Copyright (c) 2005, 2008 The NetBSD Foundation.
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 /*
69 * Structure used to manage timers in a process.
70 */
71 struct ptimer {
72 union {
73 callout_t pt_ch;
74 struct {
75 LIST_ENTRY(ptimer) pt_list;
76 int pt_active;
77 } pt_nonreal;
78 } pt_data;
79 struct sigevent pt_ev;
80 struct itimerspec pt_time;
81 struct ksiginfo pt_info;
82 int pt_overruns; /* Overruns currently accumulating */
83 int pt_poverruns; /* Overruns associated w/ a delivery */
84 int pt_type;
85 int pt_entry;
86 int pt_queued;
87 struct proc *pt_proc;
88 TAILQ_ENTRY(ptimer) pt_chain;
89 };
90
91 #define pt_ch pt_data.pt_ch
92 #define pt_list pt_data.pt_nonreal.pt_list
93 #define pt_active pt_data.pt_nonreal.pt_active
94
95 #define TIMER_MIN 4 /* [0..3] are reserved for setitimer(2) */
96 /* REAL=0,VIRTUAL=1,PROF=2,MONOTONIC=3 */
97 #define TIMER_MAX 36 /* 32 is minimum user timers per POSIX */
98 #define TIMERS_ALL 0
99 #define TIMERS_POSIX 1
100
101 LIST_HEAD(ptlist, ptimer);
102
103 struct ptimers {
104 struct ptlist pts_virtual;
105 struct ptlist pts_prof;
106 struct ptimer *pts_timers[TIMER_MAX];
107 };
108
109 /*
110 * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
111 *
112 * Functions without the "get" prefix returns the best timestamp
113 * we can produce in the given format.
114 *
115 * "bin" == struct bintime == seconds + 64 bit fraction of seconds.
116 * "nano" == struct timespec == seconds + nanoseconds.
117 * "micro" == struct timeval == seconds + microseconds.
118 *
119 * Functions containing "up" returns time relative to boot and
120 * should be used for calculating time intervals.
121 *
122 * Functions without "up" returns GMT time.
123 *
124 * Functions with the "get" prefix returns a less precise result
125 * much faster than the functions without "get" prefix and should
126 * be used where a precision of 1/HZ (eg 10 msec on a 100HZ machine)
127 * is acceptable or where performance is priority.
128 * (NB: "precision", _not_ "resolution" !)
129 *
130 */
131
132 void binuptime(struct bintime *);
133 void nanouptime(struct timespec *);
134 void microuptime(struct timeval *);
135
136 void bintime(struct bintime *);
137 void nanotime(struct timespec *);
138 void microtime(struct timeval *);
139
140 void getbinuptime(struct bintime *);
141 void getnanouptime(struct timespec *);
142 void getmicrouptime(struct timeval *);
143
144 void getbintime(struct bintime *);
145 void getnanotime(struct timespec *);
146 void getmicrotime(struct timeval *);
147
148 /* Other functions */
149 int ts2timo(clockid_t, int, struct timespec *, int *, struct timespec *);
150 void adjtime1(const struct timeval *, struct timeval *, struct proc *);
151 int clock_getres1(clockid_t, struct timespec *);
152 int clock_gettime1(clockid_t, struct timespec *);
153 int clock_settime1(struct proc *, clockid_t, const struct timespec *, bool);
154 int dogetitimer(struct proc *, int, struct itimerval *);
155 int dosetitimer(struct proc *, int, struct itimerval *);
156 int dotimer_gettime(int, struct proc *, struct itimerspec *);
157 int dotimer_settime(int, struct itimerspec *, struct itimerspec *, int,
158 struct proc *);
159 int tshzto(const struct timespec *);
160 int tshztoup(const struct timespec *);
161 int tvhzto(const struct timeval *);
162 void inittimecounter(void);
163 int itimerfix(struct timeval *);
164 int itimespecfix(struct timespec *);
165 int ppsratecheck(struct timeval *, int *, int);
166 int ratecheck(struct timeval *, const struct timeval *);
167 void realtimerexpire(void *);
168 int settime(struct proc *p, struct timespec *);
169 int nanosleep1(struct lwp *, clockid_t, int, struct timespec *,
170 struct timespec *);
171 int settimeofday1(const struct timeval *, bool,
172 const void *, struct lwp *, bool);
173 int timer_create1(timer_t *, clockid_t, struct sigevent *, copyin_t,
174 struct lwp *);
175 void timer_gettime(struct ptimer *, struct itimerspec *);
176 void timer_settime(struct ptimer *);
177 struct ptimers *timers_alloc(struct proc *);
178 void timers_free(struct proc *, int);
179 void timer_tick(struct lwp *, bool);
180 int tstohz(const struct timespec *);
181 int tvtohz(const struct timeval *);
182 int inittimeleft(struct timespec *, struct timespec *);
183 int gettimeleft(struct timespec *, struct timespec *);
184 void timerupcall(struct lwp *);
185 void time_init(void);
186 void time_init2(void);
187 bool time_wraps(struct timespec *, struct timespec *);
188
189 extern volatile time_t time_second; /* current second in the epoch */
190 extern volatile time_t time_uptime; /* system uptime in seconds */
191
time_mono_to_wall(time_t t)192 static inline time_t time_mono_to_wall(time_t t)
193 {
194
195 return t - time_uptime + time_second;
196 }
197
time_wall_to_mono(time_t t)198 static inline time_t time_wall_to_mono(time_t t)
199 {
200
201 return t - time_second + time_uptime;
202 }
203
204 #endif /* !_SYS_TIMEVAR_H_ */
205