compat_time.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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.c
  7. * \brief Portable wrappers for finding out the current time, running
  8. * timers, etc.
  9. **/
  10. #define COMPAT_TIME_PRIVATE
  11. #include "compat.h"
  12. #ifdef _WIN32
  13. #include <winsock2.h>
  14. #include <windows.h>
  15. #endif
  16. #ifdef HAVE_SYS_TYPES_H
  17. #include <sys/types.h>
  18. #endif
  19. #ifdef HAVE_UNISTD_H
  20. #include <unistd.h>
  21. #endif
  22. #ifdef TOR_UNIT_TESTS
  23. #if !defined(HAVE_USLEEP) && defined(HAVE_SYS_SELECT_H)
  24. /* as fallback implementation for tor_sleep_msec */
  25. #include <sys/select.h>
  26. #endif
  27. #endif
  28. #ifdef __APPLE__
  29. #include <mach/mach_time.h>
  30. #endif
  31. #include "torlog.h"
  32. #include "util.h"
  33. #include "container.h"
  34. #ifndef HAVE_GETTIMEOFDAY
  35. #ifdef HAVE_FTIME
  36. #include <sys/timeb.h>
  37. #endif
  38. #endif
  39. #ifdef TOR_UNIT_TESTS
  40. /** Delay for <b>msec</b> milliseconds. Only used in tests. */
  41. void
  42. tor_sleep_msec(int msec)
  43. {
  44. #ifdef _WIN32
  45. Sleep(msec);
  46. #elif defined(HAVE_USLEEP)
  47. sleep(msec / 1000);
  48. /* Some usleep()s hate sleeping more than 1 sec */
  49. usleep((msec % 1000) * 1000);
  50. #elif defined(HAVE_SYS_SELECT_H)
  51. struct timeval tv = { msec / 1000, (msec % 1000) * 1000};
  52. select(0, NULL, NULL, NULL, &tv);
  53. #else
  54. sleep(CEIL_DIV(msec, 1000));
  55. #endif
  56. }
  57. #endif
  58. /** Set *timeval to the current time of day. On error, log and terminate.
  59. * (Same as gettimeofday(timeval,NULL), but never returns -1.)
  60. */
  61. void
  62. tor_gettimeofday(struct timeval *timeval)
  63. {
  64. #ifdef _WIN32
  65. /* Epoch bias copied from perl: number of units between windows epoch and
  66. * Unix epoch. */
  67. #define EPOCH_BIAS U64_LITERAL(116444736000000000)
  68. #define UNITS_PER_SEC U64_LITERAL(10000000)
  69. #define USEC_PER_SEC U64_LITERAL(1000000)
  70. #define UNITS_PER_USEC U64_LITERAL(10)
  71. union {
  72. uint64_t ft_64;
  73. FILETIME ft_ft;
  74. } ft;
  75. /* number of 100-nsec units since Jan 1, 1601 */
  76. GetSystemTimeAsFileTime(&ft.ft_ft);
  77. if (ft.ft_64 < EPOCH_BIAS) {
  78. /* LCOV_EXCL_START */
  79. log_err(LD_GENERAL,"System time is before 1970; failing.");
  80. exit(1);
  81. /* LCOV_EXCL_STOP */
  82. }
  83. ft.ft_64 -= EPOCH_BIAS;
  84. timeval->tv_sec = (unsigned) (ft.ft_64 / UNITS_PER_SEC);
  85. timeval->tv_usec = (unsigned) ((ft.ft_64 / UNITS_PER_USEC) % USEC_PER_SEC);
  86. #elif defined(HAVE_GETTIMEOFDAY)
  87. if (gettimeofday(timeval, NULL)) {
  88. /* LCOV_EXCL_START */
  89. log_err(LD_GENERAL,"gettimeofday failed.");
  90. /* If gettimeofday dies, we have either given a bad timezone (we didn't),
  91. or segfaulted.*/
  92. exit(1);
  93. /* LCOV_EXCL_STOP */
  94. }
  95. #elif defined(HAVE_FTIME)
  96. struct timeb tb;
  97. ftime(&tb);
  98. timeval->tv_sec = tb.time;
  99. timeval->tv_usec = tb.millitm * 1000;
  100. #else
  101. #error "No way to get time."
  102. #endif
  103. return;
  104. }
  105. #define ONE_MILLION ((int64_t) (1000 * 1000))
  106. #define ONE_BILLION ((int64_t) (1000 * 1000 * 1000))
  107. /** True iff monotime_init has been called. */
  108. static int monotime_initialized = 0;
  109. /* "ratchet" functions for monotonic time. */
  110. #if defined(_WIN32) || defined(TOR_UNIT_TESTS)
  111. /** Protected by lock: last value returned by monotime_get(). */
  112. static int64_t last_pctr = 0;
  113. /** Protected by lock: offset we must add to monotonic time values. */
  114. static int64_t pctr_offset = 0;
  115. /* If we are using GetTickCount(), how many times has it rolled over? */
  116. static uint32_t rollover_count = 0;
  117. /* If we are using GetTickCount(), what's the last value it returned? */
  118. static int64_t last_tick_count = 0;
  119. /** Helper for windows: Called with a sequence of times that are supposed
  120. * to be monotonic; increments them as appropriate so that they actually
  121. * _are_ monotonic.
  122. *
  123. * Caller must hold lock. */
  124. STATIC int64_t
  125. ratchet_performance_counter(int64_t count_raw)
  126. {
  127. /* must hold lock */
  128. const int64_t count_adjusted = count_raw + pctr_offset;
  129. if (PREDICT_UNLIKELY(count_adjusted < last_pctr)) {
  130. /* Monotonicity failed! Pretend no time elapsed. */
  131. pctr_offset = last_pctr - count_raw;
  132. return last_pctr;
  133. } else {
  134. last_pctr = count_adjusted;
  135. return count_adjusted;
  136. }
  137. }
  138. STATIC int64_t
  139. ratchet_coarse_performance_counter(const int64_t count_raw)
  140. {
  141. int64_t count = count_raw + (((int64_t)rollover_count) << 32);
  142. while (PREDICT_UNLIKELY(count < last_tick_count)) {
  143. ++rollover_count;
  144. count = count_raw + (((int64_t)rollover_count) << 32);
  145. }
  146. last_tick_count = count;
  147. return count;
  148. }
  149. #endif
  150. #if defined(MONOTIME_USING_GETTIMEOFDAY) || defined(TOR_UNIT_TESTS)
  151. static struct timeval last_timeofday = { 0, 0 };
  152. static struct timeval timeofday_offset = { 0, 0 };
  153. /** Helper for gettimeofday(): Called with a sequence of times that are
  154. * supposed to be monotonic; increments them as appropriate so that they
  155. * actually _are_ monotonic.
  156. *
  157. * Caller must hold lock. */
  158. STATIC void
  159. ratchet_timeval(const struct timeval *timeval_raw, struct timeval *out)
  160. {
  161. /* must hold lock */
  162. timeradd(timeval_raw, &timeofday_offset, out);
  163. if (PREDICT_UNLIKELY(timercmp(out, &last_timeofday, <))) {
  164. /* time ran backwards. Instead, declare that no time occurred. */
  165. timersub(&last_timeofday, timeval_raw, &timeofday_offset);
  166. memcpy(out, &last_timeofday, sizeof(struct timeval));
  167. } else {
  168. memcpy(&last_timeofday, out, sizeof(struct timeval));
  169. }
  170. }
  171. #endif
  172. #ifdef TOR_UNIT_TESTS
  173. /** For testing: reset all the ratchets */
  174. void
  175. monotime_reset_ratchets_for_testing(void)
  176. {
  177. last_pctr = pctr_offset = last_tick_count = 0;
  178. rollover_count = 0;
  179. memset(&last_timeofday, 0, sizeof(struct timeval));
  180. memset(&timeofday_offset, 0, sizeof(struct timeval));
  181. }
  182. #endif
  183. #ifdef __APPLE__
  184. /** Initialized on startup: tells is how to convert from ticks to
  185. * nanoseconds.
  186. */
  187. static struct mach_timebase_info mach_time_info;
  188. static void
  189. monotime_init_internal(void)
  190. {
  191. tor_assert(!monotime_initialized);
  192. int r = mach_timebase_info(&mach_time_info);
  193. tor_assert(r == 0);
  194. tor_assert(mach_time_info.denom != 0);
  195. }
  196. /**
  197. * Set "out" to the most recent monotonic time value
  198. */
  199. void
  200. monotime_get(monotime_t *out)
  201. {
  202. out->abstime_ = mach_absolute_time();
  203. }
  204. /**
  205. * Return the number of nanoseconds between <b>start</b> and <b>end</b>.
  206. */
  207. int64_t
  208. monotime_diff_nsec(const monotime_t *start,
  209. const monotime_t *end)
  210. {
  211. if (BUG(mach_time_info.denom == 0)) {
  212. monotime_init();
  213. }
  214. const int64_t diff_ticks = end->abstime_ - start->abstime_;
  215. const int64_t diff_nsec =
  216. (diff_ticks * mach_time_info.numer) / mach_time_info.denom;
  217. return diff_nsec;
  218. }
  219. /* end of "__APPLE__" */
  220. #elif defined(HAVE_CLOCK_GETTIME)
  221. static void
  222. monotime_init_internal(void)
  223. {
  224. /* no action needed. */
  225. }
  226. void
  227. monotime_get(monotime_t *out)
  228. {
  229. int r = clock_gettime(CLOCK_MONOTONIC, &out->ts_);
  230. tor_assert(r == 0);
  231. }
  232. #ifdef CLOCK_MONOTONIC_COARSE
  233. void
  234. monotime_coarse_get(monotime_coarse_t *out)
  235. {
  236. int r = clock_gettime(CLOCK_MONOTONIC_COARSE, &out->ts_);
  237. tor_assert(r == 0);
  238. }
  239. #endif
  240. int64_t
  241. monotime_diff_nsec(const monotime_t *start,
  242. const monotime_t *end)
  243. {
  244. const int64_t diff_sec = end->ts_.tv_sec - start->ts_.tv_sec;
  245. const int64_t diff_nsec = diff_sec * ONE_BILLION +
  246. (end->ts_.tv_nsec - start->ts_.tv_nsec);
  247. return diff_nsec;
  248. }
  249. /* end of "HAVE_CLOCK_GETTIME" */
  250. #elif defined (_WIN32)
  251. /** Result of QueryPerformanceFrequency, as an int64_t. */
  252. static int64_t ticks_per_second = 0;
  253. /** Lock to protect last_pctr and pctr_offset */
  254. static CRITICAL_SECTION monotime_lock;
  255. /** Lock to protect rollover_count and last_tick_count */
  256. static CRITICAL_SECTION monotime_coarse_lock;
  257. typedef ULONGLONG (WINAPI *GetTickCount64_fn_t)(void);
  258. static GetTickCount64_fn_t GetTickCount64_fn = NULL;
  259. static void
  260. monotime_init_internal(void)
  261. {
  262. tor_assert(!monotime_initialized);
  263. BOOL ok = InitializeCriticalSectionAndSpinCount(&monotime_lock, 200);
  264. tor_assert(ok);
  265. ok = InitializeCriticalSectionAndSpinCount(&monotime_coarse_lock, 200);
  266. tor_assert(ok);
  267. LARGE_INTEGER li;
  268. ok = QueryPerformanceFrequency(&li);
  269. tor_assert(ok);
  270. tor_assert(li.QuadPart);
  271. ticks_per_second = li.QuadPart;
  272. last_pctr = 0;
  273. pctr_offset = 0;
  274. HANDLE h = load_windows_system_library(TEXT("kernel32.dll"));
  275. if (h) {
  276. GetTickCount64_fn = (GetTickCount64_fn_t)
  277. GetProcAddress(h, "GetTickCount64");
  278. }
  279. // FreeLibrary(h) ?
  280. }
  281. void
  282. monotime_get(monotime_t *out)
  283. {
  284. if (BUG(monotime_initialized == 0)) {
  285. monotime_init();
  286. }
  287. /* Alas, QueryPerformanceCounter is not always monotonic: see bug list at
  288. https://www.python.org/dev/peps/pep-0418/#windows-queryperformancecounter
  289. */
  290. EnterCriticalSection(&monotime_lock);
  291. LARGE_INTEGER res;
  292. BOOL ok = QueryPerformanceCounter(&res);
  293. tor_assert(ok);
  294. const int64_t count_raw = res.QuadPart;
  295. out->pcount_ = ratchet_performance_counter(count_raw);
  296. LeaveCriticalSection(&monotime_lock);
  297. }
  298. void
  299. monotime_coarse_get(monotime_coarse_t *out)
  300. {
  301. if (GetTickCount64_fn) {
  302. out->tick_count_ = (int64_t)GetTickCount64_fn();
  303. } else {
  304. EnterCriticalSection(&monotime_coarse_lock);
  305. DWORD tick = GetTickCount();
  306. out->tick_count_ = ratchet_coarse_performance_counter(tick);
  307. LeaveCriticalSection(&monotime_coarse_lock);
  308. }
  309. }
  310. int64_t
  311. monotime_diff_nsec(const monotime_t *start,
  312. const monotime_t *end)
  313. {
  314. if (BUG(monotime_initialized == 0)) {
  315. monotime_init();
  316. }
  317. const int64_t diff_ticks = end->pcount_ - start->pcount_;
  318. return (diff_ticks * ONE_BILLION) / ticks_per_second;
  319. }
  320. int64_t
  321. monotime_coarse_diff_msec(const monotime_coarse_t *start,
  322. const monotime_coarse_t *end)
  323. {
  324. const int64_t diff_ticks = end->tick_count_ - start->tick_count_;
  325. return diff_ticks;
  326. }
  327. int64_t
  328. monotime_coarse_diff_usec(const monotime_coarse_t *start,
  329. const monotime_coarse_t *end)
  330. {
  331. return monotime_coarse_diff_msec(start, end) * 1000;
  332. }
  333. int64_t
  334. monotime_coarse_diff_nsec(const monotime_coarse_t *start,
  335. const monotime_coarse_t *end)
  336. {
  337. return monotime_coarse_diff_msec(start, end) * ONE_MILLION;
  338. }
  339. /* end of "_WIN32" */
  340. #elif defined(MONOTIME_USING_GETTIMEOFDAY)
  341. static tor_mutex_t monotime_lock;
  342. /** Initialize the monotonic timer subsystem. */
  343. static void
  344. monotime_init_internal(void)
  345. {
  346. tor_assert(!monotime_initialized);
  347. tor_mutex_init(&monotime_lock);
  348. }
  349. void
  350. monotime_get(monotime_t *out)
  351. {
  352. if (BUG(monotime_initialized == 0)) {
  353. monotime_init();
  354. }
  355. tor_mutex_acquire(&monotime_lock);
  356. struct timeval timeval_raw;
  357. tor_gettimeofday(&timeval_raw);
  358. ratchet_timeval(&timeval_raw, &out->tv_);
  359. tor_mutex_release(&monotime_lock);
  360. }
  361. int64_t
  362. monotime_diff_nsec(const monotime_t *start,
  363. const monotime_t *end)
  364. {
  365. struct timeval diff;
  366. timersub(&end->tv_, &start->tv_, &diff);
  367. return (diff.tv_sec * ONE_BILLION + diff.tv_usec * 1000);
  368. }
  369. /* end of "MONOTIME_USING_GETTIMEOFDAY" */
  370. #else
  371. #error "No way to implement monotonic timers."
  372. #endif
  373. static monotime_t initialized_at;
  374. #ifdef MONOTIME_COARSE_FN_IS_DIFFERENT
  375. static monotime_coarse_t initialized_at_coarse;
  376. #endif
  377. /**
  378. * Initialize the monotonic timer subsystem. Must be called before any
  379. * monotonic timer functions. This function is idempotent.
  380. */
  381. void
  382. monotime_init(void)
  383. {
  384. if (!monotime_initialized) {
  385. monotime_init_internal();
  386. monotime_get(&initialized_at);
  387. #ifdef MONOTIME_COARSE_FN_IS_DIFFERENT
  388. monotime_coarse_get(&initialized_at_coarse);
  389. #endif
  390. monotime_initialized = 1;
  391. }
  392. }
  393. int64_t
  394. monotime_diff_usec(const monotime_t *start,
  395. const monotime_t *end)
  396. {
  397. const int64_t nsec = monotime_diff_nsec(start, end);
  398. return CEIL_DIV(nsec, 1000);
  399. }
  400. int64_t
  401. monotime_diff_msec(const monotime_t *start,
  402. const monotime_t *end)
  403. {
  404. const int64_t nsec = monotime_diff_nsec(start, end);
  405. return CEIL_DIV(nsec, ONE_MILLION);
  406. }
  407. uint64_t
  408. monotime_absolute_nsec(void)
  409. {
  410. monotime_t now;
  411. if (BUG(monotime_initialized == 0)) {
  412. monotime_init();
  413. }
  414. monotime_get(&now);
  415. return monotime_diff_nsec(&initialized_at, &now);
  416. }
  417. uint64_t
  418. monotime_absolute_usec(void)
  419. {
  420. return monotime_absolute_nsec() / 1000;
  421. }
  422. uint64_t
  423. monotime_absolute_msec(void)
  424. {
  425. return monotime_absolute_nsec() / ONE_MILLION;
  426. }
  427. #ifdef MONOTIME_COARSE_FN_IS_DIFFERENT
  428. uint64_t
  429. monotime_coarse_absolute_nsec(void)
  430. {
  431. if (BUG(monotime_initialized == 0)) {
  432. monotime_init();
  433. }
  434. monotime_coarse_t now;
  435. monotime_coarse_get(&now);
  436. return monotime_coarse_diff_nsec(&initialized_at_coarse, &now);
  437. }
  438. uint64_t
  439. monotime_coarse_absolute_usec(void)
  440. {
  441. return monotime_coarse_absolute_nsec() / 1000;
  442. }
  443. uint64_t
  444. monotime_coarse_absolute_msec(void)
  445. {
  446. return monotime_coarse_absolute_nsec() / ONE_MILLION;
  447. }
  448. #endif