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