event_type.cpp revision 9fd3cc1048a3d0338df4a48760dfd655560992a1
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
35static const std::vector<EventType> static_event_type_array = {
36#include "event_type_table.h"
37};
38
39static 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
68const 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
80const 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
89const 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
105std::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;
117        break;
118      }
119    }
120    if (match_modifier) {
121      name = event_type_str.substr(0, comm_pos);
122      modifier = event_type_str.substr(comm_pos + 1);
123    }
124  }
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;
130      modifier.clear();
131      event_type = FindEventTypeByName(name);
132    }
133    if (event_type == nullptr) {
134      return nullptr;
135    }
136  }
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;
142  }
143  if (modifier.find_first_of("GH") != std::string::npos) {
144    event_type_modifier->exclude_guest = true;
145    event_type_modifier->exclude_host = true;
146  }
147
148  for (auto& c : modifier) {
149    switch (c) {
150      case 'u':
151        event_type_modifier->exclude_user = false;
152        break;
153      case 'k':
154        event_type_modifier->exclude_kernel = false;
155        break;
156      case 'h':
157        event_type_modifier->exclude_hv = false;
158        break;
159      case 'G':
160        event_type_modifier->exclude_guest = false;
161        break;
162      case 'H':
163        event_type_modifier->exclude_host = false;
164        break;
165      case 'p':
166        event_type_modifier->precise_ip++;
167        break;
168      case ' ':
169        break;
170      default:
171        LOG(ERROR) << "Unknown event type modifier '" << c << "'";
172    }
173  }
174  return event_type_modifier;
175}
176