From: cgit at ml.breakpoint.cc (cgit at ml.breakpoint.cc)
Subject: [PATCH 2/3] cache: use sendfile() instead of a pair of read() + write()
Date: Thu, 21 Jun 2012 09:09:47 +0200 [thread overview]
Message-ID: <1340262588-24202-2-git-send-email-cgit@ml.breakpoint.cc> (raw)
In-Reply-To: <1340262588-24202-1-git-send-email-cgit@ml.breakpoint.cc>
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 NO_SENDFILE in case the OS (kernel / libc)
does not supported. It is disabled by default on non-linux environemnts.
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>
---
Makefile | 10 ++++++++++
cache.c | 26 +++++++++++++++++++++++++-
2 files changed, 35 insertions(+), 1 deletions(-)
diff --git a/Makefile b/Makefile
index eac24ad..5cc84f4 100644
--- a/Makefile
+++ b/Makefile
@@ -33,6 +33,9 @@ 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 NO_SENDFILE to avoid using the sendfile() offered by libc. Use this
+# if your OS does not provide support for sendfile()
+#
#-include config.mak
@@ -49,6 +52,10 @@ ifeq ($(uname_O),Cygwin)
NEEDS_LIBICONV = YesPlease
endif
+ifneq ($(uname_S),Linux)
+ NO_SENDFILE = YesPlease
+endif
+
#
# Let the user override the above settings.
#
@@ -157,6 +164,9 @@ ifdef NO_OPENSSL
else
EXTLIBS += -lcrypto
endif
+ifdef NO_SENDFILE
+ CFLAGS += -DNO_SENDFILE
+endif
cgit: $(OBJECTS) libgit
$(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o cgit $(OBJECTS) $(EXTLIBS)
diff --git a/cache.c b/cache.c
index d7a8d5a..19bbc11 100644
--- a/cache.c
+++ b/cache.c
@@ -13,6 +13,9 @@
*
*/
+#ifndef NO_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,7 @@ 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 NO_SENDFILE
ssize_t i, j;
i = lseek(slot->cache_fd, slot->keylen + 1, SEEK_SET);
@@ -97,6 +100,23 @@ static int print_slot(struct cache_slot *slot)
return errno;
else
return 0;
+#else
+ 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);
+#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
next prev parent reply other threads:[~2012-06-21 7:09 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-06-21 7:09 [PATCH 1/3] snapshots: Don't allow sneaked in snapshots requests cgit
2012-06-21 7:09 ` cgit [this message]
2012-06-23 10:27 ` [PATCH 2/3] cache: use sendfile() instead of a pair of read() + write() mathstuf
2012-06-23 19:25 ` [PATCH 2/3 v2] " cgit
2012-06-21 7:09 ` [PATCH 3/3] summary: Add tag head line in the dowload section cgit
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1340262588-24202-2-git-send-email-cgit@ml.breakpoint.cc \
--to=cgit@lists.zx2c4.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).