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