timers.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* Copyright (c) 2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file timers.c
  5. * \brief Wrapper around William Ahern's fast hierarchical timer wheel
  6. * implementation, to tie it in with a libevent backend.
  7. *
  8. * Only use these functions from the main thread.
  9. *
  10. * The main advantage of tor_timer_t over using libevent's timers is that
  11. * they're way more efficient if we need to have thousands or millions of
  12. * them. For more information, see
  13. * http://www.25thandclement.com/~william/projects/timeout.c.html
  14. *
  15. * Periodic timers are available in the backend, but I've turned them off.
  16. * We can turn them back on if needed.
  17. */
  18. /* Notes:
  19. *
  20. * The use of tor_gettimeofday_cached_monotonic() is kind of ugly. It would
  21. * be neat to fix it.
  22. *
  23. * Having a way to free all timers on shutdown would free people from the
  24. * need to track them. Not sure if that's clever though.
  25. *
  26. * In an ideal world, Libevent would just switch to use this backend, and we
  27. * could throw this file away. But even if Libevent does switch, we'll be
  28. * stuck with legacy libevents for some time.
  29. */
  30. #include "orconfig.h"
  31. #include "compat.h"
  32. #include "compat_libevent.h"
  33. #include "timers.h"
  34. #include "torlog.h"
  35. #include "util.h"
  36. #ifdef HAVE_EVENT2_EVENT_H
  37. #include <event2/event.h>
  38. #else
  39. #include <event.h>
  40. #endif
  41. struct timeout_cb {
  42. timer_cb_fn_t cb;
  43. void *arg;
  44. };
  45. /*
  46. * These definitions are for timeouts.c and timeouts.h.
  47. */
  48. #ifdef __GNUC__
  49. /* We're not exposing any of the functions outside this file. */
  50. #define TIMEOUT_PUBLIC __attribute__((__unused__)) static
  51. #else
  52. /* We're not exposing any of the functions outside this file. */
  53. #define TIMEOUT_PUBLIC static
  54. #endif
  55. /* We're not using periodic events. */
  56. #define TIMEOUT_DISABLE_INTERVALS
  57. /* We always know the global_timeouts object, so we don't need each timeout
  58. * to keep a pointer to it. */
  59. #define TIMEOUT_DISABLE_RELATIVE_ACCESS
  60. /* We're providing our own struct timeout_cb. */
  61. #define TIMEOUT_CB_OVERRIDE
  62. /* We're going to support timers that are pretty far out in advance. Making
  63. * this big can be inefficient, but having a significant number of timers
  64. * above TIMEOUT_MAX can also be super-inefficent. Choosing 5 here sets
  65. * timeout_max to 2^30 ticks, or 29 hours with our value for USEC_PER_TICK */
  66. #define WHEEL_NUM 5
  67. #include "src/ext/timeouts/timeout.c"
  68. static struct timeouts *global_timeouts = NULL;
  69. static struct event *global_timer_event = NULL;
  70. /** We need to choose this value carefully. Because we're using timer wheels,
  71. * it actually costs us to have extra resolution we don't use. So for now,
  72. * I'm going to define our resolution as .1 msec, and hope that's good enough.
  73. */
  74. #define USEC_PER_TICK 100
  75. /** One million microseconds in a second */
  76. #define USEC_PER_SEC 1000000
  77. /**
  78. * Convert the timeval in <b>tv</b> to a timeout_t, and return it.
  79. *
  80. * The output resolution is set by USEC_PER_TICK, and the time corresponding
  81. * to 0 is the same as the time corresponding to 0 from
  82. * tor_gettimeofday_cached_monotonic().
  83. */
  84. static timeout_t
  85. tv_to_timeout(const struct timeval *tv)
  86. {
  87. uint64_t usec = tv->tv_usec;
  88. usec += ((uint64_t)USEC_PER_SEC) * tv->tv_sec;
  89. return usec / USEC_PER_TICK;
  90. }
  91. /**
  92. * Convert the timeout in <b>t</b> to a timeval in <b>tv_out</b>
  93. */
  94. static void
  95. timeout_to_tv(timeout_t t, struct timeval *tv_out)
  96. {
  97. t *= USEC_PER_TICK;
  98. tv_out->tv_usec = (int)(t % USEC_PER_SEC);
  99. tv_out->tv_sec = (time_t)(t / USEC_PER_SEC);
  100. }
  101. /**
  102. * Update the timer <b>tv</b> to the current time in <b>tv</b>.
  103. */
  104. static void
  105. timer_advance_to_cur_time(const struct timeval *tv)
  106. {
  107. timeout_t cur_tick = tv_to_timeout(tv);
  108. if (BUG(cur_tick < timeouts_get_curtime(global_timeouts))) {
  109. cur_tick = timeouts_get_curtime(global_timeouts); // LCOV_EXCL_LINE
  110. }
  111. timeouts_update(global_timeouts, cur_tick);
  112. }
  113. /**
  114. * Adjust the time at which the libevent timer should fire based on
  115. * the next-expiring time in <b>global_timeouts</b>
  116. */
  117. static void
  118. libevent_timer_reschedule(void)
  119. {
  120. struct timeval now;
  121. tor_gettimeofday_cached_monotonic(&now);
  122. timer_advance_to_cur_time(&now);
  123. const timeout_t delay = timeouts_timeout(global_timeouts);
  124. struct timeval d;
  125. timeout_to_tv(delay, &d);
  126. event_add(global_timer_event, &d);
  127. }
  128. /**
  129. * Invoked when the libevent timer has expired: see which tor_timer_t events
  130. * have fired, activate their callbacks, and reschedule the libevent timer.
  131. */
  132. static void
  133. libevent_timer_callback(evutil_socket_t fd, short what, void *arg)
  134. {
  135. (void)fd;
  136. (void)what;
  137. (void)arg;
  138. struct timeval now;
  139. tor_gettimeofday_cached_monotonic(&now);
  140. timer_advance_to_cur_time(&now);
  141. tor_timer_t *t;
  142. while ((t = timeouts_get(global_timeouts))) {
  143. t->callback.cb(t, t->callback.arg, &now);
  144. }
  145. tor_gettimeofday_cache_clear();
  146. libevent_timer_reschedule();
  147. }
  148. /**
  149. * Initialize the timers subsystem. Requires that libevent has already been
  150. * initialized.
  151. */
  152. void
  153. timers_initialize(void)
  154. {
  155. if (global_timeouts)
  156. return;
  157. timeout_error_t err;
  158. global_timeouts = timeouts_open(0, &err);
  159. if (!global_timeouts) {
  160. log_err(LD_BUG, "Unable to open timer backend: %s", strerror(err));
  161. tor_assert(0);
  162. }
  163. struct event *timer_event;
  164. timer_event = tor_event_new(tor_libevent_get_base(),
  165. -1, 0, libevent_timer_callback, NULL);
  166. tor_assert(timer_event);
  167. global_timer_event = timer_event;
  168. }
  169. /**
  170. * Release all storage held in the timers subsystem. Does not fire timers.
  171. */
  172. void
  173. timers_shutdown(void)
  174. {
  175. if (global_timer_event) {
  176. tor_event_free(global_timer_event);
  177. global_timer_event = NULL;
  178. }
  179. if (global_timeouts) {
  180. timeouts_close(global_timeouts);
  181. global_timeouts = NULL;
  182. }
  183. }
  184. /**
  185. * Allocate and return a new timer, with given callback and argument.
  186. */
  187. tor_timer_t *
  188. timer_new(timer_cb_fn_t cb, void *arg)
  189. {
  190. tor_timer_t *t = tor_malloc(sizeof(tor_timer_t));
  191. timeout_init(t, 0);
  192. timer_set_cb(t, cb, arg);
  193. return t;
  194. }
  195. /**
  196. * Release all storage held by <b>t</b>, and unschedule it if was already
  197. * scheduled.
  198. */
  199. void
  200. timer_free(tor_timer_t *t)
  201. {
  202. if (! t)
  203. return;
  204. timeouts_del(global_timeouts, t);
  205. tor_free(t);
  206. }
  207. /**
  208. * Change the callback and argument associated with a timer <b>t</b>.
  209. */
  210. void
  211. timer_set_cb(tor_timer_t *t, timer_cb_fn_t cb, void *arg)
  212. {
  213. t->callback.cb = cb;
  214. t->callback.arg = arg;
  215. }
  216. /**
  217. * Schedule the timer t to fire at the current time plus a delay of <b>tv</b>.
  218. * All times are relative to tor_gettimeofday_cached_monotonic.
  219. */
  220. void
  221. timer_schedule(tor_timer_t *t, const struct timeval *tv)
  222. {
  223. const timeout_t when = tv_to_timeout(tv);
  224. struct timeval now;
  225. tor_gettimeofday_cached_monotonic(&now);
  226. timer_advance_to_cur_time(&now);
  227. timeouts_add(global_timeouts, t, when);
  228. /* Should we update the libevent timer? */
  229. if (to <= when) {
  230. return; /* we're already going to fire before this timer would trigger. */
  231. libevent_timer_reschedule();
  232. }
  233. /**
  234. * Cancel the timer <b>t</b> if it is currently scheduled. (It's okay to call
  235. * this on an unscheduled timer.
  236. */
  237. void
  238. timer_disable(tor_timer_t *t)
  239. {
  240. timeouts_del(global_timeouts, t);
  241. /* We don't reschedule the libevent timer here, since it's okay if it fires
  242. * early. */
  243. }