replace ::cast_dynamic() with relevant ActionManager::get_*_action() calls
[ardour.git] / tools / readtest.c
1 /* gcc -o readtest readtest.c `pkg-config --cflags --libs glib-2.0` -lm */
2
3 #ifndef _WIN32
4 #  define HAVE_MMAP
5 #endif
6
7 #include <stdlib.h>
8 #include <errno.h>
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <stdint.h>
12 #include <string.h>
13 #include <getopt.h>
14 #include <fcntl.h>
15 #include <math.h>
16
17 #ifdef HAVE_MMAP
18 #  include <sys/stat.h>
19 #  include <sys/mman.h>
20 #endif
21
22 #include <glib.h>
23
24 char* data = 0;
25
26 void
27 usage ()
28 {
29         fprintf (stderr, "readtest [ -b BLOCKSIZE ] [-l FILELIMIT] [ -D ] [ -R ] [ -M ] filename-template\n");
30 }
31
32 int
33 main (int argc, char* argv[])
34 {
35         int* files;
36         char optstring[] = "b:DRMl:q";
37         uint32_t block_size = 64 * 1024 * 4;
38         int max_files = -1;
39 #ifdef __APPLE__
40         int direct = 0;
41         int noreadahead = 0;
42 #endif
43 #ifdef HAVE_MMAP
44         int use_mmap = 0;
45         void  **addr;
46         size_t *flen;
47 #endif
48         const struct option longopts[] = {
49                 { "blocksize", 1, 0, 'b' },
50                 { "direct", 0, 0, 'D' },
51                 { "mmap", 0, 0, 'M' },
52                 { "noreadahead", 0, 0, 'R' },
53                 { "limit", 1, 0, 'l' },
54                 { 0, 0, 0, 0 }
55         };
56
57         int option_index = 0;
58         int c = 0;
59         char const * name_template = 0;
60         int flags = O_RDONLY;
61         int n = 0;
62         int nfiles = 0;
63         int quiet = 0;
64
65         while (1) {
66                 if ((c = getopt_long (argc, argv, optstring, longopts, &option_index)) == -1) {
67                         break;
68                 }
69
70                 switch (c) {
71                 case 'b':
72                         block_size = atoi (optarg);
73                         break;
74                 case 'l':
75                         max_files = atoi (optarg);
76                         break;
77                 case 'D':
78 #ifdef __APPLE__
79                         direct = 1;
80 #endif
81                         break;
82                 case 'M':
83 #ifdef HAVE_MMAP
84                         use_mmap = 1;
85 #endif
86                         break;
87                 case 'R':
88 #ifdef __APPLE__
89                         noreadahead = 1;
90 #endif
91                         break;
92                 case 'q':
93                         quiet = 1;
94                         break;
95                 default:
96                         usage ();
97                         return 0;
98                 }
99         }
100
101         if (optind < argc) {
102                 name_template = argv[optind];
103         } else {
104                 usage ();
105                 return 1;
106         }
107
108         while (1) {
109                 char path[PATH_MAX+1];
110
111                 snprintf (path, sizeof (path), name_template, n+1);
112
113                 if (access (path, R_OK) != 0) {
114                         break;
115                 }
116
117                 ++n;
118
119                 if (max_files > 0 &&  n >= max_files) {
120                         break;
121                 }
122         }
123
124         if (n == 0) {
125                 fprintf (stderr, "No matching files found for %s\n", name_template);
126                 return 1;
127         }
128
129         if (!quiet) {
130                 printf ("# Discovered %d files using %s\n", n, name_template);
131         }
132
133         nfiles = n;
134         files = (int *) malloc (sizeof (int) * nfiles);
135 #ifdef HAVE_MMAP
136         if (use_mmap) {
137                 if (!quiet) {
138                         printf ("# Using mmap().\n");
139                 }
140                 addr = malloc (sizeof (void*) * nfiles);
141                 flen = (size_t*) malloc (sizeof (size_t) * nfiles);
142         }
143 #endif
144
145         for (n = 0; n < nfiles; ++n) {
146
147                 char path[PATH_MAX+1];
148                 int fd;
149
150                 snprintf (path, sizeof (path), name_template, n+1);
151
152                 if ((fd = open (path, flags, 0644)) < 0) {
153                         fprintf (stderr, "Could not open file #%d @ %s (%s)\n", n, path, strerror (errno));
154                         return 1;
155                 }
156
157 #ifdef __APPLE__
158                 if (direct) {
159                         /* Apple man pages say only that it returns "a value other than -1 on success",
160                                  which probably means zero, but you just can't be too careful with
161                                  those guys.
162                                  */
163                         if (fcntl (fd, F_NOCACHE, 1) == -1) {
164                                 fprintf (stderr, "Cannot set F_NOCACHE on file #%d\n", n);
165                         }
166                 }
167
168                 if (noreadahead) {
169                         if (fcntl (fd, F_RDAHEAD, 0) == -1) {
170                                 fprintf (stderr, "Cannot set F_READAHED on file #%d\n", n);
171                         }
172                 }
173 #endif
174
175                 files[n] = fd;
176
177 #ifdef HAVE_MMAP
178                 if (use_mmap) {
179                         struct stat s;
180                         if (fstat (fd, & s)) {
181                                 fprintf (stderr, "Could not stat fd #%d @ %s\n", n, path);
182                                 return 1;
183                         }
184                         if (s.st_size < block_size) {
185                                 fprintf (stderr, "file is shorter than blocksize #%d @ %s\n", n, path);
186                                 return 1;
187                         }
188                         flen[n] = s.st_size;
189                         addr[n] = mmap (0, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
190                         if (addr[n] == MAP_FAILED) {
191                                 fprintf (stderr, "Could not mmap file #%d @ %s (%s)\n", n, path, strerror (errno));
192                                 return 1;
193                         }
194                 }
195 #endif
196         }
197
198         data = (char*) malloc (sizeof (char) * block_size);
199         uint64_t _read = 0;
200         double max_elapsed = 0;
201         double total_time = 0;
202         double var_m = 0;
203         double var_s = 0;
204         uint64_t cnt = 0;
205
206         while (1) {
207                 gint64 before;
208                 before = g_get_monotonic_time();
209
210 #ifdef HAVE_MMAP
211                 if (use_mmap) {
212                         for (n = 0; n < nfiles; ++n) {
213                                 if (_read + n + block_size > flen[n]) {
214                                         goto out;
215                                 }
216                                 memmove(data, &addr[n][_read], block_size);
217                         }
218                 }
219                 else
220 #endif
221                 {
222                         for (n = 0; n < nfiles; ++n) {
223                                 if (read (files[n], (char*) data, block_size) != block_size) {
224                                         goto out;
225                                 }
226                         }
227                 }
228
229                 _read += block_size;
230                 gint64 elapsed = g_get_monotonic_time() - before;
231                 double bandwidth = ((nfiles * block_size)/1048576.0) / (elapsed/1000000.0);
232
233                 if (!quiet) {
234                         printf ("# BW @ %lu %.3f seconds bandwidth %.4f MB/sec\n", (long unsigned int)_read, elapsed/1000000.0, bandwidth);
235                 }
236
237                 total_time += elapsed;
238
239                 ++cnt;
240                 if (max_elapsed == 0) {
241                         var_m = elapsed;
242                 } else {
243                         const double var_m1 = var_m;
244                         var_m = var_m + (elapsed - var_m) / (double)(cnt);
245                         var_s = var_s + (elapsed - var_m) * (elapsed - var_m1);
246                 }
247
248                 if (elapsed > max_elapsed) {
249                         max_elapsed = elapsed;
250                 }
251
252         }
253
254 out:
255         if (max_elapsed > 0 && total_time > 0) {
256                 double stddev = cnt > 1 ? sqrt(var_s / ((double)(cnt-1))) : 0;
257                 double bandwidth = ((nfiles * _read)/1048576.0) / (total_time/1000000.0);
258                 double min_throughput = ((nfiles * block_size)/1048576.0) / (max_elapsed/1000000.0);
259                 printf ("# Min: %.4f MB/sec Avg: %.4f MB/sec  || Max: %.3f sec \n", min_throughput, bandwidth, max_elapsed/1000000.0);
260                 printf ("# Max Track count: %d @ 48000SPS\n", (int) floor(1048576.0 * bandwidth / (4 * 48000.)));
261                 printf ("# Sus Track count: %d @ 48000SPS\n", (int) floor(1048576.0 * min_throughput / (4 * 48000.)));
262                 printf ("# seeks: %llu: bytes: %llu total_time: %f\n", cnt * nfiles, (nfiles * _read), total_time/1000000.0);
263                 printf ("%d %.4f %.4f %.4f %.5f\n", block_size, min_throughput, bandwidth, max_elapsed/1000000.0, stddev/1000000.0);
264         }
265
266         return 0;
267 }