aboutsummaryrefslogtreecommitdiff
path: root/etc/tscfreq.c
diff options
context:
space:
mode:
authorGravatar Andres Erbsen <andreser@mit.edu>2017-07-05 12:10:43 -0400
committerGravatar Andres Erbsen <andreser@mit.edu>2017-07-05 12:11:37 -0400
commit0a0a7db3bd132eab9a36c1f8fa901dc7ea20b8e7 (patch)
treec61be242b33278f640c84360def3e8c092042d83 /etc/tscfreq.c
parentd950635488d888960d876cd50d3b92ee1d48a6da (diff)
benchmarking: correct for differences in CPU and TSC frequency
Diffstat (limited to 'etc/tscfreq.c')
-rw-r--r--etc/tscfreq.c23
1 files changed, 23 insertions, 0 deletions
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 <unistd.h>
+#include <stdio.h>
+
+// 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);
+}