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