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 aux_sample { 118 u64 size; 119 void *data; 120 }; 121 122 struct perf_sample { 123 u64 ip; 124 u32 pid, tid; 125 u64 time; 126 u64 addr; 127 u64 id; 128 u64 stream_id; 129 u64 period; 130 u64 weight; 131 u64 transaction; 132 u64 insn_cnt; 133 u64 cyc_cnt; 134 u32 cpu; 135 u32 raw_size; 136 u64 data_src; 137 u64 phys_addr; 138 u32 flags; 139 u16 insn_len; 140 u8 cpumode; 141 u16 misc; 142 bool no_hw_idx; /* No hw_idx collected in branch_stack */ 143 char insn[MAX_INSN]; 144 void *raw_data; 145 struct ip_callchain *callchain; 146 struct branch_stack *branch_stack; 147 struct regs_dump user_regs; 148 struct regs_dump intr_regs; 149 struct stack_dump user_stack; 150 struct sample_read read; 151 struct aux_sample aux_sample; 152 }; 153 154 #define PERF_MEM_DATA_SRC_NONE \ 155 (PERF_MEM_S(OP, NA) |\ 156 PERF_MEM_S(LVL, NA) |\ 157 PERF_MEM_S(SNOOP, NA) |\ 158 PERF_MEM_S(LOCK, NA) |\ 159 PERF_MEM_S(TLB, NA)) 160 161 /* Attribute type for custom synthesized events */ 162 #define PERF_TYPE_SYNTH (INT_MAX + 1U) 163 164 /* Attribute config for custom synthesized events */ 165 enum perf_synth_id { 166 PERF_SYNTH_INTEL_PTWRITE, 167 PERF_SYNTH_INTEL_MWAIT, 168 PERF_SYNTH_INTEL_PWRE, 169 PERF_SYNTH_INTEL_EXSTOP, 170 PERF_SYNTH_INTEL_PWRX, 171 PERF_SYNTH_INTEL_CBR, 172 }; 173 174 /* 175 * Raw data formats for synthesized events. Note that 4 bytes of padding are 176 * present to match the 'size' member of PERF_SAMPLE_RAW data which is always 177 * 8-byte aligned. That means we must dereference raw_data with an offset of 4. 178 * Refer perf_sample__synth_ptr() and perf_synth__raw_data(). It also means the 179 * structure sizes are 4 bytes bigger than the raw_size, refer 180 * perf_synth__raw_size(). 181 */ 182 183 struct perf_synth_intel_ptwrite { 184 u32 padding; 185 union { 186 struct { 187 u32 ip : 1, 188 reserved : 31; 189 }; 190 u32 flags; 191 }; 192 u64 payload; 193 }; 194 195 struct perf_synth_intel_mwait { 196 u32 padding; 197 u32 reserved; 198 union { 199 struct { 200 u64 hints : 8, 201 reserved1 : 24, 202 extensions : 2, 203 reserved2 : 30; 204 }; 205 u64 payload; 206 }; 207 }; 208 209 struct perf_synth_intel_pwre { 210 u32 padding; 211 u32 reserved; 212 union { 213 struct { 214 u64 reserved1 : 7, 215 hw : 1, 216 subcstate : 4, 217 cstate : 4, 218 reserved2 : 48; 219 }; 220 u64 payload; 221 }; 222 }; 223 224 struct perf_synth_intel_exstop { 225 u32 padding; 226 union { 227 struct { 228 u32 ip : 1, 229 reserved : 31; 230 }; 231 u32 flags; 232 }; 233 }; 234 235 struct perf_synth_intel_pwrx { 236 u32 padding; 237 u32 reserved; 238 union { 239 struct { 240 u64 deepest_cstate : 4, 241 last_cstate : 4, 242 wake_reason : 4, 243 reserved1 : 52; 244 }; 245 u64 payload; 246 }; 247 }; 248 249 struct perf_synth_intel_cbr { 250 u32 padding; 251 union { 252 struct { 253 u32 cbr : 8, 254 reserved1 : 8, 255 max_nonturbo : 8, 256 reserved2 : 8; 257 }; 258 u32 flags; 259 }; 260 u32 freq; 261 u32 reserved3; 262 }; 263 264 /* 265 * raw_data is always 4 bytes from an 8-byte boundary, so subtract 4 to get 266 * 8-byte alignment. 267 */ 268 static inline void *perf_sample__synth_ptr(struct perf_sample *sample) 269 { 270 return sample->raw_data - 4; 271 } 272 273 static inline void *perf_synth__raw_data(void *p) 274 { 275 return p + 4; 276 } 277 278 #define perf_synth__raw_size(d) (sizeof(d) - 4) 279 280 #define perf_sample__bad_synth_size(s, d) ((s)->raw_size < sizeof(d) - 4) 281 282 enum { 283 PERF_STAT_ROUND_TYPE__INTERVAL = 0, 284 PERF_STAT_ROUND_TYPE__FINAL = 1, 285 }; 286 287 void perf_event__print_totals(void); 288 289 struct perf_cpu_map; 290 struct perf_record_stat_config; 291 struct perf_stat_config; 292 struct perf_tool; 293 294 void perf_event__read_stat_config(struct perf_stat_config *config, 295 struct perf_record_stat_config *event); 296 297 int perf_event__process_comm(struct perf_tool *tool, 298 union perf_event *event, 299 struct perf_sample *sample, 300 struct machine *machine); 301 int perf_event__process_lost(struct perf_tool *tool, 302 union perf_event *event, 303 struct perf_sample *sample, 304 struct machine *machine); 305 int perf_event__process_lost_samples(struct perf_tool *tool, 306 union perf_event *event, 307 struct perf_sample *sample, 308 struct machine *machine); 309 int perf_event__process_aux(struct perf_tool *tool, 310 union perf_event *event, 311 struct perf_sample *sample, 312 struct machine *machine); 313 int perf_event__process_itrace_start(struct perf_tool *tool, 314 union perf_event *event, 315 struct perf_sample *sample, 316 struct machine *machine); 317 int perf_event__process_switch(struct perf_tool *tool, 318 union perf_event *event, 319 struct perf_sample *sample, 320 struct machine *machine); 321 int perf_event__process_namespaces(struct perf_tool *tool, 322 union perf_event *event, 323 struct perf_sample *sample, 324 struct machine *machine); 325 int perf_event__process_mmap(struct perf_tool *tool, 326 union perf_event *event, 327 struct perf_sample *sample, 328 struct machine *machine); 329 int perf_event__process_mmap2(struct perf_tool *tool, 330 union perf_event *event, 331 struct perf_sample *sample, 332 struct machine *machine); 333 int perf_event__process_fork(struct perf_tool *tool, 334 union perf_event *event, 335 struct perf_sample *sample, 336 struct machine *machine); 337 int perf_event__process_exit(struct perf_tool *tool, 338 union perf_event *event, 339 struct perf_sample *sample, 340 struct machine *machine); 341 int perf_event__process_ksymbol(struct perf_tool *tool, 342 union perf_event *event, 343 struct perf_sample *sample, 344 struct machine *machine); 345 int perf_event__process_bpf(struct perf_tool *tool, 346 union perf_event *event, 347 struct perf_sample *sample, 348 struct machine *machine); 349 int perf_event__process(struct perf_tool *tool, 350 union perf_event *event, 351 struct perf_sample *sample, 352 struct machine *machine); 353 354 struct addr_location; 355 356 int machine__resolve(struct machine *machine, struct addr_location *al, 357 struct perf_sample *sample); 358 359 void addr_location__put(struct addr_location *al); 360 361 struct thread; 362 363 bool is_bts_event(struct perf_event_attr *attr); 364 bool sample_addr_correlates_sym(struct perf_event_attr *attr); 365 void thread__resolve(struct thread *thread, struct addr_location *al, 366 struct perf_sample *sample); 367 368 const char *perf_event__name(unsigned int id); 369 370 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp); 371 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp); 372 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp); 373 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp); 374 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp); 375 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp); 376 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp); 377 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp); 378 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp); 379 size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp); 380 size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp); 381 size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp); 382 size_t perf_event__fprintf(union perf_event *event, FILE *fp); 383 384 int kallsyms__get_function_start(const char *kallsyms_filename, 385 const char *symbol_name, u64 *addr); 386 387 void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max); 388 void cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf_cpu_map *map, 389 u16 type, int max); 390 391 void event_attr_init(struct perf_event_attr *attr); 392 393 int perf_event_paranoid(void); 394 bool perf_event_paranoid_check(int max_level); 395 396 extern int sysctl_perf_event_max_stack; 397 extern int sysctl_perf_event_max_contexts_per_stack; 398 extern unsigned int proc_map_timeout; 399 400 #endif /* __PERF_RECORD_H */ 401