]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/sys/time.h
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / sys / sys / time.h
1 /*-
2  * Copyright (c) 1982, 1986, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)time.h      8.5 (Berkeley) 5/4/95
30  * $FreeBSD$
31  */
32
33 #ifndef _SYS_TIME_H_
34 #define _SYS_TIME_H_
35
36 #include <sys/_timeval.h>
37 #include <sys/types.h>
38 #include <sys/timespec.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
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
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
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
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
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)
132
133 static __inline int
134 sbintime_getsec(sbintime_t _sbt)
135 {
136
137         return (_sbt >> 32);
138 }
139
140 static __inline sbintime_t
141 bttosbt(const struct bintime _bt)
142 {
143
144         return (((sbintime_t)_bt.sec << 32) + (_bt.frac >> 32));
145 }
146
147 static __inline struct bintime
148 sbttobt(sbintime_t _sbt)
149 {
150         struct bintime _bt;
151
152         _bt.sec = _sbt >> 32;
153         _bt.frac = _sbt << 32;
154         return (_bt);
155 }
156
157 /*-
158  * Background information:
159  *
160  * When converting between timestamps on parallel timescales of differing
161  * resolutions it is historical and scientific practice to round down rather
162  * than doing 4/5 rounding.
163  *
164  *   The date changes at midnight, not at noon.
165  *
166  *   Even at 15:59:59.999999999 it's not four'o'clock.
167  *
168  *   time_second ticks after N.999999999 not after N.4999999999
169  */
170
171 static __inline void
172 bintime2timespec(const struct bintime *_bt, struct timespec *_ts)
173 {
174
175         _ts->tv_sec = _bt->sec;
176         _ts->tv_nsec = ((uint64_t)1000000000 *
177             (uint32_t)(_bt->frac >> 32)) >> 32;
178 }
179
180 static __inline void
181 timespec2bintime(const struct timespec *_ts, struct bintime *_bt)
182 {
183
184         _bt->sec = _ts->tv_sec;
185         /* 18446744073 = int(2^64 / 1000000000) */
186         _bt->frac = _ts->tv_nsec * (uint64_t)18446744073LL;
187 }
188
189 static __inline void
190 bintime2timeval(const struct bintime *_bt, struct timeval *_tv)
191 {
192
193         _tv->tv_sec = _bt->sec;
194         _tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(_bt->frac >> 32)) >> 32;
195 }
196
197 static __inline void
198 timeval2bintime(const struct timeval *_tv, struct bintime *_bt)
199 {
200
201         _bt->sec = _tv->tv_sec;
202         /* 18446744073709 = int(2^64 / 1000000) */
203         _bt->frac = _tv->tv_usec * (uint64_t)18446744073709LL;
204 }
205
206 static __inline struct timespec
207 sbttots(sbintime_t _sbt)
208 {
209         struct timespec _ts;
210
211         _ts.tv_sec = _sbt >> 32;
212         _ts.tv_nsec = ((uint64_t)1000000000 * (uint32_t)_sbt) >> 32;
213         return (_ts);
214 }
215
216 static __inline sbintime_t
217 tstosbt(struct timespec _ts)
218 {
219
220         return (((sbintime_t)_ts.tv_sec << 32) +
221             (_ts.tv_nsec * (((uint64_t)1 << 63) / 500000000) >> 32));
222 }
223
224 static __inline struct timeval
225 sbttotv(sbintime_t _sbt)
226 {
227         struct timeval _tv;
228
229         _tv.tv_sec = _sbt >> 32;
230         _tv.tv_usec = ((uint64_t)1000000 * (uint32_t)_sbt) >> 32;
231         return (_tv);
232 }
233
234 static __inline sbintime_t
235 tvtosbt(struct timeval _tv)
236 {
237
238         return (((sbintime_t)_tv.tv_sec << 32) +
239             (_tv.tv_usec * (((uint64_t)1 << 63) / 500000) >> 32));
240 }
241 #endif /* __BSD_VISIBLE */
242
243 #ifdef _KERNEL
244
245 /* Operations on timespecs */
246 #define timespecclear(tvp)      ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
247 #define timespecisset(tvp)      ((tvp)->tv_sec || (tvp)->tv_nsec)
248 #define timespeccmp(tvp, uvp, cmp)                                      \
249         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
250             ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :                       \
251             ((tvp)->tv_sec cmp (uvp)->tv_sec))
252 #define timespecadd(vvp, uvp)                                           \
253         do {                                                            \
254                 (vvp)->tv_sec += (uvp)->tv_sec;                         \
255                 (vvp)->tv_nsec += (uvp)->tv_nsec;                       \
256                 if ((vvp)->tv_nsec >= 1000000000) {                     \
257                         (vvp)->tv_sec++;                                \
258                         (vvp)->tv_nsec -= 1000000000;                   \
259                 }                                                       \
260         } while (0)
261 #define timespecsub(vvp, uvp)                                           \
262         do {                                                            \
263                 (vvp)->tv_sec -= (uvp)->tv_sec;                         \
264                 (vvp)->tv_nsec -= (uvp)->tv_nsec;                       \
265                 if ((vvp)->tv_nsec < 0) {                               \
266                         (vvp)->tv_sec--;                                \
267                         (vvp)->tv_nsec += 1000000000;                   \
268                 }                                                       \
269         } while (0)
270
271 /* Operations on timevals. */
272
273 #define timevalclear(tvp)               ((tvp)->tv_sec = (tvp)->tv_usec = 0)
274 #define timevalisset(tvp)               ((tvp)->tv_sec || (tvp)->tv_usec)
275 #define timevalcmp(tvp, uvp, cmp)                                       \
276         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
277             ((tvp)->tv_usec cmp (uvp)->tv_usec) :                       \
278             ((tvp)->tv_sec cmp (uvp)->tv_sec))
279
280 /* timevaladd and timevalsub are not inlined */
281
282 #endif /* _KERNEL */
283
284 #ifndef _KERNEL                 /* NetBSD/OpenBSD compatible interfaces */
285
286 #define timerclear(tvp)         ((tvp)->tv_sec = (tvp)->tv_usec = 0)
287 #define timerisset(tvp)         ((tvp)->tv_sec || (tvp)->tv_usec)
288 #define timercmp(tvp, uvp, cmp)                                 \
289         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
290             ((tvp)->tv_usec cmp (uvp)->tv_usec) :                       \
291             ((tvp)->tv_sec cmp (uvp)->tv_sec))
292 #define timeradd(tvp, uvp, vvp)                                         \
293         do {                                                            \
294                 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;          \
295                 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;       \
296                 if ((vvp)->tv_usec >= 1000000) {                        \
297                         (vvp)->tv_sec++;                                \
298                         (vvp)->tv_usec -= 1000000;                      \
299                 }                                                       \
300         } while (0)
301 #define timersub(tvp, uvp, vvp)                                         \
302         do {                                                            \
303                 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;          \
304                 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;       \
305                 if ((vvp)->tv_usec < 0) {                               \
306                         (vvp)->tv_sec--;                                \
307                         (vvp)->tv_usec += 1000000;                      \
308                 }                                                       \
309         } while (0)
310 #endif
311
312 /*
313  * Names of the interval timers, and structure
314  * defining a timer setting.
315  */
316 #define ITIMER_REAL     0
317 #define ITIMER_VIRTUAL  1
318 #define ITIMER_PROF     2
319
320 struct itimerval {
321         struct  timeval it_interval;    /* timer interval */
322         struct  timeval it_value;       /* current value */
323 };
324
325 /*
326  * Getkerninfo clock information structure
327  */
328 struct clockinfo {
329         int     hz;             /* clock frequency */
330         int     tick;           /* micro-seconds per hz tick */
331         int     spare;
332         int     stathz;         /* statistics clock frequency */
333         int     profhz;         /* profiling clock frequency */
334 };
335
336 /* These macros are also in time.h. */
337 #ifndef CLOCK_REALTIME
338 #define CLOCK_REALTIME  0
339 #define CLOCK_VIRTUAL   1
340 #define CLOCK_PROF      2
341 #define CLOCK_MONOTONIC 4
342 #define CLOCK_UPTIME    5               /* FreeBSD-specific. */
343 #define CLOCK_UPTIME_PRECISE    7       /* FreeBSD-specific. */
344 #define CLOCK_UPTIME_FAST       8       /* FreeBSD-specific. */
345 #define CLOCK_REALTIME_PRECISE  9       /* FreeBSD-specific. */
346 #define CLOCK_REALTIME_FAST     10      /* FreeBSD-specific. */
347 #define CLOCK_MONOTONIC_PRECISE 11      /* FreeBSD-specific. */
348 #define CLOCK_MONOTONIC_FAST    12      /* FreeBSD-specific. */
349 #define CLOCK_SECOND    13              /* FreeBSD-specific. */
350 #define CLOCK_THREAD_CPUTIME_ID 14
351 #define CLOCK_PROCESS_CPUTIME_ID        15
352 #endif
353
354 #ifndef TIMER_ABSTIME
355 #define TIMER_RELTIME   0x0     /* relative timer */
356 #define TIMER_ABSTIME   0x1     /* absolute timer */
357 #endif
358
359 #if __BSD_VISIBLE
360 #define CPUCLOCK_WHICH_PID      0
361 #define CPUCLOCK_WHICH_TID      1
362 #endif
363
364 #ifdef _KERNEL
365
366 /*
367  * Kernel to clock driver interface.
368  */
369 void    inittodr(time_t base);
370 void    resettodr(void);
371
372 extern volatile time_t  time_second;
373 extern volatile time_t  time_uptime;
374 extern struct bintime boottimebin;
375 extern struct timeval boottime;
376 extern struct bintime tc_tick_bt;
377 extern sbintime_t tc_tick_sbt;
378 extern struct bintime tick_bt;
379 extern sbintime_t tick_sbt;
380 extern int tc_precexp;
381 extern int tc_timepercentage;
382 extern struct bintime bt_timethreshold;
383 extern struct bintime bt_tickthreshold;
384 extern sbintime_t sbt_timethreshold;
385 extern sbintime_t sbt_tickthreshold;
386
387 /*
388  * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
389  *
390  * Functions without the "get" prefix returns the best timestamp
391  * we can produce in the given format.
392  *
393  * "bin"   == struct bintime  == seconds + 64 bit fraction of seconds.
394  * "nano"  == struct timespec == seconds + nanoseconds.
395  * "micro" == struct timeval  == seconds + microseconds.
396  *
397  * Functions containing "up" returns time relative to boot and
398  * should be used for calculating time intervals.
399  *
400  * Functions without "up" returns GMT time.
401  *
402  * Functions with the "get" prefix returns a less precise result
403  * much faster than the functions without "get" prefix and should
404  * be used where a precision of 1/hz seconds is acceptable or where
405  * performance is priority. (NB: "precision", _not_ "resolution" !)
406  */
407
408 void    binuptime(struct bintime *bt);
409 void    nanouptime(struct timespec *tsp);
410 void    microuptime(struct timeval *tvp);
411
412 static __inline sbintime_t
413 sbinuptime(void)
414 {
415         struct bintime _bt;
416
417         binuptime(&_bt);
418         return (bttosbt(_bt));
419 }
420
421 void    bintime(struct bintime *bt);
422 void    nanotime(struct timespec *tsp);
423 void    microtime(struct timeval *tvp);
424
425 void    getbinuptime(struct bintime *bt);
426 void    getnanouptime(struct timespec *tsp);
427 void    getmicrouptime(struct timeval *tvp);
428
429 static __inline sbintime_t
430 getsbinuptime(void)
431 {
432         struct bintime _bt;
433
434         getbinuptime(&_bt);
435         return (bttosbt(_bt));
436 }
437
438 void    getbintime(struct bintime *bt);
439 void    getnanotime(struct timespec *tsp);
440 void    getmicrotime(struct timeval *tvp);
441
442 /* Other functions */
443 int     itimerdecr(struct itimerval *itp, int usec);
444 int     itimerfix(struct timeval *tv);
445 int     ppsratecheck(struct timeval *, int *, int);
446 int     ratecheck(struct timeval *, const struct timeval *);
447 void    timevaladd(struct timeval *t1, const struct timeval *t2);
448 void    timevalsub(struct timeval *t1, const struct timeval *t2);
449 int     tvtohz(struct timeval *tv);
450
451 #define TC_DEFAULTPERC          5
452
453 #define BT2FREQ(bt)                                                     \
454         (((uint64_t)0x8000000000000000 + ((bt)->frac >> 2)) /           \
455             ((bt)->frac >> 1))
456
457 #define SBT2FREQ(sbt)   ((SBT_1S + ((sbt) >> 1)) / (sbt))
458
459 #define FREQ2BT(freq, bt)                                               \
460 {                                                                       \
461         (bt)->sec = 0;                                                  \
462         (bt)->frac = ((uint64_t)0x8000000000000000  / (freq)) << 1;     \
463 }
464
465 #define TIMESEL(sbt, sbt2)                                              \
466         (((sbt2) >= sbt_timethreshold) ?                                \
467             ((*(sbt) = getsbinuptime()), 1) : ((*(sbt) = sbinuptime()), 0))
468
469 #else /* !_KERNEL */
470 #include <time.h>
471
472 #include <sys/cdefs.h>
473 #include <sys/select.h>
474
475 __BEGIN_DECLS
476 int     setitimer(int, const struct itimerval *, struct itimerval *);
477 int     utimes(const char *, const struct timeval *);
478
479 #if __BSD_VISIBLE
480 int     adjtime(const struct timeval *, struct timeval *);
481 int     clock_getcpuclockid2(id_t, int, clockid_t *);
482 int     futimes(int, const struct timeval *);
483 int     futimesat(int, const char *, const struct timeval [2]);
484 int     lutimes(const char *, const struct timeval *);
485 int     settimeofday(const struct timeval *, const struct timezone *);
486 #endif
487
488 #if __XSI_VISIBLE
489 int     getitimer(int, struct itimerval *);
490 int     gettimeofday(struct timeval *, struct timezone *);
491 #endif
492
493 __END_DECLS
494
495 #endif /* !_KERNEL */
496
497 #endif /* !_SYS_TIME_H_ */