ktime.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. * For licencing details see kernel-base/COPYING
  14. */
  15. #ifndef _LINUX_KTIME_H
  16. #define _LINUX_KTIME_H
  17. #include <linux/time.h>
  18. #include <linux/jiffies.h>
  19. /*
  20. * ktime_t:
  21. *
  22. * On 64-bit CPUs a single 64-bit variable is used to store the hrtimers
  23. * internal representation of time values in scalar nanoseconds. The
  24. * design plays out best on 64-bit CPUs, where most conversions are
  25. * NOPs and most arithmetic ktime_t operations are plain arithmetic
  26. * operations.
  27. *
  28. * On 32-bit CPUs an optimized representation of the timespec structure
  29. * is used to avoid expensive conversions from and to timespecs. The
  30. * endian-aware order of the tv struct members is choosen to allow
  31. * mathematical operations on the tv64 member of the union too, which
  32. * for certain operations produces better code.
  33. *
  34. * For architectures with efficient support for 64/32-bit conversions the
  35. * plain scalar nanosecond based representation can be selected by the
  36. * config switch CONFIG_KTIME_SCALAR.
  37. */
  38. typedef union {
  39. s64 tv64;
  40. #if BITS_PER_LONG != 64 && !defined(CONFIG_KTIME_SCALAR)
  41. struct {
  42. # ifdef __BIG_ENDIAN
  43. s32 sec, nsec;
  44. # else
  45. s32 nsec, sec;
  46. # endif
  47. } tv;
  48. #endif
  49. } ktime_t;
  50. #define KTIME_MAX (~((u64)1 << 63))
  51. /*
  52. * ktime_t definitions when using the 64-bit scalar representation:
  53. */
  54. #if (BITS_PER_LONG == 64) || defined(CONFIG_KTIME_SCALAR)
  55. /* Define a ktime_t variable and initialize it to zero: */
  56. #define DEFINE_KTIME(kt) ktime_t kt = { .tv64 = 0 }
  57. /**
  58. * ktime_set - Set a ktime_t variable from a seconds/nanoseconds value
  59. *
  60. * @secs: seconds to set
  61. * @nsecs: nanoseconds to set
  62. *
  63. * Return the ktime_t representation of the value
  64. */
  65. static inline ktime_t ktime_set(const long secs, const unsigned long nsecs)
  66. {
  67. return (ktime_t) { .tv64 = (s64)secs * NSEC_PER_SEC + (s64)nsecs };
  68. }
  69. /* Subtract two ktime_t variables. rem = lhs -rhs: */
  70. #define ktime_sub(lhs, rhs) \
  71. ({ (ktime_t){ .tv64 = (lhs).tv64 - (rhs).tv64 }; })
  72. /* Add two ktime_t variables. res = lhs + rhs: */
  73. #define ktime_add(lhs, rhs) \
  74. ({ (ktime_t){ .tv64 = (lhs).tv64 + (rhs).tv64 }; })
  75. /*
  76. * Add a ktime_t variable and a scalar nanosecond value.
  77. * res = kt + nsval:
  78. */
  79. #define ktime_add_ns(kt, nsval) \
  80. ({ (ktime_t){ .tv64 = (kt).tv64 + (nsval) }; })
  81. /* convert a timespec to ktime_t format: */
  82. #define timespec_to_ktime(ts) ktime_set((ts).tv_sec, (ts).tv_nsec)
  83. /* convert a timeval to ktime_t format: */
  84. #define timeval_to_ktime(tv) ktime_set((tv).tv_sec, (tv).tv_usec * 1000)
  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 timeval conversion to ns_to_timeval function */
  88. #define ktime_to_timeval(kt) ns_to_timeval((kt).tv64)
  89. /* Map the ktime_t to clock_t conversion to the inline in jiffies.h: */
  90. #define ktime_to_clock_t(kt) nsec_to_clock_t((kt).tv64)
  91. /* Convert ktime_t to nanoseconds - NOP in the scalar storage format: */
  92. #define ktime_to_ns(kt) ((kt).tv64)
  93. #else
  94. /*
  95. * Helper macros/inlines to get the ktime_t math right in the timespec
  96. * representation. The macros are sometimes ugly - their actual use is
  97. * pretty okay-ish, given the circumstances. We do all this for
  98. * performance reasons. The pure scalar nsec_t based code was nice and
  99. * simple, but created too many 64-bit / 32-bit conversions and divisions.
  100. *
  101. * Be especially aware that negative values are represented in a way
  102. * that the tv.sec field is negative and the tv.nsec field is greater
  103. * or equal to zero but less than nanoseconds per second. This is the
  104. * same representation which is used by timespecs.
  105. *
  106. * tv.sec < 0 and 0 >= tv.nsec < NSEC_PER_SEC
  107. */
  108. /* Define a ktime_t variable and initialize it to zero: */
  109. #define DEFINE_KTIME(kt) ktime_t kt = { .tv64 = 0 }
  110. /* Set a ktime_t variable to a value in sec/nsec representation: */
  111. static inline ktime_t ktime_set(const long secs, const unsigned long nsecs)
  112. {
  113. return (ktime_t) { .tv = { .sec = secs, .nsec = nsecs } };
  114. }
  115. /**
  116. * ktime_sub - subtract two ktime_t variables
  117. *
  118. * @lhs: minuend
  119. * @rhs: subtrahend
  120. *
  121. * Returns the remainder of the substraction
  122. */
  123. static inline ktime_t ktime_sub(const ktime_t lhs, const ktime_t rhs)
  124. {
  125. ktime_t res;
  126. res.tv64 = lhs.tv64 - rhs.tv64;
  127. if (res.tv.nsec < 0)
  128. res.tv.nsec += NSEC_PER_SEC;
  129. return res;
  130. }
  131. /**
  132. * ktime_add - add two ktime_t variables
  133. *
  134. * @add1: addend1
  135. * @add2: addend2
  136. *
  137. * Returns the sum of addend1 and addend2
  138. */
  139. static inline ktime_t ktime_add(const ktime_t add1, const ktime_t add2)
  140. {
  141. ktime_t res;
  142. res.tv64 = add1.tv64 + add2.tv64;
  143. /*
  144. * performance trick: the (u32) -NSEC gives 0x00000000Fxxxxxxx
  145. * so we subtract NSEC_PER_SEC and add 1 to the upper 32 bit.
  146. *
  147. * it's equivalent to:
  148. * tv.nsec -= NSEC_PER_SEC
  149. * tv.sec ++;
  150. */
  151. if (res.tv.nsec >= NSEC_PER_SEC)
  152. res.tv64 += (u32)-NSEC_PER_SEC;
  153. return res;
  154. }
  155. /**
  156. * ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
  157. *
  158. * @kt: addend
  159. * @nsec: the scalar nsec value to add
  160. *
  161. * Returns the sum of kt and nsec in ktime_t format
  162. */
  163. extern ktime_t ktime_add_ns(const ktime_t kt, u64 nsec);
  164. /**
  165. * timespec_to_ktime - convert a timespec to ktime_t format
  166. *
  167. * @ts: the timespec variable to convert
  168. *
  169. * Returns a ktime_t variable with the converted timespec value
  170. */
  171. static inline ktime_t timespec_to_ktime(const struct timespec ts)
  172. {
  173. return (ktime_t) { .tv = { .sec = (s32)ts.tv_sec,
  174. .nsec = (s32)ts.tv_nsec } };
  175. }
  176. /**
  177. * timeval_to_ktime - convert a timeval to ktime_t format
  178. *
  179. * @tv: the timeval variable to convert
  180. *
  181. * Returns a ktime_t variable with the converted timeval value
  182. */
  183. static inline ktime_t timeval_to_ktime(const struct timeval tv)
  184. {
  185. return (ktime_t) { .tv = { .sec = (s32)tv.tv_sec,
  186. .nsec = (s32)tv.tv_usec * 1000 } };
  187. }
  188. /**
  189. * ktime_to_timespec - convert a ktime_t variable to timespec format
  190. *
  191. * @kt: the ktime_t variable to convert
  192. *
  193. * Returns the timespec representation of the ktime value
  194. */
  195. static inline struct timespec ktime_to_timespec(const ktime_t kt)
  196. {
  197. return (struct timespec) { .tv_sec = (time_t) kt.tv.sec,
  198. .tv_nsec = (long) kt.tv.nsec };
  199. }
  200. /**
  201. * ktime_to_timeval - convert a ktime_t variable to timeval format
  202. *
  203. * @kt: the ktime_t variable to convert
  204. *
  205. * Returns the timeval representation of the ktime value
  206. */
  207. static inline struct timeval ktime_to_timeval(const ktime_t kt)
  208. {
  209. return (struct timeval) {
  210. .tv_sec = (time_t) kt.tv.sec,
  211. .tv_usec = (suseconds_t) (kt.tv.nsec / NSEC_PER_USEC) };
  212. }
  213. /**
  214. * ktime_to_clock_t - convert a ktime_t variable to clock_t format
  215. * @kt: the ktime_t variable to convert
  216. *
  217. * Returns a clock_t variable with the converted value
  218. */
  219. static inline clock_t ktime_to_clock_t(const ktime_t kt)
  220. {
  221. return nsec_to_clock_t( (u64) kt.tv.sec * NSEC_PER_SEC + kt.tv.nsec);
  222. }
  223. /**
  224. * ktime_to_ns - convert a ktime_t variable to scalar nanoseconds
  225. * @kt: the ktime_t variable to convert
  226. *
  227. * Returns the scalar nanoseconds representation of kt
  228. */
  229. static inline u64 ktime_to_ns(const ktime_t kt)
  230. {
  231. return (u64) kt.tv.sec * NSEC_PER_SEC + kt.tv.nsec;
  232. }
  233. #endif
  234. #endif