summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--turbowc.c64
2 files changed, 45 insertions, 21 deletions
diff --git a/Makefile b/Makefile
index 93a7aad..39b9324 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
CC ?= cc
-CFLAGS += -O3 -march=native
+CFLAGS += -O3 -march=native -Wall
CPPFLAGS +=
LDFLAGS += -pthread
diff --git a/turbowc.c b/turbowc.c
index c556ed5..16512f6 100644
--- a/turbowc.c
+++ b/turbowc.c
@@ -11,6 +11,15 @@
#include <sys/mman.h>
#include <time.h>
+typedef uint64_t ntime_t;
+
+ntime_t nclock(void)
+{
+ struct timespec ts;
+ clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
+ return (ntime_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
+}
+
static size_t count_newlines(void *data, size_t size)
{
size_t i, total = 0;
@@ -103,15 +112,18 @@ static void *thread_f(void *arg)
return NULL;
}
+struct {
+ size_t size, lines;
+ ntime_t time;
+} totals;
+
static int turbowc(const char *path, size_t num_threads)
{
- int ret = 1, opt, fd;
+ int ret = 1, fd;
struct stat stat;
void *map;
size_t i;
- pthread_t *threads;
- struct timespec t0, t1;
- double delta;
+ pthread_t *threads = NULL;
fd = open(path, O_RDONLY);
if (fd == -1) {
@@ -133,6 +145,7 @@ static int turbowc(const char *path, size_t num_threads)
data_pool.cursor = map;
data_pool.left = stat.st_size;
+ data_pool.total = 0;
if (num_threads) {
threads = calloc(num_threads, sizeof(pthread_t));
@@ -142,8 +155,6 @@ static int turbowc(const char *path, size_t num_threads)
}
}
- clock_gettime(CLOCK_MONOTONIC, &t0);
-
for (i = 0; i < num_threads; i++) {
int rv;
@@ -164,20 +175,10 @@ static int turbowc(const char *path, size_t num_threads)
pthread_join(threads[i], NULL);
data_pool.total += count_newlines(data_pool.cursor, data_pool.left);
- clock_gettime(CLOCK_MONOTONIC, &t1);
-
printf("%zu %s\n", data_pool.total, path);
- delta = (t1.tv_sec - t0.tv_sec) +
- ((double)t1.tv_nsec - t0.tv_nsec) * 1.0e-9;
-
- fprintf(stderr, " Total size: %zu\n", stat.st_size);
- fprintf(stderr, " Total time: %f s\n", delta);
- fprintf(stderr, "Time / char: %f ns\n", delta / stat.st_size * 1.0e+9);
- fprintf(stderr, " Throughput: %f GB/s\n", stat.st_size / delta * 1.0e-9);
- fprintf(stderr, "Total lines: %zu\n", data_pool.total);
- fprintf(stderr, " Avg. line: %f char(s)\n", (double)stat.st_size / data_pool.total);
- fprintf(stderr, "Time / line: %f ns\n", delta / data_pool.total * 1.0e+9);
+ totals.size += stat.st_size;
+ totals.lines += data_pool.total;
ret = 0;
@@ -197,8 +198,10 @@ int main(int argc, char **argv)
{
int opt;
size_t num_threads = 1;
+ _Bool print_stats = 0;
+ ntime_t t0;
- while ((opt = getopt(argc, argv, "vt:")) != -1) {
+ while ((opt = getopt(argc, argv, "vt:s")) != -1) {
switch (opt) {
case 'v':
printf("turbowc, with mmap, SSE2 and multithreading\n");
@@ -208,9 +211,13 @@ int main(int argc, char **argv)
num_threads = strtoull(optarg, NULL, 10);
break;
+ case 's':
+ print_stats = 1;
+ break;
+
default:
print_usage:
- fprintf(stderr, "usage: turbowc [-v] [-t THREADS] [FILE]\n");
+ fprintf(stderr, "usage: turbowc [-v] [-s] [-t THREADS] [FILE]\n");
return 1;
}
}
@@ -218,9 +225,26 @@ int main(int argc, char **argv)
if (optind >= argc)
goto print_usage;
+ t0 = nclock();
+
for (; optind < argc; optind++ )
if (turbowc(argv[optind], num_threads))
return 1;
+ totals.time = nclock() - t0;
+
+ if (!print_stats)
+ goto skip_stats;
+ printf("Lines\t%zu\n", totals.lines);
+ printf("Size\t%zu\t%f\t-\n",
+ totals.size,
+ (double)totals.size / totals.lines);
+ printf("Time\t%f s\t%f ns\t%f ps\n",
+ totals.time * 1.0e-9,
+ (double)totals.time / totals.lines,
+ totals.time * 1.0e+3 / totals.size);
+ printf("Thput\t%f GB/s\n", (double)totals.size / totals.time);
+
+skip_stats:
return 0;
}