]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/time.h
MFC
[FreeBSD/FreeBSD.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 #define bintime_clear(a)        ((a)->sec = (a)->frac = 0)
106 #define bintime_isset(a)        ((a)->sec || (a)->frac)
107 #define bintime_cmp(a, b, cmp)                                          \
108         (((a)->sec == (b)->sec) ?                                       \
109             ((a)->frac cmp (b)->frac) :                                 \
110             ((a)->sec cmp (b)->sec))
111
112 #define SBT_1S  ((sbintime_t)1 << 32)
113 #define SBT_1M  (SBT_1S * 60)
114 #define SBT_1MS (SBT_1S / 1000)
115 #define SBT_1US (SBT_1S / 1000000)
116 #define SBT_1NS (SBT_1S / 1000000000)
117
118 static __inline int
119 sbintime_getsec(sbintime_t sbt)
120 {
121
122         return (sbt >> 32);
123 }
124
125 static __inline sbintime_t
126 bttosbt(const struct bintime bt)
127 {
128
129         return (((sbintime_t)bt.sec << 32) + (bt.frac >> 32));
130 }
131
132 static __inline struct bintime
133 sbttobt(sbintime_t sbt)
134 {
135         struct bintime bt;
136
137         bt.sec = sbt >> 32;
138         bt.frac = sbt << 32;
139         return (bt);
140 }
141
142 /*-
143  * Background information:
144  *
145  * When converting between timestamps on parallel timescales of differing
146  * resolutions it is historical and scientific practice to round down rather
147  * than doing 4/5 rounding.
148  *
149  *   The date changes at midnight, not at noon.
150  *
151  *   Even at 15:59:59.999999999 it's not four'o'clock.
152  *
153  *   time_second ticks after N.999999999 not after N.4999999999
154  */
155
156 static __inline void
157 bintime2timespec(const struct bintime *bt, struct timespec *ts)
158 {
159
160         ts->tv_sec = bt->sec;
161         ts->tv_nsec = ((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32;
162 }
163
164 static __inline void
165 timespec2bintime(const struct timespec *ts, struct bintime *bt)
166 {
167
168         bt->sec = ts->tv_sec;
169         /* 18446744073 = int(2^64 / 1000000000) */
170         bt->frac = ts->tv_nsec * (uint64_t)18446744073LL;
171 }
172
173 static __inline void
174 bintime2timeval(const struct bintime *bt, struct timeval *tv)
175 {
176
177         tv->tv_sec = bt->sec;
178         tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32;
179 }
180
181 static __inline void
182 timeval2bintime(const struct timeval *tv, struct bintime *bt)
183 {
184
185         bt->sec = tv->tv_sec;
186         /* 18446744073709 = int(2^64 / 1000000) */
187         bt->frac = tv->tv_usec * (uint64_t)18446744073709LL;
188 }
189
190 static __inline struct timespec
191 sbttots(sbintime_t sbt)
192 {
193         struct timespec ts;
194
195         ts.tv_sec = sbt >> 32;
196         ts.tv_nsec = ((uint64_t)1000000000 * (uint32_t)sbt) >> 32;
197         return (ts);
198 }
199
200 static __inline sbintime_t
201 tstosbt(struct timespec ts)
202 {
203
204         return (((sbintime_t)ts.tv_sec << 32) +
205             (ts.tv_nsec * (((uint64_t)1 << 63) / 500000000) >> 32));
206 }
207
208 static __inline struct timeval
209 sbttotv(sbintime_t sbt)
210 {
211         struct timeval tv;
212
213         tv.tv_sec = sbt >> 32;
214         tv.tv_usec = ((uint64_t)1000000 * (uint32_t)sbt) >> 32;
215         return (tv);
216 }
217
218 static __inline sbintime_t
219 tvtosbt(struct timeval tv)
220 {
221
222         return (((sbintime_t)tv.tv_sec << 32) +
223             (tv.tv_usec * (((uint64_t)1 << 63) / 500000) >> 32));
224 }
225 #endif /* __BSD_VISIBLE */
226
227 #ifdef _KERNEL
228
229 /* Operations on timespecs */
230 #define timespecclear(tvp)      ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
231 #define timespecisset(tvp)      ((tvp)->tv_sec || (tvp)->tv_nsec)
232 #define timespeccmp(tvp, uvp, cmp)                                      \
233         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
234             ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :                       \
235             ((tvp)->tv_sec cmp (uvp)->tv_sec))
236 #define timespecadd(vvp, uvp)                                           \
237         do {                                                            \
238                 (vvp)->tv_sec += (uvp)->tv_sec;                         \
239                 (vvp)->tv_nsec += (uvp)->tv_nsec;                       \
240                 if ((vvp)->tv_nsec >= 1000000000) {                     \
241                         (vvp)->tv_sec++;                                \
242                         (vvp)->tv_nsec -= 1000000000;                   \
243                 }                                                       \
244         } while (0)
245 #define timespecsub(vvp, uvp)                                           \
246         do {                                                            \
247                 (vvp)->tv_sec -= (uvp)->tv_sec;                         \
248                 (vvp)->tv_nsec -= (uvp)->tv_nsec;                       \
249                 if ((vvp)->tv_nsec < 0) {                               \
250                         (vvp)->tv_sec--;                                \
251                         (vvp)->tv_nsec += 1000000000;                   \
252                 }                                                       \
253         } while (0)
254
255 /* Operations on timevals. */
256
257 #define timevalclear(tvp)               ((tvp)->tv_sec = (tvp)->tv_usec = 0)
258 #define timevalisset(tvp)               ((tvp)->tv_sec || (tvp)->tv_usec)
259 #define timevalcmp(tvp, uvp, cmp)                                       \
260         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
261             ((tvp)->tv_usec cmp (uvp)->tv_usec) :                       \
262             ((tvp)->tv_sec cmp (uvp)->tv_sec))
263
264 /* timevaladd and timevalsub are not inlined */
265
266 #endif /* _KERNEL */
267
268 #ifndef _KERNEL                 /* NetBSD/OpenBSD compatible interfaces */
269
270 #define timerclear(tvp)         ((tvp)->tv_sec = (tvp)->tv_usec = 0)
271 #define timerisset(tvp)         ((tvp)->tv_sec || (tvp)->tv_usec)
272 #define timercmp(tvp, uvp, cmp)                                 \
273         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
274             ((tvp)->tv_usec cmp (uvp)->tv_usec) :                       \
275             ((tvp)->tv_sec cmp (uvp)->tv_sec))
276 #define timeradd(tvp, uvp, vvp)                                         \
277         do {                                                            \
278                 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;          \
279                 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;       \
280                 if ((vvp)->tv_usec >= 1000000) {                        \
281                         (vvp)->tv_sec++;                                \
282                         (vvp)->tv_usec -= 1000000;                      \
283                 }                                                       \
284         } while (0)
285 #define timersub(tvp, uvp, vvp)                                         \
286         do {                                                            \
287                 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;          \
288                 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;       \
289                 if ((vvp)->tv_usec < 0) {                               \
290                         (vvp)->tv_sec--;                                \
291                         (vvp)->tv_usec += 1000000;                      \
292                 }                                                       \
293         } while (0)
294 #endif
295
296 /*
297  * Names of the interval timers, and structure
298  * defining a timer setting.
299  */
300 #define ITIMER_REAL     0
301 #define ITIMER_VIRTUAL  1
302 #define ITIMER_PROF     2
303
304 struct itimerval {
305         struct  timeval it_interval;    /* timer interval */
306         struct  timeval it_value;       /* current value */
307 };
308
309 /*
310  * Getkerninfo clock information structure
311  */
312 struct clockinfo {
313         int     hz;             /* clock frequency */
314         int     tick;           /* micro-seconds per hz tick */
315         int     spare;
316         int     stathz;         /* statistics clock frequency */
317         int     profhz;         /* profiling clock frequency */
318 };
319
320 /* These macros are also in time.h. */
321 #ifndef CLOCK_REALTIME
322 #define CLOCK_REALTIME  0
323 #define CLOCK_VIRTUAL   1
324 #define CLOCK_PROF      2
325 #define CLOCK_MONOTONIC 4
326 #define CLOCK_UPTIME    5               /* FreeBSD-specific. */
327 #define CLOCK_UPTIME_PRECISE    7       /* FreeBSD-specific. */
328 #define CLOCK_UPTIME_FAST       8       /* FreeBSD-specific. */
329 #define CLOCK_REALTIME_PRECISE  9       /* FreeBSD-specific. */
330 #define CLOCK_REALTIME_FAST     10      /* FreeBSD-specific. */
331 #define CLOCK_MONOTONIC_PRECISE 11      /* FreeBSD-specific. */
332 #define CLOCK_MONOTONIC_FAST    12      /* FreeBSD-specific. */
333 #define CLOCK_SECOND    13              /* FreeBSD-specific. */
334 #define CLOCK_THREAD_CPUTIME_ID 14
335 #define CLOCK_PROCESS_CPUTIME_ID        15
336 #endif
337
338 #ifndef TIMER_ABSTIME
339 #define TIMER_RELTIME   0x0     /* relative timer */
340 #define TIMER_ABSTIME   0x1     /* absolute timer */
341 #endif
342
343 #if __BSD_VISIBLE
344 #define CPUCLOCK_WHICH_PID      0
345 #define CPUCLOCK_WHICH_TID      1
346 #endif
347
348 #ifdef _KERNEL
349
350 /*
351  * Kernel to clock driver interface.
352  */
353 void    inittodr(time_t base);
354 void    resettodr(void);
355
356 extern volatile time_t  time_second;
357 extern volatile time_t  time_uptime;
358 extern struct bintime boottimebin;
359 extern struct timeval boottime;
360
361 /*
362  * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
363  *
364  * Functions without the "get" prefix returns the best timestamp
365  * we can produce in the given format.
366  *
367  * "bin"   == struct bintime  == seconds + 64 bit fraction of seconds.
368  * "nano"  == struct timespec == seconds + nanoseconds.
369  * "micro" == struct timeval  == seconds + microseconds.
370  *
371  * Functions containing "up" returns time relative to boot and
372  * should be used for calculating time intervals.
373  *
374  * Functions without "up" returns GMT time.
375  *
376  * Functions with the "get" prefix returns a less precise result
377  * much faster than the functions without "get" prefix and should
378  * be used where a precision of 1/hz seconds is acceptable or where
379  * performance is priority. (NB: "precision", _not_ "resolution" !)
380  */
381
382 void    binuptime(struct bintime *bt);
383 void    nanouptime(struct timespec *tsp);
384 void    microuptime(struct timeval *tvp);
385
386 static __inline sbintime_t
387 sbinuptime(void)
388 {
389         struct bintime bt;
390
391         binuptime(&bt);
392         return (bttosbt(bt));
393 }
394
395 void    bintime(struct bintime *bt);
396 void    nanotime(struct timespec *tsp);
397 void    microtime(struct timeval *tvp);
398
399 void    getbinuptime(struct bintime *bt);
400 void    getnanouptime(struct timespec *tsp);
401 void    getmicrouptime(struct timeval *tvp);
402
403 static __inline sbintime_t
404 getsbinuptime(void)
405 {
406         struct bintime bt;
407
408         getbinuptime(&bt);
409         return (bttosbt(bt));
410 }
411
412 void    getbintime(struct bintime *bt);
413 void    getnanotime(struct timespec *tsp);
414 void    getmicrotime(struct timeval *tvp);
415
416 /* Other functions */
417 int     itimerdecr(struct itimerval *itp, int usec);
418 int     itimerfix(struct timeval *tv);
419 int     ppsratecheck(struct timeval *, int *, int);
420 int     ratecheck(struct timeval *, const struct timeval *);
421 void    timevaladd(struct timeval *t1, const struct timeval *t2);
422 void    timevalsub(struct timeval *t1, const struct timeval *t2);
423 int     tvtohz(struct timeval *tv);
424 #else /* !_KERNEL */
425 #include <time.h>
426
427 #include <sys/cdefs.h>
428 #include <sys/select.h>
429
430 __BEGIN_DECLS
431 int     setitimer(int, const struct itimerval *, struct itimerval *);
432 int     utimes(const char *, const struct timeval *);
433
434 #if __BSD_VISIBLE
435 int     adjtime(const struct timeval *, struct timeval *);
436 int     clock_getcpuclockid2(id_t, int, clockid_t *);
437 int     futimes(int, const struct timeval *);
438 int     futimesat(int, const char *, const struct timeval [2]);
439 int     lutimes(const char *, const struct timeval *);
440 int     settimeofday(const struct timeval *, const struct timezone *);
441 #endif
442
443 #if __XSI_VISIBLE
444 int     getitimer(int, struct itimerval *);
445 int     gettimeofday(struct timeval *, struct timezone *);
446 #endif
447
448 __END_DECLS
449
450 #endif /* !_KERNEL */
451
452 #endif /* !_SYS_TIME_H_ */