aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Sebastian Andrzej Siewior <sebastian@breakpoint.cc>2014-01-19 05:24:58 (JST)
committerGravatar Jason A. Donenfeld <Jason@zx2c4.com>2014-01-19 23:08:49 (JST)
commitd3581b58890389794de5d5222c91a0129873e95c (patch)
tree95c4010b61869934cca86c5ac23056463754f2ee
parentea7210bef377be4ffb088a1a8e5a9dd354f82afb (diff)
downloadcgit-d3581b58890389794de5d5222c91a0129873e95c.zip
cgit-d3581b58890389794de5d5222c91a0129873e95c.tar.gz
cache: use sendfile() instead of a pair of read() + write()
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@breakpoint.cc>
-rw-r--r--Makefile1
-rw-r--r--cache.c26
-rw-r--r--cgit.mk8
3 files changed, 34 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 2dc92df..05b97d7 100644
--- a/Makefile
+++ b/Makefile
@@ -29,6 +29,7 @@ DOC_PDF = $(patsubst %.txt,%.pdf,$(MAN_TXT))
29# j, z, t. (representing long long int, char, intmax_t, size_t, ptrdiff_t). 29# j, z, t. (representing long long int, char, intmax_t, size_t, ptrdiff_t).
30# some C compilers supported these specifiers prior to C99 as an extension. 30# some C compilers supported these specifiers prior to C99 as an extension.
31# 31#
32# Define HAVE_LINUX_SENDFILE to use sendfile()
32 33
33#-include config.mak 34#-include config.mak
34 35
diff --git a/cache.c b/cache.c
index fcd461f..9e7eeb0 100644
--- a/cache.c
+++ b/cache.c
@@ -13,6 +13,9 @@
13 * 13 *
14 */ 14 */
15 15
16#ifdef HAVE_LINUX_SENDFILE
17#include <sys/sendfile.h>
18#endif
16#include "cgit.h" 19#include "cgit.h"
17#include "cache.h" 20#include "cache.h"
18#include "html.h" 21#include "html.h"
@@ -30,7 +33,6 @@ struct cache_slot {
30 const char *lock_name; 33 const char *lock_name;
31 int match; 34 int match;
32 struct stat cache_st; 35 struct stat cache_st;
33 struct stat lock_st;
34 int bufsize; 36 int bufsize;
35 char buf[CACHE_BUFSIZE]; 37 char buf[CACHE_BUFSIZE];
36}; 38};
@@ -81,6 +83,23 @@ static int close_slot(struct cache_slot *slot)
81/* Print the content of the active cache slot (but skip the key). */ 83/* Print the content of the active cache slot (but skip the key). */
82static int print_slot(struct cache_slot *slot) 84static int print_slot(struct cache_slot *slot)
83{ 85{
86#ifdef HAVE_LINUX_SENDFILE
87 off_t start_off;
88 int ret;
89
90 start_off = slot->keylen + 1;
91
92 do {
93 ret = sendfile(STDOUT_FILENO, slot->cache_fd, &start_off,
94 slot->cache_st.st_size - start_off);
95 if (ret < 0) {
96 if (errno == EAGAIN || errno == EINTR)
97 continue;
98 return errno;
99 }
100 return 0;
101 } while (1);
102#else
84 ssize_t i, j; 103 ssize_t i, j;
85 104
86 i = lseek(slot->cache_fd, slot->keylen + 1, SEEK_SET); 105 i = lseek(slot->cache_fd, slot->keylen + 1, SEEK_SET);
@@ -97,6 +116,7 @@ static int print_slot(struct cache_slot *slot)
97 return errno; 116 return errno;
98 else 117 else
99 return 0; 118 return 0;
119#endif
100} 120}
101 121
102/* Check if the slot has expired */ 122/* Check if the slot has expired */
@@ -188,6 +208,10 @@ static int fill_slot(struct cache_slot *slot)
188 /* Generate cache content */ 208 /* Generate cache content */
189 slot->fn(); 209 slot->fn();
190 210
211 /* update stat info */
212 if (fstat(slot->lock_fd, &slot->cache_st))
213 return errno;
214
191 /* Restore stdout */ 215 /* Restore stdout */
192 if (dup2(tmp, STDOUT_FILENO) == -1) 216 if (dup2(tmp, STDOUT_FILENO) == -1)
193 return errno; 217 return errno;
diff --git a/cgit.mk b/cgit.mk
index 056c3f9..3b8b79a 100644
--- a/cgit.mk
+++ b/cgit.mk
@@ -68,6 +68,14 @@ ifeq ($(findstring BSD,$(uname_S)),)
68 CGIT_LIBS += -ldl 68 CGIT_LIBS += -ldl
69endif 69endif
70 70
71# glibc 2.1+ offers sendfile which the most common C library on Linux
72ifeq ($(uname_S),Linux)
73 HAVE_LINUX_SENDFILE = YesPlease
74endif
75
76ifdef HAVE_LINUX_SENDFILE
77 CGIT_CFLAGS += -DHAVE_LINUX_SENDFILE
78endif
71 79
72CGIT_OBJ_NAMES += cgit.o 80CGIT_OBJ_NAMES += cgit.o
73CGIT_OBJ_NAMES += cache.o 81CGIT_OBJ_NAMES += cache.o