aboutsummaryrefslogtreecommitdiff
path: root/etc
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
parentd950635488d888960d876cd50d3b92ee1d48a6da (diff)
benchmarking: correct for differences in CPU and TSC frequency
Diffstat (limited to 'etc')
-rwxr-xr-xetc/cpufreq (renamed from etc/freq.sh)2
-rw-r--r--etc/tscfreq.c23
2 files changed, 24 insertions, 1 deletions
diff --git a/etc/freq.sh b/etc/cpufreq
index 7fe306f2e..bc5a66a85 100755
--- a/etc/freq.sh
+++ b/etc/cpufreq
@@ -7,7 +7,7 @@ for cpu in $(seq 1 $(nproc)); do
done | ( \
sleep .1 ;
mhz=$(cat /proc/cpuinfo | grep "^[c]pu MHz" | cut -d: -f2 | tr -d ' ' | sort -nr | head -1);
- printf "$(echo "scale=2; ($mhz + 5)/1000" | bc)ghz\n"
+ printf "$(echo "scale=2; ($mhz + 5)/1000" | bc)\n"
while IFS= read -r pid; do
kill "$pid";
done )
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);
+}