OSDN Git Service

Merge "Simpleperf: fix the process of parsing records."
[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 <base/file.h>
25 #include <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) \
32   { name, type, config }                           \
33   ,
34
35 static const std::vector<EventType> static_event_type_array = {
36 #include "event_type_table.h"
37 };
38
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)) {
52         continue;
53       }
54       char* endptr;
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;
58         continue;
59       }
60       result.push_back(EventType(system_name + ":" + event_name, PERF_TYPE_TRACEPOINT, id));
61     }
62   }
63   std::sort(result.begin(), result.end(),
64             [](const EventType& type1, const EventType& type2) { return type1.name < type2.name; });
65   return result;
66 }
67
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());
76   }
77   return event_type_array;
78 }
79
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) {
83       return &event_type;
84     }
85   }
86   return nullptr;
87 }
88
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) {
93       result = &event_type;
94       break;
95     }
96   }
97   if (result == nullptr) {
98     LOG(ERROR) << "Unknown event_type '" << name
99                << "', try `simpleperf list` to list all possible event type names";
100     return nullptr;
101   }
102   return result;
103 }
104
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   event_type_modifier->name = event_type_str;
109   std::string event_type_name = event_type_str;
110   std::string modifier;
111   size_t comm_pos = event_type_str.rfind(':');
112   if (comm_pos != std::string::npos) {
113     bool match_modifier = true;
114     for (size_t i = comm_pos + 1; i < event_type_str.size(); ++i) {
115       char c = event_type_str[i];
116       if (c != ' ' && modifier_characters.find(c) == std::string::npos) {
117         match_modifier = false;
118         break;
119       }
120     }
121     if (match_modifier) {
122       event_type_name = event_type_str.substr(0, comm_pos);
123       modifier = event_type_str.substr(comm_pos + 1);
124     }
125   }
126   const EventType* event_type = FindEventTypeByName(event_type_name);
127   if (event_type == nullptr) {
128     // Try if the modifier belongs to the event type name, like some tracepoint events.
129     if (!modifier.empty()) {
130       event_type_name = event_type_str;
131       modifier.clear();
132       event_type = FindEventTypeByName(event_type_name);
133     }
134     if (event_type == nullptr) {
135       return nullptr;
136     }
137   }
138   event_type_modifier->event_type = *event_type;
139   if (modifier.find_first_of("ukh") != std::string::npos) {
140     event_type_modifier->exclude_user = true;
141     event_type_modifier->exclude_kernel = true;
142     event_type_modifier->exclude_hv = true;
143   }
144   if (modifier.find_first_of("GH") != std::string::npos) {
145     event_type_modifier->exclude_guest = true;
146     event_type_modifier->exclude_host = true;
147   }
148
149   for (auto& c : modifier) {
150     switch (c) {
151       case 'u':
152         event_type_modifier->exclude_user = false;
153         break;
154       case 'k':
155         event_type_modifier->exclude_kernel = false;
156         break;
157       case 'h':
158         event_type_modifier->exclude_hv = false;
159         break;
160       case 'G':
161         event_type_modifier->exclude_guest = false;
162         break;
163       case 'H':
164         event_type_modifier->exclude_host = false;
165         break;
166       case 'p':
167         event_type_modifier->precise_ip++;
168         break;
169       case ' ':
170         break;
171       default:
172         LOG(ERROR) << "Unknown event type modifier '" << c << "'";
173     }
174   }
175   return event_type_modifier;
176 }