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 <stdio.h>
18 #include <map>
19 #include <string>
20 #include <vector>
21
22 #include <android-base/logging.h>
23 #include <android-base/test_utils.h>
24
25 #include "command.h"
26 #include "environment.h"
27 #include "event_attr.h"
28 #include "event_fd.h"
29 #include "event_selection_set.h"
30 #include "event_type.h"
31
IsEventTypeSupported(const EventType & event_type)32 static bool IsEventTypeSupported(const EventType& event_type) {
33 if (event_type.type != PERF_TYPE_RAW) {
34 perf_event_attr attr = CreateDefaultPerfEventAttr(event_type);
35 // Exclude kernel to list supported events even when
36 // /proc/sys/kernel/perf_event_paranoid is 2.
37 attr.exclude_kernel = 1;
38 return IsEventAttrSupported(attr);
39 }
40 if (event_type.limited_arch == "arm" && GetBuildArch() != ARCH_ARM &&
41 GetBuildArch() != ARCH_ARM64) {
42 return false;
43 }
44 // Because the kernel may not check whether the raw event is supported by the cpu pmu.
45 // We can't decide whether the raw event is supported by calling perf_event_open().
46 // Instead, we can check if it can collect some real number.
47 perf_event_attr attr = CreateDefaultPerfEventAttr(event_type);
48 std::unique_ptr<EventFd> event_fd = EventFd::OpenEventFile(attr, gettid(), -1, nullptr);
49 if (event_fd == nullptr) {
50 return false;
51 }
52 auto work_function = []() {
53 TemporaryFile tmpfile;
54 FILE* fp = fopen(tmpfile.path, "w");
55 if (fp == nullptr) {
56 return;
57 }
58 for (int i = 0; i < 10; ++i) {
59 fprintf(fp, "output some data\n");
60 }
61 fclose(fp);
62 };
63 work_function();
64 PerfCounter counter;
65 if (!event_fd->ReadCounter(&counter)) {
66 return false;
67 }
68 return (counter.value != 0u);
69 }
70
PrintEventTypesOfType(uint32_t type,const std::string & type_name,const std::vector<EventType> & event_types)71 static void PrintEventTypesOfType(uint32_t type, const std::string& type_name,
72 const std::vector<EventType>& event_types) {
73 printf("List of %s:\n", type_name.c_str());
74 if (type == PERF_TYPE_RAW && (GetBuildArch() == ARCH_ARM || GetBuildArch() == ARCH_ARM64)) {
75 printf(" # Please refer to PMU event numbers listed in ARMv8 manual for details.\n");
76 printf(" # A possible link is https://developer.arm.com/docs/ddi0487/latest/arm-architecture-reference-manual-armv8-for-armv8-a-architecture-profile.\n");
77 }
78 for (auto& event_type : event_types) {
79 if (event_type.type == type) {
80 if (IsEventTypeSupported(event_type)) {
81 printf(" %s", event_type.name.c_str());
82 if (!event_type.description.empty()) {
83 printf("\t\t# %s", event_type.description.c_str());
84 }
85 printf("\n");
86 }
87 }
88 }
89 printf("\n");
90 }
91
92 class ListCommand : public Command {
93 public:
ListCommand()94 ListCommand()
95 : Command("list", "list available event types",
96 // clang-format off
97 "Usage: simpleperf list [options] [hw|sw|cache|raw|tracepoint]\n"
98 " List all available event types.\n"
99 " Filters can be used to show only event types belong to selected types:\n"
100 " hw hardware events\n"
101 " sw software events\n"
102 " cache hardware cache events\n"
103 " raw raw pmu events\n"
104 " tracepoint tracepoint events\n"
105 "Options:\n"
106 "--show-features Show features supported on the device, including:\n"
107 " dwarf-based-call-graph\n"
108 " trace-offcpu\n"
109 // clang-format on
110 ) {
111 }
112
113 bool Run(const std::vector<std::string>& args) override;
114
115 private:
116 void ShowFeatures();
117 };
118
Run(const std::vector<std::string> & args)119 bool ListCommand::Run(const std::vector<std::string>& args) {
120 if (!CheckPerfEventLimit()) {
121 return false;
122 }
123
124 static std::map<std::string, std::pair<int, std::string>> type_map = {
125 {"hw", {PERF_TYPE_HARDWARE, "hardware events"}},
126 {"sw", {PERF_TYPE_SOFTWARE, "software events"}},
127 {"cache", {PERF_TYPE_HW_CACHE, "hw-cache events"}},
128 {"raw", {PERF_TYPE_RAW, "raw events provided by cpu pmu"}},
129 {"tracepoint", {PERF_TYPE_TRACEPOINT, "tracepoint events"}},
130 {"user-space-sampler", {SIMPLEPERF_TYPE_USER_SPACE_SAMPLERS, "user-space samplers"}},
131 };
132
133 std::vector<std::string> names;
134 if (args.empty()) {
135 for (auto& item : type_map) {
136 names.push_back(item.first);
137 }
138 } else {
139 for (auto& arg : args) {
140 if (type_map.find(arg) != type_map.end()) {
141 names.push_back(arg);
142 } else if (arg == "--show-features") {
143 ShowFeatures();
144 return true;
145 } else {
146 LOG(ERROR) << "unknown event type category: " << arg << ", try using \"help list\"";
147 return false;
148 }
149 }
150 }
151
152 auto& event_types = GetAllEventTypes();
153
154 for (auto& name : names) {
155 auto it = type_map.find(name);
156 PrintEventTypesOfType(it->second.first, it->second.second, event_types);
157 }
158 return true;
159 }
160
ShowFeatures()161 void ListCommand::ShowFeatures() {
162 if (IsDwarfCallChainSamplingSupported()) {
163 printf("dwarf-based-call-graph\n");
164 }
165 if (IsDumpingRegsForTracepointEventsSupported()) {
166 printf("trace-offcpu\n");
167 }
168 if (IsSettingClockIdSupported()) {
169 printf("set-clockid\n");
170 }
171 }
172
RegisterListCommand()173 void RegisterListCommand() {
174 RegisterCommand("list", [] { return std::unique_ptr<Command>(new ListCommand); });
175 }
176