xref: /illumos-gate/usr/src/uts/common/sys/time.h (revision 30165b7f6753bc3d48c52319bed7ec7b3ea36b3c)
1 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2 /*	  All Rights Reserved	*/
3 
4 
5 /*
6  * Copyright (c) 1982, 1986, 1993 Regents of the University of California.
7  * All rights reserved.  The Berkeley software License Agreement
8  * specifies the terms and conditions for redistribution.
9  */
10 
11 /*
12  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
13  *
14  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
15  * Use is subject to license terms.
16  *
17  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
18  */
19 
20 /*
21  * Copyright (c) 2013, 2015 by Delphix. All rights reserved.
22  */
23 
24 #ifndef _SYS_TIME_H
25 #define	_SYS_TIME_H
26 
27 #include <sys/feature_tests.h>
28 
29 /*
30  * Structure returned by gettimeofday(2) system call,
31  * and used in other calls.
32  */
33 
34 #ifdef	__cplusplus
35 extern "C" {
36 #endif
37 
38 #if !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
39 	defined(__EXTENSIONS__)
40 #ifndef	_ASM
41 
42 #if !defined(_TIME_T) || __cplusplus >= 199711L
43 #define	_TIME_T
44 typedef	long	time_t;		/* time of day in seconds */
45 #endif	/* _TIME_T */
46 
47 #ifndef	_SUSECONDS_T
48 #define	_SUSECONDS_T
49 typedef	long	suseconds_t;	/* signed # of microseconds */
50 #endif	/* _SUSECONDS_T */
51 
52 struct timeval {
53 	time_t		tv_sec;		/* seconds */
54 	suseconds_t	tv_usec;	/* and microseconds */
55 };
56 
57 #if defined(_SYSCALL32)
58 
59 #include <sys/types32.h>
60 
61 #define	TIMEVAL32_TO_TIMEVAL(tv, tv32)	{	\
62 	(tv)->tv_sec = (time_t)(tv32)->tv_sec;	\
63 	(tv)->tv_usec = (tv32)->tv_usec;	\
64 }
65 
66 #define	TIMEVAL_TO_TIMEVAL32(tv32, tv)	{		\
67 	(tv32)->tv_sec = (time32_t)(tv)->tv_sec;	\
68 	(tv32)->tv_usec = (int32_t)(tv)->tv_usec;	\
69 }
70 
71 #define	TIME32_MAX	INT32_MAX
72 #define	TIME32_MIN	INT32_MIN
73 
74 #define	TIMEVAL_OVERFLOW(tv)	\
75 	((tv)->tv_sec < TIME32_MIN || (tv)->tv_sec > TIME32_MAX)
76 
77 #endif	/* _SYSCALL32 */
78 
79 #endif	/* _ASM */
80 #endif	/* !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) ... */
81 
82 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
83 #ifndef	_ASM
84 struct timezone {
85 	int	tz_minuteswest;	/* minutes west of Greenwich */
86 	int	tz_dsttime;	/* type of dst correction */
87 };
88 
89 #endif	/* _ASM */
90 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
91 
92 #ifdef	__cplusplus
93 }
94 #endif
95 
96 /*
97  * Needed for longlong_t type.  Placement of this due to <sys/types.h>
98  * including <sys/select.h> which relies on the presense of the itimerval
99  * structure.
100  */
101 #ifndef	_ASM
102 #include <sys/types.h>
103 #endif	/* _ASM */
104 
105 #ifdef	__cplusplus
106 extern "C" {
107 #endif
108 
109 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
110 
111 #define	DST_NONE	0	/* not on dst */
112 #define	DST_USA		1	/* USA style dst */
113 #define	DST_AUST	2	/* Australian style dst */
114 #define	DST_WET		3	/* Western European dst */
115 #define	DST_MET		4	/* Middle European dst */
116 #define	DST_EET		5	/* Eastern European dst */
117 #define	DST_CAN		6	/* Canada */
118 #define	DST_GB		7	/* Great Britain and Eire */
119 #define	DST_RUM		8	/* Rumania */
120 #define	DST_TUR		9	/* Turkey */
121 #define	DST_AUSTALT	10	/* Australian style with shift in 1986 */
122 
123 /*
124  * Operations on timevals.
125  */
126 #define	timerisset(tvp)		((tvp)->tv_sec || (tvp)->tv_usec)
127 #define	timercmp(tvp, uvp, cmp) \
128 	(((tvp)->tv_sec == (uvp)->tv_sec) ? \
129 	    /* CSTYLED */ \
130 	    ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
131 	    /* CSTYLED */ \
132 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
133 
134 #define	timerclear(tvp)		(tvp)->tv_sec = (tvp)->tv_usec = 0
135 
136 #ifdef __lint
137 /*
138  * Make innocuous, lint-happy versions until do {} while (0) is acknowleged as
139  * lint-safe.  If the compiler could know that we always make tv_usec < 1000000
140  * we wouldn't need a special linted version.
141  */
142 #define	timeradd(tvp, uvp, vvp)					\
143 	do								\
144 	{								\
145 		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
146 		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
147 		if ((vvp)->tv_usec >= 1000000)				\
148 		{							\
149 			(vvp)->tv_sec++;				\
150 			(vvp)->tv_usec -= 1000000;			\
151 		}							\
152 	} while ((vvp)->tv_usec >= 1000000)
153 #define	timersub(tvp, uvp, vvp)					\
154 	do								\
155 	{								\
156 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
157 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
158 		if ((vvp)->tv_usec < 0)					\
159 		{							\
160 			(vvp)->tv_sec--;				\
161 			(vvp)->tv_usec += 1000000;			\
162 		}							\
163 	} while ((vvp)->tv_usec >= 1000000)
164 #else
165 #define	timeradd(tvp, uvp, vvp)					\
166 	do								\
167 	{								\
168 		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
169 		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
170 		if ((vvp)->tv_usec >= 1000000)				\
171 		{							\
172 			(vvp)->tv_sec++;				\
173 			(vvp)->tv_usec -= 1000000;			\
174 		}							\
175 	} while (0)
176 
177 #define	timersub(tvp, uvp, vvp)					\
178 	do								\
179 	{								\
180 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
181 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
182 		if ((vvp)->tv_usec < 0)					\
183 		{							\
184 			(vvp)->tv_sec--;				\
185 			(vvp)->tv_usec += 1000000;			\
186 		}							\
187 	} while (0)
188 #endif /* __lint */
189 
190 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
191 
192 #if !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || defined(__EXTENSIONS__)
193 /*
194  * Names of the interval timers, and structure
195  * defining a timer setting.
196  */
197 #define	ITIMER_REAL	0	/* Decrements in real time */
198 #define	ITIMER_VIRTUAL	1	/* Decrements in process virtual time */
199 #define	ITIMER_PROF	2	/* Decrements both in process virtual */
200 				/* time and when system is running on */
201 				/* behalf of the process. */
202 #define	ITIMER_REALPROF	3	/* Decrements in real time for real- */
203 				/* time profiling of multithreaded */
204 				/* programs. */
205 
206 #ifndef	_ASM
207 struct	itimerval {
208 	struct	timeval it_interval;	/* timer interval */
209 	struct	timeval it_value;	/* current value */
210 };
211 
212 #if defined(_SYSCALL32)
213 
214 struct itimerval32 {
215 	struct	timeval32 it_interval;
216 	struct	timeval32 it_value;
217 };
218 
219 #define	ITIMERVAL32_TO_ITIMERVAL(itv, itv32)	{	\
220 	TIMEVAL32_TO_TIMEVAL(&(itv)->it_interval, &(itv32)->it_interval); \
221 	TIMEVAL32_TO_TIMEVAL(&(itv)->it_value, &(itv32)->it_value);	\
222 }
223 
224 #define	ITIMERVAL_TO_ITIMERVAL32(itv32, itv)	{	\
225 	TIMEVAL_TO_TIMEVAL32(&(itv32)->it_interval, &(itv)->it_interval); \
226 	TIMEVAL_TO_TIMEVAL32(&(itv32)->it_value, &(itv)->it_value);	\
227 }
228 
229 #define	ITIMERVAL_OVERFLOW(itv)				\
230 	(TIMEVAL_OVERFLOW(&(itv)->it_interval) ||	\
231 	TIMEVAL_OVERFLOW(&(itv)->it_value))
232 
233 #endif	/* _SYSCALL32 */
234 #endif	/* _ASM */
235 #endif /* !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) ... */
236 
237 
238 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
239 /*
240  *	Definitions for commonly used resolutions.
241  */
242 #define	SEC		1
243 #define	MILLISEC	1000
244 #define	MICROSEC	1000000
245 #define	NANOSEC		1000000000LL
246 
247 #define	MSEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / MILLISEC))
248 #define	NSEC2MSEC(n)	((n) / (NANOSEC / MILLISEC))
249 
250 #define	USEC2NSEC(m)    ((hrtime_t)(m) * (NANOSEC / MICROSEC))
251 #define	NSEC2USEC(n)    ((n) / (NANOSEC / MICROSEC))
252 
253 #define	NSEC2SEC(n)	((n) / (NANOSEC / SEC))
254 #define	SEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / SEC))
255 
256 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
257 
258 #ifndef	_ASM
259 
260 /*
261  * Time expressed as a 64-bit nanosecond counter.
262  */
263 typedef	longlong_t	hrtime_t;
264 
265 #if defined(_KERNEL) || defined(_FAKE_KERNEL)
266 
267 #include <sys/time_impl.h>
268 #include <sys/mutex.h>
269 
270 extern int tick_per_msec;	/* clock ticks per millisecond (may be zero) */
271 extern int msec_per_tick;	/* milliseconds per clock tick (may be zero) */
272 extern int usec_per_tick;	/* microseconds per clock tick */
273 extern int nsec_per_tick;	/* nanoseconds per clock tick */
274 
275 /*
276  * Macros to convert from common units of time (sec, msec, usec, nsec,
277  * timeval, timestruc) to clock ticks and vice versa.
278  */
279 #define	TICK_TO_SEC(tick)	((tick) / hz)
280 #define	SEC_TO_TICK(sec)	((sec) * hz)
281 
282 #define	TICK_TO_MSEC(tick)	\
283 	(msec_per_tick ? (tick) * msec_per_tick : (tick) / tick_per_msec)
284 #define	MSEC_TO_TICK(msec)	\
285 	(msec_per_tick ? (msec) / msec_per_tick : (msec) * tick_per_msec)
286 #define	MSEC_TO_TICK_ROUNDUP(msec)	\
287 	(msec_per_tick ? \
288 	((msec) == 0 ? 0 : ((msec) - 1) / msec_per_tick + 1) : \
289 	(msec) * tick_per_msec)
290 
291 #define	TICK_TO_USEC(tick)		((tick) * usec_per_tick)
292 #define	USEC_TO_TICK(usec)		((usec) / usec_per_tick)
293 #define	USEC_TO_TICK_ROUNDUP(usec)	\
294 	((usec) == 0 ? 0 : USEC_TO_TICK((usec) - 1) + 1)
295 
296 #define	TICK_TO_NSEC(tick)		((hrtime_t)(tick) * nsec_per_tick)
297 #define	NSEC_TO_TICK(nsec)		((nsec) / nsec_per_tick)
298 #define	NSEC_TO_TICK_ROUNDUP(nsec)	\
299 	((nsec) == 0 ? 0 : NSEC_TO_TICK((nsec) - 1) + 1)
300 
301 #define	TICK_TO_TIMEVAL(tick, tvp) {	\
302 	clock_t __tmptck = (tick);	\
303 	(tvp)->tv_sec = TICK_TO_SEC(__tmptck);	\
304 	(tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK((tvp)->tv_sec)); \
305 }
306 
307 #define	TICK_TO_TIMEVAL32(tick, tvp) {	\
308 	clock_t __tmptck = (tick);	\
309 	time_t __tmptm = TICK_TO_SEC(__tmptck);	\
310 	(tvp)->tv_sec = (time32_t)__tmptm;	\
311 	(tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK(__tmptm)); \
312 }
313 
314 #define	TICK_TO_TIMESTRUC(tick, tsp) {	\
315 	clock_t __tmptck = (tick);	\
316 	(tsp)->tv_sec = TICK_TO_SEC(__tmptck);	\
317 	(tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK((tsp)->tv_sec)); \
318 }
319 
320 #define	TICK_TO_TIMESTRUC32(tick, tsp) {	\
321 	clock_t __tmptck = (tick);			\
322 	time_t __tmptm = TICK_TO_SEC(__tmptck);		\
323 	(tsp)->tv_sec = (time32_t)__tmptm;		\
324 	(tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK(__tmptm));	\
325 }
326 
327 #define	TIMEVAL_TO_TICK(tvp)	\
328 	(SEC_TO_TICK((tvp)->tv_sec) + USEC_TO_TICK((tvp)->tv_usec))
329 
330 #define	TIMESTRUC_TO_TICK(tsp)	\
331 	(SEC_TO_TICK((tsp)->tv_sec) + NSEC_TO_TICK((tsp)->tv_nsec))
332 
333 typedef struct todinfo {
334 	int	tod_sec;	/* seconds 0-59 */
335 	int	tod_min;	/* minutes 0-59 */
336 	int	tod_hour;	/* hours 0-23 */
337 	int	tod_dow;	/* day of week 1-7 */
338 	int	tod_day;	/* day of month 1-31 */
339 	int	tod_month;	/* month 1-12 */
340 	int	tod_year;	/* year 70+ */
341 } todinfo_t;
342 
343 extern	int64_t		timedelta;
344 extern	int		timechanged;
345 extern	int		tod_needsync;
346 extern	kmutex_t	tod_lock;
347 extern	volatile timestruc_t	hrestime;
348 extern	hrtime_t	hres_last_tick;
349 extern	int64_t		hrestime_adj;
350 extern	uint_t		adj_shift;
351 
352 extern	timestruc_t	tod_get(void);
353 extern	void		tod_set(timestruc_t);
354 extern	void		set_hrestime(timestruc_t *);
355 extern	todinfo_t	utc_to_tod(time_t);
356 extern	time_t		tod_to_utc(todinfo_t);
357 extern	int		hr_clock_lock(void);
358 extern	void		hr_clock_unlock(int);
359 extern	hrtime_t 	gethrtime(void);
360 extern	hrtime_t 	gethrtime_unscaled(void);
361 extern	hrtime_t	gethrtime_max(void);
362 extern	hrtime_t	gethrtime_waitfree(void);
363 extern	void		scalehrtime(hrtime_t *);
364 extern	uint64_t	unscalehrtime(hrtime_t);
365 extern	void 		gethrestime(timespec_t *);
366 extern	time_t 		gethrestime_sec(void);
367 extern	void		gethrestime_lasttick(timespec_t *);
368 extern	void		hrt2ts(hrtime_t, timestruc_t *);
369 extern	hrtime_t	ts2hrt(const timestruc_t *);
370 extern	void		hrt2tv(hrtime_t, struct timeval *);
371 extern	hrtime_t	tv2hrt(struct timeval *);
372 extern	int		itimerfix(struct timeval *, int);
373 extern	int		itimerdecr(struct itimerval *, int);
374 extern	void		timevaladd(struct timeval *, struct timeval *);
375 extern	void		timevalsub(struct timeval *, struct timeval *);
376 extern	void		timevalfix(struct timeval *);
377 extern	void		dtrace_hres_tick(void);
378 
379 extern clock_t		ddi_get_lbolt(void);
380 extern int64_t		ddi_get_lbolt64(void);
381 
382 #if defined(_SYSCALL32)
383 extern	void		hrt2ts32(hrtime_t, timestruc32_t *);
384 #endif
385 
386 #endif /* _KERNEL */
387 
388 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
389 int adjtime(struct timeval *, struct timeval *);
390 #endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
391 
392 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || \
393 	defined(_ATFILE_SOURCE) || defined(__EXTENSIONS__)
394 int futimesat(int, const char *, const struct timeval *);
395 #endif /* defined(__ATFILE_SOURCE) */
396 
397 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
398 	defined(__EXTENSIONS__)
399 
400 int getitimer(int, struct itimerval *);
401 int utimes(const char *, const struct timeval *);
402 #if defined(_XPG4_2)
403 int setitimer(int, const struct itimerval *_RESTRICT_KYWD,
404 	struct itimerval *_RESTRICT_KYWD);
405 #else
406 int setitimer(int, struct itimerval *_RESTRICT_KYWD,
407 	struct itimerval *_RESTRICT_KYWD);
408 #endif /* defined(_XPG2_2) */
409 
410 #endif /* !defined(_KERNEL) ... defined(_XPG4_2) */
411 
412 /*
413  * gettimeofday() and settimeofday() were included in SVr4 due to their
414  * common use in BSD based applications.  They were to be included exactly
415  * as in BSD, with two parameters.  However, AT&T/USL noted that the second
416  * parameter was unused and deleted it, thereby making a routine included
417  * for compatibility, incompatible.
418  *
419  * XSH4.2 (spec 1170) defines gettimeofday and settimeofday to have two
420  * parameters.
421  *
422  * This has caused general disagreement in the application community as to
423  * the syntax of these routines.  Solaris defaults to the XSH4.2 definition.
424  * The flag _SVID_GETTOD may be used to force the SVID version.
425  */
426 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
427 
428 #if defined(_SVID_GETTOD)
429 int settimeofday(struct timeval *);
430 #else
431 int settimeofday(struct timeval *, void *);
432 #endif
433 hrtime_t	gethrtime(void);
434 hrtime_t	gethrvtime(void);
435 
436 #endif /* !(defined _KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
437 
438 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
439 	defined(__EXTENSIONS__)
440 
441 #if defined(_SVID_GETTOD)
442 int gettimeofday(struct timeval *);
443 #else
444 int gettimeofday(struct timeval *_RESTRICT_KYWD, void *_RESTRICT_KYWD);
445 #endif
446 
447 #endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
448 
449 /*
450  * The inclusion of <time.h> is historical and was added for
451  * backward compatibility in delta 1.2 when a number of definitions
452  * were moved out of <sys/time.h>.  More recently, the timespec and
453  * itimerspec structure definitions, along with the _CLOCK_*, CLOCK_*,
454  * _TIMER_*, and TIMER_* symbols were moved to <sys/time_impl.h>,
455  * which is now included by <time.h>.  This change was due to POSIX
456  * 1003.1b-1993 and X/Open UNIX 98 requirements.  For non-POSIX and
457  * non-X/Open applications, including this header will still make
458  * visible these definitions.
459  */
460 #if !defined(_BOOT) && !defined(_KERNEL) && !defined(_FAKE_KERNEL) && \
461 	!defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
462 #include <time.h>
463 #endif
464 
465 /*
466  * The inclusion of <sys/select.h> is needed for the FD_CLR,
467  * FD_ISSET, FD_SET, and FD_SETSIZE macros as well as the
468  * select() prototype defined in the XOpen specifications
469  * beginning with XSH4v2.  Placement required after definition
470  * for itimerval.
471  */
472 #if !defined(_KERNEL) && !defined(_FAKE_KERNEL) && \
473 	!defined(__XOPEN_OR_POSIX) || \
474 	defined(_XPG4_2) || defined(__EXTENSIONS__)
475 #include <sys/select.h>
476 #endif
477 
478 #endif	/* _ASM */
479 
480 #ifdef	__cplusplus
481 }
482 #endif
483 
484 #endif	/* _SYS_TIME_H */
485