ktime.h revision 1.4 1 /* $NetBSD: ktime.h,v 1.4 2018/08/27 07:03:11 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 struct {
44 #if _BYTE_ORDER == _BIG_ENDIAN
45 int32_t ktsn_sec;
46 int32_t ktsn_nsec;
47 #else
48 int32_t ktsn_nsec;
49 int32_t ktsn_sec;
50 #endif
51 } kt_sec_nsec;
52 };
53
54 typedef union ktime ktime_t;
55
56 static inline ktime_t
57 ktime_add_ns(ktime_t kt, int64_t nsec)
58 {
59 return (ktime_t) { .kt_nsec = (kt.kt_nsec + nsec) };
60 }
61
62 static inline int64_t
63 ktime_to_ns(ktime_t kt)
64 {
65 return kt.kt_nsec;
66 }
67
68 static inline ktime_t
69 ns_to_ktime(int64_t nsec)
70 {
71 ktime_t kt;
72
73 kt.kt_nsec = nsec;
74
75 return kt;
76 }
77
78 static inline ktime_t
79 ktime_sub(ktime_t a, ktime_t b)
80 {
81 return ns_to_ktime(ktime_to_ns(a) - ktime_to_ns(b));
82 }
83
84 static inline ktime_t
85 ktime_sub_ns(ktime_t a, int64_t nsec)
86 {
87 return ns_to_ktime(ktime_to_ns(a) - nsec);
88 }
89
90 static inline struct timespec
91 ktime_to_timespec(ktime_t kt)
92 {
93 return ns_to_timespec(ktime_to_ns(kt));
94 }
95
96 static inline struct timeval
97 ktime_to_timeval(ktime_t kt)
98 {
99 return ns_to_timeval(ktime_to_ns(kt));
100 }
101
102 static inline ktime_t
103 timespec_to_ktime(struct timespec ts)
104 {
105 return ns_to_ktime(1000000000*ts.tv_sec + ts.tv_nsec);
106 }
107
108 static inline ktime_t
109 ktime_get(void)
110 {
111 struct timespec ts;
112 ktime_t kt;
113
114 nanouptime(&ts);
115
116 /* XXX Silently truncate? */
117 kt.kt_sec_nsec.ktsn_sec = ts.tv_sec & 0xffffffffUL;
118 kt.kt_sec_nsec.ktsn_nsec = ts.tv_nsec;
119
120 return kt;
121 }
122
123 static inline ktime_t
124 ktime_get_real(void)
125 {
126 struct timespec ts;
127 ktime_t kt;
128
129 nanotime(&ts);
130
131 /* XXX Silently truncate? */
132 kt.kt_sec_nsec.ktsn_sec = ts.tv_sec & 0xffffffffUL;
133 kt.kt_sec_nsec.ktsn_nsec = ts.tv_nsec;
134
135 return kt;
136 }
137
138 static inline ktime_t
139 ktime_get_monotonic_offset(void)
140 {
141 return timespec_to_ktime(boottime);
142 }
143
144 static inline ktime_t
145 ktime_mono_to_real(ktime_t kt)
146 {
147 struct timespec ts = ktime_to_timespec(kt);
148
149 timespecadd(&ts, &boottime, &ts);
150
151 return timespec_to_ktime(ts);
152 }
153
154 static inline bool
155 time_in_range(unsigned long x, unsigned long a, unsigned long b)
156 {
157 return ((a <= x) && (x <= b));
158 }
159
160 #endif /* _LINUX_KTIME_H_ */
161