compat_time.h 14 KB

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