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 uint64_t
bintime2us(const struct bintime * _bt)312 bintime2us(const struct bintime *_bt)
313 {
314 uint64_t ret;
315
316 ret = (uint64_t)(_bt->sec) * (uint64_t)1000000;
317 ret += __utime64_scale64_floor(
318 _bt->frac, 1000000, 1ULL << 32) >> 32;
319 return (ret);
320 }
321
322 static __inline void
timespec2bintime(const struct timespec * _ts,struct bintime * _bt)323 timespec2bintime(const struct timespec *_ts, struct bintime *_bt)
324 {
325
326 _bt->sec = _ts->tv_sec;
327 _bt->frac = __utime64_scale64_floor(
328 (uint64_t)_ts->tv_nsec << 32, 1ULL << 32, 1000000000);
329 }
330
331 static __inline void
bintime2timeval(const struct bintime * _bt,struct timeval * _tv)332 bintime2timeval(const struct bintime *_bt, struct timeval *_tv)
333 {
334
335 _tv->tv_sec = _bt->sec;
336 _tv->tv_usec = __utime64_scale64_floor(
337 _bt->frac, 1000000, 1ULL << 32) >> 32;
338 }
339
340 static __inline void
timeval2bintime(const struct timeval * _tv,struct bintime * _bt)341 timeval2bintime(const struct timeval *_tv, struct bintime *_bt)
342 {
343
344 _bt->sec = _tv->tv_sec;
345 _bt->frac = __utime64_scale64_floor(
346 (uint64_t)_tv->tv_usec << 32, 1ULL << 32, 1000000);
347 }
348
349 static __inline struct timespec
sbttots(sbintime_t _sbt)350 sbttots(sbintime_t _sbt)
351 {
352 struct timespec _ts;
353
354 _ts.tv_sec = _sbt >> 32;
355 _ts.tv_nsec = sbttons((uint32_t)_sbt);
356 return (_ts);
357 }
358
359 static __inline sbintime_t
tstosbt(struct timespec _ts)360 tstosbt(struct timespec _ts)
361 {
362
363 return (((sbintime_t)_ts.tv_sec << 32) + nstosbt(_ts.tv_nsec));
364 }
365
366 static __inline sbintime_t
tstosbt_sat(struct timespec _ts)367 tstosbt_sat(struct timespec _ts)
368 {
369 #if __SIZEOF_TIME_T > 4
370 if (_ts.tv_sec > SBT_MAX >> 32)
371 return (SBT_MAX);
372 if (_ts.tv_sec < -(SBT_MAX >> 32) - 1)
373 return (-SBT_MAX - 1);
374 #endif
375 return (tstosbt(_ts));
376 }
377
378 static __inline struct timeval
sbttotv(sbintime_t _sbt)379 sbttotv(sbintime_t _sbt)
380 {
381 struct timeval _tv;
382
383 _tv.tv_sec = _sbt >> 32;
384 _tv.tv_usec = sbttous((uint32_t)_sbt);
385 return (_tv);
386 }
387
388 static __inline sbintime_t
tvtosbt(struct timeval _tv)389 tvtosbt(struct timeval _tv)
390 {
391
392 return (((sbintime_t)_tv.tv_sec << 32) + ustosbt(_tv.tv_usec));
393 }
394
395 static __inline sbintime_t
tvtosbt_sat(struct timeval _tv)396 tvtosbt_sat(struct timeval _tv)
397 {
398 #if __SIZEOF_TIME_T > 4
399 if (_tv.tv_sec > SBT_MAX >> 32)
400 return (SBT_MAX);
401 if (_tv.tv_sec < -(SBT_MAX >> 32) - 1)
402 return (-SBT_MAX - 1);
403 #endif
404 return (tvtosbt(_tv));
405 }
406
407 #endif /* __BSD_VISIBLE */
408
409 #ifdef _KERNEL
410 /*
411 * Simple macros to convert ticks to milliseconds
412 * or microseconds and vice-versa. The answer
413 * will always be at least 1. Note the return
414 * value is a uint32_t however we step up the
415 * operations to 64 bit to avoid any overflow/underflow
416 * problems.
417 */
418 #define TICKS_2_MSEC(t) max(1, (uint32_t)(hz == 1000) ? \
419 (t) : (((uint64_t)(t) * (uint64_t)1000)/(uint64_t)hz))
420 #define TICKS_2_USEC(t) max(1, (uint32_t)(hz == 1000) ? \
421 ((t) * 1000) : (((uint64_t)(t) * (uint64_t)1000000)/(uint64_t)hz))
422 #define MSEC_2_TICKS(m) max(1, (uint32_t)((hz == 1000) ? \
423 (m) : ((uint64_t)(m) * (uint64_t)hz)/(uint64_t)1000))
424 #define USEC_2_TICKS(u) max(1, (uint32_t)((hz == 1000) ? \
425 ((u) / 1000) : ((uint64_t)(u) * (uint64_t)hz)/(uint64_t)1000000))
426
427 #endif
428 /* Operations on timespecs */
429 #define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
430 #define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec)
431 #define timespeccmp(tvp, uvp, cmp) \
432 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
433 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
434 ((tvp)->tv_sec cmp (uvp)->tv_sec))
435
436 #define timespecadd(tsp, usp, vsp) \
437 do { \
438 (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
439 (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
440 if ((vsp)->tv_nsec >= 1000000000L) { \
441 (vsp)->tv_sec++; \
442 (vsp)->tv_nsec -= 1000000000L; \
443 } \
444 } while (0)
445 #define timespecsub(tsp, usp, vsp) \
446 do { \
447 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
448 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
449 if ((vsp)->tv_nsec < 0) { \
450 (vsp)->tv_sec--; \
451 (vsp)->tv_nsec += 1000000000L; \
452 } \
453 } while (0)
454 #define timespecvalid_interval(tsp) ((tsp)->tv_sec >= 0 && \
455 (tsp)->tv_nsec >= 0 && (tsp)->tv_nsec < 1000000000L)
456
457 #ifdef _KERNEL
458
459 /* Operations on timevals. */
460
461 #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
462 #define timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
463 #define timevalcmp(tvp, uvp, cmp) \
464 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
465 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
466 ((tvp)->tv_sec cmp (uvp)->tv_sec))
467
468 /* timevaladd and timevalsub are not inlined */
469
470 #endif /* _KERNEL */
471
472 #ifndef _KERNEL /* NetBSD/OpenBSD compatible interfaces */
473
474 #define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
475 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
476 #define timercmp(tvp, uvp, cmp) \
477 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
478 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
479 ((tvp)->tv_sec cmp (uvp)->tv_sec))
480 #define timeradd(tvp, uvp, vvp) \
481 do { \
482 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
483 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
484 if ((vvp)->tv_usec >= 1000000) { \
485 (vvp)->tv_sec++; \
486 (vvp)->tv_usec -= 1000000; \
487 } \
488 } while (0)
489 #define timersub(tvp, uvp, vvp) \
490 do { \
491 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
492 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
493 if ((vvp)->tv_usec < 0) { \
494 (vvp)->tv_sec--; \
495 (vvp)->tv_usec += 1000000; \
496 } \
497 } while (0)
498 #endif
499
500 /*
501 * Names of the interval timers, and structure
502 * defining a timer setting.
503 */
504 #define ITIMER_REAL 0
505 #define ITIMER_VIRTUAL 1
506 #define ITIMER_PROF 2
507
508 struct itimerval {
509 struct timeval it_interval; /* timer interval */
510 struct timeval it_value; /* current value */
511 };
512
513 /*
514 * Getkerninfo clock information structure
515 */
516 struct clockinfo {
517 int hz; /* clock frequency */
518 int tick; /* micro-seconds per hz tick */
519 int spare;
520 int stathz; /* statistics clock frequency */
521 int profhz; /* profiling clock frequency */
522 };
523
524 #if __BSD_VISIBLE
525 #define CPUCLOCK_WHICH_PID 0
526 #define CPUCLOCK_WHICH_TID 1
527 #endif
528
529 #if defined(_KERNEL) || defined(_STANDALONE)
530
531 /*
532 * Kernel to clock driver interface.
533 */
534 void inittodr(time_t base);
535 void resettodr(void);
536
537 extern volatile time_t time_second;
538 extern volatile time_t time_uptime;
539 extern struct bintime tc_tick_bt;
540 extern sbintime_t tc_tick_sbt;
541 extern time_t tick_seconds_max;
542 extern struct bintime tick_bt;
543 extern sbintime_t tick_sbt;
544 extern int tc_precexp;
545 extern int tc_timepercentage;
546 extern struct bintime bt_timethreshold;
547 extern struct bintime bt_tickthreshold;
548 extern sbintime_t sbt_timethreshold;
549 extern sbintime_t sbt_tickthreshold;
550
551 extern volatile int rtc_generation;
552
553 /*
554 * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
555 *
556 * Functions without the "get" prefix returns the best timestamp
557 * we can produce in the given format.
558 *
559 * "bin" == struct bintime == seconds + 64 bit fraction of seconds.
560 * "nano" == struct timespec == seconds + nanoseconds.
561 * "micro" == struct timeval == seconds + microseconds.
562 *
563 * Functions containing "up" returns time relative to boot and
564 * should be used for calculating time intervals.
565 *
566 * Functions without "up" returns UTC time.
567 *
568 * Functions with the "get" prefix returns a less precise result
569 * much faster than the functions without "get" prefix and should
570 * be used where a precision of 1/hz seconds is acceptable or where
571 * performance is priority. (NB: "precision", _not_ "resolution" !)
572 */
573
574 void binuptime(struct bintime *bt);
575 void nanouptime(struct timespec *tsp);
576 void microuptime(struct timeval *tvp);
577
578 static __inline sbintime_t
sbinuptime(void)579 sbinuptime(void)
580 {
581 struct bintime _bt;
582
583 binuptime(&_bt);
584 return (bttosbt(_bt));
585 }
586
587 void bintime(struct bintime *bt);
588 void nanotime(struct timespec *tsp);
589 void microtime(struct timeval *tvp);
590
591 void getbinuptime(struct bintime *bt);
592 void getnanouptime(struct timespec *tsp);
593 void getmicrouptime(struct timeval *tvp);
594
595 static __inline sbintime_t
getsbinuptime(void)596 getsbinuptime(void)
597 {
598 struct bintime _bt;
599
600 getbinuptime(&_bt);
601 return (bttosbt(_bt));
602 }
603
604 void getbintime(struct bintime *bt);
605 void getnanotime(struct timespec *tsp);
606 void getmicrotime(struct timeval *tvp);
607
608 void getboottime(struct timeval *boottime);
609 void getboottimebin(struct bintime *boottimebin);
610
611 /* Other functions */
612 int itimerdecr(struct itimerval *itp, int usec);
613 int itimerfix(struct timeval *tv);
614 int eventratecheck(struct timeval *, int *, int);
615 #define ppsratecheck(t, c, m) eventratecheck(t, c, m)
616 int ratecheck(struct timeval *, const struct timeval *);
617 void timevaladd(struct timeval *t1, const struct timeval *t2);
618 void timevalsub(struct timeval *t1, const struct timeval *t2);
619 int tvtohz(struct timeval *tv);
620
621 /*
622 * The following HZ limits allow the tvtohz() function
623 * to only use integer computations.
624 */
625 #define HZ_MAXIMUM (INT_MAX / (1000000 >> 6)) /* 137kHz */
626 #define HZ_MINIMUM 8 /* hz */
627
628 #define TC_DEFAULTPERC 5
629
630 #define BT2FREQ(bt) \
631 (((uint64_t)0x8000000000000000 + ((bt)->frac >> 2)) / \
632 ((bt)->frac >> 1))
633
634 #define SBT2FREQ(sbt) ((SBT_1S + ((sbt) >> 1)) / (sbt))
635
636 #define FREQ2BT(freq, bt) \
637 { \
638 (bt)->sec = 0; \
639 (bt)->frac = ((uint64_t)0x8000000000000000 / (freq)) << 1; \
640 }
641
642 #define TIMESEL(sbt, sbt2) \
643 (((sbt2) >= sbt_timethreshold) ? \
644 ((*(sbt) = getsbinuptime()), 1) : ((*(sbt) = sbinuptime()), 0))
645
646 #else /* !_KERNEL && !_STANDALONE */
647 #include <time.h>
648
649 #include <sys/cdefs.h>
650 #ifndef _STANDALONE
651 #include <sys/select.h>
652 #endif
653
654 __BEGIN_DECLS
655 int setitimer(int, const struct itimerval *, struct itimerval *);
656 int utimes(const char *, const struct timeval *);
657
658 #if __BSD_VISIBLE
659 int adjtime(const struct timeval *, struct timeval *);
660 int clock_getcpuclockid2(id_t, int, clockid_t *);
661 int futimes(int, const struct timeval *);
662 int futimesat(int, const char *, const struct timeval [2]);
663 int lutimes(const char *, const struct timeval *);
664 int settimeofday(const struct timeval *, const struct timezone *);
665 #endif
666
667 #if __XSI_VISIBLE
668 int getitimer(int, struct itimerval *);
669 int gettimeofday(struct timeval *, struct timezone *);
670 #endif
671
672 __END_DECLS
673
674 #endif /* !_KERNEL */
675
676 #endif /* !_SYS_TIME_H_ */
677