2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "event_type.h"
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>
31 #include "event_attr.h"
34 #define EVENT_TYPE_TABLE_ENTRY(name, type, config, description, limited_arch) \
35 {name, type, config, description, limited_arch},
37 static const std::vector<EventType> static_event_type_array = {
38 #include "event_type_table.h"
41 static std::string tracepoint_events;
43 bool SetTracepointEventsFilePath(const std::string& filepath) {
44 if (!android::base::ReadFileToString(filepath, &tracepoint_events)) {
45 PLOG(ERROR) << "Failed to read " << filepath;
51 std::string GetTracepointEvents() {
53 for (const EventType& event : GetAllEventTypes()) {
54 if (!result.empty()) {
55 result.push_back('\n');
57 result += android::base::StringPrintf("%s %" PRIu64, event.name.c_str(), event.config);
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];
69 CHECK(android::base::ParseUint(items[1].c_str(), &id));
70 result.push_back(EventType(event_name, PERF_TYPE_TRACEPOINT, id, "", ""));
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)) {
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;
92 result.push_back(EventType(system_name + ":" + event_name, PERF_TYPE_TRACEPOINT, id, "", ""));
98 static std::vector<EventType> GetTracepointEventTypes() {
99 std::vector<EventType> result;
100 if (!tracepoint_events.empty()) {
101 result = GetTracepointEventTypesFromString(tracepoint_events);
103 result = GetTracepointEventTypesFromTraceFs();
105 std::sort(result.begin(), result.end(),
106 [](const EventType& type1, const EventType& type2) { return type1.name < type2.name; });
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());
119 return event_type_array;
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;
130 if (result == nullptr) {
131 LOG(ERROR) << "Unknown event_type '" << name
132 << "', try `simpleperf list` to list all possible event type names";
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;
154 if (match_modifier) {
155 event_type_name = event_type_str.substr(0, comm_pos);
156 modifier = event_type_str.substr(comm_pos + 1);
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;
165 event_type = FindEventTypeByName(event_type_name);
167 if (event_type == nullptr) {
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;
177 if (modifier.find_first_of("GH") != std::string::npos) {
178 event_type_modifier->exclude_guest = true;
179 event_type_modifier->exclude_host = true;
182 for (auto& c : modifier) {
185 event_type_modifier->exclude_user = false;
188 event_type_modifier->exclude_kernel = false;
191 event_type_modifier->exclude_hv = false;
194 event_type_modifier->exclude_guest = false;
197 event_type_modifier->exclude_host = false;
200 event_type_modifier->precise_ip++;
205 LOG(ERROR) << "Unknown event type modifier '" << c << "'";
208 event_type_modifier->modifier = modifier;
209 return event_type_modifier;