diff options
author | Sebastian Andrzej Siewior <sebastian@breakpoint.cc> | 2014-01-19 05:24:58 (JST) |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2014-01-19 23:08:49 (JST) |
commit | d3581b58890389794de5d5222c91a0129873e95c (patch) | |
tree | 95c4010b61869934cca86c5ac23056463754f2ee /cache.c | |
parent | ea7210bef377be4ffb088a1a8e5a9dd354f82afb (diff) | |
download | cgit-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>
Diffstat (limited to 'cache.c')
-rw-r--r-- | cache.c | 26 |
1 files changed, 25 insertions, 1 deletions
@@ -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). */ |
82 | static int print_slot(struct cache_slot *slot) | 84 | static 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; |