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 29 #ifndef _LINUXKPI_LINUX_KTIME_H 30 #define _LINUXKPI_LINUX_KTIME_H 31 32 #include <linux/types.h> 33 #include <linux/time.h> 34 #include <linux/jiffies.h> 35 36 /* time values in nanoseconds */ 37 typedef s64 ktime_t; 38 39 #define KTIME_MAX ((s64)~((u64)1 << 63)) 40 #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) 41 42 static inline int64_t 43 ktime_to_ns(ktime_t kt) 44 { 45 return (kt); 46 } 47 48 static inline ktime_t 49 ns_to_ktime(uint64_t nsec) 50 { 51 return (nsec); 52 } 53 54 static inline int64_t 55 ktime_divns(const ktime_t kt, int64_t div) 56 { 57 return (kt / div); 58 } 59 60 static inline int64_t 61 ktime_to_us(ktime_t kt) 62 { 63 return (ktime_divns(kt, NSEC_PER_USEC)); 64 } 65 66 static inline int64_t 67 ktime_to_ms(ktime_t kt) 68 { 69 return (ktime_divns(kt, NSEC_PER_MSEC)); 70 } 71 72 static inline ktime_t 73 ms_to_ktime(uint64_t ms) 74 { 75 return (ms * NSEC_PER_MSEC); 76 } 77 78 static inline struct timeval 79 ktime_to_timeval(ktime_t kt) 80 { 81 return (ns_to_timeval(kt)); 82 } 83 84 static inline ktime_t 85 ktime_add_ns(ktime_t kt, int64_t ns) 86 { 87 return (kt + ns); 88 } 89 90 static inline ktime_t 91 ktime_add_ms(ktime_t kt, int64_t ms) 92 { 93 94 return (ktime_add_ns(kt, ms * NSEC_PER_MSEC)); 95 } 96 97 static inline ktime_t 98 ktime_add_us(ktime_t kt, int64_t us) 99 { 100 101 return (ktime_add_ns(kt, us * NSEC_PER_USEC)); 102 } 103 104 static inline ktime_t 105 ktime_sub_ns(ktime_t kt, int64_t ns) 106 { 107 return (kt - ns); 108 } 109 110 static inline ktime_t 111 ktime_set(const long secs, const unsigned long nsecs) 112 { 113 ktime_t retval = {(s64) secs * NSEC_PER_SEC + (s64) nsecs}; 114 115 return (retval); 116 } 117 118 static inline ktime_t 119 ktime_sub(ktime_t lhs, ktime_t rhs) 120 { 121 return (lhs - rhs); 122 } 123 124 static inline int64_t 125 ktime_us_delta(ktime_t later, ktime_t earlier) 126 { 127 ktime_t diff = ktime_sub(later, earlier); 128 129 return (ktime_to_us(diff)); 130 } 131 132 static inline int64_t 133 ktime_ms_delta(ktime_t later, ktime_t earlier) 134 { 135 ktime_t diff = ktime_sub(later, earlier); 136 137 return (ktime_to_ms(diff)); 138 } 139 140 static inline ktime_t 141 ktime_add(ktime_t lhs, ktime_t rhs) 142 { 143 return (lhs + rhs); 144 } 145 146 static inline int 147 ktime_compare(const ktime_t cmp1, const ktime_t cmp2) 148 { 149 150 if (cmp1 > cmp2) 151 return (1); 152 else if (cmp1 < cmp2) 153 return (-1); 154 else 155 return (0); 156 } 157 158 static inline bool 159 ktime_after(const ktime_t cmp1, const ktime_t cmp2) 160 { 161 162 return (ktime_compare(cmp1, cmp2) > 0); 163 } 164 165 static inline bool 166 ktime_before(const ktime_t cmp1, const ktime_t cmp2) 167 { 168 169 return (ktime_compare(cmp1, cmp2) < 0); 170 } 171 172 static inline ktime_t 173 timespec_to_ktime(struct timespec ts) 174 { 175 return (ktime_set(ts.tv_sec, ts.tv_nsec)); 176 } 177 178 static inline ktime_t 179 timeval_to_ktime(struct timeval tv) 180 { 181 return (ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC)); 182 } 183 184 static inline int64_t 185 timespec64_to_ns(struct timespec64 *ts) 186 { 187 return (timespec_to_ns(ts)); 188 } 189 190 #define ktime_to_timespec(kt) ns_to_timespec(kt) 191 #define ktime_to_timespec64(kt) ns_to_timespec(kt) 192 #define ktime_to_timeval(kt) ns_to_timeval(kt) 193 #define ktime_to_ns(kt) (kt) 194 #define ktime_get_ts(ts) getnanouptime(ts) 195 #define ktime_get_ts64(ts) getnanouptime(ts) 196 #define ktime_get_raw_ts64(ts) getnanouptime(ts) 197 #define ktime_get_real_ts64(ts) getnanotime(ts) 198 #define getrawmonotonic64(ts) getnanouptime(ts) 199 200 static inline int64_t 201 ktime_get_ns(void) 202 { 203 struct timespec ts; 204 205 ktime_get_ts(&ts); 206 207 return (ktime_to_ns(timespec_to_ktime(ts))); 208 } 209 210 static inline ktime_t 211 ktime_get(void) 212 { 213 struct timespec ts; 214 215 ktime_get_ts(&ts); 216 return (timespec_to_ktime(ts)); 217 } 218 219 static inline ktime_t 220 ktime_get_boottime(void) 221 { 222 struct timespec ts; 223 224 nanouptime(&ts); 225 return (timespec_to_ktime(ts)); 226 } 227 228 static inline uint64_t 229 ktime_get_boottime_ns(void) 230 { 231 232 return (ktime_to_ns(ktime_get_boottime())); 233 } 234 235 static inline uint64_t 236 ktime_get_boottime_seconds(void) 237 { 238 239 return (ktime_divns(ktime_get_boottime(), NSEC_PER_SEC)); 240 } 241 242 static inline ktime_t 243 ktime_get_real(void) 244 { 245 struct timespec ts; 246 247 nanotime(&ts); 248 return (timespec_to_ktime(ts)); 249 } 250 251 static inline ktime_t 252 ktime_get_real_seconds(void) 253 { 254 struct timespec ts; 255 256 nanotime(&ts); 257 return (ts.tv_sec); 258 } 259 260 static inline ktime_t 261 ktime_get_raw(void) 262 { 263 struct timespec ts; 264 265 nanouptime(&ts); 266 return (timespec_to_ktime(ts)); 267 } 268 269 static inline u64 270 ktime_get_raw_ns(void) 271 { 272 struct timespec ts; 273 274 nanouptime(&ts); 275 return (ktime_to_ns(timespec_to_ktime(ts))); 276 } 277 278 static inline uint64_t 279 ktime_get_raw_fast_ns(void) 280 { 281 struct timespec ts; 282 283 getnanouptime(&ts); 284 return (ktime_to_ns(timespec_to_ktime(ts))); 285 } 286 287 #endif /* _LINUXKPI_LINUX_KTIME_H */ 288