1 /* $OpenBSD: ktime.h,v 1.7 2023/01/01 01:34:58 jsg Exp $ */ 2 /* 3 * Copyright (c) 2013, 2014, 2015 Mark Kettenis 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #ifndef _LINUX_KTIME_H 19 #define _LINUX_KTIME_H 20 21 #include <sys/time.h> 22 #include <linux/time.h> 23 #include <linux/jiffies.h> 24 25 typedef int64_t ktime_t; 26 #define KTIME_MAX INT64_MAX 27 28 static inline ktime_t 29 ktime_get(void) 30 { 31 struct timespec ts; 32 nanouptime(&ts); 33 return TIMESPEC_TO_NSEC(&ts); 34 } 35 36 static inline ktime_t 37 ktime_get_raw(void) 38 { 39 struct timespec ts; 40 nanouptime(&ts); 41 return TIMESPEC_TO_NSEC(&ts); 42 } 43 44 static inline int64_t 45 ktime_to_ms(ktime_t k) 46 { 47 return k / NSEC_PER_MSEC; 48 } 49 50 static inline int64_t 51 ktime_to_us(ktime_t k) 52 { 53 return k / NSEC_PER_USEC; 54 } 55 56 static inline int64_t 57 ktime_to_ns(ktime_t k) 58 { 59 return k; 60 } 61 62 static inline int64_t 63 ktime_get_raw_ns(void) 64 { 65 return ktime_to_ns(ktime_get_raw()); 66 } 67 68 static inline int64_t 69 ktime_get_raw_fast_ns(void) 70 { 71 return ktime_to_ns(ktime_get_raw()); 72 } 73 74 static inline struct timespec64 75 ktime_to_timespec64(ktime_t k) 76 { 77 struct timespec64 ts; 78 ts.tv_sec = k / NSEC_PER_SEC; 79 ts.tv_nsec = k % NSEC_PER_SEC; 80 if (ts.tv_nsec < 0) { 81 ts.tv_sec--; 82 ts.tv_nsec += NSEC_PER_SEC; 83 } 84 return ts; 85 } 86 87 static inline ktime_t 88 ktime_sub(ktime_t a, ktime_t b) 89 { 90 return a - b; 91 } 92 93 static inline ktime_t 94 ktime_add(ktime_t a, ktime_t b) 95 { 96 return a + b; 97 } 98 99 static inline ktime_t 100 ktime_add_us(ktime_t k, uint64_t us) 101 { 102 return k + (us * NSEC_PER_USEC); 103 } 104 105 static inline ktime_t 106 ktime_add_ms(ktime_t k, uint64_t ms) 107 { 108 return k + (ms * NSEC_PER_MSEC); 109 } 110 111 static inline ktime_t 112 ktime_add_ns(ktime_t k, int64_t ns) 113 { 114 return k + ns; 115 } 116 117 static inline ktime_t 118 ktime_sub_ns(ktime_t k, int64_t ns) 119 { 120 return k - ns; 121 } 122 123 static inline int64_t 124 ktime_us_delta(ktime_t a, ktime_t b) 125 { 126 return ktime_to_us(ktime_sub(a, b)); 127 } 128 129 static inline int64_t 130 ktime_ms_delta(ktime_t a, ktime_t b) 131 { 132 return ktime_to_ms(ktime_sub(a, b)); 133 } 134 135 static inline bool 136 ktime_before(ktime_t a, ktime_t b) 137 { 138 return a < b; 139 } 140 141 static inline bool 142 ktime_after(ktime_t a, ktime_t b) 143 { 144 return a > b; 145 } 146 147 static inline ktime_t 148 ns_to_ktime(uint64_t ns) 149 { 150 return ns; 151 } 152 153 static inline int64_t 154 ktime_divns(ktime_t a, int64_t ns) 155 { 156 return a / ns; 157 } 158 159 static inline ktime_t 160 ktime_set(time_t s, long ns) 161 { 162 struct timespec ts; 163 ts.tv_sec = s; 164 ts.tv_nsec = ns; 165 return TIMESPEC_TO_NSEC(&ts); 166 } 167 168 #include <linux/timekeeping.h> 169 170 #endif 171