-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathregister.cpp
More file actions
93 lines (76 loc) · 3.51 KB
/
register.cpp
File metadata and controls
93 lines (76 loc) · 3.51 KB
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
#include "../access_benchmark.h"
#include "perfcpp/sampler.h"
#include <iostream>
int
main()
{
std::cout << "libperf-cpp example: Record perf samples including time, "
"user_registers, and cpu id for single-threaded random "
"access to an in-memory array."
<< std::endl;
auto sampler = perf::Sampler{};
sampler.trigger("cycles", perf::Period{ 100000 });
sampler.values()
.timestamp(true)
.user_registers(
perf::Registers{ { perf::Registers::x86::IP, perf::Registers::x86::DI, perf::Registers::x86::R10 } })
.kernel_registers(
perf::Registers{ { perf::Registers::x86::IP, perf::Registers::x86::DI, perf::Registers::x86::R10 } })
.cpu_id(true);
/// 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;
}
/// We do not want the compiler to optimize away this (otherwise) unused value (and consequently the loop above).
benchmark.pretend_to_use(value);
/// Stop sampling.
sampler.stop();
/// Get all the recorded samples.
const auto samples = sampler.result();
samples.to_csv("samples_registers.csv");
std::cout << "Wrote samples to `samples_registers.csv`." << std::endl;
/// Print the first samples.
const auto count_show_samples = std::min<std::size_t>(samples.size(), 40U);
std::cout << "\nRecorded " << samples.size() << " samples." << 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.metadata().timestamp().has_value() &&
(sample.user_registers().has_value() || sample.kernel_registers().has_value()) &&
sample.metadata().cpu_id().has_value()) {
std::cout << "Time = " << sample.metadata().timestamp().value()
<< " | CPU ID = " << sample.metadata().cpu_id().value();
if (sample.user_registers().has_value()) {
const auto& user_registers = sample.user_registers().value();
std::cout << " | User Registers = IP(" << user_registers.get(perf::Registers::x86::IP).value_or(0) << "), DI("
<< user_registers.get(perf::Registers::x86::DI).value_or(0) << "), R10("
<< user_registers.get(perf::Registers::x86::R10).value_or(0) << ")";
}
if (sample.kernel_registers().has_value()) {
const auto& kernel_registers = sample.kernel_registers().value();
std::cout << " | Kernel Registers = IP(" << kernel_registers.get(perf::Registers::x86::IP).value_or(0)
<< "), DI(" << kernel_registers.get(perf::Registers::x86::DI).value_or(0) << "), R10("
<< kernel_registers.get(perf::Registers::x86::R10).value_or(0) << ")";
}
std::cout << "\n";
}
}
std::cout << std::flush;
/// Close the sampler.
/// Note that the sampler can only be closed after reading the samples.
sampler.close();
return 0;
}