xref: /linux/tools/lib/perf/include/perf/event.h (revision 1a646a28a0063f1fb31589f7190f2b4fb613e413)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LIBPERF_EVENT_H
3 #define __LIBPERF_EVENT_H
4 
5 #include <linux/perf_event.h>
6 #include <linux/types.h>
7 #include <linux/limits.h>
8 #include <linux/bpf.h>
9 #include <sys/types.h> /* pid_t */
10 
11 /*
12  * Verify the full field fits within the event, not just its start offset.
13  * Only valid for fixed-size scalar fields — for trailing arrays like
14  * filename[PATH_MAX], sizeof() evaluates to the declared maximum, not
15  * the actual string length, so this would spuriously return false.
16  */
17 #define event_contains(obj, mem) \
18 	((obj).header.size >= offsetof(typeof(obj), mem) + sizeof((obj).mem))
19 
20 struct perf_record_mmap {
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 
29 struct perf_record_mmap2 {
30 	struct perf_event_header header;
31 	__u32			 pid, tid;
32 	__u64			 start;
33 	__u64			 len;
34 	__u64			 pgoff;
35 	union {
36 		struct {
37 			__u32	 maj;
38 			__u32	 min;
39 			__u64	 ino;
40 			__u64	 ino_generation;
41 		};
42 		struct {
43 			__u8	 build_id_size;
44 			__u8	 __reserved_1;
45 			__u16	 __reserved_2;
46 			__u8	 build_id[20];
47 		};
48 	};
49 	__u32			 prot;
50 	__u32			 flags;
51 	char			 filename[PATH_MAX];
52 };
53 
54 struct perf_record_comm {
55 	struct perf_event_header header;
56 	__u32			 pid, tid;
57 	char			 comm[16];
58 };
59 
60 struct perf_record_namespaces {
61 	struct perf_event_header header;
62 	__u32			 pid, tid;
63 	__u64			 nr_namespaces;
64 	struct perf_ns_link_info link_info[];
65 };
66 
67 struct perf_record_fork {
68 	struct perf_event_header header;
69 	__u32			 pid, ppid;
70 	__u32			 tid, ptid;
71 	__u64			 time;
72 };
73 
74 struct perf_record_lost {
75 	struct perf_event_header header;
76 	__u64			 id;
77 	__u64			 lost;
78 };
79 
80 #define PERF_RECORD_MISC_LOST_SAMPLES_BPF (1 << 15)
81 
82 struct perf_record_lost_samples {
83 	struct perf_event_header header;
84 	__u64			 lost;
85 };
86 
87 #define MAX_ID_HDR_ENTRIES  6
88 struct perf_record_lost_samples_and_ids {
89 	struct perf_record_lost_samples lost;
90 	__u64 sample_ids[MAX_ID_HDR_ENTRIES];
91 };
92 
93 /*
94  * PERF_FORMAT_ENABLED | PERF_FORMAT_RUNNING | PERF_FORMAT_ID | PERF_FORMAT_LOST
95  */
96 struct perf_record_read {
97 	struct perf_event_header header;
98 	__u32			 pid, tid;
99 	__u64			 value;
100 	__u64			 time_enabled;
101 	__u64			 time_running;
102 	__u64			 id;
103 	__u64			 lost;
104 };
105 
106 struct perf_record_throttle {
107 	struct perf_event_header header;
108 	__u64			 time;
109 	__u64			 id;
110 	__u64			 stream_id;
111 };
112 
113 #ifndef KSYM_NAME_LEN
114 #define KSYM_NAME_LEN 512
115 #endif
116 
117 struct perf_record_ksymbol {
118 	struct perf_event_header header;
119 	__u64			 addr;
120 	__u32			 len;
121 	__u16			 ksym_type;
122 	__u16			 flags;
123 	char			 name[KSYM_NAME_LEN];
124 };
125 
126 struct perf_record_bpf_event {
127 	struct perf_event_header header;
128 	__u16			 type;
129 	__u16			 flags;
130 	__u32			 id;
131 
132 	/* for bpf_prog types */
133 	__u8			 tag[BPF_TAG_SIZE];  // prog tag
134 };
135 
136 struct perf_record_cgroup {
137 	struct perf_event_header header;
138 	__u64			 id;
139 	char			 path[PATH_MAX];
140 };
141 
142 struct perf_record_text_poke_event {
143 	struct perf_event_header header;
144 	__u64			addr;
145 	__u16			old_len;
146 	__u16			new_len;
147 	__u8			bytes[];
148 };
149 
150 struct perf_record_sample {
151 	struct perf_event_header header;
152 	__u64			 array[];
153 };
154 
155 struct perf_record_switch {
156 	struct perf_event_header header;
157 	__u32			 next_prev_pid;
158 	__u32			 next_prev_tid;
159 };
160 
161 struct perf_record_callchain_deferred {
162 	struct perf_event_header header;
163 	/*
164 	 * This is to match kernel and (deferred) user stacks together.
165 	 * The kernel part will be in the sample callchain array after
166 	 * the PERF_CONTEXT_USER_DEFERRED entry.
167 	 */
168 	__u64			 cookie;
169 	__u64			 nr;
170 	__u64			 ips[];
171 };
172 
173 struct perf_record_header_attr {
174 	struct perf_event_header header;
175 	struct perf_event_attr	 attr;
176 	/*
177 	 * Array of u64 id follows here but we cannot use a flexible array
178 	 * because size of attr in the data can be different then current
179 	 * version.  Please use perf_record_header_attr_id() below.
180 	 *
181 	 * __u64		 id[];  // do not use this
182 	 */
183 };
184 
185 /* Returns the pointer to id array based on the actual attr size. */
186 #define perf_record_header_attr_id(evt)			\
187 	((void *)&(evt)->attr.attr + (evt)->attr.attr.size)
188 
189 enum {
190 	PERF_CPU_MAP__CPUS = 0,
191 	PERF_CPU_MAP__MASK = 1,
192 	PERF_CPU_MAP__RANGE_CPUS = 2,
193 };
194 
195 /*
196  * Array encoding of a perf_cpu_map where nr is the number of entries in cpu[]
197  * and each entry is a value for a CPU in the map.
198  */
199 struct cpu_map_entries {
200 	__u16			 nr;
201 	__u16			 cpu[];
202 };
203 
204 /* Bitmap encoding of a perf_cpu_map where bitmap entries are 32-bit. */
205 struct perf_record_mask_cpu_map32 {
206 	/* Number of mask values. */
207 	__u16			 nr;
208 	/* Constant 4. */
209 	__u16			 long_size;
210 	/* Bitmap data. */
211 	__u32			 mask[];
212 };
213 
214 /* Bitmap encoding of a perf_cpu_map where bitmap entries are 64-bit. */
215 struct perf_record_mask_cpu_map64 {
216 	/* Number of mask values. */
217 	__u16			 nr;
218 	/* Constant 8. */
219 	__u16			 long_size;
220 	/* Legacy padding. */
221 	char                     __pad[4];
222 	/* Bitmap data. */
223 	__u64			 mask[];
224 };
225 
226 /*
227  * 'struct perf_record_cpu_map_data' is packed as unfortunately an earlier
228  * version had unaligned data and we wish to retain file format compatibility.
229  * -irogers
230  */
231 #pragma GCC diagnostic push
232 #pragma GCC diagnostic ignored "-Wpacked"
233 #pragma GCC diagnostic ignored "-Wattributes"
234 
235 /*
236  * An encoding of a CPU map for a range starting at start_cpu through to
237  * end_cpu. If any_cpu is 1, an any CPU (-1) value (aka dummy value) is present.
238  */
239 struct perf_record_range_cpu_map {
240 	__u8 any_cpu;
241 	__u8 __pad;
242 	__u16 start_cpu;
243 	__u16 end_cpu;
244 };
245 
246 struct perf_record_cpu_map_data {
247 	__u16			 type;
248 	union {
249 		/* Used when type == PERF_CPU_MAP__CPUS. */
250 		struct cpu_map_entries cpus_data;
251 		/* Used when type == PERF_CPU_MAP__MASK and long_size == 4. */
252 		struct perf_record_mask_cpu_map32 mask32_data;
253 		/* Used when type == PERF_CPU_MAP__MASK and long_size == 8. */
254 		struct perf_record_mask_cpu_map64 mask64_data;
255 		/* Used when type == PERF_CPU_MAP__RANGE_CPUS. */
256 		struct perf_record_range_cpu_map range_cpu_data;
257 	};
258 } __attribute__((packed));
259 
260 #pragma GCC diagnostic pop
261 
262 struct perf_record_cpu_map {
263 	struct perf_event_header	 header;
264 	struct perf_record_cpu_map_data	 data;
265 };
266 
267 enum {
268 	PERF_EVENT_UPDATE__UNIT  = 0,
269 	PERF_EVENT_UPDATE__SCALE = 1,
270 	PERF_EVENT_UPDATE__NAME  = 2,
271 	PERF_EVENT_UPDATE__CPUS  = 3,
272 };
273 
274 struct perf_record_event_update_cpus {
275 	struct perf_record_cpu_map_data	 cpus;
276 };
277 
278 struct perf_record_event_update_scale {
279 	double			 scale;
280 };
281 
282 struct perf_record_event_update {
283 	struct perf_event_header header;
284 	__u64			 type;
285 	__u64			 id;
286 	union {
287 		/* Used when type == PERF_EVENT_UPDATE__SCALE. */
288 		struct perf_record_event_update_scale scale;
289 		/* Used when type == PERF_EVENT_UPDATE__UNIT. */
290 		char unit[0];
291 		/* Used when type == PERF_EVENT_UPDATE__NAME. */
292 		char name[0];
293 		/* Used when type == PERF_EVENT_UPDATE__CPUS. */
294 		struct perf_record_event_update_cpus cpus;
295 	};
296 };
297 
298 #define MAX_EVENT_NAME 64
299 
300 struct perf_trace_event_type {
301 	__u64			 event_id;
302 	char			 name[MAX_EVENT_NAME];
303 };
304 
305 struct perf_record_header_event_type {
306 	struct perf_event_header	 header;
307 	struct perf_trace_event_type	 event_type;
308 };
309 
310 struct perf_record_header_tracing_data {
311 	struct perf_event_header header;
312 	__u32			 size;
313 	__u32			 pad;
314 };
315 
316 #define PERF_RECORD_MISC_BUILD_ID_SIZE (1 << 15)
317 
318 struct perf_record_header_build_id {
319 	struct perf_event_header header;
320 	pid_t			 pid;
321 	union {
322 		__u8		 build_id[24];
323 		struct {
324 			__u8	 data[20];
325 			__u8	 size;
326 			__u8	 reserved1__;
327 			__u16	 reserved2__;
328 		};
329 	};
330 	char			 filename[];
331 };
332 
333 struct id_index_entry {
334 	__u64			 id;
335 	__u64			 idx;
336 	__u64			 cpu;
337 	__u64			 tid;
338 };
339 
340 struct id_index_entry_2 {
341 	__u64			 machine_pid;
342 	__u64			 vcpu;
343 };
344 
345 struct perf_record_id_index {
346 	struct perf_event_header header;
347 	__u64			 nr;
348 	struct id_index_entry	 entries[];
349 };
350 
351 struct perf_record_auxtrace_info {
352 	struct perf_event_header header;
353 	__u32			 type;
354 	__u32			 reserved__; /* For alignment */
355 	__u64			 priv[];
356 };
357 
358 struct perf_record_auxtrace {
359 	struct perf_event_header header;
360 	__u64			 size;
361 	__u64			 offset;
362 	__u64			 reference;
363 	__u32			 idx;
364 	__u32			 tid;
365 	__u32			 cpu;
366 	__u32			 reserved__; /* For alignment */
367 };
368 
369 #define MAX_AUXTRACE_ERROR_MSG 64
370 
371 struct perf_record_auxtrace_error {
372 	struct perf_event_header header;
373 	__u32			 type;
374 	__u32			 code;
375 	__u32			 cpu;
376 	__u32			 pid;
377 	__u32			 tid;
378 	__u32			 fmt;
379 	__u64			 ip;
380 	__u64			 time;
381 	char			 msg[MAX_AUXTRACE_ERROR_MSG];
382 	__u32			 machine_pid;
383 	__u32			 vcpu;
384 };
385 
386 struct perf_record_aux {
387 	struct perf_event_header header;
388 	__u64			 aux_offset;
389 	__u64			 aux_size;
390 	__u64			 flags;
391 };
392 
393 struct perf_record_itrace_start {
394 	struct perf_event_header header;
395 	__u32			 pid;
396 	__u32			 tid;
397 };
398 
399 struct perf_record_aux_output_hw_id {
400 	struct perf_event_header header;
401 	__u64			hw_id;
402 };
403 
404 struct perf_record_thread_map_entry {
405 	__u64			 pid;
406 	char			 comm[16];
407 };
408 
409 struct perf_record_thread_map {
410 	struct perf_event_header		 header;
411 	__u64					 nr;
412 	struct perf_record_thread_map_entry	 entries[];
413 };
414 
415 enum {
416 	PERF_STAT_CONFIG_TERM__AGGR_MODE	= 0,
417 	PERF_STAT_CONFIG_TERM__INTERVAL		= 1,
418 	PERF_STAT_CONFIG_TERM__SCALE		= 2,
419 	PERF_STAT_CONFIG_TERM__AGGR_LEVEL	= 3,
420 	PERF_STAT_CONFIG_TERM__MAX		= 4,
421 };
422 
423 struct perf_record_stat_config_entry {
424 	__u64			 tag;
425 	__u64			 val;
426 };
427 
428 struct perf_record_stat_config {
429 	struct perf_event_header		 header;
430 	__u64					 nr;
431 	struct perf_record_stat_config_entry	 data[];
432 };
433 
434 struct perf_record_stat {
435 	struct perf_event_header header;
436 
437 	__u64			 id;
438 	__u32			 cpu;
439 	__u32			 thread;
440 
441 	union {
442 		struct {
443 			__u64	 val;
444 			__u64	 ena;
445 			__u64	 run;
446 		};
447 		__u64		 values[3];
448 	};
449 };
450 
451 struct perf_record_stat_round {
452 	struct perf_event_header header;
453 	__u64			 type;
454 	__u64			 time;
455 };
456 
457 struct perf_record_time_conv {
458 	struct perf_event_header header;
459 	__u64			 time_shift;
460 	__u64			 time_mult;
461 	__u64			 time_zero;
462 	__u64			 time_cycles;
463 	__u64			 time_mask;
464 	__u8			 cap_user_time_zero;
465 	__u8			 cap_user_time_short;
466 	__u8			 reserved[6];	/* For alignment */
467 };
468 
469 struct perf_record_header_feature {
470 	struct perf_event_header header;
471 	__u64			 feat_id;
472 	char			 data[];
473 };
474 
475 struct perf_record_compressed {
476 	struct perf_event_header header;
477 	char			 data[];
478 };
479 
480 /*
481  * `header.size` includes the padding we are going to add while writing the record.
482  * `data_size` only includes the size of `data[]` itself.
483  */
484 struct perf_record_compressed2 {
485 	struct perf_event_header header;
486 	__u64			 data_size;
487 	char			 data[];
488 };
489 
490 #define BPF_METADATA_KEY_LEN   64
491 #define BPF_METADATA_VALUE_LEN 256
492 #define BPF_PROG_NAME_LEN      KSYM_NAME_LEN
493 
494 struct perf_record_bpf_metadata_entry {
495 	char key[BPF_METADATA_KEY_LEN];
496 	char value[BPF_METADATA_VALUE_LEN];
497 };
498 
499 struct perf_record_bpf_metadata {
500 	struct perf_event_header	      header;
501 	char				      prog_name[BPF_PROG_NAME_LEN];
502 	__u64				      nr_entries;
503 	struct perf_record_bpf_metadata_entry entries[];
504 };
505 
506 struct perf_record_schedstat_cpu_v15 {
507 #define CPU_FIELD(_type, _name, _desc, _format, _is_pct, _pct_of, _ver)		_type _name
508 #include "schedstat-v15.h"
509 #undef CPU_FIELD
510 };
511 
512 struct perf_record_schedstat_cpu_v16 {
513 #define CPU_FIELD(_type, _name, _desc, _format, _is_pct, _pct_of, _ver)		_type _name
514 #include "schedstat-v16.h"
515 #undef CPU_FIELD
516 };
517 
518 struct perf_record_schedstat_cpu_v17 {
519 #define CPU_FIELD(_type, _name, _desc, _format, _is_pct, _pct_of, _ver)		_type _name
520 #include "schedstat-v17.h"
521 #undef CPU_FIELD
522 };
523 
524 struct perf_record_schedstat_cpu {
525 	struct perf_event_header header;
526 	__u64			 timestamp;
527 	__u32			 cpu;
528 	__u16			 version;
529 	/* Padding */
530 	char			 __pad[2];
531 	union {
532 		struct perf_record_schedstat_cpu_v15 v15;
533 		struct perf_record_schedstat_cpu_v16 v16;
534 		struct perf_record_schedstat_cpu_v17 v17;
535 	};
536 };
537 
538 struct perf_record_schedstat_domain_v15 {
539 #define DOMAIN_FIELD(_type, _name, _desc, _format, _is_jiffies, _ver)		_type _name
540 #include "schedstat-v15.h"
541 #undef DOMAIN_FIELD
542 };
543 
544 struct perf_record_schedstat_domain_v16 {
545 #define DOMAIN_FIELD(_type, _name, _desc, _format, _is_jiffies, _ver)		_type _name
546 #include "schedstat-v16.h"
547 #undef DOMAIN_FIELD
548 };
549 
550 struct perf_record_schedstat_domain_v17 {
551 #define DOMAIN_FIELD(_type, _name, _desc, _format, _is_jiffies, _ver)		_type _name
552 #include "schedstat-v17.h"
553 #undef DOMAIN_FIELD
554 };
555 
556 #define DOMAIN_NAME_LEN		16
557 
558 struct perf_record_schedstat_domain {
559 	struct perf_event_header header;
560 	__u64			 timestamp;
561 	__u32			 cpu;
562 	__u16			 version;
563 	__u16			 domain;
564 	union {
565 		struct perf_record_schedstat_domain_v15 v15;
566 		struct perf_record_schedstat_domain_v16 v16;
567 		struct perf_record_schedstat_domain_v17 v17;
568 	};
569 };
570 
571 enum perf_user_event_type { /* above any possible kernel type */
572 	PERF_RECORD_USER_TYPE_START		= 64,
573 	PERF_RECORD_HEADER_ATTR			= 64,
574 	PERF_RECORD_HEADER_EVENT_TYPE		= 65, /* deprecated */
575 	PERF_RECORD_HEADER_TRACING_DATA		= 66,
576 	PERF_RECORD_HEADER_BUILD_ID		= 67,
577 	PERF_RECORD_FINISHED_ROUND		= 68,
578 	PERF_RECORD_ID_INDEX			= 69,
579 	PERF_RECORD_AUXTRACE_INFO		= 70,
580 	PERF_RECORD_AUXTRACE			= 71,
581 	PERF_RECORD_AUXTRACE_ERROR		= 72,
582 	PERF_RECORD_THREAD_MAP			= 73,
583 	PERF_RECORD_CPU_MAP			= 74,
584 	PERF_RECORD_STAT_CONFIG			= 75,
585 	PERF_RECORD_STAT			= 76,
586 	PERF_RECORD_STAT_ROUND			= 77,
587 	PERF_RECORD_EVENT_UPDATE		= 78,
588 	PERF_RECORD_TIME_CONV			= 79,
589 	PERF_RECORD_HEADER_FEATURE		= 80,
590 	PERF_RECORD_COMPRESSED			= 81,
591 	PERF_RECORD_FINISHED_INIT		= 82,
592 	PERF_RECORD_COMPRESSED2			= 83,
593 	PERF_RECORD_BPF_METADATA		= 84,
594 	PERF_RECORD_SCHEDSTAT_CPU		= 85,
595 	PERF_RECORD_SCHEDSTAT_DOMAIN		= 86,
596 	PERF_RECORD_HEADER_MAX
597 };
598 
599 union perf_event {
600 	struct perf_event_header		header;
601 	struct perf_record_mmap			mmap;
602 	struct perf_record_mmap2		mmap2;
603 	struct perf_record_comm			comm;
604 	struct perf_record_namespaces		namespaces;
605 	struct perf_record_cgroup		cgroup;
606 	struct perf_record_fork			fork;
607 	struct perf_record_lost			lost;
608 	struct perf_record_lost_samples		lost_samples;
609 	struct perf_record_read			read;
610 	struct perf_record_throttle		throttle;
611 	struct perf_record_sample		sample;
612 	struct perf_record_callchain_deferred	callchain_deferred;
613 	struct perf_record_bpf_event		bpf;
614 	struct perf_record_ksymbol		ksymbol;
615 	struct perf_record_text_poke_event	text_poke;
616 	struct perf_record_header_attr		attr;
617 	struct perf_record_event_update		event_update;
618 	struct perf_record_header_event_type	event_type;
619 	struct perf_record_header_tracing_data	tracing_data;
620 	struct perf_record_header_build_id	build_id;
621 	struct perf_record_id_index		id_index;
622 	struct perf_record_auxtrace_info	auxtrace_info;
623 	struct perf_record_auxtrace		auxtrace;
624 	struct perf_record_auxtrace_error	auxtrace_error;
625 	struct perf_record_aux			aux;
626 	struct perf_record_itrace_start		itrace_start;
627 	struct perf_record_aux_output_hw_id	aux_output_hw_id;
628 	struct perf_record_switch		context_switch;
629 	struct perf_record_thread_map		thread_map;
630 	struct perf_record_cpu_map		cpu_map;
631 	struct perf_record_stat_config		stat_config;
632 	struct perf_record_stat			stat;
633 	struct perf_record_stat_round		stat_round;
634 	struct perf_record_time_conv		time_conv;
635 	struct perf_record_header_feature	feat;
636 	struct perf_record_compressed		pack;
637 	struct perf_record_compressed2		pack2;
638 	struct perf_record_bpf_metadata		bpf_metadata;
639 	struct perf_record_schedstat_cpu	schedstat_cpu;
640 	struct perf_record_schedstat_domain	schedstat_domain;
641 };
642 
643 #endif /* __LIBPERF_EVENT_H */
644