xref: /titanic_50/usr/src/uts/common/sys/time.h (revision 42cc51e07cdbcad3b9aca8d9d991fc09b251feb7)
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	NSEC2SEC(n)	((n) / (NANOSEC / SEC))
251 #define	SEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / SEC))
252 
253 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
254 
255 #ifndef	_ASM
256 
257 /*
258  * Time expressed as a 64-bit nanosecond counter.
259  */
260 typedef	longlong_t	hrtime_t;
261 
262 #if defined(_KERNEL) || defined(_FAKE_KERNEL)
263 
264 #include <sys/time_impl.h>
265 #include <sys/mutex.h>
266 
267 extern int tick_per_msec;	/* clock ticks per millisecond (may be zero) */
268 extern int msec_per_tick;	/* milliseconds per clock tick (may be zero) */
269 extern int usec_per_tick;	/* microseconds per clock tick */
270 extern int nsec_per_tick;	/* nanoseconds per clock tick */
271 
272 /*
273  * Macros to convert from common units of time (sec, msec, usec, nsec,
274  * timeval, timestruc) to clock ticks and vice versa.
275  */
276 #define	TICK_TO_SEC(tick)	((tick) / hz)
277 #define	SEC_TO_TICK(sec)	((sec) * hz)
278 
279 #define	TICK_TO_MSEC(tick)	\
280 	(msec_per_tick ? (tick) * msec_per_tick : (tick) / tick_per_msec)
281 #define	MSEC_TO_TICK(msec)	\
282 	(msec_per_tick ? (msec) / msec_per_tick : (msec) * tick_per_msec)
283 #define	MSEC_TO_TICK_ROUNDUP(msec)	\
284 	(msec_per_tick ? \
285 	((msec) == 0 ? 0 : ((msec) - 1) / msec_per_tick + 1) : \
286 	(msec) * tick_per_msec)
287 
288 #define	TICK_TO_USEC(tick)		((tick) * usec_per_tick)
289 #define	USEC_TO_TICK(usec)		((usec) / usec_per_tick)
290 #define	USEC_TO_TICK_ROUNDUP(usec)	\
291 	((usec) == 0 ? 0 : USEC_TO_TICK((usec) - 1) + 1)
292 
293 #define	TICK_TO_NSEC(tick)		((hrtime_t)(tick) * nsec_per_tick)
294 #define	NSEC_TO_TICK(nsec)		((nsec) / nsec_per_tick)
295 #define	NSEC_TO_TICK_ROUNDUP(nsec)	\
296 	((nsec) == 0 ? 0 : NSEC_TO_TICK((nsec) - 1) + 1)
297 
298 #define	TICK_TO_TIMEVAL(tick, tvp) {	\
299 	clock_t __tmptck = (tick);	\
300 	(tvp)->tv_sec = TICK_TO_SEC(__tmptck);	\
301 	(tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK((tvp)->tv_sec)); \
302 }
303 
304 #define	TICK_TO_TIMEVAL32(tick, tvp) {	\
305 	clock_t __tmptck = (tick);	\
306 	time_t __tmptm = TICK_TO_SEC(__tmptck);	\
307 	(tvp)->tv_sec = (time32_t)__tmptm;	\
308 	(tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK(__tmptm)); \
309 }
310 
311 #define	TICK_TO_TIMESTRUC(tick, tsp) {	\
312 	clock_t __tmptck = (tick);	\
313 	(tsp)->tv_sec = TICK_TO_SEC(__tmptck);	\
314 	(tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK((tsp)->tv_sec)); \
315 }
316 
317 #define	TICK_TO_TIMESTRUC32(tick, tsp) {	\
318 	clock_t __tmptck = (tick);			\
319 	time_t __tmptm = TICK_TO_SEC(__tmptck);		\
320 	(tsp)->tv_sec = (time32_t)__tmptm;		\
321 	(tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK(__tmptm));	\
322 }
323 
324 #define	TIMEVAL_TO_TICK(tvp)	\
325 	(SEC_TO_TICK((tvp)->tv_sec) + USEC_TO_TICK((tvp)->tv_usec))
326 
327 #define	TIMESTRUC_TO_TICK(tsp)	\
328 	(SEC_TO_TICK((tsp)->tv_sec) + NSEC_TO_TICK((tsp)->tv_nsec))
329 
330 typedef struct todinfo {
331 	int	tod_sec;	/* seconds 0-59 */
332 	int	tod_min;	/* minutes 0-59 */
333 	int	tod_hour;	/* hours 0-23 */
334 	int	tod_dow;	/* day of week 1-7 */
335 	int	tod_day;	/* day of month 1-31 */
336 	int	tod_month;	/* month 1-12 */
337 	int	tod_year;	/* year 70+ */
338 } todinfo_t;
339 
340 extern	int64_t		timedelta;
341 extern	int		timechanged;
342 extern	int		tod_needsync;
343 extern	kmutex_t	tod_lock;
344 extern	volatile timestruc_t	hrestime;
345 extern	hrtime_t	hres_last_tick;
346 extern	int64_t		hrestime_adj;
347 extern	uint_t		adj_shift;
348 
349 extern	timestruc_t	tod_get(void);
350 extern	void		tod_set(timestruc_t);
351 extern	void		set_hrestime(timestruc_t *);
352 extern	todinfo_t	utc_to_tod(time_t);
353 extern	time_t		tod_to_utc(todinfo_t);
354 extern	int		hr_clock_lock(void);
355 extern	void		hr_clock_unlock(int);
356 extern	hrtime_t 	gethrtime(void);
357 extern	hrtime_t 	gethrtime_unscaled(void);
358 extern	hrtime_t	gethrtime_max(void);
359 extern	hrtime_t	gethrtime_waitfree(void);
360 extern	void		scalehrtime(hrtime_t *);
361 extern	uint64_t	unscalehrtime(hrtime_t);
362 extern	void 		gethrestime(timespec_t *);
363 extern	time_t 		gethrestime_sec(void);
364 extern	void		gethrestime_lasttick(timespec_t *);
365 extern	void		hrt2ts(hrtime_t, timestruc_t *);
366 extern	hrtime_t	ts2hrt(const timestruc_t *);
367 extern	void		hrt2tv(hrtime_t, struct timeval *);
368 extern	hrtime_t	tv2hrt(struct timeval *);
369 extern	int		itimerfix(struct timeval *, int);
370 extern	int		itimerdecr(struct itimerval *, int);
371 extern	void		timevaladd(struct timeval *, struct timeval *);
372 extern	void		timevalsub(struct timeval *, struct timeval *);
373 extern	void		timevalfix(struct timeval *);
374 extern	void		dtrace_hres_tick(void);
375 
376 extern clock_t		ddi_get_lbolt(void);
377 extern int64_t		ddi_get_lbolt64(void);
378 
379 #if defined(_SYSCALL32)
380 extern	void		hrt2ts32(hrtime_t, timestruc32_t *);
381 #endif
382 
383 #endif /* _KERNEL */
384 
385 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
386 int adjtime(struct timeval *, struct timeval *);
387 #endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
388 
389 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || \
390 	defined(_ATFILE_SOURCE) || defined(__EXTENSIONS__)
391 int futimesat(int, const char *, const struct timeval *);
392 #endif /* defined(__ATFILE_SOURCE) */
393 
394 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
395 	defined(__EXTENSIONS__)
396 
397 int getitimer(int, struct itimerval *);
398 int utimes(const char *, const struct timeval *);
399 #if defined(_XPG4_2)
400 int setitimer(int, const struct itimerval *_RESTRICT_KYWD,
401 	struct itimerval *_RESTRICT_KYWD);
402 #else
403 int setitimer(int, struct itimerval *_RESTRICT_KYWD,
404 	struct itimerval *_RESTRICT_KYWD);
405 #endif /* defined(_XPG2_2) */
406 
407 #endif /* !defined(_KERNEL) ... defined(_XPG4_2) */
408 
409 /*
410  * gettimeofday() and settimeofday() were included in SVr4 due to their
411  * common use in BSD based applications.  They were to be included exactly
412  * as in BSD, with two parameters.  However, AT&T/USL noted that the second
413  * parameter was unused and deleted it, thereby making a routine included
414  * for compatibility, incompatible.
415  *
416  * XSH4.2 (spec 1170) defines gettimeofday and settimeofday to have two
417  * parameters.
418  *
419  * This has caused general disagreement in the application community as to
420  * the syntax of these routines.  Solaris defaults to the XSH4.2 definition.
421  * The flag _SVID_GETTOD may be used to force the SVID version.
422  */
423 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
424 
425 #if defined(_SVID_GETTOD)
426 int settimeofday(struct timeval *);
427 #else
428 int settimeofday(struct timeval *, void *);
429 #endif
430 hrtime_t	gethrtime(void);
431 hrtime_t	gethrvtime(void);
432 
433 #endif /* !(defined _KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
434 
435 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
436 	defined(__EXTENSIONS__)
437 
438 #if defined(_SVID_GETTOD)
439 int gettimeofday(struct timeval *);
440 #else
441 int gettimeofday(struct timeval *_RESTRICT_KYWD, void *_RESTRICT_KYWD);
442 #endif
443 
444 #endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
445 
446 /*
447  * The inclusion of <time.h> is historical and was added for
448  * backward compatibility in delta 1.2 when a number of definitions
449  * were moved out of <sys/time.h>.  More recently, the timespec and
450  * itimerspec structure definitions, along with the _CLOCK_*, CLOCK_*,
451  * _TIMER_*, and TIMER_* symbols were moved to <sys/time_impl.h>,
452  * which is now included by <time.h>.  This change was due to POSIX
453  * 1003.1b-1993 and X/Open UNIX 98 requirements.  For non-POSIX and
454  * non-X/Open applications, including this header will still make
455  * visible these definitions.
456  */
457 #if !defined(_BOOT) && !defined(_KERNEL) && !defined(_FAKE_KERNEL) && \
458 	!defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
459 #include <time.h>
460 #endif
461 
462 /*
463  * The inclusion of <sys/select.h> is needed for the FD_CLR,
464  * FD_ISSET, FD_SET, and FD_SETSIZE macros as well as the
465  * select() prototype defined in the XOpen specifications
466  * beginning with XSH4v2.  Placement required after definition
467  * for itimerval.
468  */
469 #if !defined(_KERNEL) && !defined(_FAKE_KERNEL) && \
470 	!defined(__XOPEN_OR_POSIX) || \
471 	defined(_XPG4_2) || defined(__EXTENSIONS__)
472 #include <sys/select.h>
473 #endif
474 
475 #endif	/* _ASM */
476 
477 #ifdef	__cplusplus
478 }
479 #endif
480 
481 #endif	/* _SYS_TIME_H */
482