blob: 8b17bc10915fafe5555e975df3b2a3cc3cc506ed [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 Cui8f622512015-05-05 19:58:07 -070028#include "build_id.h"
Yabin Cui9759e1b2015-04-28 15:54:13 -070029#include "perf_event.h"
30
31struct KernelMmap;
32struct ModuleMmap;
33struct ThreadComm;
34struct ThreadMmap;
35
36enum user_record_type {
37 PERF_RECORD_ATTR = 64,
38 PERF_RECORD_EVENT_TYPE,
39 PERF_RECORD_TRACING_DATA,
40 PERF_RECORD_BUILD_ID,
41 PERF_RECORD_FINISHED_ROUND,
42};
43
44struct PerfSampleIpType {
45 uint64_t ip;
46};
47
48struct PerfSampleTidType {
49 uint32_t pid, tid;
50};
51
52struct PerfSampleTimeType {
53 uint64_t time;
54};
55
56struct PerfSampleAddrType {
57 uint64_t addr;
58};
59
60struct PerfSampleIdType {
61 uint64_t id;
62};
63
64struct PerfSampleStreamIdType {
65 uint64_t stream_id;
66};
67
68struct PerfSampleCpuType {
69 uint32_t cpu, res;
70};
71
72struct PerfSamplePeriodType {
73 uint64_t period;
74};
75
Yabin Cui6e8a9a42015-06-15 14:36:43 -070076struct PerfSampleCallChainType {
77 std::vector<uint64_t> ips;
78};
79
Yabin Cuibfc11b62015-08-19 10:12:51 -070080struct PerfSampleRawType {
81 std::vector<char> data;
82};
83
Yabin Cuiddddc062015-06-02 17:54:52 -070084struct PerfSampleBranchStackType {
85 struct BranchStackItemType {
86 uint64_t from;
87 uint64_t to;
88 uint64_t flags;
89 };
90 std::vector<BranchStackItemType> stack;
91};
92
Yabin Cui76769e52015-07-13 12:23:54 -070093struct PerfSampleRegsUserType {
94 uint64_t abi;
95 uint64_t reg_mask;
96 std::vector<uint64_t> regs;
97};
98
99struct PerfSampleStackUserType {
100 std::vector<char> data;
101 uint64_t dyn_size;
102};
103
Yabin Cui9759e1b2015-04-28 15:54:13 -0700104// SampleId is optional at the end of a record in binary format. Its content is determined by
105// sample_id_all and sample_type in perf_event_attr. To avoid the complexity of referring to
106// perf_event_attr each time, we copy sample_id_all and sample_type inside the SampleId structure.
107struct SampleId {
108 bool sample_id_all;
109 uint64_t sample_type;
110
111 PerfSampleTidType tid_data; // Valid if sample_id_all && PERF_SAMPLE_TID.
112 PerfSampleTimeType time_data; // Valid if sample_id_all && PERF_SAMPLE_TIME.
113 PerfSampleIdType id_data; // Valid if sample_id_all && PERF_SAMPLE_ID.
114 PerfSampleStreamIdType stream_id_data; // Valid if sample_id_all && PERF_SAMPLE_STREAM_ID.
115 PerfSampleCpuType cpu_data; // Valid if sample_id_all && PERF_SAMPLE_CPU.
116
117 SampleId();
118
119 // Create the content of sample_id. It depends on the attr we use.
Yabin Cui2d6efe42016-04-01 20:22:35 -0700120 size_t CreateContent(const perf_event_attr& attr, uint64_t event_id);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700121
122 // Parse sample_id from binary format in the buffer pointed by p.
123 void ReadFromBinaryFormat(const perf_event_attr& attr, const char* p, const char* end);
124
125 // Write the binary format of sample_id to the buffer pointed by p.
126 void WriteToBinaryFormat(char*& p) const;
127 void Dump(size_t indent) const;
Yabin Cuib1a885b2016-02-14 19:18:02 -0800128 size_t Size() const;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700129};
130
131// Usually one record contains the following three parts in order in binary format:
132// perf_event_header (at the head of a record, containing type and size information)
133// data depends on the record type
134// sample_id (optional part at the end of a record)
135// We hold the common parts (perf_event_header and sample_id) in the base class Record, and
136// hold the type specific data part in classes derived from Record.
137struct Record {
138 perf_event_header header;
139 SampleId sample_id;
140
141 Record();
142 Record(const perf_event_header* pheader);
143
144 virtual ~Record() {
145 }
146
Yabin Cuib7f481f2015-10-23 19:48:42 -0700147 size_t size() const {
148 return header.size;
149 }
150
151 uint32_t type() const {
152 return header.type;
153 }
154
Yabin Cui9759e1b2015-04-28 15:54:13 -0700155 void Dump(size_t indent = 0) const;
Yabin Cuicb84c982015-09-30 17:22:35 -0700156 virtual std::vector<char> BinaryFormat() const = 0;
Yabin Cuif469c3d2015-10-07 15:00:46 -0700157 virtual uint64_t Timestamp() const;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700158
159 protected:
Yabin Cuicb84c982015-09-30 17:22:35 -0700160 virtual void DumpData(size_t) const = 0;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700161};
162
163struct MmapRecord : public Record {
164 struct MmapRecordDataType {
165 uint32_t pid, tid;
166 uint64_t addr;
167 uint64_t len;
168 uint64_t pgoff;
169 } data;
170 std::string filename;
171
Yabin Cuicb84c982015-09-30 17:22:35 -0700172 MmapRecord() { // For CreateMmapRecord.
Yabin Cui9759e1b2015-04-28 15:54:13 -0700173 }
174
175 MmapRecord(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cuicb84c982015-09-30 17:22:35 -0700176 std::vector<char> BinaryFormat() const override;
Yabin Cuib1a885b2016-02-14 19:18:02 -0800177 void AdjustSizeBasedOnData();
Yabin Cui8f622512015-05-05 19:58:07 -0700178
179 protected:
180 void DumpData(size_t indent) const override;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700181};
182
Yabin Cui41d4ba92015-06-22 12:27:58 -0700183struct Mmap2Record : public Record {
184 struct Mmap2RecordDataType {
185 uint32_t pid, tid;
186 uint64_t addr;
187 uint64_t len;
188 uint64_t pgoff;
189 uint32_t maj;
190 uint32_t min;
191 uint64_t ino;
192 uint64_t ino_generation;
193 uint32_t prot, flags;
194 } data;
195 std::string filename;
196
197 Mmap2Record() {
198 }
199
200 Mmap2Record(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cuicb84c982015-09-30 17:22:35 -0700201 std::vector<char> BinaryFormat() const override;
Yabin Cuib1a885b2016-02-14 19:18:02 -0800202 void AdjustSizeBasedOnData();
Yabin Cui41d4ba92015-06-22 12:27:58 -0700203
204 protected:
205 void DumpData(size_t indent) const override;
206};
207
Yabin Cui9759e1b2015-04-28 15:54:13 -0700208struct CommRecord : public Record {
209 struct CommRecordDataType {
210 uint32_t pid, tid;
211 } data;
212 std::string comm;
213
214 CommRecord() {
215 }
216
217 CommRecord(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cuicb84c982015-09-30 17:22:35 -0700218 std::vector<char> BinaryFormat() const override;
Yabin Cui8f622512015-05-05 19:58:07 -0700219
220 protected:
221 void DumpData(size_t indent) const override;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700222};
223
Yabin Cui41d4ba92015-06-22 12:27:58 -0700224struct ExitOrForkRecord : public Record {
225 struct ExitOrForkRecordDataType {
Yabin Cui9759e1b2015-04-28 15:54:13 -0700226 uint32_t pid, ppid;
227 uint32_t tid, ptid;
228 uint64_t time;
229 } data;
230
Yabin Cui41d4ba92015-06-22 12:27:58 -0700231 ExitOrForkRecord() {
232 }
233 ExitOrForkRecord(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 Cui8f622512015-05-05 19:58:07 -0700235
236 protected:
Yabin Cui9759e1b2015-04-28 15:54:13 -0700237 void DumpData(size_t indent) const override;
238};
239
Yabin Cui41d4ba92015-06-22 12:27:58 -0700240struct ExitRecord : public ExitOrForkRecord {
241 ExitRecord(const perf_event_attr& attr, const perf_event_header* pheader)
242 : ExitOrForkRecord(attr, pheader) {
243 }
244};
245
246struct ForkRecord : public ExitOrForkRecord {
247 ForkRecord() {
248 }
249 ForkRecord(const perf_event_attr& attr, const perf_event_header* pheader)
250 : ExitOrForkRecord(attr, pheader) {
251 }
Yabin Cui41d4ba92015-06-22 12:27:58 -0700252};
253
Yabin Cui9759e1b2015-04-28 15:54:13 -0700254struct SampleRecord : public Record {
255 uint64_t sample_type; // sample_type is a bit mask determining which fields below are valid.
256
257 PerfSampleIpType ip_data; // Valid if PERF_SAMPLE_IP.
258 PerfSampleTidType tid_data; // Valid if PERF_SAMPLE_TID.
259 PerfSampleTimeType time_data; // Valid if PERF_SAMPLE_TIME.
260 PerfSampleAddrType addr_data; // Valid if PERF_SAMPLE_ADDR.
261 PerfSampleIdType id_data; // Valid if PERF_SAMPLE_ID.
262 PerfSampleStreamIdType stream_id_data; // Valid if PERF_SAMPLE_STREAM_ID.
263 PerfSampleCpuType cpu_data; // Valid if PERF_SAMPLE_CPU.
264 PerfSamplePeriodType period_data; // Valid if PERF_SAMPLE_PERIOD.
265
Yabin Cui6e8a9a42015-06-15 14:36:43 -0700266 PerfSampleCallChainType callchain_data; // Valid if PERF_SAMPLE_CALLCHAIN.
Yabin Cuibfc11b62015-08-19 10:12:51 -0700267 PerfSampleRawType raw_data; // Valid if PERF_SAMPLE_RAW.
Yabin Cuiddddc062015-06-02 17:54:52 -0700268 PerfSampleBranchStackType branch_stack_data; // Valid if PERF_SAMPLE_BRANCH_STACK.
Yabin Cui76769e52015-07-13 12:23:54 -0700269 PerfSampleRegsUserType regs_user_data; // Valid if PERF_SAMPLE_REGS_USER.
270 PerfSampleStackUserType stack_user_data; // Valid if PERF_SAMPLE_STACK_USER.
Yabin Cuiddddc062015-06-02 17:54:52 -0700271
Yabin Cui9759e1b2015-04-28 15:54:13 -0700272 SampleRecord(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cuicb84c982015-09-30 17:22:35 -0700273 std::vector<char> BinaryFormat() const override;
274 void AdjustSizeBasedOnData();
Yabin Cuif469c3d2015-10-07 15:00:46 -0700275 uint64_t Timestamp() const override;
Yabin Cui8f622512015-05-05 19:58:07 -0700276
277 protected:
278 void DumpData(size_t indent) const override;
279};
280
281// BuildIdRecord is defined in user-space, stored in BuildId feature section in record file.
282struct BuildIdRecord : public Record {
283 uint32_t pid;
284 BuildId build_id;
285 std::string filename;
286
287 BuildIdRecord() {
288 }
289
290 BuildIdRecord(const perf_event_header* pheader);
Yabin Cuicb84c982015-09-30 17:22:35 -0700291 std::vector<char> BinaryFormat() const override;
292
293 protected:
294 void DumpData(size_t indent) const override;
295};
296
297// UnknownRecord is used for unknown record types, it makes sure all unknown records
298// are not changed when modifying perf.data.
299struct UnknownRecord : public Record {
300 std::vector<char> data;
301
302 UnknownRecord(const perf_event_header* pheader);
303 std::vector<char> BinaryFormat() const override;
Yabin Cui8f622512015-05-05 19:58:07 -0700304
305 protected:
Yabin Cui9759e1b2015-04-28 15:54:13 -0700306 void DumpData(size_t indent) const override;
307};
308
Yabin Cuif469c3d2015-10-07 15:00:46 -0700309// RecordCache is a cache used when receiving records from the kernel.
310// It sorts received records based on type and timestamp, and pops records
311// in sorted order. Records from the kernel need to be sorted because
312// records may come from different cpus at the same time, and it is affected
313// by the order in which we collect records from different cpus.
314// RecordCache pushes records and pops sorted record online. It uses two checks to help
315// ensure that records are popped in order. Each time we pop a record A, it is the earliest record
316// among all records in the cache. In addition, we have checks for min_cache_size and
317// min_time_diff. For min_cache_size check, we check if the cache size >= min_cache_size,
318// which is based on the assumption that if we have received (min_cache_size - 1) records
319// after record A, we are not likely to receive a record earlier than A. For min_time_diff
320// check, we check if record A is generated min_time_diff ns earlier than the latest
321// record, which is based on the assumption that if we have received a record for time t,
322// we are not likely to receive a record for time (t - min_time_diff) or earlier.
323class RecordCache {
324 public:
Yabin Cui2d6efe42016-04-01 20:22:35 -0700325 RecordCache(bool has_timestamp, size_t min_cache_size = 1000u,
Yabin Cuib7f481f2015-10-23 19:48:42 -0700326 uint64_t min_time_diff_in_ns = 1000000u);
Yabin Cuif469c3d2015-10-07 15:00:46 -0700327 ~RecordCache();
Yabin Cuib7f481f2015-10-23 19:48:42 -0700328 void Push(std::unique_ptr<Record> record);
Yabin Cui2d6efe42016-04-01 20:22:35 -0700329 void Push(std::vector<std::unique_ptr<Record>> records);
Yabin Cuif469c3d2015-10-07 15:00:46 -0700330 std::unique_ptr<Record> Pop();
331 std::vector<std::unique_ptr<Record>> PopAll();
332
333 private:
Yabin Cui4913c122016-01-11 17:15:55 -0800334 struct RecordWithSeq {
335 uint32_t seq;
336 Record *record;
337
338 bool IsHappensBefore(const RecordWithSeq& other) const;
Yabin Cuif469c3d2015-10-07 15:00:46 -0700339 };
340
Yabin Cui4913c122016-01-11 17:15:55 -0800341 struct RecordComparator {
342 bool operator()(const RecordWithSeq& r1, const RecordWithSeq& r2);
343 };
344
345 RecordWithSeq CreateRecordWithSeq(Record *r);
346
Yabin Cuif469c3d2015-10-07 15:00:46 -0700347 bool has_timestamp_;
348 size_t min_cache_size_;
349 uint64_t min_time_diff_in_ns_;
350 uint64_t last_time_;
Yabin Cui4913c122016-01-11 17:15:55 -0800351 uint32_t cur_seq_;
352 std::priority_queue<RecordWithSeq, std::vector<RecordWithSeq>,
353 RecordComparator> queue_;
Yabin Cuif469c3d2015-10-07 15:00:46 -0700354};
355
Yabin Cui2d6efe42016-04-01 20:22:35 -0700356std::unique_ptr<Record> ReadRecordFromBuffer(const perf_event_attr& attr,
357 const perf_event_header* pheader);
Yabin Cui73d80782015-07-23 21:39:57 -0700358std::vector<std::unique_ptr<Record>> ReadRecordsFromBuffer(const perf_event_attr& attr,
359 const char* buf, size_t buf_size);
Yabin Cui7d59bb42015-05-04 20:27:57 -0700360MmapRecord CreateMmapRecord(const perf_event_attr& attr, bool in_kernel, uint32_t pid, uint32_t tid,
361 uint64_t addr, uint64_t len, uint64_t pgoff,
Yabin Cui2d6efe42016-04-01 20:22:35 -0700362 const std::string& filename, uint64_t event_id);
Yabin Cui7d59bb42015-05-04 20:27:57 -0700363CommRecord CreateCommRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid,
Yabin Cui2d6efe42016-04-01 20:22:35 -0700364 const std::string& comm, uint64_t event_id);
Yabin Cui41d4ba92015-06-22 12:27:58 -0700365ForkRecord CreateForkRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid, uint32_t ppid,
Yabin Cui2d6efe42016-04-01 20:22:35 -0700366 uint32_t ptid, uint64_t event_id);
Yabin Cui8f622512015-05-05 19:58:07 -0700367BuildIdRecord CreateBuildIdRecord(bool in_kernel, pid_t pid, const BuildId& build_id,
368 const std::string& filename);
Yabin Cui73d80782015-07-23 21:39:57 -0700369
Yabin Cui9759e1b2015-04-28 15:54:13 -0700370#endif // SIMPLE_PERF_RECORD_H_