blob: ecc084399e88e78f86b2dd03d64192bb609b9fa0 [file] [log] [blame]
Yabin Cui67d3abd2015-04-16 15:26:31 -07001/*
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_attr.h"
18
19#include <inttypes.h>
20#include <stdio.h>
21#include <string>
22#include <unordered_map>
23
Elliott Hughes66dd09e2015-12-04 14:00:57 -080024#include <android-base/logging.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070025
Yabin Cuif8974522017-07-17 14:36:37 -070026#include "environment.h"
Yabin Cui67d3abd2015-04-16 15:26:31 -070027#include "event_type.h"
28#include "utils.h"
29
Yabin Cui9759e1b2015-04-28 15:54:13 -070030static std::string BitsToString(const std::string& name, uint64_t bits,
31 const std::vector<std::pair<int, std::string>>& bit_names) {
32 std::string result;
33 for (auto& p : bit_names) {
34 if (bits & p.first) {
35 bits &= ~p.first;
36 if (!result.empty()) {
37 result += ", ";
38 }
39 result += p.second;
40 }
41 }
42 if (bits != 0) {
43 LOG(DEBUG) << "unknown " << name << " bits: " << std::hex << bits;
44 }
45 return result;
46}
47
Yabin Cui67d3abd2015-04-16 15:26:31 -070048static std::string SampleTypeToString(uint64_t sample_type) {
Yabin Cui9759e1b2015-04-28 15:54:13 -070049 static std::vector<std::pair<int, std::string>> sample_type_names = {
Yabin Cuif569b472015-04-30 09:43:26 -070050 {PERF_SAMPLE_ADDR, "addr"},
Yabin Cui76769e52015-07-13 12:23:54 -070051 {PERF_SAMPLE_BRANCH_STACK, "branch_stack"},
Yabin Cuif569b472015-04-30 09:43:26 -070052 {PERF_SAMPLE_CALLCHAIN, "callchain"},
53 {PERF_SAMPLE_CPU, "cpu"},
54 {PERF_SAMPLE_ID, "id"},
Yabin Cui67d3abd2015-04-16 15:26:31 -070055 {PERF_SAMPLE_IP, "ip"},
Yabin Cuif569b472015-04-30 09:43:26 -070056 {PERF_SAMPLE_PERIOD, "period"},
57 {PERF_SAMPLE_RAW, "raw"},
58 {PERF_SAMPLE_READ, "read"},
Yabin Cui76769e52015-07-13 12:23:54 -070059 {PERF_SAMPLE_REGS_USER, "regs_user"},
60 {PERF_SAMPLE_STACK_USER, "stack_user"},
Yabin Cuif569b472015-04-30 09:43:26 -070061 {PERF_SAMPLE_STREAM_ID, "stream_id"},
Yabin Cui67d3abd2015-04-16 15:26:31 -070062 {PERF_SAMPLE_TID, "tid"},
63 {PERF_SAMPLE_TIME, "time"},
Yabin Cui67d3abd2015-04-16 15:26:31 -070064 };
Yabin Cui9759e1b2015-04-28 15:54:13 -070065 return BitsToString("sample_type", sample_type, sample_type_names);
Yabin Cui67d3abd2015-04-16 15:26:31 -070066}
67
Yabin Cui9759e1b2015-04-28 15:54:13 -070068static std::string ReadFormatToString(uint64_t read_format) {
69 static std::vector<std::pair<int, std::string>> read_format_names = {
70 {PERF_FORMAT_TOTAL_TIME_ENABLED, "total_time_enabled"},
71 {PERF_FORMAT_TOTAL_TIME_RUNNING, "total_time_running"},
72 {PERF_FORMAT_ID, "id"},
73 {PERF_FORMAT_GROUP, "group"},
74 };
75 return BitsToString("read_format", read_format, read_format_names);
76}
77
78perf_event_attr CreateDefaultPerfEventAttr(const EventType& event_type) {
Yabin Cui67d3abd2015-04-16 15:26:31 -070079 perf_event_attr attr;
80 memset(&attr, 0, sizeof(attr));
81 attr.size = sizeof(perf_event_attr);
82 attr.type = event_type.type;
83 attr.config = event_type.config;
Yabin Cuia46794f2015-12-03 10:31:37 -080084 attr.disabled = 0;
Yabin Cui323e9452015-04-20 18:07:17 -070085 // Changing read_format affects the layout of the data read from perf_event_file, namely
86 // PerfCounter in event_fd.h.
Yabin Cui67d3abd2015-04-16 15:26:31 -070087 attr.read_format =
88 PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING | PERF_FORMAT_ID;
Yabin Cui2d6efe42016-04-01 20:22:35 -070089 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_PERIOD |
90 PERF_SAMPLE_CPU | PERF_SAMPLE_ID;
Yabin Cuif569b472015-04-30 09:43:26 -070091
92 if (attr.type == PERF_TYPE_TRACEPOINT) {
Yabin Cuibfc11b62015-08-19 10:12:51 -070093 // Tracepoint information are stored in raw data in sample records.
Yabin Cuif8974522017-07-17 14:36:37 -070094 if (CanRecordRawData()) {
95 attr.sample_type |= PERF_SAMPLE_RAW;
96 }
Yabin Cuif569b472015-04-30 09:43:26 -070097 }
Yabin Cui9759e1b2015-04-28 15:54:13 -070098 return attr;
Yabin Cui67d3abd2015-04-16 15:26:31 -070099}
100
Yabin Cui9759e1b2015-04-28 15:54:13 -0700101void DumpPerfEventAttr(const perf_event_attr& attr, size_t indent) {
Yabin Cuic10a9dc2016-06-15 12:10:33 -0700102 std::string event_name = GetEventNameByAttr(attr);
Yabin Cui2672dea2015-05-21 12:17:23 -0700103 PrintIndented(indent, "event_attr: for event type %s\n", event_name.c_str());
Yabin Cui67d3abd2015-04-16 15:26:31 -0700104
Yabin Cui9759e1b2015-04-28 15:54:13 -0700105 PrintIndented(indent + 1, "type %u, size %u, config %llu\n", attr.type, attr.size, attr.config);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700106
Yabin Cui9759e1b2015-04-28 15:54:13 -0700107 if (attr.freq != 0) {
108 PrintIndented(indent + 1, "sample_freq %llu\n", attr.sample_freq);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700109 } else {
Yabin Cui9759e1b2015-04-28 15:54:13 -0700110 PrintIndented(indent + 1, "sample_period %llu\n", attr.sample_period);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700111 }
112
Yabin Cui9759e1b2015-04-28 15:54:13 -0700113 PrintIndented(indent + 1, "sample_type (0x%llx) %s\n", attr.sample_type,
114 SampleTypeToString(attr.sample_type).c_str());
Yabin Cui67d3abd2015-04-16 15:26:31 -0700115
Yabin Cui9759e1b2015-04-28 15:54:13 -0700116 PrintIndented(indent + 1, "read_format (0x%llx) %s\n", attr.read_format,
117 ReadFormatToString(attr.read_format).c_str());
Yabin Cui67d3abd2015-04-16 15:26:31 -0700118
Yabin Cui41d4ba92015-06-22 12:27:58 -0700119 PrintIndented(indent + 1, "disabled %u, inherit %u, pinned %u, exclusive %u\n", attr.disabled,
120 attr.inherit, attr.pinned, attr.exclusive);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700121
Yabin Cuid115b2f2015-06-15 13:57:23 -0700122 PrintIndented(indent + 1, "exclude_user %u, exclude_kernel %u, exclude_hv %u\n",
Yabin Cui9759e1b2015-04-28 15:54:13 -0700123 attr.exclude_user, attr.exclude_kernel, attr.exclude_hv);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700124
Yabin Cui2db05b42018-07-16 14:04:49 -0700125 PrintIndented(indent + 1, "exclude_idle %u, mmap %u, mmap2 %u, comm %u, freq %u\n",
126 attr.exclude_idle, attr.mmap, attr.mmap2, attr.comm, attr.freq);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700127
Yabin Cui41d4ba92015-06-22 12:27:58 -0700128 PrintIndented(indent + 1, "inherit_stat %u, enable_on_exec %u, task %u\n", attr.inherit_stat,
129 attr.enable_on_exec, attr.task);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700130
Yabin Cuid115b2f2015-06-15 13:57:23 -0700131 PrintIndented(indent + 1, "watermark %u, precise_ip %u, mmap_data %u\n", attr.watermark,
Yabin Cui9759e1b2015-04-28 15:54:13 -0700132 attr.precise_ip, attr.mmap_data);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700133
Yabin Cuid115b2f2015-06-15 13:57:23 -0700134 PrintIndented(indent + 1, "sample_id_all %u, exclude_host %u, exclude_guest %u\n",
Yabin Cui9759e1b2015-04-28 15:54:13 -0700135 attr.sample_id_all, attr.exclude_host, attr.exclude_guest);
Yabin Cuid5bccd12019-07-12 13:09:15 -0700136 PrintIndented(indent + 1, "config2 0x%llx\n", attr.config2);
Yabin Cui76769e52015-07-13 12:23:54 -0700137 PrintIndented(indent + 1, "branch_sample_type 0x%" PRIx64 "\n", attr.branch_sample_type);
Yabin Cui3c8c2132015-08-13 20:30:20 -0700138 PrintIndented(indent + 1, "exclude_callchain_kernel %u, exclude_callchain_user %u\n",
139 attr.exclude_callchain_kernel, attr.exclude_callchain_user);
Yabin Cui76769e52015-07-13 12:23:54 -0700140 PrintIndented(indent + 1, "sample_regs_user 0x%" PRIx64 "\n", attr.sample_regs_user);
141 PrintIndented(indent + 1, "sample_stack_user 0x%" PRIx64 "\n", attr.sample_stack_user);
Yabin Cui67d3abd2015-04-16 15:26:31 -0700142}
Yabin Cui2d6efe42016-04-01 20:22:35 -0700143
144bool GetCommonEventIdPositionsForAttrs(std::vector<perf_event_attr>& attrs,
145 size_t* event_id_pos_in_sample_records,
146 size_t* event_id_reverse_pos_in_non_sample_records) {
147 // When there are more than one perf_event_attrs, we need to read event id
148 // in each record to decide current record should use which attr. So
149 // we need to determine the event id position in a record here.
150 std::vector<uint64_t> sample_types;
151 for (const auto& attr : attrs) {
152 sample_types.push_back(attr.sample_type);
153 }
154 // First determine event_id_pos_in_sample_records.
155 // If PERF_SAMPLE_IDENTIFIER is enabled, it is just after perf_event_header.
156 // If PERF_SAMPLE_ID is enabled, then PERF_SAMPLE_IDENTIFIER | IP | TID | TIME | ADDR
157 // should also be the same.
158 bool identifier_enabled = true;
159 bool id_enabled = true;
160 uint64_t flags_before_id_mask = PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_IP | PERF_SAMPLE_TID |
161 PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR;
162 uint64_t flags_before_id = sample_types[0] & flags_before_id_mask;
163 bool flags_before_id_are_the_same = true;
164 for (auto type : sample_types) {
165 identifier_enabled &= (type & PERF_SAMPLE_IDENTIFIER) != 0;
166 id_enabled &= (type & PERF_SAMPLE_ID) != 0;
167 flags_before_id_are_the_same &= (type & flags_before_id_mask) == flags_before_id;
168 }
169 if (identifier_enabled) {
170 *event_id_pos_in_sample_records = sizeof(perf_event_header);
171 } else if (id_enabled && flags_before_id_are_the_same) {
172 uint64_t pos = sizeof(perf_event_header);
173 while (flags_before_id != 0) {
174 // Each flags takes 8 bytes in sample records.
175 flags_before_id &= flags_before_id - 1;
176 pos += 8;
177 }
178 *event_id_pos_in_sample_records = pos;
179 } else {
180 LOG(ERROR) << "perf_event_attrs don't have a common event id position in sample records";
181 return false;
182 }
183
184 // Secondly determine event_id_reverse_pos_in_non_sample_record.
185 // If sample_id_all is not enabled, there is no event id in non sample records.
186 // If PERF_SAMPLE_IDENTIFIER is enabled, it is at the last 8 bytes of the record.
187 // If PERF_SAMPLE_ID is enabled, then PERF_SAMPLE_IDENTIFIER | CPU | STREAM_ID should
188 // also be the same.
189 bool sample_id_all_enabled = true;
190 for (const auto& attr : attrs) {
191 if (attr.sample_id_all == 0) {
192 sample_id_all_enabled = false;
193 }
194 }
195 if (!sample_id_all_enabled) {
196 LOG(ERROR) << "there are perf_event_attrs not enabling sample_id_all, so can't determine "
197 << "perf_event_attr for non sample records";
198 return false;
199 }
200 uint64_t flags_after_id_mask = PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_CPU | PERF_SAMPLE_STREAM_ID;
201 uint64_t flags_after_id = sample_types[0] & flags_after_id_mask;
202 bool flags_after_id_are_the_same = true;
203 for (auto type : sample_types) {
204 flags_after_id_are_the_same &= (type & flags_after_id_mask) == flags_after_id;
205 }
206 if (identifier_enabled) {
207 *event_id_reverse_pos_in_non_sample_records = 8;
208 } else if (id_enabled && flags_after_id_are_the_same) {
209 uint64_t pos = 8;
210 while (flags_after_id != 0) {
211 // Each flag takes 8 bytes in sample_id of non sample records.
212 flags_after_id &= flags_after_id - 1;
213 pos += 8;
214 }
215 *event_id_reverse_pos_in_non_sample_records = pos;
216 } else {
217 LOG(ERROR) << "perf_event_attrs don't have a common event id reverse position in non sample records";
218 return false;
219 }
220 return true;
221}
222
223bool IsTimestampSupported(const perf_event_attr& attr) {
224 return attr.sample_id_all && (attr.sample_type & PERF_SAMPLE_TIME);
225}
Yabin Cuic10a9dc2016-06-15 12:10:33 -0700226
Yabin Cuifc22b8f2016-08-04 14:47:50 -0700227bool IsCpuSupported(const perf_event_attr& attr) {
228 return attr.sample_id_all && (attr.sample_type & PERF_SAMPLE_CPU);
229}
230
Yabin Cuic10a9dc2016-06-15 12:10:33 -0700231std::string GetEventNameByAttr(const perf_event_attr& attr) {
232 for (const auto& event_type : GetAllEventTypes()) {
Yabin Cuid5bccd12019-07-12 13:09:15 -0700233 // An event type uses both type and config value to define itself. But etm event type
234 // only uses type value (whose config value is used to set etm options).
235 if (event_type.type == attr.type &&
Yabin Cui1172d7e2019-07-25 15:12:58 -0700236 (event_type.config == attr.config || IsEtmEventType(event_type.type))) {
Yabin Cuic10a9dc2016-06-15 12:10:33 -0700237 std::string name = event_type.name;
238 if (attr.exclude_user && !attr.exclude_kernel) {
239 name += ":k";
240 } else if (attr.exclude_kernel && !attr.exclude_user) {
241 name += ":u";
242 }
243 return name;
244 }
245 }
246 return "unknown";
247}