17c478bdstevel@tonic-gate/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2b4203d7Marcel Telka/*	  All Rights Reserved	*/
37c478bdstevel@tonic-gate
47c478bdstevel@tonic-gate
57c478bdstevel@tonic-gate/*
639de7e4danmcd * Copyright (c) 1982, 1986, 1993 Regents of the University of California.
77c478bdstevel@tonic-gate * All rights reserved.  The Berkeley software License Agreement
87c478bdstevel@tonic-gate * specifies the terms and conditions for redistribution.
97c478bdstevel@tonic-gate */
107c478bdstevel@tonic-gate
117c478bdstevel@tonic-gate/*
12ba3594bGarrett D'Amore * Copyright 2014 Garrett D'Amore <garrett@damore.org>
13ba3594bGarrett D'Amore *
14113b131Eric Saxe * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
157c478bdstevel@tonic-gate * Use is subject to license terms.
16b819ceaGordon Ross *
17b819ceaGordon Ross * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
187c478bdstevel@tonic-gate */
197c478bdstevel@tonic-gate
20a8f6344Eli Rosenthal/*
21c12492cSebastien Roy * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
22a8f6344Eli Rosenthal */
23a8f6344Eli Rosenthal
247c478bdstevel@tonic-gate#ifndef _SYS_TIME_H
257c478bdstevel@tonic-gate#define	_SYS_TIME_H
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate#include <sys/feature_tests.h>
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate/*
307c478bdstevel@tonic-gate * Structure returned by gettimeofday(2) system call,
317c478bdstevel@tonic-gate * and used in other calls.
327c478bdstevel@tonic-gate */
337c478bdstevel@tonic-gate
347c478bdstevel@tonic-gate#ifdef	__cplusplus
357c478bdstevel@tonic-gateextern "C" {
367c478bdstevel@tonic-gate#endif
377c478bdstevel@tonic-gate
387c478bdstevel@tonic-gate#if !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
397c478bdstevel@tonic-gate	defined(__EXTENSIONS__)
407c478bdstevel@tonic-gate#ifndef	_ASM
417c478bdstevel@tonic-gate
427c478bdstevel@tonic-gate#if !defined(_TIME_T) || __cplusplus >= 199711L
437c478bdstevel@tonic-gate#define	_TIME_T
447c478bdstevel@tonic-gatetypedef	long	time_t;		/* time of day in seconds */
457c478bdstevel@tonic-gate#endif	/* _TIME_T */
467c478bdstevel@tonic-gate
477c478bdstevel@tonic-gate#ifndef	_SUSECONDS_T
487c478bdstevel@tonic-gate#define	_SUSECONDS_T
497c478bdstevel@tonic-gatetypedef	long	suseconds_t;	/* signed # of microseconds */
507c478bdstevel@tonic-gate#endif	/* _SUSECONDS_T */
517c478bdstevel@tonic-gate
527c478bdstevel@tonic-gatestruct timeval {
537c478bdstevel@tonic-gate	time_t		tv_sec;		/* seconds */
547c478bdstevel@tonic-gate	suseconds_t	tv_usec;	/* and microseconds */
557c478bdstevel@tonic-gate};
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gate#if defined(_SYSCALL32)
587c478bdstevel@tonic-gate
597c478bdstevel@tonic-gate#include <sys/types32.h>
607c478bdstevel@tonic-gate
617c478bdstevel@tonic-gate#define	TIMEVAL32_TO_TIMEVAL(tv, tv32)	{	\
627c478bdstevel@tonic-gate	(tv)->tv_sec = (time_t)(tv32)->tv_sec;	\
637c478bdstevel@tonic-gate	(tv)->tv_usec = (tv32)->tv_usec;	\
647c478bdstevel@tonic-gate}
657c478bdstevel@tonic-gate
667c478bdstevel@tonic-gate#define	TIMEVAL_TO_TIMEVAL32(tv32, tv)	{		\
677c478bdstevel@tonic-gate	(tv32)->tv_sec = (time32_t)(tv)->tv_sec;	\
68336069cPatrick Mooney	(tv32)->tv_usec = (int32_t)(tv)->tv_usec;	\
697c478bdstevel@tonic-gate}
707c478bdstevel@tonic-gate
717c478bdstevel@tonic-gate#define	TIME32_MAX	INT32_MAX
727c478bdstevel@tonic-gate#define	TIME32_MIN	INT32_MIN
737c478bdstevel@tonic-gate
747c478bdstevel@tonic-gate#define	TIMEVAL_OVERFLOW(tv)	\
757c478bdstevel@tonic-gate	((tv)->tv_sec < TIME32_MIN || (tv)->tv_sec > TIME32_MAX)
767c478bdstevel@tonic-gate
77b819ceaGordon Ross#endif	/* _SYSCALL32 */
787c478bdstevel@tonic-gate
797c478bdstevel@tonic-gate#endif	/* _ASM */
807c478bdstevel@tonic-gate#endif	/* !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) ... */
817c478bdstevel@tonic-gate
827c478bdstevel@tonic-gate#if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
837c478bdstevel@tonic-gate#ifndef	_ASM
847c478bdstevel@tonic-gatestruct timezone {
857c478bdstevel@tonic-gate	int	tz_minuteswest;	/* minutes west of Greenwich */
867c478bdstevel@tonic-gate	int	tz_dsttime;	/* type of dst correction */
877c478bdstevel@tonic-gate};
887c478bdstevel@tonic-gate
897c478bdstevel@tonic-gate#endif	/* _ASM */
907c478bdstevel@tonic-gate#endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
917c478bdstevel@tonic-gate
927c478bdstevel@tonic-gate#ifdef	__cplusplus
937c478bdstevel@tonic-gate}
947c478bdstevel@tonic-gate#endif
957c478bdstevel@tonic-gate
967c478bdstevel@tonic-gate/*
977c478bdstevel@tonic-gate * Needed for longlong_t type.  Placement of this due to <sys/types.h>
987c478bdstevel@tonic-gate * including <sys/select.h> which relies on the presense of the itimerval
997c478bdstevel@tonic-gate * structure.
1007c478bdstevel@tonic-gate */
1017c478bdstevel@tonic-gate#ifndef	_ASM
1027c478bdstevel@tonic-gate#include <sys/types.h>
1037c478bdstevel@tonic-gate#endif	/* _ASM */
1047c478bdstevel@tonic-gate
1057c478bdstevel@tonic-gate#ifdef	__cplusplus
1067c478bdstevel@tonic-gateextern "C" {
1077c478bdstevel@tonic-gate#endif
1087c478bdstevel@tonic-gate
1097c478bdstevel@tonic-gate#if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
1107c478bdstevel@tonic-gate
1117c478bdstevel@tonic-gate#define	DST_NONE	0	/* not on dst */
1127c478bdstevel@tonic-gate#define	DST_USA		1	/* USA style dst */
1137c478bdstevel@tonic-gate#define	DST_AUST	2	/* Australian style dst */
1147c478bdstevel@tonic-gate#define	DST_WET		3	/* Western European dst */
1157c478bdstevel@tonic-gate#define	DST_MET		4	/* Middle European dst */
1167c478bdstevel@tonic-gate#define	DST_EET		5	/* Eastern European dst */
1177c478bdstevel@tonic-gate#define	DST_CAN		6	/* Canada */
1187c478bdstevel@tonic-gate#define	DST_GB		7	/* Great Britain and Eire */
1197c478bdstevel@tonic-gate#define	DST_RUM		8	/* Rumania */
1207c478bdstevel@tonic-gate#define	DST_TUR		9	/* Turkey */
1217c478bdstevel@tonic-gate#define	DST_AUSTALT	10	/* Australian style with shift in 1986 */
1227c478bdstevel@tonic-gate
1237c478bdstevel@tonic-gate/*
1247c478bdstevel@tonic-gate * Operations on timevals.
1257c478bdstevel@tonic-gate */
1267c478bdstevel@tonic-gate#define	timerisset(tvp)		((tvp)->tv_sec || (tvp)->tv_usec)
1277c478bdstevel@tonic-gate#define	timercmp(tvp, uvp, cmp) \
1287c478bdstevel@tonic-gate	(((tvp)->tv_sec == (uvp)->tv_sec) ? \
1297c478bdstevel@tonic-gate	    /* CSTYLED */ \
1307c478bdstevel@tonic-gate	    ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
1317c478bdstevel@tonic-gate	    /* CSTYLED */ \
1327c478bdstevel@tonic-gate	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
1337c478bdstevel@tonic-gate
1347c478bdstevel@tonic-gate#define	timerclear(tvp)		(tvp)->tv_sec = (tvp)->tv_usec = 0
1357c478bdstevel@tonic-gate
13639de7e4danmcd#ifdef __lint
13739de7e4danmcd/*
13839de7e4danmcd * Make innocuous, lint-happy versions until do {} while (0) is acknowleged as
13939de7e4danmcd * lint-safe.  If the compiler could know that we always make tv_usec < 1000000
14039de7e4danmcd * we wouldn't need a special linted version.
14139de7e4danmcd */
14239de7e4danmcd#define	timeradd(tvp, uvp, vvp)					\
14339de7e4danmcd	do								\
14439de7e4danmcd	{								\
14539de7e4danmcd		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
14639de7e4danmcd		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
14739de7e4danmcd		if ((vvp)->tv_usec >= 1000000)				\
14839de7e4danmcd		{							\
14939de7e4danmcd			(vvp)->tv_sec++;				\
15039de7e4danmcd			(vvp)->tv_usec -= 1000000;			\
15139de7e4danmcd		}							\
15239de7e4danmcd	} while ((vvp)->tv_usec >= 1000000)
15339de7e4danmcd#define	timersub(tvp, uvp, vvp)					\
15439de7e4danmcd	do								\
15539de7e4danmcd	{								\
15639de7e4danmcd		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
15739de7e4danmcd		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
15839de7e4danmcd		if ((vvp)->tv_usec < 0)					\
15939de7e4danmcd		{							\
16039de7e4danmcd			(vvp)->tv_sec--;				\
16139de7e4danmcd			(vvp)->tv_usec += 1000000;			\
16239de7e4danmcd		}							\
16339de7e4danmcd	} while ((vvp)->tv_usec >= 1000000)
16439de7e4danmcd#else
16539de7e4danmcd#define	timeradd(tvp, uvp, vvp)					\
16639de7e4danmcd	do								\
16739de7e4danmcd	{								\
16839de7e4danmcd		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
16939de7e4danmcd		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
17039de7e4danmcd		if ((vvp)->tv_usec >= 1000000)				\
17139de7e4danmcd		{							\
17239de7e4danmcd			(vvp)->tv_sec++;				\
17339de7e4danmcd			(vvp)->tv_usec -= 1000000;			\
17439de7e4danmcd		}							\
17539de7e4danmcd	} while (0)
17639de7e4danmcd
17739de7e4danmcd#define	timersub(tvp, uvp, vvp)					\
17839de7e4danmcd	do								\
17939de7e4danmcd	{								\
18039de7e4danmcd		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
18139de7e4danmcd		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
18239de7e4danmcd		if ((vvp)->tv_usec < 0)					\
18339de7e4danmcd		{							\
18439de7e4danmcd			(vvp)->tv_sec--;				\
18539de7e4danmcd			(vvp)->tv_usec += 1000000;			\
18639de7e4danmcd		}							\
18739de7e4danmcd	} while (0)
18839de7e4danmcd#endif /* __lint */
18939de7e4danmcd
1907c478bdstevel@tonic-gate#endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
1917c478bdstevel@tonic-gate
1927c478bdstevel@tonic-gate#if !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || defined(__EXTENSIONS__)
1937c478bdstevel@tonic-gate/*
1947c478bdstevel@tonic-gate * Names of the interval timers, and structure
1957c478bdstevel@tonic-gate * defining a timer setting.
1967c478bdstevel@tonic-gate */
1977c478bdstevel@tonic-gate#define	ITIMER_REAL	0	/* Decrements in real time */
1987c478bdstevel@tonic-gate#define	ITIMER_VIRTUAL	1	/* Decrements in process virtual time */
1997c478bdstevel@tonic-gate#define	ITIMER_PROF	2	/* Decrements both in process virtual */
2007c478bdstevel@tonic-gate				/* time and when system is running on */
2017c478bdstevel@tonic-gate				/* behalf of the process. */
2027c478bdstevel@tonic-gate#define	ITIMER_REALPROF	3	/* Decrements in real time for real- */
2037c478bdstevel@tonic-gate				/* time profiling of multithreaded */
2047c478bdstevel@tonic-gate				/* programs. */
2057c478bdstevel@tonic-gate
2067c478bdstevel@tonic-gate#ifndef	_ASM
2077c478bdstevel@tonic-gatestruct	itimerval {
2087c478bdstevel@tonic-gate	struct	timeval it_interval;	/* timer interval */
2097c478bdstevel@tonic-gate	struct	timeval it_value;	/* current value */
2107c478bdstevel@tonic-gate};
2117c478bdstevel@tonic-gate
2127c478bdstevel@tonic-gate#if defined(_SYSCALL32)
2137c478bdstevel@tonic-gate
2147c478bdstevel@tonic-gatestruct itimerval32 {
2157c478bdstevel@tonic-gate	struct	timeval32 it_interval;
2167c478bdstevel@tonic-gate	struct	timeval32 it_value;
2177c478bdstevel@tonic-gate};
2187c478bdstevel@tonic-gate
2197c478bdstevel@tonic-gate#define	ITIMERVAL32_TO_ITIMERVAL(itv, itv32)	{	\
2207c478bdstevel@tonic-gate	TIMEVAL32_TO_TIMEVAL(&(itv)->it_interval, &(itv32)->it_interval); \
2217c478bdstevel@tonic-gate	TIMEVAL32_TO_TIMEVAL(&(itv)->it_value, &(itv32)->it_value);	\
2227c478bdstevel@tonic-gate}
2237c478bdstevel@tonic-gate
2247c478bdstevel@tonic-gate#define	ITIMERVAL_TO_ITIMERVAL32(itv32, itv)	{	\
2257c478bdstevel@tonic-gate	TIMEVAL_TO_TIMEVAL32(&(itv32)->it_interval, &(itv)->it_interval); \
2267c478bdstevel@tonic-gate	TIMEVAL_TO_TIMEVAL32(&(itv32)->it_value, &(itv)->it_value);	\
2277c478bdstevel@tonic-gate}
2287c478bdstevel@tonic-gate
2297c478bdstevel@tonic-gate#define	ITIMERVAL_OVERFLOW(itv)				\
2307c478bdstevel@tonic-gate	(TIMEVAL_OVERFLOW(&(itv)->it_interval) ||	\
2317c478bdstevel@tonic-gate	TIMEVAL_OVERFLOW(&(itv)->it_value))
2327c478bdstevel@tonic-gate
2337c478bdstevel@tonic-gate#endif	/* _SYSCALL32 */
2347c478bdstevel@tonic-gate#endif	/* _ASM */
2357c478bdstevel@tonic-gate#endif /* !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) ... */
2367c478bdstevel@tonic-gate
2377c478bdstevel@tonic-gate
2387c478bdstevel@tonic-gate#if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
2397c478bdstevel@tonic-gate/*
2407c478bdstevel@tonic-gate *	Definitions for commonly used resolutions.
2417c478bdstevel@tonic-gate */
2427c478bdstevel@tonic-gate#define	SEC		1
2437c478bdstevel@tonic-gate#define	MILLISEC	1000
2447c478bdstevel@tonic-gate#define	MICROSEC	1000000
245b59e212Josef 'Jeff' Sipek#define	NANOSEC		1000000000LL
2467c478bdstevel@tonic-gate
2470689f76Adam Leventhal#define	MSEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / MILLISEC))
2480689f76Adam Leventhal#define	NSEC2MSEC(n)	((n) / (NANOSEC / MILLISEC))
2490689f76Adam Leventhal
250c12492cSebastien Roy#define	USEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / MICROSEC))
251c12492cSebastien Roy#define	NSEC2USEC(n)	((n) / (NANOSEC / MICROSEC))
2521271e4bPrakash Surya
253a8f6344Eli Rosenthal#define	NSEC2SEC(n)	((n) / (NANOSEC / SEC))
254a8f6344Eli Rosenthal#define	SEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / SEC))
255a8f6344Eli Rosenthal
2567c478bdstevel@tonic-gate#endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
2577c478bdstevel@tonic-gate
2587c478bdstevel@tonic-gate#ifndef	_ASM
2597c478bdstevel@tonic-gate
2607c478bdstevel@tonic-gate/*
2617c478bdstevel@tonic-gate * Time expressed as a 64-bit nanosecond counter.
2627c478bdstevel@tonic-gate */
2637c478bdstevel@tonic-gatetypedef	longlong_t	hrtime_t;
2647c478bdstevel@tonic-gate
265b819ceaGordon Ross#if defined(_KERNEL) || defined(_FAKE_KERNEL)
2667c478bdstevel@tonic-gate
2677c478bdstevel@tonic-gate#include <sys/time_impl.h>
2687c478bdstevel@tonic-gate#include <sys/mutex.h>
2697c478bdstevel@tonic-gate
2707c478bdstevel@tonic-gateextern int tick_per_msec;	/* clock ticks per millisecond (may be zero) */
2717c478bdstevel@tonic-gateextern int msec_per_tick;	/* milliseconds per clock tick (may be zero) */
2727c478bdstevel@tonic-gateextern int usec_per_tick;	/* microseconds per clock tick */
2737c478bdstevel@tonic-gateextern int nsec_per_tick;	/* nanoseconds per clock tick */
2747c478bdstevel@tonic-gate
2757c478bdstevel@tonic-gate/*
2767c478bdstevel@tonic-gate * Macros to convert from common units of time (sec, msec, usec, nsec,
2777c478bdstevel@tonic-gate * timeval, timestruc) to clock ticks and vice versa.
2787c478bdstevel@tonic-gate */
2797c478bdstevel@tonic-gate#define	TICK_TO_SEC(tick)	((tick) / hz)
2807c478bdstevel@tonic-gate#define	SEC_TO_TICK(sec)	((sec) * hz)
2817c478bdstevel@tonic-gate
2827c478bdstevel@tonic-gate#define	TICK_TO_MSEC(tick)	\
2837c478bdstevel@tonic-gate	(msec_per_tick ? (tick) * msec_per_tick : (tick) / tick_per_msec)
2847c478bdstevel@tonic-gate#define	MSEC_TO_TICK(msec)	\
2857c478bdstevel@tonic-gate	(msec_per_tick ? (msec) / msec_per_tick : (msec) * tick_per_msec)
2867c478bdstevel@tonic-gate#define	MSEC_TO_TICK_ROUNDUP(msec)	\
2877c478bdstevel@tonic-gate	(msec_per_tick ? \
2887c478bdstevel@tonic-gate	((msec) == 0 ? 0 : ((msec) - 1) / msec_per_tick + 1) : \
2897c478bdstevel@tonic-gate	(msec) * tick_per_msec)
2907c478bdstevel@tonic-gate
2917c478bdstevel@tonic-gate#define	TICK_TO_USEC(tick)		((tick) * usec_per_tick)
2927c478bdstevel@tonic-gate#define	USEC_TO_TICK(usec)		((usec) / usec_per_tick)
2937c478bdstevel@tonic-gate#define	USEC_TO_TICK_ROUNDUP(usec)	\
2947c478bdstevel@tonic-gate	((usec) == 0 ? 0 : USEC_TO_TICK((usec) - 1) + 1)
2957c478bdstevel@tonic-gate
29687a18d3Madhavan Venkataraman#define	TICK_TO_NSEC(tick)		((hrtime_t)(tick) * nsec_per_tick)
2977c478bdstevel@tonic-gate#define	NSEC_TO_TICK(nsec)		((nsec) / nsec_per_tick)
2987c478bdstevel@tonic-gate#define	NSEC_TO_TICK_ROUNDUP(nsec)	\
2997c478bdstevel@tonic-gate	((nsec) == 0 ? 0 : NSEC_TO_TICK((nsec) - 1) + 1)
3007c478bdstevel@tonic-gate
3017c478bdstevel@tonic-gate#define	TICK_TO_TIMEVAL(tick, tvp) {	\
3027c478bdstevel@tonic-gate	clock_t __tmptck = (tick);	\
3037c478bdstevel@tonic-gate	(tvp)->tv_sec = TICK_TO_SEC(__tmptck);	\
3047c478bdstevel@tonic-gate	(tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK((tvp)->tv_sec)); \
3057c478bdstevel@tonic-gate}
3067c478bdstevel@tonic-gate
3077c478bdstevel@tonic-gate#define	TICK_TO_TIMEVAL32(tick, tvp) {	\
3087c478bdstevel@tonic-gate	clock_t __tmptck = (tick);	\
3097c478bdstevel@tonic-gate	time_t __tmptm = TICK_TO_SEC(__tmptck);	\
3107c478bdstevel@tonic-gate	(tvp)->tv_sec = (time32_t)__tmptm;	\
3117c478bdstevel@tonic-gate	(tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK(__tmptm)); \
3127c478bdstevel@tonic-gate}
3137c478bdstevel@tonic-gate
3147c478bdstevel@tonic-gate#define	TICK_TO_TIMESTRUC(tick, tsp) {	\
3157c478bdstevel@tonic-gate	clock_t __tmptck = (tick);	\
3167c478bdstevel@tonic-gate	(tsp)->tv_sec = TICK_TO_SEC(__tmptck);	\
3177c478bdstevel@tonic-gate	(tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK((tsp)->tv_sec)); \
3187c478bdstevel@tonic-gate}
3197c478bdstevel@tonic-gate
3207c478bdstevel@tonic-gate#define	TICK_TO_TIMESTRUC32(tick, tsp) {	\
3217c478bdstevel@tonic-gate	clock_t __tmptck = (tick);			\
3227c478bdstevel@tonic-gate	time_t __tmptm = TICK_TO_SEC(__tmptck);		\
3237c478bdstevel@tonic-gate	(tsp)->tv_sec = (time32_t)__tmptm;		\
3247c478bdstevel@tonic-gate	(tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK(__tmptm));	\
3257c478bdstevel@tonic-gate}
3267c478bdstevel@tonic-gate
3277c478bdstevel@tonic-gate#define	TIMEVAL_TO_TICK(tvp)	\
3287c478bdstevel@tonic-gate	(SEC_TO_TICK((tvp)->tv_sec) + USEC_TO_TICK((tvp)->tv_usec))
3297c478bdstevel@tonic-gate
3307c478bdstevel@tonic-gate#define	TIMESTRUC_TO_TICK(tsp)	\
3317c478bdstevel@tonic-gate	(SEC_TO_TICK((tsp)->tv_sec) + NSEC_TO_TICK((tsp)->tv_nsec))
3327c478bdstevel@tonic-gate
3337c478bdstevel@tonic-gatetypedef struct todinfo {
3347c478bdstevel@tonic-gate	int	tod_sec;	/* seconds 0-59 */
3357c478bdstevel@tonic-gate	int	tod_min;	/* minutes 0-59 */
3367c478bdstevel@tonic-gate	int	tod_hour;	/* hours 0-23 */
3377c478bdstevel@tonic-gate	int	tod_dow;	/* day of week 1-7 */
3387c478bdstevel@tonic-gate	int	tod_day;	/* day of month 1-31 */
3397c478bdstevel@tonic-gate	int	tod_month;	/* month 1-12 */
3407c478bdstevel@tonic-gate	int	tod_year;	/* year 70+ */
3417c478bdstevel@tonic-gate} todinfo_t;
3427c478bdstevel@tonic-gate
3437c478bdstevel@tonic-gateextern	int64_t		timedelta;
3443348528dmextern	int		timechanged;
3457c478bdstevel@tonic-gateextern	int		tod_needsync;
3467c478bdstevel@tonic-gateextern	kmutex_t	tod_lock;
347b5b48ccsudheerextern	volatile timestruc_t	hrestime;
3487c478bdstevel@tonic-gateextern	hrtime_t	hres_last_tick;
3497c478bdstevel@tonic-gateextern	int64_t		hrestime_adj;
3507c478bdstevel@tonic-gateextern	uint_t		adj_shift;
3517c478bdstevel@tonic-gate
3527c478bdstevel@tonic-gateextern	timestruc_t	tod_get(void);
3537c478bdstevel@tonic-gateextern	void		tod_set(timestruc_t);
3547c478bdstevel@tonic-gateextern	void		set_hrestime(timestruc_t *);
3557c478bdstevel@tonic-gateextern	todinfo_t	utc_to_tod(time_t);
3567c478bdstevel@tonic-gateextern	time_t		tod_to_utc(todinfo_t);
3577c478bdstevel@tonic-gateextern	int		hr_clock_lock(void);
3587c478bdstevel@tonic-gateextern	void		hr_clock_unlock(int);
3597c478bdstevel@tonic-gateextern	hrtime_t 	gethrtime(void);
3607c478bdstevel@tonic-gateextern	hrtime_t 	gethrtime_unscaled(void);
3617c478bdstevel@tonic-gateextern	hrtime_t	gethrtime_max(void);
3627c478bdstevel@tonic-gateextern	hrtime_t	gethrtime_waitfree(void);
3637c478bdstevel@tonic-gateextern	void		scalehrtime(hrtime_t *);
364113b131Eric Saxeextern	uint64_t	unscalehrtime(hrtime_t);
3657c478bdstevel@tonic-gateextern	void 		gethrestime(timespec_t *);
3667c478bdstevel@tonic-gateextern	time_t 		gethrestime_sec(void);
3677c478bdstevel@tonic-gateextern	void		gethrestime_lasttick(timespec_t *);
3687c478bdstevel@tonic-gateextern	void		hrt2ts(hrtime_t, timestruc_t *);
3697c478bdstevel@tonic-gateextern	hrtime_t	ts2hrt(const timestruc_t *);
3707c478bdstevel@tonic-gateextern	void		hrt2tv(hrtime_t, struct timeval *);
3717c478bdstevel@tonic-gateextern	hrtime_t	tv2hrt(struct timeval *);
3727c478bdstevel@tonic-gateextern	int		itimerfix(struct timeval *, int);
3737c478bdstevel@tonic-gateextern	int		itimerdecr(struct itimerval *, int);
3747c478bdstevel@tonic-gateextern	void		timevaladd(struct timeval *, struct timeval *);
3757c478bdstevel@tonic-gateextern	void		timevalsub(struct timeval *, struct timeval *);
3767c478bdstevel@tonic-gateextern	void		timevalfix(struct timeval *);
3777c478bdstevel@tonic-gateextern	void		dtrace_hres_tick(void);
3787c478bdstevel@tonic-gate
379d3d5073Rafael Vanoniextern clock_t		ddi_get_lbolt(void);
380d3d5073Rafael Vanoniextern int64_t		ddi_get_lbolt64(void);
381d3d5073Rafael Vanoni
3827c478bdstevel@tonic-gate#if defined(_SYSCALL32)
3837c478bdstevel@tonic-gateextern	void		hrt2ts32(hrtime_t, timestruc32_t *);
3847c478bdstevel@tonic-gate#endif
3857c478bdstevel@tonic-gate
3867c478bdstevel@tonic-gate#endif /* _KERNEL */
3877c478bdstevel@tonic-gate
3887c478bdstevel@tonic-gate#if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
3897c478bdstevel@tonic-gateint adjtime(struct timeval *, struct timeval *);
3907c478bdstevel@tonic-gate#endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
3917c478bdstevel@tonic-gate
3927c478bdstevel@tonic-gate#if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || \
3937c478bdstevel@tonic-gate	defined(_ATFILE_SOURCE) || defined(__EXTENSIONS__)
3947c478bdstevel@tonic-gateint futimesat(int, const char *, const struct timeval *);
3957c478bdstevel@tonic-gate#endif /* defined(__ATFILE_SOURCE) */
3967c478bdstevel@tonic-gate
3977c478bdstevel@tonic-gate#if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
3987c478bdstevel@tonic-gate	defined(__EXTENSIONS__)
3997c478bdstevel@tonic-gate
4007c478bdstevel@tonic-gateint getitimer(int, struct itimerval *);
4017c478bdstevel@tonic-gateint utimes(const char *, const struct timeval *);
4027c478bdstevel@tonic-gate#if defined(_XPG4_2)
4037c478bdstevel@tonic-gateint setitimer(int, const struct itimerval *_RESTRICT_KYWD,
4047c478bdstevel@tonic-gate	struct itimerval *_RESTRICT_KYWD);
4057c478bdstevel@tonic-gate#else
4067c478bdstevel@tonic-gateint setitimer(int, struct itimerval *_RESTRICT_KYWD,
4077c478bdstevel@tonic-gate	struct itimerval *_RESTRICT_KYWD);
4087c478bdstevel@tonic-gate#endif /* defined(_XPG2_2) */
4097c478bdstevel@tonic-gate
4107c478bdstevel@tonic-gate#endif /* !defined(_KERNEL) ... defined(_XPG4_2) */
4117c478bdstevel@tonic-gate
4127c478bdstevel@tonic-gate/*
4137c478bdstevel@tonic-gate * gettimeofday() and settimeofday() were included in SVr4 due to their
4147c478bdstevel@tonic-gate * common use in BSD based applications.  They were to be included exactly
4157c478bdstevel@tonic-gate * as in BSD, with two parameters.  However, AT&T/USL noted that the second
4167c478bdstevel@tonic-gate * parameter was unused and deleted it, thereby making a routine included
4177c478bdstevel@tonic-gate * for compatibility, incompatible.
4187c478bdstevel@tonic-gate *
4197c478bdstevel@tonic-gate * XSH4.2 (spec 1170) defines gettimeofday and settimeofday to have two
4207c478bdstevel@tonic-gate * parameters.
4217c478bdstevel@tonic-gate *
4227c478bdstevel@tonic-gate * This has caused general disagreement in the application community as to
4237c478bdstevel@tonic-gate * the syntax of these routines.  Solaris defaults to the XSH4.2 definition.
4247c478bdstevel@tonic-gate * The flag _SVID_GETTOD may be used to force the SVID version.
4257c478bdstevel@tonic-gate */
4267c478bdstevel@tonic-gate#if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
4277c478bdstevel@tonic-gate
4287c478bdstevel@tonic-gate#if defined(_SVID_GETTOD)
4297c478bdstevel@tonic-gateint settimeofday(struct timeval *);
4307c478bdstevel@tonic-gate#else
4317c478bdstevel@tonic-gateint settimeofday(struct timeval *, void *);
4327c478bdstevel@tonic-gate#endif
4337c478bdstevel@tonic-gatehrtime_t	gethrtime(void);
4347c478bdstevel@tonic-gatehrtime_t	gethrvtime(void);
4357c478bdstevel@tonic-gate
4367c478bdstevel@tonic-gate#endif /* !(defined _KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
4377c478bdstevel@tonic-gate
4387c478bdstevel@tonic-gate#if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
4397c478bdstevel@tonic-gate	defined(__EXTENSIONS__)
4407c478bdstevel@tonic-gate
4417c478bdstevel@tonic-gate#if defined(_SVID_GETTOD)
4427c478bdstevel@tonic-gateint gettimeofday(struct timeval *);
4437c478bdstevel@tonic-gate#else
4447c478bdstevel@tonic-gateint gettimeofday(struct timeval *_RESTRICT_KYWD, void *_RESTRICT_KYWD);
4457c478bdstevel@tonic-gate#endif
4467c478bdstevel@tonic-gate
4477c478bdstevel@tonic-gate#endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
4487c478bdstevel@tonic-gate
4497c478bdstevel@tonic-gate/*
4507c478bdstevel@tonic-gate * The inclusion of <time.h> is historical and was added for
4517c478bdstevel@tonic-gate * backward compatibility in delta 1.2 when a number of definitions
4527c478bdstevel@tonic-gate * were moved out of <sys/time.h>.  More recently, the timespec and
4537c478bdstevel@tonic-gate * itimerspec structure definitions, along with the _CLOCK_*, CLOCK_*,
4547c478bdstevel@tonic-gate * _TIMER_*, and TIMER_* symbols were moved to <sys/time_impl.h>,
4557c478bdstevel@tonic-gate * which is now included by <time.h>.  This change was due to POSIX
4567c478bdstevel@tonic-gate * 1003.1b-1993 and X/Open UNIX 98 requirements.  For non-POSIX and
4577c478bdstevel@tonic-gate * non-X/Open applications, including this header will still make
4587c478bdstevel@tonic-gate * visible these definitions.
4597c478bdstevel@tonic-gate */
460b819ceaGordon Ross#if !defined(_BOOT) && !defined(_KERNEL) && !defined(_FAKE_KERNEL) && \
461ae115bcmrj	!defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
4627c478bdstevel@tonic-gate#include <time.h>
4637c478bdstevel@tonic-gate#endif
4647c478bdstevel@tonic-gate
4657c478bdstevel@tonic-gate/*
4667c478bdstevel@tonic-gate * The inclusion of <sys/select.h> is needed for the FD_CLR,
4677c478bdstevel@tonic-gate * FD_ISSET, FD_SET, and FD_SETSIZE macros as well as the
4687c478bdstevel@tonic-gate * select() prototype defined in the XOpen specifications
4697c478bdstevel@tonic-gate * beginning with XSH4v2.  Placement required after definition
4707c478bdstevel@tonic-gate * for itimerval.
4717c478bdstevel@tonic-gate */
472b819ceaGordon Ross#if !defined(_KERNEL) && !defined(_FAKE_KERNEL) && \
473b819ceaGordon Ross	!defined(__XOPEN_OR_POSIX) || \
474b819ceaGordon Ross	defined(_XPG4_2) || defined(__EXTENSIONS__)
4757c478bdstevel@tonic-gate#include <sys/select.h>
4767c478bdstevel@tonic-gate#endif
4777c478bdstevel@tonic-gate
4787c478bdstevel@tonic-gate#endif	/* _ASM */
4797c478bdstevel@tonic-gate
4807c478bdstevel@tonic-gate#ifdef	__cplusplus
4817c478bdstevel@tonic-gate}
4827c478bdstevel@tonic-gate#endif
4837c478bdstevel@tonic-gate
4847c478bdstevel@tonic-gate#endif	/* _SYS_TIME_H */
485