aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/benchmark/rdtsc/main.cpp
blob: 8189d10f064abf35eafc1f96195faebe01487d35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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());
    }
}