xref: /src/sys/sys/time.h (revision 00dccc3164c6dff38350a1baeeea7238acf2efc3)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1993
5  *	The Regents of the University of California.  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  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef _SYS_TIME_H_
33 #define	_SYS_TIME_H_
34 
35 #include <sys/_timeval.h>
36 #include <sys/types.h>
37 #include <sys/timespec.h>
38 #include <sys/_clock_id.h>
39 
40 struct 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
53 struct bintime {
54 	time_t	sec;
55 	uint64_t frac;
56 };
57 
58 static __inline void
bintime_addx(struct bintime * _bt,uint64_t _x)59 bintime_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 
69 static __inline void
bintime_add(struct bintime * _bt,const struct bintime * _bt2)70 bintime_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 
81 static __inline void
bintime_sub(struct bintime * _bt,const struct bintime * _bt2)82 bintime_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 
93 static __inline void
bintime_mul(struct bintime * _bt,u_int _x)94 bintime_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 static __inline void
bintime_shift(struct bintime * _bt,int _exp)106 bintime_shift(struct bintime *_bt, int _exp)
107 {
108 
109 	if (_exp > 0) {
110 		_bt->sec <<= _exp;
111 		_bt->sec |= _bt->frac >> (64 - _exp);
112 		_bt->frac <<= _exp;
113 	} else if (_exp < 0) {
114 		_bt->frac >>= -_exp;
115 		_bt->frac |= (uint64_t)_bt->sec << (64 + _exp);
116 		_bt->sec >>= -_exp;
117 	}
118 }
119 
120 #define	bintime_clear(a)	((a)->sec = (a)->frac = 0)
121 #define	bintime_isset(a)	((a)->sec || (a)->frac)
122 #define	bintime_cmp(a, b, cmp)						\
123 	(((a)->sec == (b)->sec) ?					\
124 	    ((a)->frac cmp (b)->frac) :					\
125 	    ((a)->sec cmp (b)->sec))
126 
127 #define	SBT_1S	((sbintime_t)1 << 32)
128 #define	SBT_1M	(SBT_1S * 60)
129 #define	SBT_1MS	(SBT_1S / 1000)
130 #define	SBT_1US	(SBT_1S / 1000000)
131 #define	SBT_1NS	(SBT_1S / 1000000000) /* beware rounding, see nstosbt() */
132 #define	SBT_MAX	0x7fffffffffffffffLL
133 
134 static __inline int
sbintime_getsec(sbintime_t _sbt)135 sbintime_getsec(sbintime_t _sbt)
136 {
137 
138 	return (_sbt >> 32);
139 }
140 
141 static __inline sbintime_t
bttosbt(const struct bintime _bt)142 bttosbt(const struct bintime _bt)
143 {
144 
145 	return (((sbintime_t)_bt.sec << 32) + (_bt.frac >> 32));
146 }
147 
148 static __inline struct bintime
sbttobt(sbintime_t _sbt)149 sbttobt(sbintime_t _sbt)
150 {
151 	struct bintime _bt;
152 
153 	_bt.sec = _sbt >> 32;
154 	_bt.frac = _sbt << 32;
155 	return (_bt);
156 }
157 
158 /*
159  * Scaling functions for signed and unsigned 64-bit time using any
160  * 32-bit fraction:
161  */
162 
163 static __inline int64_t
__stime64_scale32_ceil(int64_t x,int32_t factor,int32_t divisor)164 __stime64_scale32_ceil(int64_t x, int32_t factor, int32_t divisor)
165 {
166 	const int64_t rem = x % divisor;
167 
168 	return (x / divisor * factor + (rem * factor + divisor - 1) / divisor);
169 }
170 
171 static __inline int64_t
__stime64_scale32_floor(int64_t x,int32_t factor,int32_t divisor)172 __stime64_scale32_floor(int64_t x, int32_t factor, int32_t divisor)
173 {
174 	const int64_t rem = x % divisor;
175 
176 	return (x / divisor * factor + (rem * factor) / divisor);
177 }
178 
179 static __inline uint64_t
__utime64_scale32_ceil(uint64_t x,uint32_t factor,uint32_t divisor)180 __utime64_scale32_ceil(uint64_t x, uint32_t factor, uint32_t divisor)
181 {
182 	const uint64_t rem = x % divisor;
183 
184 	return (x / divisor * factor + (rem * factor + divisor - 1) / divisor);
185 }
186 
187 static __inline uint64_t
__utime64_scale32_floor(uint64_t x,uint32_t factor,uint32_t divisor)188 __utime64_scale32_floor(uint64_t x, uint32_t factor, uint32_t divisor)
189 {
190 	const uint64_t rem = x % divisor;
191 
192 	return (x / divisor * factor + (rem * factor) / divisor);
193 }
194 
195 /*
196  * This function finds the common divisor between the two arguments,
197  * in powers of two. Use a macro, so the compiler will output a
198  * warning if the value overflows!
199  *
200  * Detailed description:
201  *
202  * Create a variable with 1's at the positions of the leading 0's
203  * starting at the least significant bit, producing 0 if none (e.g.,
204  * 01011000 -> 0000 0111). Then these two variables are bitwise AND'ed
205  * together, to produce the greatest common power of two minus one. In
206  * the end add one to flip the value to the actual power of two (e.g.,
207  * 0000 0111 + 1 -> 0000 1000).
208  */
209 #define	__common_powers_of_two(a, b) \
210 	((~(a) & ((a) - 1) & ~(b) & ((b) - 1)) + 1)
211 
212 /*
213  * Scaling functions for signed and unsigned 64-bit time assuming
214  * reducable 64-bit fractions to 32-bit fractions:
215  */
216 
217 static __inline int64_t
__stime64_scale64_ceil(int64_t x,int64_t factor,int64_t divisor)218 __stime64_scale64_ceil(int64_t x, int64_t factor, int64_t divisor)
219 {
220 	const int64_t gcd = __common_powers_of_two(factor, divisor);
221 
222 	return (__stime64_scale32_ceil(x, factor / gcd, divisor / gcd));
223 }
224 
225 static __inline int64_t
__stime64_scale64_floor(int64_t x,int64_t factor,int64_t divisor)226 __stime64_scale64_floor(int64_t x, int64_t factor, int64_t divisor)
227 {
228 	const int64_t gcd = __common_powers_of_two(factor, divisor);
229 
230 	return (__stime64_scale32_floor(x, factor / gcd, divisor / gcd));
231 }
232 
233 static __inline uint64_t
__utime64_scale64_ceil(uint64_t x,uint64_t factor,uint64_t divisor)234 __utime64_scale64_ceil(uint64_t x, uint64_t factor, uint64_t divisor)
235 {
236 	const uint64_t gcd = __common_powers_of_two(factor, divisor);
237 
238 	return (__utime64_scale32_ceil(x, factor / gcd, divisor / gcd));
239 }
240 
241 static __inline uint64_t
__utime64_scale64_floor(uint64_t x,uint64_t factor,uint64_t divisor)242 __utime64_scale64_floor(uint64_t x, uint64_t factor, uint64_t divisor)
243 {
244 	const uint64_t gcd = __common_powers_of_two(factor, divisor);
245 
246 	return (__utime64_scale32_floor(x, factor / gcd, divisor / gcd));
247 }
248 
249 /*
250  * Decimal<->sbt conversions. Multiplying or dividing by SBT_1NS
251  * results in large roundoff errors which sbttons() and nstosbt()
252  * avoid. Millisecond and microsecond functions are also provided for
253  * completeness.
254  *
255  * When converting from sbt to another unit, the result is always
256  * rounded down. When converting back to sbt the result is always
257  * rounded up. This gives the property that sbttoX(Xtosbt(y)) == y .
258  *
259  * The conversion functions can also handle negative values.
260  */
261 #define	SBT_DECLARE_CONVERSION_PAIR(name, units_per_second)	\
262 static __inline int64_t \
263 sbtto##name(sbintime_t sbt) \
264 { \
265 	return (__stime64_scale64_floor(sbt, units_per_second, SBT_1S)); \
266 } \
267 static __inline sbintime_t \
268 name##tosbt(int64_t name) \
269 { \
270 	return (__stime64_scale64_ceil(name, SBT_1S, units_per_second)); \
271 }
272 
273 SBT_DECLARE_CONVERSION_PAIR(ns, 1000000000)
274 SBT_DECLARE_CONVERSION_PAIR(us, 1000000)
275 SBT_DECLARE_CONVERSION_PAIR(ms, 1000)
276 
277 /*-
278  * Background information:
279  *
280  * When converting between timestamps on parallel timescales of differing
281  * resolutions it is historical and scientific practice to round down rather
282  * than doing 4/5 rounding.
283  *
284  *   The date changes at midnight, not at noon.
285  *
286  *   Even at 15:59:59.999999999 it's not four'o'clock.
287  *
288  *   time_second ticks after N.999999999 not after N.4999999999
289  */
290 
291 static __inline void
bintime2timespec(const struct bintime * _bt,struct timespec * _ts)292 bintime2timespec(const struct bintime *_bt, struct timespec *_ts)
293 {
294 
295 	_ts->tv_sec = _bt->sec;
296 	_ts->tv_nsec = __utime64_scale64_floor(
297 	    _bt->frac, 1000000000, 1ULL << 32) >> 32;
298 }
299 
300 static __inline uint64_t
bintime2ns(const struct bintime * _bt)301 bintime2ns(const struct bintime *_bt)
302 {
303 	uint64_t ret;
304 
305 	ret = (uint64_t)(_bt->sec) * (uint64_t)1000000000;
306 	ret += __utime64_scale64_floor(
307 	    _bt->frac, 1000000000, 1ULL << 32) >> 32;
308 	return (ret);
309 }
310 
311 static __inline void
timespec2bintime(const struct timespec * _ts,struct bintime * _bt)312 timespec2bintime(const struct timespec *_ts, struct bintime *_bt)
313 {
314 
315 	_bt->sec = _ts->tv_sec;
316 	_bt->frac = __utime64_scale64_floor(
317 	    (uint64_t)_ts->tv_nsec << 32, 1ULL << 32, 1000000000);
318 }
319 
320 static __inline void
bintime2timeval(const struct bintime * _bt,struct timeval * _tv)321 bintime2timeval(const struct bintime *_bt, struct timeval *_tv)
322 {
323 
324 	_tv->tv_sec = _bt->sec;
325 	_tv->tv_usec = __utime64_scale64_floor(
326 	    _bt->frac, 1000000, 1ULL << 32) >> 32;
327 }
328 
329 static __inline void
timeval2bintime(const struct timeval * _tv,struct bintime * _bt)330 timeval2bintime(const struct timeval *_tv, struct bintime *_bt)
331 {
332 
333 	_bt->sec = _tv->tv_sec;
334 	_bt->frac = __utime64_scale64_floor(
335 	    (uint64_t)_tv->tv_usec << 32, 1ULL << 32, 1000000);
336 }
337 
338 static __inline struct timespec
sbttots(sbintime_t _sbt)339 sbttots(sbintime_t _sbt)
340 {
341 	struct timespec _ts;
342 
343 	_ts.tv_sec = _sbt >> 32;
344 	_ts.tv_nsec = sbttons((uint32_t)_sbt);
345 	return (_ts);
346 }
347 
348 static __inline sbintime_t
tstosbt(struct timespec _ts)349 tstosbt(struct timespec _ts)
350 {
351 
352 	return (((sbintime_t)_ts.tv_sec << 32) + nstosbt(_ts.tv_nsec));
353 }
354 
355 static __inline sbintime_t
tstosbt_sat(struct timespec _ts)356 tstosbt_sat(struct timespec _ts)
357 {
358 #ifndef __i386__
359 	if (_ts.tv_sec > SBT_MAX >> 32)
360 		return (SBT_MAX);
361 	if (_ts.tv_sec < -(SBT_MAX >> 32) - 1)
362 		return (-SBT_MAX - 1);
363 #endif
364 	return (tstosbt(_ts));
365 }
366 
367 static __inline struct timeval
sbttotv(sbintime_t _sbt)368 sbttotv(sbintime_t _sbt)
369 {
370 	struct timeval _tv;
371 
372 	_tv.tv_sec = _sbt >> 32;
373 	_tv.tv_usec = sbttous((uint32_t)_sbt);
374 	return (_tv);
375 }
376 
377 static __inline sbintime_t
tvtosbt(struct timeval _tv)378 tvtosbt(struct timeval _tv)
379 {
380 
381 	return (((sbintime_t)_tv.tv_sec << 32) + ustosbt(_tv.tv_usec));
382 }
383 
384 static __inline sbintime_t
tvtosbt_sat(struct timeval _tv)385 tvtosbt_sat(struct timeval _tv)
386 {
387 #ifndef __i386__
388 	if (_tv.tv_sec > SBT_MAX >> 32)
389 		return (SBT_MAX);
390 	if (_tv.tv_sec < -(SBT_MAX >> 32) - 1)
391 		return (-SBT_MAX - 1);
392 #endif
393 	return (tvtosbt(_tv));
394 }
395 
396 #endif /* __BSD_VISIBLE */
397 
398 #ifdef _KERNEL
399 /*
400  * Simple macros to convert ticks to milliseconds
401  * or microseconds and vice-versa. The answer
402  * will always be at least 1. Note the return
403  * value is a uint32_t however we step up the
404  * operations to 64 bit to avoid any overflow/underflow
405  * problems.
406  */
407 #define TICKS_2_MSEC(t) max(1, (uint32_t)(hz == 1000) ? \
408 	  (t) : (((uint64_t)(t) * (uint64_t)1000)/(uint64_t)hz))
409 #define TICKS_2_USEC(t) max(1, (uint32_t)(hz == 1000) ? \
410 	  ((t) * 1000) : (((uint64_t)(t) * (uint64_t)1000000)/(uint64_t)hz))
411 #define MSEC_2_TICKS(m) max(1, (uint32_t)((hz == 1000) ? \
412 	  (m) : ((uint64_t)(m) * (uint64_t)hz)/(uint64_t)1000))
413 #define USEC_2_TICKS(u) max(1, (uint32_t)((hz == 1000) ? \
414 	 ((u) / 1000) : ((uint64_t)(u) * (uint64_t)hz)/(uint64_t)1000000))
415 
416 #endif
417 /* Operations on timespecs */
418 #define	timespecclear(tvp)	((tvp)->tv_sec = (tvp)->tv_nsec = 0)
419 #define	timespecisset(tvp)	((tvp)->tv_sec || (tvp)->tv_nsec)
420 #define	timespeccmp(tvp, uvp, cmp)					\
421 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
422 	    ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :			\
423 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
424 
425 #define	timespecadd(tsp, usp, vsp)					\
426 	do {								\
427 		(vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec;		\
428 		(vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec;	\
429 		if ((vsp)->tv_nsec >= 1000000000L) {			\
430 			(vsp)->tv_sec++;				\
431 			(vsp)->tv_nsec -= 1000000000L;			\
432 		}							\
433 	} while (0)
434 #define	timespecsub(tsp, usp, vsp)					\
435 	do {								\
436 		(vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;		\
437 		(vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;	\
438 		if ((vsp)->tv_nsec < 0) {				\
439 			(vsp)->tv_sec--;				\
440 			(vsp)->tv_nsec += 1000000000L;			\
441 		}							\
442 	} while (0)
443 #define	timespecvalid_interval(tsp)	((tsp)->tv_sec >= 0 &&		\
444 	    (tsp)->tv_nsec >= 0 && (tsp)->tv_nsec < 1000000000L)
445 
446 #ifdef _KERNEL
447 
448 /* Operations on timevals. */
449 
450 #define	timevalclear(tvp)		((tvp)->tv_sec = (tvp)->tv_usec = 0)
451 #define	timevalisset(tvp)		((tvp)->tv_sec || (tvp)->tv_usec)
452 #define	timevalcmp(tvp, uvp, cmp)					\
453 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
454 	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
455 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
456 
457 /* timevaladd and timevalsub are not inlined */
458 
459 #endif /* _KERNEL */
460 
461 #ifndef _KERNEL			/* NetBSD/OpenBSD compatible interfaces */
462 
463 #define	timerclear(tvp)		((tvp)->tv_sec = (tvp)->tv_usec = 0)
464 #define	timerisset(tvp)		((tvp)->tv_sec || (tvp)->tv_usec)
465 #define	timercmp(tvp, uvp, cmp)					\
466 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
467 	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
468 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
469 #define	timeradd(tvp, uvp, vvp)						\
470 	do {								\
471 		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
472 		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
473 		if ((vvp)->tv_usec >= 1000000) {			\
474 			(vvp)->tv_sec++;				\
475 			(vvp)->tv_usec -= 1000000;			\
476 		}							\
477 	} while (0)
478 #define	timersub(tvp, uvp, vvp)						\
479 	do {								\
480 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
481 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
482 		if ((vvp)->tv_usec < 0) {				\
483 			(vvp)->tv_sec--;				\
484 			(vvp)->tv_usec += 1000000;			\
485 		}							\
486 	} while (0)
487 #endif
488 
489 /*
490  * Names of the interval timers, and structure
491  * defining a timer setting.
492  */
493 #define	ITIMER_REAL	0
494 #define	ITIMER_VIRTUAL	1
495 #define	ITIMER_PROF	2
496 
497 struct itimerval {
498 	struct	timeval it_interval;	/* timer interval */
499 	struct	timeval it_value;	/* current value */
500 };
501 
502 /*
503  * Getkerninfo clock information structure
504  */
505 struct clockinfo {
506 	int	hz;		/* clock frequency */
507 	int	tick;		/* micro-seconds per hz tick */
508 	int	spare;
509 	int	stathz;		/* statistics clock frequency */
510 	int	profhz;		/* profiling clock frequency */
511 };
512 
513 #if __BSD_VISIBLE
514 #define	CPUCLOCK_WHICH_PID	0
515 #define	CPUCLOCK_WHICH_TID	1
516 #endif
517 
518 #if defined(_KERNEL) || defined(_STANDALONE)
519 
520 /*
521  * Kernel to clock driver interface.
522  */
523 void	inittodr(time_t base);
524 void	resettodr(void);
525 
526 extern volatile time_t	time_second;
527 extern volatile time_t	time_uptime;
528 extern struct bintime tc_tick_bt;
529 extern sbintime_t tc_tick_sbt;
530 extern time_t tick_seconds_max;
531 extern struct bintime tick_bt;
532 extern sbintime_t tick_sbt;
533 extern int tc_precexp;
534 extern int tc_timepercentage;
535 extern struct bintime bt_timethreshold;
536 extern struct bintime bt_tickthreshold;
537 extern sbintime_t sbt_timethreshold;
538 extern sbintime_t sbt_tickthreshold;
539 
540 extern volatile int rtc_generation;
541 
542 /*
543  * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
544  *
545  * Functions without the "get" prefix returns the best timestamp
546  * we can produce in the given format.
547  *
548  * "bin"   == struct bintime  == seconds + 64 bit fraction of seconds.
549  * "nano"  == struct timespec == seconds + nanoseconds.
550  * "micro" == struct timeval  == seconds + microseconds.
551  *
552  * Functions containing "up" returns time relative to boot and
553  * should be used for calculating time intervals.
554  *
555  * Functions without "up" returns UTC time.
556  *
557  * Functions with the "get" prefix returns a less precise result
558  * much faster than the functions without "get" prefix and should
559  * be used where a precision of 1/hz seconds is acceptable or where
560  * performance is priority. (NB: "precision", _not_ "resolution" !)
561  */
562 
563 void	binuptime(struct bintime *bt);
564 void	nanouptime(struct timespec *tsp);
565 void	microuptime(struct timeval *tvp);
566 
567 static __inline sbintime_t
sbinuptime(void)568 sbinuptime(void)
569 {
570 	struct bintime _bt;
571 
572 	binuptime(&_bt);
573 	return (bttosbt(_bt));
574 }
575 
576 void	bintime(struct bintime *bt);
577 void	nanotime(struct timespec *tsp);
578 void	microtime(struct timeval *tvp);
579 
580 void	getbinuptime(struct bintime *bt);
581 void	getnanouptime(struct timespec *tsp);
582 void	getmicrouptime(struct timeval *tvp);
583 
584 static __inline sbintime_t
getsbinuptime(void)585 getsbinuptime(void)
586 {
587 	struct bintime _bt;
588 
589 	getbinuptime(&_bt);
590 	return (bttosbt(_bt));
591 }
592 
593 void	getbintime(struct bintime *bt);
594 void	getnanotime(struct timespec *tsp);
595 void	getmicrotime(struct timeval *tvp);
596 
597 void	getboottime(struct timeval *boottime);
598 void	getboottimebin(struct bintime *boottimebin);
599 
600 /* Other functions */
601 int	itimerdecr(struct itimerval *itp, int usec);
602 int	itimerfix(struct timeval *tv);
603 int	eventratecheck(struct timeval *, int *, int);
604 #define	ppsratecheck(t, c, m) eventratecheck(t, c, m)
605 int	ratecheck(struct timeval *, const struct timeval *);
606 void	timevaladd(struct timeval *t1, const struct timeval *t2);
607 void	timevalsub(struct timeval *t1, const struct timeval *t2);
608 int	tvtohz(struct timeval *tv);
609 
610 /*
611  * The following HZ limits allow the tvtohz() function
612  * to only use integer computations.
613  */
614 #define	HZ_MAXIMUM (INT_MAX / (1000000 >> 6)) /* 137kHz */
615 #define	HZ_MINIMUM 8 /* hz */
616 
617 #define	TC_DEFAULTPERC		5
618 
619 #define	BT2FREQ(bt)                                                     \
620 	(((uint64_t)0x8000000000000000 + ((bt)->frac >> 2)) /           \
621 	    ((bt)->frac >> 1))
622 
623 #define	SBT2FREQ(sbt)	((SBT_1S + ((sbt) >> 1)) / (sbt))
624 
625 #define	FREQ2BT(freq, bt)                                               \
626 {									\
627 	(bt)->sec = 0;                                                  \
628 	(bt)->frac = ((uint64_t)0x8000000000000000  / (freq)) << 1;     \
629 }
630 
631 #define	TIMESEL(sbt, sbt2)						\
632 	(((sbt2) >= sbt_timethreshold) ?				\
633 	    ((*(sbt) = getsbinuptime()), 1) : ((*(sbt) = sbinuptime()), 0))
634 
635 #else /* !_KERNEL && !_STANDALONE */
636 #include <time.h>
637 
638 #include <sys/cdefs.h>
639 #ifndef _STANDALONE
640 #include <sys/select.h>
641 #endif
642 
643 __BEGIN_DECLS
644 int	setitimer(int, const struct itimerval *, struct itimerval *);
645 int	utimes(const char *, const struct timeval *);
646 
647 #if __BSD_VISIBLE
648 int	adjtime(const struct timeval *, struct timeval *);
649 int	clock_getcpuclockid2(id_t, int, clockid_t *);
650 int	futimes(int, const struct timeval *);
651 int	futimesat(int, const char *, const struct timeval [2]);
652 int	lutimes(const char *, const struct timeval *);
653 int	settimeofday(const struct timeval *, const struct timezone *);
654 #endif
655 
656 #if __XSI_VISIBLE
657 int	getitimer(int, struct itimerval *);
658 int	gettimeofday(struct timeval *, struct timezone *);
659 #endif
660 
661 __END_DECLS
662 
663 #endif /* !_KERNEL */
664 
665 #endif /* !_SYS_TIME_H_ */
666