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