1 /* Copyright (c) 2016, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
6 * \brief Wrapper around William Ahern's fast hierarchical timer wheel
7 * implementation, to tie it in with a libevent backend.
9 * Only use these functions from the main thread.
11 * The main advantage of tor_timer_t over using libevent's timers is that
12 * they're way more efficient if we need to have thousands or millions of
13 * them. For more information, see
14 * http://www.25thandclement.com/~william/projects/timeout.c.html
16 * Periodic timers are available in the backend, but I've turned them off.
17 * We can turn them back on if needed.
22 * The use of tor_gettimeofday_cached_monotonic() is kind of ugly. It would
25 * Having a way to free all timers on shutdown would free people from the
26 * need to track them. Not sure if that's clever though.
28 * In an ideal world, Libevent would just switch to use this backend, and we
29 * could throw this file away. But even if Libevent does switch, we'll be
30 * stuck with legacy libevents for some time.
36 #include "compat_libevent.h"
41 #ifdef HAVE_EVENT2_EVENT_H
42 #include <event2/event.h>
53 * These definitions are for timeouts.c and timeouts.h.
56 /* We're not exposing any of the functions outside this file. */
57 #define TIMEOUT_PUBLIC __attribute__((__unused__)) static
59 /* We're not exposing any of the functions outside this file. */
60 #define TIMEOUT_PUBLIC static
62 /* We're not using periodic events. */
63 #define TIMEOUT_DISABLE_INTERVALS
64 /* We always know the global_timeouts object, so we don't need each timeout
65 * to keep a pointer to it. */
66 #define TIMEOUT_DISABLE_RELATIVE_ACCESS
67 /* We're providing our own struct timeout_cb. */
68 #define TIMEOUT_CB_OVERRIDE
69 /* We're going to support timers that are pretty far out in advance. Making
70 * this big can be inefficient, but having a significant number of timers
71 * above TIMEOUT_MAX can also be super-inefficent. Choosing 5 here sets
72 * timeout_max to 2^30 ticks, or 29 hours with our value for USEC_PER_TICK */
74 #include "src/ext/timeouts/timeout.c"
76 static struct timeouts
*global_timeouts
= NULL
;
77 static struct event
*global_timer_event
= NULL
;
79 /** We need to choose this value carefully. Because we're using timer wheels,
80 * it actually costs us to have extra resolution we don't use. So for now,
81 * I'm going to define our resolution as .1 msec, and hope that's good enough.
83 * Note that two of the most popular libevent backends (epoll without timerfd,
84 * and windows select), simply can't support sub-millisecond resolution,
85 * do this is optimistic for a lot of users.
87 #define USEC_PER_TICK 100
89 /** One million microseconds in a second */
90 #define USEC_PER_SEC 1000000
92 /** Check at least once every N seconds. */
93 #define MIN_CHECK_SECONDS 3600
95 /** Check at least once every N ticks. */
96 #define MIN_CHECK_TICKS \
97 (((timeout_t)MIN_CHECK_SECONDS) * (1000000 / USEC_PER_TICK))
100 * Convert the timeval in <b>tv</b> to a timeout_t, and return it.
102 * The output resolution is set by USEC_PER_TICK, and the time corresponding
103 * to 0 is the same as the time corresponding to 0 from
104 * tor_gettimeofday_cached_monotonic().
107 tv_to_timeout(const struct timeval
*tv
)
109 uint64_t usec
= tv
->tv_usec
;
110 usec
+= ((uint64_t)USEC_PER_SEC
) * tv
->tv_sec
;
111 return usec
/ USEC_PER_TICK
;
115 * Convert the timeout in <b>t</b> to a timeval in <b>tv_out</b>
118 timeout_to_tv(timeout_t t
, struct timeval
*tv_out
)
121 tv_out
->tv_usec
= (int)(t
% USEC_PER_SEC
);
122 tv_out
->tv_sec
= (time_t)(t
/ USEC_PER_SEC
);
126 * Update the timer <b>tv</b> to the current time in <b>tv</b>.
129 timer_advance_to_cur_time(const struct timeval
*tv
)
131 timeout_t cur_tick
= tv_to_timeout(tv
);
132 if (BUG(cur_tick
< timeouts_get_curtime(global_timeouts
))) {
133 cur_tick
= timeouts_get_curtime(global_timeouts
); // LCOV_EXCL_LINE
135 timeouts_update(global_timeouts
, cur_tick
);
139 * Adjust the time at which the libevent timer should fire based on
140 * the next-expiring time in <b>global_timeouts</b>
143 libevent_timer_reschedule(void)
146 tor_gettimeofday_cached_monotonic(&now
);
147 timer_advance_to_cur_time(&now
);
149 timeout_t delay
= timeouts_timeout(global_timeouts
);
151 if (delay
> MIN_CHECK_TICKS
)
152 delay
= MIN_CHECK_TICKS
;
153 timeout_to_tv(delay
, &d
);
154 event_add(global_timer_event
, &d
);
158 * Invoked when the libevent timer has expired: see which tor_timer_t events
159 * have fired, activate their callbacks, and reschedule the libevent timer.
162 libevent_timer_callback(evutil_socket_t fd
, short what
, void *arg
)
169 tor_gettimeofday_cache_clear();
170 tor_gettimeofday_cached_monotonic(&now
);
171 timer_advance_to_cur_time(&now
);
174 while ((t
= timeouts_get(global_timeouts
))) {
175 t
->callback
.cb(t
, t
->callback
.arg
, &now
);
178 tor_gettimeofday_cache_clear();
179 libevent_timer_reschedule();
183 * Initialize the timers subsystem. Requires that libevent has already been
187 timers_initialize(void)
189 if (BUG(global_timeouts
))
190 return; // LCOV_EXCL_LINE
193 global_timeouts
= timeouts_open(0, &err
);
194 if (!global_timeouts
) {
195 // LCOV_EXCL_START -- this can only fail on malloc failure.
196 log_err(LD_BUG
, "Unable to open timer backend: %s", strerror(err
));
201 struct event
*timer_event
;
202 timer_event
= tor_event_new(tor_libevent_get_base(),
203 -1, 0, libevent_timer_callback
, NULL
);
204 tor_assert(timer_event
);
205 global_timer_event
= timer_event
;
207 libevent_timer_reschedule();
211 * Release all storage held in the timers subsystem. Does not fire timers.
214 timers_shutdown(void)
216 if (global_timer_event
) {
217 tor_event_free(global_timer_event
);
218 global_timer_event
= NULL
;
220 if (global_timeouts
) {
221 timeouts_close(global_timeouts
);
222 global_timeouts
= NULL
;
227 * Allocate and return a new timer, with given callback and argument.
230 timer_new(timer_cb_fn_t cb
, void *arg
)
232 tor_timer_t
*t
= tor_malloc(sizeof(tor_timer_t
));
234 timer_set_cb(t
, cb
, arg
);
239 * Release all storage held by <b>t</b>, and unschedule it if was already
243 timer_free(tor_timer_t
*t
)
248 timeouts_del(global_timeouts
, t
);
253 * Change the callback and argument associated with a timer <b>t</b>.
256 timer_set_cb(tor_timer_t
*t
, timer_cb_fn_t cb
, void *arg
)
259 t
->callback
.arg
= arg
;
263 * Schedule the timer t to fire at the current time plus a delay of <b>tv</b>.
264 * All times are relative to tor_gettimeofday_cached_monotonic.
267 timer_schedule(tor_timer_t
*t
, const struct timeval
*tv
)
269 const timeout_t when
= tv_to_timeout(tv
);
271 tor_gettimeofday_cached_monotonic(&now
);
272 timer_advance_to_cur_time(&now
);
274 /* Take the old timeout value. */
275 timeout_t to
= timeouts_timeout(global_timeouts
);
277 timeouts_add(global_timeouts
, t
, when
);
279 /* Should we update the libevent timer? */
281 return; /* we're already going to fire before this timer would trigger. */
283 libevent_timer_reschedule();
287 * Cancel the timer <b>t</b> if it is currently scheduled. (It's okay to call
288 * this on an unscheduled timer.
291 timer_disable(tor_timer_t
*t
)
293 timeouts_del(global_timeouts
, t
);
294 /* We don't reschedule the libevent timer here, since it's okay if it fires