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