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