xref: /linux/tools/perf/util/event.h (revision ec714e371f22f716a04e6ecb2a24988c92b26911)
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 struct dso;
16 struct machine;
17 struct perf_event_attr;
18 struct perf_sample;
19 
20 #ifdef __LP64__
21 /*
22  * /usr/include/inttypes.h uses just 'lu' for PRIu64, but we end up defining
23  * __u64 as long long unsigned int, and then -Werror=format= kicks in and
24  * complains of the mismatched types, so use these two special extra PRI
25  * macros to overcome that.
26  */
27 #define PRI_lu64 "l" PRIu64
28 #define PRI_lx64 "l" PRIx64
29 #define PRI_ld64 "l" PRId64
30 #else
31 #define PRI_lu64 PRIu64
32 #define PRI_lx64 PRIx64
33 #define PRI_ld64 PRId64
34 #endif
35 
36 #define PERF_SAMPLE_MASK				\
37 	(PERF_SAMPLE_IP | PERF_SAMPLE_TID |		\
38 	 PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR |		\
39 	PERF_SAMPLE_ID | PERF_SAMPLE_STREAM_ID |	\
40 	 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD |		\
41 	 PERF_SAMPLE_IDENTIFIER)
42 
43 /* perf sample has 16 bits size limit */
44 #define PERF_SAMPLE_MAX_SIZE (1 << 16)
45 
46 struct ip_callchain {
47 	u64 nr;
48 	u64 ips[];
49 };
50 
51 struct branch_stack;
52 
53 enum {
54 	PERF_IP_FLAG_BRANCH		= 1ULL << 0,
55 	PERF_IP_FLAG_CALL		= 1ULL << 1,
56 	PERF_IP_FLAG_RETURN		= 1ULL << 2,
57 	PERF_IP_FLAG_CONDITIONAL	= 1ULL << 3,
58 	PERF_IP_FLAG_SYSCALLRET		= 1ULL << 4,
59 	PERF_IP_FLAG_ASYNC		= 1ULL << 5,
60 	PERF_IP_FLAG_INTERRUPT		= 1ULL << 6,
61 	PERF_IP_FLAG_TX_ABORT		= 1ULL << 7,
62 	PERF_IP_FLAG_TRACE_BEGIN	= 1ULL << 8,
63 	PERF_IP_FLAG_TRACE_END		= 1ULL << 9,
64 	PERF_IP_FLAG_IN_TX		= 1ULL << 10,
65 	PERF_IP_FLAG_VMENTRY		= 1ULL << 11,
66 	PERF_IP_FLAG_VMEXIT		= 1ULL << 12,
67 	PERF_IP_FLAG_INTR_DISABLE	= 1ULL << 13,
68 	PERF_IP_FLAG_INTR_TOGGLE	= 1ULL << 14,
69 	PERF_IP_FLAG_BRANCH_MISS	= 1ULL << 15,
70 	PERF_IP_FLAG_NOT_TAKEN		= 1ULL << 16,
71 };
72 
73 #define PERF_IP_FLAG_CHARS "bcrosyiABExghDtmn"
74 
75 #define PERF_ADDITIONAL_STATE_MASK	\
76 	(PERF_IP_FLAG_IN_TX |		\
77 	 PERF_IP_FLAG_INTR_DISABLE |	\
78 	 PERF_IP_FLAG_INTR_TOGGLE)
79 
80 #define PERF_BRANCH_MASK		(\
81 	PERF_IP_FLAG_BRANCH		|\
82 	PERF_IP_FLAG_CALL		|\
83 	PERF_IP_FLAG_RETURN		|\
84 	PERF_IP_FLAG_CONDITIONAL	|\
85 	PERF_IP_FLAG_SYSCALLRET		|\
86 	PERF_IP_FLAG_ASYNC		|\
87 	PERF_IP_FLAG_INTERRUPT		|\
88 	PERF_IP_FLAG_TX_ABORT		|\
89 	PERF_IP_FLAG_TRACE_BEGIN	|\
90 	PERF_IP_FLAG_TRACE_END		|\
91 	PERF_IP_FLAG_VMENTRY		|\
92 	PERF_IP_FLAG_VMEXIT)
93 
94 #define PERF_IP_FLAG_BRANCH_EVENT_MASK	\
95 	(PERF_IP_FLAG_BRANCH_MISS |	\
96 	 PERF_IP_FLAG_NOT_TAKEN)
97 
98 #define PERF_MEM_DATA_SRC_NONE \
99 	(PERF_MEM_S(OP, NA) |\
100 	 PERF_MEM_S(LVL, NA) |\
101 	 PERF_MEM_S(SNOOP, NA) |\
102 	 PERF_MEM_S(LOCK, NA) |\
103 	 PERF_MEM_S(TLB, NA) |\
104 	 PERF_MEM_S(LVLNUM, NA))
105 
106 /* Attribute type for custom synthesized events */
107 #define PERF_TYPE_SYNTH		(INT_MAX + 1U)
108 
109 /* Attribute config for custom synthesized events */
110 enum perf_synth_id {
111 	PERF_SYNTH_INTEL_PTWRITE,
112 	PERF_SYNTH_INTEL_MWAIT,
113 	PERF_SYNTH_INTEL_PWRE,
114 	PERF_SYNTH_INTEL_EXSTOP,
115 	PERF_SYNTH_INTEL_PWRX,
116 	PERF_SYNTH_INTEL_CBR,
117 	PERF_SYNTH_INTEL_PSB,
118 	PERF_SYNTH_INTEL_EVT,
119 	PERF_SYNTH_INTEL_IFLAG_CHG,
120 	PERF_SYNTH_POWERPC_VPA_DTL,
121 };
122 
123 /*
124  * Raw data formats for synthesized events. Note that 4 bytes of padding are
125  * present to match the 'size' member of PERF_SAMPLE_RAW data which is always
126  * 8-byte aligned. That means we must dereference raw_data with an offset of 4.
127  * Refer perf_sample__synth_ptr() and perf_synth__raw_data().  It also means the
128  * structure sizes are 4 bytes bigger than the raw_size, refer
129  * perf_synth__raw_size().
130  */
131 
132 struct perf_synth_intel_ptwrite {
133 	u32 padding;
134 	union {
135 		struct {
136 			u32	ip		:  1,
137 				reserved	: 31;
138 		};
139 		u32	flags;
140 	};
141 	u64	payload;
142 };
143 
144 struct perf_synth_intel_mwait {
145 	u32 padding;
146 	u32 reserved;
147 	union {
148 		struct {
149 			u64	hints		:  8,
150 				reserved1	: 24,
151 				extensions	:  2,
152 				reserved2	: 30;
153 		};
154 		u64	payload;
155 	};
156 };
157 
158 struct perf_synth_intel_pwre {
159 	u32 padding;
160 	u32 reserved;
161 	union {
162 		struct {
163 			u64	reserved1	:  7,
164 				hw		:  1,
165 				subcstate	:  4,
166 				cstate		:  4,
167 				reserved2	: 48;
168 		};
169 		u64	payload;
170 	};
171 };
172 
173 struct perf_synth_intel_exstop {
174 	u32 padding;
175 	union {
176 		struct {
177 			u32	ip		:  1,
178 				reserved	: 31;
179 		};
180 		u32	flags;
181 	};
182 };
183 
184 struct perf_synth_intel_pwrx {
185 	u32 padding;
186 	u32 reserved;
187 	union {
188 		struct {
189 			u64	deepest_cstate	:  4,
190 				last_cstate	:  4,
191 				wake_reason	:  4,
192 				reserved1	: 52;
193 		};
194 		u64	payload;
195 	};
196 };
197 
198 struct perf_synth_intel_cbr {
199 	u32 padding;
200 	union {
201 		struct {
202 			u32	cbr		:  8,
203 				reserved1	:  8,
204 				max_nonturbo	:  8,
205 				reserved2	:  8;
206 		};
207 		u32	flags;
208 	};
209 	u32 freq;
210 	u32 reserved3;
211 };
212 
213 struct perf_synth_intel_psb {
214 	u32 padding;
215 	u32 reserved;
216 	u64 offset;
217 };
218 
219 struct perf_synth_intel_evd {
220 	union {
221 		struct {
222 			u8	evd_type;
223 			u8	reserved[7];
224 		};
225 		u64	et;
226 	};
227 	u64	payload;
228 };
229 
230 /* Intel PT Event Trace */
231 struct perf_synth_intel_evt {
232 	u32 padding;
233 	union {
234 		struct {
235 			u32	type		:  5,
236 				reserved	:  2,
237 				ip		:  1,
238 				vector		:  8,
239 				evd_cnt		: 16;
240 		};
241 		u32	cfe;
242 	};
243 	struct perf_synth_intel_evd evd[0];
244 };
245 
246 struct perf_synth_intel_iflag_chg {
247 	u32 padding;
248 	union {
249 		struct {
250 			u32	iflag		:  1,
251 				via_branch	:  1;
252 		};
253 		u32	flags;
254 	};
255 	u64	branch_ip; /* If via_branch */
256 };
257 
258 /*
259  * The powerpc VPA DTL entries are of below format
260  */
261 struct powerpc_vpadtl_entry {
262 	u8      dispatch_reason;
263 	u8      preempt_reason;
264 	u16     processor_id;
265 	u32     enqueue_to_dispatch_time;
266 	u32     ready_to_enqueue_time;
267 	u32     waiting_to_ready_time;
268 	u64     timebase;
269 	u64     fault_addr;
270 	u64     srr0;
271 	u64     srr1;
272 };
273 
274 extern const char *dispatch_reasons[11];
275 extern const char *preempt_reasons[10];
276 
perf_synth__raw_data(void * p)277 static inline void *perf_synth__raw_data(void *p)
278 {
279 	return p + 4;
280 }
281 
282 #define perf_synth__raw_size(d) (sizeof(d) - 4)
283 
284 #define perf_sample__bad_synth_size(s, d) ((s)->raw_size < sizeof(d) - 4)
285 
286 enum {
287 	PERF_STAT_ROUND_TYPE__INTERVAL	= 0,
288 	PERF_STAT_ROUND_TYPE__FINAL	= 1,
289 };
290 
291 void perf_event__print_totals(void);
292 
293 struct perf_cpu_map;
294 struct perf_record_stat_config;
295 struct perf_stat_config;
296 struct perf_tool;
297 
298 void perf_event__read_stat_config(struct perf_stat_config *config,
299 				  struct perf_record_stat_config *event);
300 
301 int perf_event__process_comm(const struct perf_tool *tool,
302 			     union perf_event *event,
303 			     struct perf_sample *sample,
304 			     struct machine *machine);
305 int perf_event__process_lost(const struct perf_tool *tool,
306 			     union perf_event *event,
307 			     struct perf_sample *sample,
308 			     struct machine *machine);
309 int perf_event__process_lost_samples(const struct perf_tool *tool,
310 				     union perf_event *event,
311 				     struct perf_sample *sample,
312 				     struct machine *machine);
313 int perf_event__process_aux(const struct perf_tool *tool,
314 			    union perf_event *event,
315 			    struct perf_sample *sample,
316 			    struct machine *machine);
317 int perf_event__process_itrace_start(const struct perf_tool *tool,
318 				     union perf_event *event,
319 				     struct perf_sample *sample,
320 				     struct machine *machine);
321 int perf_event__process_aux_output_hw_id(const struct perf_tool *tool,
322 					 union perf_event *event,
323 					 struct perf_sample *sample,
324 					 struct machine *machine);
325 int perf_event__process_switch(const struct perf_tool *tool,
326 			       union perf_event *event,
327 			       struct perf_sample *sample,
328 			       struct machine *machine);
329 int perf_event__process_namespaces(const struct perf_tool *tool,
330 				   union perf_event *event,
331 				   struct perf_sample *sample,
332 				   struct machine *machine);
333 int perf_event__process_cgroup(const struct perf_tool *tool,
334 			       union perf_event *event,
335 			       struct perf_sample *sample,
336 			       struct machine *machine);
337 int perf_event__process_mmap(const struct perf_tool *tool,
338 			     union perf_event *event,
339 			     struct perf_sample *sample,
340 			     struct machine *machine);
341 int perf_event__process_mmap2(const struct perf_tool *tool,
342 			     union perf_event *event,
343 			     struct perf_sample *sample,
344 			     struct machine *machine);
345 int perf_event__process_fork(const struct perf_tool *tool,
346 			     union perf_event *event,
347 			     struct perf_sample *sample,
348 			     struct machine *machine);
349 int perf_event__process_exit(const struct perf_tool *tool,
350 			     union perf_event *event,
351 			     struct perf_sample *sample,
352 			     struct machine *machine);
353 int perf_event__exit_del_thread(const struct perf_tool *tool,
354 				union perf_event *event,
355 				struct perf_sample *sample,
356 				struct machine *machine);
357 int perf_event__process_ksymbol(const struct perf_tool *tool,
358 				union perf_event *event,
359 				struct perf_sample *sample,
360 				struct machine *machine);
361 int perf_event__process_bpf(const struct perf_tool *tool,
362 			    union perf_event *event,
363 			    struct perf_sample *sample,
364 			    struct machine *machine);
365 int perf_event__process_text_poke(const struct perf_tool *tool,
366 				  union perf_event *event,
367 				  struct perf_sample *sample,
368 				  struct machine *machine);
369 int perf_event__process(const struct perf_tool *tool,
370 			union perf_event *event,
371 			struct perf_sample *sample,
372 			struct machine *machine);
373 
374 bool is_bts_event(struct perf_event_attr *attr);
375 bool sample_addr_correlates_sym(struct perf_event_attr *attr);
376 
377 const char *perf_event__name(unsigned int id);
378 
379 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp);
380 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp);
381 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp);
382 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp);
383 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp);
384 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp);
385 size_t perf_event__fprintf_aux_output_hw_id(union perf_event *event, FILE *fp);
386 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp);
387 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp);
388 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp);
389 size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp);
390 size_t perf_event__fprintf_cgroup(union perf_event *event, FILE *fp);
391 size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp);
392 size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp);
393 size_t perf_event__fprintf_bpf_metadata(union perf_event *event, FILE *fp);
394 size_t perf_event__fprintf_text_poke(union perf_event *event, struct machine *machine,FILE *fp);
395 size_t perf_event__fprintf(union perf_event *event, struct machine *machine, FILE *fp);
396 
397 int kallsyms__get_function_start(const char *kallsyms_filename,
398 				 const char *symbol_name, u64 *addr);
399 int kallsyms__get_symbol_start(const char *kallsyms_filename,
400 			       const char *symbol_name, u64 *addr);
401 
402 void event_attr_init(struct perf_event_attr *attr);
403 
404 int perf_event_paranoid(void);
405 bool perf_event_paranoid_check(int max_level);
406 
407 extern int sysctl_perf_event_max_stack;
408 extern int sysctl_perf_event_max_contexts_per_stack;
409 extern unsigned int proc_map_timeout;
410 
411 #define PAGE_SIZE_NAME_LEN	32
412 char *get_page_size_name(u64 size, char *str);
413 
perf_event_header__cpumode_is_guest(u8 cpumode)414 static inline bool perf_event_header__cpumode_is_guest(u8 cpumode)
415 {
416 	return cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
417 	       cpumode == PERF_RECORD_MISC_GUEST_USER;
418 }
419 
perf_event_header__misc_is_guest(u16 misc)420 static inline bool perf_event_header__misc_is_guest(u16 misc)
421 {
422 	return perf_event_header__cpumode_is_guest(misc & PERF_RECORD_MISC_CPUMODE_MASK);
423 }
424 
perf_event_header__is_guest(const struct perf_event_header * header)425 static inline bool perf_event_header__is_guest(const struct perf_event_header *header)
426 {
427 	return perf_event_header__misc_is_guest(header->misc);
428 }
429 
perf_event__is_guest(const union perf_event * event)430 static inline bool perf_event__is_guest(const union perf_event *event)
431 {
432 	return perf_event_header__is_guest(&event->header);
433 }
434 
435 #endif /* __PERF_RECORD_H */
436