ktime.h 7.2 KB

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