1 /* gcc -o readtest readtest.cc `pkg-config --cflags --libs glib-2.0` */
18 read_one (int fd, ssize_t sz)
27 fprintf (stderr, "readtest [ -b BLOCKSIZE ] [ -s ] [ -D ] filename-template\n");
31 main (int argc, char* argv[])
34 char optstring[] = "b:D";
35 uint32_t block_size = 64 * 1024 * 4;
39 const struct option longopts[] = {
40 { "blocksize", 1, 0, 'b' },
41 { "direct", 0, 0, 'D' },
47 char const * name_template = 0;
53 if ((c = getopt_long (argc, argv, optstring, longopts, &option_index)) == -1) {
59 block_size = atoi (optarg);
73 name_template = argv[optind];
80 char path[PATH_MAX+1];
82 snprintf (path, sizeof (path), name_template, n+1);
84 if (access (path, R_OK) != 0) {
92 fprintf (stderr, "No matching files found for %s\n", name_template);
96 printf ("Discovered %d files using %s\n", n, name_template);
99 files = (int *) malloc (sizeof (int) * nfiles);
101 for (n = 0; n < nfiles; ++n) {
103 char path[PATH_MAX+1];
106 snprintf (path, sizeof (path), name_template, n+1);
108 if ((fd = open (path, flags, 0644)) < 0) {
109 fprintf (stderr, "Could not open file #%d @ %s (%s)\n", n, path, strerror (errno));
115 /* Apple man pages say only that it returns "a value other than -1 on success",
116 which probably means zero, but you just can't be too careful with
119 if (fcntl (fd, F_NOCACHE, 1) == -1) {
120 fprintf (stderr, "Cannot set F_NOCACHE on file #%d\n", n);
128 data = (char*) malloc (sizeof (char) * block_size);
133 before = g_get_monotonic_time();
135 for (n = 0; n < nfiles; ++n) {
137 if (read (files[n], (char*) data, block_size) != block_size) {
138 fprintf (stderr, "read failed on file %d (%s)\n", n, strerror (errno));
144 gint64 elapsed = g_get_monotonic_time() - before;
145 double bandwidth = ((nfiles * block_size)/1048576.0) / (elapsed/1000000.0);
147 printf ("BW @ %Lu %.3f seconds bandwidth %.4f MB/sec\n", _read, elapsed/1000000.0, bandwidth);