• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef HIPERF_PERF_RECORD_FORMAT_H
16 #define HIPERF_PERF_RECORD_FORMAT_H
17 
18 #include <string>
19 #include <linux/types.h>
20 
21 #include "utilities.h"
22 
23 namespace OHOS {
24 namespace Developtools {
25 namespace HiPerf {
26 // description from https://man7.org/linux/man-pages/man2/perf_event_open.2.html
27 
28 #define SAMPLE_ID_ALL 0
29 
30 struct sample_id {
31     u32 pid;
32     u32 tid;       /* if PERF_SAMPLE_TID set */
33     u64 time;      /* if PERF_SAMPLE_TIME set */
34     u64 id;        /* if PERF_SAMPLE_ID set */
35     u64 stream_id; /* if PERF_SAMPLE_STREAM_ID set  */
36     u32 cpu, res;  /* if PERF_SAMPLE_CPU set */
37     u64 id2;       /* if PERF_SAMPLE_IDENTIFIER set */
38 };
39 
40 // If PERF_FORMAT_GROUP was not specified
41 struct read_format {
42     __u64 value;        /* The value of the event */
43     __u64 time_enabled; /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
44     __u64 time_running; /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
45     __u64 id;           /* if PERF_FORMAT_ID */
46 };
47 
48 /*
49     The MMAP events record the PROT_EXEC mappings so that
50     we can correlate user-space IPs to code.  They have
51     the following structure:
52         pid     is the process ID.
53         tid     is the thread ID.
54         addr    is the address of the allocated memory.
55         len     is the length of the allocated memory.
56         pgoff   is the page offset of the allocated memory.
57         filename
58             is a string describing the backing of
59             the allocated memory.
60 */
61 struct PerfRecordMmapData {
62     u32 pid, tid;
63     u64 addr;
64     u64 len;
65     u64 pgoff;
66     char filename[KILO];
67 #if SAMPLE_ID_ALL
68     struct sample_id sample_id;
69 #endif
70 };
71 
72 /*
73     This record includes extended information on mmap(2)
74     calls returning executable mappings.  The format is
75     similar to that of the PERF_RECORD_MMAP record, but
76     includes extra values that allow uniquely identifying
77     shared mappings.
78 
79     pid    is the process ID.
80     tid    is the thread ID.
81     addr   is the address of the allocated memory.
82     len    is the length of the allocated memory.
83     pgoff  is the page offset of the allocated memory.
84     maj    is the major ID of the underlying device.
85     min    is the minor ID of the underlying device.
86     ino    is the inode number.
87     ino_generation
88             is the inode generation.
89     prot   is the protection information.
90     flags  is the flags information.
91     filename
92             is a string describing the backing of the
93             allocated memory.
94 */
95 struct PerfRecordMmap2Data {
96     u32 pid;
97     u32 tid;
98     u64 addr;
99     u64 len;
100     u64 pgoff;
101     u32 maj;
102     u32 min;
103     u64 ino;
104     u64 ino_generation;
105     u32 prot;
106     u32 flags;
107     char filename[KILO];
108 #if SAMPLE_ID_ALL
109     struct sample_id sample_id;
110 #endif
111 };
112 
113 /*
114     This record indicates when events are lost.
115     id     is the unique event ID for the samples that  were lost.
116     lost   is the number of events that were lost.
117 */
118 struct PerfRecordLostData {
119     u64 id;
120     u64 lost;
121 #if SAMPLE_ID_ALL
122     struct sample_id sample_id;
123 #endif
124 };
125 
126 /*
127     This record indicates a change in the process name.
128     pid    is the process ID.
129     tid    is the thread ID.
130     comm   is a string containing the new name of the process.
131 */
132 struct PerfRecordCommData {
133     u32 pid;
134     u32 tid;
135     char comm[KILO];
136 #if SAMPLE_ID_ALL
137     struct sample_id sample_id;
138 #endif
139 };
140 
141 // This record indicates a sample.
142 struct PerfRecordSampleData {
143     u64 sample_id; /* if PERF_SAMPLE_IDENTIFIER */
144     u64 ip;        /* if PERF_SAMPLE_IP */
145     u32 pid, tid;  /* if PERF_SAMPLE_TID */
146     u64 time;      /* if PERF_SAMPLE_TIME */
147     u64 addr;      /* if PERF_SAMPLE_ADDR */
148     u64 id;        /* if PERF_SAMPLE_ID */
149     u64 stream_id; /* if PERF_SAMPLE_STREAM_ID */
150     u32 cpu, res;  /* if PERF_SAMPLE_CPU */
151     u64 period;    /* if PERF_SAMPLE_PERIOD */
152     struct read_format v;
153     /* if PERF_SAMPLE_READ */
154     u64 nr;                        /* if PERF_SAMPLE_CALLCHAIN */
155     u64 *ips;                      /* if PERF_SAMPLE_CALLCHAIN */
156     u32 raw_size;                  /* if PERF_SAMPLE_RAW */
157     u8 *raw_data;                  /* if PERF_SAMPLE_RAW */
158     u64 bnr;                       /* if PERF_SAMPLE_BRANCH_STACK */
159     struct perf_branch_entry *lbr; /* if PERF_SAMPLE_BRANCH_STACK */
160     u64 user_abi;                  /* if PERF_SAMPLE_REGS_USER */
161     u64 reg_mask;
162     u64 reg_nr;
163     u64 *user_regs;   /* if PERF_SAMPLE_REGS_USER */
164     u64 stack_size;   /* if PERF_SAMPLE_STACK_USER */
165     u8 *stack_data;   /* if PERF_SAMPLE_STACK_USER */
166     u64 dyn_size;     /* if PERF_SAMPLE_STACK_USER && stack_size != 0 */
167     u64 weight;       /* if PERF_SAMPLE_WEIGHT */
168     u64 data_src;     /* if PERF_SAMPLE_DATA_SRC */
169     u64 transaction;  /* if PERF_SAMPLE_TRANSACTION */
170     u64 intr_abi;     /* if PERF_SAMPLE_REGS_INTR */
171     u64 intr_regs[0]; /* if PERF_SAMPLE_REGS_INTR */
172     u64 phys_addr;    /* if PERF_SAMPLE_PHYS_ADDR */
173     u64 cgroup;       /* if PERF_SAMPLE_CGROUP */
174 };
175 
176 /*
177     This record indicates a process exit event.
178 */
179 struct PerfRecordExitData {
180     u32 pid, ppid;
181     u32 tid, ptid;
182     u64 time;
183 #if SAMPLE_ID_ALL
184     struct sample_id sample_id;
185 #endif
186 };
187 
188 /*
189     This record indicates a throttle/unthrottle event.
190 */
191 struct PerfRecordThrottleData {
192     u64 time;
193     u64 id;
194     u64 stream_id;
195 #if SAMPLE_ID_ALL
196     struct sample_id sample_id;
197 #endif
198 };
199 
200 /*
201     This record indicates a fork event.
202 */
203 struct PerfRecordForkData {
204     u32 pid, ppid;
205     u32 tid, ptid;
206     u64 time;
207 #if SAMPLE_ID_ALL
208     struct sample_id sample_id;
209 #endif
210 };
211 
212 /*
213     When using hardware sampling (such as Intel PEBS) this
214     record indicates some number of samples that may have
215     been lost.
216 */
217 struct PerfRecordLostSamplesData {
218     u64 lost;
219 #if SAMPLE_ID_ALL
220     struct sample_id sample_id;
221 #endif
222 };
223 
224 /*
225     This record indicates which process has initiated an
226     instruction trace event, allowing tools to properly
227     correlate the instruction addresses in the AUX buffer
228     with the proper executable.
229 
230     pid    process ID of the thread starting an
231             instruction trace.
232     tid    thread ID of the thread starting an instruction
233             trace.
234 */
235 struct PerfRecordItraceStartData {
236     u32 pid;
237     u32 tid;
238 };
239 
240 /*
241     This record reports that new data is available in the
242     separate AUX buffer region.
243 
244     aux_offset
245             offset in the AUX mmap region where the new
246             data begins.
247     aux_size
248             size of the data made available.
249     flags  describes the AUX update.
250             PERF_AUX_FLAG_TRUNCATED
251                 if set, then the data returned was
252                 truncated to fit the available buffer
253                 size.
254 
255             PERF_AUX_FLAG_OVERWRITE
256                 if set, then the data returned has
257                 overwritten previous data.
258 */
259 struct PerfRecordAuxData {
260     u64 aux_offset;
261     u64 aux_size;
262     u64 flags;
263 #if SAMPLE_ID_ALL
264     struct sample_id sample_id;
265 #endif
266 };
267 
268 /*
269     This record indicates a read event.
270 */
271 struct PerfRecordReadData {
272     u32 pid, tid;
273     read_format values;
274 #if SAMPLE_ID_ALL
275     struct sample_id sample_id;
276 #endif
277 };
278 
279 /*
280     This record indicates a context switch has happened.
281     The PERF_RECORD_MISC_SWITCH_OUT bit in the misc field
282     indicates whether it was a context switch into or away
283     from the current process.
284 */
285 struct PerfRecordSwitchData {
286 #if SAMPLE_ID_ALL
287     struct sample_id sample_id;
288 #endif
289 };
290 
291 /*
292     As with PERF_RECORD_SWITCH this record indicates a
293     context switch has happened, but it only occurs when
294     sampling in CPU-wide mode and provides additional
295     information on the process being switched to/from.
296     The PERF_RECORD_MISC_SWITCH_OUT bit in the misc field
297     indicates whether it was a context switch into or away
298     from the current process.
299 
300     next_prev_pid
301             The process ID of the previous (if switching
302             in) or next (if switching out) process on the
303             CPU.
304 
305     next_prev_tid
306             The thread ID of the previous (if switching in)
307             or next (if switching out) thread on the CPU.
308 */
309 struct PerfRecordSwitchCpuWideData {
310     u32 next_prev_pid;
311     u32 next_prev_tid;
312 #if SAMPLE_ID_ALL
313     struct sample_id sample_id;
314 #endif
315 };
316 
317 /*
318     This record includes various namespace information of
319     a process.
320 
321     pid    is the process ID
322     tid    is the thread ID
323 
324     nr_namespace
325             is the number of namespaces in this record
326 
327     Each namespace has dev and inode fields and is
328     recorded in the fixed position like below:
329 
330     NET_NS_INDEX=0
331             Network namespace
332     UTS_NS_INDEX=1
333             UTS namespace
334     IPC_NS_INDEX=2
335             IPC namespace
336     PID_NS_INDEX=3
337             PID namespace
338     USER_NS_INDEX=4
339             User namespace
340     MNT_NS_INDEX=5
341             Mount namespace
342     CGROUP_NS_INDEX=6
343             Cgroup namespace
344 */
345 struct PerfRecordNamespacesData {
346     u32 pid;
347     u32 tid;
348     u64 nr_namespaces;
349     struct name_space {
350         u64 dev;
351         u64 inode;
352     } namespaces[0];
353 #if SAMPLE_ID_ALL
354     struct sample_id sample_id;
355 #endif
356 };
357 } // namespace HiPerf
358 } // namespace Developtools
359 } // namespace OHOS
360 #endif