compat_time.h 7.7 KB

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