ktime.h revision 1.6 1 /* $NetBSD: ktime.h,v 1.6 2018/08/27 07:27:27 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _LINUX_KTIME_H_
33 #define _LINUX_KTIME_H_
34
35 #include <sys/types.h>
36 #include <sys/endian.h>
37 #include <sys/time.h>
38
39 #include <linux/time.h>
40
41 union ktime {
42 int64_t kt_nsec;
43 };
44
45 typedef union ktime ktime_t;
46
47 static inline int64_t
48 ktime_to_ns(ktime_t kt)
49 {
50 return kt.kt_nsec;
51 }
52
53 static inline int64_t
54 ktime_to_us(ktime_t kt)
55 {
56 return ktime_to_ns(kt)/1000;
57 }
58
59 static inline ktime_t
60 ns_to_ktime(int64_t nsec)
61 {
62 return (ktime_t) { .kt_nsec = nsec };
63 }
64
65 static inline ktime_t
66 ktime_add_ns(ktime_t kt, int64_t nsec)
67 {
68 return ns_to_ktime(ktime_to_ns(kt) + nsec);
69 }
70
71 static inline ktime_t
72 ktime_sub(ktime_t a, ktime_t b)
73 {
74 return ns_to_ktime(ktime_to_ns(a) - ktime_to_ns(b));
75 }
76
77 static inline ktime_t
78 ktime_sub_ns(ktime_t a, int64_t nsec)
79 {
80 return ns_to_ktime(ktime_to_ns(a) - nsec);
81 }
82
83 static inline struct timespec
84 ktime_to_timespec(ktime_t kt)
85 {
86 return ns_to_timespec(ktime_to_ns(kt));
87 }
88
89 static inline struct timeval
90 ktime_to_timeval(ktime_t kt)
91 {
92 return ns_to_timeval(ktime_to_ns(kt));
93 }
94
95 static inline ktime_t
96 timespec_to_ktime(struct timespec ts)
97 {
98 /* XXX Silently truncate? */
99 return ns_to_ktime(1000000000*(int64_t)ts.tv_sec + ts.tv_nsec);
100 }
101
102 static inline ktime_t
103 ktime_get(void)
104 {
105 struct timespec ts;
106
107 nanouptime(&ts);
108
109 return timespec_to_ktime(ts);
110 }
111
112 static inline ktime_t
113 ktime_get_real(void)
114 {
115 struct timespec ts;
116
117 nanotime(&ts);
118
119 return timespec_to_ktime(ts);
120 }
121
122 static inline uint64_t
123 ktime_get_raw_ns(void)
124 {
125
126 /* XXX */
127 return ktime_to_ns(ktime_get());
128 }
129
130 static inline ktime_t
131 ktime_get_monotonic_offset(void)
132 {
133 return timespec_to_ktime(boottime);
134 }
135
136 static inline ktime_t
137 ktime_mono_to_real(ktime_t kt)
138 {
139 struct timespec ts = ktime_to_timespec(kt);
140
141 timespecadd(&ts, &boottime, &ts);
142
143 return timespec_to_ktime(ts);
144 }
145
146 static inline int64_t
147 ktime_us_delta(ktime_t a, ktime_t b)
148 {
149 return ktime_to_us(ktime_sub(a, b));
150 }
151
152 static inline bool
153 time_in_range(unsigned long x, unsigned long a, unsigned long b)
154 {
155 return ((a <= x) && (x <= b));
156 }
157
158 #endif /* _LINUX_KTIME_H_ */
159