1 | // Copyright 2021 Google Inc. All rights reserved. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | #include "perf_counters.h" |
16 | |
17 | #include <cstring> |
18 | #include <vector> |
19 | |
20 | #if defined HAVE_LIBPFM |
21 | #include "perfmon/pfmlib.h" |
22 | #include "perfmon/pfmlib_perf_event.h" |
23 | #endif |
24 | |
25 | namespace benchmark { |
26 | namespace internal { |
27 | |
28 | constexpr size_t PerfCounterValues::kMaxCounters; |
29 | |
30 | #if defined HAVE_LIBPFM |
31 | const bool PerfCounters::kSupported = true; |
32 | |
33 | bool PerfCounters::Initialize() { return pfm_initialize() == PFM_SUCCESS; } |
34 | |
35 | PerfCounters PerfCounters::Create( |
36 | const std::vector<std::string>& counter_names) { |
37 | if (counter_names.empty()) { |
38 | return NoCounters(); |
39 | } |
40 | if (counter_names.size() > PerfCounterValues::kMaxCounters) { |
41 | GetErrorLogInstance() |
42 | << counter_names.size() |
43 | << " counters were requested. The minimum is 1, the maximum is " |
44 | << PerfCounterValues::kMaxCounters << "\n" ; |
45 | return NoCounters(); |
46 | } |
47 | std::vector<int> counter_ids(counter_names.size()); |
48 | |
49 | const int mode = PFM_PLM3; // user mode only |
50 | for (size_t i = 0; i < counter_names.size(); ++i) { |
51 | const bool is_first = i == 0; |
52 | struct perf_event_attr attr{}; |
53 | attr.size = sizeof(attr); |
54 | const int group_id = !is_first ? counter_ids[0] : -1; |
55 | const auto& name = counter_names[i]; |
56 | if (name.empty()) { |
57 | GetErrorLogInstance() << "A counter name was the empty string\n" ; |
58 | return NoCounters(); |
59 | } |
60 | pfm_perf_encode_arg_t arg{}; |
61 | arg.attr = &attr; |
62 | |
63 | const int pfm_get = |
64 | pfm_get_os_event_encoding(name.c_str(), mode, PFM_OS_PERF_EVENT, &arg); |
65 | if (pfm_get != PFM_SUCCESS) { |
66 | GetErrorLogInstance() << "Unknown counter name: " << name << "\n" ; |
67 | return NoCounters(); |
68 | } |
69 | attr.disabled = is_first; |
70 | attr.pinned = is_first; |
71 | attr.exclude_kernel = true; |
72 | attr.exclude_user = false; |
73 | attr.exclude_hv = true; |
74 | // Read all counters in one read. |
75 | attr.read_format = PERF_FORMAT_GROUP; |
76 | |
77 | int id = -1; |
78 | static constexpr size_t kNrOfSyscallRetries = 5; |
79 | // Retry syscall as it was interrupted often (b/64774091). |
80 | for (size_t num_retries = 0; num_retries < kNrOfSyscallRetries; |
81 | ++num_retries) { |
82 | id = perf_event_open(&attr, 0, -1, group_id, 0); |
83 | if (id >= 0 || errno != EINTR) { |
84 | break; |
85 | } |
86 | } |
87 | if (id < 0) { |
88 | GetErrorLogInstance() |
89 | << "Failed to get a file descriptor for " << name << "\n" ; |
90 | return NoCounters(); |
91 | } |
92 | |
93 | counter_ids[i] = id; |
94 | } |
95 | if (ioctl(counter_ids[0], PERF_EVENT_IOC_ENABLE) != 0) { |
96 | GetErrorLogInstance() << "Failed to start counters\n" ; |
97 | return NoCounters(); |
98 | } |
99 | |
100 | return PerfCounters(counter_names, std::move(counter_ids)); |
101 | } |
102 | |
103 | PerfCounters::~PerfCounters() { |
104 | if (counter_ids_.empty()) { |
105 | return; |
106 | } |
107 | ioctl(counter_ids_[0], PERF_EVENT_IOC_DISABLE); |
108 | for (int fd : counter_ids_) { |
109 | close(fd); |
110 | } |
111 | } |
112 | #else // defined HAVE_LIBPFM |
113 | const bool PerfCounters::kSupported = false; |
114 | |
115 | bool PerfCounters::Initialize() { return false; } |
116 | |
117 | PerfCounters PerfCounters::Create( |
118 | const std::vector<std::string>& counter_names) { |
119 | if (!counter_names.empty()) { |
120 | GetErrorLogInstance() << "Performance counters not supported." ; |
121 | } |
122 | return NoCounters(); |
123 | } |
124 | |
125 | PerfCounters::~PerfCounters() = default; |
126 | #endif // defined HAVE_LIBPFM |
127 | } // namespace internal |
128 | } // namespace benchmark |
129 | |