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_attr.h"
18
19 #include <inttypes.h>
20 #include <stdio.h>
21 #include <string>
22 #include <unordered_map>
23
24 #include <android-base/logging.h>
25
26 #include "environment.h"
27 #include "event_type.h"
28 #include "utils.h"
29
BitsToString(const std::string & name,uint64_t bits,const std::vector<std::pair<int,std::string>> & bit_names)30 static 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
SampleTypeToString(uint64_t sample_type)48 static std::string SampleTypeToString(uint64_t sample_type) {
49 static std::vector<std::pair<int, std::string>> sample_type_names = {
50 {PERF_SAMPLE_ADDR, "addr"},
51 {PERF_SAMPLE_BRANCH_STACK, "branch_stack"},
52 {PERF_SAMPLE_CALLCHAIN, "callchain"},
53 {PERF_SAMPLE_CPU, "cpu"},
54 {PERF_SAMPLE_ID, "id"},
55 {PERF_SAMPLE_IP, "ip"},
56 {PERF_SAMPLE_PERIOD, "period"},
57 {PERF_SAMPLE_RAW, "raw"},
58 {PERF_SAMPLE_READ, "read"},
59 {PERF_SAMPLE_REGS_USER, "regs_user"},
60 {PERF_SAMPLE_STACK_USER, "stack_user"},
61 {PERF_SAMPLE_STREAM_ID, "stream_id"},
62 {PERF_SAMPLE_TID, "tid"},
63 {PERF_SAMPLE_TIME, "time"},
64 };
65 return BitsToString("sample_type", sample_type, sample_type_names);
66 }
67
ReadFormatToString(uint64_t read_format)68 static 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
CreateDefaultPerfEventAttr(const EventType & event_type)78 perf_event_attr CreateDefaultPerfEventAttr(const EventType& event_type) {
79 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;
84 attr.disabled = 0;
85 // Changing read_format affects the layout of the data read from perf_event_file, namely
86 // PerfCounter in event_fd.h.
87 attr.read_format =
88 PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING | PERF_FORMAT_ID;
89 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_PERIOD |
90 PERF_SAMPLE_CPU | PERF_SAMPLE_ID;
91
92 if (attr.type == PERF_TYPE_TRACEPOINT) {
93 // Tracepoint information are stored in raw data in sample records.
94 if (CanRecordRawData()) {
95 attr.sample_type |= PERF_SAMPLE_RAW;
96 }
97 }
98 return attr;
99 }
100
DumpPerfEventAttr(const perf_event_attr & attr,size_t indent)101 void DumpPerfEventAttr(const perf_event_attr& attr, size_t indent) {
102 std::string event_name = GetEventNameByAttr(attr);
103 PrintIndented(indent, "event_attr: for event type %s\n", event_name.c_str());
104
105 PrintIndented(indent + 1, "type %u, size %u, config %llu\n", attr.type, attr.size, attr.config);
106
107 if (attr.freq != 0) {
108 PrintIndented(indent + 1, "sample_freq %llu\n", attr.sample_freq);
109 } else {
110 PrintIndented(indent + 1, "sample_period %llu\n", attr.sample_period);
111 }
112
113 PrintIndented(indent + 1, "sample_type (0x%llx) %s\n", attr.sample_type,
114 SampleTypeToString(attr.sample_type).c_str());
115
116 PrintIndented(indent + 1, "read_format (0x%llx) %s\n", attr.read_format,
117 ReadFormatToString(attr.read_format).c_str());
118
119 PrintIndented(indent + 1, "disabled %u, inherit %u, pinned %u, exclusive %u\n", attr.disabled,
120 attr.inherit, attr.pinned, attr.exclusive);
121
122 PrintIndented(indent + 1, "exclude_user %u, exclude_kernel %u, exclude_hv %u\n",
123 attr.exclude_user, attr.exclude_kernel, attr.exclude_hv);
124
125 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);
127
128 PrintIndented(indent + 1, "inherit_stat %u, enable_on_exec %u, task %u\n", attr.inherit_stat,
129 attr.enable_on_exec, attr.task);
130
131 PrintIndented(indent + 1, "watermark %u, precise_ip %u, mmap_data %u\n", attr.watermark,
132 attr.precise_ip, attr.mmap_data);
133
134 PrintIndented(indent + 1, "sample_id_all %u, exclude_host %u, exclude_guest %u\n",
135 attr.sample_id_all, attr.exclude_host, attr.exclude_guest);
136 PrintIndented(indent + 1, "branch_sample_type 0x%" PRIx64 "\n", attr.branch_sample_type);
137 PrintIndented(indent + 1, "exclude_callchain_kernel %u, exclude_callchain_user %u\n",
138 attr.exclude_callchain_kernel, attr.exclude_callchain_user);
139 PrintIndented(indent + 1, "sample_regs_user 0x%" PRIx64 "\n", attr.sample_regs_user);
140 PrintIndented(indent + 1, "sample_stack_user 0x%" PRIx64 "\n", attr.sample_stack_user);
141 }
142
GetCommonEventIdPositionsForAttrs(std::vector<perf_event_attr> & attrs,size_t * event_id_pos_in_sample_records,size_t * event_id_reverse_pos_in_non_sample_records)143 bool GetCommonEventIdPositionsForAttrs(std::vector<perf_event_attr>& attrs,
144 size_t* event_id_pos_in_sample_records,
145 size_t* event_id_reverse_pos_in_non_sample_records) {
146 // When there are more than one perf_event_attrs, we need to read event id
147 // in each record to decide current record should use which attr. So
148 // we need to determine the event id position in a record here.
149 std::vector<uint64_t> sample_types;
150 for (const auto& attr : attrs) {
151 sample_types.push_back(attr.sample_type);
152 }
153 // First determine event_id_pos_in_sample_records.
154 // If PERF_SAMPLE_IDENTIFIER is enabled, it is just after perf_event_header.
155 // If PERF_SAMPLE_ID is enabled, then PERF_SAMPLE_IDENTIFIER | IP | TID | TIME | ADDR
156 // should also be the same.
157 bool identifier_enabled = true;
158 bool id_enabled = true;
159 uint64_t flags_before_id_mask = PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_IP | PERF_SAMPLE_TID |
160 PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR;
161 uint64_t flags_before_id = sample_types[0] & flags_before_id_mask;
162 bool flags_before_id_are_the_same = true;
163 for (auto type : sample_types) {
164 identifier_enabled &= (type & PERF_SAMPLE_IDENTIFIER) != 0;
165 id_enabled &= (type & PERF_SAMPLE_ID) != 0;
166 flags_before_id_are_the_same &= (type & flags_before_id_mask) == flags_before_id;
167 }
168 if (identifier_enabled) {
169 *event_id_pos_in_sample_records = sizeof(perf_event_header);
170 } else if (id_enabled && flags_before_id_are_the_same) {
171 uint64_t pos = sizeof(perf_event_header);
172 while (flags_before_id != 0) {
173 // Each flags takes 8 bytes in sample records.
174 flags_before_id &= flags_before_id - 1;
175 pos += 8;
176 }
177 *event_id_pos_in_sample_records = pos;
178 } else {
179 LOG(ERROR) << "perf_event_attrs don't have a common event id position in sample records";
180 return false;
181 }
182
183 // Secondly determine event_id_reverse_pos_in_non_sample_record.
184 // If sample_id_all is not enabled, there is no event id in non sample records.
185 // If PERF_SAMPLE_IDENTIFIER is enabled, it is at the last 8 bytes of the record.
186 // If PERF_SAMPLE_ID is enabled, then PERF_SAMPLE_IDENTIFIER | CPU | STREAM_ID should
187 // also be the same.
188 bool sample_id_all_enabled = true;
189 for (const auto& attr : attrs) {
190 if (attr.sample_id_all == 0) {
191 sample_id_all_enabled = false;
192 }
193 }
194 if (!sample_id_all_enabled) {
195 LOG(ERROR) << "there are perf_event_attrs not enabling sample_id_all, so can't determine "
196 << "perf_event_attr for non sample records";
197 return false;
198 }
199 uint64_t flags_after_id_mask = PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_CPU | PERF_SAMPLE_STREAM_ID;
200 uint64_t flags_after_id = sample_types[0] & flags_after_id_mask;
201 bool flags_after_id_are_the_same = true;
202 for (auto type : sample_types) {
203 flags_after_id_are_the_same &= (type & flags_after_id_mask) == flags_after_id;
204 }
205 if (identifier_enabled) {
206 *event_id_reverse_pos_in_non_sample_records = 8;
207 } else if (id_enabled && flags_after_id_are_the_same) {
208 uint64_t pos = 8;
209 while (flags_after_id != 0) {
210 // Each flag takes 8 bytes in sample_id of non sample records.
211 flags_after_id &= flags_after_id - 1;
212 pos += 8;
213 }
214 *event_id_reverse_pos_in_non_sample_records = pos;
215 } else {
216 LOG(ERROR) << "perf_event_attrs don't have a common event id reverse position in non sample records";
217 return false;
218 }
219 return true;
220 }
221
IsTimestampSupported(const perf_event_attr & attr)222 bool IsTimestampSupported(const perf_event_attr& attr) {
223 return attr.sample_id_all && (attr.sample_type & PERF_SAMPLE_TIME);
224 }
225
IsCpuSupported(const perf_event_attr & attr)226 bool IsCpuSupported(const perf_event_attr& attr) {
227 return attr.sample_id_all && (attr.sample_type & PERF_SAMPLE_CPU);
228 }
229
GetEventNameByAttr(const perf_event_attr & attr)230 std::string GetEventNameByAttr(const perf_event_attr& attr) {
231 for (const auto& event_type : GetAllEventTypes()) {
232 if (event_type.type == attr.type && event_type.config == attr.config) {
233 std::string name = event_type.name;
234 if (attr.exclude_user && !attr.exclude_kernel) {
235 name += ":k";
236 } else if (attr.exclude_kernel && !attr.exclude_user) {
237 name += ":u";
238 }
239 return name;
240 }
241 }
242 return "unknown";
243 }
244