Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | * include/linux/ktime.h |
| 3 | * |
| 4 | * ktime_t - nanosecond-resolution time format. |
| 5 | * |
| 6 | * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de> |
| 7 | * Copyright(C) 2005, Red Hat, Inc., Ingo Molnar |
| 8 | * |
| 9 | * data type definitions, declarations, prototypes and macros. |
| 10 | * |
| 11 | * Started by: Thomas Gleixner and Ingo Molnar |
| 12 | * |
Thomas Gleixner | 66188fa | 2006-02-01 03:05:13 -0800 | [diff] [blame] | 13 | * Credits: |
| 14 | * |
| 15 | * Roman Zippel provided the ideas and primary code snippets of |
| 16 | * the ktime_t union and further simplifications of the original |
| 17 | * code. |
| 18 | * |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 19 | * For licencing details see kernel-base/COPYING |
| 20 | */ |
| 21 | #ifndef _LINUX_KTIME_H |
| 22 | #define _LINUX_KTIME_H |
| 23 | |
| 24 | #include <linux/time.h> |
| 25 | #include <linux/jiffies.h> |
| 26 | |
| 27 | /* |
| 28 | * ktime_t: |
| 29 | * |
| 30 | * On 64-bit CPUs a single 64-bit variable is used to store the hrtimers |
| 31 | * internal representation of time values in scalar nanoseconds. The |
| 32 | * design plays out best on 64-bit CPUs, where most conversions are |
| 33 | * NOPs and most arithmetic ktime_t operations are plain arithmetic |
| 34 | * operations. |
| 35 | * |
| 36 | * On 32-bit CPUs an optimized representation of the timespec structure |
| 37 | * is used to avoid expensive conversions from and to timespecs. The |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 38 | * endian-aware order of the tv struct members is chosen to allow |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 39 | * mathematical operations on the tv64 member of the union too, which |
| 40 | * for certain operations produces better code. |
| 41 | * |
| 42 | * For architectures with efficient support for 64/32-bit conversions the |
| 43 | * plain scalar nanosecond based representation can be selected by the |
| 44 | * config switch CONFIG_KTIME_SCALAR. |
| 45 | */ |
Andrew Morton | f34c506 | 2007-05-09 02:34:59 -0700 | [diff] [blame] | 46 | union ktime { |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 47 | s64 tv64; |
| 48 | #if BITS_PER_LONG != 64 && !defined(CONFIG_KTIME_SCALAR) |
| 49 | struct { |
| 50 | # ifdef __BIG_ENDIAN |
| 51 | s32 sec, nsec; |
| 52 | # else |
| 53 | s32 nsec, sec; |
| 54 | # endif |
| 55 | } tv; |
| 56 | #endif |
Andrew Morton | f34c506 | 2007-05-09 02:34:59 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | typedef union ktime ktime_t; /* Kill this */ |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 60 | |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 61 | /* |
| 62 | * ktime_t definitions when using the 64-bit scalar representation: |
| 63 | */ |
| 64 | |
| 65 | #if (BITS_PER_LONG == 64) || defined(CONFIG_KTIME_SCALAR) |
| 66 | |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 67 | /** |
| 68 | * ktime_set - Set a ktime_t variable from a seconds/nanoseconds value |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 69 | * @secs: seconds to set |
| 70 | * @nsecs: nanoseconds to set |
| 71 | * |
Yacine Belkadi | 3601926 | 2013-07-24 23:11:53 -0700 | [diff] [blame] | 72 | * Return: The ktime_t representation of the value. |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 73 | */ |
| 74 | static inline ktime_t ktime_set(const long secs, const unsigned long nsecs) |
| 75 | { |
Thomas Gleixner | 96dd742 | 2006-09-06 00:03:42 -0700 | [diff] [blame] | 76 | #if (BITS_PER_LONG == 64) |
| 77 | if (unlikely(secs >= KTIME_SEC_MAX)) |
| 78 | return (ktime_t){ .tv64 = KTIME_MAX }; |
| 79 | #endif |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 80 | return (ktime_t) { .tv64 = (s64)secs * NSEC_PER_SEC + (s64)nsecs }; |
| 81 | } |
| 82 | |
| 83 | /* Subtract two ktime_t variables. rem = lhs -rhs: */ |
| 84 | #define ktime_sub(lhs, rhs) \ |
| 85 | ({ (ktime_t){ .tv64 = (lhs).tv64 - (rhs).tv64 }; }) |
| 86 | |
| 87 | /* Add two ktime_t variables. res = lhs + rhs: */ |
| 88 | #define ktime_add(lhs, rhs) \ |
| 89 | ({ (ktime_t){ .tv64 = (lhs).tv64 + (rhs).tv64 }; }) |
| 90 | |
| 91 | /* |
| 92 | * Add a ktime_t variable and a scalar nanosecond value. |
| 93 | * res = kt + nsval: |
| 94 | */ |
| 95 | #define ktime_add_ns(kt, nsval) \ |
| 96 | ({ (ktime_t){ .tv64 = (kt).tv64 + (nsval) }; }) |
| 97 | |
Arnaldo Carvalho de Melo | a272378 | 2007-08-19 17:16:05 -0700 | [diff] [blame] | 98 | /* |
| 99 | * Subtract a scalar nanosecod from a ktime_t variable |
| 100 | * res = kt - nsval: |
| 101 | */ |
| 102 | #define ktime_sub_ns(kt, nsval) \ |
| 103 | ({ (ktime_t){ .tv64 = (kt).tv64 - (nsval) }; }) |
| 104 | |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 105 | /* convert a timespec to ktime_t format: */ |
Roman Zippel | b2ee9db | 2006-02-15 15:17:40 -0800 | [diff] [blame] | 106 | static inline ktime_t timespec_to_ktime(struct timespec ts) |
| 107 | { |
| 108 | return ktime_set(ts.tv_sec, ts.tv_nsec); |
| 109 | } |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 110 | |
| 111 | /* convert a timeval to ktime_t format: */ |
Roman Zippel | b2ee9db | 2006-02-15 15:17:40 -0800 | [diff] [blame] | 112 | static inline ktime_t timeval_to_ktime(struct timeval tv) |
| 113 | { |
| 114 | return ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC); |
| 115 | } |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 116 | |
| 117 | /* Map the ktime_t to timespec conversion to ns_to_timespec function */ |
| 118 | #define ktime_to_timespec(kt) ns_to_timespec((kt).tv64) |
| 119 | |
| 120 | /* Map the ktime_t to timeval conversion to ns_to_timeval function */ |
| 121 | #define ktime_to_timeval(kt) ns_to_timeval((kt).tv64) |
| 122 | |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 123 | /* Convert ktime_t to nanoseconds - NOP in the scalar storage format: */ |
| 124 | #define ktime_to_ns(kt) ((kt).tv64) |
| 125 | |
Chuck Lever | f56916b | 2010-05-07 13:34:37 -0400 | [diff] [blame] | 126 | #else /* !((BITS_PER_LONG == 64) || defined(CONFIG_KTIME_SCALAR)) */ |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 127 | |
| 128 | /* |
| 129 | * Helper macros/inlines to get the ktime_t math right in the timespec |
| 130 | * representation. The macros are sometimes ugly - their actual use is |
| 131 | * pretty okay-ish, given the circumstances. We do all this for |
| 132 | * performance reasons. The pure scalar nsec_t based code was nice and |
| 133 | * simple, but created too many 64-bit / 32-bit conversions and divisions. |
| 134 | * |
| 135 | * Be especially aware that negative values are represented in a way |
| 136 | * that the tv.sec field is negative and the tv.nsec field is greater |
| 137 | * or equal to zero but less than nanoseconds per second. This is the |
| 138 | * same representation which is used by timespecs. |
| 139 | * |
| 140 | * tv.sec < 0 and 0 >= tv.nsec < NSEC_PER_SEC |
| 141 | */ |
| 142 | |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 143 | /* Set a ktime_t variable to a value in sec/nsec representation: */ |
| 144 | static inline ktime_t ktime_set(const long secs, const unsigned long nsecs) |
| 145 | { |
| 146 | return (ktime_t) { .tv = { .sec = secs, .nsec = nsecs } }; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * ktime_sub - subtract two ktime_t variables |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 151 | * @lhs: minuend |
| 152 | * @rhs: subtrahend |
| 153 | * |
Yacine Belkadi | 3601926 | 2013-07-24 23:11:53 -0700 | [diff] [blame] | 154 | * Return: The remainder of the subtraction. |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 155 | */ |
| 156 | static inline ktime_t ktime_sub(const ktime_t lhs, const ktime_t rhs) |
| 157 | { |
| 158 | ktime_t res; |
| 159 | |
| 160 | res.tv64 = lhs.tv64 - rhs.tv64; |
| 161 | if (res.tv.nsec < 0) |
| 162 | res.tv.nsec += NSEC_PER_SEC; |
| 163 | |
| 164 | return res; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * ktime_add - add two ktime_t variables |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 169 | * @add1: addend1 |
| 170 | * @add2: addend2 |
| 171 | * |
Yacine Belkadi | 3601926 | 2013-07-24 23:11:53 -0700 | [diff] [blame] | 172 | * Return: The sum of @add1 and @add2. |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 173 | */ |
| 174 | static inline ktime_t ktime_add(const ktime_t add1, const ktime_t add2) |
| 175 | { |
| 176 | ktime_t res; |
| 177 | |
| 178 | res.tv64 = add1.tv64 + add2.tv64; |
| 179 | /* |
| 180 | * performance trick: the (u32) -NSEC gives 0x00000000Fxxxxxxx |
| 181 | * so we subtract NSEC_PER_SEC and add 1 to the upper 32 bit. |
| 182 | * |
| 183 | * it's equivalent to: |
| 184 | * tv.nsec -= NSEC_PER_SEC |
| 185 | * tv.sec ++; |
| 186 | */ |
| 187 | if (res.tv.nsec >= NSEC_PER_SEC) |
| 188 | res.tv64 += (u32)-NSEC_PER_SEC; |
| 189 | |
| 190 | return res; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 195 | * @kt: addend |
| 196 | * @nsec: the scalar nsec value to add |
| 197 | * |
Yacine Belkadi | 3601926 | 2013-07-24 23:11:53 -0700 | [diff] [blame] | 198 | * Return: The sum of @kt and @nsec in ktime_t format. |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 199 | */ |
| 200 | extern ktime_t ktime_add_ns(const ktime_t kt, u64 nsec); |
| 201 | |
| 202 | /** |
Arnaldo Carvalho de Melo | a272378 | 2007-08-19 17:16:05 -0700 | [diff] [blame] | 203 | * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable |
| 204 | * @kt: minuend |
| 205 | * @nsec: the scalar nsec value to subtract |
| 206 | * |
Yacine Belkadi | 3601926 | 2013-07-24 23:11:53 -0700 | [diff] [blame] | 207 | * Return: The subtraction of @nsec from @kt in ktime_t format. |
Arnaldo Carvalho de Melo | a272378 | 2007-08-19 17:16:05 -0700 | [diff] [blame] | 208 | */ |
| 209 | extern ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec); |
| 210 | |
| 211 | /** |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 212 | * timespec_to_ktime - convert a timespec to ktime_t format |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 213 | * @ts: the timespec variable to convert |
| 214 | * |
Yacine Belkadi | 3601926 | 2013-07-24 23:11:53 -0700 | [diff] [blame] | 215 | * Return: A ktime_t variable with the converted timespec value. |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 216 | */ |
| 217 | static inline ktime_t timespec_to_ktime(const struct timespec ts) |
| 218 | { |
| 219 | return (ktime_t) { .tv = { .sec = (s32)ts.tv_sec, |
| 220 | .nsec = (s32)ts.tv_nsec } }; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * timeval_to_ktime - convert a timeval to ktime_t format |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 225 | * @tv: the timeval variable to convert |
| 226 | * |
Yacine Belkadi | 3601926 | 2013-07-24 23:11:53 -0700 | [diff] [blame] | 227 | * Return: A ktime_t variable with the converted timeval value. |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 228 | */ |
| 229 | static inline ktime_t timeval_to_ktime(const struct timeval tv) |
| 230 | { |
| 231 | return (ktime_t) { .tv = { .sec = (s32)tv.tv_sec, |
Liu Ying | a44b8bd | 2013-05-07 16:38:40 +0800 | [diff] [blame] | 232 | .nsec = (s32)(tv.tv_usec * |
| 233 | NSEC_PER_USEC) } }; |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /** |
| 237 | * ktime_to_timespec - convert a ktime_t variable to timespec format |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 238 | * @kt: the ktime_t variable to convert |
| 239 | * |
Yacine Belkadi | 3601926 | 2013-07-24 23:11:53 -0700 | [diff] [blame] | 240 | * Return: The timespec representation of the ktime value. |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 241 | */ |
| 242 | static inline struct timespec ktime_to_timespec(const ktime_t kt) |
| 243 | { |
| 244 | return (struct timespec) { .tv_sec = (time_t) kt.tv.sec, |
| 245 | .tv_nsec = (long) kt.tv.nsec }; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * ktime_to_timeval - convert a ktime_t variable to timeval format |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 250 | * @kt: the ktime_t variable to convert |
| 251 | * |
Yacine Belkadi | 3601926 | 2013-07-24 23:11:53 -0700 | [diff] [blame] | 252 | * Return: The timeval representation of the ktime value. |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 253 | */ |
| 254 | static inline struct timeval ktime_to_timeval(const ktime_t kt) |
| 255 | { |
| 256 | return (struct timeval) { |
| 257 | .tv_sec = (time_t) kt.tv.sec, |
| 258 | .tv_usec = (suseconds_t) (kt.tv.nsec / NSEC_PER_USEC) }; |
| 259 | } |
| 260 | |
| 261 | /** |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 262 | * ktime_to_ns - convert a ktime_t variable to scalar nanoseconds |
| 263 | * @kt: the ktime_t variable to convert |
| 264 | * |
Yacine Belkadi | 3601926 | 2013-07-24 23:11:53 -0700 | [diff] [blame] | 265 | * Return: The scalar nanoseconds representation of @kt. |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 266 | */ |
Thomas Gleixner | cfd1893 | 2006-12-06 20:37:38 -0800 | [diff] [blame] | 267 | static inline s64 ktime_to_ns(const ktime_t kt) |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 268 | { |
Thomas Gleixner | cfd1893 | 2006-12-06 20:37:38 -0800 | [diff] [blame] | 269 | return (s64) kt.tv.sec * NSEC_PER_SEC + kt.tv.nsec; |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Chuck Lever | f56916b | 2010-05-07 13:34:37 -0400 | [diff] [blame] | 272 | #endif /* !((BITS_PER_LONG == 64) || defined(CONFIG_KTIME_SCALAR)) */ |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 273 | |
Ilpo Järvinen | b9ce204 | 2007-06-15 15:08:43 -0700 | [diff] [blame] | 274 | /** |
| 275 | * ktime_equal - Compares two ktime_t variables to see if they are equal |
| 276 | * @cmp1: comparable1 |
| 277 | * @cmp2: comparable2 |
| 278 | * |
Yacine Belkadi | 3601926 | 2013-07-24 23:11:53 -0700 | [diff] [blame] | 279 | * Compare two ktime_t variables. |
| 280 | * |
| 281 | * Return: 1 if equal. |
Ilpo Järvinen | b9ce204 | 2007-06-15 15:08:43 -0700 | [diff] [blame] | 282 | */ |
| 283 | static inline int ktime_equal(const ktime_t cmp1, const ktime_t cmp2) |
| 284 | { |
| 285 | return cmp1.tv64 == cmp2.tv64; |
| 286 | } |
| 287 | |
Daniel Borkmann | 398f382 | 2012-10-28 08:27:19 +0000 | [diff] [blame] | 288 | /** |
| 289 | * ktime_compare - Compares two ktime_t variables for less, greater or equal |
| 290 | * @cmp1: comparable1 |
| 291 | * @cmp2: comparable2 |
| 292 | * |
Yacine Belkadi | 3601926 | 2013-07-24 23:11:53 -0700 | [diff] [blame] | 293 | * Return: ... |
Daniel Borkmann | 398f382 | 2012-10-28 08:27:19 +0000 | [diff] [blame] | 294 | * cmp1 < cmp2: return <0 |
| 295 | * cmp1 == cmp2: return 0 |
| 296 | * cmp1 > cmp2: return >0 |
| 297 | */ |
| 298 | static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2) |
| 299 | { |
| 300 | if (cmp1.tv64 < cmp2.tv64) |
| 301 | return -1; |
| 302 | if (cmp1.tv64 > cmp2.tv64) |
| 303 | return 1; |
| 304 | return 0; |
| 305 | } |
| 306 | |
Daniel Borkmann | 67cb936 | 2014-06-11 18:19:28 +0200 | [diff] [blame] | 307 | /** |
| 308 | * ktime_after - Compare if a ktime_t value is bigger than another one. |
| 309 | * @cmp1: comparable1 |
| 310 | * @cmp2: comparable2 |
| 311 | * |
| 312 | * Return: true if cmp1 happened after cmp2. |
| 313 | */ |
| 314 | static inline bool ktime_after(const ktime_t cmp1, const ktime_t cmp2) |
| 315 | { |
| 316 | return ktime_compare(cmp1, cmp2) > 0; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * ktime_before - Compare if a ktime_t value is smaller than another one. |
| 321 | * @cmp1: comparable1 |
| 322 | * @cmp2: comparable2 |
| 323 | * |
| 324 | * Return: true if cmp1 happened before cmp2. |
| 325 | */ |
| 326 | static inline bool ktime_before(const ktime_t cmp1, const ktime_t cmp2) |
| 327 | { |
| 328 | return ktime_compare(cmp1, cmp2) < 0; |
| 329 | } |
| 330 | |
YOSHIFUJI Hideaki | 84299b3 | 2007-04-24 16:21:38 -0700 | [diff] [blame] | 331 | static inline s64 ktime_to_us(const ktime_t kt) |
| 332 | { |
| 333 | struct timeval tv = ktime_to_timeval(kt); |
| 334 | return (s64) tv.tv_sec * USEC_PER_SEC + tv.tv_usec; |
| 335 | } |
| 336 | |
Chuck Lever | f56916b | 2010-05-07 13:34:37 -0400 | [diff] [blame] | 337 | static inline s64 ktime_to_ms(const ktime_t kt) |
| 338 | { |
| 339 | struct timeval tv = ktime_to_timeval(kt); |
| 340 | return (s64) tv.tv_sec * MSEC_PER_SEC + tv.tv_usec / USEC_PER_MSEC; |
| 341 | } |
| 342 | |
Gerrit Renker | f1c91da | 2007-06-16 12:38:51 -0300 | [diff] [blame] | 343 | static inline s64 ktime_us_delta(const ktime_t later, const ktime_t earlier) |
| 344 | { |
| 345 | return ktime_to_us(ktime_sub(later, earlier)); |
| 346 | } |
| 347 | |
Arnaldo Carvalho de Melo | 1e180f7 | 2007-06-16 12:39:38 -0300 | [diff] [blame] | 348 | static inline ktime_t ktime_add_us(const ktime_t kt, const u64 usec) |
| 349 | { |
Liu Ying | a44b8bd | 2013-05-07 16:38:40 +0800 | [diff] [blame] | 350 | return ktime_add_ns(kt, usec * NSEC_PER_USEC); |
Arnaldo Carvalho de Melo | 1e180f7 | 2007-06-16 12:39:38 -0300 | [diff] [blame] | 351 | } |
| 352 | |
Daniel Borkmann | d36f82b | 2013-06-25 18:17:26 +0200 | [diff] [blame] | 353 | static inline ktime_t ktime_add_ms(const ktime_t kt, const u64 msec) |
| 354 | { |
| 355 | return ktime_add_ns(kt, msec * NSEC_PER_MSEC); |
| 356 | } |
| 357 | |
Arnaldo Carvalho de Melo | a272378 | 2007-08-19 17:16:05 -0700 | [diff] [blame] | 358 | static inline ktime_t ktime_sub_us(const ktime_t kt, const u64 usec) |
| 359 | { |
Liu Ying | a44b8bd | 2013-05-07 16:38:40 +0800 | [diff] [blame] | 360 | return ktime_sub_ns(kt, usec * NSEC_PER_USEC); |
Arnaldo Carvalho de Melo | a272378 | 2007-08-19 17:16:05 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Thomas Gleixner | 5a7780e | 2008-02-13 09:20:43 +0100 | [diff] [blame] | 363 | extern ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs); |
| 364 | |
Daniel Borkmann | 6e94d1e | 2013-04-16 01:29:10 +0000 | [diff] [blame] | 365 | /** |
| 366 | * ktime_to_timespec_cond - convert a ktime_t variable to timespec |
| 367 | * format only if the variable contains data |
| 368 | * @kt: the ktime_t variable to convert |
| 369 | * @ts: the timespec variable to store the result in |
| 370 | * |
Yacine Belkadi | 3601926 | 2013-07-24 23:11:53 -0700 | [diff] [blame] | 371 | * Return: %true if there was a successful conversion, %false if kt was 0. |
Daniel Borkmann | 6e94d1e | 2013-04-16 01:29:10 +0000 | [diff] [blame] | 372 | */ |
Daniel Borkmann | 35b2108 | 2013-05-16 15:47:49 +0200 | [diff] [blame] | 373 | static inline __must_check bool ktime_to_timespec_cond(const ktime_t kt, |
| 374 | struct timespec *ts) |
Daniel Borkmann | 6e94d1e | 2013-04-16 01:29:10 +0000 | [diff] [blame] | 375 | { |
| 376 | if (kt.tv64) { |
| 377 | *ts = ktime_to_timespec(kt); |
| 378 | return true; |
| 379 | } else { |
| 380 | return false; |
| 381 | } |
| 382 | } |
| 383 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 384 | /* |
| 385 | * The resolution of the clocks. The resolution value is returned in |
| 386 | * the clock_getres() system call to give application programmers an |
| 387 | * idea of the (in)accuracy of timers. Timer values are rounded up to |
| 388 | * this resolution values. |
| 389 | */ |
Tony Breeds | 151db1f | 2008-02-08 09:24:52 +1100 | [diff] [blame] | 390 | #define LOW_RES_NSEC TICK_NSEC |
| 391 | #define KTIME_LOW_RES (ktime_t){ .tv64 = LOW_RES_NSEC } |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 392 | |
| 393 | /* Get the monotonic time in timespec format: */ |
| 394 | extern void ktime_get_ts(struct timespec *ts); |
| 395 | |
| 396 | /* Get the real (wall-) time in timespec format: */ |
| 397 | #define ktime_get_real_ts(ts) getnstimeofday(ts) |
| 398 | |
Ingo Molnar | 57d3da2 | 2008-02-27 14:05:10 +0100 | [diff] [blame] | 399 | static inline ktime_t ns_to_ktime(u64 ns) |
| 400 | { |
| 401 | static const ktime_t ktime_zero = { .tv64 = 0 }; |
Daniel Borkmann | d36f82b | 2013-06-25 18:17:26 +0200 | [diff] [blame] | 402 | |
Ingo Molnar | 57d3da2 | 2008-02-27 14:05:10 +0100 | [diff] [blame] | 403 | return ktime_add_ns(ktime_zero, ns); |
| 404 | } |
| 405 | |
Daniel Borkmann | d36f82b | 2013-06-25 18:17:26 +0200 | [diff] [blame] | 406 | static inline ktime_t ms_to_ktime(u64 ms) |
| 407 | { |
| 408 | static const ktime_t ktime_zero = { .tv64 = 0 }; |
| 409 | |
| 410 | return ktime_add_ms(ktime_zero, ms); |
| 411 | } |
| 412 | |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 413 | #endif |