diff options
Diffstat (limited to 'include/linux/ktime.h')
| -rw-r--r-- | include/linux/ktime.h | 228 |
1 files changed, 42 insertions, 186 deletions
diff --git a/include/linux/ktime.h b/include/linux/ktime.h index de9e46e6bcc9..c9d645ad98ff 100644 --- a/include/linux/ktime.h +++ b/include/linux/ktime.h | |||
| @@ -27,43 +27,19 @@ | |||
| 27 | /* | 27 | /* |
| 28 | * ktime_t: | 28 | * ktime_t: |
| 29 | * | 29 | * |
| 30 | * On 64-bit CPUs a single 64-bit variable is used to store the hrtimers | 30 | * A single 64-bit variable is used to store the hrtimers |
| 31 | * internal representation of time values in scalar nanoseconds. The | 31 | * internal representation of time values in scalar nanoseconds. The |
| 32 | * design plays out best on 64-bit CPUs, where most conversions are | 32 | * design plays out best on 64-bit CPUs, where most conversions are |
| 33 | * NOPs and most arithmetic ktime_t operations are plain arithmetic | 33 | * NOPs and most arithmetic ktime_t operations are plain arithmetic |
| 34 | * operations. | 34 | * operations. |
| 35 | * | 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 | ||
| 38 | * endian-aware order of the tv struct members is chosen to allow | ||
| 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 | */ | 36 | */ |
| 46 | union ktime { | 37 | union ktime { |
| 47 | s64 tv64; | 38 | 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 | ||
| 57 | }; | 39 | }; |
| 58 | 40 | ||
| 59 | typedef union ktime ktime_t; /* Kill this */ | 41 | typedef union ktime ktime_t; /* Kill this */ |
| 60 | 42 | ||
| 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 | |||
| 67 | /** | 43 | /** |
| 68 | * ktime_set - Set a ktime_t variable from a seconds/nanoseconds value | 44 | * ktime_set - Set a ktime_t variable from a seconds/nanoseconds value |
| 69 | * @secs: seconds to set | 45 | * @secs: seconds to set |
| @@ -71,13 +47,12 @@ typedef union ktime ktime_t; /* Kill this */ | |||
| 71 | * | 47 | * |
| 72 | * Return: The ktime_t representation of the value. | 48 | * Return: The ktime_t representation of the value. |
| 73 | */ | 49 | */ |
| 74 | static inline ktime_t ktime_set(const long secs, const unsigned long nsecs) | 50 | static inline ktime_t ktime_set(const s64 secs, const unsigned long nsecs) |
| 75 | { | 51 | { |
| 76 | #if (BITS_PER_LONG == 64) | ||
| 77 | if (unlikely(secs >= KTIME_SEC_MAX)) | 52 | if (unlikely(secs >= KTIME_SEC_MAX)) |
| 78 | return (ktime_t){ .tv64 = KTIME_MAX }; | 53 | return (ktime_t){ .tv64 = KTIME_MAX }; |
| 79 | #endif | 54 | |
| 80 | return (ktime_t) { .tv64 = (s64)secs * NSEC_PER_SEC + (s64)nsecs }; | 55 | return (ktime_t) { .tv64 = secs * NSEC_PER_SEC + (s64)nsecs }; |
| 81 | } | 56 | } |
| 82 | 57 | ||
| 83 | /* Subtract two ktime_t variables. rem = lhs -rhs: */ | 58 | /* Subtract two ktime_t variables. rem = lhs -rhs: */ |
| @@ -108,6 +83,12 @@ static inline ktime_t timespec_to_ktime(struct timespec ts) | |||
| 108 | return ktime_set(ts.tv_sec, ts.tv_nsec); | 83 | return ktime_set(ts.tv_sec, ts.tv_nsec); |
| 109 | } | 84 | } |
| 110 | 85 | ||
| 86 | /* convert a timespec64 to ktime_t format: */ | ||
| 87 | static inline ktime_t timespec64_to_ktime(struct timespec64 ts) | ||
| 88 | { | ||
| 89 | return ktime_set(ts.tv_sec, ts.tv_nsec); | ||
| 90 | } | ||
| 91 | |||
| 111 | /* convert a timeval to ktime_t format: */ | 92 | /* convert a timeval to ktime_t format: */ |
| 112 | static inline ktime_t timeval_to_ktime(struct timeval tv) | 93 | static inline ktime_t timeval_to_ktime(struct timeval tv) |
| 113 | { | 94 | { |
| @@ -117,159 +98,15 @@ static inline ktime_t timeval_to_ktime(struct timeval tv) | |||
| 117 | /* Map the ktime_t to timespec conversion to ns_to_timespec function */ | 98 | /* Map the ktime_t to timespec conversion to ns_to_timespec function */ |
| 118 | #define ktime_to_timespec(kt) ns_to_timespec((kt).tv64) | 99 | #define ktime_to_timespec(kt) ns_to_timespec((kt).tv64) |
| 119 | 100 | ||
| 101 | /* Map the ktime_t to timespec conversion to ns_to_timespec function */ | ||
| 102 | #define ktime_to_timespec64(kt) ns_to_timespec64((kt).tv64) | ||
| 103 | |||
| 120 | /* Map the ktime_t to timeval conversion to ns_to_timeval function */ | 104 | /* Map the ktime_t to timeval conversion to ns_to_timeval function */ |
| 121 | #define ktime_to_timeval(kt) ns_to_timeval((kt).tv64) | 105 | #define ktime_to_timeval(kt) ns_to_timeval((kt).tv64) |
| 122 | 106 | ||
| 123 | /* Convert ktime_t to nanoseconds - NOP in the scalar storage format: */ | 107 | /* Convert ktime_t to nanoseconds - NOP in the scalar storage format: */ |
| 124 | #define ktime_to_ns(kt) ((kt).tv64) | 108 | #define ktime_to_ns(kt) ((kt).tv64) |
| 125 | 109 | ||
| 126 | #else /* !((BITS_PER_LONG == 64) || defined(CONFIG_KTIME_SCALAR)) */ | ||
| 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 | |||
| 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 | ||
| 151 | * @lhs: minuend | ||
| 152 | * @rhs: subtrahend | ||
| 153 | * | ||
| 154 | * Return: The remainder of the subtraction. | ||
| 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 | ||
| 169 | * @add1: addend1 | ||
| 170 | * @add2: addend2 | ||
| 171 | * | ||
| 172 | * Return: The sum of @add1 and @add2. | ||
| 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 | ||
| 195 | * @kt: addend | ||
| 196 | * @nsec: the scalar nsec value to add | ||
| 197 | * | ||
| 198 | * Return: The sum of @kt and @nsec in ktime_t format. | ||
| 199 | */ | ||
| 200 | extern ktime_t ktime_add_ns(const ktime_t kt, u64 nsec); | ||
| 201 | |||
| 202 | /** | ||
| 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 | * | ||
| 207 | * Return: The subtraction of @nsec from @kt in ktime_t format. | ||
| 208 | */ | ||
| 209 | extern ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec); | ||
| 210 | |||
| 211 | /** | ||
| 212 | * timespec_to_ktime - convert a timespec to ktime_t format | ||
| 213 | * @ts: the timespec variable to convert | ||
| 214 | * | ||
| 215 | * Return: A ktime_t variable with the converted timespec value. | ||
| 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 | ||
