1 /* g++ -o thread_readtest thread_readtest.cc `pkg-config --cflags --libs glibmm-2.4` -lm */
19 # include <sys/stat.h>
20 # include <sys/mman.h>
30 fprintf (stderr, "thread_readtest [ -b BLOCKSIZE ] [ -l FILELIMIT] [ -n NTHREADS ] [ -D ] [ -R ] [ -M ] filename-template\n");
33 Glib::Threads::Cond pool_run;
34 Glib::Threads::Cond pool_done;
35 Glib::Threads::Mutex pool_lock;
36 std::list<int> pool_work;
37 std::vector<Glib::Threads::Thread*> thread_pool;
39 bool thread_pool_lives = true;
48 thread_pool_work (ThreadData* td)
50 Glib::Threads::Mutex::Lock lm (pool_lock);
52 while (thread_pool_lives) {
53 pool_run.wait (pool_lock);
55 if (!thread_pool_lives) {
59 /* we're awake ... get some work */
61 while (!pool_work.empty()) {
63 int file_descriptor = pool_work.front ();
64 pool_work.pop_front ();
66 /* release the lock while we do work */
77 if ((nread = ::read (file_descriptor, td->data, td->block_size)) != td->block_size) {
78 if (nread != 0 && errno == EAGAIN) {
79 fprintf (stderr, "read requires retry\n");
83 fprintf (stderr, "thread %d has error = %s\n", td->id, strerror (errno));
88 /* reacquire lock so that we can check the status of
89 * things and possibly wake the master.
95 if (pool_work.empty()) {
96 /* work is finished, tell the master */
110 Glib::Threads::Mutex::Lock lm (pool_lock);
111 thread_pool_lives = false;
113 pool_run.broadcast ();
116 /* XXX wait for each thread to finish */
120 build_thread_pool (int nthreads, size_t block_size)
122 for (int n = 0; n < nthreads; ++n) {
123 ThreadData* td = new ThreadData;
124 td->data = (char*) malloc (sizeof (char) * block_size);
125 td->block_size = block_size;
128 thread_pool.push_back (Glib::Threads::Thread::create (sigc::bind (sigc::ptr_fun (thread_pool_work), td)));
133 run_thread_pool (int* files, int nfiles)
135 Glib::Threads::Mutex::Lock lm (pool_lock);
137 /* Queue up all the files */
138 for (int n = 0; n < nfiles; ++n) {
139 pool_work.push_back (files[n]);
144 /* wake everybody up */
145 pool_run.broadcast ();
147 /* wait for everyone to finish */
149 pool_done.wait (pool_lock);
159 main (int argc, char* argv[])
162 char optstring[] = "b:DRMl:q";
163 uint32_t block_size = 64 * 1024 * 4;
175 const struct option longopts[] = {
176 { "blocksize", 1, 0, 'b' },
177 { "direct", 0, 0, 'D' },
178 { "mmap", 0, 0, 'M' },
179 { "noreadahead", 0, 0, 'R' },
180 { "limit", 1, 0, 'l' },
181 { "nthreads", 16, 0, 'n' },
185 int option_index = 0;
187 char const * name_template = 0;
188 int flags = O_RDONLY;
194 if ((c = getopt_long (argc, argv, optstring, longopts, &option_index)) == -1) {
200 block_size = atoi (optarg);
203 max_files = atoi (optarg);
224 nthreads = atoi (optarg);
233 name_template = argv[optind];
240 char path[PATH_MAX+1];
242 snprintf (path, sizeof (path), name_template, n+1);
244 if (access (path, R_OK) != 0) {
250 if (max_files > 0 && n >= max_files) {
256 fprintf (stderr, "No matching files found for %s\n", name_template);
261 printf ("# Discovered %d files using %s\n", n, name_template);
265 files = (int *) malloc (sizeof (int) * nfiles);
269 printf ("# Using mmap().\n");
271 addr = malloc (sizeof (void*) * nfiles);
272 flen = (size_t*) malloc (sizeof (size_t) * nfiles);
276 for (n = 0; n < nfiles; ++n) {
278 char path[PATH_MAX+1];
281 snprintf (path, sizeof (path), name_template, n+1);
283 if ((fd = open (path, flags, 0644)) < 0) {
284 fprintf (stderr, "Could not open file #%d @ %s (%s)\n", n, path, strerror (errno));
290 /* Apple man pages say only that it returns "a value other than -1 on success",
291 which probably means zero, but you just can't be too careful with
294 if (fcntl (fd, F_NOCACHE, 1) == -1) {
295 fprintf (stderr, "Cannot set F_NOCACHE on file #%d\n", n);
300 if (fcntl (fd, F_RDAHEAD, 0) == -1) {
301 fprintf (stderr, "Cannot set F_READAHED on file #%d\n", n);
311 if (fstat (fd, & s)) {
312 fprintf (stderr, "Could not stat fd #%d @ %s\n", n, path);
315 if (s.st_size < block_size) {
316 fprintf (stderr, "file is shorter than blocksize #%d @ %s\n", n, path);
320 addr[n] = mmap (0, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
321 if (addr[n] == MAP_FAILED) {
322 fprintf (stderr, "Could not mmap file #%d @ %s (%s)\n", n, path, strerror (errno));
329 data = (char*) malloc (sizeof (char) * block_size);
331 double max_elapsed = 0;
332 double total_time = 0;
337 build_thread_pool (nthreads, block_size);
341 before = g_get_monotonic_time();
343 if (run_thread_pool (files, nfiles)) {
344 fprintf (stderr, "thread pool error\n");
349 gint64 elapsed = g_get_monotonic_time() - before;
350 double bandwidth = ((nfiles * block_size)/1048576.0) / (elapsed/1000000.0);
353 printf ("# BW @ %lu %.3f seconds bandwidth %.4f MB/sec\n", (long unsigned int)_read, elapsed/1000000.0, bandwidth);
356 total_time += elapsed;
359 if (max_elapsed == 0) {
362 const double var_m1 = var_m;
363 var_m = var_m + (elapsed - var_m) / (double)(cnt);
364 var_s = var_s + (elapsed - var_m) * (elapsed - var_m1);
367 if (elapsed > max_elapsed) {
368 max_elapsed = elapsed;
374 if (max_elapsed > 0 && total_time > 0) {
375 double stddev = cnt > 1 ? sqrt(var_s / ((double)(cnt-1))) : 0;
376 double bandwidth = ((nfiles * _read)/1048576.0) / (total_time/1000000.0);
377 double min_throughput = ((nfiles * block_size)/1048576.0) / (max_elapsed/1000000.0);
378 printf ("# Min: %.4f MB/sec Avg: %.4f MB/sec || Max: %.3f sec \n", min_throughput, bandwidth, max_elapsed/1000000.0);
379 printf ("# Max Track count: %d @ 48000SPS\n", (int) floor(1048576.0 * bandwidth / (4 * 48000.)));
380 printf ("# Sus Track count: %d @ 48000SPS\n", (int) floor(1048576.0 * min_throughput / (4 * 48000.)));
381 printf ("# seeks: %llu: bytes: %llu total_time: %f\n", cnt * nfiles, (nfiles * _read), total_time/1000000.0);
382 printf ("%d %.4f %.4f %.4f %.5f\n", block_size, min_throughput, bandwidth, max_elapsed/1000000.0, stddev/1000000.0);