xref: /freebsd/sys/contrib/openzfs/include/os/freebsd/spl/sys/time.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef _OPENSOLARIS_SYS_TIME_H_
29 #define	_OPENSOLARIS_SYS_TIME_H_
30 #pragma once
31 #include_next <sys/time.h>
32 #include <sys/debug.h>
33 #ifndef _SYS_KERNEL_H_
34 extern int hz;
35 #endif
36 
37 #define	SEC		1
38 #define	MILLISEC	1000UL
39 #define	MICROSEC	1000000UL
40 #define	NANOSEC	1000000000UL
41 #define	TIME_MAX	LLONG_MAX
42 
43 #define	MSEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / MILLISEC))
44 #define	NSEC2MSEC(n)	((n) / (NANOSEC / MILLISEC))
45 
46 #define	USEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / MICROSEC))
47 #define	NSEC2USEC(n)	((n) / (NANOSEC / MICROSEC))
48 
49 #define	NSEC2SEC(n)	((n) / (NANOSEC / SEC))
50 #define	SEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / SEC))
51 
52 typedef longlong_t	hrtime_t;
53 
54 #if defined(__i386__) || defined(__powerpc__)
55 #define	TIMESPEC_OVERFLOW(ts)						\
56 	((ts)->tv_sec < INT32_MIN || (ts)->tv_sec > INT32_MAX)
57 #else
58 #define	TIMESPEC_OVERFLOW(ts)						\
59 	((ts)->tv_sec < INT64_MIN || (ts)->tv_sec > INT64_MAX)
60 #endif
61 
62 #define	SEC_TO_TICK(sec)	((sec) * hz)
63 #define	NSEC_TO_TICK(nsec)	((nsec) / (NANOSEC / hz))
64 
65 static __inline hrtime_t
gethrtime(void)66 gethrtime(void)
67 {
68 	struct timespec ts;
69 	hrtime_t nsec;
70 
71 	nanouptime(&ts);
72 	nsec = ((hrtime_t)ts.tv_sec * NANOSEC) + ts.tv_nsec;
73 	return (nsec);
74 }
75 
76 #define	gethrestime_sec()	(time_second)
77 #define	gethrestime(ts)		getnanotime(ts)
78 #define	gethrtime_waitfree()	gethrtime()
79 
80 extern int nsec_per_tick;	/* nanoseconds per clock tick */
81 
82 #define	ddi_get_lbolt64()				\
83 	(int64_t)(((getsbinuptime() >> 16) * hz) >> 16)
84 #define	ddi_get_lbolt()		(clock_t)ddi_get_lbolt64()
85 
86 #else
87 
88 static __inline hrtime_t
gethrtime(void)89 gethrtime(void)
90 {
91 	struct timespec ts;
92 	clock_gettime(CLOCK_UPTIME, &ts);
93 	return (((uint64_t)ts.tv_sec) * NANOSEC + ts.tv_nsec);
94 }
95 #endif	/* !_OPENSOLARIS_SYS_TIME_H_ */
96