Add Alfred Perlstein's sendfile patches, as an optional knob.

This commit is contained in:
Anders Nordby 2002-10-21 00:29:02 +00:00
parent ed443fc0bf
commit 1e6eb53ae0
Notes: svn2git 2021-03-31 03:12:20 +00:00
svn path=/head/; revision=68452
4 changed files with 150 additions and 0 deletions

View file

@ -0,0 +1,12 @@
--- libhttpd.c.orig Mon May 27 01:22:26 2002
+++ libhttpd.c Sun Oct 20 23:49:58 2002
@@ -3816,6 +3816,9 @@
httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl );
return -1;
}
+#ifdef USE_SENDFILE
+ hc->file_fd = *((int *) hc->file_address);
+#endif
send_mime(
hc, 200, ok200title, hc->encodings, "", hc->type, hc->sb.st_size,
hc->sb.st_mtime );

View file

@ -0,0 +1,12 @@
--- libhttpd.h.orig Sun Oct 20 23:50:43 2002
+++ libhttpd.h Sun Oct 20 23:51:10 2002
@@ -141,6 +141,9 @@
struct stat sb;
int conn_fd;
char* file_address;
+#ifdef USE_SENDFILE
+ int file_fd;
+#endif
} httpd_conn;
/* Methods. */

View file

@ -0,0 +1,70 @@
--- mmc.c.orig Wed May 8 03:35:05 2002
+++ mmc.c Sun Oct 20 23:56:15 2002
@@ -74,6 +74,9 @@
time_t ctime;
int refcount;
time_t reftime;
+#ifdef USE_SENDFILE
+ int fd;
+#endif
void* addr;
unsigned int hash;
int hash_idx;
@@ -140,7 +143,11 @@
/* Yep. Just return the existing map */
++m->refcount;
m->reftime = now;
+#ifdef USE_SENDFILE
+ return (&m->fd);
+#else
return m->addr;
+#endif
}
/* Open the file. */
@@ -186,7 +193,9 @@
else
{
size_t size_size = (size_t) m->size; /* loses on files >2GB */
-#ifdef HAVE_MMAP
+#ifdef USE_SENDFILE
+ m->fd = fd;
+#elif defined(HAVE_MMAP)
/* Map the file into memory. */
m->addr = mmap( 0, size_size, PROT_READ, MAP_PRIVATE, fd, 0 );
if ( m->addr == (void*) -1 && errno == ENOMEM )
@@ -240,8 +249,9 @@
}
#endif /* HAVE_MMAP */
}
+#ifndef USE_SENDFILE
(void) close( fd );
-
+#endif /* !USE_SENDFILE */
/* Put the Map into the hash table. */
if ( add_hash( m ) < 0 )
{
@@ -259,8 +269,12 @@
/* Update the total byte count. */
mapped_bytes += m->size;
+#ifdef USE_SENDFILE
+ return (&m->fd);
+#else
/* And return the address. */
return m->addr;
+#endif
}
@@ -369,7 +383,9 @@
m = *mm;
if ( m->size != 0 )
{
-#ifdef HAVE_MMAP
+#ifdef USE_SENDFILE
+ close(m->fd);
+#elif defined(HAVE_MMAP)
if ( munmap( m->addr, m->size ) < 0 )
syslog( LOG_ERR, "munmap - %m" );
#else /* HAVE_MMAP */

View file

@ -0,0 +1,56 @@
--- thttpd.c.orig Sat May 25 19:43:13 2002
+++ thttpd.c Sun Oct 20 23:58:44 2002
@@ -1500,12 +1500,45 @@
if ( hc->responselen == 0 )
{
/* No, just write the file. */
+#ifdef USE_SENDFILE
+ off_t sbytes;
+
+ sz = sendfile(
+ hc->file_fd, hc->conn_fd, c->bytes_sent,
+ MIN( c->bytes_to_send - c->bytes_sent, c->limit ),
+ NULL, &sbytes, 0 );
+ if (sz == -1 && errno == EAGAIN)
+ sz = sbytes > 0 ? sbytes : -1;
+ else if (sz == 0)
+ sz = sbytes;
+#else
sz = write(
hc->conn_fd, &(hc->file_address[c->bytes_sent]),
MIN( c->bytes_to_send - c->bytes_sent, c->limit ) );
+#endif
}
else
{
+#ifdef USE_SENDFILE
+ struct sf_hdtr sf;
+ struct iovec iv;
+ off_t sbytes;
+
+ iv.iov_base = hc->response;
+ iv.iov_len = hc->responselen;
+ sf.headers = &iv;
+ sf.hdr_cnt = 1;
+ sf.trailers = NULL;
+ sf.trl_cnt = 0;
+ sz = sendfile(
+ hc->file_fd, hc->conn_fd, c->bytes_sent,
+ MIN( c->bytes_to_send - c->bytes_sent, c->limit ),
+ &sf, &sbytes, 0 );
+ if (sz == -1 && errno == EAGAIN)
+ sz = sbytes > 0 ? sbytes : -1;
+ else if (sz == 0)
+ sz = sbytes;
+#else
/* Yes. We'll combine headers and file into a single writev(),
** hoping that this generates a single packet.
*/
@@ -1516,6 +1549,7 @@
iv[1].iov_base = &(hc->file_address[c->bytes_sent]);
iv[1].iov_len = MIN( c->bytes_to_send - c->bytes_sent, c->limit );
sz = writev( hc->conn_fd, iv, 2 );
+#endif
}
if ( sz == 0 ||