compat_time.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file compat_time.h
  7. *
  8. * \brief Functions and types for monotonic times.
  9. *
  10. * monotime_* functions try to provide a high-resolution monotonic timer with
  11. * something the best resolution the system provides. monotime_coarse_*
  12. * functions run faster (if the operating system gives us a way to do that)
  13. * but produce a less accurate timer: accuracy will probably be on the order
  14. * of tens of milliseconds.
  15. */
  16. #ifndef TOR_COMPAT_TIME_H
  17. #define TOR_COMPAT_TIME_H
  18. #include "orconfig.h"
  19. #include "lib/cc/torint.h"
  20. #include "lib/wallclock/tor_gettimeofday.h"
  21. #ifdef _WIN32
  22. #undef HAVE_CLOCK_GETTIME
  23. #endif
  24. #if defined(HAVE_CLOCK_GETTIME)
  25. /* to ensure definition of CLOCK_MONOTONIC_COARSE if it's there */
  26. #include <time.h>
  27. #endif
  28. #if !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
  29. /** Implementation of timeval for platforms that don't have it. */
  30. struct timeval {
  31. time_t tv_sec;
  32. unsigned int tv_usec;
  33. };
  34. #endif /* !defined(HAVE_STRUCT_TIMEVAL_TV_SEC) */
  35. /** Represents a monotonic timer in a platform-dependent way. */
  36. typedef struct monotime_t {
  37. #ifdef __APPLE__
  38. /* On apple, there is a 64-bit counter whose precision we must look up. */
  39. uint64_t abstime_;
  40. #elif defined(HAVE_CLOCK_GETTIME)
  41. /* It sure would be nice to use clock_gettime(). Posix is a nice thing. */
  42. struct timespec ts_;
  43. #elif defined (_WIN32)
  44. /* On Windows, there is a 64-bit counter whose precision we must look up. */
  45. int64_t pcount_;
  46. #else
  47. #define MONOTIME_USING_GETTIMEOFDAY
  48. /* Otherwise, we will be stuck using gettimeofday. */
  49. struct timeval tv_;
  50. #endif /* defined(__APPLE__) || ... */
  51. } monotime_t;
  52. #if defined(CLOCK_MONOTONIC_COARSE) && \
  53. defined(HAVE_CLOCK_GETTIME)
  54. #define MONOTIME_COARSE_FN_IS_DIFFERENT
  55. #define monotime_coarse_t monotime_t
  56. #elif defined(_WIN32)
  57. #define MONOTIME_COARSE_FN_IS_DIFFERENT
  58. #define MONOTIME_COARSE_TYPE_IS_DIFFERENT
  59. /** Represents a coarse monotonic time in a platform-independent way. */
  60. typedef struct monotime_coarse_t {
  61. uint64_t tick_count_;
  62. } monotime_coarse_t;
  63. #elif defined(__APPLE__) && defined(HAVE_MACH_APPROXIMATE_TIME)
  64. #define MONOTIME_COARSE_FN_IS_DIFFERENT
  65. #define monotime_coarse_t monotime_t
  66. #else
  67. #define monotime_coarse_t monotime_t
  68. #endif /* defined(CLOCK_MONOTONIC_COARSE) && ... || ... */
  69. /**
  70. * Initialize the timing subsystem. This function is idempotent.
  71. */
  72. void monotime_init(void);
  73. /**
  74. * Set <b>out</b> to the current time.
  75. */
  76. void monotime_get(monotime_t *out);
  77. /**
  78. * Return the number of nanoseconds between <b>start</b> and <b>end</b>.
  79. */
  80. int64_t monotime_diff_nsec(const monotime_t *start, const monotime_t *end);
  81. /**
  82. * Return the number of microseconds between <b>start</b> and <b>end</b>.
  83. */
  84. int64_t monotime_diff_usec(const monotime_t *start, const monotime_t *end);
  85. /**
  86. * Return the number of milliseconds between <b>start</b> and <b>end</b>.
  87. */
  88. int64_t monotime_diff_msec(const monotime_t *start, const monotime_t *end);
  89. /**
  90. * Return the number of nanoseconds since the timer system was initialized.
  91. */
  92. uint64_t monotime_absolute_nsec(void);
  93. /**
  94. * Return the number of microseconds since the timer system was initialized.
  95. */
  96. uint64_t monotime_absolute_usec(void);
  97. /**
  98. * Return the number of milliseconds since the timer system was initialized.
  99. */
  100. uint64_t monotime_absolute_msec(void);
  101. /**
  102. * Set <b>out</b> to zero.
  103. */
  104. void monotime_zero(monotime_t *out);
  105. /**
  106. * Return true iff <b>out</b> is zero
  107. */
  108. int monotime_is_zero(const monotime_t *out);
  109. /**
  110. * Set <b>out</b> to N milliseconds after <b>val</b>.
  111. */
  112. /* XXXX We should add a more generic function here if we ever need to */
  113. void monotime_add_msec(monotime_t *out, const monotime_t *val, uint32_t msec);
  114. #if defined(MONOTIME_COARSE_FN_IS_DIFFERENT)
  115. /**
  116. * Set <b>out</b> to the current coarse time.
  117. */
  118. void monotime_coarse_get(monotime_coarse_t *out);
  119. uint64_t monotime_coarse_absolute_nsec(void);
  120. uint64_t monotime_coarse_absolute_usec(void);
  121. uint64_t monotime_coarse_absolute_msec(void);
  122. #else /* !(defined(MONOTIME_COARSE_FN_IS_DIFFERENT)) */
  123. #define monotime_coarse_get monotime_get
  124. #define monotime_coarse_absolute_nsec monotime_absolute_nsec
  125. #define monotime_coarse_absolute_usec monotime_absolute_usec
  126. #define monotime_coarse_absolute_msec monotime_absolute_msec
  127. #endif /* defined(MONOTIME_COARSE_FN_IS_DIFFERENT) */
  128. /**
  129. * Return a "timestamp" approximation for a coarse monotonic timer.
  130. * This timestamp is meant to be fast to calculate and easy to
  131. * compare, and have a unit of something roughly around 1 msec.
  132. *
  133. * It will wrap over from time to time.
  134. *
  135. * It has no defined zero point.
  136. */
  137. uint32_t monotime_coarse_to_stamp(const monotime_coarse_t *t);
  138. /**
  139. * Convert a difference, expressed in the units of monotime_coarse_to_stamp,
  140. * into an approximate number of milliseconds.
  141. */
  142. uint64_t monotime_coarse_stamp_units_to_approx_msec(uint64_t units);
  143. uint64_t monotime_msec_to_approx_coarse_stamp_units(uint64_t msec);
  144. uint32_t monotime_coarse_get_stamp(void);
  145. #if defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT)
  146. int64_t monotime_coarse_diff_nsec(const monotime_coarse_t *start,
  147. const monotime_coarse_t *end);
  148. int64_t monotime_coarse_diff_usec(const monotime_coarse_t *start,
  149. const monotime_coarse_t *end);
  150. int64_t monotime_coarse_diff_msec(const monotime_coarse_t *start,
  151. const monotime_coarse_t *end);
  152. void monotime_coarse_zero(monotime_coarse_t *out);
  153. int monotime_coarse_is_zero(const monotime_coarse_t *val);
  154. void monotime_coarse_add_msec(monotime_coarse_t *out,
  155. const monotime_coarse_t *val, uint32_t msec);
  156. #else /* !(defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT)) */
  157. #define monotime_coarse_diff_nsec monotime_diff_nsec
  158. #define monotime_coarse_diff_usec monotime_diff_usec
  159. #define monotime_coarse_diff_msec monotime_diff_msec
  160. #define monotime_coarse_zero monotime_zero
  161. #define monotime_coarse_is_zero monotime_is_zero
  162. #define monotime_coarse_add_msec monotime_add_msec
  163. #endif /* defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT) */
  164. /**
  165. * As monotime_coarse_diff_msec, but avoid 64-bit division.
  166. *
  167. * Requires that the difference fit into an int32_t; not for use with
  168. * large time differences.
  169. */
  170. int32_t monotime_coarse_diff_msec32_(const monotime_coarse_t *start,
  171. const monotime_coarse_t *end);
  172. /**
  173. * As monotime_coarse_diff_msec, but avoid 64-bit division if it is expensive.
  174. *
  175. * Requires that the difference fit into an int32_t; not for use with
  176. * large time differences.
  177. */
  178. static inline int32_t
  179. monotime_coarse_diff_msec32(const monotime_coarse_t *start,
  180. const monotime_coarse_t *end)
  181. {
  182. #if SIZEOF_VOID_P == 8
  183. // on a 64-bit platform, let's assume 64/64 division is cheap.
  184. return (int32_t) monotime_coarse_diff_msec(start, end);
  185. #else
  186. return monotime_coarse_diff_msec32_(start, end);
  187. #endif
  188. }
  189. #ifdef TOR_UNIT_TESTS
  190. void tor_sleep_msec(int msec);
  191. void monotime_enable_test_mocking(void);
  192. void monotime_disable_test_mocking(void);
  193. void monotime_set_mock_time_nsec(int64_t);
  194. #if defined(MONOTIME_COARSE_FN_IS_DIFFERENT)
  195. void monotime_coarse_set_mock_time_nsec(int64_t);
  196. #else
  197. #define monotime_coarse_set_mock_time_nsec monotime_set_mock_time_nsec
  198. #endif
  199. #endif /* defined(TOR_UNIT_TESTS) */
  200. #ifdef COMPAT_TIME_PRIVATE
  201. #if defined(_WIN32) || defined(TOR_UNIT_TESTS)
  202. STATIC int64_t ratchet_performance_counter(int64_t count_raw);
  203. STATIC int64_t ratchet_coarse_performance_counter(int64_t count_raw);
  204. #endif
  205. #if defined(MONOTIME_USING_GETTIMEOFDAY) || defined(TOR_UNIT_TESTS)
  206. STATIC void ratchet_timeval(const struct timeval *timeval_raw,
  207. struct timeval *out);
  208. #endif
  209. #ifdef TOR_UNIT_TESTS
  210. void monotime_reset_ratchets_for_testing(void);
  211. #endif
  212. #endif /* defined(COMPAT_TIME_PRIVATE) */
  213. #endif /* !defined(TOR_COMPAT_TIME_H) */