aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/benchmark/rdtsc/main.cpp
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/system/benchmark/rdtsc/main.cpp
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/system/benchmark/rdtsc/main.cpp')
-rw-r--r--util/system/benchmark/rdtsc/main.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/util/system/benchmark/rdtsc/main.cpp b/util/system/benchmark/rdtsc/main.cpp
new file mode 100644
index 0000000000..8189d10f06
--- /dev/null
+++ b/util/system/benchmark/rdtsc/main.cpp
@@ -0,0 +1,61 @@
+#include <library/cpp/testing/benchmark/bench.h>
+
+#include <util/system/datetime.h>
+#include <util/generic/xrange.h>
+
+Y_FORCE_INLINE ui64 GetCycleCountLinux() {
+ unsigned hi, lo;
+ __asm__ __volatile__("lfence\n"
+ "rdtsc"
+ : "=a"(lo), "=d"(hi));
+ return ((unsigned long long)lo) | (((unsigned long long)hi) << 32);
+}
+
+Y_FORCE_INLINE ui64 GetCycleCountAgri1() {
+ unsigned hi, lo;
+
+ __asm__ __volatile__("rdtscp\n"
+ : "=a"(lo), "=d"(hi)::"%rbx", "%rcx");
+
+ return ((unsigned long long)lo) | (((unsigned long long)hi) << 32);
+}
+
+Y_FORCE_INLINE ui64 GetCycleCountAgri2() {
+ unsigned hi, lo;
+ __asm__ __volatile__("rdtscp\n"
+ : "=a"(lo), "=d"(hi)::"%rbx", "%rcx");
+ /* call cpuid to prevent out of order execution */
+ __asm__ __volatile__("mov $0, %%eax\n"
+ "cpuid\n" ::
+ : "%eax");
+
+ return ((unsigned long long)lo) | (((unsigned long long)hi) << 32);
+}
+
+Y_CPU_BENCHMARK(RdtscUtil, iface) {
+ for (const auto i : xrange(iface.Iterations())) {
+ Y_UNUSED(i);
+ Y_DO_NOT_OPTIMIZE_AWAY(GetCycleCount());
+ }
+}
+
+Y_CPU_BENCHMARK(RdtscLinux, iface) {
+ for (const auto i : xrange(iface.Iterations())) {
+ Y_UNUSED(i);
+ Y_DO_NOT_OPTIMIZE_AWAY(GetCycleCountLinux());
+ }
+}
+
+Y_CPU_BENCHMARK(RdtscAgri1, iface) {
+ for (const auto i : xrange(iface.Iterations())) {
+ Y_UNUSED(i);
+ Y_DO_NOT_OPTIMIZE_AWAY(GetCycleCountAgri1());
+ }
+}
+
+Y_CPU_BENCHMARK(RdtscAgri2, iface) {
+ for (const auto i : xrange(iface.Iterations())) {
+ Y_UNUSED(i);
+ Y_DO_NOT_OPTIMIZE_AWAY(GetCycleCountAgri2());
+ }
+}