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"
24 #include <base/file.h>
25 #include <base/logging.h>
27 #include "event_attr.h"
31 #define EVENT_TYPE_TABLE_ENTRY(name, type, config) \
32 { name, type, config } \
35 static const std::vector<EventType> static_event_type_array = {
36 #include "event_type_table.h"
39 static const std::vector<EventType> GetTracepointEventTypes() {
40 std::vector<EventType> result;
41 const std::string tracepoint_dirname = "/sys/kernel/debug/tracing/events";
42 std::vector<std::string> system_dirs;
43 GetEntriesInDir(tracepoint_dirname, nullptr, &system_dirs);
44 for (auto& system_name : system_dirs) {
45 std::string system_path = tracepoint_dirname + "/" + system_name;
46 std::vector<std::string> event_dirs;
47 GetEntriesInDir(system_path, nullptr, &event_dirs);
48 for (auto& event_name : event_dirs) {
49 std::string id_path = system_path + "/" + event_name + "/id";
50 std::string id_content;
51 if (!android::base::ReadFileToString(id_path, &id_content)) {
55 uint64_t id = strtoull(id_content.c_str(), &endptr, 10);
56 if (endptr == id_content.c_str()) {
57 LOG(DEBUG) << "unexpected id '" << id_content << "' in " << id_path;
60 result.push_back(EventType(system_name + ":" + event_name, PERF_TYPE_TRACEPOINT, id));
63 std::sort(result.begin(), result.end(),
64 [](const EventType& type1, const EventType& type2) { return type1.name < type2.name; });
68 const std::vector<EventType>& GetAllEventTypes() {
69 static std::vector<EventType> event_type_array;
70 if (event_type_array.empty()) {
71 event_type_array.insert(event_type_array.end(), static_event_type_array.begin(),
72 static_event_type_array.end());
73 const std::vector<EventType> tracepoint_array = GetTracepointEventTypes();
74 event_type_array.insert(event_type_array.end(), tracepoint_array.begin(),
75 tracepoint_array.end());
77 return event_type_array;
80 const EventType* FindEventTypeByConfig(uint32_t type, uint64_t config) {
81 for (auto& event_type : GetAllEventTypes()) {
82 if (event_type.type == type && event_type.config == config) {
89 const EventType* FindEventTypeByName(const std::string& name) {
90 const EventType* result = nullptr;
91 for (auto& event_type : GetAllEventTypes()) {
92 if (event_type.name == name) {
97 if (result == nullptr) {
98 LOG(ERROR) << "Unknown event_type '" << name
99 << "', try `simpleperf list` to list all possible event type names";
105 std::unique_ptr<EventTypeAndModifier> ParseEventType(const std::string& event_type_str) {
106 static std::string modifier_characters = "ukhGHp";
107 std::unique_ptr<EventTypeAndModifier> event_type_modifier(new EventTypeAndModifier);
108 std::string name = event_type_str;
109 std::string modifier;
110 size_t comm_pos = event_type_str.rfind(':');
111 if (comm_pos != std::string::npos) {
112 bool match_modifier = true;
113 for (size_t i = comm_pos + 1; i < event_type_str.size(); ++i) {
114 char c = event_type_str[i];
115 if (c != ' ' && modifier_characters.find(c) == std::string::npos) {
116 match_modifier = false;
120 if (match_modifier) {
121 name = event_type_str.substr(0, comm_pos);
122 modifier = event_type_str.substr(comm_pos + 1);
125 const EventType* event_type = FindEventTypeByName(name);
126 if (event_type == nullptr) {
127 // Try if the modifier belongs to the event type name, like some tracepoint events.
128 if (!modifier.empty()) {
129 name = event_type_str;
131 event_type = FindEventTypeByName(name);
133 if (event_type == nullptr) {
137 event_type_modifier->event_type = *event_type;
138 if (modifier.find_first_of("ukh") != std::string::npos) {
139 event_type_modifier->exclude_user = true;
140 event_type_modifier->exclude_kernel = true;
141 event_type_modifier->exclude_hv = true;
143 if (modifier.find_first_of("GH") != std::string::npos) {
144 event_type_modifier->exclude_guest = true;
145 event_type_modifier->exclude_host = true;
148 for (auto& c : modifier) {
151 event_type_modifier->exclude_user = false;
154 event_type_modifier->exclude_kernel = false;
157 event_type_modifier->exclude_hv = false;
160 event_type_modifier->exclude_guest = false;
163 event_type_modifier->exclude_host = false;
166 event_type_modifier->precise_ip++;
171 LOG(ERROR) << "Unknown event type modifier '" << c << "'";
174 return event_type_modifier;