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: stable/11/sys/sys/time.h 363386 2020-07-21 08:13:35Z kib $ 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 40struct 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 53struct bintime { 54 time_t sec; 55 uint64_t frac; 56}; 57 58static __inline void 59bintime_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 69static __inline void 70bintime_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 81static __inline void 82bintime_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 93static __inline void 94bintime_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 105static __inline void 106bintime_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) /* beware rounding, see nstosbt() */ 132#define SBT_MAX 0x7fffffffffffffffLL 133 134static __inline int 135sbintime_getsec(sbintime_t _sbt) 136{ 137 138 return (_sbt >> 32); 139} 140 141static __inline sbintime_t 142bttosbt(const struct bintime _bt) 143{ 144 145 return (((sbintime_t)_bt.sec << 32) + (_bt.frac >> 32)); 146} 147 148static __inline struct bintime 149sbttobt(sbintime_t _sbt) 150{ 151 struct bintime _bt; 152 153 _bt.sec = _sbt >> 32; 154 _bt.frac = _sbt << 32; 155 return (_bt); 156} 157 158/* 159 * Decimal<->sbt conversions. Multiplying or dividing by SBT_1NS results in 160 * large roundoff errors which sbttons() and nstosbt() avoid. Millisecond and 161 * microsecond functions are also provided for completeness. 162 */ 163static __inline int64_t 164sbttons(sbintime_t _sbt) 165{ 166 167 return ((1000000000 * _sbt) >> 32); 168} 169 170static __inline sbintime_t 171nstosbt(int64_t _ns) 172{ 173 174 return ((_ns * (((uint64_t)1 << 63) / 500000000)) >> 32); 175} 176 177static __inline int64_t 178sbttous(sbintime_t _sbt) 179{ 180 181 return ((1000000 * _sbt) >> 32); 182} 183 184static __inline sbintime_t 185ustosbt(int64_t _us) 186{ 187 188 return ((_us * (((uint64_t)1 << 63) / 500000)) >> 32); 189} 190 191static __inline int64_t 192sbttoms(sbintime_t _sbt) 193{ 194 195 return ((1000 * _sbt) >> 32); 196} 197 198static __inline sbintime_t 199mstosbt(int64_t _ms) 200{ 201 202 return ((_ms * (((uint64_t)1 << 63) / 500)) >> 32); 203} 204 205/*- 206 * Background information: 207 * 208 * When converting between timestamps on parallel timescales of differing 209 * resolutions it is historical and scientific practice to round down rather 210 * than doing 4/5 rounding. 211 * 212 * The date changes at midnight, not at noon. 213 * 214 * Even at 15:59:59.999999999 it's not four'o'clock. 215 * 216 * time_second ticks after N.999999999 not after N.4999999999 217 */ 218 219static __inline void 220bintime2timespec(const struct bintime *_bt, struct timespec *_ts) 221{ 222 223 _ts->tv_sec = _bt->sec; 224 _ts->tv_nsec = ((uint64_t)1000000000 * 225 (uint32_t)(_bt->frac >> 32)) >> 32; 226} 227 228static __inline void 229timespec2bintime(const struct timespec *_ts, struct bintime *_bt) 230{ 231 232 _bt->sec = _ts->tv_sec; 233 /* 18446744073 = int(2^64 / 1000000000) */ 234 _bt->frac = _ts->tv_nsec * (uint64_t)18446744073LL; 235} 236 237static __inline void 238bintime2timeval(const struct bintime *_bt, struct timeval *_tv) 239{ 240 241 _tv->tv_sec = _bt->sec; 242 _tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(_bt->frac >> 32)) >> 32; 243} 244 245static __inline void 246timeval2bintime(const struct timeval *_tv, struct bintime *_bt) 247{ 248 249 _bt->sec = _tv->tv_sec; 250 /* 18446744073709 = int(2^64 / 1000000) */ 251 _bt->frac = _tv->tv_usec * (uint64_t)18446744073709LL; 252} 253 254static __inline struct timespec 255sbttots(sbintime_t _sbt) 256{ 257 struct timespec _ts; 258 259 _ts.tv_sec = _sbt >> 32; 260 _ts.tv_nsec = sbttons((uint32_t)_sbt); 261 return (_ts); 262} 263 264static __inline sbintime_t 265tstosbt(struct timespec _ts) 266{ 267 268 return (((sbintime_t)_ts.tv_sec << 32) + nstosbt(_ts.tv_nsec)); 269} 270 271static __inline struct timeval 272sbttotv(sbintime_t _sbt) 273{ 274 struct timeval _tv; 275 276 _tv.tv_sec = _sbt >> 32; 277 _tv.tv_usec = sbttous((uint32_t)_sbt); 278 return (_tv); 279} 280 281static __inline sbintime_t 282tvtosbt(struct timeval _tv) 283{ 284 285 return (((sbintime_t)_tv.tv_sec << 32) + ustosbt(_tv.tv_usec)); 286} 287#endif /* __BSD_VISIBLE */ 288 289#ifdef _KERNEL 290 291/* Operations on timespecs */ 292#define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0) 293#define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec) 294#define timespeccmp(tvp, uvp, cmp) \ 295 (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 296 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \ 297 ((tvp)->tv_sec cmp (uvp)->tv_sec)) 298#define timespecadd(vvp, uvp) \ 299 do { \ 300 (vvp)->tv_sec += (uvp)->tv_sec; \ 301 (vvp)->tv_nsec += (uvp)->tv_nsec; \ 302 if ((vvp)->tv_nsec >= 1000000000) { \ 303 (vvp)->tv_sec++; \ 304 (vvp)->tv_nsec -= 1000000000; \ 305 } \ 306 } while (0) 307#define timespecsub(vvp, uvp) \ 308 do { \ 309 (vvp)->tv_sec -= (uvp)->tv_sec; \ 310 (vvp)->tv_nsec -= (uvp)->tv_nsec; \ 311 if ((vvp)->tv_nsec < 0) { \ 312 (vvp)->tv_sec--; \ 313 (vvp)->tv_nsec += 1000000000; \ 314 } \ 315 } while (0) 316 317/* Operations on timevals. */ 318 319#define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) 320#define timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) 321#define timevalcmp(tvp, uvp, cmp) \ 322 (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 323 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 324 ((tvp)->tv_sec cmp (uvp)->tv_sec)) 325 326/* timevaladd and timevalsub are not inlined */ 327 328#endif /* _KERNEL */ 329 330#ifndef _KERNEL /* NetBSD/OpenBSD compatible interfaces */ 331 332#define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) 333#define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) 334#define timercmp(tvp, uvp, cmp) \ 335 (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 336 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 337 ((tvp)->tv_sec cmp (uvp)->tv_sec)) 338#define timeradd(tvp, uvp, vvp) \ 339 do { \ 340 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ 341 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ 342 if ((vvp)->tv_usec >= 1000000) { \ 343 (vvp)->tv_sec++; \ 344 (vvp)->tv_usec -= 1000000; \ 345 } \ 346 } while (0) 347#define timersub(tvp, uvp, vvp) \ 348 do { \ 349 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 350 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 351 if ((vvp)->tv_usec < 0) { \ 352 (vvp)->tv_sec--; \ 353 (vvp)->tv_usec += 1000000; \ 354 } \ 355 } while (0) 356#endif 357 358/* 359 * Names of the interval timers, and structure 360 * defining a timer setting. 361 */ 362#define ITIMER_REAL 0 363#define ITIMER_VIRTUAL 1 364#define ITIMER_PROF 2 365 366struct itimerval { 367 struct timeval it_interval; /* timer interval */ 368 struct timeval it_value; /* current value */ 369}; 370 371/* 372 * Getkerninfo clock information structure 373 */ 374struct clockinfo { 375 int hz; /* clock frequency */ 376 int tick; /* micro-seconds per hz tick */ 377 int spare; 378 int stathz; /* statistics clock frequency */ 379 int profhz; /* profiling clock frequency */ 380}; 381 382/* These macros are also in time.h. */ 383#ifndef CLOCK_REALTIME 384#define CLOCK_REALTIME 0 385#endif 386#ifndef CLOCK_VIRTUAL 387#define CLOCK_VIRTUAL 1 388#define CLOCK_PROF 2 389#endif 390#ifndef CLOCK_MONOTONIC 391#define CLOCK_MONOTONIC 4 392#define CLOCK_UPTIME 5 /* FreeBSD-specific. */ 393#define CLOCK_UPTIME_PRECISE 7 /* FreeBSD-specific. */ 394#define CLOCK_UPTIME_FAST 8 /* FreeBSD-specific. */ 395#define CLOCK_REALTIME_PRECISE 9 /* FreeBSD-specific. */ 396#define CLOCK_REALTIME_FAST 10 /* FreeBSD-specific. */ 397#define CLOCK_MONOTONIC_PRECISE 11 /* FreeBSD-specific. */ 398#define CLOCK_MONOTONIC_FAST 12 /* FreeBSD-specific. */ 399#define CLOCK_SECOND 13 /* FreeBSD-specific. */ 400#define CLOCK_THREAD_CPUTIME_ID 14 401#define CLOCK_PROCESS_CPUTIME_ID 15 402#endif 403 404#ifndef TIMER_ABSTIME 405#define TIMER_RELTIME 0x0 /* relative timer */ 406#define TIMER_ABSTIME 0x1 /* absolute timer */ 407#endif 408 409#if __BSD_VISIBLE 410#define CPUCLOCK_WHICH_PID 0 411#define CPUCLOCK_WHICH_TID 1 412#endif 413 414#ifdef _KERNEL 415 416/* 417 * Kernel to clock driver interface. 418 */ 419void inittodr(time_t base); 420void resettodr(void); 421 422extern volatile time_t time_second; 423extern volatile time_t time_uptime; 424extern struct bintime tc_tick_bt; 425extern sbintime_t tc_tick_sbt; 426extern struct bintime tick_bt; 427extern sbintime_t tick_sbt; 428extern int tc_precexp; 429extern int tc_timepercentage; 430extern struct bintime bt_timethreshold; 431extern struct bintime bt_tickthreshold; 432extern sbintime_t sbt_timethreshold; 433extern sbintime_t sbt_tickthreshold; 434 435extern volatile int rtc_generation; 436 437/* 438 * Functions for looking at our clock: [get]{bin,nano,micro}[up]time() 439 * 440 * Functions without the "get" prefix returns the best timestamp 441 * we can produce in the given format. 442 * 443 * "bin" == struct bintime == seconds + 64 bit fraction of seconds. 444 * "nano" == struct timespec == seconds + nanoseconds. 445 * "micro" == struct timeval == seconds + microseconds. 446 * 447 * Functions containing "up" returns time relative to boot and 448 * should be used for calculating time intervals. 449 * 450 * Functions without "up" returns UTC time. 451 * 452 * Functions with the "get" prefix returns a less precise result 453 * much faster than the functions without "get" prefix and should 454 * be used where a precision of 1/hz seconds is acceptable or where 455 * performance is priority. (NB: "precision", _not_ "resolution" !) 456 */ 457 458void binuptime(struct bintime *bt); 459void nanouptime(struct timespec *tsp); 460void microuptime(struct timeval *tvp); 461 462static __inline sbintime_t 463sbinuptime(void) 464{ 465 struct bintime _bt; 466 467 binuptime(&_bt); 468 return (bttosbt(_bt)); 469} 470 471void bintime(struct bintime *bt); 472void nanotime(struct timespec *tsp); 473void microtime(struct timeval *tvp); 474 475void getbinuptime(struct bintime *bt); 476void getnanouptime(struct timespec *tsp); 477void getmicrouptime(struct timeval *tvp); 478 479static __inline sbintime_t 480getsbinuptime(void) 481{ 482 struct bintime _bt; 483 484 getbinuptime(&_bt); 485 return (bttosbt(_bt)); 486} 487 488void getbintime(struct bintime *bt); 489void getnanotime(struct timespec *tsp); 490void getmicrotime(struct timeval *tvp); 491 492void getboottime(struct timeval *boottime); 493void getboottimebin(struct bintime *boottimebin); 494 495/* Other functions */ 496int itimerdecr(struct itimerval *itp, int usec); 497int itimerfix(struct timeval *tv); 498int ppsratecheck(struct timeval *, int *, int); 499int ratecheck(struct timeval *, const struct timeval *); 500void timevaladd(struct timeval *t1, const struct timeval *t2); 501void timevalsub(struct timeval *t1, const struct timeval *t2); 502int tvtohz(struct timeval *tv); 503 504#define TC_DEFAULTPERC 5 505 506#define BT2FREQ(bt) \ 507 (((uint64_t)0x8000000000000000 + ((bt)->frac >> 2)) / \ 508 ((bt)->frac >> 1)) 509 510#define SBT2FREQ(sbt) ((SBT_1S + ((sbt) >> 1)) / (sbt)) 511 512#define FREQ2BT(freq, bt) \ 513{ \ 514 (bt)->sec = 0; \ 515 (bt)->frac = ((uint64_t)0x8000000000000000 / (freq)) << 1; \ 516} 517 518#define TIMESEL(sbt, sbt2) \ 519 (((sbt2) >= sbt_timethreshold) ? \ 520 ((*(sbt) = getsbinuptime()), 1) : ((*(sbt) = sbinuptime()), 0)) 521 522#else /* !_KERNEL */ 523#include <time.h> 524 525#include <sys/cdefs.h> 526#include <sys/select.h> 527 528__BEGIN_DECLS 529int setitimer(int, const struct itimerval *, struct itimerval *); 530int utimes(const char *, const struct timeval *); 531 532#if __BSD_VISIBLE 533int adjtime(const struct timeval *, struct timeval *); 534int clock_getcpuclockid2(id_t, int, clockid_t *); 535int futimes(int, const struct timeval *); 536int futimesat(int, const char *, const struct timeval [2]); 537int lutimes(const char *, const struct timeval *); 538int settimeofday(const struct timeval *, const struct timezone *); 539#endif 540 541#if __XSI_VISIBLE 542int getitimer(int, struct itimerval *); 543int gettimeofday(struct timeval *, struct timezone *); 544#endif 545 546__END_DECLS 547 548#endif /* !_KERNEL */ 549 550#endif /* !_SYS_TIME_H_ */ 551