2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 * @(#)time.h 8.5 (Berkeley) 5/4/95
38 #include <sys/_timeval.h>
39 #include <sys/types.h>
40 #include <sys/timespec.h>
41 #include <sys/_clock_id.h>
44 int tz_minuteswest; /* minutes west of Greenwich */
45 int tz_dsttime; /* type of dst correction */
47 #define DST_NONE 0 /* not on dst */
48 #define DST_USA 1 /* USA style dst */
49 #define DST_AUST 2 /* Australian style dst */
50 #define DST_WET 3 /* Western European dst */
51 #define DST_MET 4 /* Middle European dst */
52 #define DST_EET 5 /* Eastern European dst */
53 #define DST_CAN 6 /* Canada */
62 bintime_addx(struct bintime *_bt, uint64_t _x)
73 bintime_add(struct bintime *_bt, const struct bintime *_bt2)
78 _bt->frac += _bt2->frac;
81 _bt->sec += _bt2->sec;
85 bintime_sub(struct bintime *_bt, const struct bintime *_bt2)
90 _bt->frac -= _bt2->frac;
93 _bt->sec -= _bt2->sec;
97 bintime_mul(struct bintime *_bt, u_int _x)
101 _p1 = (_bt->frac & 0xffffffffull) * _x;
102 _p2 = (_bt->frac >> 32) * _x + (_p1 >> 32);
104 _bt->sec += (_p2 >> 32);
105 _bt->frac = (_p2 << 32) | (_p1 & 0xffffffffull);
109 bintime_shift(struct bintime *_bt, int _exp)
114 _bt->sec |= _bt->frac >> (64 - _exp);
116 } else if (_exp < 0) {
118 _bt->frac |= (uint64_t)_bt->sec << (64 + _exp);
123 #define bintime_clear(a) ((a)->sec = (a)->frac = 0)
124 #define bintime_isset(a) ((a)->sec || (a)->frac)
125 #define bintime_cmp(a, b, cmp) \
126 (((a)->sec == (b)->sec) ? \
127 ((a)->frac cmp (b)->frac) : \
128 ((a)->sec cmp (b)->sec))
130 #define SBT_1S ((sbintime_t)1 << 32)
131 #define SBT_1M (SBT_1S * 60)
132 #define SBT_1MS (SBT_1S / 1000)
133 #define SBT_1US (SBT_1S / 1000000)
134 #define SBT_1NS (SBT_1S / 1000000000) /* beware rounding, see nstosbt() */
135 #define SBT_MAX 0x7fffffffffffffffLL
138 sbintime_getsec(sbintime_t _sbt)
144 static __inline sbintime_t
145 bttosbt(const struct bintime _bt)
148 return (((sbintime_t)_bt.sec << 32) + (_bt.frac >> 32));
151 static __inline struct bintime
152 sbttobt(sbintime_t _sbt)
156 _bt.sec = _sbt >> 32;
157 _bt.frac = _sbt << 32;
162 * Scaling functions for signed and unsigned 64-bit time using any
166 static __inline int64_t
167 __stime64_scale32_ceil(int64_t x, int32_t factor, int32_t divisor)
169 const int64_t rem = x % divisor;
171 return (x / divisor * factor + (rem * factor + divisor - 1) / divisor);
174 static __inline int64_t
175 __stime64_scale32_floor(int64_t x, int32_t factor, int32_t divisor)
177 const int64_t rem = x % divisor;
179 return (x / divisor * factor + (rem * factor) / divisor);
182 static __inline uint64_t
183 __utime64_scale32_ceil(uint64_t x, uint32_t factor, uint32_t divisor)
185 const uint64_t rem = x % divisor;
187 return (x / divisor * factor + (rem * factor + divisor - 1) / divisor);
190 static __inline uint64_t
191 __utime64_scale32_floor(uint64_t x, uint32_t factor, uint32_t divisor)
193 const uint64_t rem = x % divisor;
195 return (x / divisor * factor + (rem * factor) / divisor);
199 * This function finds the common divisor between the two arguments,
200 * in powers of two. Use a macro, so the compiler will output a
201 * warning if the value overflows!
203 * Detailed description:
205 * Create a variable with 1's at the positions of the leading 0's
206 * starting at the least significant bit, producing 0 if none (e.g.,
207 * 01011000 -> 0000 0111). Then these two variables are bitwise AND'ed
208 * together, to produce the greatest common power of two minus one. In
209 * the end add one to flip the value to the actual power of two (e.g.,
210 * 0000 0111 + 1 -> 0000 1000).
212 #define __common_powers_of_two(a, b) \
213 ((~(a) & ((a) - 1) & ~(b) & ((b) - 1)) + 1)
216 * Scaling functions for signed and unsigned 64-bit time assuming
217 * reducable 64-bit fractions to 32-bit fractions:
220 static __inline int64_t
221 __stime64_scale64_ceil(int64_t x, int64_t factor, int64_t divisor)
223 const int64_t gcd = __common_powers_of_two(factor, divisor);
225 return (__stime64_scale32_ceil(x, factor / gcd, divisor / gcd));
228 static __inline int64_t
229 __stime64_scale64_floor(int64_t x, int64_t factor, int64_t divisor)
231 const int64_t gcd = __common_powers_of_two(factor, divisor);
233 return (__stime64_scale32_floor(x, factor / gcd, divisor / gcd));
236 static __inline uint64_t
237 __utime64_scale64_ceil(uint64_t x, uint64_t factor, uint64_t divisor)
239 const uint64_t gcd = __common_powers_of_two(factor, divisor);
241 return (__utime64_scale32_ceil(x, factor / gcd, divisor / gcd));
244 static __inline uint64_t
245 __utime64_scale64_floor(uint64_t x, uint64_t factor, uint64_t divisor)
247 const uint64_t gcd = __common_powers_of_two(factor, divisor);
249 return (__utime64_scale32_floor(x, factor / gcd, divisor / gcd));
253 * Decimal<->sbt conversions. Multiplying or dividing by SBT_1NS
254 * results in large roundoff errors which sbttons() and nstosbt()
255 * avoid. Millisecond and microsecond functions are also provided for
258 * When converting from sbt to another unit, the result is always
259 * rounded down. When converting back to sbt the result is always
260 * rounded up. This gives the property that sbttoX(Xtosbt(y)) == y .
262 * The conversion functions can also handle negative values.
264 #define SBT_DECLARE_CONVERSION_PAIR(name, units_per_second) \
265 static __inline int64_t \
266 sbtto##name(sbintime_t sbt) \
268 return (__stime64_scale64_floor(sbt, units_per_second, SBT_1S)); \
270 static __inline sbintime_t \
271 name##tosbt(int64_t name) \
273 return (__stime64_scale64_ceil(name, SBT_1S, units_per_second)); \
276 SBT_DECLARE_CONVERSION_PAIR(ns, 1000000000)
277 SBT_DECLARE_CONVERSION_PAIR(us, 1000000)
278 SBT_DECLARE_CONVERSION_PAIR(ms, 1000)
281 * Background information:
283 * When converting between timestamps on parallel timescales of differing
284 * resolutions it is historical and scientific practice to round down rather
285 * than doing 4/5 rounding.
287 * The date changes at midnight, not at noon.
289 * Even at 15:59:59.999999999 it's not four'o'clock.
291 * time_second ticks after N.999999999 not after N.4999999999
295 bintime2timespec(const struct bintime *_bt, struct timespec *_ts)
298 _ts->tv_sec = _bt->sec;
299 _ts->tv_nsec = __utime64_scale64_floor(
300 _bt->frac, 1000000000, 1ULL << 32) >> 32;
303 static __inline uint64_t
304 bintime2ns(const struct bintime *_bt)
308 ret = (uint64_t)(_bt->sec) * (uint64_t)1000000000;
309 ret += __utime64_scale64_floor(
310 _bt->frac, 1000000000, 1ULL << 32) >> 32;
315 timespec2bintime(const struct timespec *_ts, struct bintime *_bt)
318 _bt->sec = _ts->tv_sec;
319 _bt->frac = __utime64_scale64_floor(
320 (uint64_t)_ts->tv_nsec << 32, 1ULL << 32, 1000000000);
324 bintime2timeval(const struct bintime *_bt, struct timeval *_tv)
327 _tv->tv_sec = _bt->sec;
328 _tv->tv_usec = __utime64_scale64_floor(
329 _bt->frac, 1000000, 1ULL << 32) >> 32;
333 timeval2bintime(const struct timeval *_tv, struct bintime *_bt)
336 _bt->sec = _tv->tv_sec;
337 _bt->frac = __utime64_scale64_floor(
338 (uint64_t)_tv->tv_usec << 32, 1ULL << 32, 1000000);
341 static __inline struct timespec
342 sbttots(sbintime_t _sbt)
346 _ts.tv_sec = _sbt >> 32;
347 _ts.tv_nsec = sbttons((uint32_t)_sbt);
351 static __inline sbintime_t
352 tstosbt(struct timespec _ts)
355 return (((sbintime_t)_ts.tv_sec << 32) + nstosbt(_ts.tv_nsec));
358 static __inline struct timeval
359 sbttotv(sbintime_t _sbt)
363 _tv.tv_sec = _sbt >> 32;
364 _tv.tv_usec = sbttous((uint32_t)_sbt);
368 static __inline sbintime_t
369 tvtosbt(struct timeval _tv)
372 return (((sbintime_t)_tv.tv_sec << 32) + ustosbt(_tv.tv_usec));
374 #endif /* __BSD_VISIBLE */
378 * Simple macros to convert ticks to milliseconds
379 * or microseconds and vice-versa. The answer
380 * will always be at least 1. Note the return
381 * value is a uint32_t however we step up the
382 * operations to 64 bit to avoid any overflow/underflow
385 #define TICKS_2_MSEC(t) max(1, (uint32_t)(hz == 1000) ? \
386 (t) : (((uint64_t)(t) * (uint64_t)1000)/(uint64_t)hz))
387 #define TICKS_2_USEC(t) max(1, (uint32_t)(hz == 1000) ? \
388 ((t) * 1000) : (((uint64_t)(t) * (uint64_t)1000000)/(uint64_t)hz))
389 #define MSEC_2_TICKS(m) max(1, (uint32_t)((hz == 1000) ? \
390 (m) : ((uint64_t)(m) * (uint64_t)hz)/(uint64_t)1000))
391 #define USEC_2_TICKS(u) max(1, (uint32_t)((hz == 1000) ? \
392 ((u) / 1000) : ((uint64_t)(u) * (uint64_t)hz)/(uint64_t)1000000))
395 /* Operations on timespecs */
396 #define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
397 #define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec)
398 #define timespeccmp(tvp, uvp, cmp) \
399 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
400 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
401 ((tvp)->tv_sec cmp (uvp)->tv_sec))
403 #define timespecadd(tsp, usp, vsp) \
405 (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
406 (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
407 if ((vsp)->tv_nsec >= 1000000000L) { \
409 (vsp)->tv_nsec -= 1000000000L; \
412 #define timespecsub(tsp, usp, vsp) \
414 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
415 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
416 if ((vsp)->tv_nsec < 0) { \
418 (vsp)->tv_nsec += 1000000000L; \
421 #define timespecvalid_interval(tsp) ((tsp)->tv_sec >= 0 && \
422 (tsp)->tv_nsec >= 0 && (tsp)->tv_nsec < 1000000000L)
426 /* Operations on timevals. */
428 #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
429 #define timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
430 #define timevalcmp(tvp, uvp, cmp) \
431 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
432 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
433 ((tvp)->tv_sec cmp (uvp)->tv_sec))
435 /* timevaladd and timevalsub are not inlined */
439 #ifndef _KERNEL /* NetBSD/OpenBSD compatible interfaces */
441 #define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
442 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
443 #define timercmp(tvp, uvp, cmp) \
444 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
445 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
446 ((tvp)->tv_sec cmp (uvp)->tv_sec))
447 #define timeradd(tvp, uvp, vvp) \
449 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
450 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
451 if ((vvp)->tv_usec >= 1000000) { \
453 (vvp)->tv_usec -= 1000000; \
456 #define timersub(tvp, uvp, vvp) \
458 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
459 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
460 if ((vvp)->tv_usec < 0) { \
462 (vvp)->tv_usec += 1000000; \
468 * Names of the interval timers, and structure
469 * defining a timer setting.
471 #define ITIMER_REAL 0
472 #define ITIMER_VIRTUAL 1
473 #define ITIMER_PROF 2
476 struct timeval it_interval; /* timer interval */
477 struct timeval it_value; /* current value */
481 * Getkerninfo clock information structure
484 int hz; /* clock frequency */
485 int tick; /* micro-seconds per hz tick */
487 int stathz; /* statistics clock frequency */
488 int profhz; /* profiling clock frequency */
492 #define CPUCLOCK_WHICH_PID 0
493 #define CPUCLOCK_WHICH_TID 1
496 #if defined(_KERNEL) || defined(_STANDALONE)
499 * Kernel to clock driver interface.
501 void inittodr(time_t base);
502 void resettodr(void);
504 extern volatile time_t time_second;
505 extern volatile time_t time_uptime;
506 extern struct bintime tc_tick_bt;
507 extern sbintime_t tc_tick_sbt;
508 extern time_t tick_seconds_max;
509 extern struct bintime tick_bt;
510 extern sbintime_t tick_sbt;
511 extern int tc_precexp;
512 extern int tc_timepercentage;
513 extern struct bintime bt_timethreshold;
514 extern struct bintime bt_tickthreshold;
515 extern sbintime_t sbt_timethreshold;
516 extern sbintime_t sbt_tickthreshold;
518 extern volatile int rtc_generation;
521 * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
523 * Functions without the "get" prefix returns the best timestamp
524 * we can produce in the given format.
526 * "bin" == struct bintime == seconds + 64 bit fraction of seconds.
527 * "nano" == struct timespec == seconds + nanoseconds.
528 * "micro" == struct timeval == seconds + microseconds.
530 * Functions containing "up" returns time relative to boot and
531 * should be used for calculating time intervals.
533 * Functions without "up" returns UTC time.
535 * Functions with the "get" prefix returns a less precise result
536 * much faster than the functions without "get" prefix and should
537 * be used where a precision of 1/hz seconds is acceptable or where
538 * performance is priority. (NB: "precision", _not_ "resolution" !)
541 void binuptime(struct bintime *bt);
542 void nanouptime(struct timespec *tsp);
543 void microuptime(struct timeval *tvp);
545 static __inline sbintime_t
551 return (bttosbt(_bt));
554 void bintime(struct bintime *bt);
555 void nanotime(struct timespec *tsp);
556 void microtime(struct timeval *tvp);
558 void getbinuptime(struct bintime *bt);
559 void getnanouptime(struct timespec *tsp);
560 void getmicrouptime(struct timeval *tvp);
562 static __inline sbintime_t
568 return (bttosbt(_bt));
571 void getbintime(struct bintime *bt);
572 void getnanotime(struct timespec *tsp);
573 void getmicrotime(struct timeval *tvp);
575 void getboottime(struct timeval *boottime);
576 void getboottimebin(struct bintime *boottimebin);
578 /* Other functions */
579 int itimerdecr(struct itimerval *itp, int usec);
580 int itimerfix(struct timeval *tv);
581 int eventratecheck(struct timeval *, int *, int);
582 #define ppsratecheck(t, c, m) eventratecheck(t, c, m)
583 int ratecheck(struct timeval *, const struct timeval *);
584 void timevaladd(struct timeval *t1, const struct timeval *t2);
585 void timevalsub(struct timeval *t1, const struct timeval *t2);
586 int tvtohz(struct timeval *tv);
589 * The following HZ limits allow the tvtohz() function
590 * to only use integer computations.
592 #define HZ_MAXIMUM (INT_MAX / (1000000 >> 6)) /* 137kHz */
593 #define HZ_MINIMUM 8 /* hz */
595 #define TC_DEFAULTPERC 5
597 #define BT2FREQ(bt) \
598 (((uint64_t)0x8000000000000000 + ((bt)->frac >> 2)) / \
601 #define SBT2FREQ(sbt) ((SBT_1S + ((sbt) >> 1)) / (sbt))
603 #define FREQ2BT(freq, bt) \
606 (bt)->frac = ((uint64_t)0x8000000000000000 / (freq)) << 1; \
609 #define TIMESEL(sbt, sbt2) \
610 (((sbt2) >= sbt_timethreshold) ? \
611 ((*(sbt) = getsbinuptime()), 1) : ((*(sbt) = sbinuptime()), 0))
613 #else /* !_KERNEL && !_STANDALONE */
616 #include <sys/cdefs.h>
617 #include <sys/select.h>
620 int setitimer(int, const struct itimerval *, struct itimerval *);
621 int utimes(const char *, const struct timeval *);
624 int adjtime(const struct timeval *, struct timeval *);
625 int clock_getcpuclockid2(id_t, int, clockid_t *);
626 int futimes(int, const struct timeval *);
627 int futimesat(int, const char *, const struct timeval [2]);
628 int lutimes(const char *, const struct timeval *);
629 int settimeofday(const struct timeval *, const struct timezone *);
633 int getitimer(int, struct itimerval *);
634 int gettimeofday(struct timeval *, struct timezone *);
639 #endif /* !_KERNEL */
641 #endif /* !_SYS_TIME_H_ */