xref: /linux/tools/perf/util/event.h (revision c1a604dff486399ae0be95e6396e0158df95ad5d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_RECORD_H
3 #define __PERF_RECORD_H
4 
5 #include <limits.h>
6 #include <stdio.h>
7 #include <linux/kernel.h>
8 #include <linux/bpf.h>
9 #include <linux/perf_event.h>
10 #include <perf/event.h>
11 
12 #include "build-id.h"
13 #include "perf_regs.h"
14 
15 #ifdef __LP64__
16 /*
17  * /usr/include/inttypes.h uses just 'lu' for PRIu64, but we end up defining
18  * __u64 as long long unsigned int, and then -Werror=format= kicks in and
19  * complains of the mismatched types, so use these two special extra PRI
20  * macros to overcome that.
21  */
22 #define PRI_lu64 "l" PRIu64
23 #define PRI_lx64 "l" PRIx64
24 #define PRI_ld64 "l" PRId64
25 #else
26 #define PRI_lu64 PRIu64
27 #define PRI_lx64 PRIx64
28 #define PRI_ld64 PRId64
29 #endif
30 
31 #define PERF_SAMPLE_MASK				\
32 	(PERF_SAMPLE_IP | PERF_SAMPLE_TID |		\
33 	 PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR |		\
34 	PERF_SAMPLE_ID | PERF_SAMPLE_STREAM_ID |	\
35 	 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD |		\
36 	 PERF_SAMPLE_IDENTIFIER)
37 
38 /* perf sample has 16 bits size limit */
39 #define PERF_SAMPLE_MAX_SIZE (1 << 16)
40 
41 struct regs_dump {
42 	u64 abi;
43 	u64 mask;
44 	u64 *regs;
45 
46 	/* Cached values/mask filled by first register access. */
47 	u64 cache_regs[PERF_REGS_MAX];
48 	u64 cache_mask;
49 };
50 
51 struct stack_dump {
52 	u16 offset;
53 	u64 size;
54 	char *data;
55 };
56 
57 struct sample_read_value {
58 	u64 value;
59 	u64 id;
60 };
61 
62 struct sample_read {
63 	u64 time_enabled;
64 	u64 time_running;
65 	union {
66 		struct {
67 			u64 nr;
68 			struct sample_read_value *values;
69 		} group;
70 		struct sample_read_value one;
71 	};
72 };
73 
74 struct ip_callchain {
75 	u64 nr;
76 	u64 ips[0];
77 };
78 
79 struct branch_stack;
80 
81 enum {
82 	PERF_IP_FLAG_BRANCH		= 1ULL << 0,
83 	PERF_IP_FLAG_CALL		= 1ULL << 1,
84 	PERF_IP_FLAG_RETURN		= 1ULL << 2,
85 	PERF_IP_FLAG_CONDITIONAL	= 1ULL << 3,
86 	PERF_IP_FLAG_SYSCALLRET		= 1ULL << 4,
87 	PERF_IP_FLAG_ASYNC		= 1ULL << 5,
88 	PERF_IP_FLAG_INTERRUPT		= 1ULL << 6,
89 	PERF_IP_FLAG_TX_ABORT		= 1ULL << 7,
90 	PERF_IP_FLAG_TRACE_BEGIN	= 1ULL << 8,
91 	PERF_IP_FLAG_TRACE_END		= 1ULL << 9,
92 	PERF_IP_FLAG_IN_TX		= 1ULL << 10,
93 };
94 
95 #define PERF_IP_FLAG_CHARS "bcrosyiABEx"
96 
97 #define PERF_BRANCH_MASK		(\
98 	PERF_IP_FLAG_BRANCH		|\
99 	PERF_IP_FLAG_CALL		|\
100 	PERF_IP_FLAG_RETURN		|\
101 	PERF_IP_FLAG_CONDITIONAL	|\
102 	PERF_IP_FLAG_SYSCALLRET		|\
103 	PERF_IP_FLAG_ASYNC		|\
104 	PERF_IP_FLAG_INTERRUPT		|\
105 	PERF_IP_FLAG_TX_ABORT		|\
106 	PERF_IP_FLAG_TRACE_BEGIN	|\
107 	PERF_IP_FLAG_TRACE_END)
108 
109 #define MAX_INSN 16
110 
111 struct perf_sample {
112 	u64 ip;
113 	u32 pid, tid;
114 	u64 time;
115 	u64 addr;
116 	u64 id;
117 	u64 stream_id;
118 	u64 period;
119 	u64 weight;
120 	u64 transaction;
121 	u64 insn_cnt;
122 	u64 cyc_cnt;
123 	u32 cpu;
124 	u32 raw_size;
125 	u64 data_src;
126 	u64 phys_addr;
127 	u32 flags;
128 	u16 insn_len;
129 	u8  cpumode;
130 	u16 misc;
131 	char insn[MAX_INSN];
132 	void *raw_data;
133 	struct ip_callchain *callchain;
134 	struct branch_stack *branch_stack;
135 	struct regs_dump  user_regs;
136 	struct regs_dump  intr_regs;
137 	struct stack_dump user_stack;
138 	struct sample_read read;
139 };
140 
141 #define PERF_MEM_DATA_SRC_NONE \
142 	(PERF_MEM_S(OP, NA) |\
143 	 PERF_MEM_S(LVL, NA) |\
144 	 PERF_MEM_S(SNOOP, NA) |\
145 	 PERF_MEM_S(LOCK, NA) |\
146 	 PERF_MEM_S(TLB, NA))
147 
148 enum auxtrace_error_type {
149 	PERF_AUXTRACE_ERROR_ITRACE  = 1,
150 	PERF_AUXTRACE_ERROR_MAX
151 };
152 
153 /* Attribute type for custom synthesized events */
154 #define PERF_TYPE_SYNTH		(INT_MAX + 1U)
155 
156 /* Attribute config for custom synthesized events */
157 enum perf_synth_id {
158 	PERF_SYNTH_INTEL_PTWRITE,
159 	PERF_SYNTH_INTEL_MWAIT,
160 	PERF_SYNTH_INTEL_PWRE,
161 	PERF_SYNTH_INTEL_EXSTOP,
162 	PERF_SYNTH_INTEL_PWRX,
163 	PERF_SYNTH_INTEL_CBR,
164 };
165 
166 /*
167  * Raw data formats for synthesized events. Note that 4 bytes of padding are
168  * present to match the 'size' member of PERF_SAMPLE_RAW data which is always
169  * 8-byte aligned. That means we must dereference raw_data with an offset of 4.
170  * Refer perf_sample__synth_ptr() and perf_synth__raw_data().  It also means the
171  * structure sizes are 4 bytes bigger than the raw_size, refer
172  * perf_synth__raw_size().
173  */
174 
175 struct perf_synth_intel_ptwrite {
176 	u32 padding;
177 	union {
178 		struct {
179 			u32	ip		:  1,
180 				reserved	: 31;
181 		};
182 		u32	flags;
183 	};
184 	u64	payload;
185 };
186 
187 struct perf_synth_intel_mwait {
188 	u32 padding;
189 	u32 reserved;
190 	union {
191 		struct {
192 			u64	hints		:  8,
193 				reserved1	: 24,
194 				extensions	:  2,
195 				reserved2	: 30;
196 		};
197 		u64	payload;
198 	};
199 };
200 
201 struct perf_synth_intel_pwre {
202 	u32 padding;
203 	u32 reserved;
204 	union {
205 		struct {
206 			u64	reserved1	:  7,
207 				hw		:  1,
208 				subcstate	:  4,
209 				cstate		:  4,
210 				reserved2	: 48;
211 		};
212 		u64	payload;
213 	};
214 };
215 
216 struct perf_synth_intel_exstop {
217 	u32 padding;
218 	union {
219 		struct {
220 			u32	ip		:  1,
221 				reserved	: 31;
222 		};
223 		u32	flags;
224 	};
225 };
226 
227 struct perf_synth_intel_pwrx {
228 	u32 padding;
229 	u32 reserved;
230 	union {
231 		struct {
232 			u64	deepest_cstate	:  4,
233 				last_cstate	:  4,
234 				wake_reason	:  4,
235 				reserved1	: 52;
236 		};
237 		u64	payload;
238 	};
239 };
240 
241 struct perf_synth_intel_cbr {
242 	u32 padding;
243 	union {
244 		struct {
245 			u32	cbr		:  8,
246 				reserved1	:  8,
247 				max_nonturbo	:  8,
248 				reserved2	:  8;
249 		};
250 		u32	flags;
251 	};
252 	u32 freq;
253 	u32 reserved3;
254 };
255 
256 /*
257  * raw_data is always 4 bytes from an 8-byte boundary, so subtract 4 to get
258  * 8-byte alignment.
259  */
260 static inline void *perf_sample__synth_ptr(struct perf_sample *sample)
261 {
262 	return sample->raw_data - 4;
263 }
264 
265 static inline void *perf_synth__raw_data(void *p)
266 {
267 	return p + 4;
268 }
269 
270 #define perf_synth__raw_size(d) (sizeof(d) - 4)
271 
272 #define perf_sample__bad_synth_size(s, d) ((s)->raw_size < sizeof(d) - 4)
273 
274 /*
275  * The kernel collects the number of events it couldn't send in a stretch and
276  * when possible sends this number in a PERF_RECORD_LOST event. The number of
277  * such "chunks" of lost events is stored in .nr_events[PERF_EVENT_LOST] while
278  * total_lost tells exactly how many events the kernel in fact lost, i.e. it is
279  * the sum of all struct perf_record_lost.lost fields reported.
280  *
281  * The kernel discards mixed up samples and sends the number in a
282  * PERF_RECORD_LOST_SAMPLES event. The number of lost-samples events is stored
283  * in .nr_events[PERF_RECORD_LOST_SAMPLES] while total_lost_samples tells
284  * exactly how many samples the kernel in fact dropped, i.e. it is the sum of
285  * all struct perf_record_lost_samples.lost fields reported.
286  *
287  * The total_period is needed because by default auto-freq is used, so
288  * multipling nr_events[PERF_EVENT_SAMPLE] by a frequency isn't possible to get
289  * the total number of low level events, it is necessary to to sum all struct
290  * perf_record_sample.period and stash the result in total_period.
291  */
292 struct events_stats {
293 	u64 total_period;
294 	u64 total_non_filtered_period;
295 	u64 total_lost;
296 	u64 total_lost_samples;
297 	u64 total_aux_lost;
298 	u64 total_aux_partial;
299 	u64 total_invalid_chains;
300 	u32 nr_events[PERF_RECORD_HEADER_MAX];
301 	u32 nr_non_filtered_samples;
302 	u32 nr_lost_warned;
303 	u32 nr_unknown_events;
304 	u32 nr_invalid_chains;
305 	u32 nr_unknown_id;
306 	u32 nr_unprocessable_samples;
307 	u32 nr_auxtrace_errors[PERF_AUXTRACE_ERROR_MAX];
308 	u32 nr_proc_map_timeout;
309 };
310 
311 enum {
312 	PERF_STAT_ROUND_TYPE__INTERVAL	= 0,
313 	PERF_STAT_ROUND_TYPE__FINAL	= 1,
314 };
315 
316 void perf_event__print_totals(void);
317 
318 struct perf_tool;
319 struct perf_thread_map;
320 struct perf_cpu_map;
321 struct perf_stat_config;
322 struct perf_counts_values;
323 
324 typedef int (*perf_event__handler_t)(struct perf_tool *tool,
325 				     union perf_event *event,
326 				     struct perf_sample *sample,
327 				     struct machine *machine);
328 
329 int perf_event__synthesize_thread_map(struct perf_tool *tool,
330 				      struct perf_thread_map *threads,
331 				      perf_event__handler_t process,
332 				      struct machine *machine, bool mmap_data);
333 int perf_event__synthesize_thread_map2(struct perf_tool *tool,
334 				      struct perf_thread_map *threads,
335 				      perf_event__handler_t process,
336 				      struct machine *machine);
337 int perf_event__synthesize_cpu_map(struct perf_tool *tool,
338 				   struct perf_cpu_map *cpus,
339 				   perf_event__handler_t process,
340 				   struct machine *machine);
341 int perf_event__synthesize_threads(struct perf_tool *tool,
342 				   perf_event__handler_t process,
343 				   struct machine *machine, bool mmap_data,
344 				   unsigned int nr_threads_synthesize);
345 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
346 				       perf_event__handler_t process,
347 				       struct machine *machine);
348 int perf_event__synthesize_stat_config(struct perf_tool *tool,
349 				       struct perf_stat_config *config,
350 				       perf_event__handler_t process,
351 				       struct machine *machine);
352 void perf_event__read_stat_config(struct perf_stat_config *config,
353 				  struct perf_record_stat_config *event);
354 int perf_event__synthesize_stat(struct perf_tool *tool,
355 				u32 cpu, u32 thread, u64 id,
356 				struct perf_counts_values *count,
357 				perf_event__handler_t process,
358 				struct machine *machine);
359 int perf_event__synthesize_stat_round(struct perf_tool *tool,
360 				      u64 time, u64 type,
361 				      perf_event__handler_t process,
362 				      struct machine *machine);
363 int perf_event__synthesize_modules(struct perf_tool *tool,
364 				   perf_event__handler_t process,
365 				   struct machine *machine);
366 
367 int perf_event__process_comm(struct perf_tool *tool,
368 			     union perf_event *event,
369 			     struct perf_sample *sample,
370 			     struct machine *machine);
371 int perf_event__process_lost(struct perf_tool *tool,
372 			     union perf_event *event,
373 			     struct perf_sample *sample,
374 			     struct machine *machine);
375 int perf_event__process_lost_samples(struct perf_tool *tool,
376 				     union perf_event *event,
377 				     struct perf_sample *sample,
378 				     struct machine *machine);
379 int perf_event__process_aux(struct perf_tool *tool,
380 			    union perf_event *event,
381 			    struct perf_sample *sample,
382 			    struct machine *machine);
383 int perf_event__process_itrace_start(struct perf_tool *tool,
384 				     union perf_event *event,
385 				     struct perf_sample *sample,
386 				     struct machine *machine);
387 int perf_event__process_switch(struct perf_tool *tool,
388 			       union perf_event *event,
389 			       struct perf_sample *sample,
390 			       struct machine *machine);
391 int perf_event__process_namespaces(struct perf_tool *tool,
392 				   union perf_event *event,
393 				   struct perf_sample *sample,
394 				   struct machine *machine);
395 int perf_event__process_mmap(struct perf_tool *tool,
396 			     union perf_event *event,
397 			     struct perf_sample *sample,
398 			     struct machine *machine);
399 int perf_event__process_mmap2(struct perf_tool *tool,
400 			     union perf_event *event,
401 			     struct perf_sample *sample,
402 			     struct machine *machine);
403 int perf_event__process_fork(struct perf_tool *tool,
404 			     union perf_event *event,
405 			     struct perf_sample *sample,
406 			     struct machine *machine);
407 int perf_event__process_exit(struct perf_tool *tool,
408 			     union perf_event *event,
409 			     struct perf_sample *sample,
410 			     struct machine *machine);
411 int perf_event__process_ksymbol(struct perf_tool *tool,
412 				union perf_event *event,
413 				struct perf_sample *sample,
414 				struct machine *machine);
415 int perf_event__process_bpf(struct perf_tool *tool,
416 			    union perf_event *event,
417 			    struct perf_sample *sample,
418 			    struct machine *machine);
419 int perf_tool__process_synth_event(struct perf_tool *tool,
420 				   union perf_event *event,
421 				   struct machine *machine,
422 				   perf_event__handler_t process);
423 int perf_event__process(struct perf_tool *tool,
424 			union perf_event *event,
425 			struct perf_sample *sample,
426 			struct machine *machine);
427 
428 struct addr_location;
429 
430 int machine__resolve(struct machine *machine, struct addr_location *al,
431 		     struct perf_sample *sample);
432 
433 void addr_location__put(struct addr_location *al);
434 
435 struct thread;
436 
437 bool is_bts_event(struct perf_event_attr *attr);
438 bool sample_addr_correlates_sym(struct perf_event_attr *attr);
439 void thread__resolve(struct thread *thread, struct addr_location *al,
440 		     struct perf_sample *sample);
441 
442 const char *perf_event__name(unsigned int id);
443 
444 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
445 				     u64 read_format);
446 int perf_event__synthesize_sample(union perf_event *event, u64 type,
447 				  u64 read_format,
448 				  const struct perf_sample *sample);
449 
450 pid_t perf_event__synthesize_comm(struct perf_tool *tool,
451 				  union perf_event *event, pid_t pid,
452 				  perf_event__handler_t process,
453 				  struct machine *machine);
454 
455 int perf_event__synthesize_namespaces(struct perf_tool *tool,
456 				      union perf_event *event,
457 				      pid_t pid, pid_t tgid,
458 				      perf_event__handler_t process,
459 				      struct machine *machine);
460 
461 int perf_event__synthesize_mmap_events(struct perf_tool *tool,
462 				       union perf_event *event,
463 				       pid_t pid, pid_t tgid,
464 				       perf_event__handler_t process,
465 				       struct machine *machine,
466 				       bool mmap_data);
467 
468 int perf_event__synthesize_extra_kmaps(struct perf_tool *tool,
469 				       perf_event__handler_t process,
470 				       struct machine *machine);
471 
472 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp);
473 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp);
474 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp);
475 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp);
476 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp);
477 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp);
478 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp);
479 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp);
480 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp);
481 size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp);
482 size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp);
483 size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp);
484 size_t perf_event__fprintf(union perf_event *event, FILE *fp);
485 
486 int kallsyms__get_function_start(const char *kallsyms_filename,
487 				 const char *symbol_name, u64 *addr);
488 
489 void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max);
490 void  cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf_cpu_map *map,
491 			       u16 type, int max);
492 
493 void event_attr_init(struct perf_event_attr *attr);
494 
495 int perf_event_paranoid(void);
496 bool perf_event_paranoid_check(int max_level);
497 
498 extern int sysctl_perf_event_max_stack;
499 extern int sysctl_perf_event_max_contexts_per_stack;
500 extern unsigned int proc_map_timeout;
501 
502 #endif /* __PERF_RECORD_H */
503