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