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