From d6dead6bb15edc672b188bc2941ab02744a899bb Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 15 May 1998 07:34:19 +0000 Subject: [PATCH] added write buffering during the file list sending. This makes things a bit more efficient (less system calls) --- flist.c | 10 +++++++++- io.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- rsync.h | 1 + 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/flist.c b/flist.c index 4c2e984c..d7da6e88 100644 --- a/flist.c +++ b/flist.c @@ -573,6 +573,10 @@ struct file_list *send_file_list(int f,int argc,char *argv[]) flist->malloced); if (!flist->files) out_of_memory("send_file_list"); + if (f != -1) { + io_start_buffering(f); + } + for (i=0;i 2) rprintf(FINFO,"send_file_list done\n"); diff --git a/io.c b/io.c index 52c4cb14..1fafaa62 100644 --- a/io.c +++ b/io.c @@ -301,7 +301,7 @@ int write_file(int f,char *buf,int len) } -static int writefd(int fd,char *buf,int len) +static int writefd_unbuffered(int fd,char *buf,int len) { int total = 0; fd_set w_fds, r_fds; @@ -375,6 +375,58 @@ static int writefd(int fd,char *buf,int len) return total; } +static char *io_buffer; +static int io_buffer_count; + +void io_start_buffering(int fd) +{ + io_buffer = (char *)malloc(IO_BUFFER_SIZE); + if (!io_buffer) out_of_memory("writefd"); + io_buffer_count = 0; +} + +void io_end_buffering(int fd) +{ + if (io_buffer_count) { + if (writefd_unbuffered(fd, io_buffer, + io_buffer_count) != + io_buffer_count) { + rprintf(FERROR,"write failed\n"); + exit_cleanup(1); + } + io_buffer_count = 0; + } + free(io_buffer); + io_buffer = NULL; +} + +static int writefd(int fd,char *buf,int len1) +{ + int len = len1; + + if (!io_buffer) return writefd_unbuffered(fd, buf, len); + + while (len) { + int n = MIN(len, IO_BUFFER_SIZE-io_buffer_count); + if (n > 0) { + memcpy(io_buffer+io_buffer_count, buf, n); + buf += n; + len -= n; + io_buffer_count += n; + } + + if (io_buffer_count == IO_BUFFER_SIZE) { + if (writefd_unbuffered(fd, io_buffer, + io_buffer_count) != + io_buffer_count) { + return -1; + } + io_buffer_count = 0; + } + } + + return len1; +} void write_int(int f,int32 x) diff --git a/rsync.h b/rsync.h index 13116707..187ef375 100644 --- a/rsync.h +++ b/rsync.h @@ -51,6 +51,7 @@ #define WRITE_SIZE (32*1024) #define CHUNK_SIZE (32*1024) #define MAX_MAP_SIZE (4*1024*1024) +#define IO_BUFFER_SIZE (4096) #define MAX_ARGS 1000 -- 2.34.1