From 0b52d7ce4aefe885f1899a99ae25b1322ed34396 Mon Sep 17 00:00:00 2001 From: phk Date: Mon, 1 May 2006 12:45:20 +0000 Subject: [PATCH] Add vca_write() and vca_flush(), two functions which will attempt to use writev() if possible. vca_flush() must be called before any memory previosly given to vca_write is overwritten, and after the last call to vca_write() git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@164 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/cache.h | 2 + varnish-cache/bin/varnishd/cache_acceptor.c | 48 +++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index 9d7dc400..379caf45 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -63,6 +63,8 @@ extern struct stevedore *stevedore; /* cache_acceptor.c */ +void vca_write(struct sess *sp, void *ptr, size_t len); +void vca_flush(struct sess *sp); void *vca_main(void *arg); void vca_retire_session(struct sess *sp); void vca_recycle_session(struct sess *sp); diff --git a/varnish-cache/bin/varnishd/cache_acceptor.c b/varnish-cache/bin/varnishd/cache_acceptor.c index fb6fae04..4e276b76 100644 --- a/varnish-cache/bin/varnishd/cache_acceptor.c +++ b/varnish-cache/bin/varnishd/cache_acceptor.c @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -32,13 +33,60 @@ static struct event_base *evb; static struct event pipe_e; static int pipes[2]; +#define SESS_IOVS 5 + static struct event accept_e[2 * HERITAGE_NSOCKS]; struct sessmem { struct sess s; struct event e; + struct iovec iov[SESS_IOVS]; + int niov; + size_t liov; }; +/*-------------------------------------------------------------------- + * Write data to client + * We try to use writev() if possible in order to minimize number of + * syscalls made and packets sent. It also just might allow the worker + * thread to complete the request without holding stuff locked. + */ + +void +vca_flush(struct sess *sp) +{ + int i; + + if (sp->fd < 0 || sp->mem->niov == 0) + return; + i = writev(sp->fd, sp->mem->iov, sp->mem->niov); + if (i != sp->mem->liov) { + VSL(SLT_SessionClose, sp->fd, "Premature %d of %d", + i, sp->mem->liov); + close(sp->fd); + sp->fd = -1; + } + sp->mem->liov = 0; + sp->mem->niov = 0; +} + +void +vca_write(struct sess *sp, void *ptr, size_t len) +{ + + if (sp->fd < 0 || len == 0) + return; + if (sp->mem->niov == SESS_IOVS) + vca_flush(sp); + if (sp->fd < 0) + return; + sp->mem->iov[sp->mem->niov].iov_base = ptr; + sp->mem->iov[sp->mem->niov++].iov_len = len; + sp->mem->liov += len; +} + +/*--------------------------------------------------------------------*/ + static void pipe_f(int fd, short event, void *arg) { -- 2.39.5