[PATCH 2/3 v2] cache: use sendfile() instead of a pair of read() + write()
Sebastian Andrzej Siewior
cgit at ml.breakpoint.cc
Sat Jun 23 21:25:23 CEST 2012
From: Sebastian Andrzej Siewior <sebastian at breakpoint.cc>
sendfile() does the same job and avoids to copy the content into userland
and back. One has to define HAVE_LINUX_SENDFILE in case the OS (kernel & libc)
supports the Linux/glibc interface. It is disabled by default on non-linux
environments. According to the glibc, sendfile64() was added in Linux 2.4 (so
it has been there for a while) but after browsing over the mapage of FreeBSD's I
noticed that the prototype is little different.
Signed-off-by: Sebastian Andrzej Siewior <sebastian at breakpoint.cc>
---
On Sat, Jun 23, 2012 at 10:27:25AM +0000, Ben Boeckel wrote:
> On Thu, Jun 21, 2012 at 07:09:47 GMT, cgit at ml.breakpoint.cc wrote:
> > From: Sebastian Andrzej Siewior <sebastian at breakpoint.cc>
> > +ifneq ($(uname_S),Linux)
> > + NO_SENDFILE = YesPlease
> > +endif
>
> Double negatives suck. Can the option be made HAVE_SENDFILE?
Yes it does. What about this.
Makefile | 9 +++++++++
cache.c | 26 +++++++++++++++++++++++++-
2 files changed, 34 insertions(+), 1 deletions(-)
diff --git a/Makefile b/Makefile
index eac24ad..20cae98 100644
--- a/Makefile
+++ b/Makefile
@@ -33,6 +33,8 @@ DOC_PDF = $(patsubst %.txt,%.pdf,$(MAN_TXT))
# j, z, t. (representing long long int, char, intmax_t, size_t, ptrdiff_t).
# some C compilers supported these specifiers prior to C99 as an extension.
#
+# Define HAVE_LINUX_SENDFILE to use sendfile() as offered by glibc on Linux.
+#
#-include config.mak
@@ -49,6 +51,10 @@ ifeq ($(uname_O),Cygwin)
NEEDS_LIBICONV = YesPlease
endif
+ifeq ($(uname_S),Linux)
+ HAVE_LINUX_SENDFILE = YesPlease
+endif
+
#
# Let the user override the above settings.
#
@@ -157,6 +163,9 @@ ifdef NO_OPENSSL
else
EXTLIBS += -lcrypto
endif
+ifdef HAVE_LINUX_SENDFILE
+ CFLAGS += -DHAVE_LINUX_SENDFILE
+endif
cgit: $(OBJECTS) libgit
$(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o cgit $(OBJECTS) $(EXTLIBS)
diff --git a/cache.c b/cache.c
index d7a8d5a..963ec3a 100644
--- a/cache.c
+++ b/cache.c
@@ -13,6 +13,9 @@
*
*/
+#ifdef HAVE_LINUX_SENDFILE
+#include <sys/sendfile.h>
+#endif
#include "cgit.h"
#include "cache.h"
@@ -30,7 +33,6 @@ struct cache_slot {
const char *lock_name;
int match;
struct stat cache_st;
- struct stat lock_st;
int bufsize;
char buf[CACHE_BUFSIZE];
};
@@ -81,6 +83,23 @@ static int close_slot(struct cache_slot *slot)
/* Print the content of the active cache slot (but skip the key). */
static int print_slot(struct cache_slot *slot)
{
+#ifdef HAVE_LINUX_SENDFILE
+ off_t start_off;
+ int ret;
+
+ start_off = slot->keylen + 1;
+
+ do {
+ ret = sendfile(STDOUT_FILENO, slot->cache_fd, &start_off,
+ slot->cache_st.st_size - start_off);
+ if (ret < 0) {
+ if (errno == EAGAIN || errno == EINTR)
+ continue;
+ return errno;
+ }
+ return 0;
+ } while (1);
+#else
ssize_t i, j;
i = lseek(slot->cache_fd, slot->keylen + 1, SEEK_SET);
@@ -97,6 +116,7 @@ static int print_slot(struct cache_slot *slot)
return errno;
else
return 0;
+#endif
}
/* Check if the slot has expired */
@@ -188,6 +208,10 @@ static int fill_slot(struct cache_slot *slot)
/* Generate cache content */
slot->fn(slot->cbdata);
+ /* update stat info */
+ if (fstat(slot->lock_fd, &slot->cache_st))
+ return errno;
+
/* Restore stdout */
if (dup2(tmp, STDOUT_FILENO) == -1)
return errno;
--
1.7.2.5
More information about the CGit
mailing list