blob: 25b91ecf40db024ed8a2e6b36c6068ee3cca55d7 [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 Cui9759e1b2015-04-28 15:54:13 -070084// SampleId is optional at the end of a record in binary format. Its content is determined by
85// sample_id_all and sample_type in perf_event_attr. To avoid the complexity of referring to
86// perf_event_attr each time, we copy sample_id_all and sample_type inside the SampleId structure.
87struct SampleId {
88 bool sample_id_all;
89 uint64_t sample_type;
90
91 PerfSampleTidType tid_data; // Valid if sample_id_all && PERF_SAMPLE_TID.
92 PerfSampleTimeType time_data; // Valid if sample_id_all && PERF_SAMPLE_TIME.
93 PerfSampleIdType id_data; // Valid if sample_id_all && PERF_SAMPLE_ID.
94 PerfSampleStreamIdType stream_id_data; // Valid if sample_id_all && PERF_SAMPLE_STREAM_ID.
95 PerfSampleCpuType cpu_data; // Valid if sample_id_all && PERF_SAMPLE_CPU.
96
97 SampleId();
98
99 // Create the content of sample_id. It depends on the attr we use.
100 size_t CreateContent(const perf_event_attr& attr);
101
102 // Parse sample_id from binary format in the buffer pointed by p.
103 void ReadFromBinaryFormat(const perf_event_attr& attr, const char* p, const char* end);
104
105 // Write the binary format of sample_id to the buffer pointed by p.
106 void WriteToBinaryFormat(char*& p) const;
107 void Dump(size_t indent) const;
108};
109
110// Usually one record contains the following three parts in order in binary format:
111// perf_event_header (at the head of a record, containing type and size information)
112// data depends on the record type
113// sample_id (optional part at the end of a record)
114// We hold the common parts (perf_event_header and sample_id) in the base class Record, and
115// hold the type specific data part in classes derived from Record.
116struct Record {
117 perf_event_header header;
118 SampleId sample_id;
119
120 Record();
121 Record(const perf_event_header* pheader);
122
123 virtual ~Record() {
124 }
125
126 void Dump(size_t indent = 0) const;
127
128 protected:
129 virtual void DumpData(size_t) const {
130 }
131};
132
133struct MmapRecord : public Record {
134 struct MmapRecordDataType {
135 uint32_t pid, tid;
136 uint64_t addr;
137 uint64_t len;
138 uint64_t pgoff;
139 } data;
140 std::string filename;
141
142 MmapRecord() { // For storage in std::vector.
143 }
144
145 MmapRecord(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700146 std::vector<char> BinaryFormat() const;
Yabin Cui8f622512015-05-05 19:58:07 -0700147
148 protected:
149 void DumpData(size_t indent) const override;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700150};
151
152struct CommRecord : public Record {
153 struct CommRecordDataType {
154 uint32_t pid, tid;
155 } data;
156 std::string comm;
157
158 CommRecord() {
159 }
160
161 CommRecord(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700162 std::vector<char> BinaryFormat() const;
Yabin Cui8f622512015-05-05 19:58:07 -0700163
164 protected:
165 void DumpData(size_t indent) const override;
Yabin Cui9759e1b2015-04-28 15:54:13 -0700166};
167
168struct ExitRecord : public Record {
169 struct ExitRecordDataType {
170 uint32_t pid, ppid;
171 uint32_t tid, ptid;
172 uint64_t time;
173 } data;
174
175 ExitRecord(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cui8f622512015-05-05 19:58:07 -0700176
177 protected:
Yabin Cui9759e1b2015-04-28 15:54:13 -0700178 void DumpData(size_t indent) const override;
179};
180
181struct SampleRecord : public Record {
182 uint64_t sample_type; // sample_type is a bit mask determining which fields below are valid.
183
184 PerfSampleIpType ip_data; // Valid if PERF_SAMPLE_IP.
185 PerfSampleTidType tid_data; // Valid if PERF_SAMPLE_TID.
186 PerfSampleTimeType time_data; // Valid if PERF_SAMPLE_TIME.
187 PerfSampleAddrType addr_data; // Valid if PERF_SAMPLE_ADDR.
188 PerfSampleIdType id_data; // Valid if PERF_SAMPLE_ID.
189 PerfSampleStreamIdType stream_id_data; // Valid if PERF_SAMPLE_STREAM_ID.
190 PerfSampleCpuType cpu_data; // Valid if PERF_SAMPLE_CPU.
191 PerfSamplePeriodType period_data; // Valid if PERF_SAMPLE_PERIOD.
192
Yabin Cui6e8a9a42015-06-15 14:36:43 -0700193 PerfSampleCallChainType callchain_data; // Valid if PERF_SAMPLE_CALLCHAIN.
Yabin Cuiddddc062015-06-02 17:54:52 -0700194 PerfSampleBranchStackType branch_stack_data; // Valid if PERF_SAMPLE_BRANCH_STACK.
195
Yabin Cui9759e1b2015-04-28 15:54:13 -0700196 SampleRecord(const perf_event_attr& attr, const perf_event_header* pheader);
Yabin Cui8f622512015-05-05 19:58:07 -0700197
198 protected:
199 void DumpData(size_t indent) const override;
200};
201
202// BuildIdRecord is defined in user-space, stored in BuildId feature section in record file.
203struct BuildIdRecord : public Record {
204 uint32_t pid;
205 BuildId build_id;
206 std::string filename;
207
208 BuildIdRecord() {
209 }
210
211 BuildIdRecord(const perf_event_header* pheader);
212 std::vector<char> BinaryFormat() const;
213
214 protected:
Yabin Cui9759e1b2015-04-28 15:54:13 -0700215 void DumpData(size_t indent) const override;
216};
217
218std::unique_ptr<const Record> ReadRecordFromBuffer(const perf_event_attr& attr,
219 const perf_event_header* pheader);
Yabin Cui7d59bb42015-05-04 20:27:57 -0700220MmapRecord CreateMmapRecord(const perf_event_attr& attr, bool in_kernel, uint32_t pid, uint32_t tid,
221 uint64_t addr, uint64_t len, uint64_t pgoff,
222 const std::string& filename);
223CommRecord CreateCommRecord(const perf_event_attr& attr, uint32_t pid, uint32_t tid,
224 const std::string& comm);
Yabin Cui8f622512015-05-05 19:58:07 -0700225BuildIdRecord CreateBuildIdRecord(bool in_kernel, pid_t pid, const BuildId& build_id,
226 const std::string& filename);
Yabin Cui9759e1b2015-04-28 15:54:13 -0700227#endif // SIMPLE_PERF_RECORD_H_