blob: dd242484f5b5daa583e245664de8f027a499260c [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
Dan Albert918e4b72015-08-11 15:59:43 -070020#include <sys/types.h>
21
Yabin Cuif469c3d2015-10-07 15:00:46 -070022#include <queue>
Yabin Cui9759e1b2015-04-28 15:54:13 -070023#include <string>
24#include <vector>
25
Yabin Cui8f622512015-05-05 19:58:07 -070026#include "build_id.h"
Yabin Cui9759e1b2015-04-28 15:54:13 -070027#include "perf_event.h"
28
29struct KernelMmap;
30struct ModuleMmap;
31struct ThreadComm;
32struct ThreadMmap;
33
34enum user_record_type {
35 PERF_RECORD_ATTR = 64,
36 PERF_RECORD_EVENT_TYPE,
37 PERF_RECORD_TRACING_DATA,
38 PERF_RECORD_BUILD_ID,
39 PERF_RECORD_FINISHED_ROUND,
40};
41
42struct PerfSampleIpType {
43 uint64_t ip;
44};
45
46struct PerfSampleTidType {
47 uint32_t pid, tid;
48};
49
50struct PerfSampleTimeType {
51 uint64_t time;
52};
53
54struct PerfSampleAddrType {
55 uint64_t addr;
56};
57
58struct PerfSampleIdType {
59 uint64_t id;
60};
61
62struct PerfSampleStreamIdType {
63 uint64_t stream_id;
64};
65
66struct PerfSampleCpuType {
67 uint32_t cpu, res;
68};
69
70struct PerfSamplePeriodType {
71 uint64_t period;
72};
73
Yabin Cui6e8a9a42015-06-15 14:36:43 -070074struct PerfSampleCallChainType {
75 std::vector<uint64_t> ips;
76};
77
Yabin Cuibfc11b62015-08-19 10:12:51 -070078struct PerfSampleRawType {
79 std::vector<char> data;
80};
81
Yabin Cuiddddc062015-06-02 17:54:52 -070082struct PerfSampleBranchStackType {
83 struct BranchStackItemType {
84 uint64_t from;
85 uint64_t to;
86 uint64_t flags;
87 };
88 std::vector<BranchStackItemType> stack;
89};
90
Yabin Cui76769e52015-07-13 12:23:54 -070091struct PerfSampleRegsUserType {
92 uint64_t abi;
93 uint64_t reg_mask;
94 std::vector<uint64_t> regs;
95};
96
97struct PerfSampleStackUserType {
98 std::vector<char> data;
99 uint64_t dyn_size;
100};
101
Yabin Cui9759e1b2015-04-28 15:54:13 -0700102// SampleId is optional at the end of a record in binary format. Its content is determined by
103// sample_id_all and sample_type in perf_event_attr. To avoid the complexity of referring to
104// perf_event_attr each time, we copy sample_id_all and sample_type inside the SampleId structure.
105struct SampleId {
106 bool sample_id_all;
107 uint64_t sample_type;
108
109 PerfSampleTidType tid_data; // Valid if sample_id_all && PERF_SAMPLE_TID.
110 PerfSampleTimeType time_data; // Valid if sample_id_all && PERF_SAMPLE_TIME.
111 PerfSampleIdType id_data; // Valid if sample_id_all && PERF_SAMPLE_ID.
112 PerfSampleStreamIdType stream_id_data; // Valid if sample_id_all && PERF_SAMPLE_STREAM_ID.
113 PerfSampleCpuType cpu_data; // Valid if sample_id_all && PERF_SAMPLE_CPU.
114
115 SampleId();
116
117 // Create the content of sample_id. It depends on the attr we use.
118 size_t CreateContent(const perf_event_attr& attr);
119
120 // Parse sample_id from binary format in the buffer pointed by p.
121 void ReadFromBinaryFormat(const perf_event_attr& attr, const char* p, const char* end);
122
123 // Write the binary format of sample_id to the buffer pointed by p.
124 void WriteToBinaryFormat(char*& p) const;
125 void Dump(size_t indent) const;
126};
127
128// Usually one record contains the following three parts in order in binary format:
129// perf_event_header (at the head of a record, containing type and size information)
130// data depends on the record type
131// sample_id (optional part at the end of a record)
132// We hold the common parts (perf_event_header and sample_id) in the base class Record, and
133// hold the type specific data part in classes derived from Record.
134struct Record {
135 perf_event_header header;
136 SampleId sample_id;
137
138 Record();
139 Record(const perf_event_header* pheader);
140
141 virtual ~Record() {
142 }
143
144 void Dump(size_t indent = 0) const;
Yabin Cuicb84c982015-09-30 17:22:35 -0700145 virtual std::vector<char> BinaryFormat() const = 0;
Yabin Cuif469c3d2015-10-07 15:00:46 -0700146 virtual uint64_t Timestamp() const;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700147
148 protected:
Yabin Cuicb84c982015-09-30 17:22:35 -0700149 virtual void DumpData(size_t) const = 0;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700150};
151
152struct MmapRecord : public Record {
153 struct MmapRecordDataType {
154 uint32_t pid, tid;
155 uint64_t addr;
156 uint64_t len;
157 uint64_t pgoff;
158 } data;
159 std::string filename;
160
Yabin Cuicb84c982015-09-30 17:22:35 -0700161 MmapRecord() { // For CreateMmapRecord.
Yabin Cui9759e1b2015-04-28 15:54:13 -0700162 }
163
164 MmapRecord(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cuicb84c982015-09-30 17:22:35 -0700165 std::vector<char> BinaryFormat() const override;
Yabin Cui8f622512015-05-05 19:58:07 -0700166
167 protected:
168 void DumpData(size_t indent) const override;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700169};
170
Yabin Cui41d4ba92015-06-22 12:27:58 -0700171struct Mmap2Record : public Record {
172 struct Mmap2RecordDataType {
173 uint32_t pid, tid;
174 uint64_t addr;
175 uint64_t len;
176 uint64_t pgoff;
177 uint32_t maj;
178 uint32_t min;
179 uint64_t ino;
180 uint64_t ino_generation;
181 uint32_t prot, flags;
182 } data;
183 std::string filename;
184
185 Mmap2Record() {
186 }
187
188 Mmap2Record(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cuicb84c982015-09-30 17:22:35 -0700189 std::vector<char> BinaryFormat() const override;
Yabin Cui41d4ba92015-06-22 12:27:58 -0700190
191 protected:
192 void DumpData(size_t indent) const override;
193};
194
Yabin Cui9759e1b2015-04-28 15:54:13 -0700195struct CommRecord : public Record {
196 struct CommRecordDataType {
197 uint32_t pid, tid;
198 } data;
199 std::string comm;
200
201 CommRecord() {
202 }
203
204 CommRecord(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cuicb84c982015-09-30 17:22:35 -0700205 std::vector<char> BinaryFormat() const override;
Yabin Cui8f622512015-05-05 19:58:07 -0700206
207 protected:
208 void DumpData(size_t indent) const override;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700209};
210
Yabin Cui41d4ba92015-06-22 12:27:58 -0700211struct ExitOrForkRecord : public Record {
212 struct ExitOrForkRecordDataType {
Yabin Cui9759e1b2015-04-28 15:54:13 -0700213 uint32_t pid, ppid;
214 uint32_t tid, ptid;
215 uint64_t time;
216 } data;
217
Yabin Cui41d4ba92015-06-22 12:27:58 -0700218 ExitOrForkRecord() {
219 }
220 ExitOrForkRecord(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cuicb84c982015-09-30 17:22:35 -0700221 std::vector<char> BinaryFormat() const override;
Yabin Cui8f622512015-05-05 19:58:07 -0700222
223 protected:
Yabin Cui9759e1b2015-04-28 15:54:13 -0700224 void DumpData(size_t indent) const override;
225};
226
Yabin Cui41d4ba92015-06-22 12:27:58 -0700227struct ExitRecord : public ExitOrForkRecord {
228 ExitRecord(const perf_event_attr& attr, const perf_event_header* pheader)
229 : ExitOrForkRecord(attr, pheader) {
230 }
231};
232
233struct ForkRecord : public ExitOrForkRecord {
234 ForkRecord() {
235 }
236 ForkRecord(const perf_event_attr& attr, const perf_event_header* pheader)
237 : ExitOrForkRecord(attr, pheader) {
238 }
Yabin Cui41d4ba92015-06-22 12:27:58 -0700239};
240
Yabin Cui9759e1b2015-04-28 15:54:13 -0700241struct SampleRecord : public Record {
242 uint64_t sample_type; // sample_type is a bit mask determining which fields below are valid.
243
244 PerfSampleIpType ip_data; // Valid if PERF_SAMPLE_IP.
245 PerfSampleTidType tid_data; // Valid if PERF_SAMPLE_TID.
246 PerfSampleTimeType time_data; // Valid if PERF_SAMPLE_TIME.
247 PerfSampleAddrType addr_data; // Valid if PERF_SAMPLE_ADDR.
248 PerfSampleIdType id_data; // Valid if PERF_SAMPLE_ID.
249 PerfSampleStreamIdType stream_id_data; // Valid if PERF_SAMPLE_STREAM_ID.
250 PerfSampleCpuType cpu_data; // Valid if PERF_SAMPLE_CPU.
251 PerfSamplePeriodType period_data; // Valid if PERF_SAMPLE_PERIOD.
252
Yabin Cui6e8a9a42015-06-15 14:36:43 -0700253 PerfSampleCallChainType callchain_data; // Valid if PERF_SAMPLE_CALLCHAIN.
Yabin Cuibfc11b62015-08-19 10:12:51 -0700254 PerfSampleRawType raw_data; // Valid if PERF_SAMPLE_RAW.
Yabin Cuiddddc062015-06-02 17:54:52 -0700255 PerfSampleBranchStackType branch_stack_data; // Valid if PERF_SAMPLE_BRANCH_STACK.
Yabin Cui76769e52015-07-13 12:23:54 -0700256 PerfSampleRegsUserType regs_user_data; // Valid if PERF_SAMPLE_REGS_USER.
257 PerfSampleStackUserType stack_user_data; // Valid if PERF_SAMPLE_STACK_USER.
Yabin Cuiddddc062015-06-02 17:54:52 -0700258
Yabin Cui9759e1b2015-04-28 15:54:13 -0700259 SampleRecord(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cuicb84c982015-09-30 17:22:35 -0700260 std::vector<char> BinaryFormat() const override;
261 void AdjustSizeBasedOnData();
Yabin Cuif469c3d2015-10-07 15:00:46 -0700262 uint64_t Timestamp() const override;
Yabin Cui8f622512015-05-05 19:58:07 -0700263
264 protected:
265 void DumpData(size_t indent) const override;
266};
267
268// BuildIdRecord is defined in user-space, stored in BuildId feature section in record file.
269struct BuildIdRecord : public Record {
270 uint32_t pid;
271 BuildId build_id;
272 std::string filename;
273
274 BuildIdRecord() {
275 }
276
277 BuildIdRecord(const perf_event_header* pheader);
Yabin Cuicb84c982015-09-30 17:22:35 -0700278 std::vector<char> BinaryFormat() const override;
279
280 protected:
281 void DumpData(size_t indent) const override;
282};
283
284// UnknownRecord is used for unknown record types, it makes sure all unknown records
285// are not changed when modifying perf.data.
286struct UnknownRecord : public Record {
287 std::vector<char> data;
288
289 UnknownRecord(const perf_event_header* pheader);
290 std::vector<char> BinaryFormat() const override;
Yabin Cui8f622512015-05-05 19:58:07 -0700291
292 protected:
Yabin Cui9759e1b2015-04-28 15:54:13 -0700293 void DumpData(size_t indent) const override;
294};
295
Yabin Cuif469c3d2015-10-07 15:00:46 -0700296// RecordCache is a cache used when receiving records from the kernel.
297// It sorts received records based on type and timestamp, and pops records
298// in sorted order. Records from the kernel need to be sorted because
299// records may come from different cpus at the same time, and it is affected
300// by the order in which we collect records from different cpus.
301// RecordCache pushes records and pops sorted record online. It uses two checks to help
302// ensure that records are popped in order. Each time we pop a record A, it is the earliest record
303// among all records in the cache. In addition, we have checks for min_cache_size and
304// min_time_diff. For min_cache_size check, we check if the cache size >= min_cache_size,
305// which is based on the assumption that if we have received (min_cache_size - 1) records
306// after record A, we are not likely to receive a record earlier than A. For min_time_diff
307// check, we check if record A is generated min_time_diff ns earlier than the latest
308// record, which is based on the assumption that if we have received a record for time t,
309// we are not likely to receive a record for time (t - min_time_diff) or earlier.
310class RecordCache {
311 public:
312 RecordCache(const perf_event_attr& attr, size_t min_cache_size, uint64_t min_time_diff_in_ns);
313 ~RecordCache();
314 void Push(const char* data, size_t size);
315 std::unique_ptr<Record> Pop();
316 std::vector<std::unique_ptr<Record>> PopAll();
317
318 private:
319 struct RecordComparator {
320 bool operator()(const Record* r1, const Record* r2);
321 };
322
323 const perf_event_attr attr_;
324 bool has_timestamp_;
325 size_t min_cache_size_;
326 uint64_t min_time_diff_in_ns_;
327 uint64_t last_time_;
328 std::priority_queue<Record*, std::vector<Record*>, RecordComparator> queue_;
329};
330
Yabin Cui73d80782015-07-23 21:39:57 -0700331std::vector<std::unique_ptr<Record>> ReadRecordsFromBuffer(const perf_event_attr& attr,
332 const char* buf, size_t buf_size);
Yabin Cui7d59bb42015-05-04 20:27:57 -0700333MmapRecord CreateMmapRecord(const perf_event_attr& attr, bool in_kernel, uint32_t pid, uint32_t tid,
334 uint64_t addr, uint64_t len, uint64_t pgoff,
335 const std::string& filename);
336CommRecord CreateCommRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid,
337 const std::string& comm);
Yabin Cui41d4ba92015-06-22 12:27:58 -0700338ForkRecord CreateForkRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid, uint32_t ppid,
339 uint32_t ptid);
Yabin Cui8f622512015-05-05 19:58:07 -0700340BuildIdRecord CreateBuildIdRecord(bool in_kernel, pid_t pid, const BuildId& build_id,
341 const std::string& filename);
Yabin Cui73d80782015-07-23 21:39:57 -0700342
Yabin Cui9759e1b2015-04-28 15:54:13 -0700343#endif // SIMPLE_PERF_RECORD_H_