perf: Move to dev-utils/perf
[akaros.git] / tools / dev-util / perf / perf_format.h
1 /* Copyright (C) 2009-2016, the Linux Perf authors
2  * Copyright (c) 2015 Google Inc
3  * Davide Libenzi <dlibenzi@google.com>
4  * See LICENSE for details.
5  *
6  * Parts of this file come, either directly, or as baseline, from the Linux
7  * kernel source file:
8  *
9  *       tools/perf/util/header.h
10  *
11  * Such file is ruled by the general Linux kernel copyright.
12  */
13
14 #pragma once
15
16 #define BITS_PER_LONG (8 * sizeof(long))
17 #define BITS_TO_LONGS(bits) (((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG)
18 #define DECLARE_BITMAP(name, bits)                              \
19         unsigned long name[BITS_TO_LONGS(bits)]
20
21 enum {
22         HEADER_RESERVED         = 0,    /* Always cleared */
23         HEADER_FIRST_FEATURE    = 1,
24         HEADER_TRACING_DATA     = 1,
25         HEADER_BUILD_ID,
26
27         HEADER_HOSTNAME,
28         HEADER_OSRELEASE,
29         HEADER_VERSION,
30         HEADER_ARCH,
31         HEADER_NRCPUS,
32         HEADER_CPUDESC,
33         HEADER_CPUID,
34         HEADER_TOTAL_MEM,
35         HEADER_CMDLINE,
36         HEADER_EVENT_DESC,
37         HEADER_CPU_TOPOLOGY,
38         HEADER_NUMA_TOPOLOGY,
39         HEADER_BRANCH_STACK,
40         HEADER_PMU_MAPPINGS,
41         HEADER_GROUP_DESC,
42         HEADER_LAST_FEATURE,
43         HEADER_FEAT_BITS        = 256,
44 };
45
46 /*
47  * Bits that can be set in attr.sample_type to request information
48  * in the overflow packets.
49  */
50 enum perf_event_sample_format {
51         PERF_SAMPLE_IP                          = 1U << 0,
52         PERF_SAMPLE_TID                         = 1U << 1,
53         PERF_SAMPLE_TIME                        = 1U << 2,
54         PERF_SAMPLE_ADDR                        = 1U << 3,
55         PERF_SAMPLE_READ                        = 1U << 4,
56         PERF_SAMPLE_CALLCHAIN                   = 1U << 5,
57         PERF_SAMPLE_ID                          = 1U << 6,
58         PERF_SAMPLE_CPU                         = 1U << 7,
59         PERF_SAMPLE_PERIOD                      = 1U << 8,
60         PERF_SAMPLE_STREAM_ID                   = 1U << 9,
61         PERF_SAMPLE_RAW                         = 1U << 10,
62         PERF_SAMPLE_BRANCH_STACK                = 1U << 11,
63         PERF_SAMPLE_REGS_USER                   = 1U << 12,
64         PERF_SAMPLE_STACK_USER                  = 1U << 13,
65         PERF_SAMPLE_WEIGHT                      = 1U << 14,
66         PERF_SAMPLE_DATA_SRC                    = 1U << 15,
67         PERF_SAMPLE_IDENTIFIER                  = 1U << 16,
68         PERF_SAMPLE_TRANSACTION                 = 1U << 17,
69         PERF_SAMPLE_REGS_INTR                   = 1U << 18,
70
71         PERF_SAMPLE_MAX = 1U << 19,             /* non-ABI */
72 };
73
74 enum perf_event_type {
75         /*
76          * If perf_event_attr.sample_id_all is set then all event types will
77          * have the sample_type selected fields related to where/when
78          * (identity) an event took place (TID, TIME, ID, STREAM_ID, CPU,
79          * IDENTIFIER) described in PERF_RECORD_SAMPLE below, it will be stashed
80          * just after the perf_event_header and the fields already present for
81          * the existing fields, i.e. at the end of the payload. That way a newer
82          * perf.data file will be supported by older perf tools, with these new
83          * optional fields being ignored.
84          *
85          * struct sample_id {
86          *      { u32                   pid, tid; } && PERF_SAMPLE_TID
87          *      { u64                   time;     } && PERF_SAMPLE_TIME
88          *      { u64                   id;               } && PERF_SAMPLE_ID
89          *      { u64                   stream_id;} && PERF_SAMPLE_STREAM_ID
90          *      { u32                   cpu, res; } && PERF_SAMPLE_CPU
91          *      { u64                   id;       } && PERF_SAMPLE_IDENTIFIER
92          * } && perf_event_attr::sample_id_all
93          *
94          * Note that PERF_SAMPLE_IDENTIFIER duplicates PERF_SAMPLE_ID.  The
95          * advantage of PERF_SAMPLE_IDENTIFIER is that its position is fixed
96          * relative to header.size.
97          */
98
99         /*
100          * The MMAP events record the PROT_EXEC mappings so that we can
101          * correlate userspace IPs to code. They have the following structure:
102          *
103          * struct {
104          *      struct perf_event_header        header;
105          *
106          *      u32                             pid, tid;
107          *      u64                             addr;
108          *      u64                             len;
109          *      u64                             pgoff;
110          *      char                            filename[];
111          * };
112          */
113         PERF_RECORD_MMAP                        = 1,
114
115         /*
116          * struct {
117          *      struct perf_event_header        header;
118          *      u64                             id;
119          *      u64                             lost;
120          *      struct sample_id                sample_id;
121          * };
122          */
123         PERF_RECORD_LOST                        = 2,
124
125         /*
126          * struct {
127          *      struct perf_event_header        header;
128          *
129          *      u32                             pid, tid;
130          *      char                            comm[];
131          *      struct sample_id                sample_id;
132          * };
133          */
134         PERF_RECORD_COMM                        = 3,
135
136         /*
137          * struct {
138          *      struct perf_event_header        header;
139          *      u32                             pid, ppid;
140          *      u32                             tid, ptid;
141          *      u64                             time;
142          *      struct sample_id                sample_id;
143          * };
144          */
145         PERF_RECORD_EXIT                        = 4,
146
147         /*
148          * struct {
149          *      struct perf_event_header        header;
150          *      u64                             time;
151          *      u64                             id;
152          *      u64                             stream_id;
153          *      struct sample_id                sample_id;
154          * };
155          */
156         PERF_RECORD_THROTTLE                    = 5,
157         PERF_RECORD_UNTHROTTLE                  = 6,
158
159         /*
160          * struct {
161          *      struct perf_event_header        header;
162          *      u32                             pid, ppid;
163          *      u32                             tid, ptid;
164          *      u64                             time;
165          *      struct sample_id                sample_id;
166          * };
167          */
168         PERF_RECORD_FORK                        = 7,
169
170         /*
171          * struct {
172          *      struct perf_event_header        header;
173          *      u32                             pid, tid;
174          *
175          *      struct read_format              values;
176          *      struct sample_id                sample_id;
177          * };
178          */
179         PERF_RECORD_READ                        = 8,
180
181         /*
182          * struct {
183          *      struct perf_event_header        header;
184          *
185          *      #
186          *      # Note that PERF_SAMPLE_IDENTIFIER duplicates PERF_SAMPLE_ID.
187          *      # The advantage of PERF_SAMPLE_IDENTIFIER is that its position
188          *      # is fixed relative to header.
189          *      #
190          *
191          *      { u64                   id;       } && PERF_SAMPLE_IDENTIFIER
192          *      { u64                   ip;       } && PERF_SAMPLE_IP
193          *      { u32                   pid, tid; } && PERF_SAMPLE_TID
194          *      { u64                   time;     } && PERF_SAMPLE_TIME
195          *      { u64                   addr;     } && PERF_SAMPLE_ADDR
196          *      { u64                   id;       } && PERF_SAMPLE_ID
197          *      { u64                   stream_id;} && PERF_SAMPLE_STREAM_ID
198          *      { u32                   cpu, res; } && PERF_SAMPLE_CPU
199          *      { u64                   period;   } && PERF_SAMPLE_PERIOD
200          *
201          *      { struct read_format    values;   } && PERF_SAMPLE_READ
202          *
203          *      { u64                   nr,
204          *        u64                   ips[nr];  } && PERF_SAMPLE_CALLCHAIN
205          *
206          *      #
207          *      # The RAW record below is opaque data wrt the ABI
208          *      #
209          *      # That is, the ABI doesn't make any promises wrt to
210          *      # the stability of its content, it may vary depending
211          *      # on event, hardware, kernel version and phase of
212          *      # the moon.
213          *      #
214          *      # In other words, PERF_SAMPLE_RAW contents are not an ABI.
215          *      #
216          *
217          *      { u32                   size;
218          *        char                                  data[size];}&& PERF_SAMPLE_RAW
219          *
220          *      { u64                                   nr;
221          *                { u64 from, to, flags } lbr[nr];} && PERF_SAMPLE_BRANCH_STACK
222          *
223          *      { u64                   abi; # enum perf_sample_regs_abi
224          *        u64                   regs[weight(mask)]; } && PERF_SAMPLE_REGS_USER
225          *
226          *      { u64                   size;
227          *        char                  data[size];
228          *        u64                   dyn_size; } && PERF_SAMPLE_STACK_USER
229          *
230          *      { u64                   weight;   } && PERF_SAMPLE_WEIGHT
231          *      { u64                   data_src; } && PERF_SAMPLE_DATA_SRC
232          *              { u64                                   transaction; } && PERF_SAMPLE_TRANSACTION
233          *      { u64                   abi; # enum perf_sample_regs_abi
234          *        u64                   regs[weight(mask)]; } && PERF_SAMPLE_REGS_INTR
235          * };
236          */
237         PERF_RECORD_SAMPLE                      = 9,
238
239         /*
240          * The MMAP2 records are an augmented version of MMAP, they add
241          * maj, min, ino numbers to be used to uniquely identify each mapping
242          *
243          * struct {
244          *      struct perf_event_header        header;
245          *
246          *      u32                             pid, tid;
247          *      u64                             addr;
248          *      u64                             len;
249          *      u64                             pgoff;
250          *      u32                             maj;
251          *      u32                             min;
252          *      u64                             ino;
253          *      u64                             ino_generation;
254          *      char                            filename[];
255          *      struct sample_id                sample_id;
256          * };
257          */
258         PERF_RECORD_MMAP2                       = 10,
259
260         PERF_RECORD_MAX,                        /* non-ABI */
261 };
262
263 #define PERF_MAX_STACK_DEPTH            127
264
265 enum perf_callchain_context {
266         PERF_CONTEXT_HV                         = (__uint64_t) -32,
267         PERF_CONTEXT_KERNEL                     = (__uint64_t) -128,
268         PERF_CONTEXT_USER                       = (__uint64_t) -512,
269
270         PERF_CONTEXT_GUEST                      = (__uint64_t) -2048,
271         PERF_CONTEXT_GUEST_KERNEL       = (__uint64_t) -2176,
272         PERF_CONTEXT_GUEST_USER         = (__uint64_t) -2560,
273
274         PERF_CONTEXT_MAX                        = (__uint64_t) -4095,
275 };
276
277 /*
278  * attr.type
279  */
280 enum perf_type_id {
281         PERF_TYPE_HARDWARE                                              = 0,
282         PERF_TYPE_SOFTWARE                                              = 1,
283         PERF_TYPE_TRACEPOINT                                    = 2,
284         PERF_TYPE_HW_CACHE                                              = 3,
285         PERF_TYPE_RAW                                                   = 4,
286         PERF_TYPE_BREAKPOINT                                    = 5,
287         PERF_TYPE_INTEL_CQM                                             = 6,
288
289         PERF_TYPE_MAX,                                                  /* non-ABI */
290 };
291
292 /*
293  * Generalized performance event event_id types, used by the
294  * attr.event_id parameter of the sys_perf_event_open()
295  * syscall:
296  */
297 enum perf_hw_id {
298         /*
299          * Common hardware events, generalized by the kernel:
300          */
301         PERF_COUNT_HW_CPU_CYCLES                                = 0,
302         PERF_COUNT_HW_INSTRUCTIONS                              = 1,
303         PERF_COUNT_HW_CACHE_REFERENCES                  = 2,
304         PERF_COUNT_HW_CACHE_MISSES                              = 3,
305         PERF_COUNT_HW_BRANCH_INSTRUCTIONS               = 4,
306         PERF_COUNT_HW_BRANCH_MISSES                             = 5,
307         PERF_COUNT_HW_BUS_CYCLES                                = 6,
308         PERF_COUNT_HW_STALLED_CYCLES_FRONTEND   = 7,
309         PERF_COUNT_HW_STALLED_CYCLES_BACKEND    = 8,
310         PERF_COUNT_HW_REF_CPU_CYCLES                    = 9,
311
312         PERF_COUNT_HW_MAX,                                              /* non-ABI */
313 };
314
315 /* We can output a bunch of different versions of perf_event_attr.  The oldest
316  * Linux perf I've run across expects version 3 and can't handle anything
317  * larger.  Since we're not using anything from versions 1 or higher, we can sit
318  * at version 0 for now. */
319 #define PERF_ATTR_VER0
320
321 #ifdef PERF_ATTR_VER1
322         #define __PERF_ATTR_VER1 1
323 #endif
324 #ifdef PERF_ATTR_VER2
325         #define __PERF_ATTR_VER1 1
326         #define __PERF_ATTR_VER2 1
327 #endif
328 #ifdef PERF_ATTR_VER3
329         #define __PERF_ATTR_VER1 1
330         #define __PERF_ATTR_VER2 1
331         #define __PERF_ATTR_VER3 1
332 #endif
333 #ifdef PERF_ATTR_VER4
334         #define __PERF_ATTR_VER1 1
335         #define __PERF_ATTR_VER2 1
336         #define __PERF_ATTR_VER3 1
337         #define __PERF_ATTR_VER4 1
338 #endif
339
340 /*
341  * Hardware event_id to monitor via a performance monitoring event:
342  */
343 struct perf_event_attr {
344         /*
345          * Major type: hardware/software/tracepoint/etc.
346          */
347         uint32_t type;
348
349         /*
350          * Size of the attr structure, for fwd/bwd compat.
351          */
352         uint32_t size;
353
354         /*
355          * Type specific configuration information.
356          */
357         uint64_t config;
358
359         union {
360                 uint64_t sample_period;
361                 uint64_t sample_freq;
362         };
363
364         uint64_t sample_type;
365         uint64_t read_format;
366
367         uint64_t disabled               :  1, /* off by default            */
368                 inherit            :  1, /* children inherit it   */
369                 pinned             :  1, /* must always be on PMU */
370                 exclusive          :  1, /* only group on PMU     */
371                 exclude_user   :  1, /* don't count user          */
372                 exclude_kernel :  1, /* ditto kernel              */
373                 exclude_hv         :  1, /* ditto hypervisor      */
374                 exclude_idle   :  1, /* don't count when idle */
375                 mmap               :  1, /* include mmap data     */
376                 comm               :  1, /* include comm data     */
377                 freq               :  1, /* use freq, not period  */
378                 inherit_stat   :  1, /* per task counts           */
379                 enable_on_exec :  1, /* next exec enables         */
380                 task               :  1, /* trace fork/exit               */
381                 watermark          :  1, /* wakeup_watermark      */
382         /*
383          * precise_ip:
384          *
385          *      0 - SAMPLE_IP can have arbitrary skid
386          *      1 - SAMPLE_IP must have constant skid
387          *      2 - SAMPLE_IP requested to have 0 skid
388          *      3 - SAMPLE_IP must have 0 skid
389          *
390          *      See also PERF_RECORD_MISC_EXACT_IP
391          */
392                 precise_ip         :  2, /* skid constraint               */
393                 mmap_data          :  1, /* non-exec mmap data    */
394                 sample_id_all  :  1, /* sample_type all events */
395
396                 exclude_host   :  1, /* don't count in host       */
397                 exclude_guest  :  1, /* don't count in guest  */
398
399                 exclude_callchain_kernel : 1, /* exclude kernel callchains */
400                 exclude_callchain_user   : 1, /* exclude user callchains */
401                 mmap2              :  1, /* include mmap with inode data         */
402
403                 __reserved_1   : 40;
404
405         union {
406                 uint32_t wakeup_events;   /* wakeup every n events */
407                 uint32_t wakeup_watermark; /* bytes before wakeup       */
408         };
409
410         uint32_t bp_type;
411         union {
412                 uint64_t bp_addr;
413                 uint64_t config1; /* extension of config */
414         };
415
416 #ifdef __PERF_ATTR_VER1
417         union {
418                 uint64_t bp_len;
419                 uint64_t config2; /* extension of config1 */
420         };
421
422 # ifdef __PERF_ATTR_VER2
423         uint64_t branch_sample_type; /* enum perf_branch_sample_type */
424
425 #  ifdef __PERF_ATTR_VER3
426         /*
427          * Defines set of user regs to dump on samples.
428          * See asm/perf_regs.h for details.
429          */
430         uint64_t sample_regs_user;
431
432         /*
433          * Defines size of the user stack to dump on samples.
434          */
435         uint32_t sample_stack_user;
436
437 #   ifdef __PERF_ATTR_VER4
438         /* Align to u64. */
439         uint32_t __reserved_2;
440
441         /*
442          * Defines set of regs to dump for each sample
443          * state captured on:
444          *      - precise = 0: PMU interrupt
445          *      - precise > 0: sampled instruction
446          *
447          * See asm/perf_regs.h for details.
448          */
449         uint64_t sample_regs_intr;
450 #   endif /* __PERF_ATTR_VER4 */
451 #  endif /* __PERF_ATTR_VER3 */
452 # endif /* __PERF_ATTR_VER2 */
453 #endif /* __PERF_ATTR_VER1 */
454 } __attribute__((packed));
455
456 #define PERF_RECORD_MISC_CPUMODE_MASK           (7 << 0)
457 #define PERF_RECORD_MISC_CPUMODE_UNKNOWN        (0 << 0)
458 #define PERF_RECORD_MISC_KERNEL                 (1 << 0)
459 #define PERF_RECORD_MISC_USER                   (2 << 0)
460 #define PERF_RECORD_MISC_HYPERVISOR             (3 << 0)
461 #define PERF_RECORD_MISC_GUEST_KERNEL           (4 << 0)
462 #define PERF_RECORD_MISC_GUEST_USER             (5 << 0)
463
464 #define PERF_RECORD_MISC_MMAP_DATA              (1 << 13)
465 /*
466  * Indicates that the content of PERF_SAMPLE_IP points to
467  * the actual instruction that triggered the event. See also
468  * perf_event_attr::precise_ip.
469  */
470 #define PERF_RECORD_MISC_EXACT_IP               (1 << 14)
471 /*
472  * Reserve the last bit to indicate some extended misc field
473  */
474 #define PERF_RECORD_MISC_EXT_RESERVED           (1 << 15)
475
476 struct perf_event_header {
477         uint32_t type;
478         uint16_t misc;
479         uint16_t size;
480 } __attribute__((packed));
481
482 struct perf_file_section {
483         uint64_t offset;        /* Offset from start of file */
484         uint64_t size;          /* Size of the section */
485 } __attribute__((packed));
486
487 #define PERF_STRING_ALIGN                                               64
488
489 struct perf_header_string {
490         uint32_t len;
491         char string[0];         /* Zero terminated */
492 };
493
494 struct perf_header_string_list {
495         uint32_t nr;
496         struct perf_header_string strings[0];   /* Variable length records */
497 };
498
499 struct nr_cpus {
500         uint32_t                                        nr_cpus_online;
501         uint32_t                                        nr_cpus_available;
502 };
503
504 struct build_id_event {
505         struct perf_event_header        header;
506         pid_t                                           pid;
507         uint8_t                                         build_id[24];   /* BUILD_ID_SIZE aligned u64 */
508         char                                            filename[];
509 };
510
511 #define MAX_EVENT_NAME 64
512
513 struct perf_trace_event_type {
514         uint64_t event_id;
515         char name[MAX_EVENT_NAME];
516 };
517
518 struct perf_file_attr {
519         struct perf_event_attr attr;
520         struct perf_file_section ids;
521 };
522
523 /* "PERFILE2"
524  */
525 static const uint64_t PERF_MAGIC2 = 0x32454c4946524550ULL;
526
527 struct perf_pipe_file_header {
528         uint64_t magic;                                 /* PERFILE2 */
529         uint64_t size;
530 };
531
532 struct perf_header {
533         uint64_t magic;                                 /* PERFILE2 */
534         uint64_t size;                                  /* Size of the header */
535         uint64_t attr_size;                             /* size of an attribute in attrs */
536         struct perf_file_section attrs;
537         struct perf_file_section data;
538         struct perf_file_section event_types;
539         DECLARE_BITMAP(adds_features, HEADER_FEAT_BITS);
540 } __attribute__((packed));
541
542 /* For type PERF_RECORD_MMAP
543  */
544 struct perf_record_mmap {
545         struct perf_event_header header;
546         uint32_t pid;
547         uint32_t tid;
548         uint64_t addr;
549         uint64_t len;
550         uint64_t pgoff;
551         char filename[0];
552 } __attribute__((packed));
553
554 /* For type PERF_RECORD_COMM
555  */
556 struct perf_record_comm {
557         struct perf_event_header header;
558         uint32_t pid;
559         uint32_t tid;
560         char comm[0];
561 } __attribute__((packed));
562
563 /* For type PERF_RECORD_SAMPLE
564  *
565  * Configured with: PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_TIME |
566  * PERF_SAMPLE_ADDR | PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_CPU |
567  * PERF_SAMPLE_CALLCHAIN. */
568 struct perf_record_sample {
569         struct perf_event_header header;
570         uint64_t identifier;
571         uint64_t ip;
572         uint32_t pid, tid;
573         uint64_t time;
574         uint64_t addr;
575         uint32_t cpu, res;
576         uint64_t nr;
577         uint64_t ips[0];
578 } __attribute__((packed));