-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmulti_event_sampling.cpp
124 lines (105 loc) · 4.53 KB
/
multi_event_sampling.cpp
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
118
119
120
121
122
123
124
#include "access_benchmark.h"
#include <iostream>
#include <perfcpp/hardware_info.h>
#include <perfcpp/sampler.h>
int
main()
{
std::cout << "libperf-cpp example: Record perf samples including time, "
"logical memory address, latency, and data source for "
"single-threaded random access to an in-memory array "
"using multiple events as trigger."
<< std::endl;
/// Initialize counter definitions.
/// Note that the perf::CounterDefinition holds all counter names and must be
/// alive until the benchmark finishes.
auto counter_definitions = perf::CounterDefinition{};
/// Initialize sampler.
auto perf_config = perf::SampleConfig{};
perf_config.period(8000U); /// Record every 8,000th event.
auto sampler = perf::Sampler{ counter_definitions, perf_config };
if (perf::HardwareInfo::is_intel()) {
sampler.trigger(std::vector<std::vector<perf::Sampler::Trigger>>{
{
perf::Sampler::Trigger{ "mem-loads", perf::Precision::RequestZeroSkid } /// Loads
},
{ perf::Sampler::Trigger{ "mem-stores", perf::Precision::MustHaveZeroSkid } } /// Stores
});
} else {
std::cout << "Error: Memory sampling with multiple triggers is not supported on this CPU." << std::endl;
return 1;
}
/// Define what to sample.
sampler.values().time(true).logical_memory_address(true).data_src(true).latency(true);
/// Create random access benchmark.
auto benchmark = perf::example::AccessBenchmark{ /*randomize the accesses*/ true,
/* create benchmark of 512 MB */ 512U,
/* also support writing */ true };
/// Start sampling.
try {
sampler.start();
} catch (std::runtime_error& exception) {
std::cerr << exception.what() << std::endl;
return 1;
}
/// Execute the benchmark (accessing cache lines in a random order).
auto value = 0ULL;
for (auto index = 0U; index < benchmark.size(); ++index) {
value += benchmark[index].value;
/// Also write a value to get store events.
benchmark.set(index, value);
}
asm volatile(""
: "+r,m"(value)
:
: "memory"); /// We do not want the compiler to optimize away
/// this unused value.
/// Stop sampling.
sampler.stop();
/// Get all the recorded samples.
auto samples = sampler.result(/* sort by time */ true);
const auto count_samples_before_filter = samples.size();
/// Print the first samples.
const auto count_show_samples = std::min<std::size_t>(samples.size(), 40U);
std::cout << "\nRecorded " << count_samples_before_filter << " samples. " << samples.size()
<< " remaining after filter." << std::endl;
std::cout << "Here are the first " << count_show_samples << " recorded samples:\n" << std::endl;
for (auto index = 0U; index < count_show_samples; ++index) {
const auto& sample = samples[index];
/// Since we recorded the time, period, the instruction pointer, and the CPU
/// id, we can only read these values.
if (sample.time().has_value() && sample.logical_memory_address().has_value() && sample.data_src().has_value()) {
auto data_source = "N/A";
if (sample.data_src()->is_mem_l1()) {
data_source = "L1d";
} else if (sample.data_src()->is_mem_lfb()) {
data_source = "LFB/MAB";
} else if (sample.data_src()->is_mem_l2()) {
data_source = "L2";
} else if (sample.data_src()->is_mem_l3()) {
data_source = "L3";
} else if (sample.data_src()->is_mem_local_ram()) {
data_source = "local RAM";
}
auto type = "N/A";
if (sample.data_src()->is_load()) {
type = "Load";
} else if (sample.data_src()->is_store()) {
type = "Store";
}
const auto latency = sample.latency().value_or(perf::Latency{});
std::cout << "Time = " << sample.time().value() << " | Logical Mem Address = 0x" << std::hex
<< sample.logical_memory_address().value() << std::dec
<< " | Latency (cache, instruction) = " << latency.cache_latency() << ", "
<< latency.instruction_retirement_latency() << " | Type = " << type
<< " | Data Source = " << data_source << "\n";
} else if (sample.count_loss().has_value()) {
std::cout << "Loss = " << sample.count_loss().value() << "\n";
}
}
std::cout << std::flush;
/// Close the sampler.
/// Note that the sampler can only be closed after reading the samples.
sampler.close();
return 0;
}