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 * 4. 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$
31 */
32
33#ifndef _SYS_TIME_H_
34#define _SYS_TIME_H_
35
36#include <sys/_timeval.h>
37#include <sys/types.h>
38#include <sys/timespec.h>
39
40struct timezone {
41	int	tz_minuteswest;	/* minutes west of Greenwich */
42	int	tz_dsttime;	/* type of dst correction */
43};
44#define	DST_NONE	0	/* not on dst */
45#define	DST_USA		1	/* USA style dst */
46#define	DST_AUST	2	/* Australian style dst */
47#define	DST_WET		3	/* Western European dst */
48#define	DST_MET		4	/* Middle European dst */
49#define	DST_EET		5	/* Eastern European dst */
50#define	DST_CAN		6	/* Canada */
51
52#if __BSD_VISIBLE
53struct bintime {
54	time_t	sec;
55	uint64_t frac;
56};
57
58static __inline void
59bintime_addx(struct bintime *bt, uint64_t x)
60{
61	uint64_t u;
62
63	u = bt->frac;
64	bt->frac += x;
65	if (u > bt->frac)
66		bt->sec++;
67}
68
69static __inline void
70bintime_add(struct bintime *bt, const struct bintime *bt2)
71{
72	uint64_t u;
73
74	u = bt->frac;
75	bt->frac += bt2->frac;
76	if (u > bt->frac)
77		bt->sec++;
78	bt->sec += bt2->sec;
79}
80
81static __inline void
82bintime_sub(struct bintime *bt, const struct bintime *bt2)
83{
84	uint64_t u;
85
86	u = bt->frac;
87	bt->frac -= bt2->frac;
88	if (u < bt->frac)
89		bt->sec--;
90	bt->sec -= bt2->sec;
91}
92
93static __inline void
94bintime_mul(struct bintime *bt, u_int x)
95{
96	uint64_t p1, p2;
97
98	p1 = (bt->frac & 0xffffffffull) * x;
99	p2 = (bt->frac >> 32) * x + (p1 >> 32);
100	bt->sec *= x;
101	bt->sec += (p2 >> 32);
102	bt->frac = (p2 << 32) | (p1 & 0xffffffffull);
103}
104
105#define	bintime_clear(a)	((a)->sec = (a)->frac = 0)
106#define	bintime_isset(a)	((a)->sec || (a)->frac)
107#define	bintime_cmp(a, b, cmp)						\
108	(((a)->sec == (b)->sec) ?					\
109	    ((a)->frac cmp (b)->frac) :					\
110	    ((a)->sec cmp (b)->sec))
111
112/*-
113 * Background information:
114 *
115 * When converting between timestamps on parallel timescales of differing
116 * resolutions it is historical and scientific practice to round down rather
117 * than doing 4/5 rounding.
118 *
119 *   The date changes at midnight, not at noon.
120 *
121 *   Even at 15:59:59.999999999 it's not four'o'clock.
122 *
123 *   time_second ticks after N.999999999 not after N.4999999999
124 */
125
126static __inline void
127bintime2timespec(const struct bintime *bt, struct timespec *ts)
128{
129
130	ts->tv_sec = bt->sec;
131	ts->tv_nsec = ((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32;
132}
133
134static __inline void
135timespec2bintime(const struct timespec *ts, struct bintime *bt)
136{
137
138	bt->sec = ts->tv_sec;
139	/* 18446744073 = int(2^64 / 1000000000) */
140	bt->frac = ts->tv_nsec * (uint64_t)18446744073LL;
141}
142
143static __inline void
144bintime2timeval(const struct bintime *bt, struct timeval *tv)
145{
146
147	tv->tv_sec = bt->sec;
148	tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32;
149}
150
151static __inline void
152timeval2bintime(const struct timeval *tv, struct bintime *bt)
153{
154
155	bt->sec = tv->tv_sec;
156	/* 18446744073709 = int(2^64 / 1000000) */
157	bt->frac = tv->tv_usec * (uint64_t)18446744073709LL;
158}
159#endif /* __BSD_VISIBLE */
160
161#ifdef _KERNEL
162
163/* Operations on timespecs */
164#define	timespecclear(tvp)	((tvp)->tv_sec = (tvp)->tv_nsec = 0)
165#define	timespecisset(tvp)	((tvp)->tv_sec || (tvp)->tv_nsec)
166#define	timespeccmp(tvp, uvp, cmp)					\
167	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
168	    ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :			\
169	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
170#define timespecadd(vvp, uvp)						\
171	do {								\
172		(vvp)->tv_sec += (uvp)->tv_sec;				\
173		(vvp)->tv_nsec += (uvp)->tv_nsec;			\
174		if ((vvp)->tv_nsec >= 1000000000) {			\
175			(vvp)->tv_sec++;				\
176			(vvp)->tv_nsec -= 1000000000;			\
177		}							\
178	} while (0)
179#define timespecsub(vvp, uvp)						\
180	do {								\
181		(vvp)->tv_sec -= (uvp)->tv_sec;				\
182		(vvp)->tv_nsec -= (uvp)->tv_nsec;			\
183		if ((vvp)->tv_nsec < 0) {				\
184			(vvp)->tv_sec--;				\
185			(vvp)->tv_nsec += 1000000000;			\
186		}							\
187	} while (0)
188
189/* Operations on timevals. */
190
191#define	timevalclear(tvp)		((tvp)->tv_sec = (tvp)->tv_usec = 0)
192#define	timevalisset(tvp)		((tvp)->tv_sec || (tvp)->tv_usec)
193#define	timevalcmp(tvp, uvp, cmp)					\
194	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
195	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
196	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
197
198/* timevaladd and timevalsub are not inlined */
199
200#endif /* _KERNEL */
201
202#ifndef _KERNEL			/* NetBSD/OpenBSD compatible interfaces */
203
204#define	timerclear(tvp)		((tvp)->tv_sec = (tvp)->tv_usec = 0)
205#define	timerisset(tvp)		((tvp)->tv_sec || (tvp)->tv_usec)
206#define	timercmp(tvp, uvp, cmp)					\
207	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
208	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
209	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
210#define timeradd(tvp, uvp, vvp)						\
211	do {								\
212		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
213		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
214		if ((vvp)->tv_usec >= 1000000) {			\
215			(vvp)->tv_sec++;				\
216			(vvp)->tv_usec -= 1000000;			\
217		}							\
218	} while (0)
219#define timersub(tvp, uvp, vvp)						\
220	do {								\
221		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
222		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
223		if ((vvp)->tv_usec < 0) {				\
224			(vvp)->tv_sec--;				\
225			(vvp)->tv_usec += 1000000;			\
226		}							\
227	} while (0)
228#endif
229
230/*
231 * Names of the interval timers, and structure
232 * defining a timer setting.
233 */
234#define	ITIMER_REAL	0
235#define	ITIMER_VIRTUAL	1
236#define	ITIMER_PROF	2
237
238struct itimerval {
239	struct	timeval it_interval;	/* timer interval */
240	struct	timeval it_value;	/* current value */
241};
242
243/*
244 * Getkerninfo clock information structure
245 */
246struct clockinfo {
247	int	hz;		/* clock frequency */
248	int	tick;		/* micro-seconds per hz tick */
249	int	spare;
250	int	stathz;		/* statistics clock frequency */
251	int	profhz;		/* profiling clock frequency */
252};
253
254/* These macros are also in time.h. */
255#ifndef CLOCK_REALTIME
256#define CLOCK_REALTIME	0
257#define CLOCK_VIRTUAL	1
258#define CLOCK_PROF	2
259#define CLOCK_MONOTONIC	4
260#define CLOCK_UPTIME	5		/* FreeBSD-specific. */
261#define CLOCK_UPTIME_PRECISE	7	/* FreeBSD-specific. */
262#define CLOCK_UPTIME_FAST	8	/* FreeBSD-specific. */
263#define CLOCK_REALTIME_PRECISE	9	/* FreeBSD-specific. */
264#define CLOCK_REALTIME_FAST	10	/* FreeBSD-specific. */
265#define CLOCK_MONOTONIC_PRECISE	11	/* FreeBSD-specific. */
266#define CLOCK_MONOTONIC_FAST	12	/* FreeBSD-specific. */
267#define CLOCK_SECOND	13		/* FreeBSD-specific. */
268#define CLOCK_THREAD_CPUTIME_ID	14
269#define CLOCK_PROCESS_CPUTIME_ID	15
270#endif
271
272#ifndef TIMER_ABSTIME
273#define TIMER_RELTIME	0x0	/* relative timer */
274#define TIMER_ABSTIME	0x1	/* absolute timer */
275#endif
276
277#if __BSD_VISIBLE
278#define	CPUCLOCK_WHICH_PID	0
279#define	CPUCLOCK_WHICH_TID	1
280#endif
281
282#ifdef _KERNEL
283
284/*
285 * Kernel to clock driver interface.
286 */
287void	inittodr(time_t base);
288void	resettodr(void);
289
290extern volatile time_t	time_second;
291extern volatile time_t	time_uptime;
292extern struct bintime boottimebin;
293extern struct timeval boottime;
294
295/*
296 * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
297 *
298 * Functions without the "get" prefix returns the best timestamp
299 * we can produce in the given format.
300 *
301 * "bin"   == struct bintime  == seconds + 64 bit fraction of seconds.
302 * "nano"  == struct timespec == seconds + nanoseconds.
303 * "micro" == struct timeval  == seconds + microseconds.
304 *
305 * Functions containing "up" returns time relative to boot and
306 * should be used for calculating time intervals.
307 *
308 * Functions without "up" returns GMT time.
309 *
310 * Functions with the "get" prefix returns a less precise result
311 * much faster than the functions without "get" prefix and should
312 * be used where a precision of 1/hz seconds is acceptable or where
313 * performance is priority. (NB: "precision", _not_ "resolution" !)
314 *
315 */
316
317void	binuptime(struct bintime *bt);
318void	nanouptime(struct timespec *tsp);
319void	microuptime(struct timeval *tvp);
320
321void	bintime(struct bintime *bt);
322void	nanotime(struct timespec *tsp);
323void	microtime(struct timeval *tvp);
324
325void	getbinuptime(struct bintime *bt);
326void	getnanouptime(struct timespec *tsp);
327void	getmicrouptime(struct timeval *tvp);
328
329void	getbintime(struct bintime *bt);
330void	getnanotime(struct timespec *tsp);
331void	getmicrotime(struct timeval *tvp);
332
333/* Other functions */
334int	itimerdecr(struct itimerval *itp, int usec);
335int	itimerfix(struct timeval *tv);
336int	ppsratecheck(struct timeval *, int *, int);
337int	ratecheck(struct timeval *, const struct timeval *);
338void	timevaladd(struct timeval *t1, const struct timeval *t2);
339void	timevalsub(struct timeval *t1, const struct timeval *t2);
340int	tvtohz(struct timeval *tv);
341#else /* !_KERNEL */
342#include <time.h>
343
344#include <sys/cdefs.h>
345#include <sys/select.h>
346
347__BEGIN_DECLS
348int	setitimer(int, const struct itimerval *, struct itimerval *);
349int	utimes(const char *, const struct timeval *);
350
351#if __BSD_VISIBLE
352int	adjtime(const struct timeval *, struct timeval *);
353int	clock_getcpuclockid2(id_t, int, clockid_t *);
354int	futimes(int, const struct timeval *);
355int	futimesat(int, const char *, const struct timeval [2]);
356int	lutimes(const char *, const struct timeval *);
357int	settimeofday(const struct timeval *, const struct timezone *);
358#endif
359
360#if __XSI_VISIBLE
361int	getitimer(int, struct itimerval *);
362int	gettimeofday(struct timeval *, struct timezone *);
363#endif
364
365__END_DECLS
366
367#endif /* !_KERNEL */
368
369#endif /* !_SYS_TIME_H_ */
370