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>
43 int tz_minuteswest; /* minutes west of Greenwich */
44 int tz_dsttime; /* type of dst correction */
46 #define DST_NONE 0 /* not on dst */
47 #define DST_USA 1 /* USA style dst */
48 #define DST_AUST 2 /* Australian style dst */
49 #define DST_WET 3 /* Western European dst */
50 #define DST_MET 4 /* Middle European dst */
51 #define DST_EET 5 /* Eastern European dst */
52 #define DST_CAN 6 /* Canada */
61 bintime_addx(struct bintime *_bt, uint64_t _x)
72 bintime_add(struct bintime *_bt, const struct bintime *_bt2)
77 _bt->frac += _bt2->frac;
80 _bt->sec += _bt2->sec;
84 bintime_sub(struct bintime *_bt, const struct bintime *_bt2)
89 _bt->frac -= _bt2->frac;
92 _bt->sec -= _bt2->sec;
96 bintime_mul(struct bintime *_bt, u_int _x)
100 _p1 = (_bt->frac & 0xffffffffull) * _x;
101 _p2 = (_bt->frac >> 32) * _x + (_p1 >> 32);
103 _bt->sec += (_p2 >> 32);
104 _bt->frac = (_p2 << 32) | (_p1 & 0xffffffffull);
108 bintime_shift(struct bintime *_bt, int _exp)
113 _bt->sec |= _bt->frac >> (64 - _exp);
115 } else if (_exp < 0) {
117 _bt->frac |= (uint64_t)_bt->sec << (64 + _exp);
122 #define bintime_clear(a) ((a)->sec = (a)->frac = 0)
123 #define bintime_isset(a) ((a)->sec || (a)->frac)
124 #define bintime_cmp(a, b, cmp) \
125 (((a)->sec == (b)->sec) ? \
126 ((a)->frac cmp (b)->frac) : \
127 ((a)->sec cmp (b)->sec))
129 #define SBT_1S ((sbintime_t)1 << 32)
130 #define SBT_1M (SBT_1S * 60)
131 #define SBT_1MS (SBT_1S / 1000)
132 #define SBT_1US (SBT_1S / 1000000)
133 #define SBT_1NS (SBT_1S / 1000000000) /* beware rounding, see nstosbt() */
134 #define SBT_MAX 0x7fffffffffffffffLL
137 sbintime_getsec(sbintime_t _sbt)
143 static __inline sbintime_t
144 bttosbt(const struct bintime _bt)
147 return (((sbintime_t)_bt.sec << 32) + (_bt.frac >> 32));
150 static __inline struct bintime
151 sbttobt(sbintime_t _sbt)
155 _bt.sec = _sbt >> 32;
156 _bt.frac = _sbt << 32;
161 * Decimal<->sbt conversions. Multiplying or dividing by SBT_1NS results in
162 * large roundoff errors which sbttons() and nstosbt() avoid. Millisecond and
163 * microsecond functions are also provided for completeness.
165 static __inline int64_t
166 sbttons(sbintime_t _sbt)
169 return ((1000000000 * _sbt) >> 32);
172 static __inline sbintime_t
176 return ((_ns * (((uint64_t)1 << 63) / 500000000)) >> 32);
179 static __inline int64_t
180 sbttous(sbintime_t _sbt)
183 return ((1000000 * _sbt) >> 32);
186 static __inline sbintime_t
190 return ((_us * (((uint64_t)1 << 63) / 500000)) >> 32);
193 static __inline int64_t
194 sbttoms(sbintime_t _sbt)
197 return ((1000 * _sbt) >> 32);
200 static __inline sbintime_t
204 return ((_ms * (((uint64_t)1 << 63) / 500)) >> 32);
208 * Background information:
210 * When converting between timestamps on parallel timescales of differing
211 * resolutions it is historical and scientific practice to round down rather
212 * than doing 4/5 rounding.
214 * The date changes at midnight, not at noon.
216 * Even at 15:59:59.999999999 it's not four'o'clock.
218 * time_second ticks after N.999999999 not after N.4999999999
222 bintime2timespec(const struct bintime *_bt, struct timespec *_ts)
225 _ts->tv_sec = _bt->sec;
226 _ts->tv_nsec = ((uint64_t)1000000000 *
227 (uint32_t)(_bt->frac >> 32)) >> 32;
231 timespec2bintime(const struct timespec *_ts, struct bintime *_bt)
234 _bt->sec = _ts->tv_sec;
235 /* 18446744073 = int(2^64 / 1000000000) */
236 _bt->frac = _ts->tv_nsec * (uint64_t)18446744073LL;
240 bintime2timeval(const struct bintime *_bt, struct timeval *_tv)
243 _tv->tv_sec = _bt->sec;
244 _tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(_bt->frac >> 32)) >> 32;
248 timeval2bintime(const struct timeval *_tv, struct bintime *_bt)
251 _bt->sec = _tv->tv_sec;
252 /* 18446744073709 = int(2^64 / 1000000) */
253 _bt->frac = _tv->tv_usec * (uint64_t)18446744073709LL;
256 static __inline struct timespec
257 sbttots(sbintime_t _sbt)
261 _ts.tv_sec = _sbt >> 32;
262 _ts.tv_nsec = sbttons((uint32_t)_sbt);
266 static __inline sbintime_t
267 tstosbt(struct timespec _ts)
270 return (((sbintime_t)_ts.tv_sec << 32) + nstosbt(_ts.tv_nsec));
273 static __inline struct timeval
274 sbttotv(sbintime_t _sbt)
278 _tv.tv_sec = _sbt >> 32;
279 _tv.tv_usec = sbttous((uint32_t)_sbt);
283 static __inline sbintime_t
284 tvtosbt(struct timeval _tv)
287 return (((sbintime_t)_tv.tv_sec << 32) + ustosbt(_tv.tv_usec));
289 #endif /* __BSD_VISIBLE */
293 * Simple macros to convert ticks to milliseconds
294 * or microseconds and vice-versa. The answer
295 * will always be at least 1. Note the return
296 * value is a uint32_t however we step up the
297 * operations to 64 bit to avoid any overflow/underflow
300 #define TICKS_2_MSEC(t) max(1, (uint32_t)(hz == 1000) ? \
301 (t) : (((uint64_t)(t) * (uint64_t)1000)/(uint64_t)hz))
302 #define TICKS_2_USEC(t) max(1, (uint32_t)(hz == 1000) ? \
303 ((t) * 1000) : (((uint64_t)(t) * (uint64_t)1000000)/(uint64_t)hz))
304 #define MSEC_2_TICKS(m) max(1, (uint32_t)((hz == 1000) ? \
305 (m) : ((uint64_t)(m) * (uint64_t)hz)/(uint64_t)1000))
306 #define USEC_2_TICKS(u) max(1, (uint32_t)((hz == 1000) ? \
307 ((u) / 1000) : ((uint64_t)(u) * (uint64_t)hz)/(uint64_t)1000000))
309 /* Operations on timespecs */
310 #define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
311 #define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec)
312 #define timespeccmp(tvp, uvp, cmp) \
313 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
314 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
315 ((tvp)->tv_sec cmp (uvp)->tv_sec))
316 #define timespecadd(vvp, uvp) \
318 (vvp)->tv_sec += (uvp)->tv_sec; \
319 (vvp)->tv_nsec += (uvp)->tv_nsec; \
320 if ((vvp)->tv_nsec >= 1000000000) { \
322 (vvp)->tv_nsec -= 1000000000; \
325 #define timespecsub(vvp, uvp) \
327 (vvp)->tv_sec -= (uvp)->tv_sec; \
328 (vvp)->tv_nsec -= (uvp)->tv_nsec; \
329 if ((vvp)->tv_nsec < 0) { \
331 (vvp)->tv_nsec += 1000000000; \
335 /* Operations on timevals. */
337 #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
338 #define timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
339 #define timevalcmp(tvp, uvp, cmp) \
340 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
341 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
342 ((tvp)->tv_sec cmp (uvp)->tv_sec))
344 /* timevaladd and timevalsub are not inlined */
348 #ifndef _KERNEL /* NetBSD/OpenBSD compatible interfaces */
350 #define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
351 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
352 #define timercmp(tvp, uvp, cmp) \
353 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
354 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
355 ((tvp)->tv_sec cmp (uvp)->tv_sec))
356 #define timeradd(tvp, uvp, vvp) \
358 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
359 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
360 if ((vvp)->tv_usec >= 1000000) { \
362 (vvp)->tv_usec -= 1000000; \
365 #define timersub(tvp, uvp, vvp) \
367 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
368 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
369 if ((vvp)->tv_usec < 0) { \
371 (vvp)->tv_usec += 1000000; \
377 * Names of the interval timers, and structure
378 * defining a timer setting.
380 #define ITIMER_REAL 0
381 #define ITIMER_VIRTUAL 1
382 #define ITIMER_PROF 2
385 struct timeval it_interval; /* timer interval */
386 struct timeval it_value; /* current value */
390 * Getkerninfo clock information structure
393 int hz; /* clock frequency */
394 int tick; /* micro-seconds per hz tick */
396 int stathz; /* statistics clock frequency */
397 int profhz; /* profiling clock frequency */
400 /* These macros are also in time.h. */
401 #ifndef CLOCK_REALTIME
402 #define CLOCK_REALTIME 0
403 #define CLOCK_VIRTUAL 1
405 #define CLOCK_MONOTONIC 4
406 #define CLOCK_UPTIME 5 /* FreeBSD-specific. */
407 #define CLOCK_UPTIME_PRECISE 7 /* FreeBSD-specific. */
408 #define CLOCK_UPTIME_FAST 8 /* FreeBSD-specific. */
409 #define CLOCK_REALTIME_PRECISE 9 /* FreeBSD-specific. */
410 #define CLOCK_REALTIME_FAST 10 /* FreeBSD-specific. */
411 #define CLOCK_MONOTONIC_PRECISE 11 /* FreeBSD-specific. */
412 #define CLOCK_MONOTONIC_FAST 12 /* FreeBSD-specific. */
413 #define CLOCK_SECOND 13 /* FreeBSD-specific. */
414 #define CLOCK_THREAD_CPUTIME_ID 14
415 #define CLOCK_PROCESS_CPUTIME_ID 15
418 #ifndef TIMER_ABSTIME
419 #define TIMER_RELTIME 0x0 /* relative timer */
420 #define TIMER_ABSTIME 0x1 /* absolute timer */
424 #define CPUCLOCK_WHICH_PID 0
425 #define CPUCLOCK_WHICH_TID 1
431 * Kernel to clock driver interface.
433 void inittodr(time_t base);
434 void resettodr(void);
436 extern volatile time_t time_second;
437 extern volatile time_t time_uptime;
438 extern struct bintime tc_tick_bt;
439 extern sbintime_t tc_tick_sbt;
440 extern struct bintime tick_bt;
441 extern sbintime_t tick_sbt;
442 extern int tc_precexp;
443 extern int tc_timepercentage;
444 extern struct bintime bt_timethreshold;
445 extern struct bintime bt_tickthreshold;
446 extern sbintime_t sbt_timethreshold;
447 extern sbintime_t sbt_tickthreshold;
449 extern volatile int rtc_generation;
452 * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
454 * Functions without the "get" prefix returns the best timestamp
455 * we can produce in the given format.
457 * "bin" == struct bintime == seconds + 64 bit fraction of seconds.
458 * "nano" == struct timespec == seconds + nanoseconds.
459 * "micro" == struct timeval == seconds + microseconds.
461 * Functions containing "up" returns time relative to boot and
462 * should be used for calculating time intervals.
464 * Functions without "up" returns UTC time.
466 * Functions with the "get" prefix returns a less precise result
467 * much faster than the functions without "get" prefix and should
468 * be used where a precision of 1/hz seconds is acceptable or where
469 * performance is priority. (NB: "precision", _not_ "resolution" !)
472 void binuptime(struct bintime *bt);
473 void nanouptime(struct timespec *tsp);
474 void microuptime(struct timeval *tvp);
476 static __inline sbintime_t
482 return (bttosbt(_bt));
485 void bintime(struct bintime *bt);
486 void nanotime(struct timespec *tsp);
487 void microtime(struct timeval *tvp);
489 void getbinuptime(struct bintime *bt);
490 void getnanouptime(struct timespec *tsp);
491 void getmicrouptime(struct timeval *tvp);
493 static __inline sbintime_t
499 return (bttosbt(_bt));
502 void getbintime(struct bintime *bt);
503 void getnanotime(struct timespec *tsp);
504 void getmicrotime(struct timeval *tvp);
506 void getboottime(struct timeval *boottime);
507 void getboottimebin(struct bintime *boottimebin);
509 /* Other functions */
510 int itimerdecr(struct itimerval *itp, int usec);
511 int itimerfix(struct timeval *tv);
512 int ppsratecheck(struct timeval *, int *, int);
513 int ratecheck(struct timeval *, const struct timeval *);
514 void timevaladd(struct timeval *t1, const struct timeval *t2);
515 void timevalsub(struct timeval *t1, const struct timeval *t2);
516 int tvtohz(struct timeval *tv);
518 #define TC_DEFAULTPERC 5
520 #define BT2FREQ(bt) \
521 (((uint64_t)0x8000000000000000 + ((bt)->frac >> 2)) / \
524 #define SBT2FREQ(sbt) ((SBT_1S + ((sbt) >> 1)) / (sbt))
526 #define FREQ2BT(freq, bt) \
529 (bt)->frac = ((uint64_t)0x8000000000000000 / (freq)) << 1; \
532 #define TIMESEL(sbt, sbt2) \
533 (((sbt2) >= sbt_timethreshold) ? \
534 ((*(sbt) = getsbinuptime()), 1) : ((*(sbt) = sbinuptime()), 0))
539 #include <sys/cdefs.h>
540 #include <sys/select.h>
543 int setitimer(int, const struct itimerval *, struct itimerval *);
544 int utimes(const char *, const struct timeval *);
547 int adjtime(const struct timeval *, struct timeval *);
548 int clock_getcpuclockid2(id_t, int, clockid_t *);
549 int futimes(int, const struct timeval *);
550 int futimesat(int, const char *, const struct timeval [2]);
551 int lutimes(const char *, const struct timeval *);
552 int settimeofday(const struct timeval *, const struct timezone *);
556 int getitimer(int, struct itimerval *);
557 int gettimeofday(struct timeval *, struct timezone *);
562 #endif /* !_KERNEL */
564 #endif /* !_SYS_TIME_H_ */