compat_time.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2017, 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. #ifdef _WIN32
  20. #undef HAVE_CLOCK_GETTIME
  21. #endif
  22. #if defined(HAVE_CLOCK_GETTIME)
  23. /* to ensure definition of CLOCK_MONOTONIC_COARSE if it's there */
  24. #include <time.h>
  25. #endif
  26. #if !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
  27. /** Implementation of timeval for platforms that don't have it. */
  28. struct timeval {
  29. time_t tv_sec;
  30. unsigned int tv_usec;
  31. };
  32. #endif /* !defined(HAVE_STRUCT_TIMEVAL_TV_SEC) */
  33. /** Represents a monotonic timer in a platform-dependent way. */
  34. typedef struct monotime_t {
  35. #ifdef __APPLE__
  36. /* On apple, there is a 64-bit counter whose precision we must look up. */
  37. uint64_t abstime_;
  38. #elif defined(HAVE_CLOCK_GETTIME)
  39. /* It sure would be nice to use clock_gettime(). Posix is a nice thing. */
  40. struct timespec ts_;
  41. #elif defined (_WIN32)
  42. /* On Windows, there is a 64-bit counter whose precision we must look up. */
  43. int64_t pcount_;
  44. #else
  45. #define MONOTIME_USING_GETTIMEOFDAY
  46. /* Otherwise, we will be stuck using gettimeofday. */
  47. struct timeval tv_;
  48. #endif /* defined(__APPLE__) || ... */
  49. } monotime_t;
  50. #if defined(CLOCK_MONOTONIC_COARSE) && \
  51. defined(HAVE_CLOCK_GETTIME)
  52. #define MONOTIME_COARSE_FN_IS_DIFFERENT
  53. #define monotime_coarse_t monotime_t
  54. #elif defined(_WIN32)
  55. #define MONOTIME_COARSE_FN_IS_DIFFERENT
  56. #define MONOTIME_COARSE_TYPE_IS_DIFFERENT
  57. /** Represents a coarse monotonic time in a platform-independent way. */
  58. typedef struct monotime_coarse_t {
  59. uint64_t tick_count_;
  60. } monotime_coarse_t;
  61. #elif defined(__APPLE__) && defined(HAVE_MACH_APPROXIMATE_TIME)
  62. #define MONOTIME_COARSE_FN_IS_DIFFERENT
  63. #define monotime_coarse_t monotime_t
  64. #else
  65. #define monotime_coarse_t monotime_t
  66. #endif /* defined(CLOCK_MONOTONIC_COARSE) && ... || ... */
  67. /**
  68. * Initialize the timing subsystem. This function is idempotent.
  69. */
  70. void monotime_init(void);
  71. /**
  72. * Set <b>out</b> to the current time.
  73. */
  74. void monotime_get(monotime_t *out);
  75. /**
  76. * Return the number of nanoseconds between <b>start</b> and <b>end</b>.
  77. */
  78. int64_t monotime_diff_nsec(const monotime_t *start, const monotime_t *end);
  79. /**
  80. * Return the number of microseconds between <b>start</b> and <b>end</b>.
  81. */
  82. int64_t monotime_diff_usec(const monotime_t *start, const monotime_t *end);
  83. /**
  84. * Return the number of milliseconds between <b>start</b> and <b>end</b>.
  85. */
  86. int64_t monotime_diff_msec(const monotime_t *start, const monotime_t *end);
  87. /**
  88. * Return the number of nanoseconds since the timer system was initialized.
  89. */
  90. uint64_t monotime_absolute_nsec(void);
  91. /**
  92. * Return the number of microseconds since the timer system was initialized.
  93. */
  94. uint64_t monotime_absolute_usec(void);
  95. /**
  96. * Return the number of milliseconds since the timer system was initialized.
  97. */
  98. uint64_t monotime_absolute_msec(void);
  99. #if defined(MONOTIME_COARSE_FN_IS_DIFFERENT)
  100. /**
  101. * Set <b>out</b> to the current coarse time.
  102. */
  103. void monotime_coarse_get(monotime_coarse_t *out);
  104. uint64_t monotime_coarse_absolute_nsec(void);
  105. uint64_t monotime_coarse_absolute_usec(void);
  106. uint64_t monotime_coarse_absolute_msec(void);
  107. #else /* !(defined(MONOTIME_COARSE_FN_IS_DIFFERENT)) */
  108. #define monotime_coarse_get monotime_get
  109. #define monotime_coarse_absolute_nsec monotime_absolute_nsec
  110. #define monotime_coarse_absolute_usec monotime_absolute_usec
  111. #define monotime_coarse_absolute_msec monotime_absolute_msec
  112. #endif /* defined(MONOTIME_COARSE_FN_IS_DIFFERENT) */
  113. /**
  114. * Return a "timestamp" approximation for a coarse monotonic timer.
  115. * This timestamp is meant to be fast to calculate and easy to
  116. * compare, and have a unit of something roughly around 1 msec.
  117. *
  118. * It will wrap over from time to time.
  119. *
  120. * It has no defined zero point.
  121. */
  122. uint32_t monotime_coarse_to_stamp(const monotime_coarse_t *t);
  123. /**
  124. * Convert a difference, expressed in the units of monotime_coarse_to_stamp,
  125. * into an approximate number of milliseconds.
  126. */
  127. uint64_t monotime_coarse_stamp_units_to_approx_msec(uint64_t units);
  128. uint32_t monotime_coarse_get_stamp(void);
  129. #if defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT)
  130. int64_t monotime_coarse_diff_nsec(const monotime_coarse_t *start,
  131. const monotime_coarse_t *end);
  132. int64_t monotime_coarse_diff_usec(const monotime_coarse_t *start,
  133. const monotime_coarse_t *end);
  134. int64_t monotime_coarse_diff_msec(const monotime_coarse_t *start,
  135. const monotime_coarse_t *end);
  136. #else /* !(defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT)) */
  137. #define monotime_coarse_diff_nsec monotime_diff_nsec
  138. #define monotime_coarse_diff_usec monotime_diff_usec
  139. #define monotime_coarse_diff_msec monotime_diff_msec
  140. #endif /* defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT) */
  141. void tor_gettimeofday(struct timeval *timeval);
  142. #ifdef TOR_UNIT_TESTS
  143. void tor_sleep_msec(int msec);
  144. void monotime_enable_test_mocking(void);
  145. void monotime_disable_test_mocking(void);
  146. void monotime_set_mock_time_nsec(int64_t);
  147. #if defined(MONOTIME_COARSE_FN_IS_DIFFERENT)
  148. void monotime_coarse_set_mock_time_nsec(int64_t);
  149. #else
  150. #define monotime_coarse_set_mock_time_nsec monotime_set_mock_time_nsec
  151. #endif
  152. #endif /* defined(TOR_UNIT_TESTS) */
  153. #ifdef COMPAT_TIME_PRIVATE
  154. #if defined(_WIN32) || defined(TOR_UNIT_TESTS)
  155. STATIC int64_t ratchet_performance_counter(int64_t count_raw);
  156. STATIC int64_t ratchet_coarse_performance_counter(int64_t count_raw);
  157. #endif
  158. #if defined(MONOTIME_USING_GETTIMEOFDAY) || defined(TOR_UNIT_TESTS)
  159. STATIC void ratchet_timeval(const struct timeval *timeval_raw,
  160. struct timeval *out);
  161. #endif
  162. #ifdef TOR_UNIT_TESTS
  163. void monotime_reset_ratchets_for_testing(void);
  164. #endif
  165. #endif /* defined(COMPAT_TIME_PRIVATE) */
  166. #endif /* !defined(TOR_COMPAT_TIME_H) */