aboutsummaryrefslogtreecommitdiff
path: root/MicroBenchmarks/XRay/ReturnReference/retref-bench.cc
blob: 84a8a4e54978d14dce4e15e4eedcf2f52cb1fbb6 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//===- retref-bench.cc - XRay Instrumentation Benchmarks ------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Define benchmarks for measuring the cost of XRay instrumentation (the sleds,
// the trampolines).
//
//===----------------------------------------------------------------------===//

#include <x86intrin.h>

#include "benchmark/benchmark.h"
#include "xray/xray_interface.h"

[[clang::xray_never_instrument]] __attribute__((noinline)) int
neverInstrumented() {
  benchmark::ClobberMemory();
  return 0;
}

[[clang::xray_never_instrument]] static void BM_ReturnNeverInstrumented(
    benchmark::State& state) {
  for (auto _ : state) {
    benchmark::DoNotOptimize(neverInstrumented());
  }
}

BENCHMARK(BM_ReturnNeverInstrumented);

[[clang::xray_always_instrument]] __attribute__((noinline)) int
alwaysInstrumented() {
  benchmark::ClobberMemory();
  return 0;
}

[[clang::xray_never_instrument]] static void BM_ReturnInstrumentedUnPatched(
    benchmark::State& state) {
  __xray_unpatch();
  for (auto _ : state) {
    int x;
    benchmark::DoNotOptimize(x = alwaysInstrumented());
    benchmark::ClobberMemory();
  }
}

BENCHMARK(BM_ReturnInstrumentedUnPatched);


[[clang::xray_never_instrument]] static void BM_ReturnInstrumentedPatchedThenUnpatched(
    benchmark::State& state) {
  __xray_patch();
  __xray_unpatch();
  for (auto _ : state) {
    int x;
    benchmark::DoNotOptimize(x = alwaysInstrumented());
    benchmark::ClobberMemory();
  }
}

BENCHMARK(BM_ReturnInstrumentedPatchedThenUnpatched);


[[clang::xray_never_instrument]] static void BM_ReturnInstrumentedPatched(
    benchmark::State& state) {
  __xray_patch();
  for (auto _ : state) {
    int x;
    benchmark::DoNotOptimize(alwaysInstrumented());
    benchmark::ClobberMemory();
  }
}

BENCHMARK(BM_ReturnInstrumentedPatched);

[[clang::xray_never_instrument]] static void BM_RDTSCP_Cost(
    benchmark::State& state) {
  for (auto _ : state) {
    unsigned cpu;
    unsigned tsc;
    benchmark::DoNotOptimize(tsc = __rdtscp(&cpu));
    benchmark::ClobberMemory();
  }
}

volatile unsigned global_cpu;
volatile unsigned tsc;
[[clang::xray_never_instrument]] void benchmark_handler(int32_t,
                                                        XRayEntryType) {
  unsigned cpu;
  benchmark::DoNotOptimize(tsc = __rdtscp(&cpu));
  global_cpu = cpu;
  benchmark::ClobberMemory();
}

BENCHMARK(BM_RDTSCP_Cost);

[[clang::xray_never_instrument]] static void
BM_ReturnInstrumentedPatchedWithLogHandler(benchmark::State& state) {
  __xray_set_handler(benchmark_handler);
  __xray_patch();
  benchmark::ClobberMemory();
  for (auto _ : state) {
    int x;
    benchmark::DoNotOptimize(x = alwaysInstrumented());
    benchmark::ClobberMemory();
  }
  __xray_remove_handler();
}

BENCHMARK(BM_ReturnInstrumentedPatchedWithLogHandler);

BENCHMARK_MAIN();