forked from jmuehlig/perf-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstruction_pointer.cpp
More file actions
84 lines (67 loc) · 3.18 KB
/
instruction_pointer.cpp
File metadata and controls
84 lines (67 loc) · 3.18 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
#include "../access_benchmark.h"
#include "perfcpp/sampler.h"
#include "perfcpp/symbol_resolver.h"
#include <iostream>
int
main()
{
std::cout << "libperf-cpp example: Record perf samples including time, "
"instruction pointer, and cpu id for single-threaded random "
"access to an in-memory array."
<< std::endl;
auto sampler = perf::Sampler{};
/// Event that generates an overflow which is samples.
sampler.trigger("cycles", perf::Precision::RequestZeroSkid, perf::Period{ 50000U });
/// Include Timestamp, period, instruction pointer, and CPU number into samples.
sampler.values().timestamp(true).period(true).instruction_pointer(true).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();
auto symbol_resolver = perf::SymbolResolver{};
/// Print the first samples.
const auto count_show_samples = std::min<std::size_t>(samples.size(), 400U);
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.metadata().period().has_value() &&
sample.instruction_execution().logical_instruction_pointer().has_value() &&
sample.metadata().cpu_id().has_value()) {
auto symbol = std::string{ "??" };
if (auto sym = symbol_resolver.resolve(sample.instruction_execution().logical_instruction_pointer().value());
sym.has_value()) {
symbol = sym->to_string();
}
std::cout << "Time = " << sample.metadata().timestamp().value()
<< " | Period = " << sample.metadata().period().value() << " | Instruction Pointer = 0x" << std::hex
<< sample.instruction_execution().logical_instruction_pointer().value() << std::dec
<< " | Symbol = " << symbol << " | CPU ID = " << sample.metadata().cpu_id().value() << " | "
<< (sample.instruction_execution().logical_instruction_pointer() ? "exact" : "not exact") << "\n";
}
}
std::cout << std::flush;
/// Close the sampler.
/// Note that the sampler can only be closed after reading the samples.
sampler.close();
return 0;
}