1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
#include <stdio.h>
#include <unistd.h>
#include <linux/unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
double readmegs(int fd, int size, char *buf);
double average(double *array, int count);
void printavg(int position, double avg, int block_size);
const int meg = 1024*1024;
const int max_blocks = 10000;
typedef double *PDOUBLE;
void usage()
{
printf("Usage: zcav file-name [count] [block-size]\n"
"File name of \"-\" means standard input\n"
"Count is the number of times to read the data (default 1).\n");
exit(1);
}
int main(int argc, char *argv[])
{
double **times = NULL;
int count[max_blocks];
int block_size = 100;
if(argc < 2 || argc > 4)
usage();
int max_loops = 1;
if(argc >= 3)
max_loops = atoi(argv[2]);
if(argc == 4)
block_size = atoi(argv[3]);
if(max_loops < 1 || block_size < 1)
usage();
int i;
if(max_loops > 1)
{
times = new PDOUBLE[max_loops];
for(i = 0; i < max_loops; i++)
times[i] = new double[max_blocks];
for(i = 0; i < max_blocks; i++)
{
times[0][i] = 0.0;
count[i] = 0;
}
}
char *buf = new char[meg];
int fd;
if(strcmp(argv[1], "-"))
{
fd = open(argv[1], O_RDONLY);
if(fd == -1)
{
printf("Can't open %s\n", argv[1]);
return 1;
}
}
else
{
fd = 0;
}
if(max_loops > 1)
{
for(int loops = 0; loops < max_loops; loops++)
{
if(lseek(fd, 0, SEEK_SET))
{
printf("Can't llseek().\n");
return 1;
}
for(i = 0; times[0][i] != -1.0; i += block_size)
{
double read_time = readmegs(fd, block_size, buf);
times[loops][i] += read_time;
if(read_time < 0.0)
{
times[0][i] = -1.0;
break;
}
count[i]++;
}
}
printf("loops: %d\n", max_loops);
double *item_times = new double[max_loops];
for(i = 0; count[i]; i += block_size)
{
for(int j = 0; j < max_loops; j++)
item_times[j] = times[j][i];
printavg(i, average(item_times, count[i]), block_size);
}
}
else
{
for(i = 0; 1; i += block_size)
{
double read_time = readmegs(fd, block_size, buf);
if(read_time < 0.0)
break;
printavg(i, read_time, block_size);
}
}
return 0;
}
void printavg(int position, double avg, int block_size)
{
double num_k = double(block_size * 1024);
if(avg < 1.0)
printf("%d %f \n", position, avg);
else
printf("%d %f %d\n", position, avg, int(num_k / avg));
}
int compar(const void *a, const void *b)
{
double *c = static_cast<double *>(a);
double *d = static_cast<double *>(b);
if(*c < *d) return -1;
if(*c > *d) return 1;
return 0;
}
double average(double *array, int count)
{
qsort(array, count, sizeof(double), compar);
int skip = count / 3;
int arr_items = count - (skip * 2);
double total = 0.0;
for(int i = skip; i < (count - skip); i++)
{
total += array[i];
}
return total / arr_items;
}
// just like the read() system call but takes a (char *) and will not return
// a partial result.
ssize_t readall(int fd, char *buf, size_t count)
{
ssize_t total = 0;
while(total != static_cast<ssize_t>(count) )
{
ssize_t rc = read(fd, &buf[total], count - total);
if(rc == -1 || rc == 0)
return -1;
total += rc;
}
return total;
}
// Read the specified number of megabytes of data from the fd and return the
// amount of time elapsed in seconds.
double readmegs(int fd, int size, char *buf)
{
struct timeval tp;
if (gettimeofday(&tp, static_cast<struct timezone *>(NULL)) == -1)
{
printf("Can't get time.\n");
return -1.0;
}
double start = double(tp.tv_sec) +
(double(tp.tv_usec) / 1000000.0);
for(int i = 0; i < size; i++)
{
int rc = readall(fd, buf, meg);
if(rc != meg)
return -1.0;
}
if (gettimeofday(&tp, static_cast<struct timezone *>(NULL)) == -1)
{
printf("Can't get time.\n");
return -1.0;
}
return (double(tp.tv_sec) + (double(tp.tv_usec) / 1000000.0))
- start;
}
|