ktime.h revision 337894
1/*-
2 * Copyright (c) 2018 Limelight Networks, Inc.
3 * Copyright (c) 2014-2018 Mellanox Technologies, Ltd.
4 * Copyright (c) 2015 Fran��ois Tigeot
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice unmodified, this list of conditions, and the following
12 *    disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/ktime.h 337894 2018-08-16 08:08:30Z hselasky $
29 */
30
31#ifndef _LINUX_KTIME_H
32#define	_LINUX_KTIME_H
33
34#include <linux/types.h>
35#include <linux/time.h>
36#include <linux/jiffies.h>
37
38#define	ktime_get_ts(x) getnanouptime(x)
39
40/* time values in nanoseconds */
41typedef s64 ktime_t;
42
43#define	KTIME_MAX			((s64)~((u64)1 << 63))
44#define	KTIME_SEC_MAX			(KTIME_MAX / NSEC_PER_SEC)
45
46static inline int64_t
47ktime_to_ns(ktime_t kt)
48{
49	return (kt);
50}
51
52static inline ktime_t
53ns_to_ktime(uint64_t nsec)
54{
55	return (nsec);
56}
57
58static inline int64_t
59ktime_divns(const ktime_t kt, int64_t div)
60{
61	return (kt / div);
62}
63
64static inline int64_t
65ktime_to_us(ktime_t kt)
66{
67	return (ktime_divns(kt, NSEC_PER_USEC));
68}
69
70static inline int64_t
71ktime_to_ms(ktime_t kt)
72{
73	return (ktime_divns(kt, NSEC_PER_MSEC));
74}
75
76static inline struct timeval
77ktime_to_timeval(ktime_t kt)
78{
79	return (ns_to_timeval(kt));
80}
81
82static inline ktime_t
83ktime_add_ns(ktime_t kt, int64_t ns)
84{
85	return (kt + ns);
86}
87
88static inline ktime_t
89ktime_add_ms(ktime_t kt, int64_t ms)
90{
91
92	return (ktime_add_ns(kt, ms * NSEC_PER_MSEC));
93}
94
95static inline ktime_t
96ktime_sub_ns(ktime_t kt, int64_t ns)
97{
98	return (kt - ns);
99}
100
101static inline ktime_t
102ktime_set(const long secs, const unsigned long nsecs)
103{
104	ktime_t retval = {(s64) secs * NSEC_PER_SEC + (s64) nsecs};
105
106	return (retval);
107}
108
109static inline ktime_t
110ktime_sub(ktime_t lhs, ktime_t rhs)
111{
112	return (lhs - rhs);
113}
114
115static inline int64_t
116ktime_us_delta(ktime_t later, ktime_t earlier)
117{
118	ktime_t diff = ktime_sub(later, earlier);
119
120	return (ktime_to_us(diff));
121}
122
123static inline int64_t
124ktime_ms_delta(ktime_t later, ktime_t earlier)
125{
126	ktime_t diff = ktime_sub(later, earlier);
127
128	return (ktime_to_ms(diff));
129}
130
131static inline ktime_t
132ktime_add(ktime_t lhs, ktime_t rhs)
133{
134	return (lhs + rhs);
135}
136
137static inline int
138ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
139{
140
141	if (cmp1 > cmp2)
142		return (1);
143	else if (cmp1 < cmp2)
144		return (-1);
145	else
146		return (0);
147}
148
149static inline bool
150ktime_after(const ktime_t cmp1, const ktime_t cmp2)
151{
152
153	return (ktime_compare(cmp1, cmp2) > 0);
154}
155
156static inline bool
157ktime_before(const ktime_t cmp1, const ktime_t cmp2)
158{
159
160	return (ktime_compare(cmp1, cmp2) < 0);
161}
162
163static inline ktime_t
164timespec_to_ktime(struct timespec ts)
165{
166	return (ktime_set(ts.tv_sec, ts.tv_nsec));
167}
168
169static inline ktime_t
170timeval_to_ktime(struct timeval tv)
171{
172	return (ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC));
173}
174
175#define	ktime_to_timespec(kt)		ns_to_timespec(kt)
176#define	ktime_to_timespec64(kt)		ns_to_timespec(kt)
177#define	ktime_to_timeval(kt)		ns_to_timeval(kt)
178#define	ktime_to_ns(kt)			(kt)
179
180static inline int64_t
181ktime_get_ns(void)
182{
183	struct timespec ts;
184
185	ktime_get_ts(&ts);
186
187	return (ktime_to_ns(timespec_to_ktime(ts)));
188}
189
190static inline ktime_t
191ktime_get(void)
192{
193	struct timespec ts;
194
195	ktime_get_ts(&ts);
196	return (timespec_to_ktime(ts));
197}
198
199static inline ktime_t
200ktime_get_boottime(void)
201{
202	struct timespec ts;
203
204	nanouptime(&ts);
205	return (timespec_to_ktime(ts));
206}
207
208static inline ktime_t
209ktime_get_real(void)
210{
211	struct timespec ts;
212
213	nanotime(&ts);
214	return (timespec_to_ktime(ts));
215}
216
217static inline ktime_t
218ktime_get_real_seconds(void)
219{
220	struct timespec ts;
221
222	nanotime(&ts);
223	return (ts.tv_sec);
224}
225
226static inline ktime_t
227ktime_get_raw(void)
228{
229	struct timespec ts;
230
231	nanotime(&ts);
232	return (timespec_to_ktime(ts));
233}
234
235static inline u64
236ktime_get_raw_ns(void)
237{
238	struct timespec ts;
239
240	nanouptime(&ts);
241	return (ktime_to_ns(timespec_to_ktime(ts)));
242}
243
244#endif /* _LINUX_KTIME_H */
245