compat_time.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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.c
  7. * \brief Portable wrappers for finding out the current time, running
  8. * timers, etc.
  9. **/
  10. #define COMPAT_TIME_PRIVATE
  11. #include "lib/time/compat_time.h"
  12. #include "lib/err/torerr.h"
  13. #include "lib/log/log.h"
  14. #include "lib/log/util_bug.h"
  15. #include "lib/intmath/muldiv.h"
  16. #include "lib/intmath/bits.h"
  17. #include "lib/fs/winlib.h"
  18. #include "lib/wallclock/timeval.h"
  19. #ifdef _WIN32
  20. #include <winsock2.h>
  21. #include <windows.h>
  22. #endif
  23. #ifdef HAVE_SYS_TYPES_H
  24. #include <sys/types.h>
  25. #endif
  26. #ifdef HAVE_SYS_TIME_H
  27. #include <sys/time.h>
  28. #endif
  29. #ifdef HAVE_UNISTD_H
  30. #include <unistd.h>
  31. #endif
  32. #ifdef TOR_UNIT_TESTS
  33. #if !defined(HAVE_USLEEP) && defined(HAVE_SYS_SELECT_H)
  34. /* as fallback implementation for tor_sleep_msec */
  35. #include <sys/select.h>
  36. #endif
  37. #endif /* defined(TOR_UNIT_TESTS) */
  38. #ifdef __APPLE__
  39. #include <mach/mach_time.h>
  40. #endif
  41. #include <errno.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #ifdef _WIN32
  45. #undef HAVE_CLOCK_GETTIME
  46. #endif
  47. #ifdef TOR_UNIT_TESTS
  48. /** Delay for <b>msec</b> milliseconds. Only used in tests. */
  49. void
  50. tor_sleep_msec(int msec)
  51. {
  52. #ifdef _WIN32
  53. Sleep(msec);
  54. #elif defined(HAVE_USLEEP)
  55. sleep(msec / 1000);
  56. /* Some usleep()s hate sleeping more than 1 sec */
  57. usleep((msec % 1000) * 1000);
  58. #elif defined(HAVE_SYS_SELECT_H)
  59. struct timeval tv = { msec / 1000, (msec % 1000) * 1000};
  60. select(0, NULL, NULL, NULL, &tv);
  61. #else
  62. sleep(CEIL_DIV(msec, 1000));
  63. #endif /* defined(_WIN32) || ... */
  64. }
  65. #endif /* defined(TOR_UNIT_TESTS) */
  66. #define ONE_MILLION ((int64_t) (1000 * 1000))
  67. #define ONE_BILLION ((int64_t) (1000 * 1000 * 1000))
  68. /** True iff monotime_init has been called. */
  69. static int monotime_initialized = 0;
  70. static monotime_t initialized_at;
  71. #ifdef MONOTIME_COARSE_FN_IS_DIFFERENT
  72. static monotime_coarse_t initialized_at_coarse;
  73. #endif
  74. #ifdef TOR_UNIT_TESTS
  75. /** True if we are running unit tests and overriding the current monotonic
  76. * time. Note that mocked monotonic time might not be monotonic.
  77. */
  78. static int monotime_mocking_enabled = 0;
  79. static monotime_t initialized_at_saved;
  80. static int64_t mock_time_nsec = 0;
  81. #ifdef MONOTIME_COARSE_FN_IS_DIFFERENT
  82. static int64_t mock_time_nsec_coarse = 0;
  83. static monotime_coarse_t initialized_at_coarse_saved;
  84. #endif
  85. void
  86. monotime_enable_test_mocking(void)
  87. {
  88. if (BUG(monotime_initialized == 0)) {
  89. monotime_init();
  90. }
  91. tor_assert_nonfatal(monotime_mocking_enabled == 0);
  92. monotime_mocking_enabled = 1;
  93. memcpy(&initialized_at_saved,
  94. &initialized_at, sizeof(monotime_t));
  95. memset(&initialized_at, 0, sizeof(monotime_t));
  96. #ifdef MONOTIME_COARSE_FN_IS_DIFFERENT
  97. memcpy(&initialized_at_coarse_saved,
  98. &initialized_at_coarse, sizeof(monotime_coarse_t));
  99. memset(&initialized_at_coarse, 0, sizeof(monotime_coarse_t));
  100. #endif
  101. }
  102. void
  103. monotime_disable_test_mocking(void)
  104. {
  105. tor_assert_nonfatal(monotime_mocking_enabled == 1);
  106. monotime_mocking_enabled = 0;
  107. memcpy(&initialized_at,
  108. &initialized_at_saved, sizeof(monotime_t));
  109. #ifdef MONOTIME_COARSE_FN_IS_DIFFERENT
  110. memcpy(&initialized_at_coarse,
  111. &initialized_at_coarse_saved, sizeof(monotime_coarse_t));
  112. #endif
  113. }
  114. void
  115. monotime_set_mock_time_nsec(int64_t nsec)
  116. {
  117. tor_assert_nonfatal(monotime_mocking_enabled == 1);
  118. mock_time_nsec = nsec;
  119. }
  120. #ifdef MONOTIME_COARSE_FN_IS_DIFFERENT
  121. void
  122. monotime_coarse_set_mock_time_nsec(int64_t nsec)
  123. {
  124. tor_assert_nonfatal(monotime_mocking_enabled == 1);
  125. mock_time_nsec_coarse = nsec;
  126. }
  127. #endif /* defined(MONOTIME_COARSE_FN_IS_DIFFERENT) */
  128. #endif /* defined(TOR_UNIT_TESTS) */
  129. /* "ratchet" functions for monotonic time. */
  130. #if defined(_WIN32) || defined(TOR_UNIT_TESTS)
  131. /** Protected by lock: last value returned by monotime_get(). */
  132. static int64_t last_pctr = 0;
  133. /** Protected by lock: offset we must add to monotonic time values. */
  134. static int64_t pctr_offset = 0;
  135. /* If we are using GetTickCount(), how many times has it rolled over? */
  136. static uint32_t rollover_count = 0;
  137. /* If we are using GetTickCount(), what's the last value it returned? */
  138. static int64_t last_tick_count = 0;
  139. /** Helper for windows: Called with a sequence of times that are supposed
  140. * to be monotonic; increments them as appropriate so that they actually
  141. * _are_ monotonic.
  142. *
  143. * Caller must hold lock. */
  144. STATIC int64_t
  145. ratchet_performance_counter(int64_t count_raw)
  146. {
  147. /* must hold lock */
  148. const int64_t count_adjusted = count_raw + pctr_offset;
  149. if (PREDICT_UNLIKELY(count_adjusted < last_pctr)) {
  150. /* Monotonicity failed! Pretend no time elapsed. */
  151. pctr_offset = last_pctr - count_raw;
  152. return last_pctr;
  153. } else {
  154. last_pctr = count_adjusted;
  155. return count_adjusted;
  156. }
  157. }
  158. STATIC int64_t
  159. ratchet_coarse_performance_counter(const int64_t count_raw)
  160. {
  161. int64_t count = count_raw + (((int64_t)rollover_count) << 32);
  162. while (PREDICT_UNLIKELY(count < last_tick_count)) {
  163. ++rollover_count;
  164. count = count_raw + (((int64_t)rollover_count) << 32);
  165. }
  166. last_tick_count = count;
  167. return count;
  168. }
  169. #endif /* defined(_WIN32) || defined(TOR_UNIT_TESTS) */
  170. #if defined(MONOTIME_USING_GETTIMEOFDAY) || defined(TOR_UNIT_TESTS)
  171. static struct timeval last_timeofday = { 0, 0 };
  172. static struct timeval timeofday_offset = { 0, 0 };
  173. /** Helper for gettimeofday(): Called with a sequence of times that are
  174. * supposed to be monotonic; increments them as appropriate so that they
  175. * actually _are_ monotonic.
  176. *
  177. * Caller must hold lock. */
  178. STATIC void
  179. ratchet_timeval(const struct timeval *timeval_raw, struct timeval *out)
  180. {
  181. /* must hold lock */
  182. timeradd(timeval_raw, &timeofday_offset, out);
  183. if (PREDICT_UNLIKELY(timercmp(out, &last_timeofday, OP_LT))) {
  184. /* time ran backwards. Instead, declare that no time occurred. */
  185. timersub(&last_timeofday, timeval_raw, &timeofday_offset);
  186. memcpy(out, &last_timeofday, sizeof(struct timeval));
  187. } else {
  188. memcpy(&last_timeofday, out, sizeof(struct timeval));
  189. }
  190. }
  191. #endif /* defined(MONOTIME_USING_GETTIMEOFDAY) || defined(TOR_UNIT_TESTS) */
  192. #ifdef TOR_UNIT_TESTS
  193. /** For testing: reset all the ratchets */
  194. void
  195. monotime_reset_ratchets_for_testing(void)
  196. {
  197. last_pctr = pctr_offset = last_tick_count = 0;
  198. rollover_count = 0;
  199. memset(&last_timeofday, 0, sizeof(struct timeval));
  200. memset(&timeofday_offset, 0, sizeof(struct timeval));
  201. }
  202. #endif /* defined(TOR_UNIT_TESTS) */
  203. #ifdef __APPLE__
  204. /** Initialized on startup: tells is how to convert from ticks to
  205. * nanoseconds.
  206. */
  207. static struct mach_timebase_info mach_time_info;
  208. static struct mach_timebase_info mach_time_info_msec_cvt;
  209. static int32_t mach_time_msec_cvt_threshold;
  210. static int monotime_shift = 0;
  211. static void
  212. monotime_init_internal(void)
  213. {
  214. tor_assert(!monotime_initialized);
  215. int r = mach_timebase_info(&mach_time_info);
  216. tor_assert(r == 0);
  217. tor_assert(mach_time_info.denom != 0);
  218. {
  219. // approximate only.
  220. uint64_t ns_per_tick = mach_time_info.numer / mach_time_info.denom;
  221. uint64_t ms_per_tick = ns_per_tick * ONE_MILLION;
  222. // requires that tor_log2(0) == 0.
  223. monotime_shift = tor_log2(ms_per_tick);
  224. }
  225. {
  226. // For converting ticks to milliseconds in a 32-bit-friendly way, we
  227. // will first right-shift by 20, and then multiply by 2048/1953, since
  228. // (1<<20) * 1953/2048 is about 1e6. We precompute a new numerator and
  229. // denominator here to avoid multiple multiplies.
  230. mach_time_info_msec_cvt.numer = mach_time_info.numer * 2048;
  231. mach_time_info_msec_cvt.denom = mach_time_info.denom * 1953;
  232. // For any value above this amount, we should divide before multiplying,
  233. // to avoid overflow. For a value below this, we should multiply
  234. // before dividing, to improve accuracy.
  235. mach_time_msec_cvt_threshold = INT32_MAX / mach_time_info_msec_cvt.numer;
  236. }
  237. }
  238. /**
  239. * Set "out" to the most recent monotonic time value
  240. */
  241. void
  242. monotime_get(monotime_t *out)
  243. {
  244. #ifdef TOR_UNIT_TESTS
  245. if (monotime_mocking_enabled) {
  246. out->abstime_ = (mock_time_nsec * mach_time_info.denom)
  247. / mach_time_info.numer;
  248. return;
  249. }
  250. #endif /* defined(TOR_UNIT_TESTS) */
  251. out->abstime_ = mach_absolute_time();
  252. }
  253. #if defined(HAVE_MACH_APPROXIMATE_TIME)
  254. void
  255. monotime_coarse_get(monotime_coarse_t *out)
  256. {
  257. #ifdef TOR_UNIT_TESTS
  258. if (monotime_mocking_enabled) {
  259. out->abstime_ = (mock_time_nsec_coarse * mach_time_info.denom)
  260. / mach_time_info.numer;
  261. return;
  262. }
  263. #endif /* defined(TOR_UNIT_TESTS) */
  264. out->abstime_ = mach_approximate_time();
  265. }
  266. #endif
  267. /**
  268. * Return the number of nanoseconds between <b>start</b> and <b>end</b>.
  269. */
  270. int64_t
  271. monotime_diff_nsec(const monotime_t *start,
  272. const monotime_t *end)
  273. {
  274. if (BUG(mach_time_info.denom == 0)) {
  275. monotime_init();
  276. }
  277. const int64_t diff_ticks = end->abstime_ - start->abstime_;
  278. const int64_t diff_nsec =
  279. (diff_ticks * mach_time_info.numer) / mach_time_info.denom;
  280. return diff_nsec;
  281. }
  282. int32_t
  283. monotime_coarse_diff_msec32_(const monotime_coarse_t *start,
  284. const monotime_coarse_t *end)
  285. {
  286. if (BUG(mach_time_info.denom == 0)) {
  287. monotime_init();
  288. }
  289. const int64_t diff_ticks = end->abstime_ - start->abstime_;
  290. /* We already require in di_ops.c that right-shift performs a sign-extend. */
  291. const int32_t diff_microticks = (int32_t)(diff_ticks >> 20);
  292. if (diff_microticks >= mach_time_msec_cvt_threshold) {
  293. return (diff_microticks / mach_time_info_msec_cvt.denom) *
  294. mach_time_info_msec_cvt.numer;
  295. } else {
  296. return (diff_microticks * mach_time_info_msec_cvt.numer) /
  297. mach_time_info_msec_cvt.denom;
  298. }
  299. }
  300. uint32_t
  301. monotime_coarse_to_stamp(const monotime_coarse_t *t)
  302. {
  303. return (uint32_t)(t->abstime_ >> monotime_shift);
  304. }
  305. int
  306. monotime_is_zero(const monotime_t *val)
  307. {
  308. return val->abstime_ == 0;
  309. }
  310. void
  311. monotime_add_msec(monotime_t *out, const monotime_t *val, uint32_t msec)
  312. {
  313. const uint64_t nsec = msec * ONE_MILLION;
  314. const uint64_t ticks = (nsec * mach_time_info.denom) / mach_time_info.numer;
  315. out->abstime_ = val->abstime_ + ticks;
  316. }
  317. /* end of "__APPLE__" */
  318. #elif defined(HAVE_CLOCK_GETTIME)
  319. #ifdef CLOCK_MONOTONIC_COARSE
  320. /**
  321. * Which clock should we use for coarse-grained monotonic time? By default
  322. * this is CLOCK_MONOTONIC_COARSE, but it might not work -- for example,
  323. * if we're compiled with newer Linux headers and then we try to run on
  324. * an old Linux kernel. In that case, we will fall back to CLOCK_MONOTONIC.
  325. */
  326. static int clock_monotonic_coarse = CLOCK_MONOTONIC_COARSE;
  327. #endif /* defined(CLOCK_MONOTONIC_COARSE) */
  328. static void
  329. monotime_init_internal(void)
  330. {
  331. #ifdef CLOCK_MONOTONIC_COARSE
  332. struct timespec ts;
  333. if (clock_gettime(CLOCK_MONOTONIC_COARSE, &ts) < 0) {
  334. log_info(LD_GENERAL, "CLOCK_MONOTONIC_COARSE isn't working (%s); "
  335. "falling back to CLOCK_MONOTONIC.", strerror(errno));
  336. clock_monotonic_coarse = CLOCK_MONOTONIC;
  337. }
  338. #endif /* defined(CLOCK_MONOTONIC_COARSE) */
  339. }
  340. void
  341. monotime_get(monotime_t *out)
  342. {
  343. #ifdef TOR_UNIT_TESTS
  344. if (monotime_mocking_enabled) {
  345. out->ts_.tv_sec = (time_t) (mock_time_nsec / ONE_BILLION);
  346. out->ts_.tv_nsec = (int) (mock_time_nsec % ONE_BILLION);
  347. return;
  348. }
  349. #endif /* defined(TOR_UNIT_TESTS) */
  350. int r = clock_gettime(CLOCK_MONOTONIC, &out->ts_);
  351. tor_assert(r == 0);
  352. }
  353. #ifdef CLOCK_MONOTONIC_COARSE
  354. void
  355. monotime_coarse_get(monotime_coarse_t *out)
  356. {
  357. #ifdef TOR_UNIT_TESTS
  358. if (monotime_mocking_enabled) {
  359. out->ts_.tv_sec = (time_t) (mock_time_nsec_coarse / ONE_BILLION);
  360. out->ts_.tv_nsec = (int) (mock_time_nsec_coarse % ONE_BILLION);
  361. return;
  362. }
  363. #endif /* defined(TOR_UNIT_TESTS) */
  364. int r = clock_gettime(clock_monotonic_coarse, &out->ts_);
  365. if (PREDICT_UNLIKELY(r < 0) &&
  366. errno == EINVAL &&
  367. clock_monotonic_coarse == CLOCK_MONOTONIC_COARSE) {
  368. /* We should have caught this at startup in monotime_init_internal!
  369. */
  370. log_warn(LD_BUG, "Falling back to non-coarse monotonic time %s initial "
  371. "system start?", monotime_initialized?"after":"without");
  372. clock_monotonic_coarse = CLOCK_MONOTONIC;
  373. r = clock_gettime(clock_monotonic_coarse, &out->ts_);
  374. }
  375. tor_assert(r == 0);
  376. }
  377. #endif /* defined(CLOCK_MONOTONIC_COARSE) */
  378. int64_t
  379. monotime_diff_nsec(const monotime_t *start,
  380. const monotime_t *end)
  381. {
  382. const int64_t diff_sec = end->ts_.tv_sec - start->ts_.tv_sec;
  383. const int64_t diff_nsec = diff_sec * ONE_BILLION +
  384. (end->ts_.tv_nsec - start->ts_.tv_nsec);
  385. return diff_nsec;
  386. }
  387. int32_t
  388. monotime_coarse_diff_msec32_(const monotime_coarse_t *start,
  389. const monotime_coarse_t *end)
  390. {
  391. const int32_t diff_sec = (int32_t)(end->ts_.tv_sec - start->ts_.tv_sec);
  392. const int32_t diff_nsec = (int32_t)(end->ts_.tv_nsec - start->ts_.tv_nsec);
  393. return diff_sec * 1000 + diff_nsec / ONE_MILLION;
  394. }
  395. /* This value is ONE_BILLION >> 20. */
  396. static const uint32_t STAMP_TICKS_PER_SECOND = 953;
  397. uint32_t
  398. monotime_coarse_to_stamp(const monotime_coarse_t *t)
  399. {
  400. uint32_t nsec = (uint32_t)t->ts_.tv_nsec;
  401. uint32_t sec = (uint32_t)t->ts_.tv_sec;
  402. return (sec * STAMP_TICKS_PER_SECOND) + (nsec >> 20);
  403. }
  404. int
  405. monotime_is_zero(const monotime_t *val)
  406. {
  407. return val->ts_.tv_sec == 0 && val->ts_.tv_nsec == 0;
  408. }
  409. void
  410. monotime_add_msec(monotime_t *out, const monotime_t *val, uint32_t msec)
  411. {
  412. const uint32_t sec = msec / 1000;
  413. const uint32_t msec_remainder = msec % 1000;
  414. out->ts_.tv_sec = val->ts_.tv_sec + sec;
  415. out->ts_.tv_nsec = val->ts_.tv_nsec + (msec_remainder * ONE_MILLION);
  416. if (out->ts_.tv_nsec > ONE_BILLION) {
  417. out->ts_.tv_nsec -= ONE_BILLION;
  418. out->ts_.tv_sec += 1;
  419. }
  420. }
  421. /* end of "HAVE_CLOCK_GETTIME" */
  422. #elif defined (_WIN32)
  423. /** Result of QueryPerformanceFrequency, in terms needed to
  424. * convert ticks to nanoseconds. */
  425. static int64_t nsec_per_tick_numer = 1;
  426. static int64_t nsec_per_tick_denom = 1;
  427. /** Lock to protect last_pctr and pctr_offset */
  428. static CRITICAL_SECTION monotime_lock;
  429. /** Lock to protect rollover_count and last_tick_count */
  430. static CRITICAL_SECTION monotime_coarse_lock;
  431. typedef ULONGLONG (WINAPI *GetTickCount64_fn_t)(void);
  432. static GetTickCount64_fn_t GetTickCount64_fn = NULL;
  433. static void
  434. monotime_init_internal(void)
  435. {
  436. tor_assert(!monotime_initialized);
  437. BOOL ok = InitializeCriticalSectionAndSpinCount(&monotime_lock, 200);
  438. tor_assert(ok);
  439. ok = InitializeCriticalSectionAndSpinCount(&monotime_coarse_lock, 200);
  440. tor_assert(ok);
  441. LARGE_INTEGER li;
  442. ok = QueryPerformanceFrequency(&li);
  443. tor_assert(ok);
  444. tor_assert(li.QuadPart);
  445. uint64_t n = ONE_BILLION;
  446. uint64_t d = li.QuadPart;
  447. /* We need to simplify this or we'll probably overflow the int64. */
  448. simplify_fraction64(&n, &d);
  449. tor_assert(n <= INT64_MAX);
  450. tor_assert(d <= INT64_MAX);
  451. nsec_per_tick_numer = (int64_t) n;
  452. nsec_per_tick_denom = (int64_t) d;
  453. last_pctr = 0;
  454. pctr_offset = 0;
  455. HANDLE h = load_windows_system_library(TEXT("kernel32.dll"));
  456. if (h) {
  457. GetTickCount64_fn = (GetTickCount64_fn_t) (void(*)(void))
  458. GetProcAddress(h, "GetTickCount64");
  459. }
  460. // We can't call FreeLibrary(h) here, because freeing the handle may
  461. // unload the library, and cause future calls to GetTickCount64_fn()
  462. // to fail. See 29642 for details.
  463. }
  464. void
  465. monotime_get(monotime_t *out)
  466. {
  467. if (BUG(monotime_initialized == 0)) {
  468. monotime_init();
  469. }
  470. #ifdef TOR_UNIT_TESTS
  471. if (monotime_mocking_enabled) {
  472. out->pcount_ = (mock_time_nsec * nsec_per_tick_denom)
  473. / nsec_per_tick_numer;
  474. return;
  475. }
  476. #endif /* defined(TOR_UNIT_TESTS) */
  477. /* Alas, QueryPerformanceCounter is not always monotonic: see bug list at
  478. https://www.python.org/dev/peps/pep-0418/#windows-queryperformancecounter
  479. */
  480. EnterCriticalSection(&monotime_lock);
  481. LARGE_INTEGER res;
  482. BOOL ok = QueryPerformanceCounter(&res);
  483. tor_assert(ok);
  484. const int64_t count_raw = res.QuadPart;
  485. out->pcount_ = ratchet_performance_counter(count_raw);
  486. LeaveCriticalSection(&monotime_lock);
  487. }
  488. void
  489. monotime_coarse_get(monotime_coarse_t *out)
  490. {
  491. #ifdef TOR_UNIT_TESTS
  492. if (monotime_mocking_enabled) {
  493. out->tick_count_ = mock_time_nsec_coarse / ONE_MILLION;
  494. return;
  495. }
  496. #endif /* defined(TOR_UNIT_TESTS) */
  497. if (GetTickCount64_fn) {
  498. out->tick_count_ = (int64_t)GetTickCount64_fn();
  499. } else {
  500. EnterCriticalSection(&monotime_coarse_lock);
  501. DWORD tick = GetTickCount();
  502. out->tick_count_ = ratchet_coarse_performance_counter(tick);
  503. LeaveCriticalSection(&monotime_coarse_lock);
  504. }
  505. }
  506. int64_t
  507. monotime_diff_nsec(const monotime_t *start,
  508. const monotime_t *end)
  509. {
  510. if (BUG(monotime_initialized == 0)) {
  511. monotime_init();
  512. }
  513. const int64_t diff_ticks = end->pcount_ - start->pcount_;
  514. return (diff_ticks * nsec_per_tick_numer) / nsec_per_tick_denom;
  515. }
  516. int64_t
  517. monotime_coarse_diff_msec(const monotime_coarse_t *start,
  518. const monotime_coarse_t *end)
  519. {
  520. const int64_t diff_ticks = end->tick_count_ - start->tick_count_;
  521. return diff_ticks;
  522. }
  523. int32_t
  524. monotime_coarse_diff_msec32_(const monotime_coarse_t *start,
  525. const monotime_coarse_t *end)
  526. {
  527. return (int32_t)monotime_coarse_diff_msec(start, end);
  528. }
  529. int64_t
  530. monotime_coarse_diff_usec(const monotime_coarse_t *start,
  531. const monotime_coarse_t *end)
  532. {
  533. return monotime_coarse_diff_msec(start, end) * 1000;
  534. }
  535. int64_t
  536. monotime_coarse_diff_nsec(const monotime_coarse_t *start,
  537. const monotime_coarse_t *end)
  538. {
  539. return monotime_coarse_diff_msec(start, end) * ONE_MILLION;
  540. }
  541. static const uint32_t STAMP_TICKS_PER_SECOND = 1000;
  542. uint32_t
  543. monotime_coarse_to_stamp(const monotime_coarse_t *t)
  544. {
  545. return (uint32_t) t->tick_count_;
  546. }
  547. int
  548. monotime_is_zero(const monotime_t *val)
  549. {
  550. return val->pcount_ == 0;
  551. }
  552. int
  553. monotime_coarse_is_zero(const monotime_coarse_t *val)
  554. {
  555. return val->tick_count_ == 0;
  556. }
  557. void
  558. monotime_add_msec(monotime_t *out, const monotime_t *val, uint32_t msec)
  559. {
  560. const uint64_t nsec = msec * ONE_MILLION;
  561. const uint64_t ticks = (nsec * nsec_per_tick_denom) / nsec_per_tick_numer;
  562. out->pcount_ = val->pcount_ + ticks;
  563. }
  564. void
  565. monotime_coarse_add_msec(monotime_coarse_t *out, const monotime_coarse_t *val,
  566. uint32_t msec)
  567. {
  568. out->tick_count_ = val->tick_count_ + msec;
  569. }
  570. /* end of "_WIN32" */
  571. #elif defined(MONOTIME_USING_GETTIMEOFDAY)
  572. static tor_mutex_t monotime_lock;
  573. /** Initialize the monotonic timer subsystem. */
  574. static void
  575. monotime_init_internal(void)
  576. {
  577. tor_assert(!monotime_initialized);
  578. tor_mutex_init(&monotime_lock);
  579. }
  580. void
  581. monotime_get(monotime_t *out)
  582. {
  583. if (BUG(monotime_initialized == 0)) {
  584. monotime_init();
  585. }
  586. tor_mutex_acquire(&monotime_lock);
  587. struct timeval timeval_raw;
  588. tor_gettimeofday(&timeval_raw);
  589. ratchet_timeval(&timeval_raw, &out->tv_);
  590. tor_mutex_release(&monotime_lock);
  591. }
  592. int64_t
  593. monotime_diff_nsec(const monotime_t *start,
  594. const monotime_t *end)
  595. {
  596. struct timeval diff;
  597. timersub(&end->tv_, &start->tv_, &diff);
  598. return (diff.tv_sec * ONE_BILLION + diff.tv_usec * 1000);
  599. }
  600. int32_t
  601. monotime_coarse_diff_msec32_(const monotime_coarse_t *start,
  602. const monotime_coarse_t *end)
  603. {
  604. struct timeval diff;
  605. timersub(&end->tv_, &start->tv_, &diff);
  606. return diff.tv_sec * 1000 + diff.tv_usec / 1000;
  607. }
  608. /* This value is ONE_MILLION >> 10. */
  609. static const uint32_t STAMP_TICKS_PER_SECOND = 976;
  610. uint32_t
  611. monotime_coarse_to_stamp(const monotime_coarse_t *t)
  612. {
  613. const uint32_t usec = (uint32_t)t->tv_.tv_usec;
  614. const uint32_t sec = (uint32_t)t->tv_.tv_sec;
  615. return (sec * STAMP_TICKS_PER_SECOND) | (nsec >> 10);
  616. }
  617. int
  618. monotime_is_zero(const monotime_t *val)
  619. {
  620. return val->tv_.tv_sec == 0 && val->tv_.tv_usec == 0;
  621. }
  622. void
  623. monotime_add_msec(monotime_t *out, const monotime_t *val, uint32_t msec)
  624. {
  625. const uint32_t sec = msec / 1000;
  626. const uint32_t msec_remainder = msec % 1000;
  627. out->tv_.tv_sec = val->tv_.tv_sec + sec;
  628. out->tv_.tv_usec = val->tv_.tv_nsec + (msec_remainder * 1000);
  629. if (out->tv_.tv_usec > ONE_MILLION) {
  630. out->tv_.tv_usec -= ONE_MILLION;
  631. out->tv_.tv_sec += 1;
  632. }
  633. }
  634. /* end of "MONOTIME_USING_GETTIMEOFDAY" */
  635. #else
  636. #error "No way to implement monotonic timers."
  637. #endif /* defined(__APPLE__) || ... */
  638. /**
  639. * Initialize the monotonic timer subsystem. Must be called before any
  640. * monotonic timer functions. This function is idempotent.
  641. */
  642. void
  643. monotime_init(void)
  644. {
  645. if (!monotime_initialized) {
  646. monotime_init_internal();
  647. monotime_initialized = 1;
  648. monotime_get(&initialized_at);
  649. #ifdef MONOTIME_COARSE_FN_IS_DIFFERENT
  650. monotime_coarse_get(&initialized_at_coarse);
  651. #endif
  652. }
  653. }
  654. void
  655. monotime_zero(monotime_t *out)
  656. {
  657. memset(out, 0, sizeof(*out));
  658. }
  659. #ifdef MONOTIME_COARSE_TYPE_IS_DIFFERENT
  660. void
  661. monotime_coarse_zero(monotime_coarse_t *out)
  662. {
  663. memset(out, 0, sizeof(*out));
  664. }
  665. #endif
  666. int64_t
  667. monotime_diff_usec(const monotime_t *start,
  668. const monotime_t *end)
  669. {
  670. const int64_t nsec = monotime_diff_nsec(start, end);
  671. return CEIL_DIV(nsec, 1000);
  672. }
  673. int64_t
  674. monotime_diff_msec(const monotime_t *start,
  675. const monotime_t *end)
  676. {
  677. const int64_t nsec = monotime_diff_nsec(start, end);
  678. return CEIL_DIV(nsec, ONE_MILLION);
  679. }
  680. uint64_t
  681. monotime_absolute_nsec(void)
  682. {
  683. monotime_t now;
  684. if (BUG(monotime_initialized == 0)) {
  685. monotime_init();
  686. }
  687. monotime_get(&now);
  688. return monotime_diff_nsec(&initialized_at, &now);
  689. }
  690. MOCK_IMPL(uint64_t,
  691. monotime_absolute_usec,(void))
  692. {
  693. return monotime_absolute_nsec() / 1000;
  694. }
  695. uint64_t
  696. monotime_absolute_msec(void)
  697. {
  698. return monotime_absolute_nsec() / ONE_MILLION;
  699. }
  700. #ifdef MONOTIME_COARSE_FN_IS_DIFFERENT
  701. uint64_t
  702. monotime_coarse_absolute_nsec(void)
  703. {
  704. if (BUG(monotime_initialized == 0)) {
  705. monotime_init();
  706. }
  707. monotime_coarse_t now;
  708. monotime_coarse_get(&now);
  709. return monotime_coarse_diff_nsec(&initialized_at_coarse, &now);
  710. }
  711. uint64_t
  712. monotime_coarse_absolute_usec(void)
  713. {
  714. return monotime_coarse_absolute_nsec() / 1000;
  715. }
  716. uint64_t
  717. monotime_coarse_absolute_msec(void)
  718. {
  719. return monotime_coarse_absolute_nsec() / ONE_MILLION;
  720. }
  721. #else
  722. #define initialized_at_coarse initialized_at
  723. #endif /* defined(MONOTIME_COARSE_FN_IS_DIFFERENT) */
  724. /**
  725. * Return the current time "stamp" as described by monotime_coarse_to_stamp.
  726. */
  727. uint32_t
  728. monotime_coarse_get_stamp(void)
  729. {
  730. monotime_coarse_t now;
  731. monotime_coarse_get(&now);
  732. return monotime_coarse_to_stamp(&now);
  733. }
  734. #ifdef __APPLE__
  735. uint64_t
  736. monotime_coarse_stamp_units_to_approx_msec(uint64_t units)
  737. {
  738. /* Recover as much precision as we can. */
  739. uint64_t abstime_diff = (units << monotime_shift);
  740. return (abstime_diff * mach_time_info.numer) /
  741. (mach_time_info.denom * ONE_MILLION);
  742. }
  743. uint64_t
  744. monotime_msec_to_approx_coarse_stamp_units(uint64_t msec)
  745. {
  746. uint64_t abstime_val =
  747. (((uint64_t)msec) * ONE_MILLION * mach_time_info.denom) /
  748. mach_time_info.numer;
  749. return abstime_val >> monotime_shift;
  750. }
  751. #else
  752. uint64_t
  753. monotime_coarse_stamp_units_to_approx_msec(uint64_t units)
  754. {
  755. return (units * 1000) / STAMP_TICKS_PER_SECOND;
  756. }
  757. uint64_t
  758. monotime_msec_to_approx_coarse_stamp_units(uint64_t msec)
  759. {
  760. return (msec * STAMP_TICKS_PER_SECOND) / 1000;
  761. }
  762. #endif