From 3bb12208fb873ad02e28802c6b4f8dcc9d2311b9 Mon Sep 17 00:00:00 2001 From: Florian Fischer <florian.fischer@muhq.space> Date: Mon, 2 Aug 2021 11:37:02 +0200 Subject: [PATCH] use uint64_t for cycles and average calculation --- Makefile | 4 +++- bench.c | 10 +++++++--- stopwatch.c | 4 ++-- stopwatch.h | 8 ++++---- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index bbc1e08..a9ced29 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ LDFLAGS := -luring -pthread -lrt CFLAGS := -Werror -Wall -g -O3 # CFLAGS := -Werror -Wall -g -O0 -.PHONY: all clean eval docker-eval +.PHONY: all clean eval docker-eval check eval: all @for syscall in $(SYSCALLS); do echo -n "$$syscall " ; ./bench-$$syscall; done @@ -28,6 +28,8 @@ $(foreach syscall,$(SYSCALLS),$(eval $(call generateTargets,$(syscall)))) clean: rm -f $(OBJ) +check: tidy check-format + tidy: clang-tidy *.c diff --git a/bench.c b/bench.c index 7a3c941..b7439a6 100644 --- a/bench.c +++ b/bench.c @@ -42,8 +42,12 @@ int main() { errx(EXIT_FAILURE, "cycles overflowed at %ld", i); } - double avg_nanos = nanos_sum / (double)iterations; - double avg_cycles = cycles_sum / (double)iterations; - printf("%lf ns, %lf cycles\n", avg_nanos, avg_cycles); + // Since uint64_t <-> double conversion are not well defined + // and we use really small units (cycles and nanoseconds) I am willing + // to accept that we throw away anything after the decimal point. + uint64_t avg_nanos = nanos_sum / iterations; + uint64_t avg_cycles = cycles_sum / iterations; + + printf("%lu ns, %lu cycles\n", avg_nanos, avg_cycles); return 0; } diff --git a/stopwatch.c b/stopwatch.c index aaf5028..472baf5 100644 --- a/stopwatch.c +++ b/stopwatch.c @@ -1,6 +1,6 @@ #include "stopwatch.h" -int64_t cycles_start, cycles_stop; +uint64_t cycles_start, cycles_stop; struct timespec start, stop; int64_t sec_to_nanos(int64_t sec) { return sec * 1000 * 1000 * 1000; } @@ -13,4 +13,4 @@ int64_t clock_diff_nanos() { return nanos; } -int64_t clock_diff_cycles() { return cycles_stop - cycles_start; } +uint64_t clock_diff_cycles() { return cycles_stop - cycles_start; } diff --git a/stopwatch.h b/stopwatch.h index 6830f97..7b670d1 100644 --- a/stopwatch.h +++ b/stopwatch.h @@ -3,17 +3,17 @@ #include <stdint.h> #include <time.h> -extern int64_t cycles_start, cycles_stop; +extern uint64_t cycles_start, cycles_stop; extern struct timespec start, stop; -static __inline__ int64_t rdtsc_s(void) { +static __inline__ uint64_t rdtsc_s(void) { unsigned a, d; asm volatile("cpuid" ::: "%rax", "%rbx", "%rcx", "%rdx"); asm volatile("rdtsc" : "=a"(a), "=d"(d)); return ((unsigned long)a) | (((unsigned long)d) << 32); } -static __inline__ int64_t rdtsc_e(void) { +static __inline__ uint64_t rdtsc_e(void) { unsigned a, d; asm volatile("rdtscp" : "=a"(a), "=d"(d)); asm volatile("cpuid" ::: "%rax", "%rbx", "%rcx", "%rdx"); @@ -30,7 +30,7 @@ static inline void stop_watch() { clock_gettime(CLOCK_MONOTONIC, &stop); } -int64_t clock_diff_cycles(); +uint64_t clock_diff_cycles(); int64_t clock_diff_nanos(); double nanos_to_millis(int64_t nanos); int64_t sec_to_nanos(int64_t sec); -- GitLab