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