diff options
Diffstat (limited to 'include/linux/ktime.h')
| -rw-r--r-- | include/linux/ktime.h | 284 | 
1 files changed, 284 insertions, 0 deletions
| diff --git a/include/linux/ktime.h b/include/linux/ktime.h new file mode 100644 index 000000000000..1bd6552cc341 --- /dev/null +++ b/include/linux/ktime.h | |||
| @@ -0,0 +1,284 @@ | |||
| 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 | * | ||
| 13 | * For licencing details see kernel-base/COPYING | ||
| 14 | */ | ||
| 15 | #ifndef _LINUX_KTIME_H | ||
| 16 | #define _LINUX_KTIME_H | ||
| 17 | |||
| 18 | #include <linux/time.h> | ||
| 19 | #include <linux/jiffies.h> | ||
| 20 | |||
| 21 | /* | ||
| 22 | * ktime_t: | ||
| 23 | * | ||
| 24 | * On 64-bit CPUs a single 64-bit variable is used to store the hrtimers | ||
| 25 | * internal representation of time values in scalar nanoseconds. The | ||
| 26 | * design plays out best on 64-bit CPUs, where most conversions are | ||
| 27 | * NOPs and most arithmetic ktime_t operations are plain arithmetic | ||
| 28 | * operations. | ||
| 29 | * | ||
| 30 | * On 32-bit CPUs an optimized representation of the timespec structure | ||
| 31 | * is used to avoid expensive conversions from and to timespecs. The | ||
| 32 | * endian-aware order of the tv struct members is choosen to allow | ||
| 33 | * mathematical operations on the tv64 member of the union too, which | ||
| 34 | * for certain operations produces better code. | ||
| 35 | * | ||
| 36 | * For architectures with efficient support for 64/32-bit conversions the | ||
| 37 | * plain scalar nanosecond based representation can be selected by the | ||
| 38 | * config switch CONFIG_KTIME_SCALAR. | ||
| 39 | */ | ||
| 40 | typedef union { | ||
| 41 | s64 tv64; | ||
| 42 | #if BITS_PER_LONG != 64 && !defined(CONFIG_KTIME_SCALAR) | ||
| 43 | struct { | ||
| 44 | # ifdef __BIG_ENDIAN | ||
| 45 | s32 sec, nsec; | ||
| 46 | # else | ||
| 47 | s32 nsec, sec; | ||
| 48 | # endif | ||
| 49 | } tv; | ||
| 50 | #endif | ||
| 51 | } ktime_t; | ||
| 52 | |||
| 53 | #define KTIME_MAX (~((u64)1 << 63)) | ||
| 54 | |||
| 55 | /* | ||
| 56 | * ktime_t definitions when using the 64-bit scalar representation: | ||
| 57 | */ | ||
| 58 | |||
| 59 | #if (BITS_PER_LONG == 64) || defined(CONFIG_KTIME_SCALAR) | ||
| 60 | |||
| 61 | /* Define a ktime_t variable and initialize it to zero: */ | ||
| 62 | #define DEFINE_KTIME(kt) ktime_t kt = { .tv64 = 0 } | ||
| 63 | |||
| 64 | /** | ||
| 65 | * ktime_set - Set a ktime_t variable from a seconds/nanoseconds value | ||
| 66 | * | ||
| 67 | * @secs: seconds to set | ||
| 68 | * @nsecs: nanoseconds to set | ||
| 69 | * | ||
| 70 | * Return the ktime_t representation of the value | ||
| 71 | */ | ||
| 72 | static inline ktime_t ktime_set(const long secs, const unsigned long nsecs) | ||
| 73 | { | ||
| 74 | return (ktime_t) { .tv64 = (s64)secs * NSEC_PER_SEC + (s64)nsecs }; | ||
| 75 | } | ||
| 76 | |||
| 77 | /* Subtract two ktime_t variables. rem = lhs -rhs: */ | ||
| 78 | #define ktime_sub(lhs, rhs) \ | ||
| 79 | ({ (ktime_t){ .tv64 = (lhs).tv64 - (rhs).tv64 }; }) | ||
| 80 | |||
| 81 | /* Add two ktime_t variables. res = lhs + rhs: */ | ||
| 82 | #define ktime_add(lhs, rhs) \ | ||
| 83 | ({ (ktime_t){ .tv64 = (lhs).tv64 + (rhs).tv64 }; }) | ||
| 84 | |||
| 85 | /* | ||
| 86 | * Add a ktime_t variable and a scalar nanosecond value. | ||
| 87 | * res = kt + nsval: | ||
| 88 | */ | ||
| 89 | #define ktime_add_ns(kt, nsval) \ | ||
| 90 | ({ (ktime_t){ .tv64 = (kt).tv64 + (nsval) }; }) | ||
| 91 | |||
| 92 | /* convert a timespec to ktime_t format: */ | ||
| 93 | #define timespec_to_ktime(ts) ktime_set((ts).tv_sec, (ts).tv_nsec) | ||
| 94 | |||
| 95 | /* convert a timeval to ktime_t format: */ | ||
| 96 | #define timeval_to_ktime(tv) ktime_set((tv).tv_sec, (tv).tv_usec * 1000) | ||
| 97 | |||
| 98 | /* Map the ktime_t to timespec conversion to ns_to_timespec function */ | ||
| 99 | #define ktime_to_timespec(kt) ns_to_timespec((kt).tv64) | ||
| 100 | |||
| 101 | /* Map the ktime_t to timeval conversion to ns_to_timeval function */ | ||
| 102 | #define ktime_to_timeval(kt) ns_to_timeval((kt).tv64) | ||
| 103 | |||
| 104 | /* Map the ktime_t to clock_t conversion to the inline in jiffies.h: */ | ||
| 105 | #define ktime_to_clock_t(kt) nsec_to_clock_t((kt).tv64) | ||
| 106 | |||
| 107 | /* Convert ktime_t to nanoseconds - NOP in the scalar storage format: */ | ||
| 108 | #define ktime_to_ns(kt) ((kt).tv64) | ||
| 109 | |||
| 110 | #else | ||
| 111 | |||
| 112 | /* | ||
| 113 | * Helper macros/inlines to get the ktime_t math right in the timespec | ||
| 114 | * representation. The macros are sometimes ugly - their actual use is | ||
| 115 | * pretty okay-ish, given the circumstances. We do all this for | ||
| 116 | * performance reasons. The pure scalar nsec_t based code was nice and | ||
| 117 | * simple, but created too many 64-bit / 32-bit conversions and divisions. | ||
| 118 | * | ||
| 119 | * Be especially aware that negative values are represented in a way | ||
| 120 | * that the tv.sec field is negative and the tv.nsec field is greater | ||
| 121 | * or equal to zero but less than nanoseconds per second. This is the | ||
| 122 | * same representation which is used by timespecs. | ||
| 123 | * | ||
| 124 | * tv.sec < 0 and 0 >= tv.nsec < NSEC_PER_SEC | ||
| 125 | */ | ||
| 126 | |||
| 127 | /* Define a ktime_t variable and initialize it to zero: */ | ||
| 128 | #define DEFINE_KTIME(kt) ktime_t kt = { .tv64 = 0 } | ||
| 129 | |||
| 130 | /* Set a ktime_t variable to a value in sec/nsec representation: */ | ||
| 131 | static inline ktime_t ktime_set(const long secs, const unsigned long nsecs) | ||
| 132 | { | ||
| 133 | return (ktime_t) { .tv = { .sec = secs, .nsec = nsecs } }; | ||
| 134 | } | ||
| 135 | |||
| 136 | /** | ||
| 137 | * ktime_sub - subtract two ktime_t variables | ||
| 138 | * | ||
| 139 | * @lhs: minuend | ||
| 140 | * @rhs: subtrahend | ||
| 141 | * | ||
| 142 | * Returns the remainder of the substraction | ||
| 143 | */ | ||
| 144 | static inline ktime_t ktime_sub(const ktime_t lhs, const ktime_t rhs) | ||
| 145 | { | ||
| 146 | ktime_t res; | ||
| 147 | |||
| 148 | res.tv64 = lhs.tv64 - rhs.tv64; | ||
| 149 | if (res.tv.nsec < 0) | ||
| 150 | res.tv.nsec += NSEC_PER_SEC; | ||
| 151 | |||
| 152 | return res; | ||
| 153 | } | ||
| 154 | |||
| 155 | /** | ||
| 156 | * ktime_add - add two ktime_t variables | ||
| 157 | * | ||
| 158 | * @add1: addend1 | ||
| 159 | * @add2: addend2 | ||
| 160 | * | ||
| 161 | * Returns the sum of addend1 and addend2 | ||
| 162 | */ | ||
| 163 | static inline ktime_t ktime_add(const ktime_t add1, const ktime_t add2) | ||
| 164 | { | ||
| 165 | ktime_t res; | ||
| 166 | |||
| 167 | res.tv64 = add1.tv64 + add2.tv64; | ||
| 168 | /* | ||
| 169 | * performance trick: the (u32) -NSEC gives 0x00000000Fxxxxxxx | ||
| 170 | * so we subtract NSEC_PER_SEC and add 1 to the upper 32 bit. | ||
| 171 | * | ||
| 172 | * it's equivalent to: | ||
| 173 | * tv.nsec -= NSEC_PER_SEC | ||
| 174 | * tv.sec ++; | ||
| 175 | */ | ||
| 176 | if (res.tv.nsec >= NSEC_PER_SEC) | ||
| 177 | res.tv64 += (u32)-NSEC_PER_SEC; | ||
| 178 | |||
| 179 | return res; | ||
| 180 | } | ||
| 181 | |||
| 182 | /** | ||
| 183 | * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable | ||
| 184 | * | ||
| 185 | * @kt: addend | ||
| 186 | * @nsec: the scalar nsec value to add | ||
| 187 | * | ||
| 188 | * Returns the sum of kt and nsec in ktime_t format | ||
| 189 | */ | ||
| 190 | extern ktime_t ktime_add_ns(const ktime_t kt, u64 nsec); | ||
| 191 | |||
| 192 | /** | ||
| 193 | * timespec_to_ktime - convert a timespec to ktime_t format | ||
| 194 | * | ||
| 195 | * @ts: the timespec variable to convert | ||
| 196 | * | ||
| 197 | * Returns a ktime_t variable with the converted timespec value | ||
| 198 | */ | ||
| 199 | static inline ktime_t timespec_to_ktime(const struct timespec ts) | ||
| 200 | { | ||
| 201 | return (ktime_t) { .tv = { .sec = (s32)ts.tv_sec, | ||
| 202 | .nsec = (s32)ts.tv_nsec } }; | ||
| 203 | } | ||
| 204 | |||
| 205 | /** | ||
| 206 | * timeval_to_ktime - convert a timeval to ktime_t format | ||
| 207 | * | ||
| 208 | * @tv: the timeval variable to convert | ||
| 209 | * | ||
| 210 | * Returns a ktime_t variable with the converted timeval value | ||
| 211 | */ | ||
| 212 | static inline ktime_t timeval_to_ktime(const struct timeval tv) | ||
| 213 | { | ||
| 214 | return (ktime_t) { .tv = { .sec = (s32)tv.tv_sec, | ||
| 215 | .nsec = (s32)tv.tv_usec * 1000 } }; | ||
| 216 | } | ||
| 217 | |||
| 218 | /** | ||
| 219 | * ktime_to_timespec - convert a ktime_t variable to timespec format | ||
| 220 | * | ||
| 221 | * @kt: the ktime_t variable to convert | ||
| 222 | * | ||
| 223 | * Returns the timespec representation of the ktime value | ||
| 224 | */ | ||
| 225 | static inline struct timespec ktime_to_timespec(const ktime_t kt) | ||
| 226 | { | ||
| 227 | return (struct timespec) { .tv_sec = (time_t) kt.tv.sec, | ||
| 228 | .tv_nsec = (long) kt.tv.nsec }; | ||
| 229 | } | ||
| 230 | |||
| 231 | /** | ||
| 232 | * ktime_to_timeval - convert a ktime_t variable to timeval format | ||
| 233 | * | ||
| 234 | * @kt: the ktime_t variable to convert | ||
| 235 | * | ||
| 236 | * Returns the timeval representation of the ktime value | ||
| 237 | */ | ||
| 238 | static inline struct timeval ktime_to_timeval(const ktime_t kt) | ||
| 239 | { | ||
| 240 | return (struct timeval) { | ||
| 241 | .tv_sec = (time_t) kt.tv.sec, | ||
| 242 | .tv_usec = (suseconds_t) (kt.tv.nsec / NSEC_PER_USEC) }; | ||
| 243 | } | ||
| 244 | |||
| 245 | /** | ||
| 246 | * ktime_to_clock_t - convert a ktime_t variable to clock_t format | ||
| 247 | * @kt: the ktime_t variable to convert | ||
| 248 | * | ||
| 249 | * Returns a clock_t variable with the converted value | ||
| 250 | */ | ||
| 251 | static inline clock_t ktime_to_clock_t(const ktime_t kt) | ||
| 252 | { | ||
| 253 | return nsec_to_clock_t( (u64) kt.tv.sec * NSEC_PER_SEC + kt.tv.nsec); | ||
| 254 | } | ||
| 255 | |||
| 256 | /** | ||
| 257 | * ktime_to_ns - convert a ktime_t variable to scalar nanoseconds | ||
| 258 | * @kt: the ktime_t variable to convert | ||
| 259 | * | ||
| 260 | * Returns the scalar nanoseconds representation of kt | ||
| 261 | */ | ||
| 262 | static inline u64 ktime_to_ns(const ktime_t kt) | ||
| 263 | { | ||
| 264 | return (u64) kt.tv.sec * NSEC_PER_SEC + kt.tv.nsec; | ||
| 265 | } | ||
| 266 | |||
| 267 | #endif | ||
| 268 | |||
| 269 | /* | ||
| 270 | * The resolution of the clocks. The resolution value is returned in | ||
| 271 | * the clock_getres() system call to give application programmers an | ||
| 272 | * idea of the (in)accuracy of timers. Timer values are rounded up to | ||
| 273 | * this resolution values. | ||
| 274 | */ | ||
| 275 | #define KTIME_REALTIME_RES (ktime_t){ .tv64 = TICK_NSEC } | ||
| 276 | #define KTIME_MONOTONIC_RES (ktime_t){ .tv64 = TICK_NSEC } | ||
| 277 | |||
| 278 | /* Get the monotonic time in timespec format: */ | ||
| 279 | extern void ktime_get_ts(struct timespec *ts); | ||
| 280 | |||
| 281 | /* Get the real (wall-) time in timespec format: */ | ||
| 282 | #define ktime_get_real_ts(ts) getnstimeofday(ts) | ||
| 283 | |||
| 284 | #endif | ||
