1 /* gcc -o readtest readtest.c `pkg-config --cflags --libs glib-2.0` -lm */
20 fprintf (stderr, "readtest [ -b BLOCKSIZE ] [-l FILELIMIT] [ -D ] filename-template\n");
24 main (int argc, char* argv[])
27 char optstring[] = "b:Dl:q";
28 uint32_t block_size = 64 * 1024 * 4;
33 const struct option longopts[] = {
34 { "blocksize", 1, 0, 'b' },
35 { "direct", 0, 0, 'D' },
36 { "limit", 1, 0, 'l' },
42 char const * name_template = 0;
49 if ((c = getopt_long (argc, argv, optstring, longopts, &option_index)) == -1) {
55 block_size = atoi (optarg);
58 max_files = atoi (optarg);
75 name_template = argv[optind];
82 char path[PATH_MAX+1];
84 snprintf (path, sizeof (path), name_template, n+1);
86 if (access (path, R_OK) != 0) {
92 if (max_files > 0 && n >= max_files) {
98 fprintf (stderr, "No matching files found for %s\n", name_template);
103 printf ("# Discovered %d files using %s\n", n, name_template);
107 files = (int *) malloc (sizeof (int) * nfiles);
109 for (n = 0; n < nfiles; ++n) {
111 char path[PATH_MAX+1];
114 snprintf (path, sizeof (path), name_template, n+1);
116 if ((fd = open (path, flags, 0644)) < 0) {
117 fprintf (stderr, "Could not open file #%d @ %s (%s)\n", n, path, strerror (errno));
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
127 if (fcntl (fd, F_NOCACHE, 1) == -1) {
128 fprintf (stderr, "Cannot set F_NOCACHE on file #%d\n", n);
136 data = (char*) malloc (sizeof (char) * block_size);
138 double max_elapsed = 0;
139 double total_time = 0;
146 before = g_get_monotonic_time();
148 for (n = 0; n < nfiles; ++n) {
150 if (read (files[n], (char*) data, block_size) != block_size) {
156 gint64 elapsed = g_get_monotonic_time() - before;
157 double bandwidth = ((nfiles * block_size)/1048576.0) / (elapsed/1000000.0);
160 printf ("# BW @ %lu %.3f seconds bandwidth %.4f MB/sec\n", (long unsigned int)_read, elapsed/1000000.0, bandwidth);
163 total_time += elapsed;
166 if (max_elapsed == 0) {
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);
174 if (elapsed > max_elapsed) {
175 max_elapsed = elapsed;
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);