1 /* gcc -o readtest readtest.c `pkg-config --cflags --libs glib-2.0` -lm */
18 # include <sys/stat.h>
19 # include <sys/mman.h>
29 fprintf (stderr, "readtest [ -b BLOCKSIZE ] [-l FILELIMIT] [ -D ] [ -R ] [ -M ] filename-template\n");
33 main (int argc, char* argv[])
36 char optstring[] = "b:DRMl:q";
37 uint32_t block_size = 64 * 1024 * 4;
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' },
59 char const * name_template = 0;
66 if ((c = getopt_long (argc, argv, optstring, longopts, &option_index)) == -1) {
72 block_size = atoi (optarg);
75 max_files = atoi (optarg);
102 name_template = argv[optind];
109 char path[PATH_MAX+1];
111 snprintf (path, sizeof (path), name_template, n+1);
113 if (access (path, R_OK) != 0) {
119 if (max_files > 0 && n >= max_files) {
125 fprintf (stderr, "No matching files found for %s\n", name_template);
130 printf ("# Discovered %d files using %s\n", n, name_template);
134 files = (int *) malloc (sizeof (int) * nfiles);
138 printf ("# Using mmap().\n");
140 addr = malloc (sizeof (void*) * nfiles);
141 flen = (size_t*) malloc (sizeof (size_t) * nfiles);
145 for (n = 0; n < nfiles; ++n) {
147 char path[PATH_MAX+1];
150 snprintf (path, sizeof (path), name_template, n+1);
152 if ((fd = open (path, flags, 0644)) < 0) {
153 fprintf (stderr, "Could not open file #%d @ %s (%s)\n", n, path, strerror (errno));
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
163 if (fcntl (fd, F_NOCACHE, 1) == -1) {
164 fprintf (stderr, "Cannot set F_NOCACHE on file #%d\n", n);
169 if (fcntl (fd, F_RDAHEAD, 0) == -1) {
170 fprintf (stderr, "Cannot set F_READAHED on file #%d\n", n);
180 if (fstat (fd, & s)) {
181 fprintf (stderr, "Could not stat fd #%d @ %s\n", n, path);
184 if (s.st_size < block_size) {
185 fprintf (stderr, "file is shorter than blocksize #%d @ %s\n", n, path);
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));
198 data = (char*) malloc (sizeof (char) * block_size);
200 double max_elapsed = 0;
201 double total_time = 0;
208 before = g_get_monotonic_time();
212 for (n = 0; n < nfiles; ++n) {
213 if (_read + n + block_size > flen[n]) {
216 memmove(data, &addr[n][_read], block_size);
222 for (n = 0; n < nfiles; ++n) {
223 if (read (files[n], (char*) data, block_size) != block_size) {
230 gint64 elapsed = g_get_monotonic_time() - before;
231 double bandwidth = ((nfiles * block_size)/1048576.0) / (elapsed/1000000.0);
234 printf ("# BW @ %lu %.3f seconds bandwidth %.4f MB/sec\n", (long unsigned int)_read, elapsed/1000000.0, bandwidth);
237 total_time += elapsed;
240 if (max_elapsed == 0) {
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);
248 if (elapsed > max_elapsed) {
249 max_elapsed = elapsed;
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);