[4] | 1 | /****************************************************************************** |
---|
| 2 | * SAGE - Scalable Adaptive Graphics Environment |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2004 Electronic Visualization Laboratory, |
---|
| 5 | * University of Illinois at Chicago |
---|
| 6 | * |
---|
| 7 | * All rights reserved. |
---|
| 8 | * |
---|
| 9 | * Redistribution and use in source and binary forms, with or without |
---|
| 10 | * modification, are permitted provided that the following conditions are met: |
---|
| 11 | * |
---|
| 12 | * * Redistributions of source code must retain the above copyright |
---|
| 13 | * notice, this list of conditions and the following disclaimer. |
---|
| 14 | * * Redistributions in binary form must reproduce the above |
---|
| 15 | * copyright notice, this list of conditions and the following disclaimer |
---|
| 16 | * in the documentation and/or other materials provided with the distribution. |
---|
| 17 | * * Neither the name of the University of Illinois at Chicago nor |
---|
| 18 | * the names of its contributors may be used to endorse or promote |
---|
| 19 | * products derived from this software without specific prior written permission. |
---|
| 20 | * |
---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 24 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
| 25 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
| 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
| 27 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
| 28 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
| 29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
| 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 32 | * |
---|
| 33 | * Direct questions, comments etc about SAGE to http://www.evl.uic.edu/cavern/forum/ |
---|
| 34 | * |
---|
| 35 | *****************************************************************************/ |
---|
| 36 | |
---|
| 37 | #define _GNU_SOURCE |
---|
| 38 | |
---|
| 39 | #include <stdio.h> |
---|
| 40 | #include <stdlib.h> |
---|
| 41 | #include <unistd.h> |
---|
| 42 | #include <fcntl.h> |
---|
| 43 | #include <sys/types.h> |
---|
| 44 | #include <sys/time.h> |
---|
| 45 | |
---|
| 46 | float dt() { |
---|
| 47 | static struct timeval then; |
---|
| 48 | struct timeval now; |
---|
| 49 | float delta; |
---|
| 50 | |
---|
| 51 | gettimeofday( &now, NULL ); |
---|
| 52 | if(then.tv_sec == 0) |
---|
| 53 | then = now; |
---|
| 54 | delta = now.tv_sec - then.tv_sec + 1e-6*(now.tv_usec - then.tv_usec); |
---|
| 55 | then = now; |
---|
| 56 | return delta; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | int main(int argc, char *argv[]) { |
---|
| 60 | int pagesize = getpagesize(); |
---|
| 61 | int pagemask = (pagesize - 1); |
---|
| 62 | char *fname = argv[1]; |
---|
| 63 | int bufsize = ( argc>2 ? atoi(argv[2]) : 128 ) * 1024; |
---|
| 64 | char *buf = (char *)malloc( bufsize + pagesize ); |
---|
| 65 | int count = argc>3 ? atoi( argv[3] ) : 1024; |
---|
| 66 | int interval = argc>4 ? atoi(argv[4]) : 32; |
---|
| 67 | int offset = 0; |
---|
| 68 | int skip = 0; |
---|
| 69 | float total = 0; |
---|
| 70 | int i, margin, fd; |
---|
| 71 | char *p; |
---|
| 72 | |
---|
| 73 | if(argc <= 1) { |
---|
| 74 | fprintf(stderr, "Usage: %s [-]infilename kbyteseach totalbufs printinterval\n\ |
---|
| 75 | Read from infilename, in buffers of kbyteseach*1024 bytes (rounded up to 4K page size).\n\ |
---|
| 76 | Read totalbufs times, reporting average MBytes/sec every printinterval times,\n\ |
---|
| 77 | and average transfer rate at end of run.\n", argv[0]); |
---|
| 78 | exit(1); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | if(argc>5) |
---|
| 82 | sscanf(argv[5], "%d%*c%d", &offset, &skip); |
---|
| 83 | |
---|
| 84 | bufsize = (bufsize + pagesize - 1) & ~pagemask; |
---|
| 85 | |
---|
| 86 | offset = offset * bufsize; |
---|
| 87 | |
---|
| 88 | #ifdef sun |
---|
| 89 | fd = open(fname, O_RDONLY); |
---|
| 90 | if (fname[0]=='-') |
---|
| 91 | directio(fd, DIRECTIO_ON); |
---|
| 92 | #else |
---|
| 93 | fd = fname[0]=='-' ? open(fname+1, O_RDONLY | O_DIRECT) : open(fname, O_RDONLY); |
---|
| 94 | #endif |
---|
| 95 | if(fd < 0) { |
---|
| 96 | perror(fname[0]=='-' ? fname+1 : fname); |
---|
| 97 | exit(1); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | lseek( fd, offset, SEEK_SET ); |
---|
| 101 | |
---|
| 102 | margin = (buf - (char *)NULL) & pagemask; |
---|
| 103 | p = buf - margin; |
---|
| 104 | |
---|
| 105 | dt(); |
---|
| 106 | for(i = 0; i < count; i++) { |
---|
| 107 | if(read(fd, p, bufsize) < bufsize) { |
---|
| 108 | perror("read"); |
---|
| 109 | break; |
---|
| 110 | } |
---|
| 111 | if(skip > 1) |
---|
| 112 | lseek(fd, (skip-1)*bufsize, SEEK_CUR); |
---|
| 113 | |
---|
| 114 | if(interval>0 && (i+1)%interval == 0) { |
---|
| 115 | float deltat = dt(); |
---|
| 116 | total += deltat; |
---|
| 117 | printf("%6.2f ", interval * bufsize / (1048576 * deltat)); |
---|
| 118 | fflush(stdout); |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | total += dt(); |
---|
| 122 | if(interval > 0) |
---|
| 123 | printf("\n"); |
---|
| 124 | printf("%.2f\n", i * bufsize / (1048576 * total)); |
---|
| 125 | return 0; |
---|
| 126 | } |
---|
| 127 | |
---|