-
Notifications
You must be signed in to change notification settings - Fork 4
/
address_sampling.cpp
123 lines (106 loc) · 4.94 KB
/
address_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
#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."
<< 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 sampler = perf::Sampler{ counter_definitions };
/// Setup which counters trigger the writing of samples (depends on the underlying hardware substrate).
if (perf::HardwareInfo::is_amd_ibs_supported()) {
sampler.trigger("ibs_op_uops", perf::Precision::MustHaveZeroSkid, perf::Period{ 16000U });
} else if (perf::HardwareInfo::is_intel()) {
sampler.trigger("mem-loads", perf::Precision::MustHaveZeroSkid, perf::Period{ 100U });
} else {
std::cout << "Error: Memory sampling is not supported on this CPU." << std::endl;
return 1;
}
/// Setup which data will be included into samples (timestamp, virtual memory address, data source like L1d or RAM,
/// and latency).
sampler.values().time(true).logical_memory_address(true).data_src(true);
#ifndef PERFCPP_NO_SAMPLE_WEIGHT_STRUCT
sampler.values().weight_struct(true);
#else
sampler.values().weight(true);
#endif
/// Create random access benchmark.
auto benchmark = perf::example::AccessBenchmark{ /*randomize the accesses*/ true,
/* create benchmark of 512 MB */ 512U };
/// 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;
}
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();
const auto count_samples_before_filter = samples.size();
/// Filter out samples without data source (AMD samples all instructions, not only data-related).
samples.erase(std::remove_if(samples.begin(),
samples.end(),
[](const auto& sample) {
return sample.count_loss().has_value() || sample.data_src().has_value() == false ||
sample.data_src().value().is_na() || sample.weight().has_value() == false ||
sample.logical_memory_address().value_or(0U) == 0U;
}),
samples.end());
/// 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";
}
const auto weight = sample.weight().value_or(perf::Weight{ 0U, 0U, 0U });
std::cout << "Time = " << sample.time().value() << " | Logical Mem Address = 0x" << std::hex
<< sample.logical_memory_address().value() << std::dec
<< " | Latency (cache, instruction) = " << weight.cache_latency() << ", "
<< weight.instruction_retirement_latency() << " | Is Load = " << sample.data_src()->is_load()
<< " | 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;
}