|
@@ -0,0 +1,276 @@
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * \file timers.c
|
|
|
+ * \brief Wrapper around William Ahern's fast hierarchical timer wheel
|
|
|
+ * implementation, to tie it in with a libevent backend.
|
|
|
+ *
|
|
|
+ * Only use these functions from the main thread.
|
|
|
+ *
|
|
|
+ * The main advantage of tor_timer_t over using libevent's timers is that
|
|
|
+ * they're way more efficient if we need to have thousands or millions of
|
|
|
+ * them. For more information, see
|
|
|
+ * http:
|
|
|
+ *
|
|
|
+ * Periodic timers are available in the backend, but I've turned them off.
|
|
|
+ * We can turn them back on if needed.
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+ *
|
|
|
+ * The use of tor_gettimeofday_cached_monotonic() is kind of ugly. It would
|
|
|
+ * be neat to fix it.
|
|
|
+ *
|
|
|
+ * Having a way to free all timers on shutdown would free people from the
|
|
|
+ * need to track them. Not sure if that's clever though.
|
|
|
+ *
|
|
|
+ * In an ideal world, Libevent would just switch to use this backend, and we
|
|
|
+ * could throw this file away. But even if Libevent does switch, we'll be
|
|
|
+ * stuck with legacy libevents for some time.
|
|
|
+ */
|
|
|
+
|
|
|
+#include "orconfig.h"
|
|
|
+
|
|
|
+#include "compat.h"
|
|
|
+#include "compat_libevent.h"
|
|
|
+#include "timers.h"
|
|
|
+#include "torlog.h"
|
|
|
+#include "util.h"
|
|
|
+
|
|
|
+#ifdef HAVE_EVENT2_EVENT_H
|
|
|
+#include <event2/event.h>
|
|
|
+#else
|
|
|
+#include <event.h>
|
|
|
+#endif
|
|
|
+
|
|
|
+struct timeout_cb {
|
|
|
+ timer_cb_fn_t cb;
|
|
|
+ void *arg;
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+ * These definitions are for timeouts.c and timeouts.h.
|
|
|
+ */
|
|
|
+#ifdef __GNUC__
|
|
|
+
|
|
|
+#define TIMEOUT_PUBLIC __attribute__((__unused__)) static
|
|
|
+#else
|
|
|
+
|
|
|
+#define TIMEOUT_PUBLIC static
|
|
|
+#endif
|
|
|
+
|
|
|
+#define TIMEOUT_DISABLE_INTERVALS
|
|
|
+
|
|
|
+ * to keep a pointer to it. */
|
|
|
+#define TIMEOUT_DISABLE_RELATIVE_ACCESS
|
|
|
+
|
|
|
+#define TIMEOUT_CB_OVERRIDE
|
|
|
+
|
|
|
+ * this big can be inefficient, but having a significant number of timers
|
|
|
+ * above TIMEOUT_MAX can also be super-inefficent. Choosing 5 here sets
|
|
|
+ * timeout_max to 2^30 ticks, or 29 hours with our value for USEC_PER_TICK */
|
|
|
+#define WHEEL_NUM 5
|
|
|
+#include "src/ext/timeouts/timeout.c"
|
|
|
+
|
|
|
+static struct timeouts *global_timeouts = NULL;
|
|
|
+static struct event *global_timer_event = NULL;
|
|
|
+
|
|
|
+
|
|
|
+ * it actually costs us to have extra resolution we don't use. So for now,
|
|
|
+ * I'm going to define our resolution as .1 msec, and hope that's good enough.
|
|
|
+ */
|
|
|
+#define USEC_PER_TICK 100
|
|
|
+
|
|
|
+
|
|
|
+#define USEC_PER_SEC 1000000
|
|
|
+
|
|
|
+
|
|
|
+ * Convert the timeval in <b>tv</b> to a timeout_t, and return it.
|
|
|
+ *
|
|
|
+ * The output resolution is set by USEC_PER_TICK, and the time corresponding
|
|
|
+ * to 0 is the same as the time corresponding to 0 from
|
|
|
+ * tor_gettimeofday_cached_monotonic().
|
|
|
+ */
|
|
|
+static timeout_t
|
|
|
+tv_to_timeout(const struct timeval *tv)
|
|
|
+{
|
|
|
+ uint64_t usec = tv->tv_usec;
|
|
|
+ usec += ((uint64_t)USEC_PER_SEC) * tv->tv_sec;
|
|
|
+ return usec / USEC_PER_TICK;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Convert the timeout in <b>t</b> to a timeval in <b>tv_out</b>
|
|
|
+ */
|
|
|
+static void
|
|
|
+timeout_to_tv(timeout_t t, struct timeval *tv_out)
|
|
|
+{
|
|
|
+ t *= USEC_PER_TICK;
|
|
|
+ tv_out->tv_usec = (int)(t % USEC_PER_SEC);
|
|
|
+ tv_out->tv_sec = (time_t)(t / USEC_PER_SEC);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Update the timer <b>tv</b> to the current time in <b>tv</b>.
|
|
|
+ */
|
|
|
+static void
|
|
|
+timer_advance_to_cur_time(const struct timeval *tv)
|
|
|
+{
|
|
|
+ timeout_t cur_tick = tv_to_timeout(tv);
|
|
|
+ if (BUG(cur_tick < timeouts_get_curtime(global_timeouts))) {
|
|
|
+ cur_tick = timeouts_get_curtime(global_timeouts);
|
|
|
+ }
|
|
|
+ timeouts_update(global_timeouts, cur_tick);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Adjust the time at which the libevent timer should fire based on
|
|
|
+ * the next-expiring time in <b>global_timeouts</b>
|
|
|
+ */
|
|
|
+static void
|
|
|
+libevent_timer_reschedule(void)
|
|
|
+{
|
|
|
+ struct timeval now;
|
|
|
+ tor_gettimeofday_cached_monotonic(&now);
|
|
|
+ timer_advance_to_cur_time(&now);
|
|
|
+
|
|
|
+ const timeout_t delay = timeouts_timeout(global_timeouts);
|
|
|
+ struct timeval d;
|
|
|
+ timeout_to_tv(delay, &d);
|
|
|
+ event_add(global_timer_event, &d);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Invoked when the libevent timer has expired: see which tor_timer_t events
|
|
|
+ * have fired, activate their callbacks, and reschedule the libevent timer.
|
|
|
+ */
|
|
|
+static void
|
|
|
+libevent_timer_callback(evutil_socket_t fd, short what, void *arg)
|
|
|
+{
|
|
|
+ (void)fd;
|
|
|
+ (void)what;
|
|
|
+ (void)arg;
|
|
|
+
|
|
|
+ struct timeval now;
|
|
|
+ tor_gettimeofday_cached_monotonic(&now);
|
|
|
+ timer_advance_to_cur_time(&now);
|
|
|
+
|
|
|
+ tor_timer_t *t;
|
|
|
+ while ((t = timeouts_get(global_timeouts))) {
|
|
|
+ t->callback.cb(t, t->callback.arg, &now);
|
|
|
+ }
|
|
|
+
|
|
|
+ tor_gettimeofday_cache_clear();
|
|
|
+ libevent_timer_reschedule();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Initialize the timers subsystem. Requires that libevent has already been
|
|
|
+ * initialized.
|
|
|
+ */
|
|
|
+void
|
|
|
+timers_initialize(void)
|
|
|
+{
|
|
|
+ if (global_timeouts)
|
|
|
+ return;
|
|
|
+
|
|
|
+ timeout_error_t err;
|
|
|
+ global_timeouts = timeouts_open(0, &err);
|
|
|
+ if (!global_timeouts) {
|
|
|
+ log_err(LD_BUG, "Unable to open timer backend: %s", strerror(err));
|
|
|
+ tor_assert(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ struct event *timer_event;
|
|
|
+ timer_event = tor_event_new(tor_libevent_get_base(),
|
|
|
+ -1, 0, libevent_timer_callback, NULL);
|
|
|
+ tor_assert(timer_event);
|
|
|
+ global_timer_event = timer_event;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Release all storage held in the timers subsystem. Does not fire timers.
|
|
|
+ */
|
|
|
+void
|
|
|
+timers_shutdown(void)
|
|
|
+{
|
|
|
+ if (global_timer_event) {
|
|
|
+ tor_event_free(global_timer_event);
|
|
|
+ global_timer_event = NULL;
|
|
|
+ }
|
|
|
+ if (global_timeouts) {
|
|
|
+ timeouts_close(global_timeouts);
|
|
|
+ global_timeouts = NULL;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Allocate and return a new timer, with given callback and argument.
|
|
|
+ */
|
|
|
+tor_timer_t *
|
|
|
+timer_new(timer_cb_fn_t cb, void *arg)
|
|
|
+{
|
|
|
+ tor_timer_t *t = tor_malloc(sizeof(tor_timer_t));
|
|
|
+ timeout_init(t, 0);
|
|
|
+ timer_set_cb(t, cb, arg);
|
|
|
+ return t;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Release all storage held by <b>t</b>, and unschedule it if was already
|
|
|
+ * scheduled.
|
|
|
+ */
|
|
|
+void
|
|
|
+timer_free(tor_timer_t *t)
|
|
|
+{
|
|
|
+ if (! t)
|
|
|
+ return;
|
|
|
+
|
|
|
+ timeouts_del(global_timeouts, t);
|
|
|
+ tor_free(t);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Change the callback and argument associated with a timer <b>t</b>.
|
|
|
+ */
|
|
|
+void
|
|
|
+timer_set_cb(tor_timer_t *t, timer_cb_fn_t cb, void *arg)
|
|
|
+{
|
|
|
+ t->callback.cb = cb;
|
|
|
+ t->callback.arg = arg;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Schedule the timer t to fire at the current time plus a delay of <b>tv</b>.
|
|
|
+ * All times are relative to tor_gettimeofday_cached_monotonic.
|
|
|
+ */
|
|
|
+void
|
|
|
+timer_schedule(tor_timer_t *t, const struct timeval *tv)
|
|
|
+{
|
|
|
+ const timeout_t when = tv_to_timeout(tv);
|
|
|
+ struct timeval now;
|
|
|
+ tor_gettimeofday_cached_monotonic(&now);
|
|
|
+ timer_advance_to_cur_time(&now);
|
|
|
+
|
|
|
+ timeouts_add(global_timeouts, t, when);
|
|
|
+
|
|
|
+
|
|
|
+ if (to <= when) {
|
|
|
+ return;
|
|
|
+
|
|
|
+ libevent_timer_reschedule();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Cancel the timer <b>t</b> if it is currently scheduled. (It's okay to call
|
|
|
+ * this on an unscheduled timer.
|
|
|
+ */
|
|
|
+void
|
|
|
+timer_disable(tor_timer_t *t)
|
|
|
+{
|
|
|
+ timeouts_del(global_timeouts, t);
|
|
|
+
|
|
|
+ * early. */
|
|
|
+}
|
|
|
+
|