compat_time.h 5.0 KB

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