2 * Copyright (c) 2018 Limelight Networks, Inc.
3 * Copyright (c) 2014-2018 Mellanox Technologies, Ltd.
4 * Copyright (c) 2015 François Tigeot
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
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.
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.
31 #ifndef _LINUX_KTIME_H
32 #define _LINUX_KTIME_H
34 #include <linux/types.h>
35 #include <linux/time.h>
36 #include <linux/jiffies.h>
38 #define ktime_get_ts(x) getnanouptime(x)
40 /* time values in nanoseconds */
43 #define KTIME_MAX ((s64)~((u64)1 << 63))
44 #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
47 ktime_to_ns(ktime_t kt)
53 ns_to_ktime(uint64_t nsec)
59 ktime_divns(const ktime_t kt, int64_t div)
65 ktime_to_us(ktime_t kt)
67 return (ktime_divns(kt, NSEC_PER_USEC));
71 ktime_to_ms(ktime_t kt)
73 return (ktime_divns(kt, NSEC_PER_MSEC));
76 static inline struct timeval
77 ktime_to_timeval(ktime_t kt)
79 return (ns_to_timeval(kt));
83 ktime_add_ns(ktime_t kt, int64_t ns)
89 ktime_sub_ns(ktime_t kt, int64_t ns)
95 ktime_set(const long secs, const unsigned long nsecs)
97 ktime_t retval = {(s64) secs * NSEC_PER_SEC + (s64) nsecs};
102 static inline ktime_t
103 ktime_sub(ktime_t lhs, ktime_t rhs)
108 static inline int64_t
109 ktime_us_delta(ktime_t later, ktime_t earlier)
111 ktime_t diff = ktime_sub(later, earlier);
113 return (ktime_to_us(diff));
116 static inline int64_t
117 ktime_ms_delta(ktime_t later, ktime_t earlier)
119 ktime_t diff = ktime_sub(later, earlier);
121 return (ktime_to_ms(diff));
124 static inline ktime_t
125 ktime_add(ktime_t lhs, ktime_t rhs)
130 static inline ktime_t
131 timespec_to_ktime(struct timespec ts)
133 return (ktime_set(ts.tv_sec, ts.tv_nsec));
136 static inline ktime_t
137 timeval_to_ktime(struct timeval tv)
139 return (ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC));
142 #define ktime_to_timespec(kt) ns_to_timespec(kt)
143 #define ktime_to_timeval(kt) ns_to_timeval(kt)
144 #define ktime_to_ns(kt) (kt)
146 static inline int64_t
153 return (ktime_to_ns(timespec_to_ktime(ts)));
156 static inline ktime_t
162 return (timespec_to_ktime(ts));
165 static inline ktime_t
166 ktime_get_boottime(void)
171 return (timespec_to_ktime(ts));
174 static inline ktime_t
180 return (timespec_to_ktime(ts));
183 static inline ktime_t
184 ktime_get_real_seconds(void)
192 static inline ktime_t
198 return (timespec_to_ktime(ts));
202 ktime_get_raw_ns(void)
207 return (ktime_to_ns(timespec_to_ktime(ts)));
210 #endif /* _LINUX_KTIME_H */