ktime.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. * 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. */
  35. union ktime {
  36. s64 tv64;
  37. };
  38. typedef union ktime ktime_t; /* Kill this */
  39. /**
  40. * ktime_set - Set a ktime_t variable from a seconds/nanoseconds value
  41. * @secs: seconds to set
  42. * @nsecs: nanoseconds to set
  43. *
  44. * Return: The ktime_t representation of the value.
  45. */
  46. static inline ktime_t ktime_set(const s64 secs, const unsigned long nsecs)
  47. {
  48. if (unlikely(secs >= KTIME_SEC_MAX))
  49. return (ktime_t){ .tv64 = KTIME_MAX };
  50. return (ktime_t) { .tv64 = secs * NSEC_PER_SEC + (s64)nsecs };
  51. }
  52. /* Subtract two ktime_t variables. rem = lhs -rhs: */
  53. #define ktime_sub(lhs, rhs) \
  54. ({ (ktime_t){ .tv64 = (lhs).tv64 - (rhs).tv64 }; })
  55. /* Add two ktime_t variables. res = lhs + rhs: */
  56. #define ktime_add(lhs, rhs) \
  57. ({ (ktime_t){ .tv64 = (lhs).tv64 + (rhs).tv64 }; })
  58. /*
  59. * Same as ktime_add(), but avoids undefined behaviour on overflow; however,
  60. * this means that you must check the result for overflow yourself.
  61. */
  62. #define ktime_add_unsafe(lhs, rhs) \
  63. ({ (ktime_t){ .tv64 = (u64) (lhs).tv64 + (rhs).tv64 }; })
  64. /*
  65. * Add a ktime_t variable and a scalar nanosecond value.
  66. * res = kt + nsval:
  67. */
  68. #define ktime_add_ns(kt, nsval) \
  69. ({ (ktime_t){ .tv64 = (kt).tv64 + (nsval) }; })
  70. /*
  71. * Subtract a scalar nanosecod from a ktime_t variable
  72. * res = kt - nsval:
  73. */
  74. #define ktime_sub_ns(kt, nsval) \
  75. ({ (ktime_t){ .tv64 = (kt).tv64 - (nsval) }; })
  76. /* convert a timespec to ktime_t format: */
  77. static inline ktime_t timespec_to_ktime(struct timespec ts)
  78. {
  79. return ktime_set(ts.tv_sec, ts.tv_nsec);
  80. }
  81. /* convert a timespec64 to ktime_t format: */
  82. static inline ktime_t timespec64_to_ktime(struct timespec64 ts)
  83. {
  84. return ktime_set(ts.tv_sec, ts.tv_nsec);
  85. }
  86. /* convert a timeval to ktime_t format: */
  87. static inline ktime_t timeval_to_ktime(struct timeval tv)
  88. {
  89. return ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC);
  90. }
  91. /* Map the ktime_t to timespec conversion to ns_to_timespec function */
  92. #define ktime_to_timespec(kt) ns_to_timespec((kt).tv64)
  93. /* Map the ktime_t to timespec conversion to ns_to_timespec function */
  94. #define ktime_to_timespec64(kt) ns_to_timespec64((kt).tv64)
  95. /* Map the ktime_t to timeval conversion to ns_to_timeval function */
  96. #define ktime_to_timeval(kt) ns_to_timeval((kt).tv64)
  97. /* Convert ktime_t to nanoseconds - NOP in the scalar storage format: */
  98. #define ktime_to_ns(kt) ((kt).tv64)
  99. /**
  100. * ktime_equal - Compares two ktime_t variables to see if they are equal
  101. * @cmp1: comparable1
  102. * @cmp2: comparable2
  103. *
  104. * Compare two ktime_t variables.
  105. *
  106. * Return: 1 if equal.
  107. */
  108. static inline int ktime_equal(const ktime_t cmp1, const ktime_t cmp2)
  109. {
  110. return cmp1.tv64 == cmp2.tv64;
  111. }
  112. /**
  113. * ktime_compare - Compares two ktime_t variables for less, greater or equal
  114. * @cmp1: comparable1
  115. * @cmp2: comparable2
  116. *
  117. * Return: ...
  118. * cmp1 < cmp2: return <0
  119. * cmp1 == cmp2: return 0
  120. * cmp1 > cmp2: return >0
  121. */
  122. static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
  123. {
  124. if (cmp1.tv64 < cmp2.tv64)
  125. return -1;
  126. if (cmp1.tv64 > cmp2.tv64)
  127. return 1;
  128. return 0;
  129. }
  130. /**
  131. * ktime_after - Compare if a ktime_t value is bigger than another one.
  132. * @cmp1: comparable1
  133. * @cmp2: comparable2
  134. *
  135. * Return: true if cmp1 happened after cmp2.
  136. */
  137. static inline bool ktime_after(const ktime_t cmp1, const ktime_t cmp2)
  138. {
  139. return ktime_compare(cmp1, cmp2) > 0;
  140. }
  141. /**
  142. * ktime_before - Compare if a ktime_t value is smaller than another one.
  143. * @cmp1: comparable1
  144. * @cmp2: comparable2
  145. *
  146. * Return: true if cmp1 happened before cmp2.
  147. */
  148. static inline bool ktime_before(const ktime_t cmp1, const ktime_t cmp2)
  149. {
  150. return ktime_compare(cmp1, cmp2) < 0;
  151. }
  152. #if BITS_PER_LONG < 64
  153. extern s64 __ktime_divns(const ktime_t kt, s64 div);
  154. static inline s64 ktime_divns(const ktime_t kt, s64 div)
  155. {
  156. /*
  157. * Negative divisors could cause an inf loop,
  158. * so bug out here.
  159. */
  160. BUG_ON(div < 0);
  161. if (__builtin_constant_p(div) && !(div >> 32)) {
  162. s64 ns = kt.tv64;
  163. u64 tmp = ns < 0 ? -ns : ns;
  164. do_div(tmp, div);
  165. return ns < 0 ? -tmp : tmp;
  166. } else {
  167. return __ktime_divns(kt, div);
  168. }
  169. }
  170. #else /* BITS_PER_LONG < 64 */
  171. static inline s64 ktime_divns(const ktime_t kt, s64 div)
  172. {
  173. /*
  174. * 32-bit implementation cannot handle negative divisors,
  175. * so catch them on 64bit as well.
  176. */
  177. WARN_ON(div < 0);
  178. return kt.tv64 / div;
  179. }
  180. #endif
  181. static inline s64 ktime_to_us(const ktime_t kt)
  182. {
  183. return ktime_divns(kt, NSEC_PER_USEC);
  184. }
  185. static inline s64 ktime_to_ms(const ktime_t kt)
  186. {
  187. return ktime_divns(kt, NSEC_PER_MSEC);
  188. }
  189. static inline s64 ktime_us_delta(const ktime_t later, const ktime_t earlier)
  190. {
  191. return ktime_to_us(ktime_sub(later, earlier));
  192. }
  193. static inline s64 ktime_ms_delta(const ktime_t later, const ktime_t earlier)
  194. {
  195. return ktime_to_ms(ktime_sub(later, earlier));
  196. }
  197. static inline ktime_t ktime_add_us(const ktime_t kt, const u64 usec)
  198. {
  199. return ktime_add_ns(kt, usec * NSEC_PER_USEC);
  200. }
  201. static inline ktime_t ktime_add_ms(const ktime_t kt, const u64 msec)
  202. {
  203. return ktime_add_ns(kt, msec * NSEC_PER_MSEC);
  204. }
  205. static inline ktime_t ktime_sub_us(const ktime_t kt, const u64 usec)
  206. {
  207. return ktime_sub_ns(kt, usec * NSEC_PER_USEC);
  208. }
  209. extern ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs);
  210. /**
  211. * ktime_to_timespec_cond - convert a ktime_t variable to timespec
  212. * format only if the variable contains data
  213. * @kt: the ktime_t variable to convert
  214. * @ts: the timespec variable to store the result in
  215. *
  216. * Return: %true if there was a successful conversion, %false if kt was 0.
  217. */
  218. static inline __must_check bool ktime_to_timespec_cond(const ktime_t kt,
  219. struct timespec *ts)
  220. {
  221. if (kt.tv64) {
  222. *ts = ktime_to_timespec(kt);
  223. return true;
  224. } else {
  225. return false;
  226. }
  227. }
  228. /**
  229. * ktime_to_timespec64_cond - convert a ktime_t variable to timespec64
  230. * format only if the variable contains data
  231. * @kt: the ktime_t variable to convert
  232. * @ts: the timespec variable to store the result in
  233. *
  234. * Return: %true if there was a successful conversion, %false if kt was 0.
  235. */
  236. static inline __must_check bool ktime_to_timespec64_cond(const ktime_t kt,
  237. struct timespec64 *ts)
  238. {
  239. if (kt.tv64) {
  240. *ts = ktime_to_timespec64(kt);
  241. return true;
  242. } else {
  243. return false;
  244. }
  245. }
  246. /*
  247. * The resolution of the clocks. The resolution value is returned in
  248. * the clock_getres() system call to give application programmers an
  249. * idea of the (in)accuracy of timers. Timer values are rounded up to
  250. * this resolution values.
  251. */
  252. #define LOW_RES_NSEC TICK_NSEC
  253. #define KTIME_LOW_RES (ktime_t){ .tv64 = LOW_RES_NSEC }
  254. static inline ktime_t ns_to_ktime(u64 ns)
  255. {
  256. static const ktime_t ktime_zero = { .tv64 = 0 };
  257. return ktime_add_ns(ktime_zero, ns);
  258. }
  259. static inline ktime_t ms_to_ktime(u64 ms)
  260. {
  261. static const ktime_t ktime_zero = { .tv64 = 0 };
  262. return ktime_add_ms(ktime_zero, ms);
  263. }
  264. # include <linux/timekeeping.h>
  265. #endif