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"}, {PERF_RECORD_LOST, "lost"},
31 {PERF_RECORD_COMM, "comm"}, {PERF_RECORD_EXIT, "exit"},
32 {PERF_RECORD_THROTTLE, "throttle"}, {PERF_RECORD_UNTHROTTLE, "unthrottle"},
33 {PERF_RECORD_FORK, "fork"}, {PERF_RECORD_READ, "read"},
34 {PERF_RECORD_SAMPLE, "sample"}, {PERF_RECORD_BUILD_ID, "build_id"},
37 auto it = record_type_names.find(record_type);
38 if (it != record_type_names.end()) {
41 return android::base::StringPrintf("unknown(%d)", record_type);
45 void MoveFromBinaryFormat(T& data, const char*& p) {
46 data = *reinterpret_cast<const T*>(p);
51 void MoveToBinaryFormat(const T& data, char*& p) {
52 *reinterpret_cast<T*>(p) = data;
56 SampleId::SampleId() {
57 memset(this, 0, sizeof(SampleId));
60 // Return sample_id size in binary format.
61 size_t SampleId::CreateContent(const perf_event_attr& attr) {
62 sample_id_all = attr.sample_id_all;
63 sample_type = attr.sample_type;
64 // Other data are not necessary. TODO: Set missing SampleId data.
67 if (sample_type & PERF_SAMPLE_TID) {
68 size += sizeof(PerfSampleTidType);
70 if (sample_type & PERF_SAMPLE_TIME) {
71 size += sizeof(PerfSampleTimeType);
73 if (sample_type & PERF_SAMPLE_ID) {
74 size += sizeof(PerfSampleIdType);
76 if (sample_type & PERF_SAMPLE_STREAM_ID) {
77 size += sizeof(PerfSampleStreamIdType);
79 if (sample_type & PERF_SAMPLE_CPU) {
80 size += sizeof(PerfSampleCpuType);
86 void SampleId::ReadFromBinaryFormat(const perf_event_attr& attr, const char* p, const char* end) {
87 sample_id_all = attr.sample_id_all;
88 sample_type = attr.sample_type;
90 if (sample_type & PERF_SAMPLE_TID) {
91 MoveFromBinaryFormat(tid_data, p);
93 if (sample_type & PERF_SAMPLE_TIME) {
94 MoveFromBinaryFormat(time_data, p);
96 if (sample_type & PERF_SAMPLE_ID) {
97 MoveFromBinaryFormat(id_data, p);
99 if (sample_type & PERF_SAMPLE_STREAM_ID) {
100 MoveFromBinaryFormat(stream_id_data, p);
102 if (sample_type & PERF_SAMPLE_CPU) {
103 MoveFromBinaryFormat(cpu_data, p);
105 // TODO: Add parsing of PERF_SAMPLE_IDENTIFIER.
109 LOG(DEBUG) << "Record SampleId part has " << end - p << " bytes left\n";
113 void SampleId::WriteToBinaryFormat(char*& p) const {
115 if (sample_type & PERF_SAMPLE_TID) {
116 MoveToBinaryFormat(tid_data, p);
118 if (sample_type & PERF_SAMPLE_TIME) {
119 MoveToBinaryFormat(time_data, p);
121 if (sample_type & PERF_SAMPLE_ID) {
122 MoveToBinaryFormat(id_data, p);
124 if (sample_type & PERF_SAMPLE_STREAM_ID) {
125 MoveToBinaryFormat(stream_id_data, p);
127 if (sample_type & PERF_SAMPLE_CPU) {
128 MoveToBinaryFormat(cpu_data, p);
133 void SampleId::Dump(size_t indent) const {
135 if (sample_type & PERF_SAMPLE_TID) {
136 PrintIndented(indent, "sample_id: pid %u, tid %u\n", tid_data.pid, tid_data.tid);
138 if (sample_type & PERF_SAMPLE_TIME) {
139 PrintIndented(indent, "sample_id: time %" PRId64 "\n", time_data.time);
141 if (sample_type & PERF_SAMPLE_ID) {
142 PrintIndented(indent, "sample_id: stream_id %" PRId64 "\n", id_data.id);
144 if (sample_type & PERF_SAMPLE_STREAM_ID) {
145 PrintIndented(indent, "sample_id: stream_id %" PRId64 "\n", stream_id_data.stream_id);
147 if (sample_type & PERF_SAMPLE_CPU) {
148 PrintIndented(indent, "sample_id: cpu %u, res %u\n", cpu_data.cpu, cpu_data.res);
154 memset(&header, 0, sizeof(header));
157 Record::Record(const perf_event_header* pheader) {
161 void Record::Dump(size_t indent) const {
162 PrintIndented(indent, "record %s: type %u, misc %u, size %u\n",
163 RecordTypeToString(header.type).c_str(), header.type, header.misc, header.size);
164 DumpData(indent + 1);
165 sample_id.Dump(indent + 1);
168 MmapRecord::MmapRecord(const perf_event_attr& attr, const perf_event_header* pheader)
170 const char* p = reinterpret_cast<const char*>(pheader + 1);
171 const char* end = reinterpret_cast<const char*>(pheader) + pheader->size;
172 MoveFromBinaryFormat(data, p);
174 p += ALIGN(filename.size() + 1, 8);
176 sample_id.ReadFromBinaryFormat(attr, p, end);
179 void MmapRecord::DumpData(size_t indent) const {
180 PrintIndented(indent, "pid %u, tid %u, addr %p, len 0x%" PRIx64 "\n", data.pid, data.tid,
181 reinterpret_cast<void*>(data.addr), data.len);
182 PrintIndented(indent, "pgoff 0x%" PRIx64 ", filename %s\n", data.pgoff, filename.c_str());
185 std::vector<char> MmapRecord::BinaryFormat() const {
186 std::vector<char> buf(header.size);
187 char* p = buf.data();
188 MoveToBinaryFormat(header, p);
189 MoveToBinaryFormat(data, p);
190 strcpy(p, filename.c_str());
191 p += ALIGN(filename.size() + 1, 8);
192 sample_id.WriteToBinaryFormat(p);
196 CommRecord::CommRecord(const perf_event_attr& attr, const perf_event_header* pheader)
198 const char* p = reinterpret_cast<const char*>(pheader + 1);
199 const char* end = reinterpret_cast<const char*>(pheader) + pheader->size;
200 MoveFromBinaryFormat(data, p);
202 p += ALIGN(strlen(p) + 1, 8);
204 sample_id.ReadFromBinaryFormat(attr, p, end);
207 void CommRecord::DumpData(size_t indent) const {
208 PrintIndented(indent, "pid %u, tid %u, comm %s\n", data.pid, data.tid, comm.c_str());
211 std::vector<char> CommRecord::BinaryFormat() const {
212 std::vector<char> buf(header.size);
213 char* p = buf.data();
214 MoveToBinaryFormat(header, p);
215 MoveToBinaryFormat(data, p);
216 strcpy(p, comm.c_str());
217 p += ALIGN(comm.size() + 1, 8);
218 sample_id.WriteToBinaryFormat(p);
222 ExitRecord::ExitRecord(const perf_event_attr& attr, const perf_event_header* pheader)
224 const char* p = reinterpret_cast<const char*>(pheader + 1);
225 const char* end = reinterpret_cast<const char*>(pheader) + pheader->size;
226 MoveFromBinaryFormat(data, p);
228 sample_id.ReadFromBinaryFormat(attr, p, end);
231 void ExitRecord::DumpData(size_t indent) const {
232 PrintIndented(indent, "pid %u, ppid %u, tid %u, ptid %u\n", data.pid, data.ppid, data.tid,
236 SampleRecord::SampleRecord(const perf_event_attr& attr, const perf_event_header* pheader)
238 const char* p = reinterpret_cast<const char*>(pheader + 1);
239 const char* end = reinterpret_cast<const char*>(pheader) + pheader->size;
240 sample_type = attr.sample_type;
242 if (sample_type & PERF_SAMPLE_IP) {
243 MoveFromBinaryFormat(ip_data, p);
245 if (sample_type & PERF_SAMPLE_TID) {
246 MoveFromBinaryFormat(tid_data, p);
248 if (sample_type & PERF_SAMPLE_TIME) {
249 MoveFromBinaryFormat(time_data, p);
251 if (sample_type & PERF_SAMPLE_ADDR) {
252 MoveFromBinaryFormat(addr_data, p);
254 if (sample_type & PERF_SAMPLE_ID) {
255 MoveFromBinaryFormat(id_data, p);
257 if (sample_type & PERF_SAMPLE_STREAM_ID) {
258 MoveFromBinaryFormat(stream_id_data, p);
260 if (sample_type & PERF_SAMPLE_CPU) {
261 MoveFromBinaryFormat(cpu_data, p);
263 if (sample_type & PERF_SAMPLE_PERIOD) {
264 MoveFromBinaryFormat(period_data, p);
266 // TODO: Add parsing of other PERF_SAMPLE_*.
269 LOG(DEBUG) << "Record has " << end - p << " bytes left\n";
273 void SampleRecord::DumpData(size_t indent) const {
274 PrintIndented(indent, "sample_type: 0x%" PRIx64 "\n", sample_type);
275 if (sample_type & PERF_SAMPLE_IP) {
276 PrintIndented(indent, "ip %p\n", reinterpret_cast<void*>(ip_data.ip));
278 if (sample_type & PERF_SAMPLE_TID) {
279 PrintIndented(indent, "pid %u, tid %u\n", tid_data.pid, tid_data.tid);
281 if (sample_type & PERF_SAMPLE_TIME) {
282 PrintIndented(indent, "time %" PRId64 "\n", time_data.time);
284 if (sample_type & PERF_SAMPLE_ADDR) {
285 PrintIndented(indent, "addr %p\n", reinterpret_cast<void*>(addr_data.addr));
287 if (sample_type & PERF_SAMPLE_ID) {
288 PrintIndented(indent, "id %" PRId64 "\n", id_data.id);
290 if (sample_type & PERF_SAMPLE_STREAM_ID) {
291 PrintIndented(indent, "stream_id %" PRId64 "\n", stream_id_data.stream_id);
293 if (sample_type & PERF_SAMPLE_CPU) {
294 PrintIndented(indent, "cpu %u, res %u\n", cpu_data.cpu, cpu_data.res);
296 if (sample_type & PERF_SAMPLE_PERIOD) {
297 PrintIndented(indent, "period %" PRId64 "\n", period_data.period);
301 BuildIdRecord::BuildIdRecord(const perf_event_header* pheader) : Record(pheader) {
302 const char* p = reinterpret_cast<const char*>(pheader + 1);
303 const char* end = reinterpret_cast<const char*>(pheader) + pheader->size;
304 MoveFromBinaryFormat(pid, p);
305 std::copy_n(p, build_id.size(), build_id.begin());
306 p += ALIGN(build_id.size(), 8);
308 p += ALIGN(filename.size() + 1, 64);
312 void BuildIdRecord::DumpData(size_t indent) const {
313 PrintIndented(indent, "pid %u\n", pid);
314 PrintIndented(indent, "build_id 0x");
315 for (auto& c : build_id) {
319 PrintIndented(indent, "filename %s\n", filename.c_str());
322 std::vector<char> BuildIdRecord::BinaryFormat() const {
323 std::vector<char> buf(header.size);
324 char* p = buf.data();
325 MoveToBinaryFormat(header, p);
326 MoveToBinaryFormat(pid, p);
327 memcpy(p, build_id.data(), build_id.size());
328 p += ALIGN(build_id.size(), 8);
329 strcpy(p, filename.c_str());
330 p += ALIGN(filename.size() + 1, 64);
334 std::unique_ptr<const Record> ReadRecordFromBuffer(const perf_event_attr& attr,
335 const perf_event_header* pheader) {
336 switch (pheader->type) {
337 case PERF_RECORD_MMAP:
338 return std::unique_ptr<const Record>(new MmapRecord(attr, pheader));
339 case PERF_RECORD_COMM:
340 return std::unique_ptr<const Record>(new CommRecord(attr, pheader));
341 case PERF_RECORD_EXIT:
342 return std::unique_ptr<const Record>(new ExitRecord(attr, pheader));
343 case PERF_RECORD_SAMPLE:
344 return std::unique_ptr<const Record>(new SampleRecord(attr, pheader));
346 return std::unique_ptr<const Record>(new Record(pheader));
350 MmapRecord CreateMmapRecord(const perf_event_attr& attr, bool in_kernel, uint32_t pid, uint32_t tid,
351 uint64_t addr, uint64_t len, uint64_t pgoff,
352 const std::string& filename) {
354 record.header.type = PERF_RECORD_MMAP;
355 record.header.misc = (in_kernel ? PERF_RECORD_MISC_KERNEL : PERF_RECORD_MISC_USER);
356 record.data.pid = pid;
357 record.data.tid = tid;
358 record.data.addr = addr;
359 record.data.len = len;
360 record.data.pgoff = pgoff;
361 record.filename = filename;
362 size_t sample_id_size = record.sample_id.CreateContent(attr);
363 record.header.size = sizeof(record.header) + sizeof(record.data) +
364 ALIGN(record.filename.size() + 1, 8) + sample_id_size;
368 CommRecord CreateCommRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid,
369 const std::string& comm) {
371 record.header.type = PERF_RECORD_COMM;
372 record.header.misc = 0;
373 record.data.pid = pid;
374 record.data.tid = tid;
376 size_t sample_id_size = record.sample_id.CreateContent(attr);
377 record.header.size = sizeof(record.header) + sizeof(record.data) +
378 ALIGN(record.comm.size() + 1, 8) + sample_id_size;
382 BuildIdRecord CreateBuildIdRecord(bool in_kernel, pid_t pid, const BuildId& build_id,
383 const std::string& filename) {
384 BuildIdRecord record;
385 record.header.type = PERF_RECORD_BUILD_ID;
386 record.header.misc = (in_kernel ? PERF_RECORD_MISC_KERNEL : PERF_RECORD_MISC_USER);
388 record.build_id = build_id;
389 record.filename = filename;
390 record.header.size = sizeof(record.header) + sizeof(record.pid) +
391 ALIGN(record.build_id.size(), 8) + ALIGN(filename.size() + 1, 64);