aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/testing/benchmark/bench.h
blob: 21551ad0dd4ee71e91bfa9fefd0cab054d31e5f8 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#pragma once

#include <util/system/compiler.h>
#include <util/system/types.h>

#include <utility>

namespace NBench {
    namespace NCpu {
        struct TParams {
            inline size_t Iterations() const noexcept {
                return Iterations_;
            }

            const size_t Iterations_;
        };

        using TUserFunc = void(TParams&);

        struct TRegistar {
            TRegistar(const char* name, TUserFunc func);

            char Buf[128];
        };
    }

    /**
     * Functions that states "I can read and write everywhere in memory".
     *
     * Use it to prevent optimizer from reordering or discarding memory writes prior to it's call,
     * and force memory reads after it's call.
     */
    void Clobber();

    /**
     * Forces whatever `p` points to be in memory and not in register.
     *
     * @param       Pointer to data.
     */
    template <typename T>
    void Escape(T* p);

#if defined(__GNUC__)
    Y_FORCE_INLINE void Clobber() {
        asm volatile(""
                     :
                     :
                     : "memory");
    }
#elif defined(_MSC_VER)
    Y_FORCE_INLINE void Clobber() {
        _ReadWriteBarrier();
    }

#else
    Y_FORCE_INLINE void Clobber() {
    }
#endif

#if defined(__GNUC__)
    template <typename T>
    Y_FORCE_INLINE void Escape(T* p) {
        asm volatile(""
                     :
                     : "g"(p)
                     : "memory");
    }
#else
    template <typename T>
    Y_FORCE_INLINE void Escape(T*) {
    }
#endif

    /**
     * Use this function to prevent unused variables elimination.
     *
     * @param       Unused variable (e.g. return value of benchmarked function).
     */
    template <typename T>
    Y_FORCE_INLINE void DoNotOptimize(T&& datum) {
        ::DoNotOptimizeAway(std::forward<T>(datum));
    }

    int Main(int argc, char** argv);
}

#define Y_CPU_BENCHMARK(name, cnt)                              \
    namespace N_bench_##name {                                  \
        static void Run(::NBench::NCpu::TParams&);              \
        const ::NBench::NCpu::TRegistar benchmark(#name, &Run); \
    }                                                           \
    static void N_bench_##name::Run(::NBench::NCpu::TParams& cnt)