compat_time.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, 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. /* Q: Should you use monotime or monotime_coarse as your source?
  17. *
  18. * A: Generally, you get better precision with monotime, but better
  19. * performance with monotime_coarse.
  20. *
  21. * Q: Should you use monotime_t or monotime_coarse_t directly? Should you use
  22. * usec? msec? "stamp units?"
  23. *
  24. * A: Using monotime_t and monotime_coarse_t directly is most time-efficient,
  25. * since no conversion needs to happen. But they can potentially use more
  26. * memory than you would need for a usec/msec/"stamp unit" count.
  27. *
  28. * Converting to usec or msec on some platforms, and working with them in
  29. * general, creates a risk of doing a 64-bit division. 64-bit division is
  30. * expensive on 32-bit platforms, which still do exist.
  31. *
  32. * The "stamp unit" type is designed to give a type that is cheap to convert
  33. * from monotime_coarse, has resolution of about 1-2ms, and fits nicely in a
  34. * 32-bit integer. Its downside is that it does not correspond directly
  35. * to a natural unit of time.
  36. *
  37. * There is not much point in using "coarse usec" or "coarse nsec", since the
  38. * current coarse monotime implementations give you on the order of
  39. * milliseconds of precision.
  40. *
  41. * Q: So, what backends is monotime_coarse using?
  42. *
  43. * A: Generally speaking, it uses "whatever monotonic-ish time implemenation
  44. * does not require a context switch." The various implementations provide
  45. * this by having a view of the current time in a read-only memory page that
  46. * is updated with a frequency corresponding to the kernel's tick count.
  47. *
  48. * On Windows, monotime_coarse uses GetCount64() [or GetTickCount() on
  49. * obsolete systems]. MSDN claims that the resolution is "typically in the
  50. * range of 10-16 msec", but it has said that for years. Storing
  51. * monotime_coarse_t uses 8 bytes.
  52. *
  53. * On OSX/iOS, monotime_coarse uses uses mach_approximate_time() where
  54. * available, and falls back to regular monotime. The precision is not
  55. * documented, but the implementation is open-source: it reads from a page
  56. * that the kernel updates. Storing monotime_coarse_t uses 8 bytes.
  57. *
  58. * On unixy systems, monotime_coarse uses clock_gettime() with
  59. * CLOCK_MONOTONIC_COARSE where available, and falls back to CLOCK_MONOTONIC.
  60. * It typically uses vdso tricks to read from a page that the kernel updates.
  61. * Its precision fixed, but you can get it with clock_getres(): on my Linux
  62. * desktop, it claims to be 1 msec, but it will depend on the system HZ
  63. * setting. Storing monotime_coarse_t uses 16 bytes.
  64. *
  65. * [TODO: Try CLOCK_MONOTONIC_FAST on foobsd.]
  66. *
  67. * Q: What backends is regular monotonic time using?
  68. *
  69. * A: In general, regular monotime uses something that requires a system call.
  70. * On platforms where system calls are cheap, you win! Otherwise, you lose.
  71. *
  72. * On Windows, monotonic time uses QuereyPerformanceCounter. Storing
  73. * monotime_t costs 8 bytes.
  74. *
  75. * On OSX/Apple, monotonic time uses mach_absolute_time. Storing
  76. * monotime_t costs 8 bytes.
  77. *
  78. * On unixy systems, monotonic time uses CLOCK_MONOTONIC. Storing
  79. * monotime_t costs 16 bytes.
  80. *
  81. * Q: Tell me about the costs of converting to a 64-bit nsec, usec, or msec
  82. * count.
  83. *
  84. * A: Windows, coarse: Cheap, since it's all multiplication.
  85. *
  86. * Windows, precise: Expensive on 32-bit: it needs 64-bit division.
  87. *
  88. * Apple, all: Expensive on 32-bit: it needs 64-bit division.
  89. *
  90. * Unixy, all: Fairly cheap, since the only division required is dividing
  91. * tv_nsec 1000, and nanoseconds-per-second fits in a 32-bit value.
  92. *
  93. * All, "timestamp units": Cheap everywhere: it never divides.
  94. *
  95. * Q: This is only somewhat related, but how much precision could I hope for
  96. * from a libevent time.?
  97. *
  98. * A: Actually, it's _very_ related if you're timing in order to have a
  99. * timeout happen.
  100. *
  101. * On Windows, it uses select: you could in theory have a microsecond
  102. * resolution, but it usually isn't that accurate.
  103. *
  104. * On OSX, iOS, and BSD, you have kqueue: You could in theory have a nanosecond
  105. * resolution, but it usually isn't that accurate.
  106. *
  107. * On Linux, you have epoll: It has a millisecond resolution. Some recent
  108. * Libevents can also use timerfd for higher resolution if
  109. * EVENT_BASE_FLAG_PRECISE_TIMER is set: Tor doesn't set that flag.
  110. */
  111. #ifndef TOR_COMPAT_TIME_H
  112. #define TOR_COMPAT_TIME_H
  113. #include "orconfig.h"
  114. #include "lib/cc/torint.h"
  115. #include "lib/wallclock/tor_gettimeofday.h"
  116. #ifdef _WIN32
  117. #undef HAVE_CLOCK_GETTIME
  118. #endif
  119. #if defined(HAVE_CLOCK_GETTIME)
  120. /* to ensure definition of CLOCK_MONOTONIC_COARSE if it's there */
  121. #include <time.h>
  122. #endif
  123. #if !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
  124. /** Implementation of timeval for platforms that don't have it. */
  125. struct timeval {
  126. time_t tv_sec;
  127. unsigned int tv_usec;
  128. };
  129. #endif /* !defined(HAVE_STRUCT_TIMEVAL_TV_SEC) */
  130. /** Represents a monotonic timer in a platform-dependent way. */
  131. typedef struct monotime_t {
  132. #ifdef __APPLE__
  133. /* On apple, there is a 64-bit counter whose precision we must look up. */
  134. uint64_t abstime_;
  135. #elif defined(HAVE_CLOCK_GETTIME)
  136. /* It sure would be nice to use clock_gettime(). Posix is a nice thing. */
  137. struct timespec ts_;
  138. #elif defined (_WIN32)
  139. /* On Windows, there is a 64-bit counter whose precision we must look up. */
  140. int64_t pcount_;
  141. #else
  142. #define MONOTIME_USING_GETTIMEOFDAY
  143. /* Otherwise, we will be stuck using gettimeofday. */
  144. struct timeval tv_;
  145. #endif /* defined(__APPLE__) || ... */
  146. } monotime_t;
  147. #if defined(CLOCK_MONOTONIC_COARSE) && \
  148. defined(HAVE_CLOCK_GETTIME)
  149. #define MONOTIME_COARSE_FN_IS_DIFFERENT
  150. #define monotime_coarse_t monotime_t
  151. #elif defined(_WIN32)
  152. #define MONOTIME_COARSE_FN_IS_DIFFERENT
  153. #define MONOTIME_COARSE_TYPE_IS_DIFFERENT
  154. /** Represents a coarse monotonic time in a platform-independent way. */
  155. typedef struct monotime_coarse_t {
  156. uint64_t tick_count_;
  157. } monotime_coarse_t;
  158. #elif defined(__APPLE__) && defined(HAVE_MACH_APPROXIMATE_TIME)
  159. #define MONOTIME_COARSE_FN_IS_DIFFERENT
  160. #define monotime_coarse_t monotime_t
  161. #else
  162. #define monotime_coarse_t monotime_t
  163. #endif /* defined(CLOCK_MONOTONIC_COARSE) && ... || ... */
  164. /**
  165. * Initialize the timing subsystem. This function is idempotent.
  166. */
  167. void monotime_init(void);
  168. /**
  169. * Set <b>out</b> to the current time.
  170. */
  171. void monotime_get(monotime_t *out);
  172. /**
  173. * Return the number of nanoseconds between <b>start</b> and <b>end</b>.
  174. */
  175. int64_t monotime_diff_nsec(const monotime_t *start, const monotime_t *end);
  176. /**
  177. * Return the number of microseconds between <b>start</b> and <b>end</b>.
  178. */
  179. int64_t monotime_diff_usec(const monotime_t *start, const monotime_t *end);
  180. /**
  181. * Return the number of milliseconds between <b>start</b> and <b>end</b>.
  182. */
  183. int64_t monotime_diff_msec(const monotime_t *start, const monotime_t *end);
  184. /**
  185. * Return the number of nanoseconds since the timer system was initialized.
  186. */
  187. uint64_t monotime_absolute_nsec(void);
  188. /**
  189. * Return the number of microseconds since the timer system was initialized.
  190. */
  191. MOCK_DECL(uint64_t, monotime_absolute_usec,(void));
  192. /**
  193. * Return the number of milliseconds since the timer system was initialized.
  194. */
  195. uint64_t monotime_absolute_msec(void);
  196. /**
  197. * Set <b>out</b> to zero.
  198. */
  199. void monotime_zero(monotime_t *out);
  200. /**
  201. * Return true iff <b>out</b> is zero
  202. */
  203. int monotime_is_zero(const monotime_t *out);
  204. /**
  205. * Set <b>out</b> to N milliseconds after <b>val</b>.
  206. */
  207. /* XXXX We should add a more generic function here if we ever need to */
  208. void monotime_add_msec(monotime_t *out, const monotime_t *val, uint32_t msec);
  209. #if defined(MONOTIME_COARSE_FN_IS_DIFFERENT)
  210. /**
  211. * Set <b>out</b> to the current coarse time.
  212. */
  213. void monotime_coarse_get(monotime_coarse_t *out);
  214. uint64_t monotime_coarse_absolute_nsec(void);
  215. uint64_t monotime_coarse_absolute_usec(void);
  216. uint64_t monotime_coarse_absolute_msec(void);
  217. #else /* !(defined(MONOTIME_COARSE_FN_IS_DIFFERENT)) */
  218. #define monotime_coarse_get monotime_get
  219. #define monotime_coarse_absolute_nsec monotime_absolute_nsec
  220. #define monotime_coarse_absolute_usec monotime_absolute_usec
  221. #define monotime_coarse_absolute_msec monotime_absolute_msec
  222. #endif /* defined(MONOTIME_COARSE_FN_IS_DIFFERENT) */
  223. /**
  224. * Return a "timestamp" approximation for a coarse monotonic timer.
  225. * This timestamp is meant to be fast to calculate and easy to
  226. * compare, and have a unit of something roughly around 1 msec.
  227. *
  228. * It will wrap over from time to time.
  229. *
  230. * It has no defined zero point.
  231. */
  232. uint32_t monotime_coarse_to_stamp(const monotime_coarse_t *t);
  233. /**
  234. * Convert a difference, expressed in the units of monotime_coarse_to_stamp,
  235. * into an approximate number of milliseconds.
  236. */
  237. uint64_t monotime_coarse_stamp_units_to_approx_msec(uint64_t units);
  238. uint64_t monotime_msec_to_approx_coarse_stamp_units(uint64_t msec);
  239. uint32_t monotime_coarse_get_stamp(void);
  240. #if defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT)
  241. int64_t monotime_coarse_diff_nsec(const monotime_coarse_t *start,
  242. const monotime_coarse_t *end);
  243. int64_t monotime_coarse_diff_usec(const monotime_coarse_t *start,
  244. const monotime_coarse_t *end);
  245. int64_t monotime_coarse_diff_msec(const monotime_coarse_t *start,
  246. const monotime_coarse_t *end);
  247. void monotime_coarse_zero(monotime_coarse_t *out);
  248. int monotime_coarse_is_zero(const monotime_coarse_t *val);
  249. void monotime_coarse_add_msec(monotime_coarse_t *out,
  250. const monotime_coarse_t *val, uint32_t msec);
  251. #else /* !(defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT)) */
  252. #define monotime_coarse_diff_nsec monotime_diff_nsec
  253. #define monotime_coarse_diff_usec monotime_diff_usec
  254. #define monotime_coarse_diff_msec monotime_diff_msec
  255. #define monotime_coarse_zero monotime_zero
  256. #define monotime_coarse_is_zero monotime_is_zero
  257. #define monotime_coarse_add_msec monotime_add_msec
  258. #endif /* defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT) */
  259. /**
  260. * As monotime_coarse_diff_msec, but avoid 64-bit division.
  261. *
  262. * Requires that the difference fit into an int32_t; not for use with
  263. * large time differences.
  264. */
  265. int32_t monotime_coarse_diff_msec32_(const monotime_coarse_t *start,
  266. const monotime_coarse_t *end);
  267. /**
  268. * As monotime_coarse_diff_msec, but avoid 64-bit division if it is expensive.
  269. *
  270. * Requires that the difference fit into an int32_t; not for use with
  271. * large time differences.
  272. */
  273. static inline int32_t
  274. monotime_coarse_diff_msec32(const monotime_coarse_t *start,
  275. const monotime_coarse_t *end)
  276. {
  277. #if SIZEOF_VOID_P == 8
  278. // on a 64-bit platform, let's assume 64/64 division is cheap.
  279. return (int32_t) monotime_coarse_diff_msec(start, end);
  280. #else
  281. #define USING_32BIT_MSEC_HACK
  282. return monotime_coarse_diff_msec32_(start, end);
  283. #endif
  284. }
  285. #ifdef TOR_UNIT_TESTS
  286. void tor_sleep_msec(int msec);
  287. void monotime_enable_test_mocking(void);
  288. void monotime_disable_test_mocking(void);
  289. void monotime_set_mock_time_nsec(int64_t);
  290. #if defined(MONOTIME_COARSE_FN_IS_DIFFERENT)
  291. void monotime_coarse_set_mock_time_nsec(int64_t);
  292. #else
  293. #define monotime_coarse_set_mock_time_nsec monotime_set_mock_time_nsec
  294. #endif
  295. #endif /* defined(TOR_UNIT_TESTS) */
  296. #ifdef COMPAT_TIME_PRIVATE
  297. #if defined(_WIN32) || defined(TOR_UNIT_TESTS)
  298. STATIC int64_t ratchet_performance_counter(int64_t count_raw);
  299. STATIC int64_t ratchet_coarse_performance_counter(int64_t count_raw);
  300. #endif
  301. #if defined(MONOTIME_USING_GETTIMEOFDAY) || defined(TOR_UNIT_TESTS)
  302. STATIC void ratchet_timeval(const struct timeval *timeval_raw,
  303. struct timeval *out);
  304. #endif
  305. #ifdef TOR_UNIT_TESTS
  306. void monotime_reset_ratchets_for_testing(void);
  307. #endif
  308. #endif /* defined(COMPAT_TIME_PRIVATE) */
  309. #endif /* !defined(TOR_COMPAT_TIME_H) */