blob: b982675c82abfbffcd6083446925db0b796d7be1 [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
20#include <string>
21#include <vector>
22
Yabin Cui8f622512015-05-05 19:58:07 -070023#include "build_id.h"
Yabin Cui9759e1b2015-04-28 15:54:13 -070024#include "perf_event.h"
25
26struct KernelMmap;
27struct ModuleMmap;
28struct ThreadComm;
29struct ThreadMmap;
30
31enum user_record_type {
32 PERF_RECORD_ATTR = 64,
33 PERF_RECORD_EVENT_TYPE,
34 PERF_RECORD_TRACING_DATA,
35 PERF_RECORD_BUILD_ID,
36 PERF_RECORD_FINISHED_ROUND,
37};
38
39struct PerfSampleIpType {
40 uint64_t ip;
41};
42
43struct PerfSampleTidType {
44 uint32_t pid, tid;
45};
46
47struct PerfSampleTimeType {
48 uint64_t time;
49};
50
51struct PerfSampleAddrType {
52 uint64_t addr;
53};
54
55struct PerfSampleIdType {
56 uint64_t id;
57};
58
59struct PerfSampleStreamIdType {
60 uint64_t stream_id;
61};
62
63struct PerfSampleCpuType {
64 uint32_t cpu, res;
65};
66
67struct PerfSamplePeriodType {
68 uint64_t period;
69};
70
Yabin Cui6e8a9a42015-06-15 14:36:43 -070071struct PerfSampleCallChainType {
72 std::vector<uint64_t> ips;
73};
74
Yabin Cuiddddc062015-06-02 17:54:52 -070075struct PerfSampleBranchStackType {
76 struct BranchStackItemType {
77 uint64_t from;
78 uint64_t to;
79 uint64_t flags;
80 };
81 std::vector<BranchStackItemType> stack;
82};
83
Yabin Cui76769e52015-07-13 12:23:54 -070084struct PerfSampleRegsUserType {
85 uint64_t abi;
86 uint64_t reg_mask;
87 std::vector<uint64_t> regs;
88};
89
90struct PerfSampleStackUserType {
91 std::vector<char> data;
92 uint64_t dyn_size;
93};
94
Yabin Cui9759e1b2015-04-28 15:54:13 -070095// SampleId is optional at the end of a record in binary format. Its content is determined by
96// sample_id_all and sample_type in perf_event_attr. To avoid the complexity of referring to
97// perf_event_attr each time, we copy sample_id_all and sample_type inside the SampleId structure.
98struct SampleId {
99 bool sample_id_all;
100 uint64_t sample_type;
101
102 PerfSampleTidType tid_data; // Valid if sample_id_all && PERF_SAMPLE_TID.
103 PerfSampleTimeType time_data; // Valid if sample_id_all && PERF_SAMPLE_TIME.
104 PerfSampleIdType id_data; // Valid if sample_id_all && PERF_SAMPLE_ID.
105 PerfSampleStreamIdType stream_id_data; // Valid if sample_id_all && PERF_SAMPLE_STREAM_ID.
106 PerfSampleCpuType cpu_data; // Valid if sample_id_all && PERF_SAMPLE_CPU.
107
108 SampleId();
109
110 // Create the content of sample_id. It depends on the attr we use.
111 size_t CreateContent(const perf_event_attr& attr);
112
113 // Parse sample_id from binary format in the buffer pointed by p.
114 void ReadFromBinaryFormat(const perf_event_attr& attr, const char* p, const char* end);
115
116 // Write the binary format of sample_id to the buffer pointed by p.
117 void WriteToBinaryFormat(char*& p) const;
118 void Dump(size_t indent) const;
119};
120
121// Usually one record contains the following three parts in order in binary format:
122// perf_event_header (at the head of a record, containing type and size information)
123// data depends on the record type
124// sample_id (optional part at the end of a record)
125// We hold the common parts (perf_event_header and sample_id) in the base class Record, and
126// hold the type specific data part in classes derived from Record.
127struct Record {
128 perf_event_header header;
129 SampleId sample_id;
130
131 Record();
132 Record(const perf_event_header* pheader);
133
134 virtual ~Record() {
135 }
136
137 void Dump(size_t indent = 0) const;
138
139 protected:
140 virtual void DumpData(size_t) const {
141 }
142};
143
144struct MmapRecord : public Record {
145 struct MmapRecordDataType {
146 uint32_t pid, tid;
147 uint64_t addr;
148 uint64_t len;
149 uint64_t pgoff;
150 } data;
151 std::string filename;
152
153 MmapRecord() { // For storage in std::vector.
154 }
155
156 MmapRecord(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700157 std::vector<char> BinaryFormat() const;
Yabin Cui8f622512015-05-05 19:58:07 -0700158
159 protected:
160 void DumpData(size_t indent) const override;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700161};
162
Yabin Cui41d4ba92015-06-22 12:27:58 -0700163struct Mmap2Record : public Record {
164 struct Mmap2RecordDataType {
165 uint32_t pid, tid;
166 uint64_t addr;
167 uint64_t len;
168 uint64_t pgoff;
169 uint32_t maj;
170 uint32_t min;
171 uint64_t ino;
172 uint64_t ino_generation;
173 uint32_t prot, flags;
174 } data;
175 std::string filename;
176
177 Mmap2Record() {
178 }
179
180 Mmap2Record(const perf_event_attr& attr, const perf_event_header* pheader);
181
182 protected:
183 void DumpData(size_t indent) const override;
184};
185
Yabin Cui9759e1b2015-04-28 15:54:13 -0700186struct CommRecord : public Record {
187 struct CommRecordDataType {
188 uint32_t pid, tid;
189 } data;
190 std::string comm;
191
192 CommRecord() {
193 }
194
195 CommRecord(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700196 std::vector<char> BinaryFormat() const;
Yabin Cui8f622512015-05-05 19:58:07 -0700197
198 protected:
199 void DumpData(size_t indent) const override;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700200};
201
Yabin Cui41d4ba92015-06-22 12:27:58 -0700202struct ExitOrForkRecord : public Record {
203 struct ExitOrForkRecordDataType {
Yabin Cui9759e1b2015-04-28 15:54:13 -0700204 uint32_t pid, ppid;
205 uint32_t tid, ptid;
206 uint64_t time;
207 } data;
208
Yabin Cui41d4ba92015-06-22 12:27:58 -0700209 ExitOrForkRecord() {
210 }
211 ExitOrForkRecord(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cui8f622512015-05-05 19:58:07 -0700212
213 protected:
Yabin Cui9759e1b2015-04-28 15:54:13 -0700214 void DumpData(size_t indent) const override;
215};
216
Yabin Cui41d4ba92015-06-22 12:27:58 -0700217struct ExitRecord : public ExitOrForkRecord {
218 ExitRecord(const perf_event_attr& attr, const perf_event_header* pheader)
219 : ExitOrForkRecord(attr, pheader) {
220 }
221};
222
223struct ForkRecord : public ExitOrForkRecord {
224 ForkRecord() {
225 }
226 ForkRecord(const perf_event_attr& attr, const perf_event_header* pheader)
227 : ExitOrForkRecord(attr, pheader) {
228 }
229 std::vector<char> BinaryFormat() const;
230};
231
Yabin Cui9759e1b2015-04-28 15:54:13 -0700232struct SampleRecord : public Record {
233 uint64_t sample_type; // sample_type is a bit mask determining which fields below are valid.
234
235 PerfSampleIpType ip_data; // Valid if PERF_SAMPLE_IP.
236 PerfSampleTidType tid_data; // Valid if PERF_SAMPLE_TID.
237 PerfSampleTimeType time_data; // Valid if PERF_SAMPLE_TIME.
238 PerfSampleAddrType addr_data; // Valid if PERF_SAMPLE_ADDR.
239 PerfSampleIdType id_data; // Valid if PERF_SAMPLE_ID.
240 PerfSampleStreamIdType stream_id_data; // Valid if PERF_SAMPLE_STREAM_ID.
241 PerfSampleCpuType cpu_data; // Valid if PERF_SAMPLE_CPU.
242 PerfSamplePeriodType period_data; // Valid if PERF_SAMPLE_PERIOD.
243
Yabin Cui6e8a9a42015-06-15 14:36:43 -0700244 PerfSampleCallChainType callchain_data; // Valid if PERF_SAMPLE_CALLCHAIN.
Yabin Cuiddddc062015-06-02 17:54:52 -0700245 PerfSampleBranchStackType branch_stack_data; // Valid if PERF_SAMPLE_BRANCH_STACK.
Yabin Cui76769e52015-07-13 12:23:54 -0700246 PerfSampleRegsUserType regs_user_data; // Valid if PERF_SAMPLE_REGS_USER.
247 PerfSampleStackUserType stack_user_data; // Valid if PERF_SAMPLE_STACK_USER.
Yabin Cuiddddc062015-06-02 17:54:52 -0700248
Yabin Cui9759e1b2015-04-28 15:54:13 -0700249 SampleRecord(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cui8f622512015-05-05 19:58:07 -0700250
251 protected:
252 void DumpData(size_t indent) const override;
253};
254
255// BuildIdRecord is defined in user-space, stored in BuildId feature section in record file.
256struct BuildIdRecord : public Record {
257 uint32_t pid;
258 BuildId build_id;
259 std::string filename;
260
261 BuildIdRecord() {
262 }
263
264 BuildIdRecord(const perf_event_header* pheader);
265 std::vector<char> BinaryFormat() const;
266
267 protected:
Yabin Cui9759e1b2015-04-28 15:54:13 -0700268 void DumpData(size_t indent) const override;
269};
270
271std::unique_ptr<const Record> ReadRecordFromBuffer(const perf_event_attr& attr,
272 const perf_event_header* pheader);
Yabin Cui7d59bb42015-05-04 20:27:57 -0700273MmapRecord CreateMmapRecord(const perf_event_attr& attr, bool in_kernel, uint32_t pid, uint32_t tid,
274 uint64_t addr, uint64_t len, uint64_t pgoff,
275 const std::string& filename);
276CommRecord CreateCommRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid,
277 const std::string& comm);
Yabin Cui41d4ba92015-06-22 12:27:58 -0700278ForkRecord CreateForkRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid, uint32_t ppid,
279 uint32_t ptid);
Yabin Cui8f622512015-05-05 19:58:07 -0700280BuildIdRecord CreateBuildIdRecord(bool in_kernel, pid_t pid, const BuildId& build_id,
281 const std::string& filename);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700282#endif // SIMPLE_PERF_RECORD_H_