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 * $FreeBSD$ 29 */ 30 31 #ifndef _LINUX_KTIME_H 32 #define _LINUX_KTIME_H 33 34 #include <linux/types.h> 35 #include <linux/time.h> 36 #include <linux/jiffies.h> 37 38 #define ktime_get_ts(x) getnanouptime(x) 39 40 /* time values in nanoseconds */ 41 typedef s64 ktime_t; 42 43 #define KTIME_MAX ((s64)~((u64)1 << 63)) 44 #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) 45 46 static inline int64_t 47 ktime_to_ns(ktime_t kt) 48 { 49 return (kt); 50 } 51 52 static inline ktime_t 53 ns_to_ktime(uint64_t nsec) 54 { 55 return (nsec); 56 } 57 58 static inline int64_t 59 ktime_divns(const ktime_t kt, int64_t div) 60 { 61 return (kt / div); 62 } 63 64 static inline int64_t 65 ktime_to_us(ktime_t kt) 66 { 67 return (ktime_divns(kt, NSEC_PER_USEC)); 68 } 69 70 static inline int64_t 71 ktime_to_ms(ktime_t kt) 72 { 73 return (ktime_divns(kt, NSEC_PER_MSEC)); 74 } 75 76 static inline struct timeval 77 ktime_to_timeval(ktime_t kt) 78 { 79 return (ns_to_timeval(kt)); 80 } 81 82 static inline ktime_t 83 ktime_add_ns(ktime_t kt, int64_t ns) 84 { 85 return (kt + ns); 86 } 87 88 static inline ktime_t 89 ktime_sub_ns(ktime_t kt, int64_t ns) 90 { 91 return (kt - ns); 92 } 93 94 static inline ktime_t 95 ktime_set(const long secs, const unsigned long nsecs) 96 { 97 ktime_t retval = {(s64) secs * NSEC_PER_SEC + (s64) nsecs}; 98 99 return (retval); 100 } 101 102 static inline ktime_t 103 ktime_sub(ktime_t lhs, ktime_t rhs) 104 { 105 return (lhs - rhs); 106 } 107 108 static inline int64_t 109 ktime_us_delta(ktime_t later, ktime_t earlier) 110 { 111 ktime_t diff = ktime_sub(later, earlier); 112 113 return (ktime_to_us(diff)); 114 } 115 116 static inline int64_t 117 ktime_ms_delta(ktime_t later, ktime_t earlier) 118 { 119 ktime_t diff = ktime_sub(later, earlier); 120 121 return (ktime_to_ms(diff)); 122 } 123 124 static inline ktime_t 125 ktime_add(ktime_t lhs, ktime_t rhs) 126 { 127 return (lhs + rhs); 128 } 129 130 static inline int 131 ktime_compare(const ktime_t cmp1, const ktime_t cmp2) 132 { 133 134 if (cmp1 > cmp2) 135 return (1); 136 else if (cmp1 < cmp2) 137 return (-1); 138 else 139 return (0); 140 } 141 142 static inline bool 143 ktime_after(const ktime_t cmp1, const ktime_t cmp2) 144 { 145 146 return (ktime_compare(cmp1, cmp2) > 0); 147 } 148 149 static inline ktime_t 150 timespec_to_ktime(struct timespec ts) 151 { 152 return (ktime_set(ts.tv_sec, ts.tv_nsec)); 153 } 154 155 static inline ktime_t 156 timeval_to_ktime(struct timeval tv) 157 { 158 return (ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC)); 159 } 160 161 #define ktime_to_timespec(kt) ns_to_timespec(kt) 162 #define ktime_to_timespec64(kt) ns_to_timespec(kt) 163 #define ktime_to_timeval(kt) ns_to_timeval(kt) 164 #define ktime_to_ns(kt) (kt) 165 166 static inline int64_t 167 ktime_get_ns(void) 168 { 169 struct timespec ts; 170 171 ktime_get_ts(&ts); 172 173 return (ktime_to_ns(timespec_to_ktime(ts))); 174 } 175 176 static inline ktime_t 177 ktime_get(void) 178 { 179 struct timespec ts; 180 181 ktime_get_ts(&ts); 182 return (timespec_to_ktime(ts)); 183 } 184 185 static inline ktime_t 186 ktime_get_boottime(void) 187 { 188 struct timespec ts; 189 190 nanouptime(&ts); 191 return (timespec_to_ktime(ts)); 192 } 193 194 static inline ktime_t 195 ktime_get_real(void) 196 { 197 struct timespec ts; 198 199 nanotime(&ts); 200 return (timespec_to_ktime(ts)); 201 } 202 203 static inline ktime_t 204 ktime_get_real_seconds(void) 205 { 206 struct timespec ts; 207 208 nanotime(&ts); 209 return (ts.tv_sec); 210 } 211 212 static inline ktime_t 213 ktime_get_raw(void) 214 { 215 struct timespec ts; 216 217 nanotime(&ts); 218 return (timespec_to_ktime(ts)); 219 } 220 221 static inline u64 222 ktime_get_raw_ns(void) 223 { 224 struct timespec ts; 225 226 nanouptime(&ts); 227 return (ktime_to_ns(timespec_to_ktime(ts))); 228 } 229 230 #endif /* _LINUX_KTIME_H */ 231