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 sample_id_all = false;
66 // Return sample_id size in binary format.
67 size_t SampleId::CreateContent(const perf_event_attr& attr) {
68 sample_id_all = attr.sample_id_all;
69 sample_type = attr.sample_type;
70 // Other data are not necessary. TODO: Set missing SampleId data.
73 if (sample_type & PERF_SAMPLE_TID) {
74 size += sizeof(PerfSampleTidType);
76 if (sample_type & PERF_SAMPLE_TIME) {
77 size += sizeof(PerfSampleTimeType);
79 if (sample_type & PERF_SAMPLE_ID) {
80 size += sizeof(PerfSampleIdType);
82 if (sample_type & PERF_SAMPLE_STREAM_ID) {
83 size += sizeof(PerfSampleStreamIdType);
85 if (sample_type & PERF_SAMPLE_CPU) {
86 size += sizeof(PerfSampleCpuType);
92 void SampleId::ReadFromBinaryFormat(const perf_event_attr& attr, const char* p, const char* end) {
93 sample_id_all = attr.sample_id_all;
94 sample_type = attr.sample_type;
96 if (sample_type & PERF_SAMPLE_TID) {
97 MoveFromBinaryFormat(tid_data, p);
99 if (sample_type & PERF_SAMPLE_TIME) {
100 MoveFromBinaryFormat(time_data, p);
102 if (sample_type & PERF_SAMPLE_ID) {
103 MoveFromBinaryFormat(id_data, p);
105 if (sample_type & PERF_SAMPLE_STREAM_ID) {
106 MoveFromBinaryFormat(stream_id_data, p);
108 if (sample_type & PERF_SAMPLE_CPU) {
109 MoveFromBinaryFormat(cpu_data, p);
111 // TODO: Add parsing of PERF_SAMPLE_IDENTIFIER.
115 LOG(DEBUG) << "Record SampleId part has " << end - p << " bytes left\n";
119 void SampleId::WriteToBinaryFormat(char*& p) const {
121 if (sample_type & PERF_SAMPLE_TID) {
122 MoveToBinaryFormat(tid_data, p);
124 if (sample_type & PERF_SAMPLE_TIME) {
125 MoveToBinaryFormat(time_data, p);
127 if (sample_type & PERF_SAMPLE_ID) {
128 MoveToBinaryFormat(id_data, p);
130 if (sample_type & PERF_SAMPLE_STREAM_ID) {
131 MoveToBinaryFormat(stream_id_data, p);
133 if (sample_type & PERF_SAMPLE_CPU) {
134 MoveToBinaryFormat(cpu_data, p);
139 void SampleId::Dump(size_t indent) const {
141 if (sample_type & PERF_SAMPLE_TID) {
142 PrintIndented(indent, "sample_id: pid %u, tid %u\n", tid_data.pid, tid_data.tid);
144 if (sample_type & PERF_SAMPLE_TIME) {
145 PrintIndented(indent, "sample_id: time %" PRId64 "\n", time_data.time);
147 if (sample_type & PERF_SAMPLE_ID) {
148 PrintIndented(indent, "sample_id: stream_id %" PRId64 "\n", id_data.id);
150 if (sample_type & PERF_SAMPLE_STREAM_ID) {
151 PrintIndented(indent, "sample_id: stream_id %" PRId64 "\n", stream_id_data.stream_id);
153 if (sample_type & PERF_SAMPLE_CPU) {
154 PrintIndented(indent, "sample_id: cpu %u, res %u\n", cpu_data.cpu, cpu_data.res);
160 memset(&header, 0, sizeof(header));
163 Record::Record(const perf_event_header* pheader) {
167 void Record::Dump(size_t indent) const {
168 PrintIndented(indent, "record %s: type %u, misc %u, size %u\n",
169 RecordTypeToString(header.type).c_str(), header.type, header.misc, header.size);
170 DumpData(indent + 1);
171 sample_id.Dump(indent + 1);
174 MmapRecord::MmapRecord(const perf_event_attr& attr, const perf_event_header* pheader)
176 const char* p = reinterpret_cast<const char*>(pheader + 1);
177 const char* end = reinterpret_cast<const char*>(pheader) + pheader->size;
178 MoveFromBinaryFormat(data, p);
180 p += ALIGN(filename.size() + 1, 8);
182 sample_id.ReadFromBinaryFormat(attr, p, end);
185 void MmapRecord::DumpData(size_t indent) const {
186 PrintIndented(indent, "pid %u, tid %u, addr %p, len 0x%" PRIx64 "\n", data.pid, data.tid,
187 reinterpret_cast<void*>(data.addr), data.len);
188 PrintIndented(indent, "pgoff 0x%" PRIx64 ", filename %s\n", data.pgoff, filename.c_str());
191 std::vector<char> MmapRecord::BinaryFormat() const {
192 std::vector<char> buf(header.size);
193 char* p = buf.data();
194 MoveToBinaryFormat(header, p);
195 MoveToBinaryFormat(data, p);
196 strcpy(p, filename.c_str());
197 p += ALIGN(filename.size() + 1, 8);
198 sample_id.WriteToBinaryFormat(p);
202 CommRecord::CommRecord(const perf_event_attr& attr, const perf_event_header* pheader)
204 const char* p = reinterpret_cast<const char*>(pheader + 1);
205 const char* end = reinterpret_cast<const char*>(pheader) + pheader->size;
206 MoveFromBinaryFormat(data, p);
208 p += ALIGN(strlen(p) + 1, 8);
210 sample_id.ReadFromBinaryFormat(attr, p, end);
213 void CommRecord::DumpData(size_t indent) const {
214 PrintIndented(indent, "pid %u, tid %u, comm %s\n", data.pid, data.tid, comm.c_str());
217 std::vector<char> CommRecord::BinaryFormat() const {
218 std::vector<char> buf(header.size);
219 char* p = buf.data();
220 MoveToBinaryFormat(header, p);
221 MoveToBinaryFormat(data, p);
222 strcpy(p, comm.c_str());
223 p += ALIGN(comm.size() + 1, 8);
224 sample_id.WriteToBinaryFormat(p);
228 ExitRecord::ExitRecord(const perf_event_attr& attr, const perf_event_header* pheader)
230 const char* p = reinterpret_cast<const char*>(pheader + 1);
231 const char* end = reinterpret_cast<const char*>(pheader) + pheader->size;
232 MoveFromBinaryFormat(data, p);
234 sample_id.ReadFromBinaryFormat(attr, p, end);
237 void ExitRecord::DumpData(size_t indent) const {
238 PrintIndented(indent, "pid %u, ppid %u, tid %u, ptid %u\n", data.pid, data.ppid, data.tid,
242 SampleRecord::SampleRecord(const perf_event_attr& attr, const perf_event_header* pheader)
244 const char* p = reinterpret_cast<const char*>(pheader + 1);
245 const char* end = reinterpret_cast<const char*>(pheader) + pheader->size;
246 sample_type = attr.sample_type;
248 if (sample_type & PERF_SAMPLE_IP) {
249 MoveFromBinaryFormat(ip_data, p);
251 if (sample_type & PERF_SAMPLE_TID) {
252 MoveFromBinaryFormat(tid_data, p);
254 if (sample_type & PERF_SAMPLE_TIME) {
255 MoveFromBinaryFormat(time_data, p);
257 if (sample_type & PERF_SAMPLE_ADDR) {
258 MoveFromBinaryFormat(addr_data, p);
260 if (sample_type & PERF_SAMPLE_ID) {
261 MoveFromBinaryFormat(id_data, p);
263 if (sample_type & PERF_SAMPLE_STREAM_ID) {
264 MoveFromBinaryFormat(stream_id_data, p);
266 if (sample_type & PERF_SAMPLE_CPU) {
267 MoveFromBinaryFormat(cpu_data, p);
269 if (sample_type & PERF_SAMPLE_PERIOD) {
270 MoveFromBinaryFormat(period_data, p);
272 // TODO: Add parsing of other PERF_SAMPLE_*.
275 LOG(DEBUG) << "Record has " << end - p << " bytes left\n";
279 void SampleRecord::DumpData(size_t indent) const {
280 PrintIndented(indent, "sample_type: 0x%" PRIx64 "\n", sample_type);
281 if (sample_type & PERF_SAMPLE_IP) {
282 PrintIndented(indent, "ip %p\n", reinterpret_cast<void*>(ip_data.ip));
284 if (sample_type & PERF_SAMPLE_TID) {
285 PrintIndented(indent, "pid %u, tid %u\n", tid_data.pid, tid_data.tid);
287 if (sample_type & PERF_SAMPLE_TIME) {
288 PrintIndented(indent, "time %" PRId64 "\n", time_data.time);
290 if (sample_type & PERF_SAMPLE_ADDR) {
291 PrintIndented(indent, "addr %p\n", reinterpret_cast<void*>(addr_data.addr));
293 if (sample_type & PERF_SAMPLE_ID) {
294 PrintIndented(indent, "id %" PRId64 "\n", id_data.id);
296 if (sample_type & PERF_SAMPLE_STREAM_ID) {
297 PrintIndented(indent, "stream_id %" PRId64 "\n", stream_id_data.stream_id);
299 if (sample_type & PERF_SAMPLE_CPU) {
300 PrintIndented(indent, "cpu %u, res %u\n", cpu_data.cpu, cpu_data.res);
302 if (sample_type & PERF_SAMPLE_PERIOD) {
303 PrintIndented(indent, "period %" PRId64 "\n", period_data.period);
307 std::unique_ptr<const Record> ReadRecordFromBuffer(const perf_event_attr& attr,
308 const perf_event_header* pheader) {
309 switch (pheader->type) {
310 case PERF_RECORD_MMAP:
311 return std::unique_ptr<const Record>(new MmapRecord(attr, pheader));
312 case PERF_RECORD_COMM:
313 return std::unique_ptr<const Record>(new CommRecord(attr, pheader));
314 case PERF_RECORD_EXIT:
315 return std::unique_ptr<const Record>(new ExitRecord(attr, pheader));
316 case PERF_RECORD_SAMPLE:
317 return std::unique_ptr<const Record>(new SampleRecord(attr, pheader));
319 return std::unique_ptr<const Record>(new Record(pheader));