perf: Convert a FILE* instead of a filename
[akaros.git] / tools / profile / 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 /*
316  * Hardware event_id to monitor via a performance monitoring event:
317  */
318 struct perf_event_attr {
319         /*
320          * Major type: hardware/software/tracepoint/etc.
321          */
322         uint32_t type;
323
324         /*
325          * Size of the attr structure, for fwd/bwd compat.
326          */
327         uint32_t size;
328
329         /*
330          * Type specific configuration information.
331          */
332         uint64_t config;
333
334         union {
335                 uint64_t sample_period;
336                 uint64_t sample_freq;
337         };
338
339         uint64_t sample_type;
340         uint64_t read_format;
341
342         uint64_t disabled               :  1, /* off by default            */
343                 inherit            :  1, /* children inherit it   */
344                 pinned             :  1, /* must always be on PMU */
345                 exclusive          :  1, /* only group on PMU     */
346                 exclude_user   :  1, /* don't count user          */
347                 exclude_kernel :  1, /* ditto kernel              */
348                 exclude_hv         :  1, /* ditto hypervisor      */
349                 exclude_idle   :  1, /* don't count when idle */
350                 mmap               :  1, /* include mmap data     */
351                 comm               :  1, /* include comm data     */
352                 freq               :  1, /* use freq, not period  */
353                 inherit_stat   :  1, /* per task counts           */
354                 enable_on_exec :  1, /* next exec enables         */
355                 task               :  1, /* trace fork/exit               */
356                 watermark          :  1, /* wakeup_watermark      */
357         /*
358          * precise_ip:
359          *
360          *      0 - SAMPLE_IP can have arbitrary skid
361          *      1 - SAMPLE_IP must have constant skid
362          *      2 - SAMPLE_IP requested to have 0 skid
363          *      3 - SAMPLE_IP must have 0 skid
364          *
365          *      See also PERF_RECORD_MISC_EXACT_IP
366          */
367                 precise_ip         :  2, /* skid constraint               */
368                 mmap_data          :  1, /* non-exec mmap data    */
369                 sample_id_all  :  1, /* sample_type all events */
370
371                 exclude_host   :  1, /* don't count in host       */
372                 exclude_guest  :  1, /* don't count in guest  */
373
374                 exclude_callchain_kernel : 1, /* exclude kernel callchains */
375                 exclude_callchain_user   : 1, /* exclude user callchains */
376                 mmap2              :  1, /* include mmap with inode data         */
377
378                 __reserved_1   : 40;
379
380         union {
381                 uint32_t wakeup_events;   /* wakeup every n events */
382                 uint32_t wakeup_watermark; /* bytes before wakeup       */
383         };
384
385         uint32_t bp_type;
386         union {
387                 uint64_t bp_addr;
388                 uint64_t config1; /* extension of config */
389         };
390         union {
391                 uint64_t bp_len;
392                 uint64_t config2; /* extension of config1 */
393         };
394         uint64_t branch_sample_type; /* enum perf_branch_sample_type */
395
396         /*
397          * Defines set of user regs to dump on samples.
398          * See asm/perf_regs.h for details.
399          */
400         uint64_t sample_regs_user;
401
402         /*
403          * Defines size of the user stack to dump on samples.
404          */
405         uint32_t sample_stack_user;
406
407         /* Align to u64. */
408         uint32_t __reserved_2;
409
410         /*
411          * Defines set of regs to dump for each sample
412          * state captured on:
413          *      - precise = 0: PMU interrupt
414          *      - precise > 0: sampled instruction
415          *
416          * See asm/perf_regs.h for details.
417          */
418         uint64_t sample_regs_intr;
419 } __attribute__((packed));
420
421 #define PERF_RECORD_MISC_CPUMODE_MASK           (7 << 0)
422 #define PERF_RECORD_MISC_CPUMODE_UNKNOWN        (0 << 0)
423 #define PERF_RECORD_MISC_KERNEL                 (1 << 0)
424 #define PERF_RECORD_MISC_USER                   (2 << 0)
425 #define PERF_RECORD_MISC_HYPERVISOR             (3 << 0)
426 #define PERF_RECORD_MISC_GUEST_KERNEL           (4 << 0)
427 #define PERF_RECORD_MISC_GUEST_USER             (5 << 0)
428
429 #define PERF_RECORD_MISC_MMAP_DATA              (1 << 13)
430 /*
431  * Indicates that the content of PERF_SAMPLE_IP points to
432  * the actual instruction that triggered the event. See also
433  * perf_event_attr::precise_ip.
434  */
435 #define PERF_RECORD_MISC_EXACT_IP               (1 << 14)
436 /*
437  * Reserve the last bit to indicate some extended misc field
438  */
439 #define PERF_RECORD_MISC_EXT_RESERVED           (1 << 15)
440
441 struct perf_event_header {
442         uint32_t type;
443         uint16_t misc;
444         uint16_t size;
445 } __attribute__((packed));
446
447 struct perf_file_section {
448         uint64_t offset;        /* Offset from start of file */
449         uint64_t size;          /* Size of the section */
450 } __attribute__((packed));
451
452 #define PERF_STRING_ALIGN                                               64
453
454 struct perf_header_string {
455         uint32_t len;
456         char string[0];         /* Zero terminated */
457 };
458
459 struct perf_header_string_list {
460         uint32_t nr;
461         struct perf_header_string strings[0];   /* Variable length records */
462 };
463
464 struct nr_cpus {
465         uint32_t                                        nr_cpus_online;
466         uint32_t                                        nr_cpus_available;
467 };
468
469 struct build_id_event {
470         struct perf_event_header        header;
471         pid_t                                           pid;
472         uint8_t                                         build_id[24];   /* BUILD_ID_SIZE aligned u64 */
473         char                                            filename[];
474 };
475
476 #define MAX_EVENT_NAME 64
477
478 struct perf_trace_event_type {
479         uint64_t event_id;
480         char name[MAX_EVENT_NAME];
481 };
482
483 struct perf_file_attr {
484         struct perf_event_attr attr;
485         struct perf_file_section ids;
486 };
487
488 /* "PERFILE2"
489  */
490 static const uint64_t PERF_MAGIC2 = 0x32454c4946524550ULL;
491
492 struct perf_pipe_file_header {
493         uint64_t magic;                                 /* PERFILE2 */
494         uint64_t size;
495 };
496
497 struct perf_header {
498         uint64_t magic;                                 /* PERFILE2 */
499         uint64_t size;                                  /* Size of the header */
500         uint64_t attr_size;                             /* size of an attribute in attrs */
501         struct perf_file_section attrs;
502         struct perf_file_section data;
503         struct perf_file_section event_types;
504         DECLARE_BITMAP(adds_features, HEADER_FEAT_BITS);
505 } __attribute__((packed));
506
507 /* For type PERF_RECORD_MMAP
508  */
509 struct perf_record_mmap {
510         struct perf_event_header header;
511         uint32_t pid;
512         uint32_t tid;
513         uint64_t addr;
514         uint64_t len;
515         uint64_t pgoff;
516         char filename[0];
517 } __attribute__((packed));
518
519 /* For type PERF_RECORD_COMM
520  */
521 struct perf_record_comm {
522         struct perf_event_header header;
523         uint32_t pid;
524         uint32_t tid;
525         char comm[0];
526 } __attribute__((packed));
527
528 /* For type PERF_RECORD_SAMPLE
529  *
530  * Configured with: PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_TIME |
531  * PERF_SAMPLE_ADDR | PERF_SAMPLE_ID | PERF_SAMPLE_CPU |
532  * PERF_SAMPLE_CALLCHAIN. */
533 struct perf_record_sample {
534         struct perf_event_header header;
535         uint64_t ip;
536         uint32_t pid, tid;
537         uint64_t time;
538         uint64_t addr;
539         uint64_t id;
540         uint32_t cpu, res;
541         uint64_t nr;
542         uint64_t ips[0];
543 } __attribute__((packed));