Rewrite default_eventloop to use hash tables instead of arrays

This commit is contained in:
Neil Cook 2016-12-14 15:45:39 +00:00
parent a9386e621a
commit d3b097fffe
3 changed files with 1212 additions and 94 deletions

View File

@ -27,12 +27,41 @@
#include "config.h" #include "config.h"
#ifdef USE_WINSOCK
#else
#include <poll.h> #include <poll.h>
#endif
#include <sys/resource.h> #include <sys/resource.h>
#include "extension/default_eventloop.h" #include "extension/default_eventloop.h"
#include "debug.h" #include "debug.h"
#include "types-internal.h" #include "types-internal.h"
_getdns_eventloop_info *find_event(_getdns_eventloop_info** events, int id)
{
_getdns_eventloop_info* ev;
DEBUG_SCHED("finding event in events ptr %p with id %d", *events, id);
HASH_FIND_INT(*events, &id, ev);
DEBUG_SCHED("found event in events ptr %p with id %d and ptr %p", *events, id, ev);
return ev;
}
void add_event(_getdns_eventloop_info** events, int id, _getdns_eventloop_info* ev)
{
DEBUG_SCHED("adding event in events ptr %p with id %d and ptr %p", *events, id, ev);
HASH_ADD_INT(*events, id, ev);
}
void delete_event(_getdns_eventloop_info** events, _getdns_eventloop_info* ev)
{
DEBUG_SCHED("deleting event in events ptr %p and ptr %p", *events, ev);
HASH_DEL(*events, ev);
}
static uint64_t get_now_plus(uint64_t amount) static uint64_t get_now_plus(uint64_t amount)
{ {
struct timeval tv; struct timeval tv;
@ -72,22 +101,33 @@ default_eventloop_schedule(getdns_eventloop *loop,
fd = -1; fd = -1;
} }
if (fd >= 0) { if (fd >= 0) {
DEBUG_SCHED("default_eventloop_schedule: find_event(default_loop->fd_events)");
_getdns_eventloop_info* fd_event = find_event(&default_loop->fd_events, fd);
#if defined(SCHED_DEBUG) && SCHED_DEBUG #if defined(SCHED_DEBUG) && SCHED_DEBUG
if (default_loop->fd_events[fd]) { if (fd_event) {
if (default_loop->fd_events[fd] == event) { if (fd_event->event == event) {
DEBUG_SCHED("WARNING: Event %p not cleared " DEBUG_SCHED("WARNING: Event %p not cleared "
"before being rescheduled!\n" "before being rescheduled!\n"
, default_loop->fd_events[fd]); , fd_event->event);
} else { } else {
DEBUG_SCHED("ERROR: A different event is " DEBUG_SCHED("ERROR: A different event is "
"already present at fd slot: %p!\n" "already present at fd slot: %p!\n"
, default_loop->fd_events[fd]); , fd_event->event);
} }
} }
#endif #endif
default_loop->fd_events[fd] = event; /* cleanup the old event if it exists */
default_loop->fd_timeout_times[fd] = get_now_plus(timeout); if (fd_event) {
event->ev = (void *)(intptr_t)(fd + 1); delete_event(&default_loop->fd_events, fd_event);
free(fd_event);
}
fd_event = calloc(1, sizeof(_getdns_eventloop_info));
fd_event->id = fd;
fd_event->event = event;
fd_event->timeout_time = get_now_plus(timeout);
add_event(&default_loop->fd_events, fd, fd_event);
event->ev = (void *) (intptr_t) fd + 1;
DEBUG_SCHED( "scheduled read/write at %d\n", fd); DEBUG_SCHED( "scheduled read/write at %d\n", fd);
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} }
@ -104,10 +144,16 @@ default_eventloop_schedule(getdns_eventloop *loop,
event->write_cb = NULL; event->write_cb = NULL;
} }
for (i = 0; i < default_loop->max_timeouts; i++) { for (i = 0; i < default_loop->max_timeouts; i++) {
if (default_loop->timeout_events[i] == NULL) { _getdns_eventloop_info* timeout_event = NULL;
default_loop->timeout_events[i] = event; DEBUG_SCHED("default_eventloop_schedule: find_event(default_loop->timeout_events)");
default_loop->timeout_times[i] = get_now_plus(timeout); if ((timeout_event = find_event(&default_loop->timeout_events, i)) == NULL) {
event->ev = (void *)(intptr_t)(i + 1); timeout_event = calloc(1, sizeof(_getdns_eventloop_info));
timeout_event->id = i;
timeout_event->event = event;
timeout_event->timeout_time = get_now_plus(timeout);
add_event(&default_loop->timeout_events, i, timeout_event);
event->ev = (void *) (intptr_t) i + 1;
DEBUG_SCHED( "scheduled timeout at %d\n", (int)i); DEBUG_SCHED( "scheduled timeout at %d\n", (int)i);
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} }
@ -132,21 +178,31 @@ default_eventloop_clear(getdns_eventloop *loop, getdns_eventloop_event *event)
return GETDNS_RETURN_GENERIC_ERROR; return GETDNS_RETURN_GENERIC_ERROR;
} }
if (event->timeout_cb && !event->read_cb && !event->write_cb) { if (event->timeout_cb && !event->read_cb && !event->write_cb) {
DEBUG_SCHED("default_eventloop_clear: find_event(default_loop->timeout_events)");
_getdns_eventloop_info* timeout_event = find_event(&default_loop->timeout_events, i);
#if defined(SCHED_DEBUG) && SCHED_DEBUG #if defined(SCHED_DEBUG) && SCHED_DEBUG
if (default_loop->timeout_events[i] != event) if (timeout_event && timeout_event->event != event)
DEBUG_SCHED( "ERROR: Different/wrong event present at " DEBUG_SCHED( "ERROR: Different/wrong event present at "
"timeout slot: %p!\n" "timeout slot: %p!\n"
, default_loop->timeout_events[i]); , timeout_event);
#endif #endif
default_loop->timeout_events[i] = NULL; if (timeout_event) {
delete_event(&default_loop->timeout_events, timeout_event);
free(timeout_event);
}
} else { } else {
DEBUG_SCHED("default_eventloop_clear: find_event(default_loop->fd_events)");
_getdns_eventloop_info* fd_event = find_event(&default_loop->fd_events, i);
#if defined(SCHED_DEBUG) && SCHED_DEBUG #if defined(SCHED_DEBUG) && SCHED_DEBUG
if (default_loop->fd_events[i] != event) if (fd_event && fd_event->event != event)
DEBUG_SCHED( "ERROR: Different/wrong event present at " DEBUG_SCHED( "ERROR: Different/wrong event present at "
"fd slot: %p!\n" "fd slot: %p!\n"
, default_loop->fd_events[i]); , fd_event);
#endif #endif
default_loop->fd_events[i] = NULL; if (fd_event) {
delete_event(&default_loop->fd_events, fd_event);
free(fd_event);
}
} }
event->ev = NULL; event->ev = NULL;
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
@ -156,14 +212,8 @@ static void
default_eventloop_cleanup(getdns_eventloop *loop) default_eventloop_cleanup(getdns_eventloop *loop)
{ {
_getdns_default_eventloop *default_loop = (_getdns_default_eventloop *)loop; _getdns_default_eventloop *default_loop = (_getdns_default_eventloop *)loop;
if (default_loop->fd_events) HASH_CLEAR(hh, default_loop->fd_events);
free(default_loop->fd_events); HASH_CLEAR(hh, default_loop->timeout_events);
if (default_loop->fd_timeout_times)
free(default_loop->fd_timeout_times);
if (default_loop->timeout_events)
free(default_loop->timeout_events);
if (default_loop->timeout_times)
free(default_loop->timeout_times);
} }
static void static void
@ -200,10 +250,9 @@ static void
default_eventloop_run_once(getdns_eventloop *loop, int blocking) default_eventloop_run_once(getdns_eventloop *loop, int blocking)
{ {
_getdns_default_eventloop *default_loop = (_getdns_default_eventloop *)loop; _getdns_default_eventloop *default_loop = (_getdns_default_eventloop *)loop;
_getdns_eventloop_info *s, *tmp;
int fd, max_fd = -1;
uint64_t now, timeout = TIMEOUT_FOREVER; uint64_t now, timeout = TIMEOUT_FOREVER;
size_t i; size_t i=0;
int poll_timeout = 0; int poll_timeout = 0;
struct pollfd* pfds = NULL; struct pollfd* pfds = NULL;
int num_pfds = 0; int num_pfds = 0;
@ -213,78 +262,81 @@ default_eventloop_run_once(getdns_eventloop *loop, int blocking)
now = get_now_plus(0); now = get_now_plus(0);
for (i = 0; i < default_loop->max_timeouts; i++) { HASH_ITER(hh, default_loop->timeout_events, s, tmp) {
if (!default_loop->timeout_events[i]) if (now > s->timeout_time)
continue; default_timeout_cb(-1, s->event);
if (now > default_loop->timeout_times[i]) else if (s->timeout_time < timeout)
default_timeout_cb(-1, default_loop->timeout_events[i]); timeout = s->timeout_time;
else if (default_loop->timeout_times[i] < timeout)
timeout = default_loop->timeout_times[i];
} }
// first we count the number of fds that will be active // first we count the number of fds that will be active
for (fd = 0; fd < default_loop->max_fds; fd++) { HASH_ITER(hh, default_loop->fd_events, s, tmp) {
if (!default_loop->fd_events[fd]) if (s->event->read_cb ||
continue; s->event->write_cb)
if (default_loop->fd_events[fd]->read_cb ||
default_loop->fd_events[fd]->write_cb)
num_pfds++; num_pfds++;
if (fd > max_fd) if (s->timeout_time < timeout)
max_fd = fd; timeout = s->timeout_time;
if (default_loop->fd_timeout_times[fd] < timeout)
timeout = default_loop->fd_timeout_times[fd];
} }
if ((max_fd == -1 && timeout == (uint64_t)-1) || (num_pfds == 0)) if ((timeout == (uint64_t)-1) && (num_pfds == 0))
return; return;
pfds = calloc(num_pfds, sizeof(struct pollfd)); pfds = calloc(num_pfds, sizeof(struct pollfd));
for (fd = 0, i=0; fd < default_loop->max_fds; fd++) { i = 0;
if (!default_loop->fd_events[fd]) HASH_ITER(hh, default_loop->fd_events, s, tmp) {
continue; if (s->event->read_cb) {
if (default_loop->fd_events[fd]->read_cb) { pfds[i].fd = s->id;
pfds[i].fd = fd;
pfds[i].events |= POLLIN; pfds[i].events |= POLLIN;
} }
if (default_loop->fd_events[fd]->write_cb) { if (s->event->write_cb) {
pfds[i].fd = fd; pfds[i].fd = s->id;
pfds[i].events |= POLLOUT; pfds[i].events |= POLLOUT;
} }
i++;
} }
if (! blocking || now > timeout) { if (! blocking || now > timeout) {
poll_timeout = 0; poll_timeout = 0;
} else { } else {
poll_timeout = (timeout - now) * 1000; /* turn seconds in millseconds */ poll_timeout = (timeout - now) * 1000; /* turn seconds into millseconds */
} }
#ifdef USE_WINSOCK
if (WSAPoll(pfds, num_pfds, poll_timeout) < 0) {
#else
if (poll(pfds, num_pfds, poll_timeout) < 0) { if (poll(pfds, num_pfds, poll_timeout) < 0) {
#endif
perror("poll() failed"); perror("poll() failed");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
now = get_now_plus(0); now = get_now_plus(0);
for (int i = 0; i < num_pfds; i++) { for (i = 0; i < num_pfds; i++) {
int fd = pfds[i].fd; int fd = pfds[i].fd;
if (default_loop->fd_events[fd] && DEBUG_SCHED("default_eventloop_runonce: find_event(default_loop->fd_events)");
default_loop->fd_events[fd]->read_cb && _getdns_eventloop_info* fd_event = find_event(&default_loop->fd_events, fd);
if (fd_event &&
fd_event->event &&
fd_event->event->read_cb &&
(pfds[i].revents & POLLIN)) (pfds[i].revents & POLLIN))
default_read_cb(fd, default_loop->fd_events[fd]); default_read_cb(fd, fd_event->event);
if (default_loop->fd_events[fd] && if (fd_event &&
default_loop->fd_events[fd]->write_cb && fd_event->event &&
fd_event->event->write_cb &&
(pfds[i].revents & POLLOUT)) (pfds[i].revents & POLLOUT))
default_write_cb(fd, default_loop->fd_events[fd]); default_write_cb(fd, fd_event->event);
} }
if (pfds) if (pfds)
free(pfds); free(pfds);
for (int fd=0; fd < default_loop->max_fds; fd++) { HASH_ITER(hh, default_loop->fd_events, s, tmp) {
if (default_loop->fd_events[fd] && if (s->event &&
default_loop->fd_events[fd]->timeout_cb && s->event->timeout_cb &&
now > default_loop->fd_timeout_times[fd]) now > s->timeout_time)
default_timeout_cb(fd, default_loop->fd_events[fd]); default_timeout_cb(s->id, s->event);
i = fd; }
if (default_loop->timeout_events[i] && HASH_ITER(hh, default_loop->timeout_events, s, tmp) {
default_loop->timeout_events[i]->timeout_cb && if (s->event &&
now > default_loop->timeout_times[i]) s->event->timeout_cb &&
default_timeout_cb(-1, default_loop->timeout_events[i]); now > s->timeout_time)
default_timeout_cb(s->id, s->event);
} }
} }
@ -292,19 +344,13 @@ static void
default_eventloop_run(getdns_eventloop *loop) default_eventloop_run(getdns_eventloop *loop)
{ {
_getdns_default_eventloop *default_loop = (_getdns_default_eventloop *)loop; _getdns_default_eventloop *default_loop = (_getdns_default_eventloop *)loop;
size_t i;
if (!loop) if (!loop)
return; return;
i = 0; /* keep going until all the events are cleared */
while (i < default_loop->max_timeouts) { while (default_loop->fd_events && default_loop->timeout_events) {
if (default_loop->fd_events[i] || default_loop->timeout_events[i]) {
default_eventloop_run_once(loop, 1); default_eventloop_run_once(loop, 1);
i = 0;
} else {
i++;
}
} }
} }
@ -325,18 +371,10 @@ _getdns_default_eventloop_init(_getdns_default_eventloop *loop)
struct rlimit rl; struct rlimit rl;
if (getrlimit(RLIMIT_NOFILE, &rl) == 0) { if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
loop->max_fds = rl.rlim_cur; loop->max_fds = rl.rlim_cur;
loop->max_timeouts = loop->max_fds; loop->max_timeouts = loop->max_fds; /* this is somewhat arbitrary */
} else { } else {
DEBUG_SCHED("ERROR: could not obtain RLIMIT_NOFILE from getrlimit()\n"); DEBUG_SCHED("ERROR: could not obtain RLIMIT_NOFILE from getrlimit()\n");
loop->max_fds = 0; loop->max_fds = 0;
loop->max_timeouts = loop->max_fds; loop->max_timeouts = loop->max_fds;
} }
if (loop->max_fds) {
loop->fd_events = calloc(loop->max_fds, sizeof(getdns_eventloop_event *));
loop->fd_timeout_times = calloc(loop->max_fds, sizeof(uint64_t));
}
if (loop->max_timeouts) {
loop->timeout_events = calloc(loop->max_timeouts, sizeof(getdns_eventloop_event *));
loop->timeout_times = calloc(loop->max_timeouts, sizeof(uint64_t));
}
} }

View File

@ -34,19 +34,25 @@
#include "config.h" #include "config.h"
#include "getdns/getdns.h" #include "getdns/getdns.h"
#include "getdns/getdns_extra.h" #include "getdns/getdns_extra.h"
#include "util/uthash.h"
/* Eventloop based on poll */ /* Eventloop based on poll */
typedef struct _getdns_eventloop_info {
int id;
getdns_eventloop_event *event;
uint64_t timeout_time;
UT_hash_handle hh;
} _getdns_eventloop_info;
typedef struct _getdns_default_eventloop { typedef struct _getdns_default_eventloop {
getdns_eventloop loop; getdns_eventloop loop;
int max_fds; int max_fds;
int max_timeouts; int max_timeouts;
getdns_eventloop_event **fd_events; _getdns_eventloop_info *fd_events;
uint64_t *fd_timeout_times; _getdns_eventloop_info *timeout_events;
getdns_eventloop_event **timeout_events;
uint64_t *timeout_times;
} _getdns_default_eventloop; } _getdns_default_eventloop;
void void
_getdns_default_eventloop_init(_getdns_default_eventloop *loop); _getdns_default_eventloop_init(_getdns_default_eventloop *loop);

1074
src/util/uthash.h Normal file

File diff suppressed because it is too large Load Diff