xref: /minix/sys/sys/timevar.h (revision 0a6a1f1d)
1 /*	$NetBSD: timevar.h,v 1.34 2015/08/07 08:11:33 ozaki-r 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_MAX	32	/* See ptimers->pts_fired if you enlarge this */
96 #define	TIMERS_ALL	0
97 #define	TIMERS_POSIX	1
98 
99 LIST_HEAD(ptlist, ptimer);
100 
101 struct	ptimers {
102 	struct ptlist pts_virtual;
103 	struct ptlist pts_prof;
104 	struct ptimer *pts_timers[TIMER_MAX];
105 	int pts_fired;
106 };
107 
108 /*
109  * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
110  *
111  * Functions without the "get" prefix returns the best timestamp
112  * we can produce in the given format.
113  *
114  * "bin"   == struct bintime  == seconds + 64 bit fraction of seconds.
115  * "nano"  == struct timespec == seconds + nanoseconds.
116  * "micro" == struct timeval  == seconds + microseconds.
117  *
118  * Functions containing "up" returns time relative to boot and
119  * should be used for calculating time intervals.
120  *
121  * Functions without "up" returns GMT time.
122  *
123  * Functions with the "get" prefix returns a less precise result
124  * much faster than the functions without "get" prefix and should
125  * be used where a precision of 1/HZ (eg 10 msec on a 100HZ machine)
126  * is acceptable or where performance is priority.
127  * (NB: "precision", _not_ "resolution" !)
128  *
129  */
130 
131 void	binuptime(struct bintime *);
132 void	nanouptime(struct timespec *);
133 void	microuptime(struct timeval *);
134 
135 void	bintime(struct bintime *);
136 void	nanotime(struct timespec *);
137 void	microtime(struct timeval *);
138 
139 void	getbinuptime(struct bintime *);
140 void	getnanouptime(struct timespec *);
141 void	getmicrouptime(struct timeval *);
142 
143 void	getbintime(struct bintime *);
144 void	getnanotime(struct timespec *);
145 void	getmicrotime(struct timeval *);
146 
147 /* Other functions */
148 int	ts2timo(clockid_t, int, struct timespec *, int *, struct timespec *);
149 void	adjtime1(const struct timeval *, struct timeval *, struct proc *);
150 int	clock_getres1(clockid_t, struct timespec *);
151 int	clock_gettime1(clockid_t, struct timespec *);
152 int	clock_settime1(struct proc *, clockid_t, const struct timespec *, bool);
153 int	dogetitimer(struct proc *, int, struct itimerval *);
154 int	dosetitimer(struct proc *, int, struct itimerval *);
155 int	dotimer_gettime(int, struct proc *, struct itimerspec *);
156 int	dotimer_settime(int, struct itimerspec *, struct itimerspec *, int,
157 	    struct proc *);
158 int	tshzto(const struct timespec *);
159 int	tshztoup(const struct timespec *);
160 int	tvhzto(const struct timeval *);
161 void	inittimecounter(void);
162 int	itimerfix(struct timeval *);
163 int	itimespecfix(struct timespec *);
164 int	ppsratecheck(struct timeval *, int *, int);
165 int	ratecheck(struct timeval *, const struct timeval *);
166 void	realtimerexpire(void *);
167 int	settime(struct proc *p, struct timespec *);
168 int	nanosleep1(struct lwp *, clockid_t, int, struct timespec *,
169 	    struct timespec *);
170 int	settimeofday1(const struct timeval *, bool,
171 	    const void *, struct lwp *, bool);
172 int	timer_create1(timer_t *, clockid_t, struct sigevent *, copyin_t,
173 	    struct lwp *);
174 void	timer_gettime(struct ptimer *, struct itimerspec *);
175 void	timer_settime(struct ptimer *);
176 struct	ptimers *timers_alloc(struct proc *);
177 void	timers_free(struct proc *, int);
178 void	timer_tick(struct lwp *, bool);
179 int	tstohz(const struct timespec *);
180 int	tvtohz(const struct timeval *);
181 int	inittimeleft(struct timespec *, struct timespec *);
182 int	gettimeleft(struct timespec *, struct timespec *);
183 void	timerupcall(struct lwp *);
184 void	time_init(void);
185 void	time_init2(void);
186 bool	time_wraps(struct timespec *, struct timespec *);
187 
188 extern volatile time_t time_second;	/* current second in the epoch */
189 extern volatile time_t time_uptime;	/* system uptime in seconds */
190 
time_mono_to_wall(time_t t)191 static inline time_t time_mono_to_wall(time_t t)
192 {
193 
194 	return t - time_uptime + time_second;
195 }
196 
time_wall_to_mono(time_t t)197 static inline time_t time_wall_to_mono(time_t t)
198 {
199 
200 	return t - time_second + time_uptime;
201 }
202 
203 #endif /* !_SYS_TIMEVAR_H_ */
204