ktime.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #include <asm/bug.h>
  26. /* Nanosecond scalar representation for kernel time values */
  27. typedef s64 ktime_t;
  28. /**
  29. * ktime_set - Set a ktime_t variable from a seconds/nanoseconds value
  30. * @secs: seconds to set
  31. * @nsecs: nanoseconds to set
  32. *
  33. * Return: The ktime_t representation of the value.
  34. */
  35. static inline ktime_t ktime_set(const s64 secs, const unsigned long nsecs)
  36. {
  37. if (unlikely(secs >= KTIME_SEC_MAX))
  38. return KTIME_MAX;
  39. return secs * NSEC_PER_SEC + (s64)nsecs;
  40. }
  41. /* Subtract two ktime_t variables. rem = lhs -rhs: */
  42. #define ktime_sub(lhs, rhs) ((lhs) - (rhs))
  43. /* Add two ktime_t variables. res = lhs + rhs: */
  44. #define ktime_add(lhs, rhs) ((lhs) + (rhs))
  45. /*
  46. * Same as ktime_add(), but avoids undefined behaviour on overflow; however,
  47. * this means that you must check the result for overflow yourself.
  48. */
  49. #define ktime_add_unsafe(lhs, rhs) ((u64) (lhs) + (rhs))
  50. /*
  51. * Add a ktime_t variable and a scalar nanosecond value.
  52. * res = kt + nsval:
  53. */
  54. #define ktime_add_ns(kt, nsval) ((kt) + (nsval))
  55. /*
  56. * Subtract a scalar nanosecod from a ktime_t variable
  57. * res = kt - nsval:
  58. */
  59. #define ktime_sub_ns(kt, nsval) ((kt) - (nsval))
  60. /* convert a timespec64 to ktime_t format: */
  61. static inline ktime_t timespec64_to_ktime(struct timespec64 ts)
  62. {
  63. return ktime_set(ts.tv_sec, ts.tv_nsec);
  64. }
  65. /* Map the ktime_t to timespec conversion to ns_to_timespec function */
  66. #define ktime_to_timespec64(kt) ns_to_timespec64((kt))
  67. /* Convert ktime_t to nanoseconds */
  68. static inline s64 ktime_to_ns(const ktime_t kt)
  69. {
  70. return kt;
  71. }
  72. /**
  73. * ktime_compare - Compares two ktime_t variables for less, greater or equal
  74. * @cmp1: comparable1
  75. * @cmp2: comparable2
  76. *
  77. * Return: ...
  78. * cmp1 < cmp2: return <0
  79. * cmp1 == cmp2: return 0
  80. * cmp1 > cmp2: return >0
  81. */
  82. static inline int ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
  83. {
  84. if (cmp1 < cmp2)
  85. return -1;
  86. if (cmp1 > cmp2)
  87. return 1;
  88. return 0;
  89. }
  90. /**
  91. * ktime_after - Compare if a ktime_t value is bigger than another one.
  92. * @cmp1: comparable1
  93. * @cmp2: comparable2
  94. *
  95. * Return: true if cmp1 happened after cmp2.
  96. */
  97. static inline bool ktime_after(const ktime_t cmp1, const ktime_t cmp2)
  98. {
  99. return ktime_compare(cmp1, cmp2) > 0;
  100. }
  101. /**
  102. * ktime_before - Compare if a ktime_t value is smaller than another one.
  103. * @cmp1: comparable1
  104. * @cmp2: comparable2
  105. *
  106. * Return: true if cmp1 happened before cmp2.
  107. */
  108. static inline bool ktime_before(const ktime_t cmp1, const ktime_t cmp2)
  109. {
  110. return ktime_compare(cmp1, cmp2) < 0;
  111. }
  112. #if BITS_PER_LONG < 64
  113. extern s64 __ktime_divns(const ktime_t kt, s64 div);
  114. static inline s64 ktime_divns(const ktime_t kt, s64 div)
  115. {
  116. /*
  117. * Negative divisors could cause an inf loop,
  118. * so bug out here.
  119. */
  120. BUG_ON(div < 0);
  121. if (__builtin_constant_p(div) && !(div >> 32)) {
  122. s64 ns = kt;
  123. u64 tmp = ns < 0 ? -ns : ns;
  124. do_div(tmp, div);
  125. return ns < 0 ? -tmp : tmp;
  126. } else {
  127. return __ktime_divns(kt, div);
  128. }
  129. }
  130. #else /* BITS_PER_LONG < 64 */
  131. static inline s64 ktime_divns(const ktime_t kt, s64 div)
  132. {
  133. /*
  134. * 32-bit implementation cannot handle negative divisors,
  135. * so catch them on 64bit as well.
  136. */
  137. WARN_ON(div < 0);
  138. return kt / div;
  139. }
  140. #endif
  141. static inline s64 ktime_to_us(const ktime_t kt)
  142. {
  143. return ktime_divns(kt, NSEC_PER_USEC);
  144. }
  145. static inline s64 ktime_to_ms(const ktime_t kt)
  146. {
  147. return ktime_divns(kt, NSEC_PER_MSEC);
  148. }
  149. static inline s64 ktime_us_delta(const ktime_t later, const ktime_t earlier)
  150. {
  151. return ktime_to_us(ktime_sub(later, earlier));
  152. }
  153. static inline s64 ktime_ms_delta(const ktime_t later, const ktime_t earlier)
  154. {
  155. return ktime_to_ms(ktime_sub(later, earlier));
  156. }
  157. static inline ktime_t ktime_add_us(const ktime_t kt, const u64 usec)
  158. {
  159. return ktime_add_ns(kt, usec * NSEC_PER_USEC);
  160. }
  161. static inline ktime_t ktime_add_ms(const ktime_t kt, const u64 msec)
  162. {
  163. return ktime_add_ns(kt, msec * NSEC_PER_MSEC);
  164. }
  165. static inline ktime_t ktime_sub_us(const ktime_t kt, const u64 usec)
  166. {
  167. return ktime_sub_ns(kt, usec * NSEC_PER_USEC);
  168. }
  169. static inline ktime_t ktime_sub_ms(const ktime_t kt, const u64 msec)
  170. {
  171. return ktime_sub_ns(kt, msec * NSEC_PER_MSEC);
  172. }
  173. extern ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs);
  174. /**
  175. * ktime_to_timespec64_cond - convert a ktime_t variable to timespec64
  176. * format only if the variable contains data
  177. * @kt: the ktime_t variable to convert
  178. * @ts: the timespec variable to store the result in
  179. *
  180. * Return: %true if there was a successful conversion, %false if kt was 0.
  181. */
  182. static inline __must_check bool ktime_to_timespec64_cond(const ktime_t kt,
  183. struct timespec64 *ts)
  184. {
  185. if (kt) {
  186. *ts = ktime_to_timespec64(kt);
  187. return true;
  188. } else {
  189. return false;
  190. }
  191. }
  192. #include <vdso/ktime.h>
  193. static inline ktime_t ns_to_ktime(u64 ns)
  194. {
  195. return ns;
  196. }
  197. static inline ktime_t ms_to_ktime(u64 ms)
  198. {
  199. return ms * NSEC_PER_MSEC;
  200. }
  201. # include <linux/timekeeping.h>
  202. # include <linux/timekeeping32.h>
  203. #endif