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