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