blob: 6e5806ce090115ac3883d7182a36e631a0f53650 [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 Cui81a9d332017-12-10 13:09:07 -080031#include "CallChainJoiner.h"
Yabin Cui03381452017-12-13 11:31:53 -080032#include "OfflineUnwinder.h"
Yabin Cui9759e1b2015-04-28 15:54:13 -070033#include "perf_event.h"
34
Yabin Cui9759e1b2015-04-28 15:54:13 -070035enum user_record_type {
Yabin Cuib4212972016-05-25 14:08:05 -070036 PERF_RECORD_USER_DEFINED_TYPE_START = 64,
Yabin Cui9759e1b2015-04-28 15:54:13 -070037 PERF_RECORD_ATTR = 64,
38 PERF_RECORD_EVENT_TYPE,
39 PERF_RECORD_TRACING_DATA,
40 PERF_RECORD_BUILD_ID,
41 PERF_RECORD_FINISHED_ROUND,
Yabin Cuib4212972016-05-25 14:08:05 -070042
43 SIMPLE_PERF_RECORD_TYPE_START = 32768,
44 SIMPLE_PERF_RECORD_KERNEL_SYMBOL,
Yabin Cuic5b4a312016-10-24 13:38:38 -070045 // TODO: remove DsoRecord and SymbolRecord.
Yabin Cui767dd172016-06-02 21:02:43 -070046 SIMPLE_PERF_RECORD_DSO,
47 SIMPLE_PERF_RECORD_SYMBOL,
Yabin Cui1761a272016-06-23 17:11:14 -070048 SIMPLE_PERF_RECORD_SPLIT,
49 SIMPLE_PERF_RECORD_SPLIT_END,
Yabin Cui825e56b2016-08-26 18:25:21 -070050 SIMPLE_PERF_RECORD_EVENT_ID,
Yabin Cui81a9d332017-12-10 13:09:07 -080051 SIMPLE_PERF_RECORD_CALLCHAIN,
Yabin Cui03381452017-12-13 11:31:53 -080052 SIMPLE_PERF_RECORD_UNWINDING_RESULT,
Yabin Cui9759e1b2015-04-28 15:54:13 -070053};
54
Yabin Cui1761a272016-06-23 17:11:14 -070055// perf_event_header uses u16 to store record size. However, that is not
56// enough for storing records like KERNEL_SYMBOL or TRACING_DATA. So define
57// a simpleperf_record_header struct to store record header for simpleperf
58// defined records (type > SIMPLE_PERF_RECORD_TYPE_START).
59struct simpleperf_record_header {
60 uint32_t type;
61 uint16_t size1;
62 uint16_t size0;
63};
64
65static_assert(
66 sizeof(simpleperf_record_header) == sizeof(perf_event_header),
67 "simpleperf_record_header should have the same size as perf_event_header");
68
Yabin Cui9759e1b2015-04-28 15:54:13 -070069struct PerfSampleIpType {
70 uint64_t ip;
71};
72
73struct PerfSampleTidType {
74 uint32_t pid, tid;
75};
76
77struct PerfSampleTimeType {
78 uint64_t time;
79};
80
81struct PerfSampleAddrType {
82 uint64_t addr;
83};
84
85struct PerfSampleIdType {
86 uint64_t id;
87};
88
89struct PerfSampleStreamIdType {
90 uint64_t stream_id;
91};
92
93struct PerfSampleCpuType {
94 uint32_t cpu, res;
95};
96
97struct PerfSamplePeriodType {
98 uint64_t period;
99};
100
Yabin Cui6e8a9a42015-06-15 14:36:43 -0700101struct PerfSampleCallChainType {
Yabin Cui190a8482016-08-04 10:22:17 -0700102 uint64_t ip_nr;
Yabin Cui3d4aa262017-11-01 15:58:55 -0700103 uint64_t* ips;
Yabin Cui6e8a9a42015-06-15 14:36:43 -0700104};
105
Yabin Cuibfc11b62015-08-19 10:12:51 -0700106struct PerfSampleRawType {
Yabin Cui190a8482016-08-04 10:22:17 -0700107 uint32_t size;
108 const char* data;
Yabin Cuibfc11b62015-08-19 10:12:51 -0700109};
110
Yabin Cuib64a8632016-05-24 18:23:33 -0700111struct BranchStackItemType {
112 uint64_t from;
113 uint64_t to;
114 uint64_t flags;
115};
116
Yabin Cuiddddc062015-06-02 17:54:52 -0700117struct PerfSampleBranchStackType {
Yabin Cui190a8482016-08-04 10:22:17 -0700118 uint64_t stack_nr;
119 const BranchStackItemType* stack;
Yabin Cuiddddc062015-06-02 17:54:52 -0700120};
121
Yabin Cui76769e52015-07-13 12:23:54 -0700122struct PerfSampleRegsUserType {
123 uint64_t abi;
124 uint64_t reg_mask;
Yabin Cui190a8482016-08-04 10:22:17 -0700125 uint64_t reg_nr;
126 const uint64_t* regs;
Yabin Cui76769e52015-07-13 12:23:54 -0700127};
128
129struct PerfSampleStackUserType {
Yabin Cui190a8482016-08-04 10:22:17 -0700130 uint64_t size;
Yabin Cui3d4aa262017-11-01 15:58:55 -0700131 char* data;
Yabin Cui76769e52015-07-13 12:23:54 -0700132 uint64_t dyn_size;
133};
134
Yabin Cui1761a272016-06-23 17:11:14 -0700135struct RecordHeader {
136 public:
137 uint32_t type;
138 uint16_t misc;
139 uint32_t size;
140
141 RecordHeader() : type(0), misc(0), size(0) {}
142
Chih-Hung Hsieh5674ed82016-07-12 11:35:16 -0700143 explicit RecordHeader(const char* p) {
Yabin Cui1761a272016-06-23 17:11:14 -0700144 auto pheader = reinterpret_cast<const perf_event_header*>(p);
145 if (pheader->type < SIMPLE_PERF_RECORD_TYPE_START) {
146 type = pheader->type;
147 misc = pheader->misc;
148 size = pheader->size;
149 } else {
150 auto sheader = reinterpret_cast<const simpleperf_record_header*>(p);
151 type = sheader->type;
152 misc = 0;
153 size = (sheader->size1 << 16) | sheader->size0;
154 }
155 }
156
157 void MoveToBinaryFormat(char*& p) const {
158 if (type < SIMPLE_PERF_RECORD_TYPE_START) {
159 auto pheader = reinterpret_cast<perf_event_header*>(p);
160 pheader->type = type;
161 pheader->misc = misc;
162 CHECK_LT(size, 1u << 16);
163 pheader->size = static_cast<uint16_t>(size);
164 } else {
165 auto sheader = reinterpret_cast<simpleperf_record_header*>(p);
166 sheader->type = type;
167 CHECK_EQ(misc, 0u);
168 sheader->size1 = size >> 16;
169 sheader->size0 = size & 0xffff;
170 }
171 p += sizeof(perf_event_header);
172 }
173};
174
Yabin Cui767dd172016-06-02 21:02:43 -0700175// SampleId is optional at the end of a record in binary format. Its content is
176// determined by sample_id_all and sample_type in perf_event_attr. To avoid the
177// complexity of referring to perf_event_attr each time, we copy sample_id_all
178// and sample_type inside the SampleId structure.
Yabin Cui9759e1b2015-04-28 15:54:13 -0700179struct SampleId {
180 bool sample_id_all;
181 uint64_t sample_type;
182
Yabin Cui767dd172016-06-02 21:02:43 -0700183 PerfSampleTidType tid_data; // Valid if sample_id_all && PERF_SAMPLE_TID.
184 PerfSampleTimeType time_data; // Valid if sample_id_all && PERF_SAMPLE_TIME.
185 PerfSampleIdType id_data; // Valid if sample_id_all && PERF_SAMPLE_ID.
186 PerfSampleStreamIdType
187 stream_id_data; // Valid if sample_id_all && PERF_SAMPLE_STREAM_ID.
188 PerfSampleCpuType cpu_data; // Valid if sample_id_all && PERF_SAMPLE_CPU.
Yabin Cui9759e1b2015-04-28 15:54:13 -0700189
190 SampleId();
191
192 // Create the content of sample_id. It depends on the attr we use.
Yabin Cui2d6efe42016-04-01 20:22:35 -0700193 size_t CreateContent(const perf_event_attr& attr, uint64_t event_id);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700194
195 // Parse sample_id from binary format in the buffer pointed by p.
Yabin Cui767dd172016-06-02 21:02:43 -0700196 void ReadFromBinaryFormat(const perf_event_attr& attr, const char* p,
197 const char* end);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700198
199 // Write the binary format of sample_id to the buffer pointed by p.
200 void WriteToBinaryFormat(char*& p) const;
201 void Dump(size_t indent) const;
Yabin Cuib1a885b2016-02-14 19:18:02 -0800202 size_t Size() const;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700203};
204
Yabin Cui767dd172016-06-02 21:02:43 -0700205// Usually one record contains the following three parts in order in binary
206// format:
Yabin Cui1761a272016-06-23 17:11:14 -0700207// RecordHeader (at the head of a record, containing type and size info)
Yabin Cui9759e1b2015-04-28 15:54:13 -0700208// data depends on the record type
Yabin Cui1761a272016-06-23 17:11:14 -0700209// SampleId (optional part at the end of a record)
210// We hold the common parts (RecordHeader and SampleId) in the base class
Yabin Cui767dd172016-06-02 21:02:43 -0700211// Record, and hold the type specific data part in classes derived from Record.
Yabin Cui9759e1b2015-04-28 15:54:13 -0700212struct Record {
Yabin Cui1761a272016-06-23 17:11:14 -0700213 RecordHeader header;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700214 SampleId sample_id;
215
Yabin Cui190a8482016-08-04 10:22:17 -0700216 Record() : binary_(nullptr), own_binary_(false) {}
Yabin Cui3d4aa262017-11-01 15:58:55 -0700217 explicit Record(char* p) : header(p), binary_(p), own_binary_(false) {}
Yabin Cui190a8482016-08-04 10:22:17 -0700218 Record(Record&& other);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700219
Yabin Cui190a8482016-08-04 10:22:17 -0700220 virtual ~Record() {
221 if (own_binary_) {
222 delete[] binary_;
223 }
224 }
225
226 void OwnBinary() { own_binary_ = true; }
Yabin Cuib7f481f2015-10-23 19:48:42 -0700227
Yabin Cui767dd172016-06-02 21:02:43 -0700228 uint32_t type() const { return header.type; }
Yabin Cuib7f481f2015-10-23 19:48:42 -0700229
Yabin Cui767dd172016-06-02 21:02:43 -0700230 uint16_t misc() const { return header.misc; }
Yabin Cuib4212972016-05-25 14:08:05 -0700231
Yabin Cui1761a272016-06-23 17:11:14 -0700232 uint32_t size() const { return header.size; }
Yabin Cuib4212972016-05-25 14:08:05 -0700233
Yabin Cui767dd172016-06-02 21:02:43 -0700234 static uint32_t header_size() { return sizeof(perf_event_header); }
Yabin Cuib4212972016-05-25 14:08:05 -0700235
Yabin Cuib64a8632016-05-24 18:23:33 -0700236 bool InKernel() const {
Yabin Cui767dd172016-06-02 21:02:43 -0700237 return (header.misc & PERF_RECORD_MISC_CPUMODE_MASK) ==
238 PERF_RECORD_MISC_KERNEL;
Yabin Cuib64a8632016-05-24 18:23:33 -0700239 }
240
Yabin Cuib4212972016-05-25 14:08:05 -0700241 void SetTypeAndMisc(uint32_t type, uint16_t misc) {
242 header.type = type;
243 header.misc = misc;
244 }
245
Yabin Cui1761a272016-06-23 17:11:14 -0700246 void SetSize(uint32_t size) { header.size = size; }
Yabin Cuib4212972016-05-25 14:08:05 -0700247
Yabin Cui9759e1b2015-04-28 15:54:13 -0700248 void Dump(size_t indent = 0) const;
Yabin Cui190a8482016-08-04 10:22:17 -0700249
250 const char* Binary() const { return binary_; }
Yabin Cui3d4aa262017-11-01 15:58:55 -0700251 char* BinaryForTestingOnly() { return binary_; }
Yabin Cui190a8482016-08-04 10:22:17 -0700252
Yabin Cuif469c3d2015-10-07 15:00:46 -0700253 virtual uint64_t Timestamp() const;
Yabin Cuifc22b8f2016-08-04 14:47:50 -0700254 virtual uint32_t Cpu() const;
Yabin Cuiada97db2017-02-23 15:54:11 -0800255 virtual uint64_t Id() const;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700256
257 protected:
Yabin Cui3d4aa262017-11-01 15:58:55 -0700258 void UpdateBinary(char* new_binary);
Yabin Cuicb84c982015-09-30 17:22:35 -0700259 virtual void DumpData(size_t) const = 0;
Yabin Cui190a8482016-08-04 10:22:17 -0700260
Yabin Cui3d4aa262017-11-01 15:58:55 -0700261 char* binary_;
Yabin Cui190a8482016-08-04 10:22:17 -0700262 bool own_binary_;
263
264 DISALLOW_COPY_AND_ASSIGN(Record);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700265};
266
267struct MmapRecord : public Record {
268 struct MmapRecordDataType {
269 uint32_t pid, tid;
270 uint64_t addr;
271 uint64_t len;
272 uint64_t pgoff;
Yabin Cui190a8482016-08-04 10:22:17 -0700273 };
274 const MmapRecordDataType* data;
275 const char* filename;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700276
Yabin Cui3d4aa262017-11-01 15:58:55 -0700277 MmapRecord(const perf_event_attr& attr, char* p);
Yabin Cui8f622512015-05-05 19:58:07 -0700278
Yabin Cui190a8482016-08-04 10:22:17 -0700279 MmapRecord(const perf_event_attr& attr, bool in_kernel, uint32_t pid,
280 uint32_t tid, uint64_t addr, uint64_t len, uint64_t pgoff,
281 const std::string& filename, uint64_t event_id, uint64_t time = 0);
282
283 void SetDataAndFilename(const MmapRecordDataType& data,
284 const std::string& filename);
Yabin Cui767dd172016-06-02 21:02:43 -0700285
Yabin Cui8f622512015-05-05 19:58:07 -0700286 protected:
287 void DumpData(size_t indent) const override;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700288};
289
Yabin Cui41d4ba92015-06-22 12:27:58 -0700290struct Mmap2Record : public Record {
291 struct Mmap2RecordDataType {
292 uint32_t pid, tid;
293 uint64_t addr;
294 uint64_t len;
295 uint64_t pgoff;
296 uint32_t maj;
297 uint32_t min;
298 uint64_t ino;
299 uint64_t ino_generation;
300 uint32_t prot, flags;
Yabin Cui190a8482016-08-04 10:22:17 -0700301 };
302 const Mmap2RecordDataType* data;
303 const char* filename;
Yabin Cui41d4ba92015-06-22 12:27:58 -0700304
Yabin Cui3d4aa262017-11-01 15:58:55 -0700305 Mmap2Record(const perf_event_attr& attr, char* p);
Yabin Cuicdc11a32018-03-20 15:29:03 -0700306 Mmap2Record(const perf_event_attr& attr, bool in_kernel, uint32_t pid, uint32_t tid,
307 uint64_t addr, uint64_t len, uint64_t pgoff, uint32_t prot,
308 const std::string& filename, uint64_t event_id, uint64_t time = 0);
Yabin Cui190a8482016-08-04 10:22:17 -0700309
310 void SetDataAndFilename(const Mmap2RecordDataType& data,
311 const std::string& filename);
Yabin Cui41d4ba92015-06-22 12:27:58 -0700312
313 protected:
314 void DumpData(size_t indent) const override;
315};
316
Yabin Cui9759e1b2015-04-28 15:54:13 -0700317struct CommRecord : public Record {
318 struct CommRecordDataType {
319 uint32_t pid, tid;
Yabin Cui190a8482016-08-04 10:22:17 -0700320 };
321 const CommRecordDataType* data;
322 const char* comm;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700323
Yabin Cui3d4aa262017-11-01 15:58:55 -0700324 CommRecord(const perf_event_attr& attr, char* p);
Yabin Cui8f622512015-05-05 19:58:07 -0700325
Yabin Cui190a8482016-08-04 10:22:17 -0700326 CommRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid,
Yabin Cui2597ef02016-10-19 11:28:48 -0700327 const std::string& comm, uint64_t event_id, uint64_t time);
Yabin Cui767dd172016-06-02 21:02:43 -0700328
Yabin Cui8f622512015-05-05 19:58:07 -0700329 protected:
330 void DumpData(size_t indent) const override;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700331};
332
Yabin Cui41d4ba92015-06-22 12:27:58 -0700333struct ExitOrForkRecord : public Record {
334 struct ExitOrForkRecordDataType {
Yabin Cui9759e1b2015-04-28 15:54:13 -0700335 uint32_t pid, ppid;
336 uint32_t tid, ptid;
337 uint64_t time;
Yabin Cui190a8482016-08-04 10:22:17 -0700338 };
339 const ExitOrForkRecordDataType* data;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700340
Yabin Cui3d4aa262017-11-01 15:58:55 -0700341 ExitOrForkRecord(const perf_event_attr& attr, char* p);
Yabin Cui190a8482016-08-04 10:22:17 -0700342
343 ExitOrForkRecord() : data(nullptr) {}
Yabin Cui8f622512015-05-05 19:58:07 -0700344
345 protected:
Yabin Cui9759e1b2015-04-28 15:54:13 -0700346 void DumpData(size_t indent) const override;
347};
348
Yabin Cui41d4ba92015-06-22 12:27:58 -0700349struct ExitRecord : public ExitOrForkRecord {
Yabin Cui3d4aa262017-11-01 15:58:55 -0700350 ExitRecord(const perf_event_attr& attr, char* p)
Yabin Cui1761a272016-06-23 17:11:14 -0700351 : ExitOrForkRecord(attr, p) {}
Yabin Cui41d4ba92015-06-22 12:27:58 -0700352};
353
354struct ForkRecord : public ExitOrForkRecord {
Yabin Cui3d4aa262017-11-01 15:58:55 -0700355 ForkRecord(const perf_event_attr& attr, char* p)
Yabin Cui1761a272016-06-23 17:11:14 -0700356 : ExitOrForkRecord(attr, p) {}
Yabin Cui767dd172016-06-02 21:02:43 -0700357
Yabin Cui190a8482016-08-04 10:22:17 -0700358 ForkRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid,
359 uint32_t ppid, uint32_t ptid, uint64_t event_id);
Yabin Cui41d4ba92015-06-22 12:27:58 -0700360};
361
Yabin Cuie5adc132016-06-22 11:37:26 -0700362struct LostRecord : public Record {
363 uint64_t id;
364 uint64_t lost;
365
Yabin Cui3d4aa262017-11-01 15:58:55 -0700366 LostRecord(const perf_event_attr& attr, char* p);
Yabin Cuie5adc132016-06-22 11:37:26 -0700367
368 protected:
369 void DumpData(size_t indent) const override;
370};
371
Yabin Cui9759e1b2015-04-28 15:54:13 -0700372struct SampleRecord : public Record {
Yabin Cui767dd172016-06-02 21:02:43 -0700373 uint64_t sample_type; // sample_type is a bit mask determining which fields
374 // below are valid.
Yabin Cui9759e1b2015-04-28 15:54:13 -0700375
376 PerfSampleIpType ip_data; // Valid if PERF_SAMPLE_IP.
377 PerfSampleTidType tid_data; // Valid if PERF_SAMPLE_TID.
378 PerfSampleTimeType time_data; // Valid if PERF_SAMPLE_TIME.
379 PerfSampleAddrType addr_data; // Valid if PERF_SAMPLE_ADDR.
380 PerfSampleIdType id_data; // Valid if PERF_SAMPLE_ID.
381 PerfSampleStreamIdType stream_id_data; // Valid if PERF_SAMPLE_STREAM_ID.
382 PerfSampleCpuType cpu_data; // Valid if PERF_SAMPLE_CPU.
383 PerfSamplePeriodType period_data; // Valid if PERF_SAMPLE_PERIOD.
384
Yabin Cui767dd172016-06-02 21:02:43 -0700385 PerfSampleCallChainType callchain_data; // Valid if PERF_SAMPLE_CALLCHAIN.
386 PerfSampleRawType raw_data; // Valid if PERF_SAMPLE_RAW.
387 PerfSampleBranchStackType
388 branch_stack_data; // Valid if PERF_SAMPLE_BRANCH_STACK.
389 PerfSampleRegsUserType regs_user_data; // Valid if PERF_SAMPLE_REGS_USER.
390 PerfSampleStackUserType stack_user_data; // Valid if PERF_SAMPLE_STACK_USER.
Yabin Cuiddddc062015-06-02 17:54:52 -0700391
Yabin Cui3d4aa262017-11-01 15:58:55 -0700392 SampleRecord(const perf_event_attr& attr, char* p);
Yabin Cui2597ef02016-10-19 11:28:48 -0700393 SampleRecord(const perf_event_attr& attr, uint64_t id, uint64_t ip,
394 uint32_t pid, uint32_t tid, uint64_t time, uint32_t cpu,
395 uint64_t period, const std::vector<uint64_t>& ips);
396
Yabin Cui190a8482016-08-04 10:22:17 -0700397 void ReplaceRegAndStackWithCallChain(const std::vector<uint64_t>& ips);
Yabin Cuid3cb3b02017-07-24 14:59:46 -0700398 size_t ExcludeKernelCallChain();
Yabin Cui81a9d332017-12-10 13:09:07 -0800399 bool HasUserCallChain() const;
400 void UpdateUserCallChain(const std::vector<uint64_t>& user_ips);
Yabin Cui5a5e92c2018-01-29 16:02:16 -0800401 void RemoveInvalidStackData();
Yabin Cui81a9d332017-12-10 13:09:07 -0800402
Yabin Cuif469c3d2015-10-07 15:00:46 -0700403 uint64_t Timestamp() const override;
Yabin Cuifc22b8f2016-08-04 14:47:50 -0700404 uint32_t Cpu() const override;
Yabin Cuiada97db2017-02-23 15:54:11 -0800405 uint64_t Id() const override;
Yabin Cui8f622512015-05-05 19:58:07 -0700406
Yabin Cuicf31e9d2016-07-14 14:29:33 -0700407 uint64_t GetValidStackSize() const {
408 // If stack_user_data.dyn_size == 0, it may be because the kernel misses
409 // the patch to update dyn_size, like in N9 (See b/22612370). So assume
410 // all stack data is valid if dyn_size == 0.
411 if (stack_user_data.dyn_size == 0) {
Yabin Cui190a8482016-08-04 10:22:17 -0700412 return stack_user_data.size;
Yabin Cuicf31e9d2016-07-14 14:29:33 -0700413 }
414 return stack_user_data.dyn_size;
415 }
416
Yabin Cui3d4aa262017-11-01 15:58:55 -0700417 void AdjustCallChainGeneratedByKernel();
418
Yabin Cui8f622512015-05-05 19:58:07 -0700419 protected:
420 void DumpData(size_t indent) const override;
421};
422
Yabin Cui767dd172016-06-02 21:02:43 -0700423// BuildIdRecord is defined in user-space, stored in BuildId feature section in
424// record file.
Yabin Cui8f622512015-05-05 19:58:07 -0700425struct BuildIdRecord : public Record {
426 uint32_t pid;
427 BuildId build_id;
Yabin Cui190a8482016-08-04 10:22:17 -0700428 const char* filename;
Yabin Cui8f622512015-05-05 19:58:07 -0700429
Yabin Cui3d4aa262017-11-01 15:58:55 -0700430 explicit BuildIdRecord(char* p);
Yabin Cuicb84c982015-09-30 17:22:35 -0700431
Yabin Cui190a8482016-08-04 10:22:17 -0700432 BuildIdRecord(bool in_kernel, pid_t pid, const BuildId& build_id,
433 const std::string& filename);
Yabin Cui767dd172016-06-02 21:02:43 -0700434
Yabin Cuicb84c982015-09-30 17:22:35 -0700435 protected:
436 void DumpData(size_t indent) const override;
437};
438
Yabin Cuib4212972016-05-25 14:08:05 -0700439struct KernelSymbolRecord : public Record {
Yabin Cui190a8482016-08-04 10:22:17 -0700440 uint32_t kallsyms_size;
441 const char* kallsyms;
Yabin Cuib4212972016-05-25 14:08:05 -0700442
Yabin Cui3d4aa262017-11-01 15:58:55 -0700443 explicit KernelSymbolRecord(char* p);
Yabin Cuib4212972016-05-25 14:08:05 -0700444
Chih-Hung Hsieh1786a882016-08-11 11:06:00 -0700445 explicit KernelSymbolRecord(const std::string& kallsyms);
Yabin Cui767dd172016-06-02 21:02:43 -0700446
Yabin Cuib4212972016-05-25 14:08:05 -0700447 protected:
448 void DumpData(size_t indent) const override;
449};
450
Yabin Cui767dd172016-06-02 21:02:43 -0700451struct DsoRecord : public Record {
452 uint64_t dso_type;
453 uint64_t dso_id;
Yabin Cuic855ecc2016-07-11 17:04:54 -0700454 uint64_t min_vaddr;
Yabin Cui190a8482016-08-04 10:22:17 -0700455 const char* dso_name;
Yabin Cui767dd172016-06-02 21:02:43 -0700456
Yabin Cui3d4aa262017-11-01 15:58:55 -0700457 explicit DsoRecord(char* p);
Yabin Cui767dd172016-06-02 21:02:43 -0700458
Yabin Cui190a8482016-08-04 10:22:17 -0700459 DsoRecord(uint64_t dso_type, uint64_t dso_id, const std::string& dso_name,
460 uint64_t min_vaddr);
Yabin Cui767dd172016-06-02 21:02:43 -0700461
462 protected:
463 void DumpData(size_t indent) const override;
464};
465
466struct SymbolRecord : public Record {
467 uint64_t addr;
468 uint64_t len;
469 uint64_t dso_id;
Yabin Cui190a8482016-08-04 10:22:17 -0700470 const char* name;
Yabin Cui767dd172016-06-02 21:02:43 -0700471
Yabin Cui3d4aa262017-11-01 15:58:55 -0700472 explicit SymbolRecord(char* p);
Yabin Cui767dd172016-06-02 21:02:43 -0700473
Yabin Cui190a8482016-08-04 10:22:17 -0700474 SymbolRecord(uint64_t addr, uint64_t len, const std::string& name,
475 uint64_t dso_id);
Yabin Cui1761a272016-06-23 17:11:14 -0700476
Yabin Cui4f41df62016-06-01 17:29:06 -0700477 protected:
478 void DumpData(size_t indent) const override;
479};
Yabin Cui767dd172016-06-02 21:02:43 -0700480
Yabin Cui4f41df62016-06-01 17:29:06 -0700481struct TracingDataRecord : public Record {
Yabin Cui190a8482016-08-04 10:22:17 -0700482 uint32_t data_size;
483 const char* data;
Yabin Cui4f41df62016-06-01 17:29:06 -0700484
Yabin Cui3d4aa262017-11-01 15:58:55 -0700485 explicit TracingDataRecord(char* p);
Yabin Cui4f41df62016-06-01 17:29:06 -0700486
Chih-Hung Hsieh1786a882016-08-11 11:06:00 -0700487 explicit TracingDataRecord(const std::vector<char>& tracing_data);
Yabin Cuie5adc132016-06-22 11:37:26 -0700488
Yabin Cui767dd172016-06-02 21:02:43 -0700489 protected:
490 void DumpData(size_t indent) const override;
491};
492
Yabin Cui825e56b2016-08-26 18:25:21 -0700493struct EventIdRecord : public Record {
494 uint64_t count;
495 struct EventIdData {
496 uint64_t attr_id;
497 uint64_t event_id;
498 } const* data;
499
Yabin Cui3d4aa262017-11-01 15:58:55 -0700500 explicit EventIdRecord(char* p);
Yabin Cui825e56b2016-08-26 18:25:21 -0700501
502 explicit EventIdRecord(const std::vector<uint64_t>& data);
503
504 protected:
505 void DumpData(size_t indent) const override;
506};
507
Yabin Cui81a9d332017-12-10 13:09:07 -0800508struct CallChainRecord : public Record {
509 uint32_t pid;
510 uint32_t tid;
511 uint64_t chain_type;
512 uint64_t time;
513 uint64_t ip_nr;
514 uint64_t* ips;
515 uint64_t* sps;
516
517 explicit CallChainRecord(char* p);
518
519 CallChainRecord(pid_t pid, pid_t tid, simpleperf::CallChainJoiner::ChainType type, uint64_t time,
520 const std::vector<uint64_t>& ips, const std::vector<uint64_t>& sps);
521
522 uint64_t Timestamp() const override {
523 return time;
524 }
525
526 protected:
527 void DumpData(size_t indent) const override;
528};
529
Yabin Cui03381452017-12-13 11:31:53 -0800530struct UnwindingResultRecord : public Record {
531 uint64_t time;
Yabin Cui85faad92018-01-04 17:17:55 -0800532 simpleperf::UnwindingResult unwinding_result;
Yabin Cui03381452017-12-13 11:31:53 -0800533
534 explicit UnwindingResultRecord(char* p);
535
Yabin Cui85faad92018-01-04 17:17:55 -0800536 UnwindingResultRecord(uint64_t time, const simpleperf::UnwindingResult& unwinding_result);
Yabin Cui03381452017-12-13 11:31:53 -0800537
538 uint64_t Timestamp() const override {
539 return time;
540 }
541
542 protected:
543 void DumpData(size_t indent) const override;
544};
545
Yabin Cui767dd172016-06-02 21:02:43 -0700546// UnknownRecord is used for unknown record types, it makes sure all unknown
Yabin Cui4f41df62016-06-01 17:29:06 -0700547// records are not changed when modifying perf.data.
Yabin Cuicb84c982015-09-30 17:22:35 -0700548struct UnknownRecord : public Record {
Yabin Cui190a8482016-08-04 10:22:17 -0700549 const char* data;
Yabin Cuicb84c982015-09-30 17:22:35 -0700550
Yabin Cui3d4aa262017-11-01 15:58:55 -0700551 explicit UnknownRecord(char* p);
Yabin Cui8f622512015-05-05 19:58:07 -0700552
553 protected:
Yabin Cui9759e1b2015-04-28 15:54:13 -0700554 void DumpData(size_t indent) const override;
555};
556
Yabin Cui190a8482016-08-04 10:22:17 -0700557// Read record from the buffer pointed by [p]. But the record doesn't own
558// the buffer.
Yabin Cui3d4aa262017-11-01 15:58:55 -0700559std::unique_ptr<Record> ReadRecordFromBuffer(const perf_event_attr& attr, uint32_t type, char* p);
Yabin Cui190a8482016-08-04 10:22:17 -0700560
561// Read record from the buffer pointed by [p]. And the record owns the buffer.
562std::unique_ptr<Record> ReadRecordFromOwnedBuffer(const perf_event_attr& attr,
Yabin Cui3d4aa262017-11-01 15:58:55 -0700563 uint32_t type, char* p);
Yabin Cui190a8482016-08-04 10:22:17 -0700564
565// Read records from the buffer pointed by [buf]. None of the records own
566// the buffer.
Yabin Cui767dd172016-06-02 21:02:43 -0700567std::vector<std::unique_ptr<Record>> ReadRecordsFromBuffer(
Yabin Cui3d4aa262017-11-01 15:58:55 -0700568 const perf_event_attr& attr, char* buf, size_t buf_size);
Yabin Cui767dd172016-06-02 21:02:43 -0700569
Yabin Cui2ea6de12016-10-24 19:13:09 -0700570// Read one record from the buffer pointed by [p]. But the record doesn't
571// own the buffer.
Yabin Cui3d4aa262017-11-01 15:58:55 -0700572std::unique_ptr<Record> ReadRecordFromBuffer(const perf_event_attr& attr, char* p);
Yabin Cui2ea6de12016-10-24 19:13:09 -0700573
Yabin Cuif469c3d2015-10-07 15:00:46 -0700574// RecordCache is a cache used when receiving records from the kernel.
575// It sorts received records based on type and timestamp, and pops records
576// in sorted order. Records from the kernel need to be sorted because
577// records may come from different cpus at the same time, and it is affected
578// by the order in which we collect records from different cpus.
Yabin Cui767dd172016-06-02 21:02:43 -0700579// RecordCache pushes records and pops sorted record online. It uses two checks
580// to help ensure that records are popped in order. Each time we pop a record A,
581// it is the earliest record among all records in the cache. In addition, we
582// have checks for min_cache_size and min_time_diff. For min_cache_size check,
583// we check if the cache size >= min_cache_size, which is based on the
584// assumption that if we have received (min_cache_size - 1) records after
585// record A, we are not likely to receive a record earlier than A. For
586// min_time_diff check, we check if record A is generated min_time_diff ns
587// earlier than the latest record, which is based on the assumption that if we
588// have received a record for time t, we are not likely to receive a record for
589// time (t - min_time_diff) or earlier.
Yabin Cuif469c3d2015-10-07 15:00:46 -0700590class RecordCache {
591 public:
Chih-Hung Hsieh5674ed82016-07-12 11:35:16 -0700592 explicit RecordCache(bool has_timestamp, size_t min_cache_size = 1000u,
Yabin Cuicf31e9d2016-07-14 14:29:33 -0700593 uint64_t min_time_diff_in_ns = 1000000u);
Yabin Cuif469c3d2015-10-07 15:00:46 -0700594 ~RecordCache();
Yabin Cuib7f481f2015-10-23 19:48:42 -0700595 void Push(std::unique_ptr<Record> record);
Yabin Cui2d6efe42016-04-01 20:22:35 -0700596 void Push(std::vector<std::unique_ptr<Record>> records);
Yabin Cuif469c3d2015-10-07 15:00:46 -0700597 std::unique_ptr<Record> Pop();
598 std::vector<std::unique_ptr<Record>> PopAll();
Yabin Cuifc22b8f2016-08-04 14:47:50 -0700599 std::unique_ptr<Record> ForcedPop();
Yabin Cuif469c3d2015-10-07 15:00:46 -0700600
601 private:
Yabin Cui4913c122016-01-11 17:15:55 -0800602 struct RecordWithSeq {
603 uint32_t seq;
Yabin Cui767dd172016-06-02 21:02:43 -0700604 Record* record;
Yabin Cui4913c122016-01-11 17:15:55 -0800605
Yabin Cuicf31e9d2016-07-14 14:29:33 -0700606 RecordWithSeq(uint32_t seq, Record* record) : seq(seq), record(record) {}
Yabin Cui4913c122016-01-11 17:15:55 -0800607 bool IsHappensBefore(const RecordWithSeq& other) const;
Yabin Cuif469c3d2015-10-07 15:00:46 -0700608 };
609
Yabin Cui4913c122016-01-11 17:15:55 -0800610 struct RecordComparator {
611 bool operator()(const RecordWithSeq& r1, const RecordWithSeq& r2);
612 };
613
Yabin Cuif469c3d2015-10-07 15:00:46 -0700614 bool has_timestamp_;
615 size_t min_cache_size_;
616 uint64_t min_time_diff_in_ns_;
617 uint64_t last_time_;
Yabin Cui4913c122016-01-11 17:15:55 -0800618 uint32_t cur_seq_;
619 std::priority_queue<RecordWithSeq, std::vector<RecordWithSeq>,
Yabin Cui767dd172016-06-02 21:02:43 -0700620 RecordComparator> queue_;
Yabin Cuif469c3d2015-10-07 15:00:46 -0700621};
622
Yabin Cui9759e1b2015-04-28 15:54:13 -0700623#endif // SIMPLE_PERF_RECORD_H_