rename readtest (.c not .cc)
[ardour.git] / tools / readtest.c
1 /* gcc -o readtest readtest.cc `pkg-config --cflags --libs glib-2.0` */
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
12 #include <glib.h>
13
14 char* data = 0;
15
16 int
17 read_one (int fd, ssize_t sz)
18 {
19
20         return 0;
21 }
22
23 void
24 usage ()
25 {
26         fprintf (stderr, "readtest [ -b BLOCKSIZE ] [ -s ] [ -D ] filename-template\n");
27 }
28
29 int
30 main (int argc, char* argv[])
31 {
32         int* files;
33         char optstring[] = "b:D";
34         uint32_t block_size = 64 * 1024 * 4;
35         bool direct = false;
36         const struct option longopts[] = {
37                 { "blocksize", 1, 0, 'b' },
38                 { "direct", 0, 0, 'D' },
39                 { 0, 0, 0, 0 }
40         };
41
42         int option_index = 0;
43         int c = 0;
44         char const * name_template = 0;
45         int flags = O_RDONLY;
46         int n = 0;
47         int nfiles = 0;
48
49         while (1) {
50                 if ((c = getopt_long (argc, argv, optstring, longopts, &option_index)) == -1) {
51                         break;
52                 }
53
54                 switch (c) {
55                 case 'b':
56                         block_size = atoi (optarg);
57                         break;
58                 case 'D':
59                         direct = true;
60                         break;
61                 default:
62                         usage ();
63                         return 0;
64                 }
65         }
66
67         if (optind < argc) {
68                 name_template = argv[optind];
69         } else {
70                 usage ();
71                 return 1;
72         }
73         
74         while (1) {
75                 char path[PATH_MAX+1];
76
77                 snprintf (path, sizeof (path), name_template, n+1);
78
79                 if (access (path, R_OK) != 0) {
80                         break;
81                 }
82
83                 ++n;
84         }
85
86         if (n == 0) {
87                 fprintf (stderr, "No matching files found for %s\n", name_template);
88                 return 1;
89         }
90
91         printf ("Discovered %d files using %s\n", n, name_template);
92         
93         nfiles = n;
94         files = (int *) malloc (sizeof (int) * nfiles);
95
96         for (n = 0; n < nfiles; ++n) {
97
98                 char path[PATH_MAX+1];
99                 int fd;
100
101                 snprintf (path, sizeof (path), name_template, n+1);
102
103                 if ((fd = open (path, flags, 0644)) < 0) {
104                         fprintf (stderr, "Could not open file #%d @ %s (%s)\n", n, path, strerror (errno));
105                         return 1;
106                 }
107
108 #ifdef __APPLE__
109                 if (direct) {
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
112                            those guys.
113                         */
114                         if (fcntl (fd, F_NOCACHE, 1) == -1) {
115                                 fprintf (stderr, "Cannot set F_NOCACHE on file #%d\n", n);
116                         }
117                 }
118 #endif
119
120                 files[n] = fd;
121         }
122
123         data = (char*) malloc (sizeof (char) * block_size);
124         uint64_t read = 0;
125         
126         while (true) {
127                 gint64 before;
128                 before = g_get_monotonic_time();
129
130                 for (n = 0; n < nfiles; ++n) {
131
132                         if (::read (files[n], (char*) data, block_size) != block_size) {
133                                 fprintf (stderr, "read failed on file %d (%s)\n", n, strerror (errno));
134                                 return -1;
135                         }
136                 }
137
138                 read += block_size;
139                 gint64 elapsed = g_get_monotonic_time() - before;
140                 double bandwidth = ((nfiles * block_size)/1048576.0) / (elapsed/1000000.0);
141                 
142                 printf ("BW @ %Lu %.3f seconds bandwidth %.4f MB/sec\n", read, elapsed/1000000.0, bandwidth);
143         }
144
145         return 0;
146 }
147
148