diff -ur src-orig/network_linux_sendfile.c src/network_linux_sendfile.c --- src-orig/network_linux_sendfile.c 2006-10-08 20:24:14.000000000 +0200 +++ src/network_linux_sendfile.c 2007-10-26 15:43:43.000000000 +0200 @@ -1,6 +1,13 @@ #include "network_backends.h" #ifdef USE_LINUX_SENDFILE + +/* on linux 2.4.29 + debian/ubuntu we have crashes if this is enabled */ +#undef HAVE_POSIX_FADVISE +#ifdef HAVE_POSIX_FADVISE +#define FADVISE_READ_AHEAD 1024*1024 /* must be a power of 2 */ +#endif + #include #include #include @@ -23,9 +30,6 @@ #include "log.h" #include "stat_cache.h" -/* on linux 2.4.29 + debian/ubuntu we have crashes if this is enabled */ -#undef HAVE_POSIX_FADVISE - int network_write_chunkqueue_linuxsendfile(server *srv, connection *con, int fd, chunkqueue *cq) { chunk *c; size_t chunks_written = 0; @@ -136,6 +140,17 @@ /* limit the toSend to 2^31-1 bytes in a chunk */ toSend = c->file.length - c->offset > ((1 << 30) - 1) ? ((1 << 30) - 1) : c->file.length - c->offset; +#if FADVISE_READ_AHEAD + { + size_t ntS; + + ntS = ( (offset+FADVISE_READ_AHEAD) & (~(FADVISE_READ_AHEAD-1)) ) - offset; + + if (toSend > ntS) { + toSend = ntS; + } + } +#endif /* open file if not already opened */ if (-1 == c->file.fd) { @@ -148,6 +163,13 @@ fcntl(c->file.fd, F_SETFD, FD_CLOEXEC); #endif #ifdef HAVE_POSIX_FADVISE +#if FADVISE_READ_AHEAD + /* pre-read the first chunk */ + if (-1 == posix_fadvise(c->file.fd, offset, toSend, POSIX_FADV_NOREUSE)) { + log_error_write(srv, __FILE__, __LINE__, "ssd", + "posix_fadvise failed:", strerror(errno), c->file.fd); + } +#else /* tell the kernel that we want to stream the file */ if (-1 == posix_fadvise(c->file.fd, 0, 0, POSIX_FADV_SEQUENTIAL)) { if (ENOSYS != errno) { @@ -156,6 +178,7 @@ } } #endif +#endif } if (-1 == (r = sendfile(fd, c->file.fd, &offset, toSend))) { @@ -197,21 +220,16 @@ return -2; } -#ifdef HAVE_POSIX_FADVISE -#if 0 -#define K * 1024 -#define M * 1024 K -#define READ_AHEAD 4 M +#ifdef FADVISE_READ_AHEAD /* check if we need a new chunk */ - if ((c->offset & ~(READ_AHEAD - 1)) != ((c->offset + r) & ~(READ_AHEAD - 1))) { + if ((offset & ~(FADVISE_READ_AHEAD - 1)) != ((offset + r) & ~(FADVISE_READ_AHEAD - 1))) { /* tell the kernel that we want to stream the file */ - if (-1 == posix_fadvise(c->file.fd, (c->offset + r) & ~(READ_AHEAD - 1), READ_AHEAD, POSIX_FADV_NOREUSE)) { + if (-1 == posix_fadvise(c->file.fd, (offset + r) & ~(FADVISE_READ_AHEAD - 1), FADVISE_READ_AHEAD, POSIX_FADV_NOREUSE)) { log_error_write(srv, __FILE__, __LINE__, "ssd", "posix_fadvise failed:", strerror(errno), c->file.fd); } } #endif -#endif c->offset += r; cq->bytes_out += r;