xref: /openbmc/linux/tools/perf/util/event.h (revision 7ae5c03a)
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 	PERF_IP_FLAG_INTR_DISABLE	= 1ULL << 13,
105 	PERF_IP_FLAG_INTR_TOGGLE	= 1ULL << 14,
106 };
107 
108 #define PERF_IP_FLAG_CHARS "bcrosyiABExghDt"
109 
110 #define PERF_BRANCH_MASK		(\
111 	PERF_IP_FLAG_BRANCH		|\
112 	PERF_IP_FLAG_CALL		|\
113 	PERF_IP_FLAG_RETURN		|\
114 	PERF_IP_FLAG_CONDITIONAL	|\
115 	PERF_IP_FLAG_SYSCALLRET		|\
116 	PERF_IP_FLAG_ASYNC		|\
117 	PERF_IP_FLAG_INTERRUPT		|\
118 	PERF_IP_FLAG_TX_ABORT		|\
119 	PERF_IP_FLAG_TRACE_BEGIN	|\
120 	PERF_IP_FLAG_TRACE_END		|\
121 	PERF_IP_FLAG_VMENTRY		|\
122 	PERF_IP_FLAG_VMEXIT)
123 
124 #define MAX_INSN 16
125 
126 struct aux_sample {
127 	u64 size;
128 	void *data;
129 };
130 
131 struct perf_sample {
132 	u64 ip;
133 	u32 pid, tid;
134 	u64 time;
135 	u64 addr;
136 	u64 id;
137 	u64 stream_id;
138 	u64 period;
139 	u64 weight;
140 	u64 transaction;
141 	u64 insn_cnt;
142 	u64 cyc_cnt;
143 	u32 cpu;
144 	u32 raw_size;
145 	u64 data_src;
146 	u64 phys_addr;
147 	u64 data_page_size;
148 	u64 code_page_size;
149 	u64 cgroup;
150 	u32 flags;
151 	u32 machine_pid;
152 	u32 vcpu;
153 	u16 insn_len;
154 	u8  cpumode;
155 	u16 misc;
156 	u16 ins_lat;
157 	u16 p_stage_cyc;
158 	bool no_hw_idx;		/* No hw_idx collected in branch_stack */
159 	char insn[MAX_INSN];
160 	void *raw_data;
161 	struct ip_callchain *callchain;
162 	struct branch_stack *branch_stack;
163 	struct regs_dump  user_regs;
164 	struct regs_dump  intr_regs;
165 	struct stack_dump user_stack;
166 	struct sample_read read;
167 	struct aux_sample aux_sample;
168 };
169 
170 #define PERF_MEM_DATA_SRC_NONE \
171 	(PERF_MEM_S(OP, NA) |\
172 	 PERF_MEM_S(LVL, NA) |\
173 	 PERF_MEM_S(SNOOP, NA) |\
174 	 PERF_MEM_S(LOCK, NA) |\
175 	 PERF_MEM_S(TLB, NA))
176 
177 /* Attribute type for custom synthesized events */
178 #define PERF_TYPE_SYNTH		(INT_MAX + 1U)
179 
180 /* Attribute config for custom synthesized events */
181 enum perf_synth_id {
182 	PERF_SYNTH_INTEL_PTWRITE,
183 	PERF_SYNTH_INTEL_MWAIT,
184 	PERF_SYNTH_INTEL_PWRE,
185 	PERF_SYNTH_INTEL_EXSTOP,
186 	PERF_SYNTH_INTEL_PWRX,
187 	PERF_SYNTH_INTEL_CBR,
188 	PERF_SYNTH_INTEL_PSB,
189 	PERF_SYNTH_INTEL_EVT,
190 	PERF_SYNTH_INTEL_IFLAG_CHG,
191 };
192 
193 /*
194  * Raw data formats for synthesized events. Note that 4 bytes of padding are
195  * present to match the 'size' member of PERF_SAMPLE_RAW data which is always
196  * 8-byte aligned. That means we must dereference raw_data with an offset of 4.
197  * Refer perf_sample__synth_ptr() and perf_synth__raw_data().  It also means the
198  * structure sizes are 4 bytes bigger than the raw_size, refer
199  * perf_synth__raw_size().
200  */
201 
202 struct perf_synth_intel_ptwrite {
203 	u32 padding;
204 	union {
205 		struct {
206 			u32	ip		:  1,
207 				reserved	: 31;
208 		};
209 		u32	flags;
210 	};
211 	u64	payload;
212 };
213 
214 struct perf_synth_intel_mwait {
215 	u32 padding;
216 	u32 reserved;
217 	union {
218 		struct {
219 			u64	hints		:  8,
220 				reserved1	: 24,
221 				extensions	:  2,
222 				reserved2	: 30;
223 		};
224 		u64	payload;
225 	};
226 };
227 
228 struct perf_synth_intel_pwre {
229 	u32 padding;
230 	u32 reserved;
231 	union {
232 		struct {
233 			u64	reserved1	:  7,
234 				hw		:  1,
235 				subcstate	:  4,
236 				cstate		:  4,
237 				reserved2	: 48;
238 		};
239 		u64	payload;
240 	};
241 };
242 
243 struct perf_synth_intel_exstop {
244 	u32 padding;
245 	union {
246 		struct {
247 			u32	ip		:  1,
248 				reserved	: 31;
249 		};
250 		u32	flags;
251 	};
252 };
253 
254 struct perf_synth_intel_pwrx {
255 	u32 padding;
256 	u32 reserved;
257 	union {
258 		struct {
259 			u64	deepest_cstate	:  4,
260 				last_cstate	:  4,
261 				wake_reason	:  4,
262 				reserved1	: 52;
263 		};
264 		u64	payload;
265 	};
266 };
267 
268 struct perf_synth_intel_cbr {
269 	u32 padding;
270 	union {
271 		struct {
272 			u32	cbr		:  8,
273 				reserved1	:  8,
274 				max_nonturbo	:  8,
275 				reserved2	:  8;
276 		};
277 		u32	flags;
278 	};
279 	u32 freq;
280 	u32 reserved3;
281 };
282 
283 struct perf_synth_intel_psb {
284 	u32 padding;
285 	u32 reserved;
286 	u64 offset;
287 };
288 
289 struct perf_synth_intel_evd {
290 	union {
291 		struct {
292 			u8	evd_type;
293 			u8	reserved[7];
294 		};
295 		u64	et;
296 	};
297 	u64	payload;
298 };
299 
300 /* Intel PT Event Trace */
301 struct perf_synth_intel_evt {
302 	u32 padding;
303 	union {
304 		struct {
305 			u32	type		:  5,
306 				reserved	:  2,
307 				ip		:  1,
308 				vector		:  8,
309 				evd_cnt		: 16;
310 		};
311 		u32	cfe;
312 	};
313 	struct perf_synth_intel_evd evd[0];
314 };
315 
316 struct perf_synth_intel_iflag_chg {
317 	u32 padding;
318 	union {
319 		struct {
320 			u32	iflag		:  1,
321 				via_branch	:  1;
322 		};
323 		u32	flags;
324 	};
325 	u64	branch_ip; /* If via_branch */
326 };
327 
328 /*
329  * raw_data is always 4 bytes from an 8-byte boundary, so subtract 4 to get
330  * 8-byte alignment.
331  */
332 static inline void *perf_sample__synth_ptr(struct perf_sample *sample)
333 {
334 	return sample->raw_data - 4;
335 }
336 
337 static inline void *perf_synth__raw_data(void *p)
338 {
339 	return p + 4;
340 }
341 
342 #define perf_synth__raw_size(d) (sizeof(d) - 4)
343 
344 #define perf_sample__bad_synth_size(s, d) ((s)->raw_size < sizeof(d) - 4)
345 
346 enum {
347 	PERF_STAT_ROUND_TYPE__INTERVAL	= 0,
348 	PERF_STAT_ROUND_TYPE__FINAL	= 1,
349 };
350 
351 void perf_event__print_totals(void);
352 
353 struct perf_cpu_map;
354 struct perf_record_stat_config;
355 struct perf_stat_config;
356 struct perf_tool;
357 
358 void perf_event__read_stat_config(struct perf_stat_config *config,
359 				  struct perf_record_stat_config *event);
360 
361 int perf_event__process_comm(struct perf_tool *tool,
362 			     union perf_event *event,
363 			     struct perf_sample *sample,
364 			     struct machine *machine);
365 int perf_event__process_lost(struct perf_tool *tool,
366 			     union perf_event *event,
367 			     struct perf_sample *sample,
368 			     struct machine *machine);
369 int perf_event__process_lost_samples(struct perf_tool *tool,
370 				     union perf_event *event,
371 				     struct perf_sample *sample,
372 				     struct machine *machine);
373 int perf_event__process_aux(struct perf_tool *tool,
374 			    union perf_event *event,
375 			    struct perf_sample *sample,
376 			    struct machine *machine);
377 int perf_event__process_itrace_start(struct perf_tool *tool,
378 				     union perf_event *event,
379 				     struct perf_sample *sample,
380 				     struct machine *machine);
381 int perf_event__process_aux_output_hw_id(struct perf_tool *tool,
382 					 union perf_event *event,
383 					 struct perf_sample *sample,
384 					 struct machine *machine);
385 int perf_event__process_switch(struct perf_tool *tool,
386 			       union perf_event *event,
387 			       struct perf_sample *sample,
388 			       struct machine *machine);
389 int perf_event__process_namespaces(struct perf_tool *tool,
390 				   union perf_event *event,
391 				   struct perf_sample *sample,
392 				   struct machine *machine);
393 int perf_event__process_cgroup(struct perf_tool *tool,
394 			       union perf_event *event,
395 			       struct perf_sample *sample,
396 			       struct machine *machine);
397 int perf_event__process_mmap(struct perf_tool *tool,
398 			     union perf_event *event,
399 			     struct perf_sample *sample,
400 			     struct machine *machine);
401 int perf_event__process_mmap2(struct perf_tool *tool,
402 			     union perf_event *event,
403 			     struct perf_sample *sample,
404 			     struct machine *machine);
405 int perf_event__process_fork(struct perf_tool *tool,
406 			     union perf_event *event,
407 			     struct perf_sample *sample,
408 			     struct machine *machine);
409 int perf_event__process_exit(struct perf_tool *tool,
410 			     union perf_event *event,
411 			     struct perf_sample *sample,
412 			     struct machine *machine);
413 int perf_event__process_ksymbol(struct perf_tool *tool,
414 				union perf_event *event,
415 				struct perf_sample *sample,
416 				struct machine *machine);
417 int perf_event__process_bpf(struct perf_tool *tool,
418 			    union perf_event *event,
419 			    struct perf_sample *sample,
420 			    struct machine *machine);
421 int perf_event__process_text_poke(struct perf_tool *tool,
422 				  union perf_event *event,
423 				  struct perf_sample *sample,
424 				  struct machine *machine);
425 int perf_event__process(struct perf_tool *tool,
426 			union perf_event *event,
427 			struct perf_sample *sample,
428 			struct machine *machine);
429 
430 struct addr_location;
431 
432 int machine__resolve(struct machine *machine, struct addr_location *al,
433 		     struct perf_sample *sample);
434 
435 void addr_location__put(struct addr_location *al);
436 
437 struct thread;
438 
439 bool is_bts_event(struct perf_event_attr *attr);
440 bool sample_addr_correlates_sym(struct perf_event_attr *attr);
441 void thread__resolve(struct thread *thread, struct addr_location *al,
442 		     struct perf_sample *sample);
443 
444 const char *perf_event__name(unsigned int id);
445 
446 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp);
447 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp);
448 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp);
449 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp);
450 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp);
451 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp);
452 size_t perf_event__fprintf_aux_output_hw_id(union perf_event *event, FILE *fp);
453 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp);
454 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp);
455 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp);
456 size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp);
457 size_t perf_event__fprintf_cgroup(union perf_event *event, FILE *fp);
458 size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp);
459 size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp);
460 size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *machine,FILE *fp);
461 size_t perf_event__fprintf(union perf_event *event, struct machine *machine, FILE *fp);
462 
463 int kallsyms__get_function_start(const char *kallsyms_filename,
464 				 const char *symbol_name, u64 *addr);
465 
466 void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max);
467 void  cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf_cpu_map *map,
468 			       u16 type, int max);
469 
470 void event_attr_init(struct perf_event_attr *attr);
471 
472 int perf_event_paranoid(void);
473 bool perf_event_paranoid_check(int max_level);
474 
475 extern int sysctl_perf_event_max_stack;
476 extern int sysctl_perf_event_max_contexts_per_stack;
477 extern unsigned int proc_map_timeout;
478 
479 #define PAGE_SIZE_NAME_LEN	32
480 char *get_page_size_name(u64 size, char *str);
481 
482 void arch_perf_parse_sample_weight(struct perf_sample *data, const __u64 *array, u64 type);
483 void arch_perf_synthesize_sample_weight(const struct perf_sample *data, __u64 *array, u64 type);
484 const char *arch_perf_header_entry(const char *se_header);
485 int arch_support_sort_key(const char *sort_key);
486 
487 static inline bool perf_event_header__cpumode_is_guest(u8 cpumode)
488 {
489 	return cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
490 	       cpumode == PERF_RECORD_MISC_GUEST_USER;
491 }
492 
493 static inline bool perf_event_header__misc_is_guest(u16 misc)
494 {
495 	return perf_event_header__cpumode_is_guest(misc & PERF_RECORD_MISC_CPUMODE_MASK);
496 }
497 
498 static inline bool perf_event_header__is_guest(const struct perf_event_header *header)
499 {
500 	return perf_event_header__misc_is_guest(header->misc);
501 }
502 
503 static inline bool perf_event__is_guest(const union perf_event *event)
504 {
505 	return perf_event_header__is_guest(&event->header);
506 }
507 
508 #endif /* __PERF_RECORD_H */
509