2 * Copyright (c) 1982, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * @(#)time.h 8.5 (Berkeley) 5/4/95
36 #include <sys/_timeval.h>
37 #include <sys/types.h>
38 #include <sys/timespec.h>
41 int tz_minuteswest; /* minutes west of Greenwich */
42 int tz_dsttime; /* type of dst correction */
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 */
59 bintime_addx(struct bintime *bt, uint64_t x)
70 bintime_add(struct bintime *bt, const struct bintime *bt2)
75 bt->frac += bt2->frac;
82 bintime_sub(struct bintime *bt, const struct bintime *bt2)
87 bt->frac -= bt2->frac;
94 bintime_mul(struct bintime *bt, u_int x)
98 p1 = (bt->frac & 0xffffffffull) * x;
99 p2 = (bt->frac >> 32) * x + (p1 >> 32);
101 bt->sec += (p2 >> 32);
102 bt->frac = (p2 << 32) | (p1 & 0xffffffffull);
106 bintime_shift(struct bintime *bt, int exp)
111 bt->sec |= bt->frac >> (64 - exp);
113 } else if (exp < 0) {
115 bt->frac |= (uint64_t)bt->sec << (64 + exp);
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))
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)
134 sbintime_getsec(sbintime_t sbt)
140 static __inline sbintime_t
141 bttosbt(const struct bintime bt)
144 return (((sbintime_t)bt.sec << 32) + (bt.frac >> 32));
147 static __inline struct bintime
148 sbttobt(sbintime_t sbt)
158 * Background information:
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.
164 * The date changes at midnight, not at noon.
166 * Even at 15:59:59.999999999 it's not four'o'clock.
168 * time_second ticks after N.999999999 not after N.4999999999
172 bintime2timespec(const struct bintime *bt, struct timespec *ts)
175 ts->tv_sec = bt->sec;
176 ts->tv_nsec = ((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32;
180 timespec2bintime(const struct timespec *ts, struct bintime *bt)
183 bt->sec = ts->tv_sec;
184 /* 18446744073 = int(2^64 / 1000000000) */
185 bt->frac = ts->tv_nsec * (uint64_t)18446744073LL;
189 bintime2timeval(const struct bintime *bt, struct timeval *tv)
192 tv->tv_sec = bt->sec;
193 tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32;
197 timeval2bintime(const struct timeval *tv, struct bintime *bt)
200 bt->sec = tv->tv_sec;
201 /* 18446744073709 = int(2^64 / 1000000) */
202 bt->frac = tv->tv_usec * (uint64_t)18446744073709LL;
205 static __inline struct timespec
206 sbttots(sbintime_t sbt)
210 ts.tv_sec = sbt >> 32;
211 ts.tv_nsec = ((uint64_t)1000000000 * (uint32_t)sbt) >> 32;
215 static __inline sbintime_t
216 tstosbt(struct timespec ts)
219 return (((sbintime_t)ts.tv_sec << 32) +
220 (ts.tv_nsec * (((uint64_t)1 << 63) / 500000000) >> 32));
223 static __inline struct timeval
224 sbttotv(sbintime_t sbt)
228 tv.tv_sec = sbt >> 32;
229 tv.tv_usec = ((uint64_t)1000000 * (uint32_t)sbt) >> 32;
233 static __inline sbintime_t
234 tvtosbt(struct timeval tv)
237 return (((sbintime_t)tv.tv_sec << 32) +
238 (tv.tv_usec * (((uint64_t)1 << 63) / 500000) >> 32));
240 #endif /* __BSD_VISIBLE */
244 /* Operations on timespecs */
245 #define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
246 #define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec)
247 #define timespeccmp(tvp, uvp, cmp) \
248 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
249 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
250 ((tvp)->tv_sec cmp (uvp)->tv_sec))
251 #define timespecadd(vvp, uvp) \
253 (vvp)->tv_sec += (uvp)->tv_sec; \
254 (vvp)->tv_nsec += (uvp)->tv_nsec; \
255 if ((vvp)->tv_nsec >= 1000000000) { \
257 (vvp)->tv_nsec -= 1000000000; \
260 #define timespecsub(vvp, uvp) \
262 (vvp)->tv_sec -= (uvp)->tv_sec; \
263 (vvp)->tv_nsec -= (uvp)->tv_nsec; \
264 if ((vvp)->tv_nsec < 0) { \
266 (vvp)->tv_nsec += 1000000000; \
270 /* Operations on timevals. */
272 #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
273 #define timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
274 #define timevalcmp(tvp, uvp, cmp) \
275 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
276 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
277 ((tvp)->tv_sec cmp (uvp)->tv_sec))
279 /* timevaladd and timevalsub are not inlined */
283 #ifndef _KERNEL /* NetBSD/OpenBSD compatible interfaces */
285 #define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
286 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
287 #define timercmp(tvp, uvp, cmp) \
288 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
289 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
290 ((tvp)->tv_sec cmp (uvp)->tv_sec))
291 #define timeradd(tvp, uvp, vvp) \
293 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
294 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
295 if ((vvp)->tv_usec >= 1000000) { \
297 (vvp)->tv_usec -= 1000000; \
300 #define timersub(tvp, uvp, vvp) \
302 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
303 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
304 if ((vvp)->tv_usec < 0) { \
306 (vvp)->tv_usec += 1000000; \
312 * Names of the interval timers, and structure
313 * defining a timer setting.
315 #define ITIMER_REAL 0
316 #define ITIMER_VIRTUAL 1
317 #define ITIMER_PROF 2
320 struct timeval it_interval; /* timer interval */
321 struct timeval it_value; /* current value */
325 * Getkerninfo clock information structure
328 int hz; /* clock frequency */
329 int tick; /* micro-seconds per hz tick */
331 int stathz; /* statistics clock frequency */
332 int profhz; /* profiling clock frequency */
335 /* These macros are also in time.h. */
336 #ifndef CLOCK_REALTIME
337 #define CLOCK_REALTIME 0
338 #define CLOCK_VIRTUAL 1
340 #define CLOCK_MONOTONIC 4
341 #define CLOCK_UPTIME 5 /* FreeBSD-specific. */
342 #define CLOCK_UPTIME_PRECISE 7 /* FreeBSD-specific. */
343 #define CLOCK_UPTIME_FAST 8 /* FreeBSD-specific. */
344 #define CLOCK_REALTIME_PRECISE 9 /* FreeBSD-specific. */
345 #define CLOCK_REALTIME_FAST 10 /* FreeBSD-specific. */
346 #define CLOCK_MONOTONIC_PRECISE 11 /* FreeBSD-specific. */
347 #define CLOCK_MONOTONIC_FAST 12 /* FreeBSD-specific. */
348 #define CLOCK_SECOND 13 /* FreeBSD-specific. */
349 #define CLOCK_THREAD_CPUTIME_ID 14
350 #define CLOCK_PROCESS_CPUTIME_ID 15
353 #ifndef TIMER_ABSTIME
354 #define TIMER_RELTIME 0x0 /* relative timer */
355 #define TIMER_ABSTIME 0x1 /* absolute timer */
359 #define CPUCLOCK_WHICH_PID 0
360 #define CPUCLOCK_WHICH_TID 1
366 * Kernel to clock driver interface.
368 void inittodr(time_t base);
369 void resettodr(void);
371 extern volatile time_t time_second;
372 extern volatile time_t time_uptime;
373 extern struct bintime boottimebin;
374 extern struct timeval boottime;
375 extern struct bintime tc_tick_bt;
376 extern sbintime_t tc_tick_sbt;
377 extern struct bintime tick_bt;
378 extern sbintime_t tick_sbt;
379 extern int tc_precexp;
380 extern int tc_timepercentage;
381 extern struct bintime bt_timethreshold;
382 extern struct bintime bt_tickthreshold;
383 extern sbintime_t sbt_timethreshold;
384 extern sbintime_t sbt_tickthreshold;
387 * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
389 * Functions without the "get" prefix returns the best timestamp
390 * we can produce in the given format.
392 * "bin" == struct bintime == seconds + 64 bit fraction of seconds.
393 * "nano" == struct timespec == seconds + nanoseconds.
394 * "micro" == struct timeval == seconds + microseconds.
396 * Functions containing "up" returns time relative to boot and
397 * should be used for calculating time intervals.
399 * Functions without "up" returns GMT time.
401 * Functions with the "get" prefix returns a less precise result
402 * much faster than the functions without "get" prefix and should
403 * be used where a precision of 1/hz seconds is acceptable or where
404 * performance is priority. (NB: "precision", _not_ "resolution" !)
407 void binuptime(struct bintime *bt);
408 void nanouptime(struct timespec *tsp);
409 void microuptime(struct timeval *tvp);
411 static __inline sbintime_t
417 return (bttosbt(bt));
420 void bintime(struct bintime *bt);
421 void nanotime(struct timespec *tsp);
422 void microtime(struct timeval *tvp);
424 void getbinuptime(struct bintime *bt);
425 void getnanouptime(struct timespec *tsp);
426 void getmicrouptime(struct timeval *tvp);
428 static __inline sbintime_t
434 return (bttosbt(bt));
437 void getbintime(struct bintime *bt);
438 void getnanotime(struct timespec *tsp);
439 void getmicrotime(struct timeval *tvp);
441 /* Other functions */
442 int itimerdecr(struct itimerval *itp, int usec);
443 int itimerfix(struct timeval *tv);
444 int ppsratecheck(struct timeval *, int *, int);
445 int ratecheck(struct timeval *, const struct timeval *);
446 void timevaladd(struct timeval *t1, const struct timeval *t2);
447 void timevalsub(struct timeval *t1, const struct timeval *t2);
448 int tvtohz(struct timeval *tv);
450 #define TC_DEFAULTPERC 5
452 #define BT2FREQ(bt) \
453 (((uint64_t)0x8000000000000000 + ((bt)->frac >> 2)) / \
456 #define SBT2FREQ(sbt) ((SBT_1S + ((sbt) >> 1)) / (sbt))
458 #define FREQ2BT(freq, bt) \
461 (bt)->frac = ((uint64_t)0x8000000000000000 / (freq)) << 1; \
464 #define TIMESEL(sbt, sbt2) \
465 (((sbt2) >= sbt_timethreshold) ? \
466 ((*(sbt) = getsbinuptime()), 1) : ((*(sbt) = sbinuptime()), 0))
471 #include <sys/cdefs.h>
472 #include <sys/select.h>
475 int setitimer(int, const struct itimerval *, struct itimerval *);
476 int utimes(const char *, const struct timeval *);
479 int adjtime(const struct timeval *, struct timeval *);
480 int clock_getcpuclockid2(id_t, int, clockid_t *);
481 int futimes(int, const struct timeval *);
482 int futimesat(int, const char *, const struct timeval [2]);
483 int lutimes(const char *, const struct timeval *);
484 int settimeofday(const struct timeval *, const struct timezone *);
488 int getitimer(int, struct itimerval *);
489 int gettimeofday(struct timeval *, struct timezone *);
494 #endif /* !_KERNEL */
496 #endif /* !_SYS_TIME_H_ */