2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <unordered_map>
22 #include <base/logging.h>
23 #include <base/stringprintf.h>
25 #include "environment.h"
28 static std::string RecordTypeToString(int record_type) {
29 static std::unordered_map<int, std::string> record_type_names = {
30 {PERF_RECORD_MMAP, "mmap"},
31 {PERF_RECORD_LOST, "lost"},
32 {PERF_RECORD_COMM, "comm"},
33 {PERF_RECORD_EXIT, "exit"},
34 {PERF_RECORD_THROTTLE, "throttle"},
35 {PERF_RECORD_UNTHROTTLE, "unthrottle"},
36 {PERF_RECORD_FORK, "fork"},
37 {PERF_RECORD_READ, "read"},
38 {PERF_RECORD_SAMPLE, "sample"},
39 {PERF_RECORD_BUILD_ID, "build_id"},
42 auto it = record_type_names.find(record_type);
43 if (it != record_type_names.end()) {
46 return android::base::StringPrintf("unknown(%d)", record_type);
50 void MoveFromBinaryFormat(T& data, const char*& p) {
51 data = *reinterpret_cast<const T*>(p);
56 void MoveToBinaryFormat(const T& data, char*& p) {
57 *reinterpret_cast<T*>(p) = data;
61 SampleId::SampleId() {
62 memset(this, 0, sizeof(SampleId));
65 // Return sample_id size in binary format.
66 size_t SampleId::CreateContent(const perf_event_attr& attr) {
67 sample_id_all = attr.sample_id_all;
68 sample_type = attr.sample_type;
69 // Other data are not necessary. TODO: Set missing SampleId data.
72 if (sample_type & PERF_SAMPLE_TID) {
73 size += sizeof(PerfSampleTidType);
75 if (sample_type & PERF_SAMPLE_TIME) {
76 size += sizeof(PerfSampleTimeType);
78 if (sample_type & PERF_SAMPLE_ID) {
79 size += sizeof(PerfSampleIdType);
81 if (sample_type & PERF_SAMPLE_STREAM_ID) {
82 size += sizeof(PerfSampleStreamIdType);
84 if (sample_type & PERF_SAMPLE_CPU) {
85 size += sizeof(PerfSampleCpuType);
91 void SampleId::ReadFromBinaryFormat(const perf_event_attr& attr, const char* p, const char* end) {
92 sample_id_all = attr.sample_id_all;
93 sample_type = attr.sample_type;
95 if (sample_type & PERF_SAMPLE_TID) {
96 MoveFromBinaryFormat(tid_data, p);
98 if (sample_type & PERF_SAMPLE_TIME) {
99 MoveFromBinaryFormat(time_data, p);
101 if (sample_type & PERF_SAMPLE_ID) {
102 MoveFromBinaryFormat(id_data, p);
104 if (sample_type & PERF_SAMPLE_STREAM_ID) {
105 MoveFromBinaryFormat(stream_id_data, p);
107 if (sample_type & PERF_SAMPLE_CPU) {
108 MoveFromBinaryFormat(cpu_data, p);
110 // TODO: Add parsing of PERF_SAMPLE_IDENTIFIER.
114 LOG(DEBUG) << "Record SampleId part has " << end - p << " bytes left\n";
118 void SampleId::WriteToBinaryFormat(char*& p) const {
120 if (sample_type & PERF_SAMPLE_TID) {
121 MoveToBinaryFormat(tid_data, p);
123 if (sample_type & PERF_SAMPLE_TIME) {
124 MoveToBinaryFormat(time_data, p);
126 if (sample_type & PERF_SAMPLE_ID) {
127 MoveToBinaryFormat(id_data, p);
129 if (sample_type & PERF_SAMPLE_STREAM_ID) {
130 MoveToBinaryFormat(stream_id_data, p);
132 if (sample_type & PERF_SAMPLE_CPU) {
133 MoveToBinaryFormat(cpu_data, p);
138 void SampleId::Dump(size_t indent) const {
140 if (sample_type & PERF_SAMPLE_TID) {
141 PrintIndented(indent, "sample_id: pid %u, tid %u\n", tid_data.pid, tid_data.tid);
143 if (sample_type & PERF_SAMPLE_TIME) {
144 PrintIndented(indent, "sample_id: time %" PRId64 "\n", time_data.time);
146 if (sample_type & PERF_SAMPLE_ID) {
147 PrintIndented(indent, "sample_id: stream_id %" PRId64 "\n", id_data.id);
149 if (sample_type & PERF_SAMPLE_STREAM_ID) {
150 PrintIndented(indent, "sample_id: stream_id %" PRId64 "\n", stream_id_data.stream_id);
152 if (sample_type & PERF_SAMPLE_CPU) {
153 PrintIndented(indent, "sample_id: cpu %u, res %u\n", cpu_data.cpu, cpu_data.res);
159 memset(&header, 0, sizeof(header));
162 Record::Record(const perf_event_header* pheader) {
166 void Record::Dump(size_t indent) const {
167 PrintIndented(indent, "record %s: type %u, misc %u, size %u\n",
168 RecordTypeToString(header.type).c_str(), header.type, header.misc, header.size);
169 DumpData(indent + 1);
170 sample_id.Dump(indent + 1);
173 MmapRecord::MmapRecord(const perf_event_attr& attr, const perf_event_header* pheader)
175 const char* p = reinterpret_cast<const char*>(pheader + 1);
176 const char* end = reinterpret_cast<const char*>(pheader) + pheader->size;
177 MoveFromBinaryFormat(data, p);
179 p += ALIGN(filename.size() + 1, 8);
181 sample_id.ReadFromBinaryFormat(attr, p, end);
184 void MmapRecord::DumpData(size_t indent) const {
185 PrintIndented(indent, "pid %u, tid %u, addr %p, len 0x%" PRIx64 "\n", data.pid, data.tid,
186 reinterpret_cast<void*>(data.addr), data.len);
187 PrintIndented(indent, "pgoff 0x%" PRIx64 ", filename %s\n", data.pgoff, filename.c_str());
190 std::vector<char> MmapRecord::BinaryFormat() const {
191 std::vector<char> buf(header.size);
192 char* p = buf.data();
193 MoveToBinaryFormat(header, p);
194 MoveToBinaryFormat(data, p);
195 strcpy(p, filename.c_str());
196 p += ALIGN(filename.size() + 1, 8);
197 sample_id.WriteToBinaryFormat(p);
201 CommRecord::CommRecord(const perf_event_attr& attr, const perf_event_header* pheader)
203 const char* p = reinterpret_cast<const char*>(pheader + 1);
204 const char* end = reinterpret_cast<const char*>(pheader) + pheader->size;
205 MoveFromBinaryFormat(data, p);
207 p += ALIGN(strlen(p) + 1, 8);
209 sample_id.ReadFromBinaryFormat(attr, p, end);
212 void CommRecord::DumpData(size_t indent) const {
213 PrintIndented(indent, "pid %u, tid %u, comm %s\n", data.pid, data.tid, comm.c_str());
216 std::vector<char> CommRecord::BinaryFormat() const {
217 std::vector<char> buf(header.size);
218 char* p = buf.data();
219 MoveToBinaryFormat(header, p);
220 MoveToBinaryFormat(data, p);
221 strcpy(p, comm.c_str());
222 p += ALIGN(comm.size() + 1, 8);
223 sample_id.WriteToBinaryFormat(p);
227 ExitRecord::ExitRecord(const perf_event_attr& attr, const perf_event_header* pheader)
229 const char* p = reinterpret_cast<const char*>(pheader + 1);
230 const char* end = reinterpret_cast<const char*>(pheader) + pheader->size;
231 MoveFromBinaryFormat(data, p);
233 sample_id.ReadFromBinaryFormat(attr, p, end);
236 void ExitRecord::DumpData(size_t indent) const {
237 PrintIndented(indent, "pid %u, ppid %u, tid %u, ptid %u\n", data.pid, data.ppid, data.tid,
241 SampleRecord::SampleRecord(const perf_event_attr& attr, const perf_event_header* pheader)
243 const char* p = reinterpret_cast<const char*>(pheader + 1);
244 const char* end = reinterpret_cast<const char*>(pheader) + pheader->size;
245 sample_type = attr.sample_type;
247 if (sample_type & PERF_SAMPLE_IP) {
248 MoveFromBinaryFormat(ip_data, p);
250 if (sample_type & PERF_SAMPLE_TID) {
251 MoveFromBinaryFormat(tid_data, p);
253 if (sample_type & PERF_SAMPLE_TIME) {
254 MoveFromBinaryFormat(time_data, p);
256 if (sample_type & PERF_SAMPLE_ADDR) {
257 MoveFromBinaryFormat(addr_data, p);
259 if (sample_type & PERF_SAMPLE_ID) {
260 MoveFromBinaryFormat(id_data, p);
262 if (sample_type & PERF_SAMPLE_STREAM_ID) {
263 MoveFromBinaryFormat(stream_id_data, p);
265 if (sample_type & PERF_SAMPLE_CPU) {
266 MoveFromBinaryFormat(cpu_data, p);
268 if (sample_type & PERF_SAMPLE_PERIOD) {
269 MoveFromBinaryFormat(period_data, p);
271 // TODO: Add parsing of other PERF_SAMPLE_*.
274 LOG(DEBUG) << "Record has " << end - p << " bytes left\n";
278 void SampleRecord::DumpData(size_t indent) const {
279 PrintIndented(indent, "sample_type: 0x%" PRIx64 "\n", sample_type);
280 if (sample_type & PERF_SAMPLE_IP) {
281 PrintIndented(indent, "ip %p\n", reinterpret_cast<void*>(ip_data.ip));
283 if (sample_type & PERF_SAMPLE_TID) {
284 PrintIndented(indent, "pid %u, tid %u\n", tid_data.pid, tid_data.tid);
286 if (sample_type & PERF_SAMPLE_TIME) {
287 PrintIndented(indent, "time %" PRId64 "\n", time_data.time);
289 if (sample_type & PERF_SAMPLE_ADDR) {
290 PrintIndented(indent, "addr %p\n", reinterpret_cast<void*>(addr_data.addr));
292 if (sample_type & PERF_SAMPLE_ID) {
293 PrintIndented(indent, "id %" PRId64 "\n", id_data.id);
295 if (sample_type & PERF_SAMPLE_STREAM_ID) {
296 PrintIndented(indent, "stream_id %" PRId64 "\n", stream_id_data.stream_id);
298 if (sample_type & PERF_SAMPLE_CPU) {
299 PrintIndented(indent, "cpu %u, res %u\n", cpu_data.cpu, cpu_data.res);
301 if (sample_type & PERF_SAMPLE_PERIOD) {
302 PrintIndented(indent, "period %" PRId64 "\n", period_data.period);
306 BuildIdRecord::BuildIdRecord(const perf_event_header* pheader) : Record(pheader) {
307 const char* p = reinterpret_cast<const char*>(pheader + 1);
308 const char* end = reinterpret_cast<const char*>(pheader) + pheader->size;
309 MoveFromBinaryFormat(pid, p);
310 std::copy_n(p, build_id.size(), build_id.begin());
311 p += ALIGN(build_id.size(), 8);
313 p += ALIGN(filename.size() + 1, 64);
317 void BuildIdRecord::DumpData(size_t indent) const {
318 PrintIndented(indent, "pid %u\n", pid);
319 PrintIndented(indent, "build_id 0x");
320 for (auto& c : build_id) {
324 PrintIndented(indent, "filename %s\n", filename.c_str());
327 std::vector<char> BuildIdRecord::BinaryFormat() const {
328 std::vector<char> buf(header.size);
329 char* p = buf.data();
330 MoveToBinaryFormat(header, p);
331 MoveToBinaryFormat(pid, p);
332 memcpy(p, build_id.data(), build_id.size());
333 p += ALIGN(build_id.size(), 8);
334 strcpy(p, filename.c_str());
335 p += ALIGN(filename.size() + 1, 64);
339 std::unique_ptr<const Record> ReadRecordFromBuffer(const perf_event_attr& attr,
340 const perf_event_header* pheader) {
341 switch (pheader->type) {
342 case PERF_RECORD_MMAP:
343 return std::unique_ptr<const Record>(new MmapRecord(attr, pheader));
344 case PERF_RECORD_COMM:
345 return std::unique_ptr<const Record>(new CommRecord(attr, pheader));
346 case PERF_RECORD_EXIT:
347 return std::unique_ptr<const Record>(new ExitRecord(attr, pheader));
348 case PERF_RECORD_SAMPLE:
349 return std::unique_ptr<const Record>(new SampleRecord(attr, pheader));
351 return std::unique_ptr<const Record>(new Record(pheader));
355 MmapRecord CreateMmapRecord(const perf_event_attr& attr, bool in_kernel, uint32_t pid, uint32_t tid,
356 uint64_t addr, uint64_t len, uint64_t pgoff,
357 const std::string& filename) {
359 record.header.type = PERF_RECORD_MMAP;
360 record.header.misc = (in_kernel ? PERF_RECORD_MISC_KERNEL : PERF_RECORD_MISC_USER);
361 record.data.pid = pid;
362 record.data.tid = tid;
363 record.data.addr = addr;
364 record.data.len = len;
365 record.data.pgoff = pgoff;
366 record.filename = filename;
367 size_t sample_id_size = record.sample_id.CreateContent(attr);
368 record.header.size = sizeof(record.header) + sizeof(record.data) +
369 ALIGN(record.filename.size() + 1, 8) + sample_id_size;
373 CommRecord CreateCommRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid,
374 const std::string& comm) {
376 record.header.type = PERF_RECORD_COMM;
377 record.header.misc = 0;
378 record.data.pid = pid;
379 record.data.tid = tid;
381 size_t sample_id_size = record.sample_id.CreateContent(attr);
382 record.header.size = sizeof(record.header) + sizeof(record.data) +
383 ALIGN(record.comm.size() + 1, 8) + sample_id_size;
387 BuildIdRecord CreateBuildIdRecord(bool in_kernel, pid_t pid, const BuildId& build_id,
388 const std::string& filename) {
389 BuildIdRecord record;
390 record.header.type = PERF_RECORD_BUILD_ID;
391 record.header.misc = (in_kernel ? PERF_RECORD_MISC_KERNEL : PERF_RECORD_MISC_USER);
393 record.build_id = build_id;
394 record.filename = filename;
395 record.header.size = sizeof(record.header) + sizeof(record.pid) +
396 ALIGN(record.build_id.size(), 8) + ALIGN(filename.size() + 1, 64);