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