added latency tool
[tridge/junkcode.git] / readfiles.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <fcntl.h>
5 #include <errno.h>
6 #include <sys/time.h>
7
8
9 static struct timeval tp1,tp2;
10 static size_t block_size = 64 * 1024;
11 static int show_latency;
12 static double min_latency, max_latency, total_time;
13 static unsigned num_blocks;
14
15 static void start_timer()
16 {
17         gettimeofday(&tp1,NULL);
18 }
19
20 static double end_timer()
21 {
22         gettimeofday(&tp2,NULL);
23         return((tp2.tv_sec - tp1.tv_sec) + 
24                (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
25 }
26
27
28 static void read_file(char *fname)
29 {
30         int fd;
31         static double total, thisrun;
32         int n;
33         char *buf;
34         double last_time;
35
36         buf = malloc(block_size);
37
38         if (!buf) {
39                 printf("Malloc of %d failed\n", block_size);
40                 exit(1);
41         }
42
43         fd = open(fname, O_RDONLY);
44         if (fd == -1) {
45                 perror(fname);
46                 free(buf);
47                 return;
48         }
49
50         if (show_latency) {
51                 last_time = end_timer();
52         }
53
54         while ((n = read(fd, buf, block_size)) > 0) {
55                 total += n;
56                 thisrun += n;
57                 if (show_latency) {
58                         double t = end_timer();
59                         double latency = t - last_time;
60                         if (latency > max_latency) {
61                                 max_latency = latency;
62                         }
63                         if (min_latency == 0 ||
64                             latency < min_latency) {
65                                 min_latency = latency;
66                         }
67                         last_time = t;
68                 }
69                 if (end_timer() >= 1.0) {
70                         if (show_latency) {
71                                 printf("%d MB    %g MB/sec  minlat=%.3f ms  maxlat=%.3f ms\n", 
72                                        (int)(total/1.0e6),
73                                        (thisrun*1.0e-6)/end_timer(),
74                                        min_latency * 1000, max_latency * 1000);
75                         } else {
76                                 printf("%d MB    %g MB/sec\n", 
77                                        (int)(total/1.0e6),
78                                        (thisrun*1.0e-6)/end_timer());
79                         }
80
81                         start_timer();
82                         last_time = 0;
83                         thisrun = 0;
84                         min_latency = max_latency = 0;
85                 }
86         }
87
88         free(buf);
89         close(fd);
90 }
91
92
93 static void usage(void)
94 {
95         printf("\n" \
96 "readfiles - reads from a list of files, showing read throughput\n" \
97 "\n" \
98 "Usage: readfiles [options] <files>\n" \
99 "\n" \
100 "Options:\n" \
101 "    -B size        set the block size in bytes\n" \
102 "    -L             show latencies\n" \
103 "");
104 }
105
106 int main(int argc, char *argv[])
107 {
108         int i;
109         extern char *optarg;
110         extern int optind;
111         int c;
112
113         while ((c = getopt(argc, argv, "B:hL")) != -1) {
114                 switch (c) {
115                 case 'B':
116                         block_size = strtol(optarg, NULL, 0);
117                         break;
118                 case 'L':
119                         show_latency = 1;
120                         break;
121                 case 'h':
122                 default:
123                         usage();
124                         exit(1);
125                 }
126         }
127
128         argc -= optind;
129         argv += optind;
130
131         if (argc == 0) {
132                 usage();
133                 exit(1);
134         }
135
136         start_timer();
137
138         while (1) {
139                 for (i=0; i < argc; i++) {
140                         read_file(argv[i]);
141                 }
142         }
143
144         return 0;
145 }
146