From 5796659ccc605115ab1901a289ef9a6b1963cad7 Mon Sep 17 00:00:00 2001 From: phk Date: Tue, 8 Aug 2006 07:36:00 +0000 Subject: [PATCH] Add back sendfile support (under #ifdef HAVE_SENDFILE) but don't engage it for small objects on the suspicion that it has highish setup cost. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@762 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/cache.h | 3 ++ varnish-cache/bin/varnishd/cache_pool.c | 34 ++++++++++++++++++--- varnish-cache/bin/varnishd/cache_response.c | 13 ++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index 1558f374..3eea0c73 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -374,6 +374,9 @@ void WRK_Reset(struct worker *w, int *fd); int WRK_Flush(struct worker *w); unsigned WRK_Write(struct worker *w, const void *ptr, int len); unsigned WRK_WriteH(struct worker *w, struct http_hdr *hh, const char *suf); +#ifdef HAVE_SENDFILE +void WRK_Sendfile(struct worker *w, int fd, off_t off, unsigned len); +#endif /* HAVE_SENDFILE */ /* cache_session.c [SES] */ void SES_Init(void); diff --git a/varnish-cache/bin/varnishd/cache_pool.c b/varnish-cache/bin/varnishd/cache_pool.c index 91e809ac..4eccc6af 100644 --- a/varnish-cache/bin/varnishd/cache_pool.c +++ b/varnish-cache/bin/varnishd/cache_pool.c @@ -8,6 +8,10 @@ #include #include #include +#ifdef HAVE_SENDFILE +#include +#include +#endif /* HAVE_SENDFILE */ #include #include "heritage.h" @@ -54,10 +58,8 @@ WRK_Flush(struct worker *w) i = writev(*w->wfd, w->iov, w->niov); if (i != w->liov) w->werr++; - else { - w->liov = 0; - w->niov = 0; - } + w->liov = 0; + w->niov = 0; return (w->werr); } @@ -94,6 +96,30 @@ WRK_Write(struct worker *w, const void *ptr, int len) return (len); } +#ifdef HAVE_SENDFILE +void +WRK_Sendfile(struct worker *w, int fd, off_t off, unsigned len) +{ + struct sf_hdtr sfh; + int i; + + CHECK_OBJ_NOTNULL(w, WORKER_MAGIC); + assert(fd >= 0); + assert(len > 0); + + memset(&sfh, 0, sizeof sfh); + if (w->niov > 0) { + sfh.headers = w->iov; + sfh.hdr_cnt = w->niov; + } + i = sendfile(fd, *w->wfd, off, len, &sfh, NULL, 0); + if (i != 0) + w->werr++; + w->liov = 0; + w->niov = 0; +} +#endif /* HAVE_SENDFILE */ + /*--------------------------------------------------------------------*/ static void diff --git a/varnish-cache/bin/varnishd/cache_response.c b/varnish-cache/bin/varnishd/cache_response.c index 0c15704c..46f5c5d5 100644 --- a/varnish-cache/bin/varnishd/cache_response.c +++ b/varnish-cache/bin/varnishd/cache_response.c @@ -161,6 +161,19 @@ RES_WriteObj(struct sess *sp) assert(st->stevedore != NULL); u += st->len; sp->wrk->acct.bodybytes += st->len; +#ifdef HAVE_SENDFILE + /* + * XXX: the overhead of setting up senddile is not + * XXX: epsilon and maybe not even delta, so avoid + * XXX: engaging sendfile for small objects. + * XXX: Should use getpagesize() ? + */ + if (st->fd >= 0 && st->len >= 8192) { + WRK_Sendfile(sp->wrk, st->fd, + st->where, st->len); + continue; + } +#endif /* HAVE_SENDFILE */ WRK_Write(sp->wrk, st->ptr, st->len); } assert(u == sp->obj->len); -- 2.39.5