From 55b8a30049a0797789c8929094bbb074cfd7469d Mon Sep 17 00:00:00 2001 From: briggs Date: Wed, 6 Jun 2007 17:49:14 +0000 Subject: Dynamically allocate the buffer used for reading data from the input handle and default to 64k instead of the 1k (BUFSIZ) static buffer. This makes a large difference in performance of some applications. Make the buffer size tunable from the command line. --- progress.1 | 10 ++++++++-- progress.c | 28 +++++++++++++++++++++------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/progress.1 b/progress.1 index 3a19ab1..ac35bd3 100644 --- a/progress.1 +++ b/progress.1 @@ -1,4 +1,4 @@ -.\" $NetBSD: progress.1,v 1.12 2007/04/12 06:31:20 lukem Exp $ +.\" $NetBSD: progress.1,v 1.13 2007/06/06 17:49:14 briggs Exp $ .\" .\" Copyright (c) 2003-2007 The NetBSD Foundation, Inc. .\" All rights reserved. @@ -30,7 +30,7 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd April 12, 2007 +.Dd June 6, 2007 .Dt PROGRESS 1 .Os .Sh NAME @@ -39,6 +39,7 @@ .Sh SYNOPSIS .Nm .Op Fl ez +.Op Fl b Ar buffersize .Op Fl f Ar file .Op Fl l Ar length .Op Fl p Ar prefix @@ -65,6 +66,11 @@ simply displays a count of the data and the data rate. .Pp The options are as follows: .Bl -tag -width XlXlengthXX +.It Fl b Ar buffersize +Read in buffers of the specified size (default 64k). +An optional suffix (per +.Xr strsuftoll 3 ) +may be given. .It Fl e Display progress to standard error instead of standard output. .It Fl f Ar file diff --git a/progress.c b/progress.c index 79076f9..5c1eae5 100644 --- a/progress.c +++ b/progress.c @@ -1,4 +1,4 @@ -/* $NetBSD: progress.c,v 1.14 2007/02/07 15:21:21 hubertf Exp $ */ +/* $NetBSD: progress.c,v 1.15 2007/06/06 17:49:14 briggs Exp $ */ /*- * Copyright (c) 2003 The NetBSD Foundation, Inc. @@ -34,7 +34,7 @@ #include #ifndef lint -__RCSID("$NetBSD: progress.c,v 1.14 2007/02/07 15:21:21 hubertf Exp $"); +__RCSID("$NetBSD: progress.c,v 1.15 2007/06/06 17:49:14 briggs Exp $"); #endif /* not lint */ #include @@ -73,8 +73,9 @@ static void usage(void) { fprintf(stderr, - "usage: %s [-ez] [-f file] [-l length] [-p prefix] cmd [args...]\n", - getprogname()); + "usage: %s [-ez] [-b buffersize] [-f file] [-l length]\n" + " %*.s [-p prefix] cmd [args...]\n", + getprogname(), (int) strlen(getprogname()), ""); exit(EXIT_FAILURE); } @@ -82,13 +83,14 @@ usage(void) int main(int argc, char *argv[]) { - static char fb_buf[BUFSIZ]; + char *fb_buf; char *infile = NULL; pid_t pid = 0, gzippid = 0, deadpid; int ch, fd, outpipe[2]; int ws, gzipstat, cmdstat; int eflag = 0, lflag = 0, zflag = 0; ssize_t nr, nw, off; + size_t buffersize; struct stat statb; struct ttysize ts; @@ -97,10 +99,15 @@ main(int argc, char *argv[]) /* defaults: Read from stdin, 0 filesize (no completion estimate) */ fd = STDIN_FILENO; filesize = 0; + buffersize = 64 * 1024; prefix = NULL; - while ((ch = getopt(argc, argv, "ef:l:p:z")) != -1) + while ((ch = getopt(argc, argv, "b:ef:l:p:z")) != -1) switch (ch) { + case 'b': + buffersize = (size_t) strsuftoll("buffer size", optarg, + 0, SIZE_T_MAX); + break; case 'e': eflag++; break; @@ -202,6 +209,10 @@ main(int argc, char *argv[]) else ttywidth = ts.ts_cols; + fb_buf = malloc(buffersize); + if (fb_buf == NULL) + err(1, "malloc for buffersize"); + if (pipe(outpipe) < 0) err(1, "output pipe"); pid = fork(); @@ -219,7 +230,7 @@ main(int argc, char *argv[]) close(outpipe[0]); progressmeter(-1); - while ((nr = read(fd, fb_buf, BUFSIZ)) > 0) + while ((nr = read(fd, fb_buf, buffersize)) > 0) for (off = 0; nr; nr -= nw, off += nw, bytes += nw) if ((nw = write(outpipe[1], fb_buf + off, (size_t) nr)) < 0) @@ -255,5 +266,8 @@ main(int argc, char *argv[]) } progressmeter(1); + + free(fb_buf); + exit(cmdstat ? cmdstat : gzipstat); } -- cgit v1.2.3-56-ge451