[tor-commits] [tor/master] Add wrappers to tie the new timeouts into libevent.

nickm at torproject.org nickm at torproject.org
Mon May 9 18:08:56 UTC 2016


commit dcf948da0652ca13f5b44f0e15e062f1ff3f8993
Author: Nick Mathewson <nickm at torproject.org>
Date:   Wed Apr 13 11:11:35 2016 -0400

    Add wrappers to tie the new timeouts into libevent.
---
 src/common/include.am |   2 +
 src/common/timers.c   | 276 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/common/timers.h   |  22 ++++
 src/ext/include.am    |  12 +--
 4 files changed, 306 insertions(+), 6 deletions(-)

diff --git a/src/common/include.am b/src/common/include.am
index e61ed68..0450268 100644
--- a/src/common/include.am
+++ b/src/common/include.am
@@ -97,6 +97,7 @@ LIBOR_CRYPTO_A_SOURCES = \
 LIBOR_EVENT_A_SOURCES = \
 	src/common/compat_libevent.c \
 	src/common/procmon.c         \
+	src/common/timers.c          \
 	src/ext/timeouts/timeout.c
 
 src_common_libor_a_SOURCES = $(LIBOR_A_SOURCES)
@@ -136,6 +137,7 @@ COMMONHEADERS = \
   src/common/procmon.h				\
   src/common/sandbox.h				\
   src/common/testsupport.h			\
+  src/common/timers.h				\
   src/common/torgzip.h				\
   src/common/torint.h				\
   src/common/torlog.h				\
diff --git a/src/common/timers.c b/src/common/timers.c
new file mode 100644
index 0000000..5f42cd4
--- /dev/null
+++ b/src/common/timers.c
@@ -0,0 +1,276 @@
+/* Copyright (c) 2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \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://www.25thandclement.com/~william/projects/timeout.c.html
+ *
+ * Periodic timers are available in the backend, but I've turned them off.
+ * We can turn them back on if needed.
+ */
+
+/* Notes:
+ *
+ * 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__
+/* We're not exposing any of the functions outside this file. */
+#define TIMEOUT_PUBLIC __attribute__((__unused__)) static
+#else
+/* We're not exposing any of the functions outside this file. */
+#define TIMEOUT_PUBLIC static
+#endif
+/* We're not using periodic events. */
+#define TIMEOUT_DISABLE_INTERVALS
+/* We always know the global_timeouts object, so we don't need each timeout
+ * to keep a pointer to it. */
+#define TIMEOUT_DISABLE_RELATIVE_ACCESS
+/* We're providing our own struct timeout_cb. */
+#define TIMEOUT_CB_OVERRIDE
+/* We're going to support timers that are pretty far out in advance. Making
+ * 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;
+
+/** We need to choose this value carefully.  Because we're using timer wheels,
+ * 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
+
+/** One million microseconds in a second */
+#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); // LCOV_EXCL_LINE
+  }
+  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);
+
+  /* Should we update the libevent timer? */
+  if (to <= when) {
+    return; /* we're already going to fire before this timer would trigger. */
+
+  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);
+  /* We don't reschedule the libevent timer here, since it's okay if it fires
+   * early. */
+}
+
diff --git a/src/common/timers.h b/src/common/timers.h
new file mode 100644
index 0000000..594cf38
--- /dev/null
+++ b/src/common/timers.h
@@ -0,0 +1,22 @@
+/* Copyright (c) 2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#ifndef TOR_TIMERS_H
+#define TOR_TIMERS_H
+
+#include "orconfig.h"
+#include "testsupport.h"
+
+typedef struct timeout tor_timer_t;
+typedef void (*timer_cb_fn_t)(tor_timer_t *, void *, const struct timeval *);
+tor_timer_t *timer_new(timer_cb_fn_t cb, void *arg);
+void timer_set_cb(tor_timer_t *t, timer_cb_fn_t cb, void *arg);
+void timer_schedule(tor_timer_t *t, const struct timeval *delay);
+void timer_disable(tor_timer_t *t);
+void timer_free(tor_timer_t *t);
+
+void timers_initialize(void);
+void timers_shutdown(void);
+
+#endif
+
diff --git a/src/ext/include.am b/src/ext/include.am
index 42a38f1..d1a3b47 100644
--- a/src/ext/include.am
+++ b/src/ext/include.am
@@ -10,11 +10,13 @@ EXTHEADERS = \
   src/ext/tor_readpassphrase.h \
   src/ext/strlcat.c	\
   src/ext/strlcpy.c	\
-  src/ext/timeouts/timeout.h \
-  src/ext/timeouts/timeout-debug.h \
   src/ext/tinytest_macros.h \
   src/ext/tor_queue.h	\
-  src/ext/siphash.h
+  src/ext/siphash.h \
+  src/ext/timeouts/timeout.h \
+  src/ext/timeouts/timeout-debug.h \
+  src/ext/timeouts/timeout-bitops.c \
+  src/ext/timeouts/timeout.c
 
 noinst_HEADERS+= $(EXTHEADERS)
 
@@ -166,7 +168,5 @@ EXTRA_DIST += \
 	timeouts/lua/timeout-lua.c		\
 	timeouts/Makefile			\
 	timeouts/Rules.shrc			\
-	timeouts/test-timeout.c			\
-	timeouts/timeout-bitops.c
-
+	timeouts/test-timeout.c
 





More information about the tor-commits mailing list