ktime.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. * 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. *
  19. * For licencing details see kernel-base/COPYING
  20. */
  21. #ifndef _LINUX_KTIME_H
  22. #define _LINUX_KTIME_H
  23. #include <linux/time.h>
  24. #include <linux/jiffies.h>
  25. /*
  26. * ktime_t:
  27. *
  28. * On 64-bit CPUs a single 64-bit variable is used to store the hrtimers
  29. * internal representation of time values in scalar nanoseconds. The
  30. * design plays out best on 64-bit CPUs, where most conversions are
  31. * NOPs and most arithmetic ktime_t operations are plain arithmetic
  32. * operations.
  33. *
  34. * On 32-bit CPUs an optimized representation of the timespec structure
  35. * is used to avoid expensive conversions from and to timespecs. The
  36. * endian-aware order of the tv struct members is chosen to allow
  37. * mathematical operations on the tv64 member of the union too, which
  38. * for certain operations produces better code.
  39. *
  40. * For architectures with efficient support for 64/32-bit conversions the
  41. * plain scalar nanosecond based representation can be selected by the
  42. * config switch CONFIG_KTIME_SCALAR.
  43. */
  44. union ktime {
  45. s64 tv64;
  46. #if BITS_PER_LONG != 64 && !defined(CONFIG_KTIME_SCALAR)
  47. struct {
  48. # ifdef __BIG_ENDIAN
  49. s32 sec, nsec;
  50. # else
  51. s32 nsec, sec;
  52. # endif
  53. } tv;
  54. #endif
  55. };
  56. typedef union ktime ktime_t; /* Kill this */
  57. /*
  58. * ktime_t definitions when using the 64-bit scalar representation:
  59. */
  60. #if (BITS_PER_LONG == 64) || defined(CONFIG_KTIME_SCALAR)
  61. /**
  62. * ktime_set - Set a ktime_t variable from a seconds/nanoseconds value
  63. * @secs: seconds to set
  64. * @nsecs: nanoseconds to set
  65. *
  66. * Return the ktime_t representation of the value
  67. */
  68. static inline ktime_t ktime_set(const long secs, const unsigned long nsecs)
  69. {
  70. #if (BITS_PER_LONG == 64)
  71. if (unlikely(secs >= KTIME_SEC_MAX))
  72. return (ktime_t){ .tv64 = KTIME_MAX };
  73. #endif
  74. return (ktime_t) { .tv64 = (s64)secs * NSEC_PER_SEC + (s64)nsecs };
  75. }
  76. /* Subtract two ktime_t variables. rem = lhs -rhs: */
  77. #define ktime_sub(lhs, rhs) \
  78. ({ (ktime_t){ .tv64 = (lhs).tv64 - (rhs).tv64 }; })
  79. /* Add two ktime_t variables. res = lhs + rhs: */
  80. #define ktime_add(lhs, rhs) \
  81. ({ (ktime_t){ .tv64 = (lhs).tv64 + (rhs).tv64 }; })
  82. /*
  83. * Add a ktime_t variable and a scalar nanosecond value.
  84. * res = kt + nsval:
  85. */
  86. #define ktime_add_ns(kt, nsval) \
  87. ({ (ktime_t){ .tv64 = (kt).tv64 + (nsval) }; })
  88. /*
  89. * Subtract a scalar nanosecod from a ktime_t variable
  90. * res = kt - nsval:
  91. */
  92. #define ktime_sub_ns(kt, nsval) \
  93. ({ (ktime_t){ .tv64 = (kt).tv64 - (nsval) }; })
  94. /* convert a timespec to ktime_t format: */
  95. static inline ktime_t timespec_to_ktime(struct timespec ts)
  96. {
  97. return ktime_set(ts.tv_sec, ts.tv_nsec);
  98. }
  99. /* convert a timeval to ktime_t format: */
  100. static inline ktime_t timeval_to_ktime(struct timeval tv)
  101. {
  102. return ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC);
  103. }
  104. /* Map the ktime_t to timespec conversion to ns_to_timespec function */
  105. #define ktime_to_timespec(kt) ns_to_timespec((kt).tv64)
  106. /* Map the ktime_t to timeval conversion to ns_to_timeval function */
  107. #define ktime_to_timeval(kt) ns_to_timeval((kt).tv64)
  108. /* Convert ktime_t to nanoseconds - NOP in the scalar storage format: */
  109. #define ktime_to_ns(kt) ((kt).tv64)
  110. #else /* !((BITS_PER_LONG == 64) || defined(CONFIG_KTIME_SCALAR)) */
  111. /*
  112. * Helper macros/inlines to get the ktime_t math right in the timespec
  113. * representation. The macros are sometimes ugly - their actual use is
  114. * pretty okay-ish, given the circumstances. We do all this for
  115. * performance reasons. The pure scalar nsec_t based code was nice and
  116. * simple, but created too many 64-bit / 32-bit conversions and divisions.
  117. *
  118. * Be especially aware that negative values are represented in a way
  119. * that the tv.sec field is negative and the tv.nsec field is greater
  120. * or equal to zero but less than nanoseconds per second. This is the
  121. * same representation which is used by timespecs.
  122. *
  123. * tv.sec < 0 and 0 >= tv.nsec < NSEC_PER_SEC
  124. */
  125. /* Set a ktime_t variable to a value in sec/nsec representation: */
  126. static inline ktime_t ktime_set(const long secs, const unsigned long nsecs)
  127. {
  128. return (ktime_t) { .tv = { .sec = secs, .nsec = nsecs } };
  129. }
  130. /**
  131. * ktime_sub - subtract two ktime_t variables
  132. * @lhs: minuend
  133. * @rhs: subtrahend
  134. *
  135. * Returns the remainder of the subtraction
  136. */
  137. static inline ktime_t ktime_sub(const ktime_t lhs, const ktime_t rhs)
  138. {
  139. ktime_t res;
  140. res.tv64 = lhs.tv64 - rhs.tv64;
  141. if (res.tv.nsec < 0)
  142. res.tv.nsec += NSEC_PER_SEC;
  143. return res;
  144. }
  145. /**
  146. * ktime_add - add two ktime_t variables
  147. * @add1: addend1
  148. * @add2: addend2
  149. *
  150. * Returns the sum of @add1 and @add2.
  151. */
  152. static inline ktime_t ktime_add(const ktime_t add1, const ktime_t add2)
  153. {
  154. ktime_t res;
  155. res.tv64 = add1.tv64 + add2.tv64;
  156. /*
  157. * performance trick: the (u32) -NSEC gives 0x00000000Fxxxxxxx
  158. * so we subtract NSEC_PER_SEC and add 1 to the upper 32 bit.
  159. *
  160. * it's equivalent to:
  161. * tv.nsec -= NSEC_PER_SEC
  162. * tv.sec ++;
  163. */
  164. if (res.tv.nsec >= NSEC_PER_SEC)
  165. res.tv64 += (u32)-NSEC_PER_SEC;
  166. return res;
  167. }
  168. /**
  169. * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
  170. * @kt: addend
  171. * @nsec: the scalar nsec value to add
  172. *
  173. * Returns the sum of @kt and @nsec in ktime_t format
  174. */
  175. extern ktime_t ktime_add_ns(const ktime_t kt, u64 nsec);
  176. /**
  177. * ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
  178. * @kt: minuend
  179. * @nsec: the scalar nsec value to subtract
  180. *
  181. * Returns the subtraction of @nsec from @kt in ktime_t format
  182. */
  183. extern ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec);
  184. /**
  185. * timespec_to_ktime - convert a timespec to ktime_t format
  186. * @ts: the timespec variable to convert
  187. *
  188. * Returns a ktime_t variable with the converted timespec value
  189. */
  190. static inline ktime_t timespec_to_ktime(const struct timespec ts)
  191. {
  192. return (ktime_t) { .tv = { .sec = (s32)ts.tv_sec,
  193. .nsec = (s32)ts.tv_nsec } };
  194. }
  195. /**
  196. * timeval_to_ktime - convert a timeval to ktime_t format
  197. * @tv: the timeval variable to convert
  198. *
  199. * Returns a ktime_t variable with the converted timeval value
  200. */
  201. static inline ktime_t timeval_to_ktime(const struct timeval tv)
  202. {
  203. return (ktime_t) { .tv = { .sec = (s32)tv.tv_sec,
  204. .nsec = (s32)tv.tv_usec * 1000 } };
  205. }
  206. /**
  207. * ktime_to_timespec - convert a ktime_t variable to timespec format
  208. * @kt: the ktime_t variable to convert
  209. *
  210. * Returns the timespec representation of the ktime value
  211. */
  212. static inline struct timespec ktime_to_timespec(const ktime_t kt)
  213. {
  214. return (struct timespec) { .tv_sec = (time_t) kt.tv.sec,
  215. .tv_nsec = (long) kt.tv.nsec };
  216. }
  217. /**
  218. * ktime_to_timeval - convert a ktime_t variable to timeval format
  219. * @kt: the ktime_t variable to convert
  220. *
  221. * Returns the timeval representation of the ktime value
  222. */
  223. static inline struct timeval ktime_to_timeval(const ktime_t kt)
  224. {
  225. return (struct timeval) {
  226. .tv_sec = (time_t) kt.tv.sec,
  227. .tv_usec = (suseconds_t) (kt.tv.nsec / NSEC_PER_USEC) };
  228. }
  229. /**
  230. * ktime_to_ns - convert a ktime_t variable to scalar nanoseconds
  231. * @kt: the ktime_t variable to convert
  232. *
  233. * Returns the scalar nanoseconds representation of @kt
  234. */
  235. static inline s64 ktime_to_ns(const ktime_t kt)
  236. {
  237. return (s64) kt.tv.sec * NSEC_PER_SEC + kt.tv.nsec;
  238. }
  239. #endif /* !((BITS_PER_LONG == 64) || defined(CONFIG_KTIME_SCALAR)) */
  240. /**
  241. * ktime_equal - Compares two ktime_t variables to see if they are equal
  242. * @cmp1: comparable1
  243. * @cmp2: comparable2
  244. *
  245. * Compare two ktime_t variables, returns 1 if equal
  246. */
  247. static inline int ktime_equal(const ktime_t cmp1, const ktime_t cmp2)
  248. {
  249. return cmp1.tv64 == cmp2.tv64;
  250. }
  251. /**
  252. * ktime_compare - Compares two ktime_t variables for less, greater or equal
  253. * @cmp1: comparable1
  254. * @cmp2: comparable2
  255. *
  256. * Returns ...
  257. * cmp1 < cmp2: return <0
  258. * cmp1 == cmp2: return 0
  259. * cmp1 > cmp2: return >0
  260. */
  261. static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
  262. {
  263. if (cmp1.tv64 < cmp2.tv64)
  264. return -1;
  265. if (cmp1.tv64 > cmp2.tv64)
  266. return 1;
  267. return 0;
  268. }
  269. #if BITS_PER_LONG < 64
  270. extern s64 __ktime_divns(const ktime_t kt, s64 div);
  271. static inline s64 ktime_divns(const ktime_t kt, s64 div)
  272. {
  273. /*
  274. * Negative divisors could cause an inf loop,
  275. * so bug out here.
  276. */
  277. BUG_ON(div < 0);
  278. if (__builtin_constant_p(div) && !(div >> 32)) {
  279. s64 ns = kt.tv64;
  280. u64 tmp = ns < 0 ? -ns : ns;
  281. do_div(tmp, div);
  282. return ns < 0 ? -tmp : tmp;
  283. } else {
  284. return __ktime_divns(kt, div);
  285. }
  286. }
  287. #else /* BITS_PER_LONG < 64 */
  288. static inline s64 ktime_divns(const ktime_t kt, s64 div)
  289. {
  290. /*
  291. * 32-bit implementation cannot handle negative divisors,
  292. * so catch them on 64bit as well.
  293. */
  294. WARN_ON(div < 0);
  295. return kt.tv64 / div;
  296. }
  297. #endif
  298. static inline s64 ktime_to_us(const ktime_t kt)
  299. {
  300. return ktime_divns(kt, NSEC_PER_USEC);
  301. }
  302. static inline s64 ktime_to_ms(const ktime_t kt)
  303. {
  304. return ktime_divns(kt, NSEC_PER_MSEC);
  305. }
  306. static inline s64 ktime_us_delta(const ktime_t later, const ktime_t earlier)
  307. {
  308. return ktime_to_us(ktime_sub(later, earlier));
  309. }
  310. static inline ktime_t ktime_add_us(const ktime_t kt, const u64 usec)
  311. {
  312. return ktime_add_ns(kt, usec * 1000);
  313. }
  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. extern ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs);
  319. /*
  320. * The resolution of the clocks. The resolution value is returned in
  321. * the clock_getres() system call to give application programmers an
  322. * idea of the (in)accuracy of timers. Timer values are rounded up to
  323. * this resolution values.
  324. */
  325. #define LOW_RES_NSEC TICK_NSEC
  326. #define KTIME_LOW_RES (ktime_t){ .tv64 = LOW_RES_NSEC }
  327. /* Get the monotonic time in timespec format: */
  328. extern void ktime_get_ts(struct timespec *ts);
  329. /* Get the real (wall-) time in timespec format: */
  330. #define ktime_get_real_ts(ts) getnstimeofday(ts)
  331. static inline ktime_t ns_to_ktime(u64 ns)
  332. {
  333. static const ktime_t ktime_zero = { .tv64 = 0 };
  334. return ktime_add_ns(ktime_zero, ns);
  335. }
  336. #endif