Loading...
1#ifndef __PERF_RECORD_H
2#define __PERF_RECORD_H
3
4#include <limits.h>
5#include <stdio.h>
6
7#include "../perf.h"
8#include "map.h"
9
10/*
11 * PERF_SAMPLE_IP | PERF_SAMPLE_TID | *
12 */
13struct ip_event {
14 struct perf_event_header header;
15 u64 ip;
16 u32 pid, tid;
17 unsigned char __more_data[];
18};
19
20struct mmap_event {
21 struct perf_event_header header;
22 u32 pid, tid;
23 u64 start;
24 u64 len;
25 u64 pgoff;
26 char filename[PATH_MAX];
27};
28
29struct comm_event {
30 struct perf_event_header header;
31 u32 pid, tid;
32 char comm[16];
33};
34
35struct fork_event {
36 struct perf_event_header header;
37 u32 pid, ppid;
38 u32 tid, ptid;
39 u64 time;
40};
41
42struct lost_event {
43 struct perf_event_header header;
44 u64 id;
45 u64 lost;
46};
47
48/*
49 * PERF_FORMAT_ENABLED | PERF_FORMAT_RUNNING | PERF_FORMAT_ID
50 */
51struct read_event {
52 struct perf_event_header header;
53 u32 pid, tid;
54 u64 value;
55 u64 time_enabled;
56 u64 time_running;
57 u64 id;
58};
59
60
61#define PERF_SAMPLE_MASK \
62 (PERF_SAMPLE_IP | PERF_SAMPLE_TID | \
63 PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR | \
64 PERF_SAMPLE_ID | PERF_SAMPLE_STREAM_ID | \
65 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD)
66
67struct sample_event {
68 struct perf_event_header header;
69 u64 array[];
70};
71
72struct perf_sample {
73 u64 ip;
74 u32 pid, tid;
75 u64 time;
76 u64 addr;
77 u64 id;
78 u64 stream_id;
79 u64 period;
80 u32 cpu;
81 u32 raw_size;
82 void *raw_data;
83 struct ip_callchain *callchain;
84 struct branch_stack *branch_stack;
85};
86
87#define BUILD_ID_SIZE 20
88
89struct build_id_event {
90 struct perf_event_header header;
91 pid_t pid;
92 u8 build_id[ALIGN(BUILD_ID_SIZE, sizeof(u64))];
93 char filename[];
94};
95
96enum perf_user_event_type { /* above any possible kernel type */
97 PERF_RECORD_USER_TYPE_START = 64,
98 PERF_RECORD_HEADER_ATTR = 64,
99 PERF_RECORD_HEADER_EVENT_TYPE = 65,
100 PERF_RECORD_HEADER_TRACING_DATA = 66,
101 PERF_RECORD_HEADER_BUILD_ID = 67,
102 PERF_RECORD_FINISHED_ROUND = 68,
103 PERF_RECORD_HEADER_MAX
104};
105
106struct attr_event {
107 struct perf_event_header header;
108 struct perf_event_attr attr;
109 u64 id[];
110};
111
112#define MAX_EVENT_NAME 64
113
114struct perf_trace_event_type {
115 u64 event_id;
116 char name[MAX_EVENT_NAME];
117};
118
119struct event_type_event {
120 struct perf_event_header header;
121 struct perf_trace_event_type event_type;
122};
123
124struct tracing_data_event {
125 struct perf_event_header header;
126 u32 size;
127};
128
129union perf_event {
130 struct perf_event_header header;
131 struct ip_event ip;
132 struct mmap_event mmap;
133 struct comm_event comm;
134 struct fork_event fork;
135 struct lost_event lost;
136 struct read_event read;
137 struct sample_event sample;
138 struct attr_event attr;
139 struct event_type_event event_type;
140 struct tracing_data_event tracing_data;
141 struct build_id_event build_id;
142};
143
144void perf_event__print_totals(void);
145
146struct perf_tool;
147struct thread_map;
148
149typedef int (*perf_event__handler_t)(struct perf_tool *tool,
150 union perf_event *event,
151 struct perf_sample *sample,
152 struct machine *machine);
153
154int perf_event__synthesize_thread_map(struct perf_tool *tool,
155 struct thread_map *threads,
156 perf_event__handler_t process,
157 struct machine *machine);
158int perf_event__synthesize_threads(struct perf_tool *tool,
159 perf_event__handler_t process,
160 struct machine *machine);
161int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
162 perf_event__handler_t process,
163 struct machine *machine,
164 const char *symbol_name);
165
166int perf_event__synthesize_modules(struct perf_tool *tool,
167 perf_event__handler_t process,
168 struct machine *machine);
169
170int perf_event__process_comm(struct perf_tool *tool,
171 union perf_event *event,
172 struct perf_sample *sample,
173 struct machine *machine);
174int perf_event__process_lost(struct perf_tool *tool,
175 union perf_event *event,
176 struct perf_sample *sample,
177 struct machine *machine);
178int perf_event__process_mmap(struct perf_tool *tool,
179 union perf_event *event,
180 struct perf_sample *sample,
181 struct machine *machine);
182int perf_event__process_task(struct perf_tool *tool,
183 union perf_event *event,
184 struct perf_sample *sample,
185 struct machine *machine);
186int perf_event__process(struct perf_tool *tool,
187 union perf_event *event,
188 struct perf_sample *sample,
189 struct machine *machine);
190
191struct addr_location;
192int perf_event__preprocess_sample(const union perf_event *self,
193 struct machine *machine,
194 struct addr_location *al,
195 struct perf_sample *sample,
196 symbol_filter_t filter);
197
198const char *perf_event__name(unsigned int id);
199
200int perf_event__parse_sample(const union perf_event *event, u64 type,
201 int sample_size, bool sample_id_all,
202 struct perf_sample *sample, bool swapped);
203int perf_event__synthesize_sample(union perf_event *event, u64 type,
204 const struct perf_sample *sample,
205 bool swapped);
206
207size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp);
208size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp);
209size_t perf_event__fprintf_task(union perf_event *event, FILE *fp);
210size_t perf_event__fprintf(union perf_event *event, FILE *fp);
211
212#endif /* __PERF_RECORD_H */
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __PERF_RECORD_H
3#define __PERF_RECORD_H
4/*
5 * The linux/stddef.h isn't need here, but is needed for __always_inline used
6 * in files included from uapi/linux/perf_event.h such as
7 * /usr/include/linux/swab.h and /usr/include/linux/byteorder/little_endian.h,
8 * detected in at least musl libc, used in Alpine Linux. -acme
9 */
10#include <stdio.h>
11#include <linux/stddef.h>
12#include <perf/event.h>
13#include <linux/types.h>
14
15#include "perf_regs.h"
16
17struct dso;
18struct machine;
19struct perf_event_attr;
20
21#ifdef __LP64__
22/*
23 * /usr/include/inttypes.h uses just 'lu' for PRIu64, but we end up defining
24 * __u64 as long long unsigned int, and then -Werror=format= kicks in and
25 * complains of the mismatched types, so use these two special extra PRI
26 * macros to overcome that.
27 */
28#define PRI_lu64 "l" PRIu64
29#define PRI_lx64 "l" PRIx64
30#define PRI_ld64 "l" PRId64
31#else
32#define PRI_lu64 PRIu64
33#define PRI_lx64 PRIx64
34#define PRI_ld64 PRId64
35#endif
36
37#define PERF_SAMPLE_MASK \
38 (PERF_SAMPLE_IP | PERF_SAMPLE_TID | \
39 PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR | \
40 PERF_SAMPLE_ID | PERF_SAMPLE_STREAM_ID | \
41 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD | \
42 PERF_SAMPLE_IDENTIFIER)
43
44/* perf sample has 16 bits size limit */
45#define PERF_SAMPLE_MAX_SIZE (1 << 16)
46
47struct regs_dump {
48 u64 abi;
49 u64 mask;
50 u64 *regs;
51
52 /* Cached values/mask filled by first register access. */
53 u64 cache_regs[PERF_REGS_MAX];
54 u64 cache_mask;
55};
56
57struct stack_dump {
58 u16 offset;
59 u64 size;
60 char *data;
61};
62
63struct sample_read_value {
64 u64 value;
65 u64 id;
66};
67
68struct sample_read {
69 u64 time_enabled;
70 u64 time_running;
71 union {
72 struct {
73 u64 nr;
74 struct sample_read_value *values;
75 } group;
76 struct sample_read_value one;
77 };
78};
79
80struct ip_callchain {
81 u64 nr;
82 u64 ips[];
83};
84
85struct branch_stack;
86
87enum {
88 PERF_IP_FLAG_BRANCH = 1ULL << 0,
89 PERF_IP_FLAG_CALL = 1ULL << 1,
90 PERF_IP_FLAG_RETURN = 1ULL << 2,
91 PERF_IP_FLAG_CONDITIONAL = 1ULL << 3,
92 PERF_IP_FLAG_SYSCALLRET = 1ULL << 4,
93 PERF_IP_FLAG_ASYNC = 1ULL << 5,
94 PERF_IP_FLAG_INTERRUPT = 1ULL << 6,
95 PERF_IP_FLAG_TX_ABORT = 1ULL << 7,
96 PERF_IP_FLAG_TRACE_BEGIN = 1ULL << 8,
97 PERF_IP_FLAG_TRACE_END = 1ULL << 9,
98 PERF_IP_FLAG_IN_TX = 1ULL << 10,
99};
100
101#define PERF_IP_FLAG_CHARS "bcrosyiABEx"
102
103#define PERF_BRANCH_MASK (\
104 PERF_IP_FLAG_BRANCH |\
105 PERF_IP_FLAG_CALL |\
106 PERF_IP_FLAG_RETURN |\
107 PERF_IP_FLAG_CONDITIONAL |\
108 PERF_IP_FLAG_SYSCALLRET |\
109 PERF_IP_FLAG_ASYNC |\
110 PERF_IP_FLAG_INTERRUPT |\
111 PERF_IP_FLAG_TX_ABORT |\
112 PERF_IP_FLAG_TRACE_BEGIN |\
113 PERF_IP_FLAG_TRACE_END)
114
115#define MAX_INSN 16
116
117struct aux_sample {
118 u64 size;
119 void *data;
120};
121
122struct perf_sample {
123 u64 ip;
124 u32 pid, tid;
125 u64 time;
126 u64 addr;
127 u64 id;
128 u64 stream_id;
129 u64 period;
130 u64 weight;
131 u64 transaction;
132 u64 insn_cnt;
133 u64 cyc_cnt;
134 u32 cpu;
135 u32 raw_size;
136 u64 data_src;
137 u64 phys_addr;
138 u64 cgroup;
139 u32 flags;
140 u16 insn_len;
141 u8 cpumode;
142 u16 misc;
143 bool no_hw_idx; /* No hw_idx collected in branch_stack */
144 char insn[MAX_INSN];
145 void *raw_data;
146 struct ip_callchain *callchain;
147 struct branch_stack *branch_stack;
148 struct regs_dump user_regs;
149 struct regs_dump intr_regs;
150 struct stack_dump user_stack;
151 struct sample_read read;
152 struct aux_sample aux_sample;
153};
154
155#define PERF_MEM_DATA_SRC_NONE \
156 (PERF_MEM_S(OP, NA) |\
157 PERF_MEM_S(LVL, NA) |\
158 PERF_MEM_S(SNOOP, NA) |\
159 PERF_MEM_S(LOCK, NA) |\
160 PERF_MEM_S(TLB, NA))
161
162/* Attribute type for custom synthesized events */
163#define PERF_TYPE_SYNTH (INT_MAX + 1U)
164
165/* Attribute config for custom synthesized events */
166enum perf_synth_id {
167 PERF_SYNTH_INTEL_PTWRITE,
168 PERF_SYNTH_INTEL_MWAIT,
169 PERF_SYNTH_INTEL_PWRE,
170 PERF_SYNTH_INTEL_EXSTOP,
171 PERF_SYNTH_INTEL_PWRX,
172 PERF_SYNTH_INTEL_CBR,
173};
174
175/*
176 * Raw data formats for synthesized events. Note that 4 bytes of padding are
177 * present to match the 'size' member of PERF_SAMPLE_RAW data which is always
178 * 8-byte aligned. That means we must dereference raw_data with an offset of 4.
179 * Refer perf_sample__synth_ptr() and perf_synth__raw_data(). It also means the
180 * structure sizes are 4 bytes bigger than the raw_size, refer
181 * perf_synth__raw_size().
182 */
183
184struct perf_synth_intel_ptwrite {
185 u32 padding;
186 union {
187 struct {
188 u32 ip : 1,
189 reserved : 31;
190 };
191 u32 flags;
192 };
193 u64 payload;
194};
195
196struct perf_synth_intel_mwait {
197 u32 padding;
198 u32 reserved;
199 union {
200 struct {
201 u64 hints : 8,
202 reserved1 : 24,
203 extensions : 2,
204 reserved2 : 30;
205 };
206 u64 payload;
207 };
208};
209
210struct perf_synth_intel_pwre {
211 u32 padding;
212 u32 reserved;
213 union {
214 struct {
215 u64 reserved1 : 7,
216 hw : 1,
217 subcstate : 4,
218 cstate : 4,
219 reserved2 : 48;
220 };
221 u64 payload;
222 };
223};
224
225struct perf_synth_intel_exstop {
226 u32 padding;
227 union {
228 struct {
229 u32 ip : 1,
230 reserved : 31;
231 };
232 u32 flags;
233 };
234};
235
236struct perf_synth_intel_pwrx {
237 u32 padding;
238 u32 reserved;
239 union {
240 struct {
241 u64 deepest_cstate : 4,
242 last_cstate : 4,
243 wake_reason : 4,
244 reserved1 : 52;
245 };
246 u64 payload;
247 };
248};
249
250struct perf_synth_intel_cbr {
251 u32 padding;
252 union {
253 struct {
254 u32 cbr : 8,
255 reserved1 : 8,
256 max_nonturbo : 8,
257 reserved2 : 8;
258 };
259 u32 flags;
260 };
261 u32 freq;
262 u32 reserved3;
263};
264
265/*
266 * raw_data is always 4 bytes from an 8-byte boundary, so subtract 4 to get
267 * 8-byte alignment.
268 */
269static inline void *perf_sample__synth_ptr(struct perf_sample *sample)
270{
271 return sample->raw_data - 4;
272}
273
274static inline void *perf_synth__raw_data(void *p)
275{
276 return p + 4;
277}
278
279#define perf_synth__raw_size(d) (sizeof(d) - 4)
280
281#define perf_sample__bad_synth_size(s, d) ((s)->raw_size < sizeof(d) - 4)
282
283enum {
284 PERF_STAT_ROUND_TYPE__INTERVAL = 0,
285 PERF_STAT_ROUND_TYPE__FINAL = 1,
286};
287
288void perf_event__print_totals(void);
289
290struct perf_cpu_map;
291struct perf_record_stat_config;
292struct perf_stat_config;
293struct perf_tool;
294
295void perf_event__read_stat_config(struct perf_stat_config *config,
296 struct perf_record_stat_config *event);
297
298int perf_event__process_comm(struct perf_tool *tool,
299 union perf_event *event,
300 struct perf_sample *sample,
301 struct machine *machine);
302int perf_event__process_lost(struct perf_tool *tool,
303 union perf_event *event,
304 struct perf_sample *sample,
305 struct machine *machine);
306int perf_event__process_lost_samples(struct perf_tool *tool,
307 union perf_event *event,
308 struct perf_sample *sample,
309 struct machine *machine);
310int perf_event__process_aux(struct perf_tool *tool,
311 union perf_event *event,
312 struct perf_sample *sample,
313 struct machine *machine);
314int perf_event__process_itrace_start(struct perf_tool *tool,
315 union perf_event *event,
316 struct perf_sample *sample,
317 struct machine *machine);
318int perf_event__process_switch(struct perf_tool *tool,
319 union perf_event *event,
320 struct perf_sample *sample,
321 struct machine *machine);
322int perf_event__process_namespaces(struct perf_tool *tool,
323 union perf_event *event,
324 struct perf_sample *sample,
325 struct machine *machine);
326int perf_event__process_cgroup(struct perf_tool *tool,
327 union perf_event *event,
328 struct perf_sample *sample,
329 struct machine *machine);
330int perf_event__process_mmap(struct perf_tool *tool,
331 union perf_event *event,
332 struct perf_sample *sample,
333 struct machine *machine);
334int perf_event__process_mmap2(struct perf_tool *tool,
335 union perf_event *event,
336 struct perf_sample *sample,
337 struct machine *machine);
338int perf_event__process_fork(struct perf_tool *tool,
339 union perf_event *event,
340 struct perf_sample *sample,
341 struct machine *machine);
342int perf_event__process_exit(struct perf_tool *tool,
343 union perf_event *event,
344 struct perf_sample *sample,
345 struct machine *machine);
346int perf_event__process_ksymbol(struct perf_tool *tool,
347 union perf_event *event,
348 struct perf_sample *sample,
349 struct machine *machine);
350int perf_event__process_bpf(struct perf_tool *tool,
351 union perf_event *event,
352 struct perf_sample *sample,
353 struct machine *machine);
354int perf_event__process_text_poke(struct perf_tool *tool,
355 union perf_event *event,
356 struct perf_sample *sample,
357 struct machine *machine);
358int perf_event__process(struct perf_tool *tool,
359 union perf_event *event,
360 struct perf_sample *sample,
361 struct machine *machine);
362
363struct addr_location;
364
365int machine__resolve(struct machine *machine, struct addr_location *al,
366 struct perf_sample *sample);
367
368void addr_location__put(struct addr_location *al);
369
370struct thread;
371
372bool is_bts_event(struct perf_event_attr *attr);
373bool sample_addr_correlates_sym(struct perf_event_attr *attr);
374void thread__resolve(struct thread *thread, struct addr_location *al,
375 struct perf_sample *sample);
376
377const char *perf_event__name(unsigned int id);
378
379size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp);
380size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp);
381size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp);
382size_t perf_event__fprintf_task(union perf_event *event, FILE *fp);
383size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp);
384size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp);
385size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp);
386size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp);
387size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp);
388size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp);
389size_t perf_event__fprintf_cgroup(union perf_event *event, FILE *fp);
390size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp);
391size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp);
392size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *machine,FILE *fp);
393size_t perf_event__fprintf(union perf_event *event, struct machine *machine, FILE *fp);
394
395int kallsyms__get_function_start(const char *kallsyms_filename,
396 const char *symbol_name, u64 *addr);
397
398void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max);
399void cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf_cpu_map *map,
400 u16 type, int max);
401
402void event_attr_init(struct perf_event_attr *attr);
403
404int perf_event_paranoid(void);
405bool perf_event_paranoid_check(int max_level);
406
407extern int sysctl_perf_event_max_stack;
408extern int sysctl_perf_event_max_contexts_per_stack;
409extern unsigned int proc_map_timeout;
410
411#endif /* __PERF_RECORD_H */