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