ktime.h revision 337894
1270710Shselasky/*-
2329977Shselasky * Copyright (c) 2018 Limelight Networks, Inc.
3329977Shselasky * Copyright (c) 2014-2018 Mellanox Technologies, Ltd.
4289572Shselasky * Copyright (c) 2015 Fran��ois Tigeot
5270710Shselasky * All rights reserved.
6270710Shselasky *
7270710Shselasky * Redistribution and use in source and binary forms, with or without
8270710Shselasky * modification, are permitted provided that the following conditions
9270710Shselasky * are met:
10270710Shselasky * 1. Redistributions of source code must retain the above copyright
11270710Shselasky *    notice unmodified, this list of conditions, and the following
12270710Shselasky *    disclaimer.
13270710Shselasky * 2. Redistributions in binary form must reproduce the above copyright
14270710Shselasky *    notice, this list of conditions and the following disclaimer in the
15270710Shselasky *    documentation and/or other materials provided with the distribution.
16270710Shselasky *
17270710Shselasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18270710Shselasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19270710Shselasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20270710Shselasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21270710Shselasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22270710Shselasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23270710Shselasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24270710Shselasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25270710Shselasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26270710Shselasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27289644Shselasky *
28289644Shselasky * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/ktime.h 337894 2018-08-16 08:08:30Z hselasky $
29270710Shselasky */
30328653Shselasky
31270710Shselasky#ifndef _LINUX_KTIME_H
32328653Shselasky#define	_LINUX_KTIME_H
33270710Shselasky
34270710Shselasky#include <linux/types.h>
35289572Shselasky#include <linux/time.h>
36270710Shselasky#include <linux/jiffies.h>
37270710Shselasky
38329977Shselasky#define	ktime_get_ts(x) getnanouptime(x)
39270710Shselasky
40289572Shselasky/* time values in nanoseconds */
41329977Shselaskytypedef s64 ktime_t;
42270710Shselasky
43331756Semaste#define	KTIME_MAX			((s64)~((u64)1 << 63))
44331756Semaste#define	KTIME_SEC_MAX			(KTIME_MAX / NSEC_PER_SEC)
45270710Shselasky
46289572Shselaskystatic inline int64_t
47289572Shselaskyktime_to_ns(ktime_t kt)
48270710Shselasky{
49329977Shselasky	return (kt);
50270710Shselasky}
51270710Shselasky
52328653Shselaskystatic inline ktime_t
53328653Shselaskyns_to_ktime(uint64_t nsec)
54328653Shselasky{
55329977Shselasky	return (nsec);
56328653Shselasky}
57328653Shselasky
58300498Shselaskystatic inline int64_t
59300498Shselaskyktime_divns(const ktime_t kt, int64_t div)
60300498Shselasky{
61329977Shselasky	return (kt / div);
62300498Shselasky}
63300498Shselasky
64300498Shselaskystatic inline int64_t
65300498Shselaskyktime_to_us(ktime_t kt)
66300498Shselasky{
67329977Shselasky	return (ktime_divns(kt, NSEC_PER_USEC));
68300498Shselasky}
69300498Shselasky
70300498Shselaskystatic inline int64_t
71300498Shselaskyktime_to_ms(ktime_t kt)
72300498Shselasky{
73329977Shselasky	return (ktime_divns(kt, NSEC_PER_MSEC));
74300498Shselasky}
75300498Shselasky
76289572Shselaskystatic inline struct timeval
77289572Shselaskyktime_to_timeval(ktime_t kt)
78270710Shselasky{
79329977Shselasky	return (ns_to_timeval(kt));
80270710Shselasky}
81270710Shselasky
82289572Shselaskystatic inline ktime_t
83289572Shselaskyktime_add_ns(ktime_t kt, int64_t ns)
84270710Shselasky{
85329977Shselasky	return (kt + ns);
86270710Shselasky}
87270710Shselasky
88289572Shselaskystatic inline ktime_t
89337894Shselaskyktime_add_ms(ktime_t kt, int64_t ms)
90337894Shselasky{
91337894Shselasky
92337894Shselasky	return (ktime_add_ns(kt, ms * NSEC_PER_MSEC));
93337894Shselasky}
94337894Shselasky
95337894Shselaskystatic inline ktime_t
96289572Shselaskyktime_sub_ns(ktime_t kt, int64_t ns)
97270710Shselasky{
98329977Shselasky	return (kt - ns);
99270710Shselasky}
100270710Shselasky
101289572Shselaskystatic inline ktime_t
102289572Shselaskyktime_set(const long secs, const unsigned long nsecs)
103270710Shselasky{
104329977Shselasky	ktime_t retval = {(s64) secs * NSEC_PER_SEC + (s64) nsecs};
105329977Shselasky
106289572Shselasky	return (retval);
107270710Shselasky}
108270710Shselasky
109289572Shselaskystatic inline ktime_t
110289572Shselaskyktime_sub(ktime_t lhs, ktime_t rhs)
111270710Shselasky{
112329977Shselasky	return (lhs - rhs);
113270710Shselasky}
114270710Shselasky
115300498Shselaskystatic inline int64_t
116300498Shselaskyktime_us_delta(ktime_t later, ktime_t earlier)
117300498Shselasky{
118329977Shselasky	ktime_t diff = ktime_sub(later, earlier);
119329977Shselasky
120329977Shselasky	return (ktime_to_us(diff));
121300498Shselasky}
122300498Shselasky
123300498Shselaskystatic inline int64_t
124300498Shselaskyktime_ms_delta(ktime_t later, ktime_t earlier)
125300498Shselasky{
126329977Shselasky	ktime_t diff = ktime_sub(later, earlier);
127329977Shselasky
128329977Shselasky	return (ktime_to_ms(diff));
129300498Shselasky}
130300498Shselasky
131289572Shselaskystatic inline ktime_t
132289572Shselaskyktime_add(ktime_t lhs, ktime_t rhs)
133270710Shselasky{
134329977Shselasky	return (lhs + rhs);
135270710Shselasky}
136270710Shselasky
137335421Shselaskystatic inline int
138335421Shselaskyktime_compare(const ktime_t cmp1, const ktime_t cmp2)
139335421Shselasky{
140335421Shselasky
141335421Shselasky	if (cmp1 > cmp2)
142335421Shselasky		return (1);
143335421Shselasky	else if (cmp1 < cmp2)
144335421Shselasky		return (-1);
145335421Shselasky	else
146335421Shselasky		return (0);
147335421Shselasky}
148335421Shselasky
149335421Shselaskystatic inline bool
150335421Shselaskyktime_after(const ktime_t cmp1, const ktime_t cmp2)
151335421Shselasky{
152335421Shselasky
153335421Shselasky	return (ktime_compare(cmp1, cmp2) > 0);
154335421Shselasky}
155335421Shselasky
156337894Shselaskystatic inline bool
157337894Shselaskyktime_before(const ktime_t cmp1, const ktime_t cmp2)
158337894Shselasky{
159337894Shselasky
160337894Shselasky	return (ktime_compare(cmp1, cmp2) < 0);
161337894Shselasky}
162337894Shselasky
163289572Shselaskystatic inline ktime_t
164289572Shselaskytimespec_to_ktime(struct timespec ts)
165270710Shselasky{
166289572Shselasky	return (ktime_set(ts.tv_sec, ts.tv_nsec));
167270710Shselasky}
168270710Shselasky
169289572Shselaskystatic inline ktime_t
170289572Shselaskytimeval_to_ktime(struct timeval tv)
171270710Shselasky{
172289572Shselasky	return (ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC));
173270710Shselasky}
174270710Shselasky
175329977Shselasky#define	ktime_to_timespec(kt)		ns_to_timespec(kt)
176335429Shselasky#define	ktime_to_timespec64(kt)		ns_to_timespec(kt)
177329977Shselasky#define	ktime_to_timeval(kt)		ns_to_timeval(kt)
178329977Shselasky#define	ktime_to_ns(kt)			(kt)
179270710Shselasky
180328653Shselaskystatic inline int64_t
181289572Shselaskyktime_get_ns(void)
182277396Shselasky{
183277396Shselasky	struct timespec ts;
184289572Shselasky
185277396Shselasky	ktime_get_ts(&ts);
186329977Shselasky
187329977Shselasky	return (ktime_to_ns(timespec_to_ktime(ts)));
188277396Shselasky}
189277396Shselasky
190289572Shselaskystatic inline ktime_t
191289572Shselaskyktime_get(void)
192289572Shselasky{
193289572Shselasky	struct timespec ts;
194289572Shselasky
195289572Shselasky	ktime_get_ts(&ts);
196289572Shselasky	return (timespec_to_ktime(ts));
197289572Shselasky}
198289572Shselasky
199328653Shselaskystatic inline ktime_t
200328653Shselaskyktime_get_boottime(void)
201328653Shselasky{
202328653Shselasky	struct timespec ts;
203328653Shselasky
204328653Shselasky	nanouptime(&ts);
205328653Shselasky	return (timespec_to_ktime(ts));
206328653Shselasky}
207328653Shselasky
208328653Shselaskystatic inline ktime_t
209328653Shselaskyktime_get_real(void)
210328653Shselasky{
211328653Shselasky	struct timespec ts;
212328653Shselasky
213328653Shselasky	nanotime(&ts);
214328653Shselasky	return (timespec_to_ktime(ts));
215328653Shselasky}
216328653Shselasky
217329977Shselaskystatic inline ktime_t
218329977Shselaskyktime_get_real_seconds(void)
219329977Shselasky{
220329977Shselasky	struct timespec ts;
221329977Shselasky
222329977Shselasky	nanotime(&ts);
223329977Shselasky	return (ts.tv_sec);
224329977Shselasky}
225329977Shselasky
226330848Shselaskystatic inline ktime_t
227330848Shselaskyktime_get_raw(void)
228330848Shselasky{
229330848Shselasky	struct timespec ts;
230330848Shselasky
231330848Shselasky	nanotime(&ts);
232330848Shselasky	return (timespec_to_ktime(ts));
233330848Shselasky}
234330848Shselasky
235329977Shselaskystatic inline u64
236329977Shselaskyktime_get_raw_ns(void)
237329977Shselasky{
238329977Shselasky	struct timespec ts;
239329977Shselasky
240329977Shselasky	nanouptime(&ts);
241329977Shselasky	return (ktime_to_ns(timespec_to_ktime(ts)));
242329977Shselasky}
243329977Shselasky
244328653Shselasky#endif /* _LINUX_KTIME_H */
245