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