• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (auto& event : GetAllEventTypes()) {
54     if (event.type != PERF_TYPE_TRACEPOINT) {
55       continue;
56     }
57     if (!result.empty()) {
58       result.push_back('\n');
59     }
60     result += android::base::StringPrintf("%s %" PRIu64, event.name.c_str(), event.config);
61   }
62   return result;
63 }
64 
GetTracepointEventTypesFromString(const std::string & s)65 static std::vector<EventType> GetTracepointEventTypesFromString(const std::string& s) {
66   std::vector<EventType> result;
67   for (auto& line : android::base::Split(s, "\n")) {
68     std::vector<std::string> items = android::base::Split(line, " ");
69     CHECK_EQ(items.size(), 2u);
70     std::string event_name = items[0];
71     uint64_t id;
72     CHECK(android::base::ParseUint(items[1].c_str(), &id));
73     result.push_back(EventType(event_name, PERF_TYPE_TRACEPOINT, id, "", ""));
74   }
75   return result;
76 }
77 
GetTracepointEventTypesFromTraceFs()78 static std::vector<EventType> GetTracepointEventTypesFromTraceFs() {
79   std::vector<EventType> result;
80   const std::string tracepoint_dirname = "/sys/kernel/debug/tracing/events";
81   for (const auto& system_name : GetSubDirs(tracepoint_dirname)) {
82     std::string system_path = tracepoint_dirname + "/" + system_name;
83     for (const auto& event_name : GetSubDirs(system_path)) {
84       std::string id_path = system_path + "/" + event_name + "/id";
85       std::string id_content;
86       if (!android::base::ReadFileToString(id_path, &id_content)) {
87         continue;
88       }
89       char* endptr;
90       uint64_t id = strtoull(id_content.c_str(), &endptr, 10);
91       if (endptr == id_content.c_str()) {
92         LOG(DEBUG) << "unexpected id '" << id_content << "' in " << id_path;
93         continue;
94       }
95       result.push_back(EventType(system_name + ":" + event_name, PERF_TYPE_TRACEPOINT, id, "", ""));
96     }
97   }
98   return result;
99 }
100 
GetTracepointEventTypes()101 static std::vector<EventType> GetTracepointEventTypes() {
102   std::vector<EventType> result;
103   if (!tracepoint_events.empty()) {
104     result = GetTracepointEventTypesFromString(tracepoint_events);
105   } else {
106     result = GetTracepointEventTypesFromTraceFs();
107   }
108   std::sort(result.begin(), result.end(),
109             [](const EventType& type1, const EventType& type2) { return type1.name < type2.name; });
110   return result;
111 }
112 
113 static std::set<EventType> g_event_types;
114 
BuildString(const std::vector<const EventType * > & event_types)115 std::string ScopedEventTypes::BuildString(const std::vector<const EventType*>& event_types) {
116   std::string result;
117   for (auto type : event_types) {
118     if (!result.empty()) {
119       result.push_back('\n');
120     }
121     result += android::base::StringPrintf("%s,%u,%" PRIu64, type->name.c_str(), type->type,
122                                           type->config);
123   }
124   return result;
125 }
126 
ScopedEventTypes(const std::string & event_type_str)127 ScopedEventTypes::ScopedEventTypes(const std::string& event_type_str) {
128   saved_event_types_ = std::move(g_event_types);
129   g_event_types.clear();
130   for (auto& s : android::base::Split(event_type_str, "\n")) {
131     std::string name = s.substr(0, s.find(','));
132     uint32_t type;
133     uint64_t config;
134     sscanf(s.c_str() + name.size(), ",%u,%" PRIu64, &type, &config);
135     g_event_types.emplace(name, type, config, "", "");
136   }
137 }
138 
~ScopedEventTypes()139 ScopedEventTypes::~ScopedEventTypes() {
140   g_event_types = std::move(saved_event_types_);
141 }
142 
GetAllEventTypes()143 const std::set<EventType>& GetAllEventTypes() {
144   if (g_event_types.empty()) {
145     g_event_types.insert(static_event_type_array.begin(), static_event_type_array.end());
146     std::vector<EventType> tracepoint_array = GetTracepointEventTypes();
147     g_event_types.insert(tracepoint_array.begin(), tracepoint_array.end());
148   }
149   return g_event_types;
150 }
151 
FindEventTypeByName(const std::string & name,bool report_error)152 const EventType* FindEventTypeByName(const std::string& name, bool report_error) {
153   const auto& event_types = GetAllEventTypes();
154   auto it = event_types.find(EventType(name, 0, 0, "", ""));
155   if (it != event_types.end()) {
156     return &*it;
157   }
158   if (!name.empty() && name[0] == 'r') {
159     char* end;
160     uint64_t config = strtoull(&name[1], &end, 16);
161     if (end != &name[1] && *end == '\0') {
162       auto result = g_event_types.emplace(name, PERF_TYPE_RAW, config, "", "");
163       CHECK(result.second);
164       return &*(result.first);
165     }
166   }
167   if (report_error) {
168     LOG(ERROR) << "Unknown event_type '" << name
169                << "', try `simpleperf list` to list all possible event type names";
170   }
171   return nullptr;
172 }
173 
ParseEventType(const std::string & event_type_str)174 std::unique_ptr<EventTypeAndModifier> ParseEventType(const std::string& event_type_str) {
175   static std::string modifier_characters = "ukhGHp";
176   std::unique_ptr<EventTypeAndModifier> event_type_modifier(new EventTypeAndModifier);
177   event_type_modifier->name = event_type_str;
178   std::string event_type_name = event_type_str;
179   std::string modifier;
180   size_t comm_pos = event_type_str.rfind(':');
181   if (comm_pos != std::string::npos) {
182     bool match_modifier = true;
183     for (size_t i = comm_pos + 1; i < event_type_str.size(); ++i) {
184       char c = event_type_str[i];
185       if (c != ' ' && modifier_characters.find(c) == std::string::npos) {
186         match_modifier = false;
187         break;
188       }
189     }
190     if (match_modifier) {
191       event_type_name = event_type_str.substr(0, comm_pos);
192       modifier = event_type_str.substr(comm_pos + 1);
193     }
194   }
195   const EventType* event_type = FindEventTypeByName(event_type_name);
196   if (event_type == nullptr) {
197     // Try if the modifier belongs to the event type name, like some tracepoint events.
198     if (!modifier.empty()) {
199       event_type_name = event_type_str;
200       modifier.clear();
201       event_type = FindEventTypeByName(event_type_name);
202     }
203     if (event_type == nullptr) {
204       return nullptr;
205     }
206   }
207   event_type_modifier->event_type = *event_type;
208   if (modifier.find_first_of("ukh") != std::string::npos) {
209     event_type_modifier->exclude_user = true;
210     event_type_modifier->exclude_kernel = true;
211     event_type_modifier->exclude_hv = true;
212   }
213   if (modifier.find_first_of("GH") != std::string::npos) {
214     event_type_modifier->exclude_guest = true;
215     event_type_modifier->exclude_host = true;
216   }
217 
218   for (auto& c : modifier) {
219     switch (c) {
220       case 'u':
221         event_type_modifier->exclude_user = false;
222         break;
223       case 'k':
224         event_type_modifier->exclude_kernel = false;
225         break;
226       case 'h':
227         event_type_modifier->exclude_hv = false;
228         break;
229       case 'G':
230         event_type_modifier->exclude_guest = false;
231         break;
232       case 'H':
233         event_type_modifier->exclude_host = false;
234         break;
235       case 'p':
236         event_type_modifier->precise_ip++;
237         break;
238       case ' ':
239         break;
240       default:
241         LOG(ERROR) << "Unknown event type modifier '" << c << "'";
242     }
243   }
244   event_type_modifier->modifier = modifier;
245   return event_type_modifier;
246 }
247