OSDN Git Service

am db1d49c7: (-s ours) DO NOT MERGE New ext4enc kernel switching from xattrs to ioctl
[android-x86/system-extras.git] / simpleperf / record.cpp
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 "record.h"
18
19 #include <inttypes.h>
20 #include <unordered_map>
21
22 #include <base/logging.h>
23 #include <base/stringprintf.h>
24
25 #include "environment.h"
26 #include "utils.h"
27
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"},
35   };
36
37   auto it = record_type_names.find(record_type);
38   if (it != record_type_names.end()) {
39     return it->second;
40   }
41   return android::base::StringPrintf("unknown(%d)", record_type);
42 }
43
44 template <class T>
45 void MoveFromBinaryFormat(T& data, const char*& p) {
46   data = *reinterpret_cast<const T*>(p);
47   p += sizeof(T);
48 }
49
50 template <class T>
51 void MoveToBinaryFormat(const T& data, char*& p) {
52   *reinterpret_cast<T*>(p) = data;
53   p += sizeof(T);
54 }
55
56 SampleId::SampleId() {
57   memset(this, 0, sizeof(SampleId));
58 }
59
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.
65   size_t size = 0;
66   if (sample_id_all) {
67     if (sample_type & PERF_SAMPLE_TID) {
68       size += sizeof(PerfSampleTidType);
69     }
70     if (sample_type & PERF_SAMPLE_TIME) {
71       size += sizeof(PerfSampleTimeType);
72     }
73     if (sample_type & PERF_SAMPLE_ID) {
74       size += sizeof(PerfSampleIdType);
75     }
76     if (sample_type & PERF_SAMPLE_STREAM_ID) {
77       size += sizeof(PerfSampleStreamIdType);
78     }
79     if (sample_type & PERF_SAMPLE_CPU) {
80       size += sizeof(PerfSampleCpuType);
81     }
82   }
83   return size;
84 }
85
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;
89   if (sample_id_all) {
90     if (sample_type & PERF_SAMPLE_TID) {
91       MoveFromBinaryFormat(tid_data, p);
92     }
93     if (sample_type & PERF_SAMPLE_TIME) {
94       MoveFromBinaryFormat(time_data, p);
95     }
96     if (sample_type & PERF_SAMPLE_ID) {
97       MoveFromBinaryFormat(id_data, p);
98     }
99     if (sample_type & PERF_SAMPLE_STREAM_ID) {
100       MoveFromBinaryFormat(stream_id_data, p);
101     }
102     if (sample_type & PERF_SAMPLE_CPU) {
103       MoveFromBinaryFormat(cpu_data, p);
104     }
105     // TODO: Add parsing of PERF_SAMPLE_IDENTIFIER.
106   }
107   CHECK_LE(p, end);
108   if (p < end) {
109     LOG(DEBUG) << "Record SampleId part has " << end - p << " bytes left\n";
110   }
111 }
112
113 void SampleId::WriteToBinaryFormat(char*& p) const {
114   if (sample_id_all) {
115     if (sample_type & PERF_SAMPLE_TID) {
116       MoveToBinaryFormat(tid_data, p);
117     }
118     if (sample_type & PERF_SAMPLE_TIME) {
119       MoveToBinaryFormat(time_data, p);
120     }
121     if (sample_type & PERF_SAMPLE_ID) {
122       MoveToBinaryFormat(id_data, p);
123     }
124     if (sample_type & PERF_SAMPLE_STREAM_ID) {
125       MoveToBinaryFormat(stream_id_data, p);
126     }
127     if (sample_type & PERF_SAMPLE_CPU) {
128       MoveToBinaryFormat(cpu_data, p);
129     }
130   }
131 }
132
133 void SampleId::Dump(size_t indent) const {
134   if (sample_id_all) {
135     if (sample_type & PERF_SAMPLE_TID) {
136       PrintIndented(indent, "sample_id: pid %u, tid %u\n", tid_data.pid, tid_data.tid);
137     }
138     if (sample_type & PERF_SAMPLE_TIME) {
139       PrintIndented(indent, "sample_id: time %" PRId64 "\n", time_data.time);
140     }
141     if (sample_type & PERF_SAMPLE_ID) {
142       PrintIndented(indent, "sample_id: stream_id %" PRId64 "\n", id_data.id);
143     }
144     if (sample_type & PERF_SAMPLE_STREAM_ID) {
145       PrintIndented(indent, "sample_id: stream_id %" PRId64 "\n", stream_id_data.stream_id);
146     }
147     if (sample_type & PERF_SAMPLE_CPU) {
148       PrintIndented(indent, "sample_id: cpu %u, res %u\n", cpu_data.cpu, cpu_data.res);
149     }
150   }
151 }
152
153 Record::Record() {
154   memset(&header, 0, sizeof(header));
155 }
156
157 Record::Record(const perf_event_header* pheader) {
158   header = *pheader;
159 }
160
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);
166 }
167
168 MmapRecord::MmapRecord(const perf_event_attr& attr, const perf_event_header* pheader)
169     : Record(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);
173   filename = p;
174   p += ALIGN(filename.size() + 1, 8);
175   CHECK_LE(p, end);
176   sample_id.ReadFromBinaryFormat(attr, p, end);
177 }
178
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());
183 }
184
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);
193   return buf;
194 }
195
196 CommRecord::CommRecord(const perf_event_attr& attr, const perf_event_header* pheader)
197     : Record(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);
201   comm = p;
202   p += ALIGN(strlen(p) + 1, 8);
203   CHECK_LE(p, end);
204   sample_id.ReadFromBinaryFormat(attr, p, end);
205 }
206
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());
209 }
210
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);
219   return buf;
220 }
221
222 ExitRecord::ExitRecord(const perf_event_attr& attr, const perf_event_header* pheader)
223     : Record(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);
227   CHECK_LE(p, end);
228   sample_id.ReadFromBinaryFormat(attr, p, end);
229 }
230
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,
233                 data.ptid);
234 }
235
236 SampleRecord::SampleRecord(const perf_event_attr& attr, const perf_event_header* pheader)
237     : Record(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;
241
242   if (sample_type & PERF_SAMPLE_IP) {
243     MoveFromBinaryFormat(ip_data, p);
244   }
245   if (sample_type & PERF_SAMPLE_TID) {
246     MoveFromBinaryFormat(tid_data, p);
247   }
248   if (sample_type & PERF_SAMPLE_TIME) {
249     MoveFromBinaryFormat(time_data, p);
250   }
251   if (sample_type & PERF_SAMPLE_ADDR) {
252     MoveFromBinaryFormat(addr_data, p);
253   }
254   if (sample_type & PERF_SAMPLE_ID) {
255     MoveFromBinaryFormat(id_data, p);
256   }
257   if (sample_type & PERF_SAMPLE_STREAM_ID) {
258     MoveFromBinaryFormat(stream_id_data, p);
259   }
260   if (sample_type & PERF_SAMPLE_CPU) {
261     MoveFromBinaryFormat(cpu_data, p);
262   }
263   if (sample_type & PERF_SAMPLE_PERIOD) {
264     MoveFromBinaryFormat(period_data, p);
265   }
266   // TODO: Add parsing of other PERF_SAMPLE_*.
267   CHECK_LE(p, end);
268   if (p < end) {
269     LOG(DEBUG) << "Record has " << end - p << " bytes left\n";
270   }
271 }
272
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));
277   }
278   if (sample_type & PERF_SAMPLE_TID) {
279     PrintIndented(indent, "pid %u, tid %u\n", tid_data.pid, tid_data.tid);
280   }
281   if (sample_type & PERF_SAMPLE_TIME) {
282     PrintIndented(indent, "time %" PRId64 "\n", time_data.time);
283   }
284   if (sample_type & PERF_SAMPLE_ADDR) {
285     PrintIndented(indent, "addr %p\n", reinterpret_cast<void*>(addr_data.addr));
286   }
287   if (sample_type & PERF_SAMPLE_ID) {
288     PrintIndented(indent, "id %" PRId64 "\n", id_data.id);
289   }
290   if (sample_type & PERF_SAMPLE_STREAM_ID) {
291     PrintIndented(indent, "stream_id %" PRId64 "\n", stream_id_data.stream_id);
292   }
293   if (sample_type & PERF_SAMPLE_CPU) {
294     PrintIndented(indent, "cpu %u, res %u\n", cpu_data.cpu, cpu_data.res);
295   }
296   if (sample_type & PERF_SAMPLE_PERIOD) {
297     PrintIndented(indent, "period %" PRId64 "\n", period_data.period);
298   }
299 }
300
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);
307   filename = p;
308   p += ALIGN(filename.size() + 1, 64);
309   CHECK_EQ(p, end);
310 }
311
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) {
316     printf("%02x", c);
317   }
318   printf("\n");
319   PrintIndented(indent, "filename %s\n", filename.c_str());
320 }
321
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);
331   return buf;
332 }
333
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));
345     default:
346       return std::unique_ptr<const Record>(new Record(pheader));
347   }
348 }
349
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) {
353   MmapRecord record;
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;
365   return record;
366 }
367
368 CommRecord CreateCommRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid,
369                             const std::string& comm) {
370   CommRecord record;
371   record.header.type = PERF_RECORD_COMM;
372   record.header.misc = 0;
373   record.data.pid = pid;
374   record.data.tid = tid;
375   record.comm = comm;
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;
379   return record;
380 }
381
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);
387   record.pid = pid;
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);
392   return record;
393 }