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