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, 2016 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 USEC2NSEC(m) ((hrtime_t)(m) * (NANOSEC / MICROSEC))
252 #define NSEC2USEC(n) ((n) / (NANOSEC / MICROSEC))
253
254 #define NSEC2SEC(n) ((n) / (NANOSEC / SEC))
255 #define SEC2NSEC(m) ((hrtime_t)(m) * (NANOSEC / SEC))
256
257 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
258
259 #ifndef _ASM
260
261 /*
262 * Time expressed as a 64-bit nanosecond counter.
263 */
264 typedef longlong_t hrtime_t;
265
266 #if defined(_KERNEL) || defined(_FAKE_KERNEL)
267
268 /*
269 * Unsigned counterpart to hrtime_t
270 */
271 typedef u_longlong_t uhrtime_t;
272
273 #define HRTIME_MAX LLONG_MAX
274 #define UHRTIME_MAX ULLONG_MAX
275
276 #include <sys/time_impl.h>
277 #include <sys/mutex.h>
278
279 extern int tick_per_msec; /* clock ticks per millisecond (may be zero) */
280 extern int msec_per_tick; /* milliseconds per clock tick (may be zero) */
281 extern int usec_per_tick; /* microseconds per clock tick */
282 extern int nsec_per_tick; /* nanoseconds per clock tick */
283
284 /*
285 * Macros to convert from common units of time (sec, msec, usec, nsec,
286 * timeval, timestruc) to clock ticks and vice versa.
287 */
288 #define TICK_TO_SEC(tick) ((tick) / hz)
289 #define SEC_TO_TICK(sec) ((sec) * hz)
290
291 #define TICK_TO_MSEC(tick) \
292 (msec_per_tick ? (tick) * msec_per_tick : (tick) / tick_per_msec)
293 #define MSEC_TO_TICK(msec) \
294 (msec_per_tick ? (msec) / msec_per_tick : (msec) * tick_per_msec)
295 #define MSEC_TO_TICK_ROUNDUP(msec) \
296 (msec_per_tick ? \
297 ((msec) == 0 ? 0 : ((msec) - 1) / msec_per_tick + 1) : \
298 (msec) * tick_per_msec)
299
300 #define TICK_TO_USEC(tick) ((tick) * usec_per_tick)
301 #define USEC_TO_TICK(usec) ((usec) / usec_per_tick)
302 #define USEC_TO_TICK_ROUNDUP(usec) \
303 ((usec) == 0 ? 0 : USEC_TO_TICK((usec) - 1) + 1)
304
305 #define TICK_TO_NSEC(tick) ((hrtime_t)(tick) * nsec_per_tick)
306 #define NSEC_TO_TICK(nsec) ((nsec) / nsec_per_tick)
307 #define NSEC_TO_TICK_ROUNDUP(nsec) \
308 ((nsec) == 0 ? 0 : NSEC_TO_TICK((nsec) - 1) + 1)
309
310 #define TICK_TO_TIMEVAL(tick, tvp) { \
311 clock_t __tmptck = (tick); \
312 (tvp)->tv_sec = TICK_TO_SEC(__tmptck); \
313 (tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK((tvp)->tv_sec)); \
314 }
315
316 #define TICK_TO_TIMEVAL32(tick, tvp) { \
317 clock_t __tmptck = (tick); \
318 time_t __tmptm = TICK_TO_SEC(__tmptck); \
319 (tvp)->tv_sec = (time32_t)__tmptm; \
320 (tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK(__tmptm)); \
321 }
322
323 #define TICK_TO_TIMESTRUC(tick, tsp) { \
324 clock_t __tmptck = (tick); \
325 (tsp)->tv_sec = TICK_TO_SEC(__tmptck); \
326 (tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK((tsp)->tv_sec)); \
327 }
328
329 #define TICK_TO_TIMESTRUC32(tick, tsp) { \
330 clock_t __tmptck = (tick); \
331 time_t __tmptm = TICK_TO_SEC(__tmptck); \
332 (tsp)->tv_sec = (time32_t)__tmptm; \
333 (tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK(__tmptm)); \
334 }
335
336 #define TIMEVAL_TO_TICK(tvp) \
337 (SEC_TO_TICK((tvp)->tv_sec) + USEC_TO_TICK((tvp)->tv_usec))
338
339 #define TIMESTRUC_TO_TICK(tsp) \
340 (SEC_TO_TICK((tsp)->tv_sec) + NSEC_TO_TICK((tsp)->tv_nsec))
341
342 typedef struct todinfo {
343 int tod_sec; /* seconds 0-59 */
344 int tod_min; /* minutes 0-59 */
345 int tod_hour; /* hours 0-23 */
346 int tod_dow; /* day of week 1-7 */
347 int tod_day; /* day of month 1-31 */
348 int tod_month; /* month 1-12 */
349 int tod_year; /* year 70+ */
350 } todinfo_t;
351
352 extern int64_t timedelta;
353 extern int timechanged;
354 extern int tod_needsync;
355 extern kmutex_t tod_lock;
356 extern volatile timestruc_t hrestime;
357 extern hrtime_t hres_last_tick;
358 extern int64_t hrestime_adj;
359 extern uint_t adj_shift;
360
361 extern timestruc_t tod_get(void);
362 extern void tod_set(timestruc_t);
363 extern void set_hrestime(timestruc_t *);
364 extern todinfo_t utc_to_tod(time_t);
365 extern time_t tod_to_utc(todinfo_t);
366 extern int hr_clock_lock(void);
367 extern void hr_clock_unlock(int);
368 extern hrtime_t gethrtime(void);
369 extern hrtime_t gethrtime_unscaled(void);
370 extern hrtime_t gethrtime_max(void);
371 extern hrtime_t gethrtime_waitfree(void);
372 extern void scalehrtime(hrtime_t *);
373 extern uint64_t unscalehrtime(hrtime_t);
374 extern void gethrestime(timespec_t *);
375 extern time_t gethrestime_sec(void);
376 extern void gethrestime_lasttick(timespec_t *);
377 extern void hrt2ts(hrtime_t, timestruc_t *);
378 extern hrtime_t ts2hrt(const timestruc_t *);
379 extern void hrt2tv(hrtime_t, struct timeval *);
380 extern hrtime_t tv2hrt(struct timeval *);
381 extern int itimerfix(struct timeval *, int);
382 extern int itimerdecr(struct itimerval *, int);
383 extern void timevaladd(struct timeval *, struct timeval *);
384 extern void timevalsub(struct timeval *, struct timeval *);
385 extern void timevalfix(struct timeval *);
386 extern void dtrace_hres_tick(void);
387
388 extern clock_t ddi_get_lbolt(void);
389 extern int64_t ddi_get_lbolt64(void);
390
391 #if defined(_SYSCALL32)
392 extern void hrt2ts32(hrtime_t, timestruc32_t *);
393 #endif
394
395 #endif /* _KERNEL */
396
397 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
398 int adjtime(struct timeval *, struct timeval *);
399 #endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
400
401 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || \
402 defined(_ATFILE_SOURCE) || defined(__EXTENSIONS__)
403 int futimesat(int, const char *, const struct timeval *);
404 #endif /* defined(__ATFILE_SOURCE) */
405
406 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
407 defined(__EXTENSIONS__)
408
409 int getitimer(int, struct itimerval *);
410 int utimes(const char *, const struct timeval *);
411 #if defined(_XPG4_2)
412 int setitimer(int, const struct itimerval *_RESTRICT_KYWD,
413 struct itimerval *_RESTRICT_KYWD);
414 #else
415 int setitimer(int, struct itimerval *_RESTRICT_KYWD,
416 struct itimerval *_RESTRICT_KYWD);
417 #endif /* defined(_XPG2_2) */
418
419 #endif /* !defined(_KERNEL) ... defined(_XPG4_2) */
420
421 /*
422 * gettimeofday() and settimeofday() were included in SVr4 due to their
423 * common use in BSD based applications. They were to be included exactly
424 * as in BSD, with two parameters. However, AT&T/USL noted that the second
425 * parameter was unused and deleted it, thereby making a routine included
426 * for compatibility, incompatible.
427 *
428 * XSH4.2 (spec 1170) defines gettimeofday and settimeofday to have two
429 * parameters.
430 *
431 * This has caused general disagreement in the application community as to
432 * the syntax of these routines. Solaris defaults to the XSH4.2 definition.
433 * The flag _SVID_GETTOD may be used to force the SVID version.
434 */
435 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
436
437 #if defined(_SVID_GETTOD)
438 int settimeofday(struct timeval *);
439 #else
440 int settimeofday(struct timeval *, void *);
441 #endif
442 hrtime_t gethrtime(void);
443 hrtime_t gethrvtime(void);
444
445 #endif /* !(defined _KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
446
447 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
448 defined(__EXTENSIONS__)
449
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
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) && !defined(_FAKE_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(_FAKE_KERNEL) && \
482 !defined(__XOPEN_OR_POSIX) || \
483 defined(_XPG4_2) || defined(__EXTENSIONS__)
484 #include <sys/select.h>
485 #endif
486
487 #endif /* _ASM */
488
489 #ifdef __cplusplus
490 }
491 #endif
492
493 #endif /* _SYS_TIME_H */