xref: /openbmc/linux/tools/perf/util/event.h (revision 94d964e5)
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 
17 struct dso;
18 struct machine;
19 struct 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 
47 /* number of register is bound by the number of bits in regs_dump::mask (64) */
48 #define PERF_SAMPLE_REGS_CACHE_SIZE (8 * sizeof(u64))
49 
50 struct regs_dump {
51 	u64 abi;
52 	u64 mask;
53 	u64 *regs;
54 
55 	/* Cached values/mask filled by first register access. */
56 	u64 cache_regs[PERF_SAMPLE_REGS_CACHE_SIZE];
57 	u64 cache_mask;
58 };
59 
60 struct stack_dump {
61 	u16 offset;
62 	u64 size;
63 	char *data;
64 };
65 
66 struct sample_read_value {
67 	u64 value;
68 	u64 id;
69 };
70 
71 struct sample_read {
72 	u64 time_enabled;
73 	u64 time_running;
74 	union {
75 		struct {
76 			u64 nr;
77 			struct sample_read_value *values;
78 		} group;
79 		struct sample_read_value one;
80 	};
81 };
82 
83 struct ip_callchain {
84 	u64 nr;
85 	u64 ips[];
86 };
87 
88 struct branch_stack;
89 
90 enum {
91 	PERF_IP_FLAG_BRANCH		= 1ULL << 0,
92 	PERF_IP_FLAG_CALL		= 1ULL << 1,
93 	PERF_IP_FLAG_RETURN		= 1ULL << 2,
94 	PERF_IP_FLAG_CONDITIONAL	= 1ULL << 3,
95 	PERF_IP_FLAG_SYSCALLRET		= 1ULL << 4,
96 	PERF_IP_FLAG_ASYNC		= 1ULL << 5,
97 	PERF_IP_FLAG_INTERRUPT		= 1ULL << 6,
98 	PERF_IP_FLAG_TX_ABORT		= 1ULL << 7,
99 	PERF_IP_FLAG_TRACE_BEGIN	= 1ULL << 8,
100 	PERF_IP_FLAG_TRACE_END		= 1ULL << 9,
101 	PERF_IP_FLAG_IN_TX		= 1ULL << 10,
102 	PERF_IP_FLAG_VMENTRY		= 1ULL << 11,
103 	PERF_IP_FLAG_VMEXIT		= 1ULL << 12,
104 };
105 
106 #define PERF_IP_FLAG_CHARS "bcrosyiABExgh"
107 
108 #define PERF_BRANCH_MASK		(\
109 	PERF_IP_FLAG_BRANCH		|\
110 	PERF_IP_FLAG_CALL		|\
111 	PERF_IP_FLAG_RETURN		|\
112 	PERF_IP_FLAG_CONDITIONAL	|\
113 	PERF_IP_FLAG_SYSCALLRET		|\
114 	PERF_IP_FLAG_ASYNC		|\
115 	PERF_IP_FLAG_INTERRUPT		|\
116 	PERF_IP_FLAG_TX_ABORT		|\
117 	PERF_IP_FLAG_TRACE_BEGIN	|\
118 	PERF_IP_FLAG_TRACE_END		|\
119 	PERF_IP_FLAG_VMENTRY		|\
120 	PERF_IP_FLAG_VMEXIT)
121 
122 #define MAX_INSN 16
123 
124 struct aux_sample {
125 	u64 size;
126 	void *data;
127 };
128 
129 struct perf_sample {
130 	u64 ip;
131 	u32 pid, tid;
132 	u64 time;
133 	u64 addr;
134 	u64 id;
135 	u64 stream_id;
136 	u64 period;
137 	u64 weight;
138 	u64 transaction;
139 	u64 insn_cnt;
140 	u64 cyc_cnt;
141 	u32 cpu;
142 	u32 raw_size;
143 	u64 data_src;
144 	u64 phys_addr;
145 	u64 data_page_size;
146 	u64 code_page_size;
147 	u64 cgroup;
148 	u32 flags;
149 	u16 insn_len;
150 	u8  cpumode;
151 	u16 misc;
152 	u16 ins_lat;
153 	u16 p_stage_cyc;
154 	bool no_hw_idx;		/* No hw_idx collected in branch_stack */
155 	char insn[MAX_INSN];
156 	void *raw_data;
157 	struct ip_callchain *callchain;
158 	struct branch_stack *branch_stack;
159 	struct regs_dump  user_regs;
160 	struct regs_dump  intr_regs;
161 	struct stack_dump user_stack;
162 	struct sample_read read;
163 	struct aux_sample aux_sample;
164 };
165 
166 #define PERF_MEM_DATA_SRC_NONE \
167 	(PERF_MEM_S(OP, NA) |\
168 	 PERF_MEM_S(LVL, NA) |\
169 	 PERF_MEM_S(SNOOP, NA) |\
170 	 PERF_MEM_S(LOCK, NA) |\
171 	 PERF_MEM_S(TLB, NA))
172 
173 /* Attribute type for custom synthesized events */
174 #define PERF_TYPE_SYNTH		(INT_MAX + 1U)
175 
176 /* Attribute config for custom synthesized events */
177 enum perf_synth_id {
178 	PERF_SYNTH_INTEL_PTWRITE,
179 	PERF_SYNTH_INTEL_MWAIT,
180 	PERF_SYNTH_INTEL_PWRE,
181 	PERF_SYNTH_INTEL_EXSTOP,
182 	PERF_SYNTH_INTEL_PWRX,
183 	PERF_SYNTH_INTEL_CBR,
184 	PERF_SYNTH_INTEL_PSB,
185 };
186 
187 /*
188  * Raw data formats for synthesized events. Note that 4 bytes of padding are
189  * present to match the 'size' member of PERF_SAMPLE_RAW data which is always
190  * 8-byte aligned. That means we must dereference raw_data with an offset of 4.
191  * Refer perf_sample__synth_ptr() and perf_synth__raw_data().  It also means the
192  * structure sizes are 4 bytes bigger than the raw_size, refer
193  * perf_synth__raw_size().
194  */
195 
196 struct perf_synth_intel_ptwrite {
197 	u32 padding;
198 	union {
199 		struct {
200 			u32	ip		:  1,
201 				reserved	: 31;
202 		};
203 		u32	flags;
204 	};
205 	u64	payload;
206 };
207 
208 struct perf_synth_intel_mwait {
209 	u32 padding;
210 	u32 reserved;
211 	union {
212 		struct {
213 			u64	hints		:  8,
214 				reserved1	: 24,
215 				extensions	:  2,
216 				reserved2	: 30;
217 		};
218 		u64	payload;
219 	};
220 };
221 
222 struct perf_synth_intel_pwre {
223 	u32 padding;
224 	u32 reserved;
225 	union {
226 		struct {
227 			u64	reserved1	:  7,
228 				hw		:  1,
229 				subcstate	:  4,
230 				cstate		:  4,
231 				reserved2	: 48;
232 		};
233 		u64	payload;
234 	};
235 };
236 
237 struct perf_synth_intel_exstop {
238 	u32 padding;
239 	union {
240 		struct {
241 			u32	ip		:  1,
242 				reserved	: 31;
243 		};
244 		u32	flags;
245 	};
246 };
247 
248 struct perf_synth_intel_pwrx {
249 	u32 padding;
250 	u32 reserved;
251 	union {
252 		struct {
253 			u64	deepest_cstate	:  4,
254 				last_cstate	:  4,
255 				wake_reason	:  4,
256 				reserved1	: 52;
257 		};
258 		u64	payload;
259 	};
260 };
261 
262 struct perf_synth_intel_cbr {
263 	u32 padding;
264 	union {
265 		struct {
266 			u32	cbr		:  8,
267 				reserved1	:  8,
268 				max_nonturbo	:  8,
269 				reserved2	:  8;
270 		};
271 		u32	flags;
272 	};
273 	u32 freq;
274 	u32 reserved3;
275 };
276 
277 struct perf_synth_intel_psb {
278 	u32 padding;
279 	u32 reserved;
280 	u64 offset;
281 };
282 
283 /*
284  * raw_data is always 4 bytes from an 8-byte boundary, so subtract 4 to get
285  * 8-byte alignment.
286  */
287 static inline void *perf_sample__synth_ptr(struct perf_sample *sample)
288 {
289 	return sample->raw_data - 4;
290 }
291 
292 static inline void *perf_synth__raw_data(void *p)
293 {
294 	return p + 4;
295 }
296 
297 #define perf_synth__raw_size(d) (sizeof(d) - 4)
298 
299 #define perf_sample__bad_synth_size(s, d) ((s)->raw_size < sizeof(d) - 4)
300 
301 enum {
302 	PERF_STAT_ROUND_TYPE__INTERVAL	= 0,
303 	PERF_STAT_ROUND_TYPE__FINAL	= 1,
304 };
305 
306 void perf_event__print_totals(void);
307 
308 struct perf_cpu_map;
309 struct perf_record_stat_config;
310 struct perf_stat_config;
311 struct perf_tool;
312 
313 void perf_event__read_stat_config(struct perf_stat_config *config,
314 				  struct perf_record_stat_config *event);
315 
316 int perf_event__process_comm(struct perf_tool *tool,
317 			     union perf_event *event,
318 			     struct perf_sample *sample,
319 			     struct machine *machine);
320 int perf_event__process_lost(struct perf_tool *tool,
321 			     union perf_event *event,
322 			     struct perf_sample *sample,
323 			     struct machine *machine);
324 int perf_event__process_lost_samples(struct perf_tool *tool,
325 				     union perf_event *event,
326 				     struct perf_sample *sample,
327 				     struct machine *machine);
328 int perf_event__process_aux(struct perf_tool *tool,
329 			    union perf_event *event,
330 			    struct perf_sample *sample,
331 			    struct machine *machine);
332 int perf_event__process_itrace_start(struct perf_tool *tool,
333 				     union perf_event *event,
334 				     struct perf_sample *sample,
335 				     struct machine *machine);
336 int perf_event__process_aux_output_hw_id(struct perf_tool *tool,
337 					 union perf_event *event,
338 					 struct perf_sample *sample,
339 					 struct machine *machine);
340 int perf_event__process_switch(struct perf_tool *tool,
341 			       union perf_event *event,
342 			       struct perf_sample *sample,
343 			       struct machine *machine);
344 int perf_event__process_namespaces(struct perf_tool *tool,
345 				   union perf_event *event,
346 				   struct perf_sample *sample,
347 				   struct machine *machine);
348 int perf_event__process_cgroup(struct perf_tool *tool,
349 			       union perf_event *event,
350 			       struct perf_sample *sample,
351 			       struct machine *machine);
352 int perf_event__process_mmap(struct perf_tool *tool,
353 			     union perf_event *event,
354 			     struct perf_sample *sample,
355 			     struct machine *machine);
356 int perf_event__process_mmap2(struct perf_tool *tool,
357 			     union perf_event *event,
358 			     struct perf_sample *sample,
359 			     struct machine *machine);
360 int perf_event__process_fork(struct perf_tool *tool,
361 			     union perf_event *event,
362 			     struct perf_sample *sample,
363 			     struct machine *machine);
364 int perf_event__process_exit(struct perf_tool *tool,
365 			     union perf_event *event,
366 			     struct perf_sample *sample,
367 			     struct machine *machine);
368 int perf_event__process_ksymbol(struct perf_tool *tool,
369 				union perf_event *event,
370 				struct perf_sample *sample,
371 				struct machine *machine);
372 int perf_event__process_bpf(struct perf_tool *tool,
373 			    union perf_event *event,
374 			    struct perf_sample *sample,
375 			    struct machine *machine);
376 int perf_event__process_text_poke(struct perf_tool *tool,
377 				  union perf_event *event,
378 				  struct perf_sample *sample,
379 				  struct machine *machine);
380 int perf_event__process(struct perf_tool *tool,
381 			union perf_event *event,
382 			struct perf_sample *sample,
383 			struct machine *machine);
384 
385 struct addr_location;
386 
387 int machine__resolve(struct machine *machine, struct addr_location *al,
388 		     struct perf_sample *sample);
389 
390 void addr_location__put(struct addr_location *al);
391 
392 struct thread;
393 
394 bool is_bts_event(struct perf_event_attr *attr);
395 bool sample_addr_correlates_sym(struct perf_event_attr *attr);
396 void thread__resolve(struct thread *thread, struct addr_location *al,
397 		     struct perf_sample *sample);
398 
399 const char *perf_event__name(unsigned int id);
400 
401 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp);
402 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp);
403 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp);
404 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp);
405 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp);
406 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp);
407 size_t perf_event__fprintf_aux_output_hw_id(union perf_event *event, FILE *fp);
408 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp);
409 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp);
410 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp);
411 size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp);
412 size_t perf_event__fprintf_cgroup(union perf_event *event, FILE *fp);
413 size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp);
414 size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp);
415 size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *machine,FILE *fp);
416 size_t perf_event__fprintf(union perf_event *event, struct machine *machine, FILE *fp);
417 
418 int kallsyms__get_function_start(const char *kallsyms_filename,
419 				 const char *symbol_name, u64 *addr);
420 
421 void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max);
422 void  cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf_cpu_map *map,
423 			       u16 type, int max);
424 
425 void event_attr_init(struct perf_event_attr *attr);
426 
427 int perf_event_paranoid(void);
428 bool perf_event_paranoid_check(int max_level);
429 
430 extern int sysctl_perf_event_max_stack;
431 extern int sysctl_perf_event_max_contexts_per_stack;
432 extern unsigned int proc_map_timeout;
433 
434 #define PAGE_SIZE_NAME_LEN	32
435 char *get_page_size_name(u64 size, char *str);
436 
437 void arch_perf_parse_sample_weight(struct perf_sample *data, const __u64 *array, u64 type);
438 void arch_perf_synthesize_sample_weight(const struct perf_sample *data, __u64 *array, u64 type);
439 const char *arch_perf_header_entry(const char *se_header);
440 int arch_support_sort_key(const char *sort_key);
441 
442 #endif /* __PERF_RECORD_H */
443