Gene Cumm
2010-Mar-20 12:13 UTC
[syslinux] [MODULE] COM32 module to time null-dumping a file, v1.1
From: Gene Cumm <gene.cumm at gmail.com> COM32/samples/cptime.c: A module for coarse null-dumping speed comparisons. Accepts multiple files and several options. -l shows long format (default; for overriding -s). -s shows short format. -b <NUM> changes the transfer buffer size. Also computes/displays +/- 1 tick to show coarseness in long format. Signed-off-by: Gene Cumm <gene.cumm at gmail.com> --- Added -b <NUM> and -l options and improved the command line option parsing. diff --git a/com32/samples/cptime.c b/com32/samples/cptime.c new file mode 100644 index 0000000..4df7fc3 --- /dev/null +++ b/com32/samples/cptime.c @@ -0,0 +1,198 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 2010 Gene Cumm - All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, Inc., 53 Temple Place Ste 330, + * Boston MA 02111-1307, USA; either version 2 of the License, or + * (at your option) any later version; incorporated herein by reference. + * + * ----------------------------------------------------------------------- */ + +/* + * cptime.c Version 1.1 + * + * Timed copy; read entire file then output total time, bytes transferred, + * and compute transfer rate. + * + * cptime [-s|-l] [-b _SIZE_] _FILE_... + * -s Change to simple output mode without computing transfer rate + * -l Change to long output mode (to allow for overriding previous -s) + * -b _SIZE_ use _SIZE_ for transfer size + * _FILE_... Space delimited list of files to dump + * + * Hisory: + * 1.1 Added -l and -b switches; more flexible command line processing + * 1.0 First release + */ + +/* + * ToDos: + * - Refine timing to be more precise. Low priority. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/times.h> +#include <consoles.h> +#include <minmax.h> +#include <limits.h> +#include <string.h> + +#ifdef __COM32__ +# define BUFSZ_DEF 2048 +/* What's optimal? Under 4k? + * layout.inc: xfer_buf_seg equ 1000h + */ +# define O_DIRECT 0 + +static inline float get_tps(){ return 18.2; } + +#else /* __COM32__ */ + +# define BUFSZ_DEF 16384 +/* Need to check what might be a "best" buffer/fetch block size here */ + +static inline float get_tps(){ return (float)sysconf(_SC_CLK_TCK); } + +#endif /* __COM32__ */ + +#ifndef SSIZE_MAX +# define SSIZE_MAX PTRDIFF_MAX +#endif +/* typedef ptrdiff_t ssize_t; */ +#define BUFSZ_MAX SSIZE_MAX +/* ssize_t max */ +#define BUFSZ_MIN 1 +#ifndef OFF_MAX +# define OFF_MAX SIZE_MAX +#endif +/* typedef size_t off_t */ +#define LEN_MAX OFF_MAX +/* off_t max */ + +void print_cp_result_tick(int bcnt, clock_t et, float tps, int offs) +{ + int dr; + dr = bcnt * tps / max(1,(et + offs)); // prevent divide by 0 + printf(" %+d %d B/s; %d KiB/s; %d MiB/s\n", offs, dr, dr/1024, dr/1048576); +} /* void print_cp_result_tick(int bcnt, clock_t et, float tps, int offs) */ + +void print_cp_result(char *fn, int bcnt, clock_t bc, clock_t ec, int bufsz) +{ + float tps; + tps = get_tps(); + printf(" %dB in %d ticks from '%s'\n", bcnt, (int)(ec - bc), fn); + printf(" ~%d ticks per second; %dB block/transfer size\n", (int)tps, bufsz); + print_cp_result_tick(bcnt, (ec - bc), tps, 0); + print_cp_result_tick(bcnt, (ec - bc), tps, 1); + print_cp_result_tick(bcnt, (ec - bc), tps, -1); +} /* void print_cp_result(char *fn, int bcnt, clock_t bc, clock_t ec) */ + +void print_cp_result_simple(char *fn, int bcnt, clock_t bc, clock_t ec, int bufsz) +{ + printf(" %dB %dt %dx '%s'\n", bcnt, (int)(ec - bc), bufsz, fn); +} /* void print_cp_result_simple(char *fn, int bcnt, clock_t bc, clock_t ec) */ + +int time_copy(char *fn, char do_simple, int bufsz) +{ + int fd, rv = 0; + int bcnt; + int numrd; + struct tms tm; + clock_t bc, ec; + char buf[bufsz + 1]; + + buf[0] = 0; + fd = open(fn, O_RDONLY); + // | O_DIRECT leads to alignment issues that aren't easy to resolve + if (fd == -1) { + switch (errno){ + case ENOENT : + printf("File '%s' does not exist\n", fn); + break; + default : + printf("Error '%d' opening file '%s'\n", errno, fn); + } + rv = 1; + } else { + bc = times(&tm); + numrd = read(fd, buf, bufsz); + bcnt = numrd; + while(numrd > 0){ + numrd = read(fd, buf, bufsz); + if (numrd >= 0) + bcnt = bcnt + numrd; + } + ec = times(&tm); + close(fd); + if (numrd < 0) + { + if (bcnt < 0) + bcnt = 0; + switch (errno){ + case EIO : + printf("IO Error at %dB reading file '%s'\n", bcnt, fn); + break; + case EINVAL : + printf("Invalid Mode at %dB reading file '%s'\n", bcnt, fn); + break; + default : + printf("Error '%d' at %dB reading file '%s'\n", errno, bcnt, fn); + } + rv = 2; + } + if (bcnt > 0) + { + if (do_simple) + print_cp_result_simple(fn, bcnt, bc, ec, bufsz); + else + print_cp_result(fn, bcnt, bc, ec, bufsz); + } + } + return rv; +} /* int time_copy(char *fn, char do_simple) */ + +int main(int argc, char *argv[]) +{ + int i; + char do_simple = 0; + int bufsz = min((BUFSZ_DEF), (BUFSZ_MAX)); + int tbufsz; + int numfl = 0; + console_ansi_std(); + for (i=1;i<argc;i++) + { + if (strcmp(argv[i], "-b") == 0) + { + i++; + if (i<argc) + { + tbufsz = atoi(argv[i]); + if (tbufsz > 0) + bufsz = min(max(BUFSZ_MIN, tbufsz), (BUFSZ_MAX)); + printf("Using bufsz %d\n", bufsz); + } + }else if (strcmp(argv[i], "-s") == 0) + do_simple = 1; + else if (strcmp(argv[i], "-l") == 0) + do_simple = 0; + } + for (i=1;i<argc;i++) + { + if (strcmp(argv[i], "-b") == 0) + i++; // Skip next arg assuming attempt at size + else if (!((strcmp(argv[i], "-s") == 0) || (strcmp(argv[i], "-l") == 0))) + { + time_copy(argv[i], do_simple, bufsz); + numfl++; + } + } + if (numfl == 0) + fprintf(stderr, "%s: Please specify a file\n", argv[0]); + return 0; +} /* int main(int argc, char *argv[]) */
Andrew Stuart
2010-Mar-23 00:15 UTC
[syslinux] [MODULE] COM32 module to time null-dumping a file, v1.1
On 3/20/2010 5:13 AM, Gene Cumm wrote:> From: Gene Cumm<gene.cumm at gmail.com> > > COM32/samples/cptime.c: A module for coarse null-dumping speed > comparisons. Accepts multiple files and several options. -l shows > long format (default; for overriding -s). -s shows short format. -b > <NUM> changes the transfer buffer size. Also computes/displays +/- 1 > tick to show coarseness in long format. > > Signed-off-by: Gene Cumm<gene.cumm at gmail.com> >Gene, I haven't had a chance to try this yet, I don't have the means to compile it at the moment (but hope to have that resolved sometime this week), But I did want to say thank you for your efforts. -Andrew