Fix unnecessary const violation.
[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         double var_m = 0;
147         double var_s = 0;
148         uint64_t cnt = 0;
149
150         while (1) {
151                 gint64 before;
152                 before = g_get_monotonic_time();
153
154                 for (n = 0; n < nfiles; ++n) {
155
156                         if (read (files[n], (char*) data, block_size) != block_size) {
157                                 goto out;
158                         }
159                 }
160
161                 _read += block_size;
162                 gint64 elapsed = g_get_monotonic_time() - before;
163                 double bandwidth = ((nfiles * block_size)/1048576.0) / (elapsed/1000000.0);
164
165                 if (!quiet) {
166                         printf ("# BW @ %lu %.3f seconds bandwidth %.4f MB/sec\n", (long unsigned int)_read, elapsed/1000000.0, bandwidth);
167                 }
168                 
169                 total_time += elapsed;
170
171                 ++cnt;
172                 if (max_elapsed == 0) {
173                         var_m = elapsed;
174                 } else {
175                         const double var_m1 = var_m;
176                         var_m = var_m + (elapsed - var_m) / (double)(cnt);
177                         var_s = var_s + (elapsed - var_m) * (elapsed - var_m1);
178                 }
179
180                 if (elapsed > max_elapsed) {
181                         max_elapsed = elapsed;
182                 }
183
184         }
185
186 out:
187         if (max_elapsed > 0 && total_time > 0) {
188                 double stddev = cnt > 1 ? sqrt(var_s / ((double)(cnt-1))) : 0;
189                 double bandwidth = ((nfiles * _read)/1048576.0) / (total_time/1000000.0);
190                 double min_throughput = ((nfiles * block_size)/1048576.0) / (max_elapsed/1000000.0);
191                 printf ("# Min: %.4f MB/sec Avg: %.4f MB/sec  || Max: %.3f sec \n", min_throughput, bandwidth, max_elapsed/1000000.0);
192                 printf ("# Max Track count: %d @ 48000SPS\n", (int) floor(1048576.0 * bandwidth / (4 * 48000.)));
193                 printf ("# Sus Track count: %d @ 48000SPS\n", (int) floor(1048576.0 * min_throughput / (4 * 48000.)));
194                 printf ("%d %.4f %.4f %.4f %.5f\n", block_size, min_throughput, bandwidth, max_elapsed/1000000.0, stddev/1000000.0);
195         }
196
197         return 0;
198 }