1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <sched.h>
18 #include <stdio.h>
19
20 #include <atomic>
21 #include <map>
22 #include <string>
23 #include <thread>
24 #include <vector>
25
26 #include <android-base/file.h>
27 #include <android-base/logging.h>
28 #include <android-base/strings.h>
29
30 #include "ETMRecorder.h"
31 #include "RegEx.h"
32 #include "command.h"
33 #include "environment.h"
34 #include "event_attr.h"
35 #include "event_fd.h"
36 #include "event_selection_set.h"
37 #include "event_type.h"
38
39 namespace simpleperf {
40
41 extern std::unordered_map<std::string, std::unordered_set<int>> cpu_supported_raw_events;
42
43 #if defined(__aarch64__) || defined(__arm__)
44 extern std::unordered_map<uint64_t, std::string> cpuid_to_name;
45 #endif // defined(__aarch64__) || defined(__arm__)
46
47 #if defined(__riscv)
48 extern std::map<std::tuple<uint64_t, std::string, std::string>, std::string> cpuid_to_name;
49 #endif // defined(__riscv)
50
51 namespace {
52
53 struct RawEventTestThreadArg {
54 int cpu;
55 std::atomic<pid_t> tid;
56 std::atomic<bool> start;
57 };
58
RawEventTestThread(RawEventTestThreadArg * arg)59 static void RawEventTestThread(RawEventTestThreadArg* arg) {
60 cpu_set_t mask;
61 CPU_ZERO(&mask);
62 CPU_SET(arg->cpu, &mask);
63 int tid = gettid();
64 sched_setaffinity(tid, sizeof(mask), &mask);
65 arg->tid = tid;
66 while (!arg->start) {
67 std::this_thread::sleep_for(std::chrono::milliseconds(1));
68 }
69 TemporaryFile tmpfile;
70 FILE* fp = fopen(tmpfile.path, "w");
71 if (fp == nullptr) {
72 return;
73 }
74 for (int i = 0; i < 10; ++i) {
75 fprintf(fp, "output some data\n");
76 }
77 fclose(fp);
78 }
79
80 struct RawEventSupportStatus {
81 std::vector<int> supported_cpus;
82 std::vector<int> may_supported_cpus;
83 };
84
85 #if defined(__riscv)
to_hex_string(uint64_t value)86 std::string to_hex_string(uint64_t value) {
87 std::stringstream stream;
88 stream << "0x" << std::hex << value;
89 return stream.str();
90 }
91
find_cpu_name(const std::tuple<uint64_t,uint64_t,uint64_t> & cpu_id,const std::map<std::tuple<uint64_t,std::string,std::string>,std::string> & cpuid_to_name)92 auto find_cpu_name(
93 const std::tuple<uint64_t, uint64_t, uint64_t>& cpu_id,
94 const std::map<std::tuple<uint64_t, std::string, std::string>, std::string>& cpuid_to_name) {
95 // cpu_id: mvendorid, marchid, mimpid
96 // cpuid_to_name: mvendorid, marchid regex, mimpid regex
97
98 std::string marchid_hex = to_hex_string(get<1>(cpu_id));
99 std::string mimpid_hex = to_hex_string(get<2>(cpu_id));
100 uint64_t mvendorid = std::get<0>(cpu_id);
101
102 // Search the first entry that matches mvendorid
103 auto it = cpuid_to_name.lower_bound({mvendorid, "", ""});
104
105 // Search the iterator of correct regex for current CPU from entries with same mvendorid
106 for (; it != cpuid_to_name.end() && std::get<0>(it->first) == mvendorid; ++it) {
107 const auto& [_, marchid_regex, mimpid_regex] = it->first;
108 if (RegEx::Create(marchid_regex)->Match(marchid_hex) &&
109 RegEx::Create(mimpid_regex)->Match(mimpid_hex)) {
110 break;
111 }
112 }
113
114 return it;
115 }
116 #endif // defined(__riscv)
117
118 class RawEventSupportChecker {
119 public:
Init()120 bool Init() {
121 cpu_models_ = GetCpuModels();
122 if (cpu_models_.empty()) {
123 LOG(ERROR) << "can't get device cpu info";
124 return false;
125 }
126 for (const auto& model : cpu_models_) {
127 cpu_model_names_.push_back(GetCpuModelName(model));
128 }
129 return true;
130 }
131
GetCpusSupportingEvent(const EventType & event_type)132 RawEventSupportStatus GetCpusSupportingEvent(const EventType& event_type) {
133 RawEventSupportStatus status;
134 std::string required_cpu_model;
135 // For cpu model specific events, the limited_arch is like "arm64:Cortex-A520".
136 if (auto pos = event_type.limited_arch.find(':'); pos != std::string::npos) {
137 required_cpu_model = event_type.limited_arch.substr(pos + 1);
138 }
139
140 for (size_t i = 0; i < cpu_models_.size(); ++i) {
141 const CpuModel& model = cpu_models_[i];
142 const std::string& model_name = cpu_model_names_[i];
143 bool got_status = false;
144 bool supported = false;
145 bool may_supported = false;
146
147 if (model.arch == "arm") {
148 if (!required_cpu_model.empty()) {
149 // This is a cpu model specific event, only supported on required_cpu_model.
150 supported = model_name == required_cpu_model;
151 got_status = true;
152 } else if (!model_name.empty()) {
153 // We know events supported on this cpu model.
154 auto it = cpu_supported_raw_events.find(model_name);
155 CHECK(it != cpu_supported_raw_events.end())
156 << "no events configuration for " << model_name;
157 supported = it->second.count(event_type.config) > 0;
158 got_status = true;
159 }
160 } else if (model.arch == "x86") {
161 std::string limited_arch = event_type.limited_arch;
162 if (auto pos = limited_arch.find(':'); pos != std::string::npos) {
163 limited_arch = limited_arch.substr(0, pos);
164 }
165 if (limited_arch != model_name) {
166 supported = false;
167 got_status = true;
168 }
169 }
170
171 if (!got_status) {
172 // We need to test the event support status.
173 TestEventSupportOnCpu(event_type, model.cpus[0], supported, may_supported);
174 }
175
176 if (supported) {
177 status.supported_cpus.insert(status.supported_cpus.end(), model.cpus.begin(),
178 model.cpus.end());
179 } else if (may_supported) {
180 status.may_supported_cpus.insert(status.may_supported_cpus.end(), model.cpus.begin(),
181 model.cpus.end());
182 }
183 }
184 return status;
185 }
186
187 private:
GetCpuModelName(const CpuModel & model)188 std::string GetCpuModelName(const CpuModel& model) {
189 #if defined(__aarch64__) || defined(__arm__)
190 uint64_t cpu_id =
191 (static_cast<uint64_t>(model.arm_data.implementer) << 32) | model.arm_data.partnum;
192 auto it = cpuid_to_name.find(cpu_id);
193 if (it != cpuid_to_name.end()) {
194 return it->second;
195 }
196 #elif defined(__riscv)
197 std::tuple<uint64_t, uint64_t, uint64_t> cpu_id = {
198 model.riscv_data.mvendorid, model.riscv_data.marchid, model.riscv_data.mimpid};
199 auto it = find_cpu_name(cpu_id, cpuid_to_name);
200 if (it != cpuid_to_name.end()) {
201 return it->second;
202 }
203 #elif defined(__i386__) || defined(__x86_64__)
204 if (android::base::StartsWith(model.x86_data.vendor_id, "GenuineIntel")) {
205 return "x86-intel";
206 }
207 if (android::base::StartsWith(model.x86_data.vendor_id, "AuthenticAMD")) {
208 return "x86-amd";
209 }
210 #endif // defined(__i386__) || defined(__x86_64__)
211 return "";
212 }
213
TestEventSupportOnCpu(const EventType & event_type,int cpu,bool & supported,bool & may_supported)214 void TestEventSupportOnCpu(const EventType& event_type, int cpu, bool& supported,
215 bool& may_supported) {
216 // Because the kernel may not check whether the raw event is supported by the cpu pmu.
217 // We can't decide whether the raw event is supported by calling perf_event_open().
218 // Instead, we can check if it can collect some real number.
219 RawEventTestThreadArg test_thread_arg;
220 test_thread_arg.cpu = cpu;
221 test_thread_arg.tid = 0;
222 test_thread_arg.start = false;
223 std::thread test_thread(RawEventTestThread, &test_thread_arg);
224 while (test_thread_arg.tid == 0) {
225 std::this_thread::sleep_for(std::chrono::milliseconds(1));
226 }
227 perf_event_attr attr = CreateDefaultPerfEventAttr(event_type);
228 attr.exclude_kernel = 1;
229 #if defined(__i386__) || defined(__x86_64__)
230 std::set<int> atom_cpus = GetX86IntelAtomCpus();
231 if (atom_cpus.count(cpu) > 0) {
232 attr.config = event_type.GetIntelAtomCpuConfig();
233 }
234 #endif // defined(__i386__) || defined(__x86_64__)
235 std::unique_ptr<EventFd> event_fd = EventFd::OpenEventFile(
236 attr, test_thread_arg.tid, test_thread_arg.cpu, nullptr, event_type.name, false);
237 test_thread_arg.start = true;
238 test_thread.join();
239 if (event_fd == nullptr) {
240 supported = may_supported = false;
241 return;
242 }
243 PerfCounter counter;
244 if (!event_fd->ReadCounter(&counter)) {
245 supported = may_supported = false;
246 return;
247 }
248 if (counter.value != 0) {
249 supported = true;
250 may_supported = false;
251 } else {
252 supported = false;
253 may_supported = true;
254 }
255 }
256
257 std::vector<CpuModel> cpu_models_;
258
259 std::vector<std::string> cpu_model_names_;
260 };
261
ToCpuString(const std::vector<int> & cpus)262 static std::string ToCpuString(const std::vector<int>& cpus) {
263 std::string s;
264 if (cpus.empty()) {
265 return s;
266 }
267 s += std::to_string(cpus[0]);
268 int last_cpu = cpus[0];
269 bool added = true;
270 for (size_t i = 1; i < cpus.size(); ++i) {
271 if (cpus[i] == last_cpu + 1) {
272 last_cpu = cpus[i];
273 added = false;
274 } else {
275 s += "-" + std::to_string(last_cpu) + "," + std::to_string(cpus[i]);
276 last_cpu = cpus[i];
277 added = true;
278 }
279 }
280 if (!added) {
281 s += "-" + std::to_string(last_cpu);
282 }
283 return s;
284 }
285
PrintRawEventTypes(const std::string & type_desc)286 static void PrintRawEventTypes(const std::string& type_desc) {
287 printf("List of %s:\n", type_desc.c_str());
288 #if defined(__aarch64__) || defined(__arm__)
289 printf(
290 // clang-format off
291 " # Please refer to \"PMU common architectural and microarchitectural event numbers\"\n"
292 " # and \"ARM recommendations for IMPLEMENTATION DEFINED event numbers\" listed in\n"
293 " # ARMv9 manual for details.\n"
294 " # A possible link is https://developer.arm.com/documentation/ddi0487.\n"
295 // clang-format on
296 );
297 #endif // defined(__aarch64__) || defined(__arm__)
298 RawEventSupportChecker support_checker;
299 if (!support_checker.Init()) {
300 return;
301 }
302 auto callback = [&](const EventType& event_type) {
303 if (event_type.type != PERF_TYPE_RAW) {
304 return true;
305 }
306 RawEventSupportStatus status = support_checker.GetCpusSupportingEvent(event_type);
307 if (status.supported_cpus.empty() && status.may_supported_cpus.empty()) {
308 return true;
309 }
310 std::string text = " " + event_type.name + " (";
311 if (!status.supported_cpus.empty()) {
312 text += "supported on cpu " + ToCpuString(status.supported_cpus);
313 if (!status.may_supported_cpus.empty()) {
314 text += ", ";
315 }
316 }
317 if (!status.may_supported_cpus.empty()) {
318 text += "may supported on cpu " + ToCpuString(status.may_supported_cpus);
319 }
320 text += ")";
321 printf("%s", text.c_str());
322 if (!event_type.description.empty()) {
323 printf("\t\t# %s", event_type.description.c_str());
324 }
325 printf("\n");
326 return true;
327 };
328 EventTypeManager::Instance().ForEachType(callback);
329 printf("\n");
330 }
331
IsEventTypeSupported(const EventType & event_type)332 static bool IsEventTypeSupported(const EventType& event_type) {
333 // PMU and tracepoint events are provided by kernel. So we assume they're supported.
334 if (event_type.IsPmuEvent() || event_type.IsTracepointEvent()) {
335 return true;
336 }
337 perf_event_attr attr = CreateDefaultPerfEventAttr(event_type);
338 // Exclude kernel to list supported events even when kernel recording isn't allowed.
339 attr.exclude_kernel = 1;
340 return IsEventAttrSupported(attr, event_type.name);
341 }
342
PrintEventTypesOfType(const std::string & type_name,const std::string & type_desc,const std::function<bool (const EventType &)> & is_type_fn)343 static void PrintEventTypesOfType(const std::string& type_name, const std::string& type_desc,
344 const std::function<bool(const EventType&)>& is_type_fn) {
345 if (type_name == "raw") {
346 return PrintRawEventTypes(type_desc);
347 }
348 printf("List of %s:\n", type_desc.c_str());
349 if (GetTargetArch() == ARCH_ARM || GetTargetArch() == ARCH_ARM64) {
350 if (type_name == "cache") {
351 printf(" # More cache events are available in `simpleperf list raw`.\n");
352 }
353 }
354 auto callback = [&](const EventType& event_type) {
355 if (is_type_fn(event_type)) {
356 if (!IsEventTypeSupported(event_type)) {
357 return true;
358 }
359 printf(" %s", event_type.name.c_str());
360 if (!event_type.description.empty()) {
361 printf("\t\t# %s", event_type.description.c_str());
362 }
363 printf("\n");
364 }
365 return true;
366 };
367 EventTypeManager::Instance().ForEachType(callback);
368 printf("\n");
369 }
370
371 class ListCommand : public Command {
372 public:
ListCommand()373 ListCommand()
374 : Command("list", "list available event types",
375 // clang-format off
376 "Usage: simpleperf list [options] [hw|sw|cache|raw|tracepoint|pmu]\n"
377 " List all available event types.\n"
378 " Filters can be used to show only event types belong to selected types:\n"
379 " hw hardware events\n"
380 " sw software events\n"
381 " cache hardware cache events\n"
382 " raw raw cpu pmu events\n"
383 " tracepoint tracepoint events\n"
384 " cs-etm coresight etm instruction tracing events\n"
385 " pmu system-specific pmu events\n"
386 "Options:\n"
387 "--show-features Show features supported on the device, including:\n"
388 " dwarf-based-call-graph\n"
389 " trace-offcpu\n"
390 // clang-format on
391 ) {}
392
393 bool Run(const std::vector<std::string>& args) override;
394
395 private:
396 void ShowFeatures();
397 };
398
Run(const std::vector<std::string> & args)399 bool ListCommand::Run(const std::vector<std::string>& args) {
400 if (!CheckPerfEventLimit()) {
401 return false;
402 }
403
404 static std::map<std::string, std::pair<std::string, std::function<bool(const EventType&)>>>
405 type_map = {
406 {"hw",
407 {"hardware events", [](const EventType& e) { return e.type == PERF_TYPE_HARDWARE; }}},
408 {"sw",
409 {"software events", [](const EventType& e) { return e.type == PERF_TYPE_SOFTWARE; }}},
410 {"cache",
411 {"hw-cache events", [](const EventType& e) { return e.type == PERF_TYPE_HW_CACHE; }}},
412 {"raw",
413 {"raw events provided by cpu pmu",
414 [](const EventType& e) { return e.type == PERF_TYPE_RAW; }}},
415 {"tracepoint",
416 {"tracepoint events",
417 [](const EventType& e) { return e.type == PERF_TYPE_TRACEPOINT; }}},
418 #if defined(__arm__) || defined(__aarch64__)
419 {"cs-etm",
420 {"coresight etm events",
421 [](const EventType& e) {
422 return e.type == ETMRecorder::GetInstance().GetEtmEventType();
423 }}},
424 #endif
425 {"pmu", {"pmu events", [](const EventType& e) { return e.IsPmuEvent(); }}},
426 };
427
428 std::vector<std::string> names;
429 if (args.empty()) {
430 for (auto& item : type_map) {
431 names.push_back(item.first);
432 }
433 } else {
434 for (auto& arg : args) {
435 if (type_map.find(arg) != type_map.end()) {
436 names.push_back(arg);
437 } else if (arg == "--show-features") {
438 ShowFeatures();
439 return true;
440 } else {
441 LOG(ERROR) << "unknown event type category: " << arg << ", try using \"help list\"";
442 return false;
443 }
444 }
445 }
446
447 for (auto& name : names) {
448 auto it = type_map.find(name);
449 PrintEventTypesOfType(name, it->second.first, it->second.second);
450 }
451 return true;
452 }
453
ShowFeatures()454 void ListCommand::ShowFeatures() {
455 if (IsDwarfCallChainSamplingSupported()) {
456 printf("dwarf-based-call-graph\n");
457 }
458 if (IsDumpingRegsForTracepointEventsSupported()) {
459 printf("trace-offcpu\n");
460 }
461 if (IsSettingClockIdSupported()) {
462 printf("set-clockid\n");
463 }
464 }
465
466 } // namespace
467
RegisterListCommand()468 void RegisterListCommand() {
469 RegisterCommand("list", [] { return std::unique_ptr<Command>(new ListCommand); });
470 }
471
472 } // namespace simpleperf
473