1139825Simp/*-
21541Srgrimes * Copyright (c) 1982, 1986, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 4. Neither the name of the University nor the names of its contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
2914487Shsu *	@(#)time.h	8.5 (Berkeley) 5/4/95
3050477Speter * $FreeBSD$
311541Srgrimes */
321541Srgrimes
331541Srgrimes#ifndef _SYS_TIME_H_
34239973Sed#define	_SYS_TIME_H_
351541Srgrimes
36108477Smike#include <sys/_timeval.h>
3714487Shsu#include <sys/types.h>
3898270Swollman#include <sys/timespec.h>
3914487Shsu
401541Srgrimesstruct timezone {
411541Srgrimes	int	tz_minuteswest;	/* minutes west of Greenwich */
421541Srgrimes	int	tz_dsttime;	/* type of dst correction */
431541Srgrimes};
441541Srgrimes#define	DST_NONE	0	/* not on dst */
451541Srgrimes#define	DST_USA		1	/* USA style dst */
461541Srgrimes#define	DST_AUST	2	/* Australian style dst */
471541Srgrimes#define	DST_WET		3	/* Western European dst */
481541Srgrimes#define	DST_MET		4	/* Middle European dst */
491541Srgrimes#define	DST_EET		5	/* Eastern European dst */
501541Srgrimes#define	DST_CAN		6	/* Canada */
511541Srgrimes
5298270Swollman#if __BSD_VISIBLE
5390362Sphkstruct bintime {
5495817Sphk	time_t	sec;
5595817Sphk	uint64_t frac;
5690362Sphk};
5790362Sphk
5890362Sphkstatic __inline void
59255135Sdavidebintime_addx(struct bintime *_bt, uint64_t _x)
6090362Sphk{
61255135Sdavide	uint64_t _u;
6290362Sphk
63255135Sdavide	_u = _bt->frac;
64255135Sdavide	_bt->frac += _x;
65255135Sdavide	if (_u > _bt->frac)
66255135Sdavide		_bt->sec++;
6790362Sphk}
6890362Sphk
6990362Sphkstatic __inline void
70255135Sdavidebintime_add(struct bintime *_bt, const struct bintime *_bt2)
7190362Sphk{
72255135Sdavide	uint64_t _u;
7390362Sphk
74255135Sdavide	_u = _bt->frac;
75255135Sdavide	_bt->frac += _bt2->frac;
76255135Sdavide	if (_u > _bt->frac)
77255135Sdavide		_bt->sec++;
78255135Sdavide	_bt->sec += _bt2->sec;
7990362Sphk}
8090362Sphk
8190362Sphkstatic __inline void
82255135Sdavidebintime_sub(struct bintime *_bt, const struct bintime *_bt2)
8390362Sphk{
84255135Sdavide	uint64_t _u;
8590362Sphk
86255135Sdavide	_u = _bt->frac;
87255135Sdavide	_bt->frac -= _bt2->frac;
88255135Sdavide	if (_u < _bt->frac)
89255135Sdavide		_bt->sec--;
90255135Sdavide	_bt->sec -= _bt2->sec;
9190362Sphk}
9290362Sphk
93212336Smavstatic __inline void
94255135Sdavidebintime_mul(struct bintime *_bt, u_int _x)
95212336Smav{
96255135Sdavide	uint64_t _p1, _p2;
97212336Smav
98255135Sdavide	_p1 = (_bt->frac & 0xffffffffull) * _x;
99255135Sdavide	_p2 = (_bt->frac >> 32) * _x + (_p1 >> 32);
100255135Sdavide	_bt->sec *= _x;
101255135Sdavide	_bt->sec += (_p2 >> 32);
102255135Sdavide	_bt->frac = (_p2 << 32) | (_p1 & 0xffffffffull);
103212336Smav}
104212336Smav
105247777Sdavidestatic __inline void
106255135Sdavidebintime_shift(struct bintime *_bt, int _exp)
107247777Sdavide{
108247777Sdavide
109255135Sdavide	if (_exp > 0) {
110255135Sdavide		_bt->sec <<= _exp;
111255135Sdavide		_bt->sec |= _bt->frac >> (64 - _exp);
112255135Sdavide		_bt->frac <<= _exp;
113255135Sdavide	} else if (_exp < 0) {
114255135Sdavide		_bt->frac >>= -_exp;
115255135Sdavide		_bt->frac |= (uint64_t)_bt->sec << (64 + _exp);
116255135Sdavide		_bt->sec >>= -_exp;
117247777Sdavide	}
118247777Sdavide}
119247777Sdavide
120212336Smav#define	bintime_clear(a)	((a)->sec = (a)->frac = 0)
121212336Smav#define	bintime_isset(a)	((a)->sec || (a)->frac)
122212336Smav#define	bintime_cmp(a, b, cmp)						\
123212336Smav	(((a)->sec == (b)->sec) ?					\
124212336Smav	    ((a)->frac cmp (b)->frac) :					\
125212336Smav	    ((a)->sec cmp (b)->sec))
126212336Smav
127247452Smav#define	SBT_1S	((sbintime_t)1 << 32)
128247452Smav#define	SBT_1M	(SBT_1S * 60)
129247452Smav#define	SBT_1MS	(SBT_1S / 1000)
130247452Smav#define	SBT_1US	(SBT_1S / 1000000)
131247673Smav#define	SBT_1NS	(SBT_1S / 1000000000)
132247452Smav
133247452Smavstatic __inline int
134255135Sdavidesbintime_getsec(sbintime_t _sbt)
135247452Smav{
136247452Smav
137255135Sdavide	return (_sbt >> 32);
138247452Smav}
139247452Smav
140247452Smavstatic __inline sbintime_t
141255135Sdavidebttosbt(const struct bintime _bt)
142247452Smav{
143247452Smav
144255135Sdavide	return (((sbintime_t)_bt.sec << 32) + (_bt.frac >> 32));
145247452Smav}
146247452Smav
147247452Smavstatic __inline struct bintime
148255135Sdavidesbttobt(sbintime_t _sbt)
149247452Smav{
150255135Sdavide	struct bintime _bt;
151247673Smav
152255135Sdavide	_bt.sec = _sbt >> 32;
153255135Sdavide	_bt.frac = _sbt << 32;
154255135Sdavide	return (_bt);
155247452Smav}
156247452Smav
157210226Strasz/*-
15892769Sphk * Background information:
15992769Sphk *
16092769Sphk * When converting between timestamps on parallel timescales of differing
16192769Sphk * resolutions it is historical and scientific practice to round down rather
16292769Sphk * than doing 4/5 rounding.
16392769Sphk *
16492769Sphk *   The date changes at midnight, not at noon.
16592769Sphk *
16692769Sphk *   Even at 15:59:59.999999999 it's not four'o'clock.
16792769Sphk *
16892769Sphk *   time_second ticks after N.999999999 not after N.4999999999
16992769Sphk */
17092769Sphk
17190362Sphkstatic __inline void
172255135Sdavidebintime2timespec(const struct bintime *_bt, struct timespec *_ts)
17390362Sphk{
17490362Sphk
175255135Sdavide	_ts->tv_sec = _bt->sec;
176255135Sdavide	_ts->tv_nsec = ((uint64_t)1000000000 *
177255135Sdavide	    (uint32_t)(_bt->frac >> 32)) >> 32;
17890362Sphk}
17990362Sphk
18090362Sphkstatic __inline void
181255135Sdavidetimespec2bintime(const struct timespec *_ts, struct bintime *_bt)
18290362Sphk{
18390362Sphk
184255135Sdavide	_bt->sec = _ts->tv_sec;
18592769Sphk	/* 18446744073 = int(2^64 / 1000000000) */
186255135Sdavide	_bt->frac = _ts->tv_nsec * (uint64_t)18446744073LL;
18790362Sphk}
18890362Sphk
18990362Sphkstatic __inline void
190255135Sdavidebintime2timeval(const struct bintime *_bt, struct timeval *_tv)
19190362Sphk{
19290362Sphk
193255135Sdavide	_tv->tv_sec = _bt->sec;
194255135Sdavide	_tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(_bt->frac >> 32)) >> 32;
19590362Sphk}
19690362Sphk
19790362Sphkstatic __inline void
198255135Sdavidetimeval2bintime(const struct timeval *_tv, struct bintime *_bt)
19990362Sphk{
20090362Sphk
201255135Sdavide	_bt->sec = _tv->tv_sec;
20292769Sphk	/* 18446744073709 = int(2^64 / 1000000) */
203255135Sdavide	_bt->frac = _tv->tv_usec * (uint64_t)18446744073709LL;
20490362Sphk}
205247452Smav
206247452Smavstatic __inline struct timespec
207255135Sdavidesbttots(sbintime_t _sbt)
208247452Smav{
209255135Sdavide	struct timespec _ts;
210247452Smav
211255135Sdavide	_ts.tv_sec = _sbt >> 32;
212255135Sdavide	_ts.tv_nsec = ((uint64_t)1000000000 * (uint32_t)_sbt) >> 32;
213255135Sdavide	return (_ts);
214247452Smav}
215247452Smav
216247452Smavstatic __inline sbintime_t
217255135Sdavidetstosbt(struct timespec _ts)
218247452Smav{
219247452Smav
220255135Sdavide	return (((sbintime_t)_ts.tv_sec << 32) +
221255135Sdavide	    (_ts.tv_nsec * (((uint64_t)1 << 63) / 500000000) >> 32));
222247452Smav}
223247452Smav
224247452Smavstatic __inline struct timeval
225255135Sdavidesbttotv(sbintime_t _sbt)
226247452Smav{
227255135Sdavide	struct timeval _tv;
228247452Smav
229255135Sdavide	_tv.tv_sec = _sbt >> 32;
230255135Sdavide	_tv.tv_usec = ((uint64_t)1000000 * (uint32_t)_sbt) >> 32;
231255135Sdavide	return (_tv);
232247452Smav}
233247452Smav
234247452Smavstatic __inline sbintime_t
235255135Sdavidetvtosbt(struct timeval _tv)
236247452Smav{
237247452Smav
238255135Sdavide	return (((sbintime_t)_tv.tv_sec << 32) +
239255135Sdavide	    (_tv.tv_usec * (((uint64_t)1 << 63) / 500000) >> 32));
240247452Smav}
241247338Sdelphij#endif /* __BSD_VISIBLE */
24290362Sphk
243247338Sdelphij#ifdef _KERNEL
244247338Sdelphij
24535029Sphk/* Operations on timespecs */
24635401Seivind#define	timespecclear(tvp)	((tvp)->tv_sec = (tvp)->tv_nsec = 0)
24735029Sphk#define	timespecisset(tvp)	((tvp)->tv_sec || (tvp)->tv_nsec)
24835029Sphk#define	timespeccmp(tvp, uvp, cmp)					\
24935029Sphk	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
25035029Sphk	    ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :			\
25135029Sphk	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
252239973Sed#define	timespecadd(vvp, uvp)						\
25335029Sphk	do {								\
25435058Sphk		(vvp)->tv_sec += (uvp)->tv_sec;				\
25535058Sphk		(vvp)->tv_nsec += (uvp)->tv_nsec;			\
25635029Sphk		if ((vvp)->tv_nsec >= 1000000000) {			\
25735029Sphk			(vvp)->tv_sec++;				\
25835029Sphk			(vvp)->tv_nsec -= 1000000000;			\
25935029Sphk		}							\
26035029Sphk	} while (0)
261239973Sed#define	timespecsub(vvp, uvp)						\
26235029Sphk	do {								\
26335058Sphk		(vvp)->tv_sec -= (uvp)->tv_sec;				\
26435058Sphk		(vvp)->tv_nsec -= (uvp)->tv_nsec;			\
26535029Sphk		if ((vvp)->tv_nsec < 0) {				\
26635029Sphk			(vvp)->tv_sec--;				\
26735029Sphk			(vvp)->tv_nsec += 1000000000;			\
26835029Sphk		}							\
26935029Sphk	} while (0)
27035058Sphk
2711541Srgrimes/* Operations on timevals. */
27235058Sphk
27374574Smarkm#define	timevalclear(tvp)		((tvp)->tv_sec = (tvp)->tv_usec = 0)
27435058Sphk#define	timevalisset(tvp)		((tvp)->tv_sec || (tvp)->tv_usec)
27535029Sphk#define	timevalcmp(tvp, uvp, cmp)					\
27635029Sphk	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
27735029Sphk	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
27835029Sphk	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
27935058Sphk
28035058Sphk/* timevaladd and timevalsub are not inlined */
28135058Sphk
282247338Sdelphij#endif /* _KERNEL */
28335029Sphk
28472093Sasmodai#ifndef _KERNEL			/* NetBSD/OpenBSD compatible interfaces */
28535058Sphk
28674574Smarkm#define	timerclear(tvp)		((tvp)->tv_sec = (tvp)->tv_usec = 0)
2871541Srgrimes#define	timerisset(tvp)		((tvp)->tv_sec || (tvp)->tv_usec)
28835058Sphk#define	timercmp(tvp, uvp, cmp)					\
2891541Srgrimes	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
2901541Srgrimes	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
2911541Srgrimes	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
292239973Sed#define	timeradd(tvp, uvp, vvp)						\
29321099Speter	do {								\
29421099Speter		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
29521099Speter		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
29621099Speter		if ((vvp)->tv_usec >= 1000000) {			\
29721099Speter			(vvp)->tv_sec++;				\
29821099Speter			(vvp)->tv_usec -= 1000000;			\
29921099Speter		}							\
30021099Speter	} while (0)
301239973Sed#define	timersub(tvp, uvp, vvp)						\
30221099Speter	do {								\
30321099Speter		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
30421099Speter		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
30521099Speter		if ((vvp)->tv_usec < 0) {				\
30621099Speter			(vvp)->tv_sec--;				\
30721099Speter			(vvp)->tv_usec += 1000000;			\
30821099Speter		}							\
30921099Speter	} while (0)
31021099Speter#endif
3111541Srgrimes
3121541Srgrimes/*
3131541Srgrimes * Names of the interval timers, and structure
3141541Srgrimes * defining a timer setting.
3151541Srgrimes */
3161541Srgrimes#define	ITIMER_REAL	0
3171541Srgrimes#define	ITIMER_VIRTUAL	1
3181541Srgrimes#define	ITIMER_PROF	2
3191541Srgrimes
32083045Sobrienstruct itimerval {
3211541Srgrimes	struct	timeval it_interval;	/* timer interval */
3221541Srgrimes	struct	timeval it_value;	/* current value */
3231541Srgrimes};
3241541Srgrimes
3251541Srgrimes/*
3261541Srgrimes * Getkerninfo clock information structure
3271541Srgrimes */
3281541Srgrimesstruct clockinfo {
3291541Srgrimes	int	hz;		/* clock frequency */
3301541Srgrimes	int	tick;		/* micro-seconds per hz tick */
33196052Sbde	int	spare;
3321541Srgrimes	int	stathz;		/* statistics clock frequency */
3331541Srgrimes	int	profhz;		/* profiling clock frequency */
3341541Srgrimes};
3351541Srgrimes
336144529Sdas/* These macros are also in time.h. */
33734030Sdufault#ifndef CLOCK_REALTIME
338239973Sed#define	CLOCK_REALTIME	0
339239973Sed#define	CLOCK_VIRTUAL	1
340239973Sed#define	CLOCK_PROF	2
341239973Sed#define	CLOCK_MONOTONIC	4
342239973Sed#define	CLOCK_UPTIME	5		/* FreeBSD-specific. */
343239973Sed#define	CLOCK_UPTIME_PRECISE	7	/* FreeBSD-specific. */
344239973Sed#define	CLOCK_UPTIME_FAST	8	/* FreeBSD-specific. */
345239973Sed#define	CLOCK_REALTIME_PRECISE	9	/* FreeBSD-specific. */
346239973Sed#define	CLOCK_REALTIME_FAST	10	/* FreeBSD-specific. */
347239973Sed#define	CLOCK_MONOTONIC_PRECISE	11	/* FreeBSD-specific. */
348239973Sed#define	CLOCK_MONOTONIC_FAST	12	/* FreeBSD-specific. */
349239973Sed#define	CLOCK_SECOND	13		/* FreeBSD-specific. */
350239973Sed#define	CLOCK_THREAD_CPUTIME_ID	14
351239973Sed#define	CLOCK_PROCESS_CPUTIME_ID	15
352144529Sdas#endif
35325578Speter
354144529Sdas#ifndef TIMER_ABSTIME
355239973Sed#define	TIMER_RELTIME	0x0	/* relative timer */
356239973Sed#define	TIMER_ABSTIME	0x1	/* absolute timer */
35734030Sdufault#endif
35825578Speter
359239347Sdavidxu#if __BSD_VISIBLE
360239347Sdavidxu#define	CPUCLOCK_WHICH_PID	0
361239347Sdavidxu#define	CPUCLOCK_WHICH_TID	1
362239347Sdavidxu#endif
363239347Sdavidxu
36455205Speter#ifdef _KERNEL
365178429Sphk
366178429Sphk/*
367178429Sphk * Kernel to clock driver interface.
368178429Sphk */
369178429Sphkvoid	inittodr(time_t base);
370178429Sphkvoid	resettodr(void);
371178429Sphk
372246037Sjhbextern volatile time_t	time_second;
373246037Sjhbextern volatile time_t	time_uptime;
374209216Sjkimextern struct bintime boottimebin;
375126401Sphkextern struct timeval boottime;
376247777Sdavideextern struct bintime tc_tick_bt;
377247777Sdavideextern sbintime_t tc_tick_sbt;
378247777Sdavideextern struct bintime tick_bt;
379247777Sdavideextern sbintime_t tick_sbt;
380247777Sdavideextern int tc_precexp;
381247777Sdavideextern int tc_timepercentage;
382247777Sdavideextern struct bintime bt_timethreshold;
383247777Sdavideextern struct bintime bt_tickthreshold;
384247777Sdavideextern sbintime_t sbt_timethreshold;
385247777Sdavideextern sbintime_t sbt_tickthreshold;
38633690Sphk
38795817Sphk/*
38895491Sphk * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
38995491Sphk *
39095491Sphk * Functions without the "get" prefix returns the best timestamp
39195491Sphk * we can produce in the given format.
39295491Sphk *
39395491Sphk * "bin"   == struct bintime  == seconds + 64 bit fraction of seconds.
39495491Sphk * "nano"  == struct timespec == seconds + nanoseconds.
39595491Sphk * "micro" == struct timeval  == seconds + microseconds.
396239973Sed *
39795491Sphk * Functions containing "up" returns time relative to boot and
39895491Sphk * should be used for calculating time intervals.
39995491Sphk *
40095491Sphk * Functions without "up" returns GMT time.
40195491Sphk *
40295491Sphk * Functions with the "get" prefix returns a less precise result
40395491Sphk * much faster than the functions without "get" prefix and should
404198570Sru * be used where a precision of 1/hz seconds is acceptable or where
405239973Sed * performance is priority. (NB: "precision", _not_ "resolution" !)
40695491Sphk */
40795491Sphk
40890362Sphkvoid	binuptime(struct bintime *bt);
40995817Sphkvoid	nanouptime(struct timespec *tsp);
41095817Sphkvoid	microuptime(struct timeval *tvp);
41195491Sphk
412247452Smavstatic __inline sbintime_t
413247452Smavsbinuptime(void)
414247452Smav{
415255135Sdavide	struct bintime _bt;
416247452Smav
417255135Sdavide	binuptime(&_bt);
418255135Sdavide	return (bttosbt(_bt));
419247452Smav}
420247452Smav
42190362Sphkvoid	bintime(struct bintime *bt);
42295817Sphkvoid	nanotime(struct timespec *tsp);
42395817Sphkvoid	microtime(struct timeval *tvp);
42495491Sphk
42595491Sphkvoid	getbinuptime(struct bintime *bt);
42695491Sphkvoid	getnanouptime(struct timespec *tsp);
42795817Sphkvoid	getmicrouptime(struct timeval *tvp);
42895491Sphk
429247452Smavstatic __inline sbintime_t
430247452Smavgetsbinuptime(void)
431247452Smav{
432255135Sdavide	struct bintime _bt;
433247452Smav
434255135Sdavide	getbinuptime(&_bt);
435255135Sdavide	return (bttosbt(_bt));
436247452Smav}
437247452Smav
43895491Sphkvoid	getbintime(struct bintime *bt);
43995491Sphkvoid	getnanotime(struct timespec *tsp);
44095817Sphkvoid	getmicrotime(struct timeval *tvp);
44195491Sphk
44295817Sphk/* Other functions */
44392719Salfredint	itimerdecr(struct itimerval *itp, int usec);
44492719Salfredint	itimerfix(struct timeval *tv);
445108142Ssamint	ppsratecheck(struct timeval *, int *, int);
446108142Ssamint	ratecheck(struct timeval *, const struct timeval *);
447121523Salfredvoid	timevaladd(struct timeval *t1, const struct timeval *t2);
448121523Salfredvoid	timevalsub(struct timeval *t1, const struct timeval *t2);
44995817Sphkint	tvtohz(struct timeval *tv);
450247777Sdavide
451247777Sdavide#define	TC_DEFAULTPERC		5
452247777Sdavide
453247777Sdavide#define	BT2FREQ(bt)                                                     \
454247777Sdavide	(((uint64_t)0x8000000000000000 + ((bt)->frac >> 2)) /           \
455247777Sdavide	    ((bt)->frac >> 1))
456247777Sdavide
457247777Sdavide#define	SBT2FREQ(sbt)	((SBT_1S + ((sbt) >> 1)) / (sbt))
458247777Sdavide
459247777Sdavide#define	FREQ2BT(freq, bt)                                               \
460247777Sdavide{									\
461247777Sdavide	(bt)->sec = 0;                                                  \
462247777Sdavide	(bt)->frac = ((uint64_t)0x8000000000000000  / (freq)) << 1;     \
463247777Sdavide}
464247777Sdavide
465247777Sdavide#define	TIMESEL(sbt, sbt2)						\
466247777Sdavide	(((sbt2) >= sbt_timethreshold) ?				\
467247777Sdavide	    ((*(sbt) = getsbinuptime()), 1) : ((*(sbt) = sbinuptime()), 0))
468247777Sdavide
46955205Speter#else /* !_KERNEL */
4701541Srgrimes#include <time.h>
4711541Srgrimes
4721541Srgrimes#include <sys/cdefs.h>
473189821Sdas#include <sys/select.h>
4741541Srgrimes
4751541Srgrimes__BEGIN_DECLS
476189821Sdasint	setitimer(int, const struct itimerval *, struct itimerval *);
477189821Sdasint	utimes(const char *, const struct timeval *);
478189821Sdas
479189821Sdas#if __BSD_VISIBLE
48092719Salfredint	adjtime(const struct timeval *, struct timeval *);
481239347Sdavidxuint	clock_getcpuclockid2(id_t, int, clockid_t *);
48292719Salfredint	futimes(int, const struct timeval *);
483189821Sdasint	futimesat(int, const char *, const struct timeval [2]);
484189821Sdasint	lutimes(const char *, const struct timeval *);
485189821Sdasint	settimeofday(const struct timeval *, const struct timezone *);
486189821Sdas#endif
487189821Sdas
488189821Sdas#if __XSI_VISIBLE
48992719Salfredint	getitimer(int, struct itimerval *);
49092719Salfredint	gettimeofday(struct timeval *, struct timezone *);
491189821Sdas#endif
492189821Sdas
4931541Srgrimes__END_DECLS
4941541Srgrimes
49555205Speter#endif /* !_KERNEL */
4961541Srgrimes
4971541Srgrimes#endif /* !_SYS_TIME_H_ */
498