1 /* gcc -o readtest readtest.cc `pkg-config --cflags --libs glib-2.0` */
17 read_one (int fd, ssize_t sz)
26 fprintf (stderr, "readtest [ -b BLOCKSIZE ] [ -s ] [ -D ] filename-template\n");
30 main (int argc, char* argv[])
33 char optstring[] = "b:D";
34 uint32_t block_size = 64 * 1024 * 4;
36 const struct option longopts[] = {
37 { "blocksize", 1, 0, 'b' },
38 { "direct", 0, 0, 'D' },
44 char const * name_template = 0;
50 if ((c = getopt_long (argc, argv, optstring, longopts, &option_index)) == -1) {
56 block_size = atoi (optarg);
68 name_template = argv[optind];
75 char path[PATH_MAX+1];
77 snprintf (path, sizeof (path), name_template, n+1);
79 if (access (path, R_OK) != 0) {
87 fprintf (stderr, "No matching files found for %s\n", name_template);
91 printf ("Discovered %d files using %s\n", n, name_template);
94 files = (int *) malloc (sizeof (int) * nfiles);
96 for (n = 0; n < nfiles; ++n) {
98 char path[PATH_MAX+1];
101 snprintf (path, sizeof (path), name_template, n+1);
103 if ((fd = open (path, flags, 0644)) < 0) {
104 fprintf (stderr, "Could not open file #%d @ %s (%s)\n", n, path, strerror (errno));
110 /* Apple man pages say only that it returns "a value other than -1 on success",
111 which probably means zero, but you just can't be too careful with
114 if (fcntl (fd, F_NOCACHE, 1) == -1) {
115 fprintf (stderr, "Cannot set F_NOCACHE on file #%d\n", n);
123 data = (char*) malloc (sizeof (char) * block_size);
128 before = g_get_monotonic_time();
130 for (n = 0; n < nfiles; ++n) {
132 if (::read (files[n], (char*) data, block_size) != block_size) {
133 fprintf (stderr, "read failed on file %d (%s)\n", n, strerror (errno));
139 gint64 elapsed = g_get_monotonic_time() - before;
140 double bandwidth = ((nfiles * block_size)/1048576.0) / (elapsed/1000000.0);
142 printf ("BW @ %Lu %.3f seconds bandwidth %.4f MB/sec\n", read, elapsed/1000000.0, bandwidth);