tweaks to make more C-like and quieten down if desired; plus additional output at end
[ardour.git] / tools / readtest.c
1 /* gcc -o readtest readtest.c `pkg-config --cflags --libs glib-2.0` -lm */
2
3 #include <stdlib.h>
4 #include <errno.h>
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <getopt.h>
10 #include <fcntl.h>
11 #include <math.h>
12
13 #include <glib.h>
14
15 char* data = 0;
16
17 int
18 read_one (int fd, ssize_t sz)
19 {
20         return 0;
21 }
22
23 void
24 usage ()
25 {
26         fprintf (stderr, "readtest [ -b BLOCKSIZE ] [-l FILELIMIT] [ -D ] filename-template\n");
27 }
28
29 int
30 main (int argc, char* argv[])
31 {
32         int* files;
33         char optstring[] = "b:Dl:q";
34         uint32_t block_size = 64 * 1024 * 4;
35         int max_files = -1;
36 #ifdef __APPLE__
37         int direct = 0;
38 #endif
39         const struct option longopts[] = {
40                 { "blocksize", 1, 0, 'b' },
41                 { "direct", 0, 0, 'D' },
42                 { "limit", 1, 0, 'l' },
43                 { 0, 0, 0, 0 }
44         };
45
46         int option_index = 0;
47         int c = 0;
48         char const * name_template = 0;
49         int flags = O_RDONLY;
50         int n = 0;
51         int nfiles = 0;
52         int quiet = 0;
53         
54         while (1) {
55                 if ((c = getopt_long (argc, argv, optstring, longopts, &option_index)) == -1) {
56                         break;
57                 }
58
59                 switch (c) {
60                 case 'b':
61                         block_size = atoi (optarg);
62                         break;
63                 case 'l':
64                         max_files = atoi (optarg);
65                         break;
66                 case 'D':
67 #ifdef __APPLE__
68                         direct = 1;
69 #endif
70                         break;
71                 case 'q':
72                         quiet = 1;
73                         break;
74                 default:
75                         usage ();
76                         return 0;
77                 }
78         }
79         
80         if (optind < argc) {
81                 name_template = argv[optind];
82         } else {
83                 usage ();
84                 return 1;
85         }
86
87         while (1) {
88                 char path[PATH_MAX+1];
89
90                 snprintf (path, sizeof (path), name_template, n+1);
91
92                 if (access (path, R_OK) != 0) {
93                         break;
94                 }
95
96                 ++n;
97
98                 if (max_files > 0 &&  n >= max_files) {
99                         break;
100                 }
101         }
102
103         if (n == 0) {
104                 fprintf (stderr, "No matching files found for %s\n", name_template);
105                 return 1;
106         }
107
108         if (!quiet) {
109                 printf ("Discovered %d files using %s\n", n, name_template);
110         }
111         
112         nfiles = n;
113         files = (int *) malloc (sizeof (int) * nfiles);
114
115         for (n = 0; n < nfiles; ++n) {
116
117                 char path[PATH_MAX+1];
118                 int fd;
119
120                 snprintf (path, sizeof (path), name_template, n+1);
121
122                 if ((fd = open (path, flags, 0644)) < 0) {
123                         fprintf (stderr, "Could not open file #%d @ %s (%s)\n", n, path, strerror (errno));
124                         return 1;
125                 }
126
127 #ifdef __APPLE__
128                 if (direct) {
129                         /* Apple man pages say only that it returns "a value other than -1 on success",
130                                  which probably means zero, but you just can't be too careful with
131                                  those guys.
132                                  */
133                         if (fcntl (fd, F_NOCACHE, 1) == -1) {
134                                 fprintf (stderr, "Cannot set F_NOCACHE on file #%d\n", n);
135                         }
136                 }
137 #endif
138
139                 files[n] = fd;
140         }
141
142         data = (char*) malloc (sizeof (char) * block_size);
143         uint64_t _read = 0;
144         double max_elapsed = 0;
145         double total_time = 0;
146
147         while (1) {
148                 gint64 before;
149                 before = g_get_monotonic_time();
150
151                 for (n = 0; n < nfiles; ++n) {
152
153                         if (read (files[n], (char*) data, block_size) != block_size) {
154                                 fprintf (stderr, "read failed on file %d (%s)\n", n, strerror (errno));
155                                 goto out;
156                         }
157                 }
158
159                 _read += block_size;
160                 gint64 elapsed = g_get_monotonic_time() - before;
161                 double bandwidth = ((nfiles * block_size)/1048576.0) / (elapsed/1000000.0);
162
163                 if (!quiet) {
164                         printf ("BW @ %lu %.3f seconds bandwidth %.4f MB/sec\n", (long unsigned int)_read, elapsed/1000000.0, bandwidth);
165                 }
166                 
167                 total_time += elapsed;
168
169                 if (elapsed > max_elapsed) {
170                         max_elapsed = elapsed;
171                 }
172
173         }
174
175 out:
176         if (max_elapsed > 0 && total_time > 0) {
177                 double bandwidth = ((nfiles * _read)/1048576.0) / (total_time/1000000.0);
178                 double min_throughput = ((nfiles * block_size)/1048576.0) / (max_elapsed/1000000.0);
179                 printf ("Min: %.4f MB/sec Avg: %.4f MB/sec  || Max: %.3f sec \n", min_throughput, bandwidth, max_elapsed/1000000.0);
180                 printf ("Max Track count: %d @ 48000SPS\n", (int) floor(1048576.0 * bandwidth / (4 * 48000.)));
181         }
182
183         return 0;
184 }