OSDN Git Service

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