94777ee435c2ab7658f6422b52bb27266f4983c2
[linux-2.6-microblaze.git] / tools / perf / util / event.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_RECORD_H
3 #define __PERF_RECORD_H
4
5 #include <limits.h>
6 #include <stdio.h>
7 #include <linux/kernel.h>
8 #include <linux/bpf.h>
9 #include <linux/perf_event.h>
10 #include <perf/event.h>
11
12 #include "../perf.h"
13 #include "build-id.h"
14 #include "perf_regs.h"
15
16 #ifdef __LP64__
17 /*
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.
22  */
23 #define PRI_lu64 "l" PRIu64
24 #define PRI_lx64 "l" PRIx64
25 #define PRI_ld64 "l" PRId64
26 #else
27 #define PRI_lu64 PRIu64
28 #define PRI_lx64 PRIx64
29 #define PRI_ld64 PRId64
30 #endif
31
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)
38
39 /* perf sample has 16 bits size limit */
40 #define PERF_SAMPLE_MAX_SIZE (1 << 16)
41
42 struct regs_dump {
43         u64 abi;
44         u64 mask;
45         u64 *regs;
46
47         /* Cached values/mask filled by first register access. */
48         u64 cache_regs[PERF_REGS_MAX];
49         u64 cache_mask;
50 };
51
52 struct stack_dump {
53         u16 offset;
54         u64 size;
55         char *data;
56 };
57
58 struct sample_read_value {
59         u64 value;
60         u64 id;
61 };
62
63 struct sample_read {
64         u64 time_enabled;
65         u64 time_running;
66         union {
67                 struct {
68                         u64 nr;
69                         struct sample_read_value *values;
70                 } group;
71                 struct sample_read_value one;
72         };
73 };
74
75 struct ip_callchain {
76         u64 nr;
77         u64 ips[0];
78 };
79
80 struct branch_stack;
81
82 enum {
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,
94 };
95
96 #define PERF_IP_FLAG_CHARS "bcrosyiABEx"
97
98 #define PERF_BRANCH_MASK                (\
99         PERF_IP_FLAG_BRANCH             |\
100         PERF_IP_FLAG_CALL               |\
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)
109
110 #define MAX_INSN 16
111
112 struct perf_sample {
113         u64 ip;
114         u32 pid, tid;
115         u64 time;
116         u64 addr;
117         u64 id;
118         u64 stream_id;
119         u64 period;
120         u64 weight;
121         u64 transaction;
122         u64 insn_cnt;
123         u64 cyc_cnt;
124         u32 cpu;
125         u32 raw_size;
126         u64 data_src;
127         u64 phys_addr;
128         u32 flags;
129         u16 insn_len;
130         u8  cpumode;
131         u16 misc;
132         char insn[MAX_INSN];
133         void *raw_data;
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;
140 };
141
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) |\
147          PERF_MEM_S(TLB, NA))
148
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
170 };
171
172 enum auxtrace_error_type {
173         PERF_AUXTRACE_ERROR_ITRACE  = 1,
174         PERF_AUXTRACE_ERROR_MAX
175 };
176
177 /* Attribute type for custom synthesized events */
178 #define PERF_TYPE_SYNTH         (INT_MAX + 1U)
179
180 /* Attribute config for custom synthesized events */
181 enum perf_synth_id {
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,
188 };
189
190 /*
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().
197  */
198
199 struct perf_synth_intel_ptwrite {
200         u32 padding;
201         union {
202                 struct {
203                         u32     ip              :  1,
204                                 reserved        : 31;
205                 };
206                 u32     flags;
207         };
208         u64     payload;
209 };
210
211 struct perf_synth_intel_mwait {
212         u32 padding;
213         u32 reserved;
214         union {
215                 struct {
216                         u64     hints           :  8,
217                                 reserved1       : 24,
218                                 extensions      :  2,
219                                 reserved2       : 30;
220                 };
221                 u64     payload;
222         };
223 };
224
225 struct perf_synth_intel_pwre {
226         u32 padding;
227         u32 reserved;
228         union {
229                 struct {
230                         u64     reserved1       :  7,
231                                 hw              :  1,
232                                 subcstate       :  4,
233                                 cstate          :  4,
234                                 reserved2       : 48;
235                 };
236                 u64     payload;
237         };
238 };
239
240 struct perf_synth_intel_exstop {
241         u32 padding;
242         union {
243                 struct {
244                         u32     ip              :  1,
245                                 reserved        : 31;
246                 };
247                 u32     flags;
248         };
249 };
250
251 struct perf_synth_intel_pwrx {
252         u32 padding;
253         u32 reserved;
254         union {
255                 struct {
256                         u64     deepest_cstate  :  4,
257                                 last_cstate     :  4,
258                                 wake_reason     :  4,
259                                 reserved1       : 52;
260                 };
261                 u64     payload;
262         };
263 };
264
265 struct perf_synth_intel_cbr {
266         u32 padding;
267         union {
268                 struct {
269                         u32     cbr             :  8,
270                                 reserved1       :  8,
271                                 max_nonturbo    :  8,
272                                 reserved2       :  8;
273                 };
274                 u32     flags;
275         };
276         u32 freq;
277         u32 reserved3;
278 };
279
280 /*
281  * raw_data is always 4 bytes from an 8-byte boundary, so subtract 4 to get
282  * 8-byte alignment.
283  */
284 static inline void *perf_sample__synth_ptr(struct perf_sample *sample)
285 {
286         return sample->raw_data - 4;
287 }
288
289 static inline void *perf_synth__raw_data(void *p)
290 {
291         return p + 4;
292 }
293
294 #define perf_synth__raw_size(d) (sizeof(d) - 4)
295
296 #define perf_sample__bad_synth_size(s, d) ((s)->raw_size < sizeof(d) - 4)
297
298 /*
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.
304  *
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.
310  *
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.
315  */
316 struct events_stats {
317         u64 total_period;
318         u64 total_non_filtered_period;
319         u64 total_lost;
320         u64 total_lost_samples;
321         u64 total_aux_lost;
322         u64 total_aux_partial;
323         u64 total_invalid_chains;
324         u32 nr_events[PERF_RECORD_HEADER_MAX];
325         u32 nr_non_filtered_samples;
326         u32 nr_lost_warned;
327         u32 nr_unknown_events;
328         u32 nr_invalid_chains;
329         u32 nr_unknown_id;
330         u32 nr_unprocessable_samples;
331         u32 nr_auxtrace_errors[PERF_AUXTRACE_ERROR_MAX];
332         u32 nr_proc_map_timeout;
333 };
334
335 enum {
336         PERF_STAT_ROUND_TYPE__INTERVAL  = 0,
337         PERF_STAT_ROUND_TYPE__FINAL     = 1,
338 };
339
340 struct compressed_event {
341         struct perf_event_header        header;
342         char                            data[];
343 };
344
345 union perf_event {
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;
379 };
380
381 void perf_event__print_totals(void);
382
383 struct perf_tool;
384 struct perf_thread_map;
385 struct perf_cpu_map;
386 struct perf_stat_config;
387 struct perf_counts_values;
388
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);
393
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,
425                                       u64 time, u64 type,
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);
431
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);
492
493 struct addr_location;
494
495 int machine__resolve(struct machine *machine, struct addr_location *al,
496                      struct perf_sample *sample);
497
498 void addr_location__put(struct addr_location *al);
499
500 struct thread;
501
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);
506
507 const char *perf_event__name(unsigned int id);
508
509 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
510                                      u64 read_format);
511 int perf_event__synthesize_sample(union perf_event *event, u64 type,
512                                   u64 read_format,
513                                   const struct perf_sample *sample);
514
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);
519
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);
525
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,
531                                        bool mmap_data);
532
533 int perf_event__synthesize_extra_kmaps(struct perf_tool *tool,
534                                        perf_event__handler_t process,
535                                        struct machine *machine);
536
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);
550
551 int kallsyms__get_function_start(const char *kallsyms_filename,
552                                  const char *symbol_name, u64 *addr);
553
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,
556                                u16 type, int max);
557
558 void event_attr_init(struct perf_event_attr *attr);
559
560 int perf_event_paranoid(void);
561 bool perf_event_paranoid_check(int max_level);
562
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;
566
567 #endif /* __PERF_RECORD_H */