Commit 214a832a authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Replace libevent based acceptor with poll(2) based acceptor.



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@598 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent ae46a67d
...@@ -225,7 +225,6 @@ struct sess { ...@@ -225,7 +225,6 @@ struct sess {
int id; int id;
unsigned xid; unsigned xid;
struct event ev;
struct worker *wrk; struct worker *wrk;
unsigned sockaddrlen; unsigned sockaddrlen;
......
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
* write the session pointer to a pipe which the event engine monitors. * write the session pointer to a pipe which the event engine monitors.
*/ */
#define ACCEPTOR_USE_KQUEUE #define ACCEPTOR_USE_POLL
#undef ACCEPTOR_USE_LIBEVENT #undef ACCEPTOR_USE_KQUEUE
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
...@@ -84,59 +84,62 @@ vca_handover(struct sess *sp, int bad) ...@@ -84,59 +84,62 @@ vca_handover(struct sess *sp, int bad)
WRK_QueueSession(sp); WRK_QueueSession(sp);
} }
#ifdef ACCEPTOR_USE_LIBEVENT /*====================================================================*/
#ifdef ACCEPTOR_USE_POLL
static struct event_base *evb; #include <poll.h>
static struct event pipe_e;
static int pipes[2]; static struct pollfd *pollfd;
static unsigned npoll;
static struct event tick_e; static int pipes[2];
static struct timeval tick_rate;
static struct event accept_e[2 * HERITAGE_NSOCKS];
static TAILQ_HEAD(,sess) sesshead = TAILQ_HEAD_INITIALIZER(sesshead); static TAILQ_HEAD(,sess) sesshead = TAILQ_HEAD_INITIALIZER(sesshead);
/*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/
static void static void
vca_tick(int a, short b, void *c) vca_pollspace(int fd)
{ {
struct sess *sp, *sp2; struct pollfd *p;
struct timespec t; unsigned u, v;
(void)a; if (fd < npoll)
(void)b; return;
(void)c; if (npoll == 0)
AZ(evtimer_add(&tick_e, &tick_rate)); npoll = 16;
clock_gettime(CLOCK_MONOTONIC, &t); for (u = npoll; fd >= u; )
TAILQ_FOREACH_SAFE(sp, &sesshead, list, sp2) { u += u;
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); VSL(SLT_Debug, 0, "Acceptor Pollspace %u", u);
if (sp->t_idle.tv_sec + 30 < t.tv_sec) { p = realloc(pollfd, u * sizeof *p);
TAILQ_REMOVE(&sesshead, sp, list); assert(p != NULL);
vca_close_session(sp, "timeout"); memset(p + npoll, 0, (u - npoll) * sizeof *p);
vca_return_session(sp); for (v = npoll ; v <= u; v++)
} p->fd = -1;
} pollfd = p;
npoll = u;
} }
/*--------------------------------------------------------------------*/
static void static void
vca_rcvhd_f(int fd, short event, void *arg) vca_poll(int fd)
{ {
struct sess *sp; vca_pollspace(fd);
int i; pollfd[fd].fd = fd;
pollfd[fd].events = POLLIN;
(void)event; }
CAST_OBJ_NOTNULL(sp, arg, SESS_MAGIC);
i = http_RecvSome(fd, sp->http);
if (i < 0)
return;
event_del(&sp->ev); static void
TAILQ_REMOVE(&sesshead, sp, list); vca_unpoll(int fd)
vca_handover(sp, i); {
vca_pollspace(fd);
pollfd[fd].fd = -1;
pollfd[fd].events = 0;
} }
/*--------------------------------------------------------------------*/
static void static void
vca_rcvhdev(struct sess *sp) vca_rcvhdev(struct sess *sp)
{ {
...@@ -144,36 +147,14 @@ vca_rcvhdev(struct sess *sp) ...@@ -144,36 +147,14 @@ vca_rcvhdev(struct sess *sp)
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
clock_gettime(CLOCK_MONOTONIC, &sp->t_idle); clock_gettime(CLOCK_MONOTONIC, &sp->t_idle);
TAILQ_INSERT_TAIL(&sesshead, sp, list); TAILQ_INSERT_TAIL(&sesshead, sp, list);
event_set(&sp->ev, sp->fd, EV_READ | EV_PERSIST, vca_rcvhd_f, sp); vca_poll(sp->fd);
AZ(event_base_set(evb, &sp->ev));
AZ(event_add(&sp->ev, NULL)); /* XXX: timeout */
}
static void
pipe_f(int fd, short event, void *arg)
{
struct sess *sp;
int i;
(void)event;
(void)arg;
i = read(fd, &sp, sizeof sp);
assert(i == sizeof sp);
if (http_RecvPrepAgain(sp->http)) {
vca_handover(sp, 0);
return;
}
vca_rcvhdev(sp);
} }
static void static void
accept_f(int fd, short event, void *arg) accept_f(int fd)
{ {
struct sess *sp; struct sess *sp;
(void)event;
(void)arg;
sp = vca_accept_sess(fd); sp = vca_accept_sess(fd);
if (sp == NULL) if (sp == NULL)
return; return;
...@@ -185,47 +166,73 @@ accept_f(int fd, short event, void *arg) ...@@ -185,47 +166,73 @@ accept_f(int fd, short event, void *arg)
static void * static void *
vca_main(void *arg) vca_main(void *arg)
{ {
unsigned u; unsigned u, v;
struct event *ep; struct sess *sp, *sp2;
struct timespec t;
int i;
(void)arg; (void)arg;
tick_rate.tv_sec = 1;
tick_rate.tv_usec = 0;
AZ(pipe(pipes)); AZ(pipe(pipes));
evb = event_init(); vca_poll(pipes[0]);
assert(evb != NULL);
event_set(&pipe_e, pipes[0], EV_READ | EV_PERSIST, pipe_f, NULL);
AZ(event_base_set(evb, &pipe_e));
AZ(event_add(&pipe_e, NULL));
evtimer_set(&tick_e, vca_tick, NULL);
AZ(event_base_set(evb, &tick_e));
AZ(evtimer_add(&tick_e, &tick_rate));
ep = accept_e;
for (u = 0; u < HERITAGE_NSOCKS; u++) { for (u = 0; u < HERITAGE_NSOCKS; u++) {
if (heritage.sock_local[u] >= 0) { if (heritage.sock_local[u] >= 0)
event_set(ep, heritage.sock_local[u], vca_poll(heritage.sock_local[u]);
EV_READ | EV_PERSIST, if (heritage.sock_remote[u] >= 0)
accept_f, NULL); vca_poll(heritage.sock_local[u]);
AZ(event_base_set(evb, ep)); }
AZ(event_add(ep, NULL));
ep++; while (1) {
v = poll(pollfd, npoll, 5000);
if (v && pollfd[pipes[0]].revents) {
v--;
i = read(pipes[0], &sp, sizeof sp);
assert(i == sizeof sp);
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
if (http_RecvPrepAgain(sp->http))
vca_handover(sp, 0);
else
vca_rcvhdev(sp);
} }
if (heritage.sock_remote[u] >= 0) { for (u = 0; v && u < HERITAGE_NSOCKS; u++) {
event_set(ep, heritage.sock_remote[u], if (heritage.sock_local[u] >= 0 &&
EV_READ | EV_PERSIST, pollfd[heritage.sock_local[u]].revents) {
accept_f, NULL); accept_f(heritage.sock_local[u]);
AZ(event_base_set(evb, ep)); v--;
AZ(event_add(ep, NULL)); }
ep++; if (heritage.sock_remote[u] >= 0 &&
pollfd[heritage.sock_remote[u]].revents) {
accept_f(heritage.sock_local[u]);
v--;
}
}
clock_gettime(CLOCK_MONOTONIC, &t);
TAILQ_FOREACH_SAFE(sp, &sesshead, list, sp2) {
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
if (pollfd[sp->fd].revents) {
v--;
i = http_RecvSome(sp->fd, sp->http);
if (i < 0)
continue;
vca_unpoll(sp->fd);
TAILQ_REMOVE(&sesshead, sp, list);
vca_handover(sp, i);
continue;
}
if (sp->t_idle.tv_sec + 5 < t.tv_sec) {
TAILQ_REMOVE(&sesshead, sp, list);
vca_unpoll(sp->fd);
vca_close_session(sp, "timeout");
vca_return_session(sp);
continue;
}
if (v == 0)
break;
} }
} }
AZ(event_base_loop(evb, 0));
INCOMPL(); INCOMPL();
} }
...@@ -243,8 +250,8 @@ vca_return_session(struct sess *sp) ...@@ -243,8 +250,8 @@ vca_return_session(struct sess *sp)
assert(sizeof sp == write(pipes[1], &sp, sizeof sp)); assert(sizeof sp == write(pipes[1], &sp, sizeof sp));
} }
#endif /* ACCEPTOR_USE_LIBEVENT */ #endif /* ACCEPTOR_USE_POLL */
/*====================================================================*/
#ifdef ACCEPTOR_USE_KQUEUE #ifdef ACCEPTOR_USE_KQUEUE
#include <sys/event.h> #include <sys/event.h>
...@@ -360,6 +367,7 @@ vca_return_session(struct sess *sp) ...@@ -360,6 +367,7 @@ vca_return_session(struct sess *sp)
} }
#endif /* ACCEPTOR_USE_KQUEUE */ #endif /* ACCEPTOR_USE_KQUEUE */
/*====================================================================*/
/*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment