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 "event_type.h"
18
19 #include <inttypes.h>
20 #include <unistd.h>
21 #include <algorithm>
22 #include <string>
23 #include <vector>
24
25 #include <android-base/file.h>
26 #include <android-base/logging.h>
27 #include <android-base/parseint.h>
28 #include <android-base/stringprintf.h>
29 #include <android-base/strings.h>
30
31 #include "event_attr.h"
32 #include "utils.h"
33
34 #define EVENT_TYPE_TABLE_ENTRY(name, type, config, description, limited_arch) \
35 {name, type, config, description, limited_arch},
36
37 static const std::vector<EventType> static_event_type_array = {
38 #include "event_type_table.h"
39 };
40
41 static std::string tracepoint_events;
42
SetTracepointEventsFilePath(const std::string & filepath)43 bool SetTracepointEventsFilePath(const std::string& filepath) {
44 if (!android::base::ReadFileToString(filepath, &tracepoint_events)) {
45 PLOG(ERROR) << "Failed to read " << filepath;
46 return false;
47 }
48 return true;
49 }
50
GetTracepointEvents()51 std::string GetTracepointEvents() {
52 std::string result;
53 for (const EventType& event : GetAllEventTypes()) {
54 if (!result.empty()) {
55 result.push_back('\n');
56 }
57 result += android::base::StringPrintf("%s %" PRIu64, event.name.c_str(), event.config);
58 }
59 return result;
60 }
61
GetTracepointEventTypesFromString(const std::string & s)62 static std::vector<EventType> GetTracepointEventTypesFromString(const std::string& s) {
63 std::vector<EventType> result;
64 for (auto& line : android::base::Split(s, "\n")) {
65 std::vector<std::string> items = android::base::Split(line, " ");
66 CHECK_EQ(items.size(), 2u);
67 std::string event_name = items[0];
68 uint64_t id;
69 CHECK(android::base::ParseUint(items[1].c_str(), &id));
70 result.push_back(EventType(event_name, PERF_TYPE_TRACEPOINT, id, "", ""));
71 }
72 return result;
73 }
74
GetTracepointEventTypesFromTraceFs()75 static std::vector<EventType> GetTracepointEventTypesFromTraceFs() {
76 std::vector<EventType> result;
77 const std::string tracepoint_dirname = "/sys/kernel/debug/tracing/events";
78 for (const auto& system_name : GetSubDirs(tracepoint_dirname)) {
79 std::string system_path = tracepoint_dirname + "/" + system_name;
80 for (const auto& event_name : GetSubDirs(system_path)) {
81 std::string id_path = system_path + "/" + event_name + "/id";
82 std::string id_content;
83 if (!android::base::ReadFileToString(id_path, &id_content)) {
84 continue;
85 }
86 char* endptr;
87 uint64_t id = strtoull(id_content.c_str(), &endptr, 10);
88 if (endptr == id_content.c_str()) {
89 LOG(DEBUG) << "unexpected id '" << id_content << "' in " << id_path;
90 continue;
91 }
92 result.push_back(EventType(system_name + ":" + event_name, PERF_TYPE_TRACEPOINT, id, "", ""));
93 }
94 }
95 return result;
96 }
97
GetTracepointEventTypes()98 static std::vector<EventType> GetTracepointEventTypes() {
99 std::vector<EventType> result;
100 if (!tracepoint_events.empty()) {
101 result = GetTracepointEventTypesFromString(tracepoint_events);
102 } else {
103 result = GetTracepointEventTypesFromTraceFs();
104 }
105 std::sort(result.begin(), result.end(),
106 [](const EventType& type1, const EventType& type2) { return type1.name < type2.name; });
107 return result;
108 }
109
GetAllEventTypes()110 const std::vector<EventType>& GetAllEventTypes() {
111 static std::vector<EventType> event_type_array;
112 if (event_type_array.empty()) {
113 event_type_array.insert(event_type_array.end(), static_event_type_array.begin(),
114 static_event_type_array.end());
115 std::vector<EventType> tracepoint_array = GetTracepointEventTypes();
116 event_type_array.insert(event_type_array.end(), tracepoint_array.begin(),
117 tracepoint_array.end());
118 }
119 return event_type_array;
120 }
121
FindEventTypeByName(const std::string & name)122 const EventType* FindEventTypeByName(const std::string& name) {
123 const EventType* result = nullptr;
124 for (auto& event_type : GetAllEventTypes()) {
125 if (android::base::EqualsIgnoreCase(event_type.name, name)) {
126 result = &event_type;
127 break;
128 }
129 }
130 if (result == nullptr) {
131 LOG(ERROR) << "Unknown event_type '" << name
132 << "', try `simpleperf list` to list all possible event type names";
133 return nullptr;
134 }
135 return result;
136 }
137
ParseEventType(const std::string & event_type_str)138 std::unique_ptr<EventTypeAndModifier> ParseEventType(const std::string& event_type_str) {
139 static std::string modifier_characters = "ukhGHp";
140 std::unique_ptr<EventTypeAndModifier> event_type_modifier(new EventTypeAndModifier);
141 event_type_modifier->name = event_type_str;
142 std::string event_type_name = event_type_str;
143 std::string modifier;
144 size_t comm_pos = event_type_str.rfind(':');
145 if (comm_pos != std::string::npos) {
146 bool match_modifier = true;
147 for (size_t i = comm_pos + 1; i < event_type_str.size(); ++i) {
148 char c = event_type_str[i];
149 if (c != ' ' && modifier_characters.find(c) == std::string::npos) {
150 match_modifier = false;
151 break;
152 }
153 }
154 if (match_modifier) {
155 event_type_name = event_type_str.substr(0, comm_pos);
156 modifier = event_type_str.substr(comm_pos + 1);
157 }
158 }
159 const EventType* event_type = FindEventTypeByName(event_type_name);
160 if (event_type == nullptr) {
161 // Try if the modifier belongs to the event type name, like some tracepoint events.
162 if (!modifier.empty()) {
163 event_type_name = event_type_str;
164 modifier.clear();
165 event_type = FindEventTypeByName(event_type_name);
166 }
167 if (event_type == nullptr) {
168 return nullptr;
169 }
170 }
171 event_type_modifier->event_type = *event_type;
172 if (modifier.find_first_of("ukh") != std::string::npos) {
173 event_type_modifier->exclude_user = true;
174 event_type_modifier->exclude_kernel = true;
175 event_type_modifier->exclude_hv = true;
176 }
177 if (modifier.find_first_of("GH") != std::string::npos) {
178 event_type_modifier->exclude_guest = true;
179 event_type_modifier->exclude_host = true;
180 }
181
182 for (auto& c : modifier) {
183 switch (c) {
184 case 'u':
185 event_type_modifier->exclude_user = false;
186 break;
187 case 'k':
188 event_type_modifier->exclude_kernel = false;
189 break;
190 case 'h':
191 event_type_modifier->exclude_hv = false;
192 break;
193 case 'G':
194 event_type_modifier->exclude_guest = false;
195 break;
196 case 'H':
197 event_type_modifier->exclude_host = false;
198 break;
199 case 'p':
200 event_type_modifier->precise_ip++;
201 break;
202 case ' ':
203 break;
204 default:
205 LOG(ERROR) << "Unknown event type modifier '" << c << "'";
206 }
207 }
208 event_type_modifier->modifier = modifier;
209 return event_type_modifier;
210 }
211