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