2015-01-20 16:17:53 -05:00
|
|
|
/* gcc -o readtest readtest.c `pkg-config --cflags --libs glib-2.0` -lm */
|
2015-01-19 16:16:51 -05:00
|
|
|
|
2015-03-12 08:15:12 -04:00
|
|
|
#ifndef _WIN32
|
|
|
|
# define HAVE_MMAP
|
|
|
|
#endif
|
|
|
|
|
2015-01-19 16:16:51 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <fcntl.h>
|
2015-01-20 16:17:53 -05:00
|
|
|
#include <math.h>
|
2015-01-19 16:16:51 -05:00
|
|
|
|
2015-03-12 08:15:12 -04:00
|
|
|
#ifdef HAVE_MMAP
|
|
|
|
# include <sys/stat.h>
|
|
|
|
# include <sys/mman.h>
|
|
|
|
#endif
|
|
|
|
|
2015-01-19 16:16:51 -05:00
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
char* data = 0;
|
|
|
|
|
|
|
|
void
|
|
|
|
usage ()
|
|
|
|
{
|
2015-03-12 08:15:12 -04:00
|
|
|
fprintf (stderr, "readtest [ -b BLOCKSIZE ] [-l FILELIMIT] [ -D ] [ -R ] [ -M ] filename-template\n");
|
2015-01-19 16:16:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char* argv[])
|
|
|
|
{
|
2015-01-19 18:57:22 -05:00
|
|
|
int* files;
|
2015-03-12 08:15:12 -04:00
|
|
|
char optstring[] = "b:DRMl:q";
|
2015-01-19 16:16:51 -05:00
|
|
|
uint32_t block_size = 64 * 1024 * 4;
|
2015-01-20 12:29:35 -05:00
|
|
|
int max_files = -1;
|
2015-01-19 18:55:11 -05:00
|
|
|
#ifdef __APPLE__
|
2015-01-20 16:17:53 -05:00
|
|
|
int direct = 0;
|
2015-03-12 08:15:12 -04:00
|
|
|
int noreadahead = 0;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_MMAP
|
|
|
|
int use_mmap = 0;
|
|
|
|
void **addr;
|
|
|
|
size_t *flen;
|
2015-01-19 18:55:11 -05:00
|
|
|
#endif
|
2015-01-19 16:16:51 -05:00
|
|
|
const struct option longopts[] = {
|
|
|
|
{ "blocksize", 1, 0, 'b' },
|
|
|
|
{ "direct", 0, 0, 'D' },
|
2015-03-12 08:15:12 -04:00
|
|
|
{ "mmap", 0, 0, 'M' },
|
|
|
|
{ "noreadahead", 0, 0, 'R' },
|
2015-01-20 12:29:35 -05:00
|
|
|
{ "limit", 1, 0, 'l' },
|
2015-01-19 16:16:51 -05:00
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
int option_index = 0;
|
|
|
|
int c = 0;
|
|
|
|
char const * name_template = 0;
|
2015-01-19 18:57:22 -05:00
|
|
|
int flags = O_RDONLY;
|
|
|
|
int n = 0;
|
|
|
|
int nfiles = 0;
|
2015-01-20 16:17:53 -05:00
|
|
|
int quiet = 0;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-01-19 16:16:51 -05:00
|
|
|
while (1) {
|
|
|
|
if ((c = getopt_long (argc, argv, optstring, longopts, &option_index)) == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (c) {
|
2015-01-20 16:17:53 -05:00
|
|
|
case 'b':
|
|
|
|
block_size = atoi (optarg);
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
max_files = atoi (optarg);
|
|
|
|
break;
|
|
|
|
case 'D':
|
2015-01-19 18:55:11 -05:00
|
|
|
#ifdef __APPLE__
|
2015-01-20 16:17:53 -05:00
|
|
|
direct = 1;
|
2015-03-12 08:15:12 -04:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
#ifdef HAVE_MMAP
|
|
|
|
use_mmap = 1;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
#ifdef __APPLE__
|
|
|
|
noreadahead = 1;
|
2015-01-19 18:55:11 -05:00
|
|
|
#endif
|
2015-01-20 16:17:53 -05:00
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
quiet = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage ();
|
|
|
|
return 0;
|
2015-01-19 16:16:51 -05:00
|
|
|
}
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-01-19 16:16:51 -05:00
|
|
|
if (optind < argc) {
|
|
|
|
name_template = argv[optind];
|
|
|
|
} else {
|
|
|
|
usage ();
|
|
|
|
return 1;
|
|
|
|
}
|
2015-01-19 18:57:22 -05:00
|
|
|
|
|
|
|
while (1) {
|
2015-01-19 16:16:51 -05:00
|
|
|
char path[PATH_MAX+1];
|
|
|
|
|
|
|
|
snprintf (path, sizeof (path), name_template, n+1);
|
|
|
|
|
|
|
|
if (access (path, R_OK) != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-01-19 18:57:22 -05:00
|
|
|
++n;
|
2015-01-20 12:29:35 -05:00
|
|
|
|
|
|
|
if (max_files > 0 && n >= max_files) {
|
|
|
|
break;
|
|
|
|
}
|
2015-01-19 18:57:22 -05:00
|
|
|
}
|
2015-01-19 16:16:51 -05:00
|
|
|
|
2015-01-19 18:57:22 -05:00
|
|
|
if (n == 0) {
|
|
|
|
fprintf (stderr, "No matching files found for %s\n", name_template);
|
|
|
|
return 1;
|
|
|
|
}
|
2015-01-19 16:16:51 -05:00
|
|
|
|
2015-01-20 16:17:53 -05:00
|
|
|
if (!quiet) {
|
2015-01-28 08:35:17 -05:00
|
|
|
printf ("# Discovered %d files using %s\n", n, name_template);
|
2015-01-20 16:17:53 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-01-19 18:57:22 -05:00
|
|
|
nfiles = n;
|
|
|
|
files = (int *) malloc (sizeof (int) * nfiles);
|
2015-03-12 08:15:12 -04:00
|
|
|
#ifdef HAVE_MMAP
|
|
|
|
if (use_mmap) {
|
|
|
|
if (!quiet) {
|
|
|
|
printf ("# Using mmap().\n");
|
|
|
|
}
|
|
|
|
addr = malloc (sizeof (void*) * nfiles);
|
|
|
|
flen = (size_t*) malloc (sizeof (size_t) * nfiles);
|
|
|
|
}
|
|
|
|
#endif
|
2015-01-19 18:57:22 -05:00
|
|
|
|
|
|
|
for (n = 0; n < nfiles; ++n) {
|
2015-01-19 16:16:51 -05:00
|
|
|
|
|
|
|
char path[PATH_MAX+1];
|
2015-01-19 18:57:22 -05:00
|
|
|
int fd;
|
2015-01-19 16:16:51 -05:00
|
|
|
|
|
|
|
snprintf (path, sizeof (path), name_template, n+1);
|
|
|
|
|
2015-01-19 18:57:22 -05:00
|
|
|
if ((fd = open (path, flags, 0644)) < 0) {
|
2015-01-19 16:16:51 -05:00
|
|
|
fprintf (stderr, "Could not open file #%d @ %s (%s)\n", n, path, strerror (errno));
|
2015-01-19 18:57:22 -05:00
|
|
|
return 1;
|
|
|
|
}
|
2015-01-19 16:16:51 -05:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
2015-01-19 18:57:22 -05:00
|
|
|
if (direct) {
|
|
|
|
/* Apple man pages say only that it returns "a value other than -1 on success",
|
|
|
|
which probably means zero, but you just can't be too careful with
|
|
|
|
those guys.
|
|
|
|
*/
|
|
|
|
if (fcntl (fd, F_NOCACHE, 1) == -1) {
|
|
|
|
fprintf (stderr, "Cannot set F_NOCACHE on file #%d\n", n);
|
|
|
|
}
|
|
|
|
}
|
2015-03-12 08:15:12 -04:00
|
|
|
|
|
|
|
if (noreadahead) {
|
|
|
|
if (fcntl (fd, F_RDAHEAD, 0) == -1) {
|
|
|
|
fprintf (stderr, "Cannot set F_READAHED on file #%d\n", n);
|
|
|
|
}
|
|
|
|
}
|
2015-01-19 16:16:51 -05:00
|
|
|
#endif
|
|
|
|
|
2015-01-19 18:57:22 -05:00
|
|
|
files[n] = fd;
|
2015-03-12 08:15:12 -04:00
|
|
|
|
|
|
|
#ifdef HAVE_MMAP
|
|
|
|
if (use_mmap) {
|
|
|
|
struct stat s;
|
|
|
|
if (fstat (fd, & s)) {
|
|
|
|
fprintf (stderr, "Could not stat fd #%d @ %s\n", n, path);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (s.st_size < block_size) {
|
|
|
|
fprintf (stderr, "file is shorter than blocksize #%d @ %s\n", n, path);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
flen[n] = s.st_size;
|
|
|
|
addr[n] = mmap (0, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
if (addr[n] == MAP_FAILED) {
|
|
|
|
fprintf (stderr, "Could not mmap file #%d @ %s (%s)\n", n, path, strerror (errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2015-01-19 16:16:51 -05:00
|
|
|
}
|
|
|
|
|
2015-01-19 16:18:48 -05:00
|
|
|
data = (char*) malloc (sizeof (char) * block_size);
|
2015-01-19 18:55:11 -05:00
|
|
|
uint64_t _read = 0;
|
2015-01-20 12:29:35 -05:00
|
|
|
double max_elapsed = 0;
|
2015-01-19 19:27:47 -05:00
|
|
|
double total_time = 0;
|
2015-01-28 11:29:33 -05:00
|
|
|
double var_m = 0;
|
|
|
|
double var_s = 0;
|
|
|
|
uint64_t cnt = 0;
|
2015-01-19 18:57:22 -05:00
|
|
|
|
2015-01-20 16:17:53 -05:00
|
|
|
while (1) {
|
2015-01-19 16:16:51 -05:00
|
|
|
gint64 before;
|
|
|
|
before = g_get_monotonic_time();
|
|
|
|
|
2015-03-12 08:15:12 -04:00
|
|
|
#ifdef HAVE_MMAP
|
|
|
|
if (use_mmap) {
|
|
|
|
for (n = 0; n < nfiles; ++n) {
|
|
|
|
if (_read + n + block_size > flen[n]) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
memmove(data, &addr[n][_read], block_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
for (n = 0; n < nfiles; ++n) {
|
|
|
|
if (read (files[n], (char*) data, block_size) != block_size) {
|
|
|
|
goto out;
|
|
|
|
}
|
2015-01-19 18:57:22 -05:00
|
|
|
}
|
2015-01-19 16:16:51 -05:00
|
|
|
}
|
|
|
|
|
2015-01-19 18:55:11 -05:00
|
|
|
_read += block_size;
|
2015-01-19 16:16:51 -05:00
|
|
|
gint64 elapsed = g_get_monotonic_time() - before;
|
2015-01-19 18:57:22 -05:00
|
|
|
double bandwidth = ((nfiles * block_size)/1048576.0) / (elapsed/1000000.0);
|
|
|
|
|
2015-01-20 16:17:53 -05:00
|
|
|
if (!quiet) {
|
2015-01-28 08:35:17 -05:00
|
|
|
printf ("# BW @ %lu %.3f seconds bandwidth %.4f MB/sec\n", (long unsigned int)_read, elapsed/1000000.0, bandwidth);
|
2015-01-20 16:17:53 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-01-19 19:27:47 -05:00
|
|
|
total_time += elapsed;
|
2015-01-20 12:29:35 -05:00
|
|
|
|
2015-01-28 11:29:33 -05:00
|
|
|
++cnt;
|
|
|
|
if (max_elapsed == 0) {
|
|
|
|
var_m = elapsed;
|
|
|
|
} else {
|
|
|
|
const double var_m1 = var_m;
|
|
|
|
var_m = var_m + (elapsed - var_m) / (double)(cnt);
|
|
|
|
var_s = var_s + (elapsed - var_m) * (elapsed - var_m1);
|
|
|
|
}
|
|
|
|
|
2015-01-20 12:29:35 -05:00
|
|
|
if (elapsed > max_elapsed) {
|
|
|
|
max_elapsed = elapsed;
|
2015-01-19 19:27:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2015-01-20 12:29:35 -05:00
|
|
|
if (max_elapsed > 0 && total_time > 0) {
|
2015-01-28 11:29:33 -05:00
|
|
|
double stddev = cnt > 1 ? sqrt(var_s / ((double)(cnt-1))) : 0;
|
2015-01-19 19:27:47 -05:00
|
|
|
double bandwidth = ((nfiles * _read)/1048576.0) / (total_time/1000000.0);
|
2015-01-20 12:29:35 -05:00
|
|
|
double min_throughput = ((nfiles * block_size)/1048576.0) / (max_elapsed/1000000.0);
|
2015-01-28 08:35:17 -05:00
|
|
|
printf ("# Min: %.4f MB/sec Avg: %.4f MB/sec || Max: %.3f sec \n", min_throughput, bandwidth, max_elapsed/1000000.0);
|
|
|
|
printf ("# Max Track count: %d @ 48000SPS\n", (int) floor(1048576.0 * bandwidth / (4 * 48000.)));
|
|
|
|
printf ("# Sus Track count: %d @ 48000SPS\n", (int) floor(1048576.0 * min_throughput / (4 * 48000.)));
|
2015-03-12 11:06:02 -04:00
|
|
|
printf ("# seeks: %llu: bytes: %llu total_time: %f\n", cnt * nfiles, (nfiles * _read), total_time/1000000.0);
|
2015-01-28 11:29:33 -05:00
|
|
|
printf ("%d %.4f %.4f %.4f %.5f\n", block_size, min_throughput, bandwidth, max_elapsed/1000000.0, stddev/1000000.0);
|
2015-01-19 16:16:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|