From 0a0a7db3bd132eab9a36c1f8fa901dc7ea20b8e7 Mon Sep 17 00:00:00 2001 From: Andres Erbsen Date: Wed, 5 Jul 2017 12:10:43 -0400 Subject: benchmarking: correct for differences in CPU and TSC frequency --- etc/tscfreq.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 etc/tscfreq.c (limited to 'etc/tscfreq.c') diff --git a/etc/tscfreq.c b/etc/tscfreq.c new file mode 100644 index 000000000..459db083c --- /dev/null +++ b/etc/tscfreq.c @@ -0,0 +1,23 @@ +#include +#include + +// cpucycles_amd64cpuinfo +long long cpucycles(void) +{ + unsigned long long result; + asm volatile(".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax" + : "=a" (result) :: "%rdx"); + return result; +} + +static const long long usecs = 100*1000; + +int main() { + long long t0 = cpucycles(); + usleep(usecs); + long long t1 = cpucycles(); + const long long tsc_hz = ((t1-t0)*1000*1000)/usecs; + const long long tsc_mhz = tsc_hz / 1000 / 1000; + const long long tsc_ghz = tsc_mhz / 1000; + printf("%1d.%02d\n", tsc_ghz, (tsc_mhz - 1000*tsc_ghz + 5)/10); +} -- cgit v1.2.3