blob: 55512b8a674f687c0dc1808fce7840b2322e00e6 [file] [log] [blame]
Yabin Cui9759e1b2015-04-28 15:54:13 -07001/*
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#ifndef SIMPLE_PERF_RECORD_H_
18#define SIMPLE_PERF_RECORD_H_
19
Yabin Cuib7f481f2015-10-23 19:48:42 -070020#include <stdio.h>
Dan Albert918e4b72015-08-11 15:59:43 -070021#include <sys/types.h>
22
Yabin Cuiffaa9122016-01-15 15:25:48 -080023#include <memory>
Yabin Cuif469c3d2015-10-07 15:00:46 -070024#include <queue>
Yabin Cui9759e1b2015-04-28 15:54:13 -070025#include <string>
26#include <vector>
27
Yabin Cuib4212972016-05-25 14:08:05 -070028#include <android-base/logging.h>
29
Yabin Cui8f622512015-05-05 19:58:07 -070030#include "build_id.h"
Yabin Cui9759e1b2015-04-28 15:54:13 -070031#include "perf_event.h"
32
33struct KernelMmap;
34struct ModuleMmap;
35struct ThreadComm;
36struct ThreadMmap;
37
38enum user_record_type {
Yabin Cuib4212972016-05-25 14:08:05 -070039 PERF_RECORD_USER_DEFINED_TYPE_START = 64,
Yabin Cui9759e1b2015-04-28 15:54:13 -070040 PERF_RECORD_ATTR = 64,
41 PERF_RECORD_EVENT_TYPE,
42 PERF_RECORD_TRACING_DATA,
43 PERF_RECORD_BUILD_ID,
44 PERF_RECORD_FINISHED_ROUND,
Yabin Cuib4212972016-05-25 14:08:05 -070045
46 SIMPLE_PERF_RECORD_TYPE_START = 32768,
47 SIMPLE_PERF_RECORD_KERNEL_SYMBOL,
Yabin Cui9759e1b2015-04-28 15:54:13 -070048};
49
50struct PerfSampleIpType {
51 uint64_t ip;
52};
53
54struct PerfSampleTidType {
55 uint32_t pid, tid;
56};
57
58struct PerfSampleTimeType {
59 uint64_t time;
60};
61
62struct PerfSampleAddrType {
63 uint64_t addr;
64};
65
66struct PerfSampleIdType {
67 uint64_t id;
68};
69
70struct PerfSampleStreamIdType {
71 uint64_t stream_id;
72};
73
74struct PerfSampleCpuType {
75 uint32_t cpu, res;
76};
77
78struct PerfSamplePeriodType {
79 uint64_t period;
80};
81
Yabin Cui6e8a9a42015-06-15 14:36:43 -070082struct PerfSampleCallChainType {
83 std::vector<uint64_t> ips;
84};
85
Yabin Cuibfc11b62015-08-19 10:12:51 -070086struct PerfSampleRawType {
87 std::vector<char> data;
88};
89
Yabin Cuib64a8632016-05-24 18:23:33 -070090struct BranchStackItemType {
91 uint64_t from;
92 uint64_t to;
93 uint64_t flags;
94};
95
Yabin Cuiddddc062015-06-02 17:54:52 -070096struct PerfSampleBranchStackType {
Yabin Cuiddddc062015-06-02 17:54:52 -070097 std::vector<BranchStackItemType> stack;
98};
99
Yabin Cui76769e52015-07-13 12:23:54 -0700100struct PerfSampleRegsUserType {
101 uint64_t abi;
102 uint64_t reg_mask;
103 std::vector<uint64_t> regs;
104};
105
106struct PerfSampleStackUserType {
107 std::vector<char> data;
108 uint64_t dyn_size;
109};
110
Yabin Cui9759e1b2015-04-28 15:54:13 -0700111// SampleId is optional at the end of a record in binary format. Its content is determined by
112// sample_id_all and sample_type in perf_event_attr. To avoid the complexity of referring to
113// perf_event_attr each time, we copy sample_id_all and sample_type inside the SampleId structure.
114struct SampleId {
115 bool sample_id_all;
116 uint64_t sample_type;
117
118 PerfSampleTidType tid_data; // Valid if sample_id_all && PERF_SAMPLE_TID.
119 PerfSampleTimeType time_data; // Valid if sample_id_all && PERF_SAMPLE_TIME.
120 PerfSampleIdType id_data; // Valid if sample_id_all && PERF_SAMPLE_ID.
121 PerfSampleStreamIdType stream_id_data; // Valid if sample_id_all && PERF_SAMPLE_STREAM_ID.
122 PerfSampleCpuType cpu_data; // Valid if sample_id_all && PERF_SAMPLE_CPU.
123
124 SampleId();
125
126 // Create the content of sample_id. It depends on the attr we use.
Yabin Cui2d6efe42016-04-01 20:22:35 -0700127 size_t CreateContent(const perf_event_attr& attr, uint64_t event_id);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700128
129 // Parse sample_id from binary format in the buffer pointed by p.
130 void ReadFromBinaryFormat(const perf_event_attr& attr, const char* p, const char* end);
131
132 // Write the binary format of sample_id to the buffer pointed by p.
133 void WriteToBinaryFormat(char*& p) const;
134 void Dump(size_t indent) const;
Yabin Cuib1a885b2016-02-14 19:18:02 -0800135 size_t Size() const;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700136};
137
138// Usually one record contains the following three parts in order in binary format:
139// perf_event_header (at the head of a record, containing type and size information)
140// data depends on the record type
141// sample_id (optional part at the end of a record)
142// We hold the common parts (perf_event_header and sample_id) in the base class Record, and
143// hold the type specific data part in classes derived from Record.
144struct Record {
145 perf_event_header header;
146 SampleId sample_id;
147
Yabin Cuib4212972016-05-25 14:08:05 -0700148 Record() {
149 memset(&header, 0, sizeof(header));
150 }
151 Record(const perf_event_header* pheader) {
152 header = *pheader;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700153 }
154
Yabin Cuib4212972016-05-25 14:08:05 -0700155 virtual ~Record() {
Yabin Cuib7f481f2015-10-23 19:48:42 -0700156 }
157
158 uint32_t type() const {
159 return header.type;
160 }
161
Yabin Cuib4212972016-05-25 14:08:05 -0700162 uint16_t misc() const {
163 return header.misc;
164 }
165
166 size_t size() const {
167 return header.size;
168 }
169
170 static uint32_t header_size() {
171 return sizeof(perf_event_header);
172 }
173
Yabin Cuib64a8632016-05-24 18:23:33 -0700174 bool InKernel() const {
175 return (header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == PERF_RECORD_MISC_KERNEL;
176 }
177
Yabin Cuib4212972016-05-25 14:08:05 -0700178 void SetTypeAndMisc(uint32_t type, uint16_t misc) {
179 header.type = type;
180 header.misc = misc;
181 }
182
183 void SetSize(uint32_t size) {
184 CHECK_LT(size, 1u << 16);
185 header.size = size;
186 }
187
Yabin Cui9759e1b2015-04-28 15:54:13 -0700188 void Dump(size_t indent = 0) const;
Yabin Cuicb84c982015-09-30 17:22:35 -0700189 virtual std::vector<char> BinaryFormat() const = 0;
Yabin Cuif469c3d2015-10-07 15:00:46 -0700190 virtual uint64_t Timestamp() const;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700191
192 protected:
Yabin Cuicb84c982015-09-30 17:22:35 -0700193 virtual void DumpData(size_t) const = 0;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700194};
195
196struct MmapRecord : public Record {
197 struct MmapRecordDataType {
198 uint32_t pid, tid;
199 uint64_t addr;
200 uint64_t len;
201 uint64_t pgoff;
202 } data;
203 std::string filename;
204
Yabin Cuicb84c982015-09-30 17:22:35 -0700205 MmapRecord() { // For CreateMmapRecord.
Yabin Cui9759e1b2015-04-28 15:54:13 -0700206 }
207
208 MmapRecord(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cuicb84c982015-09-30 17:22:35 -0700209 std::vector<char> BinaryFormat() const override;
Yabin Cuib1a885b2016-02-14 19:18:02 -0800210 void AdjustSizeBasedOnData();
Yabin Cui8f622512015-05-05 19:58:07 -0700211
212 protected:
213 void DumpData(size_t indent) const override;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700214};
215
Yabin Cui41d4ba92015-06-22 12:27:58 -0700216struct Mmap2Record : public Record {
217 struct Mmap2RecordDataType {
218 uint32_t pid, tid;
219 uint64_t addr;
220 uint64_t len;
221 uint64_t pgoff;
222 uint32_t maj;
223 uint32_t min;
224 uint64_t ino;
225 uint64_t ino_generation;
226 uint32_t prot, flags;
227 } data;
228 std::string filename;
229
230 Mmap2Record() {
231 }
232
233 Mmap2Record(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cuicb84c982015-09-30 17:22:35 -0700234 std::vector<char> BinaryFormat() const override;
Yabin Cuib1a885b2016-02-14 19:18:02 -0800235 void AdjustSizeBasedOnData();
Yabin Cui41d4ba92015-06-22 12:27:58 -0700236
237 protected:
238 void DumpData(size_t indent) const override;
239};
240
Yabin Cui9759e1b2015-04-28 15:54:13 -0700241struct CommRecord : public Record {
242 struct CommRecordDataType {
243 uint32_t pid, tid;
244 } data;
245 std::string comm;
246
247 CommRecord() {
248 }
249
250 CommRecord(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cuicb84c982015-09-30 17:22:35 -0700251 std::vector<char> BinaryFormat() const override;
Yabin Cui8f622512015-05-05 19:58:07 -0700252
253 protected:
254 void DumpData(size_t indent) const override;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700255};
256
Yabin Cui41d4ba92015-06-22 12:27:58 -0700257struct ExitOrForkRecord : public Record {
258 struct ExitOrForkRecordDataType {
Yabin Cui9759e1b2015-04-28 15:54:13 -0700259 uint32_t pid, ppid;
260 uint32_t tid, ptid;
261 uint64_t time;
262 } data;
263
Yabin Cui41d4ba92015-06-22 12:27:58 -0700264 ExitOrForkRecord() {
265 }
266 ExitOrForkRecord(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cuicb84c982015-09-30 17:22:35 -0700267 std::vector<char> BinaryFormat() const override;
Yabin Cui8f622512015-05-05 19:58:07 -0700268
269 protected:
Yabin Cui9759e1b2015-04-28 15:54:13 -0700270 void DumpData(size_t indent) const override;
271};
272
Yabin Cui41d4ba92015-06-22 12:27:58 -0700273struct ExitRecord : public ExitOrForkRecord {
274 ExitRecord(const perf_event_attr& attr, const perf_event_header* pheader)
275 : ExitOrForkRecord(attr, pheader) {
276 }
277};
278
279struct ForkRecord : public ExitOrForkRecord {
280 ForkRecord() {
281 }
282 ForkRecord(const perf_event_attr& attr, const perf_event_header* pheader)
283 : ExitOrForkRecord(attr, pheader) {
284 }
Yabin Cui41d4ba92015-06-22 12:27:58 -0700285};
286
Yabin Cui9759e1b2015-04-28 15:54:13 -0700287struct SampleRecord : public Record {
288 uint64_t sample_type; // sample_type is a bit mask determining which fields below are valid.
289
290 PerfSampleIpType ip_data; // Valid if PERF_SAMPLE_IP.
291 PerfSampleTidType tid_data; // Valid if PERF_SAMPLE_TID.
292 PerfSampleTimeType time_data; // Valid if PERF_SAMPLE_TIME.
293 PerfSampleAddrType addr_data; // Valid if PERF_SAMPLE_ADDR.
294 PerfSampleIdType id_data; // Valid if PERF_SAMPLE_ID.
295 PerfSampleStreamIdType stream_id_data; // Valid if PERF_SAMPLE_STREAM_ID.
296 PerfSampleCpuType cpu_data; // Valid if PERF_SAMPLE_CPU.
297 PerfSamplePeriodType period_data; // Valid if PERF_SAMPLE_PERIOD.
298
Yabin Cui6e8a9a42015-06-15 14:36:43 -0700299 PerfSampleCallChainType callchain_data; // Valid if PERF_SAMPLE_CALLCHAIN.
Yabin Cuibfc11b62015-08-19 10:12:51 -0700300 PerfSampleRawType raw_data; // Valid if PERF_SAMPLE_RAW.
Yabin Cuiddddc062015-06-02 17:54:52 -0700301 PerfSampleBranchStackType branch_stack_data; // Valid if PERF_SAMPLE_BRANCH_STACK.
Yabin Cui76769e52015-07-13 12:23:54 -0700302 PerfSampleRegsUserType regs_user_data; // Valid if PERF_SAMPLE_REGS_USER.
303 PerfSampleStackUserType stack_user_data; // Valid if PERF_SAMPLE_STACK_USER.
Yabin Cuiddddc062015-06-02 17:54:52 -0700304
Yabin Cui9759e1b2015-04-28 15:54:13 -0700305 SampleRecord(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cuicb84c982015-09-30 17:22:35 -0700306 std::vector<char> BinaryFormat() const override;
307 void AdjustSizeBasedOnData();
Yabin Cuif469c3d2015-10-07 15:00:46 -0700308 uint64_t Timestamp() const override;
Yabin Cui8f622512015-05-05 19:58:07 -0700309
310 protected:
311 void DumpData(size_t indent) const override;
312};
313
314// BuildIdRecord is defined in user-space, stored in BuildId feature section in record file.
315struct BuildIdRecord : public Record {
316 uint32_t pid;
317 BuildId build_id;
318 std::string filename;
319
320 BuildIdRecord() {
321 }
322
323 BuildIdRecord(const perf_event_header* pheader);
Yabin Cuicb84c982015-09-30 17:22:35 -0700324 std::vector<char> BinaryFormat() const override;
325
326 protected:
327 void DumpData(size_t indent) const override;
328};
329
Yabin Cuib4212972016-05-25 14:08:05 -0700330struct KernelSymbolRecord : public Record {
331 bool end_of_symbols;
332 std::string kallsyms;
333
334 KernelSymbolRecord() {
335 }
336
337 KernelSymbolRecord(const perf_event_header* pheader);
338 std::vector<char> BinaryFormat() const override;
339
340 protected:
341 void DumpData(size_t indent) const override;
342};
343
Yabin Cuicb84c982015-09-30 17:22:35 -0700344// UnknownRecord is used for unknown record types, it makes sure all unknown records
345// are not changed when modifying perf.data.
346struct UnknownRecord : public Record {
347 std::vector<char> data;
348
349 UnknownRecord(const perf_event_header* pheader);
350 std::vector<char> BinaryFormat() const override;
Yabin Cui8f622512015-05-05 19:58:07 -0700351
352 protected:
Yabin Cui9759e1b2015-04-28 15:54:13 -0700353 void DumpData(size_t indent) const override;
354};
355
Yabin Cuif469c3d2015-10-07 15:00:46 -0700356// RecordCache is a cache used when receiving records from the kernel.
357// It sorts received records based on type and timestamp, and pops records
358// in sorted order. Records from the kernel need to be sorted because
359// records may come from different cpus at the same time, and it is affected
360// by the order in which we collect records from different cpus.
361// RecordCache pushes records and pops sorted record online. It uses two checks to help
362// ensure that records are popped in order. Each time we pop a record A, it is the earliest record
363// among all records in the cache. In addition, we have checks for min_cache_size and
364// min_time_diff. For min_cache_size check, we check if the cache size >= min_cache_size,
365// which is based on the assumption that if we have received (min_cache_size - 1) records
366// after record A, we are not likely to receive a record earlier than A. For min_time_diff
367// check, we check if record A is generated min_time_diff ns earlier than the latest
368// record, which is based on the assumption that if we have received a record for time t,
369// we are not likely to receive a record for time (t - min_time_diff) or earlier.
370class RecordCache {
371 public:
Yabin Cui2d6efe42016-04-01 20:22:35 -0700372 RecordCache(bool has_timestamp, size_t min_cache_size = 1000u,
Yabin Cuib7f481f2015-10-23 19:48:42 -0700373 uint64_t min_time_diff_in_ns = 1000000u);
Yabin Cuif469c3d2015-10-07 15:00:46 -0700374 ~RecordCache();
Yabin Cuib7f481f2015-10-23 19:48:42 -0700375 void Push(std::unique_ptr<Record> record);
Yabin Cui2d6efe42016-04-01 20:22:35 -0700376 void Push(std::vector<std::unique_ptr<Record>> records);
Yabin Cuif469c3d2015-10-07 15:00:46 -0700377 std::unique_ptr<Record> Pop();
378 std::vector<std::unique_ptr<Record>> PopAll();
379
380 private:
Yabin Cui4913c122016-01-11 17:15:55 -0800381 struct RecordWithSeq {
382 uint32_t seq;
383 Record *record;
384
385 bool IsHappensBefore(const RecordWithSeq& other) const;
Yabin Cuif469c3d2015-10-07 15:00:46 -0700386 };
387
Yabin Cui4913c122016-01-11 17:15:55 -0800388 struct RecordComparator {
389 bool operator()(const RecordWithSeq& r1, const RecordWithSeq& r2);
390 };
391
392 RecordWithSeq CreateRecordWithSeq(Record *r);
393
Yabin Cuif469c3d2015-10-07 15:00:46 -0700394 bool has_timestamp_;
395 size_t min_cache_size_;
396 uint64_t min_time_diff_in_ns_;
397 uint64_t last_time_;
Yabin Cui4913c122016-01-11 17:15:55 -0800398 uint32_t cur_seq_;
399 std::priority_queue<RecordWithSeq, std::vector<RecordWithSeq>,
400 RecordComparator> queue_;
Yabin Cuif469c3d2015-10-07 15:00:46 -0700401};
402
Yabin Cui2d6efe42016-04-01 20:22:35 -0700403std::unique_ptr<Record> ReadRecordFromBuffer(const perf_event_attr& attr,
404 const perf_event_header* pheader);
Yabin Cui73d80782015-07-23 21:39:57 -0700405std::vector<std::unique_ptr<Record>> ReadRecordsFromBuffer(const perf_event_attr& attr,
406 const char* buf, size_t buf_size);
Yabin Cui7d59bb42015-05-04 20:27:57 -0700407MmapRecord CreateMmapRecord(const perf_event_attr& attr, bool in_kernel, uint32_t pid, uint32_t tid,
408 uint64_t addr, uint64_t len, uint64_t pgoff,
Yabin Cui2d6efe42016-04-01 20:22:35 -0700409 const std::string& filename, uint64_t event_id);
Yabin Cui7d59bb42015-05-04 20:27:57 -0700410CommRecord CreateCommRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid,
Yabin Cui2d6efe42016-04-01 20:22:35 -0700411 const std::string& comm, uint64_t event_id);
Yabin Cui41d4ba92015-06-22 12:27:58 -0700412ForkRecord CreateForkRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid, uint32_t ppid,
Yabin Cui2d6efe42016-04-01 20:22:35 -0700413 uint32_t ptid, uint64_t event_id);
Yabin Cui8f622512015-05-05 19:58:07 -0700414BuildIdRecord CreateBuildIdRecord(bool in_kernel, pid_t pid, const BuildId& build_id,
415 const std::string& filename);
Yabin Cuib4212972016-05-25 14:08:05 -0700416std::vector<KernelSymbolRecord> CreateKernelSymbolRecords(const std::string& kallsyms);
Yabin Cui73d80782015-07-23 21:39:57 -0700417
Yabin Cui9759e1b2015-04-28 15:54:13 -0700418#endif // SIMPLE_PERF_RECORD_H_