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