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 | 96dd742 | 2006-09-06 00:03:42 -0700 | [diff] [blame] | 61 | #define KTIME_MAX ((s64)~((u64)1 << 63)) |
Thomas Gleixner | 5379058 | 2007-03-16 14:15:57 -0800 | [diff] [blame] | 62 | #if (BITS_PER_LONG == 64) |
| 63 | # define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) |
| 64 | #else |
| 65 | # define KTIME_SEC_MAX LONG_MAX |
| 66 | #endif |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | * ktime_t definitions when using the 64-bit scalar representation: |
| 70 | */ |
| 71 | |
| 72 | #if (BITS_PER_LONG == 64) || defined(CONFIG_KTIME_SCALAR) |
| 73 | |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 74 | /** |
| 75 | * ktime_set - Set a ktime_t variable from a seconds/nanoseconds value |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 76 | * @secs: seconds to set |
| 77 | * @nsecs: nanoseconds to set |
| 78 | * |
| 79 | * Return the ktime_t representation of the value |
| 80 | */ |
| 81 | static inline ktime_t ktime_set(const long secs, const unsigned long nsecs) |
| 82 | { |
Thomas Gleixner | 96dd742 | 2006-09-06 00:03:42 -0700 | [diff] [blame] | 83 | #if (BITS_PER_LONG == 64) |
| 84 | if (unlikely(secs >= KTIME_SEC_MAX)) |
| 85 | return (ktime_t){ .tv64 = KTIME_MAX }; |
| 86 | #endif |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 87 | return (ktime_t) { .tv64 = (s64)secs * NSEC_PER_SEC + (s64)nsecs }; |
| 88 | } |
| 89 | |
| 90 | /* Subtract two ktime_t variables. rem = lhs -rhs: */ |
| 91 | #define ktime_sub(lhs, rhs) \ |
| 92 | ({ (ktime_t){ .tv64 = (lhs).tv64 - (rhs).tv64 }; }) |
| 93 | |
| 94 | /* Add two ktime_t variables. res = lhs + rhs: */ |
| 95 | #define ktime_add(lhs, rhs) \ |
| 96 | ({ (ktime_t){ .tv64 = (lhs).tv64 + (rhs).tv64 }; }) |
| 97 | |
| 98 | /* |
| 99 | * Add a ktime_t variable and a scalar nanosecond value. |
| 100 | * res = kt + nsval: |
| 101 | */ |
| 102 | #define ktime_add_ns(kt, nsval) \ |
| 103 | ({ (ktime_t){ .tv64 = (kt).tv64 + (nsval) }; }) |
| 104 | |
Arnaldo Carvalho de Melo | a272378 | 2007-08-19 17:16:05 -0700 | [diff] [blame] | 105 | /* |
| 106 | * Subtract a scalar nanosecod from a ktime_t variable |
| 107 | * res = kt - nsval: |
| 108 | */ |
| 109 | #define ktime_sub_ns(kt, nsval) \ |
| 110 | ({ (ktime_t){ .tv64 = (kt).tv64 - (nsval) }; }) |
| 111 | |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 112 | /* convert a timespec to ktime_t format: */ |
Roman Zippel | b2ee9db | 2006-02-15 15:17:40 -0800 | [diff] [blame] | 113 | static inline ktime_t timespec_to_ktime(struct timespec ts) |
| 114 | { |
| 115 | return ktime_set(ts.tv_sec, ts.tv_nsec); |
| 116 | } |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 117 | |
| 118 | /* convert a timeval to ktime_t format: */ |
Roman Zippel | b2ee9db | 2006-02-15 15:17:40 -0800 | [diff] [blame] | 119 | static inline ktime_t timeval_to_ktime(struct timeval tv) |
| 120 | { |
| 121 | return ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC); |
| 122 | } |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 123 | |
| 124 | /* Map the ktime_t to timespec conversion to ns_to_timespec function */ |
| 125 | #define ktime_to_timespec(kt) ns_to_timespec((kt).tv64) |
| 126 | |
| 127 | /* Map the ktime_t to timeval conversion to ns_to_timeval function */ |
| 128 | #define ktime_to_timeval(kt) ns_to_timeval((kt).tv64) |
| 129 | |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 130 | /* Convert ktime_t to nanoseconds - NOP in the scalar storage format: */ |
| 131 | #define ktime_to_ns(kt) ((kt).tv64) |
| 132 | |
Chuck Lever | f56916b | 2010-05-07 13:34:37 -0400 | [diff] [blame] | 133 | #else /* !((BITS_PER_LONG == 64) || defined(CONFIG_KTIME_SCALAR)) */ |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * Helper macros/inlines to get the ktime_t math right in the timespec |
| 137 | * representation. The macros are sometimes ugly - their actual use is |
| 138 | * pretty okay-ish, given the circumstances. We do all this for |
| 139 | * performance reasons. The pure scalar nsec_t based code was nice and |
| 140 | * simple, but created too many 64-bit / 32-bit conversions and divisions. |
| 141 | * |
| 142 | * Be especially aware that negative values are represented in a way |
| 143 | * that the tv.sec field is negative and the tv.nsec field is greater |
| 144 | * or equal to zero but less than nanoseconds per second. This is the |
| 145 | * same representation which is used by timespecs. |
| 146 | * |
| 147 | * tv.sec < 0 and 0 >= tv.nsec < NSEC_PER_SEC |
| 148 | */ |
| 149 | |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 150 | /* Set a ktime_t variable to a value in sec/nsec representation: */ |
| 151 | static inline ktime_t ktime_set(const long secs, const unsigned long nsecs) |
| 152 | { |
| 153 | return (ktime_t) { .tv = { .sec = secs, .nsec = nsecs } }; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * ktime_sub - subtract two ktime_t variables |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 158 | * @lhs: minuend |
| 159 | * @rhs: subtrahend |
| 160 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 161 | * Returns the remainder of the subtraction |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 162 | */ |
| 163 | static inline ktime_t ktime_sub(const ktime_t lhs, const ktime_t rhs) |
| 164 | { |
| 165 | ktime_t res; |
| 166 | |
| 167 | res.tv64 = lhs.tv64 - rhs.tv64; |
| 168 | if (res.tv.nsec < 0) |
| 169 | res.tv.nsec += NSEC_PER_SEC; |
| 170 | |
| 171 | return res; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * ktime_add - add two ktime_t variables |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 176 | * @add1: addend1 |
| 177 | * @add2: addend2 |
| 178 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 179 | * Returns the sum of @add1 and @add2. |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 180 | */ |
| 181 | static inline ktime_t ktime_add(const ktime_t add1, const ktime_t add2) |
| 182 | { |
| 183 | ktime_t res; |
| 184 | |
| 185 | res.tv64 = add1.tv64 + add2.tv64; |
| 186 | /* |
| 187 | * performance trick: the (u32) -NSEC gives 0x00000000Fxxxxxxx |
| 188 | * so we subtract NSEC_PER_SEC and add 1 to the upper 32 bit. |
| 189 | * |
| 190 | * it's equivalent to: |
| 191 | * tv.nsec -= NSEC_PER_SEC |
| 192 | * tv.sec ++; |
| 193 | */ |
| 194 | if (res.tv.nsec >= NSEC_PER_SEC) |
| 195 | res.tv64 += (u32)-NSEC_PER_SEC; |
| 196 | |
| 197 | return res; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * 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] | 202 | * @kt: addend |
| 203 | * @nsec: the scalar nsec value to add |
| 204 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 205 | * Returns the sum of @kt and @nsec in ktime_t format |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 206 | */ |
| 207 | extern ktime_t ktime_add_ns(const ktime_t kt, u64 nsec); |
| 208 | |
| 209 | /** |
Arnaldo Carvalho de Melo | a272378 | 2007-08-19 17:16:05 -0700 | [diff] [blame] | 210 | * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable |
| 211 | * @kt: minuend |
| 212 | * @nsec: the scalar nsec value to subtract |
| 213 | * |
| 214 | * Returns the subtraction of @nsec from @kt in ktime_t format |
| 215 | */ |
| 216 | extern ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec); |
| 217 | |
| 218 | /** |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 219 | * timespec_to_ktime - convert a timespec to ktime_t format |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 220 | * @ts: the timespec variable to convert |
| 221 | * |
| 222 | * Returns a ktime_t variable with the converted timespec value |
| 223 | */ |
| 224 | static inline ktime_t timespec_to_ktime(const struct timespec ts) |
| 225 | { |
| 226 | return (ktime_t) { .tv = { .sec = (s32)ts.tv_sec, |
| 227 | .nsec = (s32)ts.tv_nsec } }; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * timeval_to_ktime - convert a timeval to ktime_t format |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 232 | * @tv: the timeval variable to convert |
| 233 | * |
| 234 | * Returns a ktime_t variable with the converted timeval value |
| 235 | */ |
| 236 | static inline ktime_t timeval_to_ktime(const struct timeval tv) |
| 237 | { |
| 238 | return (ktime_t) { .tv = { .sec = (s32)tv.tv_sec, |
| 239 | .nsec = (s32)tv.tv_usec * 1000 } }; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * ktime_to_timespec - convert a ktime_t variable to timespec format |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 244 | * @kt: the ktime_t variable to convert |
| 245 | * |
| 246 | * Returns the timespec representation of the ktime value |
| 247 | */ |
| 248 | static inline struct timespec ktime_to_timespec(const ktime_t kt) |
| 249 | { |
| 250 | return (struct timespec) { .tv_sec = (time_t) kt.tv.sec, |
| 251 | .tv_nsec = (long) kt.tv.nsec }; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * ktime_to_timeval - convert a ktime_t variable to timeval format |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 256 | * @kt: the ktime_t variable to convert |
| 257 | * |
| 258 | * Returns the timeval representation of the ktime value |
| 259 | */ |
| 260 | static inline struct timeval ktime_to_timeval(const ktime_t kt) |
| 261 | { |
| 262 | return (struct timeval) { |
| 263 | .tv_sec = (time_t) kt.tv.sec, |
| 264 | .tv_usec = (suseconds_t) (kt.tv.nsec / NSEC_PER_USEC) }; |
| 265 | } |
| 266 | |
| 267 | /** |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 268 | * ktime_to_ns - convert a ktime_t variable to scalar nanoseconds |
| 269 | * @kt: the ktime_t variable to convert |
| 270 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 271 | * Returns the scalar nanoseconds representation of @kt |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 272 | */ |
Thomas Gleixner | cfd1893 | 2006-12-06 20:37:38 -0800 | [diff] [blame] | 273 | static inline s64 ktime_to_ns(const ktime_t kt) |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 274 | { |
Thomas Gleixner | cfd1893 | 2006-12-06 20:37:38 -0800 | [diff] [blame] | 275 | return (s64) kt.tv.sec * NSEC_PER_SEC + kt.tv.nsec; |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Chuck Lever | f56916b | 2010-05-07 13:34:37 -0400 | [diff] [blame] | 278 | #endif /* !((BITS_PER_LONG == 64) || defined(CONFIG_KTIME_SCALAR)) */ |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 279 | |
Ilpo Järvinen | b9ce204 | 2007-06-15 15:08:43 -0700 | [diff] [blame] | 280 | /** |
| 281 | * ktime_equal - Compares two ktime_t variables to see if they are equal |
| 282 | * @cmp1: comparable1 |
| 283 | * @cmp2: comparable2 |
| 284 | * |
| 285 | * Compare two ktime_t variables, returns 1 if equal |
| 286 | */ |
| 287 | static inline int ktime_equal(const ktime_t cmp1, const ktime_t cmp2) |
| 288 | { |
| 289 | return cmp1.tv64 == cmp2.tv64; |
| 290 | } |
| 291 | |
YOSHIFUJI Hideaki | 84299b3 | 2007-04-24 16:21:38 -0700 | [diff] [blame] | 292 | static inline s64 ktime_to_us(const ktime_t kt) |
| 293 | { |
| 294 | struct timeval tv = ktime_to_timeval(kt); |
| 295 | return (s64) tv.tv_sec * USEC_PER_SEC + tv.tv_usec; |
| 296 | } |
| 297 | |
Chuck Lever | f56916b | 2010-05-07 13:34:37 -0400 | [diff] [blame] | 298 | static inline s64 ktime_to_ms(const ktime_t kt) |
| 299 | { |
| 300 | struct timeval tv = ktime_to_timeval(kt); |
| 301 | return (s64) tv.tv_sec * MSEC_PER_SEC + tv.tv_usec / USEC_PER_MSEC; |
| 302 | } |
| 303 | |
Gerrit Renker | f1c91da | 2007-06-16 12:38:51 -0300 | [diff] [blame] | 304 | static inline s64 ktime_us_delta(const ktime_t later, const ktime_t earlier) |
| 305 | { |
| 306 | return ktime_to_us(ktime_sub(later, earlier)); |
| 307 | } |
| 308 | |
Arnaldo Carvalho de Melo | 1e180f7 | 2007-06-16 12:39:38 -0300 | [diff] [blame] | 309 | static inline ktime_t ktime_add_us(const ktime_t kt, const u64 usec) |
| 310 | { |
| 311 | return ktime_add_ns(kt, usec * 1000); |
| 312 | } |
| 313 | |
Arnaldo Carvalho de Melo | a272378 | 2007-08-19 17:16:05 -0700 | [diff] [blame] | 314 | static inline ktime_t ktime_sub_us(const ktime_t kt, const u64 usec) |
| 315 | { |
| 316 | return ktime_sub_ns(kt, usec * 1000); |
| 317 | } |
| 318 | |
Thomas Gleixner | 5a7780e | 2008-02-13 09:20:43 +0100 | [diff] [blame] | 319 | extern ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs); |
| 320 | |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 321 | /* |
| 322 | * The resolution of the clocks. The resolution value is returned in |
| 323 | * the clock_getres() system call to give application programmers an |
| 324 | * idea of the (in)accuracy of timers. Timer values are rounded up to |
| 325 | * this resolution values. |
| 326 | */ |
Tony Breeds | 151db1f | 2008-02-08 09:24:52 +1100 | [diff] [blame] | 327 | #define LOW_RES_NSEC TICK_NSEC |
| 328 | #define KTIME_LOW_RES (ktime_t){ .tv64 = LOW_RES_NSEC } |
Thomas Gleixner | c0a3132 | 2006-01-09 20:52:32 -0800 | [diff] [blame] | 329 | |
| 330 | /* Get the monotonic time in timespec format: */ |
| 331 | extern void ktime_get_ts(struct timespec *ts); |
| 332 | |
| 333 | /* Get the real (wall-) time in timespec format: */ |
| 334 | #define ktime_get_real_ts(ts) getnstimeofday(ts) |
| 335 | |
Ingo Molnar | 57d3da2 | 2008-02-27 14:05:10 +0100 | [diff] [blame] | 336 | static inline ktime_t ns_to_ktime(u64 ns) |
| 337 | { |
| 338 | static const ktime_t ktime_zero = { .tv64 = 0 }; |
| 339 | return ktime_add_ns(ktime_zero, ns); |
| 340 | } |
| 341 | |
Thomas Gleixner | 97fc79f | 2006-01-09 20:52:31 -0800 | [diff] [blame] | 342 | #endif |