xref: /linux/tools/lib/perf/include/perf/event.h (revision 2a6b6c9a226279b4f6668450ddb21ae655558087)
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 #define event_contains(obj, mem) ((obj).header.size > offsetof(typeof(obj), mem))
12 
13 struct perf_record_mmap {
14 	struct perf_event_header header;
15 	__u32			 pid, tid;
16 	__u64			 start;
17 	__u64			 len;
18 	__u64			 pgoff;
19 	char			 filename[PATH_MAX];
20 };
21 
22 struct perf_record_mmap2 {
23 	struct perf_event_header header;
24 	__u32			 pid, tid;
25 	__u64			 start;
26 	__u64			 len;
27 	__u64			 pgoff;
28 	union {
29 		struct {
30 			__u32	 maj;
31 			__u32	 min;
32 			__u64	 ino;
33 			__u64	 ino_generation;
34 		};
35 		struct {
36 			__u8	 build_id_size;
37 			__u8	 __reserved_1;
38 			__u16	 __reserved_2;
39 			__u8	 build_id[20];
40 		};
41 	};
42 	__u32			 prot;
43 	__u32			 flags;
44 	char			 filename[PATH_MAX];
45 };
46 
47 struct perf_record_comm {
48 	struct perf_event_header header;
49 	__u32			 pid, tid;
50 	char			 comm[16];
51 };
52 
53 struct perf_record_namespaces {
54 	struct perf_event_header header;
55 	__u32			 pid, tid;
56 	__u64			 nr_namespaces;
57 	struct perf_ns_link_info link_info[];
58 };
59 
60 struct perf_record_fork {
61 	struct perf_event_header header;
62 	__u32			 pid, ppid;
63 	__u32			 tid, ptid;
64 	__u64			 time;
65 };
66 
67 struct perf_record_lost {
68 	struct perf_event_header header;
69 	__u64			 id;
70 	__u64			 lost;
71 };
72 
73 #define PERF_RECORD_MISC_LOST_SAMPLES_BPF (1 << 15)
74 
75 struct perf_record_lost_samples {
76 	struct perf_event_header header;
77 	__u64			 lost;
78 };
79 
80 #define MAX_ID_HDR_ENTRIES  6
81 struct perf_record_lost_samples_and_ids {
82 	struct perf_record_lost_samples lost;
83 	__u64 sample_ids[MAX_ID_HDR_ENTRIES];
84 };
85 
86 /*
87  * PERF_FORMAT_ENABLED | PERF_FORMAT_RUNNING | PERF_FORMAT_ID | PERF_FORMAT_LOST
88  */
89 struct perf_record_read {
90 	struct perf_event_header header;
91 	__u32			 pid, tid;
92 	__u64			 value;
93 	__u64			 time_enabled;
94 	__u64			 time_running;
95 	__u64			 id;
96 	__u64			 lost;
97 };
98 
99 struct perf_record_throttle {
100 	struct perf_event_header header;
101 	__u64			 time;
102 	__u64			 id;
103 	__u64			 stream_id;
104 };
105 
106 #ifndef KSYM_NAME_LEN
107 #define KSYM_NAME_LEN 512
108 #endif
109 
110 struct perf_record_ksymbol {
111 	struct perf_event_header header;
112 	__u64			 addr;
113 	__u32			 len;
114 	__u16			 ksym_type;
115 	__u16			 flags;
116 	char			 name[KSYM_NAME_LEN];
117 };
118 
119 struct perf_record_bpf_event {
120 	struct perf_event_header header;
121 	__u16			 type;
122 	__u16			 flags;
123 	__u32			 id;
124 
125 	/* for bpf_prog types */
126 	__u8			 tag[BPF_TAG_SIZE];  // prog tag
127 };
128 
129 struct perf_record_cgroup {
130 	struct perf_event_header header;
131 	__u64			 id;
132 	char			 path[PATH_MAX];
133 };
134 
135 struct perf_record_text_poke_event {
136 	struct perf_event_header header;
137 	__u64			addr;
138 	__u16			old_len;
139 	__u16			new_len;
140 	__u8			bytes[];
141 };
142 
143 struct perf_record_sample {
144 	struct perf_event_header header;
145 	__u64			 array[];
146 };
147 
148 struct perf_record_switch {
149 	struct perf_event_header header;
150 	__u32			 next_prev_pid;
151 	__u32			 next_prev_tid;
152 };
153 
154 struct perf_record_header_attr {
155 	struct perf_event_header header;
156 	struct perf_event_attr	 attr;
157 	/*
158 	 * Array of u64 id follows here but we cannot use a flexible array
159 	 * because size of attr in the data can be different then current
160 	 * version.  Please use perf_record_header_attr_id() below.
161 	 *
162 	 * __u64		 id[];  // do not use this
163 	 */
164 };
165 
166 /* Returns the pointer to id array based on the actual attr size. */
167 #define perf_record_header_attr_id(evt)			\
168 	((void *)&(evt)->attr.attr + (evt)->attr.attr.size)
169 
170 enum {
171 	PERF_CPU_MAP__CPUS = 0,
172 	PERF_CPU_MAP__MASK = 1,
173 	PERF_CPU_MAP__RANGE_CPUS = 2,
174 };
175 
176 /*
177  * Array encoding of a perf_cpu_map where nr is the number of entries in cpu[]
178  * and each entry is a value for a CPU in the map.
179  */
180 struct cpu_map_entries {
181 	__u16			 nr;
182 	__u16			 cpu[];
183 };
184 
185 /* Bitmap encoding of a perf_cpu_map where bitmap entries are 32-bit. */
186 struct perf_record_mask_cpu_map32 {
187 	/* Number of mask values. */
188 	__u16			 nr;
189 	/* Constant 4. */
190 	__u16			 long_size;
191 	/* Bitmap data. */
192 	__u32			 mask[];
193 };
194 
195 /* Bitmap encoding of a perf_cpu_map where bitmap entries are 64-bit. */
196 struct perf_record_mask_cpu_map64 {
197 	/* Number of mask values. */
198 	__u16			 nr;
199 	/* Constant 8. */
200 	__u16			 long_size;
201 	/* Legacy padding. */
202 	char                     __pad[4];
203 	/* Bitmap data. */
204 	__u64			 mask[];
205 };
206 
207 /*
208  * 'struct perf_record_cpu_map_data' is packed as unfortunately an earlier
209  * version had unaligned data and we wish to retain file format compatibility.
210  * -irogers
211  */
212 #pragma GCC diagnostic push
213 #pragma GCC diagnostic ignored "-Wpacked"
214 #pragma GCC diagnostic ignored "-Wattributes"
215 
216 /*
217  * An encoding of a CPU map for a range starting at start_cpu through to
218  * end_cpu. If any_cpu is 1, an any CPU (-1) value (aka dummy value) is present.
219  */
220 struct perf_record_range_cpu_map {
221 	__u8 any_cpu;
222 	__u8 __pad;
223 	__u16 start_cpu;
224 	__u16 end_cpu;
225 };
226 
227 struct perf_record_cpu_map_data {
228 	__u16			 type;
229 	union {
230 		/* Used when type == PERF_CPU_MAP__CPUS. */
231 		struct cpu_map_entries cpus_data;
232 		/* Used when type == PERF_CPU_MAP__MASK and long_size == 4. */
233 		struct perf_record_mask_cpu_map32 mask32_data;
234 		/* Used when type == PERF_CPU_MAP__MASK and long_size == 8. */
235 		struct perf_record_mask_cpu_map64 mask64_data;
236 		/* Used when type == PERF_CPU_MAP__RANGE_CPUS. */
237 		struct perf_record_range_cpu_map range_cpu_data;
238 	};
239 } __attribute__((packed));
240 
241 #pragma GCC diagnostic pop
242 
243 struct perf_record_cpu_map {
244 	struct perf_event_header	 header;
245 	struct perf_record_cpu_map_data	 data;
246 };
247 
248 enum {
249 	PERF_EVENT_UPDATE__UNIT  = 0,
250 	PERF_EVENT_UPDATE__SCALE = 1,
251 	PERF_EVENT_UPDATE__NAME  = 2,
252 	PERF_EVENT_UPDATE__CPUS  = 3,
253 };
254 
255 struct perf_record_event_update_cpus {
256 	struct perf_record_cpu_map_data	 cpus;
257 };
258 
259 struct perf_record_event_update_scale {
260 	double			 scale;
261 };
262 
263 struct perf_record_event_update {
264 	struct perf_event_header header;
265 	__u64			 type;
266 	__u64			 id;
267 	union {
268 		/* Used when type == PERF_EVENT_UPDATE__SCALE. */
269 		struct perf_record_event_update_scale scale;
270 		/* Used when type == PERF_EVENT_UPDATE__UNIT. */
271 		char unit[0];
272 		/* Used when type == PERF_EVENT_UPDATE__NAME. */
273 		char name[0];
274 		/* Used when type == PERF_EVENT_UPDATE__CPUS. */
275 		struct perf_record_event_update_cpus cpus;
276 	};
277 };
278 
279 #define MAX_EVENT_NAME 64
280 
281 struct perf_trace_event_type {
282 	__u64			 event_id;
283 	char			 name[MAX_EVENT_NAME];
284 };
285 
286 struct perf_record_header_event_type {
287 	struct perf_event_header	 header;
288 	struct perf_trace_event_type	 event_type;
289 };
290 
291 struct perf_record_header_tracing_data {
292 	struct perf_event_header header;
293 	__u32			 size;
294 };
295 
296 #define PERF_RECORD_MISC_BUILD_ID_SIZE (1 << 15)
297 
298 struct perf_record_header_build_id {
299 	struct perf_event_header header;
300 	pid_t			 pid;
301 	union {
302 		__u8		 build_id[24];
303 		struct {
304 			__u8	 data[20];
305 			__u8	 size;
306 			__u8	 reserved1__;
307 			__u16	 reserved2__;
308 		};
309 	};
310 	char			 filename[];
311 };
312 
313 struct id_index_entry {
314 	__u64			 id;
315 	__u64			 idx;
316 	__u64			 cpu;
317 	__u64			 tid;
318 };
319 
320 struct id_index_entry_2 {
321 	__u64			 machine_pid;
322 	__u64			 vcpu;
323 };
324 
325 struct perf_record_id_index {
326 	struct perf_event_header header;
327 	__u64			 nr;
328 	struct id_index_entry	 entries[];
329 };
330 
331 struct perf_record_auxtrace_info {
332 	struct perf_event_header header;
333 	__u32			 type;
334 	__u32			 reserved__; /* For alignment */
335 	__u64			 priv[];
336 };
337 
338 struct perf_record_auxtrace {
339 	struct perf_event_header header;
340 	__u64			 size;
341 	__u64			 offset;
342 	__u64			 reference;
343 	__u32			 idx;
344 	__u32			 tid;
345 	__u32			 cpu;
346 	__u32			 reserved__; /* For alignment */
347 };
348 
349 #define MAX_AUXTRACE_ERROR_MSG 64
350 
351 struct perf_record_auxtrace_error {
352 	struct perf_event_header header;
353 	__u32			 type;
354 	__u32			 code;
355 	__u32			 cpu;
356 	__u32			 pid;
357 	__u32			 tid;
358 	__u32			 fmt;
359 	__u64			 ip;
360 	__u64			 time;
361 	char			 msg[MAX_AUXTRACE_ERROR_MSG];
362 	__u32			 machine_pid;
363 	__u32			 vcpu;
364 };
365 
366 struct perf_record_aux {
367 	struct perf_event_header header;
368 	__u64			 aux_offset;
369 	__u64			 aux_size;
370 	__u64			 flags;
371 };
372 
373 struct perf_record_itrace_start {
374 	struct perf_event_header header;
375 	__u32			 pid;
376 	__u32			 tid;
377 };
378 
379 struct perf_record_aux_output_hw_id {
380 	struct perf_event_header header;
381 	__u64			hw_id;
382 };
383 
384 struct perf_record_thread_map_entry {
385 	__u64			 pid;
386 	char			 comm[16];
387 };
388 
389 struct perf_record_thread_map {
390 	struct perf_event_header		 header;
391 	__u64					 nr;
392 	struct perf_record_thread_map_entry	 entries[];
393 };
394 
395 enum {
396 	PERF_STAT_CONFIG_TERM__AGGR_MODE	= 0,
397 	PERF_STAT_CONFIG_TERM__INTERVAL		= 1,
398 	PERF_STAT_CONFIG_TERM__SCALE		= 2,
399 	PERF_STAT_CONFIG_TERM__AGGR_LEVEL	= 3,
400 	PERF_STAT_CONFIG_TERM__MAX		= 4,
401 };
402 
403 struct perf_record_stat_config_entry {
404 	__u64			 tag;
405 	__u64			 val;
406 };
407 
408 struct perf_record_stat_config {
409 	struct perf_event_header		 header;
410 	__u64					 nr;
411 	struct perf_record_stat_config_entry	 data[];
412 };
413 
414 struct perf_record_stat {
415 	struct perf_event_header header;
416 
417 	__u64			 id;
418 	__u32			 cpu;
419 	__u32			 thread;
420 
421 	union {
422 		struct {
423 			__u64	 val;
424 			__u64	 ena;
425 			__u64	 run;
426 		};
427 		__u64		 values[3];
428 	};
429 };
430 
431 struct perf_record_stat_round {
432 	struct perf_event_header header;
433 	__u64			 type;
434 	__u64			 time;
435 };
436 
437 struct perf_record_time_conv {
438 	struct perf_event_header header;
439 	__u64			 time_shift;
440 	__u64			 time_mult;
441 	__u64			 time_zero;
442 	__u64			 time_cycles;
443 	__u64			 time_mask;
444 	__u8			 cap_user_time_zero;
445 	__u8			 cap_user_time_short;
446 	__u8			 reserved[6];	/* For alignment */
447 };
448 
449 struct perf_record_header_feature {
450 	struct perf_event_header header;
451 	__u64			 feat_id;
452 	char			 data[];
453 };
454 
455 struct perf_record_compressed {
456 	struct perf_event_header header;
457 	char			 data[];
458 };
459 
460 enum perf_user_event_type { /* above any possible kernel type */
461 	PERF_RECORD_USER_TYPE_START		= 64,
462 	PERF_RECORD_HEADER_ATTR			= 64,
463 	PERF_RECORD_HEADER_EVENT_TYPE		= 65, /* deprecated */
464 	PERF_RECORD_HEADER_TRACING_DATA		= 66,
465 	PERF_RECORD_HEADER_BUILD_ID		= 67,
466 	PERF_RECORD_FINISHED_ROUND		= 68,
467 	PERF_RECORD_ID_INDEX			= 69,
468 	PERF_RECORD_AUXTRACE_INFO		= 70,
469 	PERF_RECORD_AUXTRACE			= 71,
470 	PERF_RECORD_AUXTRACE_ERROR		= 72,
471 	PERF_RECORD_THREAD_MAP			= 73,
472 	PERF_RECORD_CPU_MAP			= 74,
473 	PERF_RECORD_STAT_CONFIG			= 75,
474 	PERF_RECORD_STAT			= 76,
475 	PERF_RECORD_STAT_ROUND			= 77,
476 	PERF_RECORD_EVENT_UPDATE		= 78,
477 	PERF_RECORD_TIME_CONV			= 79,
478 	PERF_RECORD_HEADER_FEATURE		= 80,
479 	PERF_RECORD_COMPRESSED			= 81,
480 	PERF_RECORD_FINISHED_INIT		= 82,
481 	PERF_RECORD_HEADER_MAX
482 };
483 
484 union perf_event {
485 	struct perf_event_header		header;
486 	struct perf_record_mmap			mmap;
487 	struct perf_record_mmap2		mmap2;
488 	struct perf_record_comm			comm;
489 	struct perf_record_namespaces		namespaces;
490 	struct perf_record_cgroup		cgroup;
491 	struct perf_record_fork			fork;
492 	struct perf_record_lost			lost;
493 	struct perf_record_lost_samples		lost_samples;
494 	struct perf_record_read			read;
495 	struct perf_record_throttle		throttle;
496 	struct perf_record_sample		sample;
497 	struct perf_record_bpf_event		bpf;
498 	struct perf_record_ksymbol		ksymbol;
499 	struct perf_record_text_poke_event	text_poke;
500 	struct perf_record_header_attr		attr;
501 	struct perf_record_event_update		event_update;
502 	struct perf_record_header_event_type	event_type;
503 	struct perf_record_header_tracing_data	tracing_data;
504 	struct perf_record_header_build_id	build_id;
505 	struct perf_record_id_index		id_index;
506 	struct perf_record_auxtrace_info	auxtrace_info;
507 	struct perf_record_auxtrace		auxtrace;
508 	struct perf_record_auxtrace_error	auxtrace_error;
509 	struct perf_record_aux			aux;
510 	struct perf_record_itrace_start		itrace_start;
511 	struct perf_record_aux_output_hw_id	aux_output_hw_id;
512 	struct perf_record_switch		context_switch;
513 	struct perf_record_thread_map		thread_map;
514 	struct perf_record_cpu_map		cpu_map;
515 	struct perf_record_stat_config		stat_config;
516 	struct perf_record_stat			stat;
517 	struct perf_record_stat_round		stat_round;
518 	struct perf_record_time_conv		time_conv;
519 	struct perf_record_header_feature	feat;
520 	struct perf_record_compressed		pack;
521 };
522 
523 #endif /* __LIBPERF_EVENT_H */
524