xref: /titanic_44/usr/src/uts/common/sys/time.h (revision 12cc75c814f0c017004a9bbc96429911e008601b)
1 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2 /*	  All Rights Reserved  	*/
3 
4 
5 /*
6  * Copyright (c) 1982, 1986 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 2007 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.16	*/
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 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
131 
132 #if !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || defined(__EXTENSIONS__)
133 /*
134  * Names of the interval timers, and structure
135  * defining a timer setting.
136  */
137 #define	ITIMER_REAL	0	/* Decrements in real time */
138 #define	ITIMER_VIRTUAL	1	/* Decrements in process virtual time */
139 #define	ITIMER_PROF	2	/* Decrements both in process virtual */
140 				/* time and when system is running on */
141 				/* behalf of the process. */
142 #define	ITIMER_REALPROF	3	/* Decrements in real time for real- */
143 				/* time profiling of multithreaded */
144 				/* programs. */
145 
146 #ifndef	_ASM
147 struct	itimerval {
148 	struct	timeval it_interval;	/* timer interval */
149 	struct	timeval it_value;	/* current value */
150 };
151 
152 #if defined(_SYSCALL32)
153 
154 struct itimerval32 {
155 	struct	timeval32 it_interval;
156 	struct	timeval32 it_value;
157 };
158 
159 #define	ITIMERVAL32_TO_ITIMERVAL(itv, itv32)	{	\
160 	TIMEVAL32_TO_TIMEVAL(&(itv)->it_interval, &(itv32)->it_interval); \
161 	TIMEVAL32_TO_TIMEVAL(&(itv)->it_value, &(itv32)->it_value);	\
162 }
163 
164 #define	ITIMERVAL_TO_ITIMERVAL32(itv32, itv)	{	\
165 	TIMEVAL_TO_TIMEVAL32(&(itv32)->it_interval, &(itv)->it_interval); \
166 	TIMEVAL_TO_TIMEVAL32(&(itv32)->it_value, &(itv)->it_value);	\
167 }
168 
169 #define	ITIMERVAL_OVERFLOW(itv)				\
170 	(TIMEVAL_OVERFLOW(&(itv)->it_interval) ||	\
171 	TIMEVAL_OVERFLOW(&(itv)->it_value))
172 
173 #endif	/* _SYSCALL32 */
174 #endif	/* _ASM */
175 #endif /* !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) ... */
176 
177 
178 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
179 /*
180  *	Definitions for commonly used resolutions.
181  */
182 #define	SEC		1
183 #define	MILLISEC	1000
184 #define	MICROSEC	1000000
185 #define	NANOSEC		1000000000
186 
187 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
188 
189 #ifndef	_ASM
190 
191 /*
192  * Time expressed as a 64-bit nanosecond counter.
193  */
194 typedef	longlong_t	hrtime_t;
195 
196 #ifdef _KERNEL
197 
198 #include <sys/time_impl.h>
199 #include <sys/mutex.h>
200 
201 extern int tick_per_msec;	/* clock ticks per millisecond (may be zero) */
202 extern int msec_per_tick;	/* milliseconds per clock tick (may be zero) */
203 extern int usec_per_tick;	/* microseconds per clock tick */
204 extern int nsec_per_tick;	/* nanoseconds per clock tick */
205 
206 /*
207  * Macros to convert from common units of time (sec, msec, usec, nsec,
208  * timeval, timestruc) to clock ticks and vice versa.
209  */
210 #define	TICK_TO_SEC(tick)	((tick) / hz)
211 #define	SEC_TO_TICK(sec)	((sec) * hz)
212 
213 #define	TICK_TO_MSEC(tick)	\
214 	(msec_per_tick ? (tick) * msec_per_tick : (tick) / tick_per_msec)
215 #define	MSEC_TO_TICK(msec)	\
216 	(msec_per_tick ? (msec) / msec_per_tick : (msec) * tick_per_msec)
217 #define	MSEC_TO_TICK_ROUNDUP(msec)	\
218 	(msec_per_tick ? \
219 	((msec) == 0 ? 0 : ((msec) - 1) / msec_per_tick + 1) : \
220 	(msec) * tick_per_msec)
221 
222 #define	TICK_TO_USEC(tick)		((tick) * usec_per_tick)
223 #define	USEC_TO_TICK(usec)		((usec) / usec_per_tick)
224 #define	USEC_TO_TICK_ROUNDUP(usec)	\
225 	((usec) == 0 ? 0 : USEC_TO_TICK((usec) - 1) + 1)
226 
227 #define	TICK_TO_NSEC(tick)		((tick) * nsec_per_tick)
228 #define	NSEC_TO_TICK(nsec)		((nsec) / nsec_per_tick)
229 #define	NSEC_TO_TICK_ROUNDUP(nsec)	\
230 	((nsec) == 0 ? 0 : NSEC_TO_TICK((nsec) - 1) + 1)
231 
232 #define	TICK_TO_TIMEVAL(tick, tvp) {	\
233 	clock_t __tmptck = (tick);	\
234 	(tvp)->tv_sec = TICK_TO_SEC(__tmptck);	\
235 	(tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK((tvp)->tv_sec)); \
236 }
237 
238 #define	TICK_TO_TIMEVAL32(tick, tvp) {	\
239 	clock_t __tmptck = (tick);	\
240 	time_t __tmptm = TICK_TO_SEC(__tmptck);	\
241 	(tvp)->tv_sec = (time32_t)__tmptm;	\
242 	(tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK(__tmptm)); \
243 }
244 
245 #define	TICK_TO_TIMESTRUC(tick, tsp) {	\
246 	clock_t __tmptck = (tick);	\
247 	(tsp)->tv_sec = TICK_TO_SEC(__tmptck);	\
248 	(tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK((tsp)->tv_sec)); \
249 }
250 
251 #define	TICK_TO_TIMESTRUC32(tick, tsp) {	\
252 	clock_t __tmptck = (tick);			\
253 	time_t __tmptm = TICK_TO_SEC(__tmptck);		\
254 	(tsp)->tv_sec = (time32_t)__tmptm;		\
255 	(tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK(__tmptm));	\
256 }
257 
258 #define	TIMEVAL_TO_TICK(tvp)	\
259 	(SEC_TO_TICK((tvp)->tv_sec) + USEC_TO_TICK((tvp)->tv_usec))
260 
261 #define	TIMESTRUC_TO_TICK(tsp)	\
262 	(SEC_TO_TICK((tsp)->tv_sec) + NSEC_TO_TICK((tsp)->tv_nsec))
263 
264 typedef struct todinfo {
265 	int	tod_sec;	/* seconds 0-59 */
266 	int	tod_min;	/* minutes 0-59 */
267 	int	tod_hour;	/* hours 0-23 */
268 	int	tod_dow;	/* day of week 1-7 */
269 	int	tod_day;	/* day of month 1-31 */
270 	int	tod_month;	/* month 1-12 */
271 	int	tod_year;	/* year 70+ */
272 } todinfo_t;
273 
274 extern	int64_t		timedelta;
275 extern	int		timechanged;
276 extern	int		tod_needsync;
277 extern	kmutex_t	tod_lock;
278 extern	timestruc_t	hrestime;
279 extern	hrtime_t	hres_last_tick;
280 extern	int64_t		hrestime_adj;
281 extern	uint_t		adj_shift;
282 
283 extern	timestruc_t	tod_get(void);
284 extern	void		tod_set(timestruc_t);
285 extern	void		set_hrestime(timestruc_t *);
286 extern	todinfo_t	utc_to_tod(time_t);
287 extern	time_t		tod_to_utc(todinfo_t);
288 extern	int		hr_clock_lock(void);
289 extern	void		hr_clock_unlock(int);
290 extern	hrtime_t 	gethrtime(void);
291 extern	hrtime_t 	gethrtime_unscaled(void);
292 extern	hrtime_t	gethrtime_max(void);
293 extern	hrtime_t	gethrtime_waitfree(void);
294 extern	void		scalehrtime(hrtime_t *);
295 extern  uint64_t	unscalehrtime(hrtime_t);
296 extern	void 		gethrestime(timespec_t *);
297 extern	time_t 		gethrestime_sec(void);
298 extern	void		gethrestime_lasttick(timespec_t *);
299 extern	void		hrt2ts(hrtime_t, timestruc_t *);
300 extern	hrtime_t	ts2hrt(const timestruc_t *);
301 extern	void		hrt2tv(hrtime_t, struct timeval *);
302 extern	hrtime_t	tv2hrt(struct timeval *);
303 extern	int		itimerfix(struct timeval *, int);
304 extern	int		itimerdecr(struct itimerval *, int);
305 extern	void		timevaladd(struct timeval *, struct timeval *);
306 extern	void		timevalsub(struct timeval *, struct timeval *);
307 extern	void		timevalfix(struct timeval *);
308 extern	void		dtrace_hres_tick(void);
309 
310 #if defined(_SYSCALL32)
311 extern	void		hrt2ts32(hrtime_t, timestruc32_t *);
312 #endif
313 
314 #endif /* _KERNEL */
315 
316 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
317 #if defined(__STDC__)
318 int adjtime(struct timeval *, struct timeval *);
319 #else
320 int adjtime();
321 #endif
322 #endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
323 
324 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || \
325 	defined(_ATFILE_SOURCE) || defined(__EXTENSIONS__)
326 #if defined(__STDC__)
327 int futimesat(int, const char *, const struct timeval *);
328 #else
329 int futimesat();
330 #endif /* defined(__STDC__) */
331 #endif /* defined(__ATFILE_SOURCE) */
332 
333 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
334 	defined(__EXTENSIONS__)
335 
336 #if defined(__STDC__)
337 
338 int getitimer(int, struct itimerval *);
339 int utimes(const char *, const struct timeval *);
340 #if defined(_XPG4_2)
341 int setitimer(int, const struct itimerval *_RESTRICT_KYWD,
342 	struct itimerval *_RESTRICT_KYWD);
343 #else
344 int setitimer(int, struct itimerval *_RESTRICT_KYWD,
345 	struct itimerval *_RESTRICT_KYWD);
346 #endif /* defined(_XPG2_2) */
347 
348 #else /* __STDC__ */
349 
350 int gettimer();
351 int settimer();
352 int utimes();
353 #endif /* __STDC__ */
354 #endif /* !defined(_KERNEL) ... defined(_XPG4_2) */
355 
356 /*
357  * gettimeofday() and settimeofday() were included in SVr4 due to their
358  * common use in BSD based applications.  They were to be included exactly
359  * as in BSD, with two parameters.  However, AT&T/USL noted that the second
360  * parameter was unused and deleted it, thereby making a routine included
361  * for compatibility, incompatible.
362  *
363  * XSH4.2 (spec 1170) defines gettimeofday and settimeofday to have two
364  * parameters.
365  *
366  * This has caused general disagreement in the application community as to
367  * the syntax of these routines.  Solaris defaults to the XSH4.2 definition.
368  * The flag _SVID_GETTOD may be used to force the SVID version.
369  */
370 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
371 
372 #if defined(__STDC__)
373 #if defined(_SVID_GETTOD)
374 int settimeofday(struct timeval *);
375 #else
376 int settimeofday(struct timeval *, void *);
377 #endif
378 hrtime_t	gethrtime(void);
379 hrtime_t	gethrvtime(void);
380 #else /* __STDC__ */
381 int settimeofday();
382 hrtime_t	gethrtime();
383 hrtime_t	gethrvtime();
384 #endif /* __STDC__ */
385 
386 #endif /* !(defined _KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
387 
388 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
389 	defined(__EXTENSIONS__)
390 
391 #if defined(__STDC__)
392 #if defined(_SVID_GETTOD)
393 int gettimeofday(struct timeval *);
394 #else
395 int gettimeofday(struct timeval *_RESTRICT_KYWD, void *_RESTRICT_KYWD);
396 #endif
397 #else /* __STDC__ */
398 int gettimeofday();
399 #endif /* __STDC__ */
400 
401 #endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
402 
403 /*
404  * The inclusion of <time.h> is historical and was added for
405  * backward compatibility in delta 1.2 when a number of definitions
406  * were moved out of <sys/time.h>.  More recently, the timespec and
407  * itimerspec structure definitions, along with the _CLOCK_*, CLOCK_*,
408  * _TIMER_*, and TIMER_* symbols were moved to <sys/time_impl.h>,
409  * which is now included by <time.h>.  This change was due to POSIX
410  * 1003.1b-1993 and X/Open UNIX 98 requirements.  For non-POSIX and
411  * non-X/Open applications, including this header will still make
412  * visible these definitions.
413  */
414 #if !defined(_BOOT) && !defined(_KERNEL) && \
415 	!defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
416 #include <time.h>
417 #endif
418 
419 /*
420  * The inclusion of <sys/select.h> is needed for the FD_CLR,
421  * FD_ISSET, FD_SET, and FD_SETSIZE macros as well as the
422  * select() prototype defined in the XOpen specifications
423  * beginning with XSH4v2.  Placement required after definition
424  * for itimerval.
425  */
426 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
427 	defined(__EXTENSIONS__)
428 #include <sys/select.h>
429 #endif
430 
431 #endif	/* _ASM */
432 
433 #ifdef	__cplusplus
434 }
435 #endif
436 
437 #endif	/* _SYS_TIME_H */
438