xref: /dragonfly/sys/sys/time.h (revision d9d67b59)
1 /*
2  * Copyright (c) 1982, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)time.h	8.5 (Berkeley) 5/4/95
30  * $FreeBSD: src/sys/sys/time.h,v 1.42 1999/12/29 04:24:48 peter Exp $
31  */
32 
33 #ifndef _SYS_TIME_H_
34 #define _SYS_TIME_H_
35 
36 #ifdef _KERNEL
37 #include <sys/types.h>
38 #include <machine/clock.h>
39 #else
40 #include <machine/stdint.h>
41 #endif
42 
43 #include <sys/_timespec.h>
44 #include <sys/_timeval.h>
45 #include <sys/select.h>
46 
47 #define	TIMEVAL_TO_TIMESPEC(tv, ts)					\
48 	do {								\
49 		(ts)->tv_sec = (tv)->tv_sec;				\
50 		(ts)->tv_nsec = (tv)->tv_usec * 1000;			\
51 	} while (0)
52 #define	TIMESPEC_TO_TIMEVAL(tv, ts)					\
53 	do {								\
54 		(tv)->tv_sec = (ts)->tv_sec;				\
55 		(tv)->tv_usec = (ts)->tv_nsec / 1000;			\
56 	} while (0)
57 
58 struct timezone {
59 	int	tz_minuteswest;	/* minutes west of Greenwich */
60 	int	tz_dsttime;	/* type of dst correction */
61 };
62 #define	DST_NONE	0	/* not on dst */
63 #define	DST_USA		1	/* USA style dst */
64 #define	DST_AUST	2	/* Australian style dst */
65 #define	DST_WET		3	/* Western European dst */
66 #define	DST_MET		4	/* Middle European dst */
67 #define	DST_EET		5	/* Eastern European dst */
68 #define	DST_CAN		6	/* Canada */
69 
70 #ifdef _KERNEL
71 
72 /* Operations on timespecs */
73 #define	timespecclear(tvp)	((tvp)->tv_sec = (tvp)->tv_nsec = 0)
74 #define	timespecisset(tvp)	((tvp)->tv_sec || (tvp)->tv_nsec)
75 #define	timespeccmp(tvp, uvp, cmp)					\
76 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
77 	    ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :			\
78 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
79 #define timespecadd(vvp, uvp)						\
80 	do {								\
81 		(vvp)->tv_sec += (uvp)->tv_sec;				\
82 		(vvp)->tv_nsec += (uvp)->tv_nsec;			\
83 		if ((vvp)->tv_nsec >= 1000000000) {			\
84 			(vvp)->tv_sec++;				\
85 			(vvp)->tv_nsec -= 1000000000;			\
86 		}							\
87 	} while (0)
88 #define timespecsub(vvp, uvp)						\
89 	do {								\
90 		(vvp)->tv_sec -= (uvp)->tv_sec;				\
91 		(vvp)->tv_nsec -= (uvp)->tv_nsec;			\
92 		if ((vvp)->tv_nsec < 0) {				\
93 			(vvp)->tv_sec--;				\
94 			(vvp)->tv_nsec += 1000000000;			\
95 		}							\
96 	} while (0)
97 
98 /* Operations on timevals. */
99 
100 #define	timevalclear(tvp)		((tvp)->tv_sec = (tvp)->tv_usec = 0)
101 #define	timevalisset(tvp)		((tvp)->tv_sec || (tvp)->tv_usec)
102 #define	timevalcmp(tvp, uvp, cmp)					\
103 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
104 	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
105 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
106 
107 /* timevaladd and timevalsub are not inlined */
108 
109 #endif /* _KERNEL */
110 
111 #ifndef _KERNEL			/* NetBSD/OpenBSD compatible interfaces */
112 
113 #define	timerclear(tvp)		((tvp)->tv_sec = (tvp)->tv_usec = 0)
114 #define	timerisset(tvp)		((tvp)->tv_sec || (tvp)->tv_usec)
115 #define	timercmp(tvp, uvp, cmp)					\
116 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
117 	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
118 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
119 #define timeradd(tvp, uvp, vvp)						\
120 	do {								\
121 		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
122 		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
123 		if ((vvp)->tv_usec >= 1000000) {			\
124 			(vvp)->tv_sec++;				\
125 			(vvp)->tv_usec -= 1000000;			\
126 		}							\
127 	} while (0)
128 #define timersub(tvp, uvp, vvp)						\
129 	do {								\
130 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
131 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
132 		if ((vvp)->tv_usec < 0) {				\
133 			(vvp)->tv_sec--;				\
134 			(vvp)->tv_usec += 1000000;			\
135 		}							\
136 	} while (0)
137 #endif
138 
139 /*
140  * Names of the interval timers, and structure
141  * defining a timer setting.
142  */
143 #define	ITIMER_REAL	0
144 #define	ITIMER_VIRTUAL	1
145 #define	ITIMER_PROF	2
146 
147 struct	itimerval {
148 	struct	timeval it_interval;	/* timer interval */
149 	struct	timeval it_value;	/* current value */
150 };
151 
152 /*
153  * Getkerninfo clock information structure
154  */
155 struct clockinfo {
156 	int	hz;		/* clock frequency */
157 	int	tick;		/* micro-seconds per hz tick */
158 	int	tickadj;	/* clock skew rate for adjtime() */
159 	int	stathz;		/* statistics clock frequency */
160 	int	profhz;		/* profiling clock frequency */
161 };
162 
163 /* CLOCK_REALTIME and TIMER_ABSTIME are supposed to be in time.h */
164 
165 #ifndef CLOCK_REALTIME
166 #define CLOCK_REALTIME		0
167 #endif
168 #define CLOCK_VIRTUAL		1
169 #define CLOCK_PROF		2
170 #define CLOCK_MONOTONIC		4
171 
172 #define CLOCK_UPTIME		5	/* from freebsd */
173 #define CLOCK_UPTIME_PRECISE	7	/* from freebsd */
174 #define CLOCK_UPTIME_FAST	8	/* from freebsd */
175 #define CLOCK_REALTIME_PRECISE	9	/* from freebsd */
176 #define CLOCK_REALTIME_FAST	10	/* from freebsd */
177 #define CLOCK_MONOTONIC_PRECISE	11	/* from freebsd */
178 #define CLOCK_MONOTONIC_FAST	12	/* from freebsd */
179 #define CLOCK_SECOND		13	/* from freebsd */
180 #define CLOCK_THREAD_CPUTIME_ID		14
181 #define CLOCK_PROCESS_CPUTIME_ID	15
182 
183 #define TIMER_RELTIME	0x0	/* relative timer */
184 #ifndef TIMER_ABSTIME
185 #define TIMER_ABSTIME	0x1	/* absolute timer */
186 #endif
187 
188 #ifdef _KERNEL
189 
190 extern time_t	time_second;		/* simple time_t (can step) */
191 extern time_t	time_uptime;		/* monotonic simple uptime / seconds */
192 extern int64_t	ntp_tick_permanent;
193 extern int64_t	ntp_tick_acc;
194 extern int64_t	ntp_delta;
195 extern int64_t	ntp_big_delta;
196 extern int32_t	ntp_tick_delta;
197 extern int32_t	ntp_default_tick_delta;
198 extern time_t	ntp_leap_second;
199 extern int	ntp_leap_insert;
200 
201 void	initclocks_pcpu(void);
202 void	getmicrouptime(struct timeval *tv);
203 void	getmicrotime(struct timeval *tv);
204 void	getnanouptime(struct timespec *tv);
205 void	getnanotime(struct timespec *tv);
206 int	itimerdecr(struct itimerval *itp, int usec);
207 int	itimerfix(struct timeval *tv);
208 int	itimespecfix(struct timespec *ts);
209 int 	ppsratecheck(struct timeval *, int *, int usec);
210 int 	ratecheck(struct timeval *, const struct timeval *);
211 void	microuptime(struct timeval *tv);
212 void	microtime(struct timeval *tv);
213 void	nanouptime(struct timespec *ts);
214 void	nanotime(struct timespec *ts);
215 time_t	get_approximate_time_t(void);
216 void	set_timeofday(struct timespec *ts);
217 void	kern_adjtime(int64_t, int64_t *);
218 void	kern_reladjtime(int64_t);
219 void	timevaladd(struct timeval *, const struct timeval *);
220 void	timevalsub(struct timeval *, const struct timeval *);
221 int	tvtohz_high(struct timeval *);
222 int	tvtohz_low(struct timeval *);
223 int	tstohz_high(struct timespec *);
224 int	tstohz_low(struct timespec *);
225 int	tsc_test_target(int64_t target);
226 void	tsc_delay(int ns);
227 int	nanosleep1(struct timespec *rqt, struct timespec *rmt);
228 
229 tsc_uclock_t tsc_get_target(int ns);
230 
231 #else /* !_KERNEL */
232 
233 #include <time.h>
234 #include <sys/cdefs.h>
235 
236 #endif /* !_KERNEL */
237 
238 __BEGIN_DECLS
239 
240 #if __BSD_VISIBLE
241 int	adjtime(const struct timeval *, struct timeval *);
242 int	futimes(int, const struct timeval *);
243 int	lutimes(const char *, const struct timeval *);
244 int	settimeofday(const struct timeval *, const struct timezone *);
245 #endif
246 
247 #if __XSI_VISIBLE
248 int	getitimer(int, struct itimerval *);
249 int	gettimeofday(struct timeval *, struct timezone *);
250 int	setitimer(int, const struct itimerval *, struct itimerval *);
251 int	utimes(const char *, const struct timeval *);
252 #endif
253 
254 __END_DECLS
255 
256 #endif /* !_SYS_TIME_H_ */
257