xref: /freebsd/sys/contrib/openzfs/lib/libspl/include/sys/time.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License, Version 1.0 only
7  * (the "License").  You may not use this file except in compliance
8  * with the License.
9  *
10  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11  * or https://opensource.org/licenses/CDDL-1.0.
12  * See the License for the specific language governing permissions
13  * and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL HEADER in each
16  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17  * If applicable, add the following below this CDDL HEADER, with the
18  * fields enclosed by brackets "[]" replaced with your own identifying
19  * information: Portions Copyright [yyyy] [name of copyright owner]
20  *
21  * CDDL HEADER END
22  */
23 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #ifndef _LIBSPL_SYS_TIME_H
29 #define	_LIBSPL_SYS_TIME_H
30 
31 #include <time.h>
32 #include <sys/types.h>
33 #include_next <sys/time.h>
34 
35 #ifndef SEC
36 #define	SEC		1
37 #endif
38 
39 #ifndef MILLISEC
40 #define	MILLISEC	1000
41 #endif
42 
43 #ifndef MICROSEC
44 #define	MICROSEC	1000000
45 #endif
46 
47 #ifndef NANOSEC
48 #define	NANOSEC		1000000000
49 #endif
50 
51 #ifndef NSEC_PER_USEC
52 #define	NSEC_PER_USEC	1000L
53 #endif
54 
55 #ifndef MSEC2NSEC
56 #define	MSEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / MILLISEC))
57 #endif
58 
59 #ifndef NSEC2MSEC
60 #define	NSEC2MSEC(n)	((n) / (NANOSEC / MILLISEC))
61 #endif
62 
63 #ifndef USEC2NSEC
64 #define	USEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / MICROSEC))
65 #endif
66 
67 #ifndef NSEC2USEC
68 #define	NSEC2USEC(n)	((n) / (NANOSEC / MICROSEC))
69 #endif
70 
71 #ifndef NSEC2SEC
72 #define	NSEC2SEC(n)	((n) / (NANOSEC / SEC))
73 #endif
74 
75 #ifndef SEC2NSEC
76 #define	SEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / SEC))
77 #endif
78 
79 typedef	long long		hrtime_t;
80 typedef	struct timespec		timespec_t;
81 typedef struct timespec		inode_timespec_t;
82 
83 static inline void
gethrestime(inode_timespec_t * ts)84 gethrestime(inode_timespec_t *ts)
85 {
86 	struct timeval tv;
87 	(void) gettimeofday(&tv, NULL);
88 	ts->tv_sec = tv.tv_sec;
89 	ts->tv_nsec = tv.tv_usec * NSEC_PER_USEC;
90 }
91 
92 static inline uint64_t
gethrestime_sec(void)93 gethrestime_sec(void)
94 {
95 	struct timeval tv;
96 	(void) gettimeofday(&tv, NULL);
97 	return (tv.tv_sec);
98 }
99 
100 static inline hrtime_t
gethrtime(void)101 gethrtime(void)
102 {
103 	struct timespec ts;
104 	(void) clock_gettime(CLOCK_MONOTONIC, &ts);
105 	return ((((uint64_t)ts.tv_sec) * NANOSEC) + ts.tv_nsec);
106 }
107 
108 #endif /* _LIBSPL_SYS_TIME_H */
109