1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_RECORD_H
3 #define __PERF_RECORD_H
7 #include <linux/kernel.h>
9 #include <linux/perf_event.h>
10 #include <perf/event.h>
14 #include "perf_regs.h"
18 * /usr/include/inttypes.h uses just 'lu' for PRIu64, but we end up defining
19 * __u64 as long long unsigned int, and then -Werror=format= kicks in and
20 * complains of the mismatched types, so use these two special extra PRI
21 * macros to overcome that.
23 #define PRI_lu64 "l" PRIu64
24 #define PRI_lx64 "l" PRIx64
25 #define PRI_ld64 "l" PRId64
27 #define PRI_lu64 PRIu64
28 #define PRI_lx64 PRIx64
29 #define PRI_ld64 PRId64
32 #define PERF_SAMPLE_MASK \
33 (PERF_SAMPLE_IP | PERF_SAMPLE_TID | \
34 PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR | \
35 PERF_SAMPLE_ID | PERF_SAMPLE_STREAM_ID | \
36 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD | \
37 PERF_SAMPLE_IDENTIFIER)
39 /* perf sample has 16 bits size limit */
40 #define PERF_SAMPLE_MAX_SIZE (1 << 16)
47 /* Cached values/mask filled by first register access. */
48 u64 cache_regs[PERF_REGS_MAX];
58 struct sample_read_value {
69 struct sample_read_value *values;
71 struct sample_read_value one;
83 PERF_IP_FLAG_BRANCH = 1ULL << 0,
84 PERF_IP_FLAG_CALL = 1ULL << 1,
85 PERF_IP_FLAG_RETURN = 1ULL << 2,
86 PERF_IP_FLAG_CONDITIONAL = 1ULL << 3,
87 PERF_IP_FLAG_SYSCALLRET = 1ULL << 4,
88 PERF_IP_FLAG_ASYNC = 1ULL << 5,
89 PERF_IP_FLAG_INTERRUPT = 1ULL << 6,
90 PERF_IP_FLAG_TX_ABORT = 1ULL << 7,
91 PERF_IP_FLAG_TRACE_BEGIN = 1ULL << 8,
92 PERF_IP_FLAG_TRACE_END = 1ULL << 9,
93 PERF_IP_FLAG_IN_TX = 1ULL << 10,
96 #define PERF_IP_FLAG_CHARS "bcrosyiABEx"
98 #define PERF_BRANCH_MASK (\
99 PERF_IP_FLAG_BRANCH |\
101 PERF_IP_FLAG_RETURN |\
102 PERF_IP_FLAG_CONDITIONAL |\
103 PERF_IP_FLAG_SYSCALLRET |\
104 PERF_IP_FLAG_ASYNC |\
105 PERF_IP_FLAG_INTERRUPT |\
106 PERF_IP_FLAG_TX_ABORT |\
107 PERF_IP_FLAG_TRACE_BEGIN |\
108 PERF_IP_FLAG_TRACE_END)
134 struct ip_callchain *callchain;
135 struct branch_stack *branch_stack;
136 struct regs_dump user_regs;
137 struct regs_dump intr_regs;
138 struct stack_dump user_stack;
139 struct sample_read read;
142 #define PERF_MEM_DATA_SRC_NONE \
143 (PERF_MEM_S(OP, NA) |\
144 PERF_MEM_S(LVL, NA) |\
145 PERF_MEM_S(SNOOP, NA) |\
146 PERF_MEM_S(LOCK, NA) |\
149 enum perf_user_event_type { /* above any possible kernel type */
150 PERF_RECORD_USER_TYPE_START = 64,
151 PERF_RECORD_HEADER_ATTR = 64,
152 PERF_RECORD_HEADER_EVENT_TYPE = 65, /* deprecated */
153 PERF_RECORD_HEADER_TRACING_DATA = 66,
154 PERF_RECORD_HEADER_BUILD_ID = 67,
155 PERF_RECORD_FINISHED_ROUND = 68,
156 PERF_RECORD_ID_INDEX = 69,
157 PERF_RECORD_AUXTRACE_INFO = 70,
158 PERF_RECORD_AUXTRACE = 71,
159 PERF_RECORD_AUXTRACE_ERROR = 72,
160 PERF_RECORD_THREAD_MAP = 73,
161 PERF_RECORD_CPU_MAP = 74,
162 PERF_RECORD_STAT_CONFIG = 75,
163 PERF_RECORD_STAT = 76,
164 PERF_RECORD_STAT_ROUND = 77,
165 PERF_RECORD_EVENT_UPDATE = 78,
166 PERF_RECORD_TIME_CONV = 79,
167 PERF_RECORD_HEADER_FEATURE = 80,
168 PERF_RECORD_COMPRESSED = 81,
169 PERF_RECORD_HEADER_MAX
172 enum auxtrace_error_type {
173 PERF_AUXTRACE_ERROR_ITRACE = 1,
174 PERF_AUXTRACE_ERROR_MAX
177 /* Attribute type for custom synthesized events */
178 #define PERF_TYPE_SYNTH (INT_MAX + 1U)
180 /* Attribute config for custom synthesized events */
182 PERF_SYNTH_INTEL_PTWRITE,
183 PERF_SYNTH_INTEL_MWAIT,
184 PERF_SYNTH_INTEL_PWRE,
185 PERF_SYNTH_INTEL_EXSTOP,
186 PERF_SYNTH_INTEL_PWRX,
187 PERF_SYNTH_INTEL_CBR,
191 * Raw data formats for synthesized events. Note that 4 bytes of padding are
192 * present to match the 'size' member of PERF_SAMPLE_RAW data which is always
193 * 8-byte aligned. That means we must dereference raw_data with an offset of 4.
194 * Refer perf_sample__synth_ptr() and perf_synth__raw_data(). It also means the
195 * structure sizes are 4 bytes bigger than the raw_size, refer
196 * perf_synth__raw_size().
199 struct perf_synth_intel_ptwrite {
211 struct perf_synth_intel_mwait {
225 struct perf_synth_intel_pwre {
240 struct perf_synth_intel_exstop {
251 struct perf_synth_intel_pwrx {
256 u64 deepest_cstate : 4,
265 struct perf_synth_intel_cbr {
281 * raw_data is always 4 bytes from an 8-byte boundary, so subtract 4 to get
284 static inline void *perf_sample__synth_ptr(struct perf_sample *sample)
286 return sample->raw_data - 4;
289 static inline void *perf_synth__raw_data(void *p)
294 #define perf_synth__raw_size(d) (sizeof(d) - 4)
296 #define perf_sample__bad_synth_size(s, d) ((s)->raw_size < sizeof(d) - 4)
299 * The kernel collects the number of events it couldn't send in a stretch and
300 * when possible sends this number in a PERF_RECORD_LOST event. The number of
301 * such "chunks" of lost events is stored in .nr_events[PERF_EVENT_LOST] while
302 * total_lost tells exactly how many events the kernel in fact lost, i.e. it is
303 * the sum of all struct perf_record_lost.lost fields reported.
305 * The kernel discards mixed up samples and sends the number in a
306 * PERF_RECORD_LOST_SAMPLES event. The number of lost-samples events is stored
307 * in .nr_events[PERF_RECORD_LOST_SAMPLES] while total_lost_samples tells
308 * exactly how many samples the kernel in fact dropped, i.e. it is the sum of
309 * all struct perf_record_lost_samples.lost fields reported.
311 * The total_period is needed because by default auto-freq is used, so
312 * multipling nr_events[PERF_EVENT_SAMPLE] by a frequency isn't possible to get
313 * the total number of low level events, it is necessary to to sum all struct
314 * perf_record_sample.period and stash the result in total_period.
316 struct events_stats {
318 u64 total_non_filtered_period;
320 u64 total_lost_samples;
322 u64 total_aux_partial;
323 u64 total_invalid_chains;
324 u32 nr_events[PERF_RECORD_HEADER_MAX];
325 u32 nr_non_filtered_samples;
327 u32 nr_unknown_events;
328 u32 nr_invalid_chains;
330 u32 nr_unprocessable_samples;
331 u32 nr_auxtrace_errors[PERF_AUXTRACE_ERROR_MAX];
332 u32 nr_proc_map_timeout;
336 PERF_STAT_ROUND_TYPE__INTERVAL = 0,
337 PERF_STAT_ROUND_TYPE__FINAL = 1,
340 struct compressed_event {
341 struct perf_event_header header;
346 struct perf_event_header header;
347 struct perf_record_mmap mmap;
348 struct perf_record_mmap2 mmap2;
349 struct perf_record_comm comm;
350 struct perf_record_namespaces namespaces;
351 struct perf_record_fork fork;
352 struct perf_record_lost lost;
353 struct perf_record_lost_samples lost_samples;
354 struct perf_record_read read;
355 struct perf_record_throttle throttle;
356 struct perf_record_sample sample;
357 struct perf_record_bpf_event bpf;
358 struct perf_record_ksymbol ksymbol;
359 struct attr_event attr;
360 struct event_update_event event_update;
361 struct event_type_event event_type;
362 struct tracing_data_event tracing_data;
363 struct build_id_event build_id;
364 struct id_index_event id_index;
365 struct auxtrace_info_event auxtrace_info;
366 struct auxtrace_event auxtrace;
367 struct auxtrace_error_event auxtrace_error;
368 struct aux_event aux;
369 struct itrace_start_event itrace_start;
370 struct context_switch_event context_switch;
371 struct thread_map_event thread_map;
372 struct cpu_map_event cpu_map;
373 struct stat_config_event stat_config;
374 struct stat_event stat;
375 struct stat_round_event stat_round;
376 struct time_conv_event time_conv;
377 struct feature_event feat;
378 struct compressed_event pack;
381 void perf_event__print_totals(void);
384 struct perf_thread_map;
386 struct perf_stat_config;
387 struct perf_counts_values;
389 typedef int (*perf_event__handler_t)(struct perf_tool *tool,
390 union perf_event *event,
391 struct perf_sample *sample,
392 struct machine *machine);
394 int perf_event__synthesize_thread_map(struct perf_tool *tool,
395 struct perf_thread_map *threads,
396 perf_event__handler_t process,
397 struct machine *machine, bool mmap_data);
398 int perf_event__synthesize_thread_map2(struct perf_tool *tool,
399 struct perf_thread_map *threads,
400 perf_event__handler_t process,
401 struct machine *machine);
402 int perf_event__synthesize_cpu_map(struct perf_tool *tool,
403 struct perf_cpu_map *cpus,
404 perf_event__handler_t process,
405 struct machine *machine);
406 int perf_event__synthesize_threads(struct perf_tool *tool,
407 perf_event__handler_t process,
408 struct machine *machine, bool mmap_data,
409 unsigned int nr_threads_synthesize);
410 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
411 perf_event__handler_t process,
412 struct machine *machine);
413 int perf_event__synthesize_stat_config(struct perf_tool *tool,
414 struct perf_stat_config *config,
415 perf_event__handler_t process,
416 struct machine *machine);
417 void perf_event__read_stat_config(struct perf_stat_config *config,
418 struct stat_config_event *event);
419 int perf_event__synthesize_stat(struct perf_tool *tool,
420 u32 cpu, u32 thread, u64 id,
421 struct perf_counts_values *count,
422 perf_event__handler_t process,
423 struct machine *machine);
424 int perf_event__synthesize_stat_round(struct perf_tool *tool,
426 perf_event__handler_t process,
427 struct machine *machine);
428 int perf_event__synthesize_modules(struct perf_tool *tool,
429 perf_event__handler_t process,
430 struct machine *machine);
432 int perf_event__process_comm(struct perf_tool *tool,
433 union perf_event *event,
434 struct perf_sample *sample,
435 struct machine *machine);
436 int perf_event__process_lost(struct perf_tool *tool,
437 union perf_event *event,
438 struct perf_sample *sample,
439 struct machine *machine);
440 int perf_event__process_lost_samples(struct perf_tool *tool,
441 union perf_event *event,
442 struct perf_sample *sample,
443 struct machine *machine);
444 int perf_event__process_aux(struct perf_tool *tool,
445 union perf_event *event,
446 struct perf_sample *sample,
447 struct machine *machine);
448 int perf_event__process_itrace_start(struct perf_tool *tool,
449 union perf_event *event,
450 struct perf_sample *sample,
451 struct machine *machine);
452 int perf_event__process_switch(struct perf_tool *tool,
453 union perf_event *event,
454 struct perf_sample *sample,
455 struct machine *machine);
456 int perf_event__process_namespaces(struct perf_tool *tool,
457 union perf_event *event,
458 struct perf_sample *sample,
459 struct machine *machine);
460 int perf_event__process_mmap(struct perf_tool *tool,
461 union perf_event *event,
462 struct perf_sample *sample,
463 struct machine *machine);
464 int perf_event__process_mmap2(struct perf_tool *tool,
465 union perf_event *event,
466 struct perf_sample *sample,
467 struct machine *machine);
468 int perf_event__process_fork(struct perf_tool *tool,
469 union perf_event *event,
470 struct perf_sample *sample,
471 struct machine *machine);
472 int perf_event__process_exit(struct perf_tool *tool,
473 union perf_event *event,
474 struct perf_sample *sample,
475 struct machine *machine);
476 int perf_event__process_ksymbol(struct perf_tool *tool,
477 union perf_event *event,
478 struct perf_sample *sample,
479 struct machine *machine);
480 int perf_event__process_bpf(struct perf_tool *tool,
481 union perf_event *event,
482 struct perf_sample *sample,
483 struct machine *machine);
484 int perf_tool__process_synth_event(struct perf_tool *tool,
485 union perf_event *event,
486 struct machine *machine,
487 perf_event__handler_t process);
488 int perf_event__process(struct perf_tool *tool,
489 union perf_event *event,
490 struct perf_sample *sample,
491 struct machine *machine);
493 struct addr_location;
495 int machine__resolve(struct machine *machine, struct addr_location *al,
496 struct perf_sample *sample);
498 void addr_location__put(struct addr_location *al);
502 bool is_bts_event(struct perf_event_attr *attr);
503 bool sample_addr_correlates_sym(struct perf_event_attr *attr);
504 void thread__resolve(struct thread *thread, struct addr_location *al,
505 struct perf_sample *sample);
507 const char *perf_event__name(unsigned int id);
509 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
511 int perf_event__synthesize_sample(union perf_event *event, u64 type,
513 const struct perf_sample *sample);
515 pid_t perf_event__synthesize_comm(struct perf_tool *tool,
516 union perf_event *event, pid_t pid,
517 perf_event__handler_t process,
518 struct machine *machine);
520 int perf_event__synthesize_namespaces(struct perf_tool *tool,
521 union perf_event *event,
522 pid_t pid, pid_t tgid,
523 perf_event__handler_t process,
524 struct machine *machine);
526 int perf_event__synthesize_mmap_events(struct perf_tool *tool,
527 union perf_event *event,
528 pid_t pid, pid_t tgid,
529 perf_event__handler_t process,
530 struct machine *machine,
533 int perf_event__synthesize_extra_kmaps(struct perf_tool *tool,
534 perf_event__handler_t process,
535 struct machine *machine);
537 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp);
538 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp);
539 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp);
540 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp);
541 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp);
542 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp);
543 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp);
544 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp);
545 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp);
546 size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp);
547 size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp);
548 size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp);
549 size_t perf_event__fprintf(union perf_event *event, FILE *fp);
551 int kallsyms__get_function_start(const char *kallsyms_filename,
552 const char *symbol_name, u64 *addr);
554 void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max);
555 void cpu_map_data__synthesize(struct cpu_map_data *data, struct perf_cpu_map *map,
558 void event_attr_init(struct perf_event_attr *attr);
560 int perf_event_paranoid(void);
561 bool perf_event_paranoid_check(int max_level);
563 extern int sysctl_perf_event_max_stack;
564 extern int sysctl_perf_event_max_contexts_per_stack;
565 extern unsigned int proc_map_timeout;
567 #endif /* __PERF_RECORD_H */