OSDN Git Service

Implement simpleperf record/dumprecord subcommands.
[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"},
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"},
40   };
41
42   auto it = record_type_names.find(record_type);
43   if (it != record_type_names.end()) {
44     return it->second;
45   }
46   return android::base::StringPrintf("unknown(%d)", record_type);
47 }
48
49 template <class T>
50 void MoveFromBinaryFormat(T& data, const char*& p) {
51   data = *reinterpret_cast<const T*>(p);
52   p += sizeof(T);
53 }
54
55 template <class T>
56 void MoveToBinaryFormat(const T& data, char*& p) {
57   *reinterpret_cast<T*>(p) = data;
58   p += sizeof(T);
59 }
60
61 SampleId::SampleId() {
62   sample_id_all = false;
63   sample_type = 0;
64 }
65
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.
71   size_t size = 0;
72   if (sample_id_all) {
73     if (sample_type & PERF_SAMPLE_TID) {
74       size += sizeof(PerfSampleTidType);
75     }
76     if (sample_type & PERF_SAMPLE_TIME) {
77       size += sizeof(PerfSampleTimeType);
78     }
79     if (sample_type & PERF_SAMPLE_ID) {
80       size += sizeof(PerfSampleIdType);
81     }
82     if (sample_type & PERF_SAMPLE_STREAM_ID) {
83       size += sizeof(PerfSampleStreamIdType);
84     }
85     if (sample_type & PERF_SAMPLE_CPU) {
86       size += sizeof(PerfSampleCpuType);
87     }
88   }
89   return size;
90 }
91
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;
95   if (sample_id_all) {
96     if (sample_type & PERF_SAMPLE_TID) {
97       MoveFromBinaryFormat(tid_data, p);
98     }
99     if (sample_type & PERF_SAMPLE_TIME) {
100       MoveFromBinaryFormat(time_data, p);
101     }
102     if (sample_type & PERF_SAMPLE_ID) {
103       MoveFromBinaryFormat(id_data, p);
104     }
105     if (sample_type & PERF_SAMPLE_STREAM_ID) {
106       MoveFromBinaryFormat(stream_id_data, p);
107     }
108     if (sample_type & PERF_SAMPLE_CPU) {
109       MoveFromBinaryFormat(cpu_data, p);
110     }
111     // TODO: Add parsing of PERF_SAMPLE_IDENTIFIER.
112   }
113   CHECK_LE(p, end);
114   if (p < end) {
115     LOG(DEBUG) << "Record SampleId part has " << end - p << " bytes left\n";
116   }
117 }
118
119 void SampleId::WriteToBinaryFormat(char*& p) const {
120   if (sample_id_all) {
121     if (sample_type & PERF_SAMPLE_TID) {
122       MoveToBinaryFormat(tid_data, p);
123     }
124     if (sample_type & PERF_SAMPLE_TIME) {
125       MoveToBinaryFormat(time_data, p);
126     }
127     if (sample_type & PERF_SAMPLE_ID) {
128       MoveToBinaryFormat(id_data, p);
129     }
130     if (sample_type & PERF_SAMPLE_STREAM_ID) {
131       MoveToBinaryFormat(stream_id_data, p);
132     }
133     if (sample_type & PERF_SAMPLE_CPU) {
134       MoveToBinaryFormat(cpu_data, p);
135     }
136   }
137 }
138
139 void SampleId::Dump(size_t indent) const {
140   if (sample_id_all) {
141     if (sample_type & PERF_SAMPLE_TID) {
142       PrintIndented(indent, "sample_id: pid %u, tid %u\n", tid_data.pid, tid_data.tid);
143     }
144     if (sample_type & PERF_SAMPLE_TIME) {
145       PrintIndented(indent, "sample_id: time %" PRId64 "\n", time_data.time);
146     }
147     if (sample_type & PERF_SAMPLE_ID) {
148       PrintIndented(indent, "sample_id: stream_id %" PRId64 "\n", id_data.id);
149     }
150     if (sample_type & PERF_SAMPLE_STREAM_ID) {
151       PrintIndented(indent, "sample_id: stream_id %" PRId64 "\n", stream_id_data.stream_id);
152     }
153     if (sample_type & PERF_SAMPLE_CPU) {
154       PrintIndented(indent, "sample_id: cpu %u, res %u\n", cpu_data.cpu, cpu_data.res);
155     }
156   }
157 }
158
159 Record::Record() {
160   memset(&header, 0, sizeof(header));
161 }
162
163 Record::Record(const perf_event_header* pheader) {
164   header = *pheader;
165 }
166
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);
172 }
173
174 MmapRecord::MmapRecord(const perf_event_attr& attr, const perf_event_header* pheader)
175     : Record(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);
179   filename = p;
180   p += ALIGN(filename.size() + 1, 8);
181   CHECK_LE(p, end);
182   sample_id.ReadFromBinaryFormat(attr, p, end);
183 }
184
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());
189 }
190
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);
199   return buf;
200 }
201
202 CommRecord::CommRecord(const perf_event_attr& attr, const perf_event_header* pheader)
203     : Record(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);
207   comm = p;
208   p += ALIGN(strlen(p) + 1, 8);
209   CHECK_LE(p, end);
210   sample_id.ReadFromBinaryFormat(attr, p, end);
211 }
212
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());
215 }
216
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);
225   return buf;
226 }
227
228 ExitRecord::ExitRecord(const perf_event_attr& attr, const perf_event_header* pheader)
229     : Record(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);
233   CHECK_LE(p, end);
234   sample_id.ReadFromBinaryFormat(attr, p, end);
235 }
236
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,
239                 data.ptid);
240 }
241
242 SampleRecord::SampleRecord(const perf_event_attr& attr, const perf_event_header* pheader)
243     : Record(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;
247
248   if (sample_type & PERF_SAMPLE_IP) {
249     MoveFromBinaryFormat(ip_data, p);
250   }
251   if (sample_type & PERF_SAMPLE_TID) {
252     MoveFromBinaryFormat(tid_data, p);
253   }
254   if (sample_type & PERF_SAMPLE_TIME) {
255     MoveFromBinaryFormat(time_data, p);
256   }
257   if (sample_type & PERF_SAMPLE_ADDR) {
258     MoveFromBinaryFormat(addr_data, p);
259   }
260   if (sample_type & PERF_SAMPLE_ID) {
261     MoveFromBinaryFormat(id_data, p);
262   }
263   if (sample_type & PERF_SAMPLE_STREAM_ID) {
264     MoveFromBinaryFormat(stream_id_data, p);
265   }
266   if (sample_type & PERF_SAMPLE_CPU) {
267     MoveFromBinaryFormat(cpu_data, p);
268   }
269   if (sample_type & PERF_SAMPLE_PERIOD) {
270     MoveFromBinaryFormat(period_data, p);
271   }
272   // TODO: Add parsing of other PERF_SAMPLE_*.
273   CHECK_LE(p, end);
274   if (p < end) {
275     LOG(DEBUG) << "Record has " << end - p << " bytes left\n";
276   }
277 }
278
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));
283   }
284   if (sample_type & PERF_SAMPLE_TID) {
285     PrintIndented(indent, "pid %u, tid %u\n", tid_data.pid, tid_data.tid);
286   }
287   if (sample_type & PERF_SAMPLE_TIME) {
288     PrintIndented(indent, "time %" PRId64 "\n", time_data.time);
289   }
290   if (sample_type & PERF_SAMPLE_ADDR) {
291     PrintIndented(indent, "addr %p\n", reinterpret_cast<void*>(addr_data.addr));
292   }
293   if (sample_type & PERF_SAMPLE_ID) {
294     PrintIndented(indent, "id %" PRId64 "\n", id_data.id);
295   }
296   if (sample_type & PERF_SAMPLE_STREAM_ID) {
297     PrintIndented(indent, "stream_id %" PRId64 "\n", stream_id_data.stream_id);
298   }
299   if (sample_type & PERF_SAMPLE_CPU) {
300     PrintIndented(indent, "cpu %u, res %u\n", cpu_data.cpu, cpu_data.res);
301   }
302   if (sample_type & PERF_SAMPLE_PERIOD) {
303     PrintIndented(indent, "period %" PRId64 "\n", period_data.period);
304   }
305 }
306
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));
318     default:
319       return std::unique_ptr<const Record>(new Record(pheader));
320   }
321 }