namespaces.7: ffix
[man-pages.git] / man2 / perf_event_open.2
blobdaf5edddf2cb34822382f9f3d77680d51d7c3ac4
1 .\" Copyright (c) 2012, Vincent Weaver
2 .\"
3 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
4 .\" This is free documentation; you can redistribute it and/or
5 .\" modify it under the terms of the GNU General Public License as
6 .\" published by the Free Software Foundation; either version 2 of
7 .\" the License, or (at your option) any later version.
8 .\"
9 .\" The GNU General Public License's references to "object code"
10 .\" and "executables" are to be interpreted as the output of any
11 .\" document formatting or typesetting system, including
12 .\" intermediate and printed output.
13 .\"
14 .\" This manual is distributed in the hope that it will be useful,
15 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
16 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 .\" GNU General Public License for more details.
18 .\"
19 .\" You should have received a copy of the GNU General Public
20 .\" License along with this manual; if not, see
21 .\" <http://www.gnu.org/licenses/>.
22 .\" %%%LICENSE_END
23 .\"
24 .\" This document is based on the perf_event.h header file, the
25 .\" tools/perf/design.txt file, and a lot of bitter experience.
26 .\"
27 .TH PERF_EVENT_OPEN 2 2021-03-22 "Linux" "Linux Programmer's Manual"
28 .SH NAME
29 perf_event_open \- set up performance monitoring
30 .SH SYNOPSIS
31 .nf
32 .BR "#include <linux/perf_event.h>" "    /* Definition of " PERF_* " constants */"
33 .BR "#include <linux/hw_breakpoint.h>" " /* Definition of " HW_* " constants */"
34 .BR "#include <sys/syscall.h>" "         /* Definition of " SYS_* " constants */"
35 .B #include <unistd.h>
36 .PP
37 .BI "int syscall(SYS_perf_event_open, struct perf_event_attr *" attr ,
38 .BI "            pid_t " pid ", int " cpu ", int " group_fd \
39 ", unsigned long " flags  );
40 .fi
41 .PP
42 .IR Note :
43 glibc provides no wrapper for
44 .BR perf_event_open (),
45 necessitating the use of
46 .BR syscall (2).
47 .SH DESCRIPTION
48 Given a list of parameters,
49 .BR perf_event_open ()
50 returns a file descriptor, for use in subsequent system calls
51 .RB ( read "(2), " mmap "(2), " prctl "(2), " fcntl "(2), etc.)."
52 .PP
53 A call to
54 .BR perf_event_open ()
55 creates a file descriptor that allows measuring performance
56 information.
57 Each file descriptor corresponds to one
58 event that is measured; these can be grouped together
59 to measure multiple events simultaneously.
60 .PP
61 Events can be enabled and disabled in two ways: via
62 .BR ioctl (2)
63 and via
64 .BR prctl (2).
65 When an event is disabled it does not count or generate overflows but does
66 continue to exist and maintain its count value.
67 .PP
68 Events come in two flavors: counting and sampled.
70 .I counting
71 event is one that is used for counting the aggregate number of events
72 that occur.
73 In general, counting event results are gathered with a
74 .BR read (2)
75 call.
77 .I sampling
78 event periodically writes measurements to a buffer that can then
79 be accessed via
80 .BR mmap (2).
81 .SS Arguments
82 The
83 .I pid
84 and
85 .I cpu
86 arguments allow specifying which process and CPU to monitor:
87 .TP
88 .BR "pid == 0" " and " "cpu == \-1"
89 This measures the calling process/thread on any CPU.
90 .TP
91 .BR "pid == 0" " and " "cpu >= 0"
92 This measures the calling process/thread only
93 when running on the specified CPU.
94 .TP
95 .BR "pid > 0" " and " "cpu == \-1"
96 This measures the specified process/thread on any CPU.
97 .TP
98 .BR "pid > 0" " and " "cpu >= 0"
99 This measures the specified process/thread only
100 when running on the specified CPU.
102 .BR "pid == \-1" " and " "cpu >= 0"
103 This measures all processes/threads on the specified CPU.
104 This requires
105 .B CAP_PERFMON
106 (since Linux 5.8) or
107 .B CAP_SYS_ADMIN
108 capability or a
109 .I /proc/sys/kernel/perf_event_paranoid
110 value of less than 1.
112 .BR "pid == \-1" " and " "cpu == \-1"
113 This setting is invalid and will return an error.
115 When
116 .I pid
117 is greater than zero, permission to perform this system call
118 is governed by
119 .B CAP_PERFMON
120 (since Linux 5.9) and a ptrace access mode
121 .B PTRACE_MODE_READ_REALCREDS
122 check on older Linux versions; see
123 .BR ptrace (2).
126 .I group_fd
127 argument allows event groups to be created.
128 An event group has one event which is the group leader.
129 The leader is created first, with
130 .IR group_fd " = \-1."
131 The rest of the group members are created with subsequent
132 .BR perf_event_open ()
133 calls with
134 .I group_fd
135 being set to the file descriptor of the group leader.
136 (A single event on its own is created with
137 .IR group_fd " = \-1"
138 and is considered to be a group with only 1 member.)
139 An event group is scheduled onto the CPU as a unit: it will
140 be put onto the CPU only if all of the events in the group can be put onto
141 the CPU.
142 This means that the values of the member events can be
143 meaningfully compared\(emadded, divided (to get ratios), and so on\(emwith each
144 other, since they have counted events for the same set of executed
145 instructions.
148 .I flags
149 argument is formed by ORing together zero or more of the following values:
151 .BR PERF_FLAG_FD_CLOEXEC " (since Linux 3.14)"
152 .\" commit a21b0b354d4ac39be691f51c53562e2c24443d9e
153 This flag enables the close-on-exec flag for the created
154 event file descriptor,
155 so that the file descriptor is automatically closed on
156 .BR execve (2).
157 Setting the close-on-exec flags at creation time, rather than later with
158 .BR fcntl (2),
159 avoids potential race conditions where the calling thread invokes
160 .BR perf_event_open ()
162 .BR fcntl (2)
163 at the same time as another thread calls
164 .BR fork (2)
165 then
166 .BR execve (2).
168 .BR PERF_FLAG_FD_NO_GROUP
169 This flag tells the event to ignore the
170 .I group_fd
171 parameter except for the purpose of setting up output redirection
172 using the
173 .B PERF_FLAG_FD_OUTPUT
174 flag.
176 .BR PERF_FLAG_FD_OUTPUT " (broken since Linux 2.6.35)"
177 .\" commit ac9721f3f54b27a16c7e1afb2481e7ee95a70318
178 This flag re-routes the event's sampled output to instead
179 be included in the mmap buffer of the event specified by
180 .IR group_fd .
182 .BR PERF_FLAG_PID_CGROUP " (since Linux 2.6.39)"
183 .\" commit e5d1367f17ba6a6fed5fd8b74e4d5720923e0c25
184 This flag activates per-container system-wide monitoring.
185 A container
186 is an abstraction that isolates a set of resources for finer-grained
187 control (CPUs, memory, etc.).
188 In this mode, the event is measured
189 only if the thread running on the monitored CPU belongs to the designated
190 container (cgroup).
191 The cgroup is identified by passing a file descriptor
192 opened on its directory in the cgroupfs filesystem.
193 For instance, if the
194 cgroup to monitor is called
195 .IR test ,
196 then a file descriptor opened on
197 .I /dev/cgroup/test
198 (assuming cgroupfs is mounted on
199 .IR /dev/cgroup )
200 must be passed as the
201 .I pid
202 parameter.
203 cgroup monitoring is available only
204 for system-wide events and may therefore require extra permissions.
207 .I perf_event_attr
208 structure provides detailed configuration information
209 for the event being created.
211 .in +4n
213 struct perf_event_attr {
214     __u32 type;                 /* Type of event */
215     __u32 size;                 /* Size of attribute structure */
216     __u64 config;               /* Type\-specific configuration */
218     union {
219         __u64 sample_period;    /* Period of sampling */
220         __u64 sample_freq;      /* Frequency of sampling */
221     };
223     __u64 sample_type;  /* Specifies values included in sample */
224     __u64 read_format;  /* Specifies values returned in read */
226     __u64 disabled       : 1,   /* off by default */
227           inherit        : 1,   /* children inherit it */
228           pinned         : 1,   /* must always be on PMU */
229           exclusive      : 1,   /* only group on PMU */
230           exclude_user   : 1,   /* don\(aqt count user */
231           exclude_kernel : 1,   /* don\(aqt count kernel */
232           exclude_hv     : 1,   /* don\(aqt count hypervisor */
233           exclude_idle   : 1,   /* don\(aqt count when idle */
234           mmap           : 1,   /* include mmap data */
235           comm           : 1,   /* include comm data */
236           freq           : 1,   /* use freq, not period */
237           inherit_stat   : 1,   /* per task counts */
238           enable_on_exec : 1,   /* next exec enables */
239           task           : 1,   /* trace fork/exit */
240           watermark      : 1,   /* wakeup_watermark */
241           precise_ip     : 2,   /* skid constraint */
242           mmap_data      : 1,   /* non\-exec mmap data */
243           sample_id_all  : 1,   /* sample_type all events */
244           exclude_host   : 1,   /* don\(aqt count in host */
245           exclude_guest  : 1,   /* don\(aqt count in guest */
246           exclude_callchain_kernel : 1,
247                                 /* exclude kernel callchains */
248           exclude_callchain_user   : 1,
249                                 /* exclude user callchains */
250           mmap2          :  1,  /* include mmap with inode data */
251           comm_exec      :  1,  /* flag comm events that are
252                                    due to exec */
253           use_clockid    :  1,  /* use clockid for time fields */
254           context_switch :  1,  /* context switch data */
255           write_backward :  1,  /* Write ring buffer from end
256                                    to beginning */
257           namespaces     :  1,  /* include namespaces data */
258           ksymbol        :  1,  /* include ksymbol events */
259           bpf_event      :  1,  /* include bpf events */
260           aux_output     :  1,  /* generate AUX records
261                                    instead of events */
262           cgroup         :  1,  /* include cgroup events */
263           text_poke      :  1,  /* include text poke events */
265           __reserved_1   : 30;
267     union {
268         __u32 wakeup_events;    /* wakeup every n events */
269         __u32 wakeup_watermark; /* bytes before wakeup */
270     };
272     __u32     bp_type;          /* breakpoint type */
274     union {
275         __u64 bp_addr;          /* breakpoint address */
276         __u64 kprobe_func;      /* for perf_kprobe */
277         __u64 uprobe_path;      /* for perf_uprobe */
278         __u64 config1;          /* extension of config */
279     };
281     union {
282         __u64 bp_len;           /* breakpoint length */
283         __u64 kprobe_addr;      /* with kprobe_func == NULL */
284         __u64 probe_offset;     /* for perf_[k,u]probe */
285         __u64 config2;          /* extension of config1 */
286     };
287     __u64 branch_sample_type;   /* enum perf_branch_sample_type */
288     __u64 sample_regs_user;     /* user regs to dump on samples */
289     __u32 sample_stack_user;    /* size of stack to dump on
290                                    samples */
291     __s32 clockid;              /* clock to use for time fields */
292     __u64 sample_regs_intr;     /* regs to dump on samples */
293     __u32 aux_watermark;        /* aux bytes before wakeup */
294     __u16 sample_max_stack;     /* max frames in callchain */
295     __u16 __reserved_2;         /* align to u64 */
301 The fields of the
302 .I perf_event_attr
303 structure are described in more detail below:
305 .I type
306 This field specifies the overall event type.
307 It has one of the following values:
310 .B PERF_TYPE_HARDWARE
311 This indicates one of the "generalized" hardware events provided
312 by the kernel.
313 See the
314 .I config
315 field definition for more details.
317 .B PERF_TYPE_SOFTWARE
318 This indicates one of the software-defined events provided by the kernel
319 (even if no hardware support is available).
321 .B PERF_TYPE_TRACEPOINT
322 This indicates a tracepoint
323 provided by the kernel tracepoint infrastructure.
325 .B PERF_TYPE_HW_CACHE
326 This indicates a hardware cache event.
327 This has a special encoding, described in the
328 .I config
329 field definition.
331 .B PERF_TYPE_RAW
332 This indicates a "raw" implementation-specific event in the
333 .IR config " field."
335 .BR PERF_TYPE_BREAKPOINT " (since Linux 2.6.33)"
336 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
337 This indicates a hardware breakpoint as provided by the CPU.
338 Breakpoints can be read/write accesses to an address as well as
339 execution of an instruction address.
341 dynamic PMU
342 Since Linux 2.6.38,
343 .\" commit 2e80a82a49c4c7eca4e35734380f28298ba5db19
344 .BR perf_event_open ()
345 can support multiple PMUs.
346 To enable this, a value exported by the kernel can be used in the
347 .I type
348 field to indicate which PMU to use.
349 The value to use can be found in the sysfs filesystem:
350 there is a subdirectory per PMU instance under
351 .IR /sys/bus/event_source/devices .
352 In each subdirectory there is a
353 .I type
354 file whose content is an integer that can be used in the
355 .I type
356 field.
357 For instance,
358 .I /sys/bus/event_source/devices/cpu/type
359 contains the value for the core CPU PMU, which is usually 4.
361 .BR kprobe " and " uprobe " (since Linux 4.17)"
362 .\" commit 65074d43fc77bcae32776724b7fa2696923c78e4
363 .\" commit e12f03d7031a977356e3d7b75a68c2185ff8d155
364 .\" commit 33ea4b24277b06dbc55d7f5772a46f029600255e
365 These two dynamic PMUs create a kprobe/uprobe and attach it to the
366 file descriptor generated by perf_event_open.
367 The kprobe/uprobe will be destroyed on the destruction of the file descriptor.
368 See fields
369 .IR kprobe_func ,
370 .IR uprobe_path ,
371 .IR kprobe_addr ,
373 .I probe_offset
374 for more details.
377 .I "size"
378 The size of the
379 .I perf_event_attr
380 structure for forward/backward compatibility.
381 Set this using
382 .I sizeof(struct perf_event_attr)
383 to allow the kernel to see
384 the struct size at the time of compilation.
386 The related define
387 .B PERF_ATTR_SIZE_VER0
388 is set to 64; this was the size of the first published struct.
389 .B PERF_ATTR_SIZE_VER1
390 is 72, corresponding to the addition of breakpoints in Linux 2.6.33.
391 .\" commit cb5d76999029ae7a517cb07dfa732c1b5a934fc2
392 .\" this was added much later when PERF_ATTR_SIZE_VER2 happened
393 .\" but the actual attr_size had increased in 2.6.33
394 .B PERF_ATTR_SIZE_VER2
395 is 80 corresponding to the addition of branch sampling in Linux 3.4.
396 .\" commit cb5d76999029ae7a517cb07dfa732c1b5a934fc2
397 .B PERF_ATTR_SIZE_VER3
398 is 96 corresponding to the addition
400 .I sample_regs_user
402 .I sample_stack_user
403 in Linux 3.7.
404 .\" commit 1659d129ed014b715b0b2120e6fd929bdd33ed03
405 .B PERF_ATTR_SIZE_VER4
406 is 104 corresponding to the addition of
407 .I sample_regs_intr
408 in Linux 3.19.
409 .\" commit 60e2364e60e86e81bc6377f49779779e6120977f
410 .B PERF_ATTR_SIZE_VER5
411 is 112 corresponding to the addition of
412 .I aux_watermark
413 in Linux 4.1.
414 .\" commit 1a5941312414c71dece6717da9a0fa1303127afa
416 .I "config"
417 This specifies which event you want, in conjunction with
419 .I type
420 field.
422 .I config1
424 .I config2
425 fields are also taken into account in cases where 64 bits is not
426 enough to fully specify the event.
427 The encoding of these fields are event dependent.
429 There are various ways to set the
430 .I config
431 field that are dependent on the value of the previously
432 described
433 .I type
434 field.
435 What follows are various possible settings for
436 .I config
437 separated out by
438 .IR type .
441 .I type
443 .BR PERF_TYPE_HARDWARE ,
444 we are measuring one of the generalized hardware CPU events.
445 Not all of these are available on all platforms.
447 .I config
448 to one of the following:
449 .RS 12
451 .B PERF_COUNT_HW_CPU_CYCLES
452 Total cycles.
453 Be wary of what happens during CPU frequency scaling.
455 .B PERF_COUNT_HW_INSTRUCTIONS
456 Retired instructions.
457 Be careful, these can be affected by various
458 issues, most notably hardware interrupt counts.
460 .B PERF_COUNT_HW_CACHE_REFERENCES
461 Cache accesses.
462 Usually this indicates Last Level Cache accesses but this may
463 vary depending on your CPU.
464 This may include prefetches and coherency messages; again this
465 depends on the design of your CPU.
467 .B PERF_COUNT_HW_CACHE_MISSES
468 Cache misses.
469 Usually this indicates Last Level Cache misses; this is intended to be
470 used in conjunction with the
471 .B PERF_COUNT_HW_CACHE_REFERENCES
472 event to calculate cache miss rates.
474 .B PERF_COUNT_HW_BRANCH_INSTRUCTIONS
475 Retired branch instructions.
476 Prior to Linux 2.6.35, this used
477 the wrong event on AMD processors.
478 .\" commit f287d332ce835f77a4f5077d2c0ef1e3f9ea42d2
480 .B PERF_COUNT_HW_BRANCH_MISSES
481 Mispredicted branch instructions.
483 .B PERF_COUNT_HW_BUS_CYCLES
484 Bus cycles, which can be different from total cycles.
486 .BR PERF_COUNT_HW_STALLED_CYCLES_FRONTEND " (since Linux 3.0)"
487 .\" commit 8f62242246351b5a4bc0c1f00c0c7003edea128a
488 Stalled cycles during issue.
490 .BR PERF_COUNT_HW_STALLED_CYCLES_BACKEND  " (since Linux 3.0)"
491 .\" commit 8f62242246351b5a4bc0c1f00c0c7003edea128a
492 Stalled cycles during retirement.
494 .BR PERF_COUNT_HW_REF_CPU_CYCLES  " (since Linux 3.3)"
495 .\" commit c37e17497e01fc0f5d2d6feb5723b210b3ab8890
496 Total cycles; not affected by CPU frequency scaling.
500 .I type
502 .BR PERF_TYPE_SOFTWARE ,
503 we are measuring software events provided by the kernel.
505 .I config
506 to one of the following:
507 .RS 12
509 .B PERF_COUNT_SW_CPU_CLOCK
510 This reports the CPU clock, a high-resolution per-CPU timer.
512 .B PERF_COUNT_SW_TASK_CLOCK
513 This reports a clock count specific to the task that is running.
515 .B PERF_COUNT_SW_PAGE_FAULTS
516 This reports the number of page faults.
518 .B PERF_COUNT_SW_CONTEXT_SWITCHES
519 This counts context switches.
520 Until Linux 2.6.34, these were all reported as user-space
521 events, after that they are reported as happening in the kernel.
522 .\" commit e49a5bd38159dfb1928fd25b173bc9de4bbadb21
524 .B PERF_COUNT_SW_CPU_MIGRATIONS
525 This reports the number of times the process
526 has migrated to a new CPU.
528 .B PERF_COUNT_SW_PAGE_FAULTS_MIN
529 This counts the number of minor page faults.
530 These did not require disk I/O to handle.
532 .B PERF_COUNT_SW_PAGE_FAULTS_MAJ
533 This counts the number of major page faults.
534 These required disk I/O to handle.
536 .BR PERF_COUNT_SW_ALIGNMENT_FAULTS " (since Linux 2.6.33)"
537 .\" commit f7d7986060b2890fc26db6ab5203efbd33aa2497
538 This counts the number of alignment faults.
539 These happen when unaligned memory accesses happen; the kernel
540 can handle these but it reduces performance.
541 This happens only on some architectures (never on x86).
543 .BR PERF_COUNT_SW_EMULATION_FAULTS " (since Linux 2.6.33)"
544 .\" commit f7d7986060b2890fc26db6ab5203efbd33aa2497
545 This counts the number of emulation faults.
546 The kernel sometimes traps on unimplemented instructions
547 and emulates them for user space.
548 This can negatively impact performance.
550 .BR PERF_COUNT_SW_DUMMY " (since Linux 3.12)"
551 .\" commit fa0097ee690693006ab1aea6c01ad3c851b65c77
552 This is a placeholder event that counts nothing.
553 Informational sample record types such as mmap or comm
554 must be associated with an active event.
555 This dummy event allows gathering such records without requiring
556 a counting event.
561 .I type
563 .BR PERF_TYPE_TRACEPOINT ,
564 then we are measuring kernel tracepoints.
565 The value to use in
566 .I config
567 can be obtained from under debugfs
568 .I tracing/events/*/*/id
569 if ftrace is enabled in the kernel.
574 .I type
576 .BR PERF_TYPE_HW_CACHE ,
577 then we are measuring a hardware CPU cache event.
578 To calculate the appropriate
579 .I config
580 value, use the following equation:
581 .RS 4
583 .in +4n
585 config = (perf_hw_cache_id) |
586          (perf_hw_cache_op_id << 8) |
587          (perf_hw_cache_op_result_id << 16);
591 where
592 .I perf_hw_cache_id
593 is one of:
594 .RS 4
596 .B PERF_COUNT_HW_CACHE_L1D
597 for measuring Level 1 Data Cache
599 .B PERF_COUNT_HW_CACHE_L1I
600 for measuring Level 1 Instruction Cache
602 .B PERF_COUNT_HW_CACHE_LL
603 for measuring Last-Level Cache
605 .B PERF_COUNT_HW_CACHE_DTLB
606 for measuring the Data TLB
608 .B PERF_COUNT_HW_CACHE_ITLB
609 for measuring the Instruction TLB
611 .B PERF_COUNT_HW_CACHE_BPU
612 for measuring the branch prediction unit
614 .BR PERF_COUNT_HW_CACHE_NODE " (since Linux 3.1)"
615 .\" commit 89d6c0b5bdbb1927775584dcf532d98b3efe1477
616 for measuring local memory accesses
620 .I perf_hw_cache_op_id
621 is one of:
622 .RS 4
624 .B PERF_COUNT_HW_CACHE_OP_READ
625 for read accesses
627 .B PERF_COUNT_HW_CACHE_OP_WRITE
628 for write accesses
630 .B PERF_COUNT_HW_CACHE_OP_PREFETCH
631 for prefetch accesses
635 .I perf_hw_cache_op_result_id
636 is one of:
637 .RS 4
639 .B PERF_COUNT_HW_CACHE_RESULT_ACCESS
640 to measure accesses
642 .B PERF_COUNT_HW_CACHE_RESULT_MISS
643 to measure misses
648 .I type
650 .BR PERF_TYPE_RAW ,
651 then a custom "raw"
652 .I config
653 value is needed.
654 Most CPUs support events that are not covered by the "generalized" events.
655 These are implementation defined; see your CPU manual (for example
656 the Intel Volume 3B documentation or the AMD BIOS and Kernel Developer
657 Guide).
658 The libpfm4 library can be used to translate from the name in the
659 architectural manuals to the raw hex value
660 .BR perf_event_open ()
661 expects in this field.
664 .I type
666 .BR PERF_TYPE_BREAKPOINT ,
667 then leave
668 .I config
669 set to zero.
670 Its parameters are set in other places.
673 .I type
675 .B kprobe
677 .BR uprobe ,
679 .I retprobe
680 (bit 0 of
681 .IR config ,
683 .IR /sys/bus/event_source/devices/[k,u]probe/format/retprobe )
684 for kretprobe/uretprobe.
685 See fields
686 .IR kprobe_func ,
687 .IR uprobe_path ,
688 .IR kprobe_addr ,
690 .I probe_offset
691 for more details.
694 .IR kprobe_func ", " uprobe_path ", " kprobe_addr ", and " probe_offset
695 These fields describe the kprobe/uprobe for dynamic PMUs
696 .B kprobe
698 .BR uprobe .
700 .BR kprobe :
702 .I kprobe_func
704 .IR probe_offset ,
705 or use
706 .I kprobe_addr
707 and leave
708 .I kprobe_func
709 as NULL.
711 .BR uprobe :
713 .I uprobe_path
715 .IR probe_offset .
717 .IR sample_period ", " sample_freq
718 A "sampling" event is one that generates an overflow notification
719 every N events, where N is given by
720 .IR sample_period .
721 A sampling event has
722 .IR sample_period " > 0."
723 When an overflow occurs, requested data is recorded
724 in the mmap buffer.
726 .I sample_type
727 field controls what data is recorded on each overflow.
729 .I sample_freq
730 can be used if you wish to use frequency rather than period.
731 In this case, you set the
732 .I freq
733 flag.
734 The kernel will adjust the sampling period
735 to try and achieve the desired rate.
736 The rate of adjustment is a
737 timer tick.
739 .I sample_type
740 The various bits in this field specify which values to include
741 in the sample.
742 They will be recorded in a ring-buffer,
743 which is available to user space using
744 .BR mmap (2).
745 The order in which the values are saved in the
746 sample are documented in the MMAP Layout subsection below;
747 it is not the
748 .I "enum perf_event_sample_format"
749 order.
752 .B PERF_SAMPLE_IP
753 Records instruction pointer.
755 .B PERF_SAMPLE_TID
756 Records the process and thread IDs.
758 .B PERF_SAMPLE_TIME
759 Records a timestamp.
761 .B PERF_SAMPLE_ADDR
762 Records an address, if applicable.
764 .B PERF_SAMPLE_READ
765 Record counter values for all events in a group, not just the group leader.
767 .B PERF_SAMPLE_CALLCHAIN
768 Records the callchain (stack backtrace).
770 .B PERF_SAMPLE_ID
771 Records a unique ID for the opened event's group leader.
773 .B PERF_SAMPLE_CPU
774 Records CPU number.
776 .B PERF_SAMPLE_PERIOD
777 Records the current sampling period.
779 .B PERF_SAMPLE_STREAM_ID
780 Records a unique ID for the opened event.
781 Unlike
782 .B PERF_SAMPLE_ID
783 the actual ID is returned, not the group leader.
784 This ID is the same as the one returned by
785 .BR PERF_FORMAT_ID .
787 .B PERF_SAMPLE_RAW
788 Records additional data, if applicable.
789 Usually returned by tracepoint events.
791 .BR PERF_SAMPLE_BRANCH_STACK " (since Linux 3.4)"
792 .\" commit bce38cd53e5ddba9cb6d708c4ef3d04a4016ec7e
793 This provides a record of recent branches, as provided
794 by CPU branch sampling hardware (such as Intel Last Branch Record).
795 Not all hardware supports this feature.
797 See the
798 .I branch_sample_type
799 field for how to filter which branches are reported.
801 .BR PERF_SAMPLE_REGS_USER " (since Linux 3.7)"
802 .\" commit 4018994f3d8785275ef0e7391b75c3462c029e56
803 Records the current user-level CPU register state
804 (the values in the process before the kernel was called).
806 .BR PERF_SAMPLE_STACK_USER " (since Linux 3.7)"
807 .\" commit c5ebcedb566ef17bda7b02686e0d658a7bb42ee7
808 Records the user level stack, allowing stack unwinding.
810 .BR PERF_SAMPLE_WEIGHT " (since Linux 3.10)"
811 .\" commit c3feedf2aaf9ac8bad6f19f5d21e4ee0b4b87e9c
812 Records a hardware provided weight value that expresses how
813 costly the sampled event was.
814 This allows the hardware to highlight expensive events in
815 a profile.
817 .BR PERF_SAMPLE_DATA_SRC " (since Linux 3.10)"
818 .\" commit d6be9ad6c960f43800a6f118932bc8a5a4eadcd1
819 Records the data source: where in the memory hierarchy
820 the data associated with the sampled instruction came from.
821 This is available only if the underlying hardware
822 supports this feature.
824 .BR PERF_SAMPLE_IDENTIFIER " (since Linux 3.12)"
825 .\" commit ff3d527cebc1fa3707c617bfe9e74f53fcfb0955
826 Places the
827 .B SAMPLE_ID
828 value in a fixed position in the record,
829 either at the beginning (for sample events) or at the end
830 (if a non-sample event).
832 This was necessary because a sample stream may have
833 records from various different event sources with different
834 .I sample_type
835 settings.
836 Parsing the event stream properly was not possible because the
837 format of the record was needed to find
838 .BR SAMPLE_ID ,
840 the format could not be found without knowing what
841 event the sample belonged to (causing a circular
842 dependency).
845 .B PERF_SAMPLE_IDENTIFIER
846 setting makes the event stream always parsable
847 by putting
848 .B SAMPLE_ID
849 in a fixed location, even though
850 it means having duplicate
851 .B SAMPLE_ID
852 values in records.
854 .BR PERF_SAMPLE_TRANSACTION " (since Linux 3.13)"
855 .\" commit fdfbbd07e91f8fe387140776f3fd94605f0c89e5
856 Records reasons for transactional memory abort events
857 (for example, from Intel TSX transactional memory support).
860 .I precise_ip
861 setting must be greater than 0 and a transactional memory abort
862 event must be measured or no values will be recorded.
863 Also note that some perf_event measurements, such as sampled
864 cycle counting, may cause extraneous aborts (by causing an
865 interrupt during a transaction).
867 .BR PERF_SAMPLE_REGS_INTR " (since Linux 3.19)"
868 .\" commit 60e2364e60e86e81bc6377f49779779e6120977f
869 Records a subset of the current CPU register state
870 as specified by
871 .IR sample_regs_intr .
872 Unlike
873 .B PERF_SAMPLE_REGS_USER
874 the register values will return kernel register
875 state if the overflow happened while kernel
876 code is running.
877 If the CPU supports hardware sampling of
878 register state (i.e., PEBS on Intel x86) and
879 .I precise_ip
880 is set higher than zero then the register
881 values returned are those captured by
882 hardware at the time of the sampled
883 instruction's retirement.
885 .BR PERF_SAMPLE_PHYS_ADDR " (since Linux 4.13)"
886 .\" commit fc7ce9c74c3ad232b084d80148654f926d01ece7
887 Records physical address of data like in
888 .BR PERF_SAMPLE_ADDR .
890 .BR PERF_SAMPLE_CGROUP " (since Linux 5.7)"
891 .\" commit 96aaab686505c449e24d76e76507290dcc30e008
892 Records (perf_event) cgroup ID of the process.
893 This corresponds to the
894 .I id
895 field in the
896 .B PERF_RECORD_CGROUP
897 event.
900 .I read_format
901 This field specifies the format of the data returned by
902 .BR read (2)
903 on a
904 .BR perf_event_open ()
905 file descriptor.
908 .B PERF_FORMAT_TOTAL_TIME_ENABLED
909 Adds the 64-bit
910 .I time_enabled
911 field.
912 This can be used to calculate estimated totals if
913 the PMU is overcommitted and multiplexing is happening.
915 .B PERF_FORMAT_TOTAL_TIME_RUNNING
916 Adds the 64-bit
917 .I time_running
918 field.
919 This can be used to calculate estimated totals if
920 the PMU is overcommitted and multiplexing is happening.
922 .B PERF_FORMAT_ID
923 Adds a 64-bit unique value that corresponds to the event group.
925 .B PERF_FORMAT_GROUP
926 Allows all counter values in an event group to be read with one read.
929 .I disabled
931 .I disabled
932 bit specifies whether the counter starts out disabled or enabled.
933 If disabled, the event can later be enabled by
934 .BR ioctl (2),
935 .BR prctl (2),
937 .IR enable_on_exec .
939 When creating an event group, typically the group leader is initialized
940 with
941 .I disabled
942 set to 1 and any child events are initialized with
943 .I disabled
944 set to 0.
945 Despite
946 .I disabled
947 being 0, the child events will not start until the group leader
948 is enabled.
950 .I inherit
952 .I inherit
953 bit specifies that this counter should count events of child
954 tasks as well as the task specified.
955 This applies only to new children, not to any existing children at
956 the time the counter is created (nor to any new children of
957 existing children).
959 Inherit does not work for some combinations of
960 .IR read_format
961 values, such as
962 .BR PERF_FORMAT_GROUP .
964 .I pinned
966 .I pinned
967 bit specifies that the counter should always be on the CPU if at all
968 possible.
969 It applies only to hardware counters and only to group leaders.
970 If a pinned counter cannot be put onto the CPU (e.g., because there are
971 not enough hardware counters or because of a conflict with some other
972 event), then the counter goes into an 'error' state, where reads
973 return end-of-file (i.e.,
974 .BR read (2)
975 returns 0) until the counter is subsequently enabled or disabled.
977 .I exclusive
979 .I exclusive
980 bit specifies that when this counter's group is on the CPU,
981 it should be the only group using the CPU's counters.
982 In the future this may allow monitoring programs to
983 support PMU features that need to run alone so that they do not
984 disrupt other hardware counters.
986 Note that many unexpected situations may prevent events with the
987 .I exclusive
988 bit set from ever running.
989 This includes any users running a system-wide
990 measurement as well as any kernel use of the performance counters
991 (including the commonly enabled NMI Watchdog Timer interface).
993 .I exclude_user
994 If this bit is set, the count excludes events that happen in user space.
996 .I exclude_kernel
997 If this bit is set, the count excludes events that happen in kernel space.
999 .I exclude_hv
1000 If this bit is set, the count excludes events that happen in the
1001 hypervisor.
1002 This is mainly for PMUs that have built-in support for handling this
1003 (such as POWER).
1004 Extra support is needed for handling hypervisor measurements on most
1005 machines.
1007 .I exclude_idle
1008 If set, don't count when the CPU is running the idle task.
1009 While you can currently enable this for any event type, it is ignored
1010 for all but software events.
1012 .I mmap
1014 .I mmap
1015 bit enables generation of
1016 .B PERF_RECORD_MMAP
1017 samples for every
1018 .BR mmap (2)
1019 call that has
1020 .B PROT_EXEC
1021 set.
1022 This allows tools to notice new executable code being mapped into
1023 a program (dynamic shared libraries for example)
1024 so that addresses can be mapped back to the original code.
1026 .I comm
1028 .I comm
1029 bit enables tracking of process command name as modified by the
1030 .BR execve (2)
1032 .BR prctl (PR_SET_NAME)
1033 system calls as well as writing to
1034 .IR /proc/self/comm .
1035 If the
1036 .I comm_exec
1037 flag is also successfully set (possible since Linux 3.16),
1038 .\" commit 82b897782d10fcc4930c9d4a15b175348fdd2871
1039 then the misc flag
1040 .B PERF_RECORD_MISC_COMM_EXEC
1041 can be used to differentiate the
1042 .BR execve (2)
1043 case from the others.
1045 .I freq
1046 If this bit is set, then
1047 .I sample_frequency
1049 .I sample_period
1050 is used when setting up the sampling interval.
1052 .I inherit_stat
1053 This bit enables saving of event counts on context switch for
1054 inherited tasks.
1055 This is meaningful only if the
1056 .I inherit
1057 field is set.
1059 .I enable_on_exec
1060 If this bit is set, a counter is automatically
1061 enabled after a call to
1062 .BR execve (2).
1064 .I task
1065 If this bit is set, then
1066 fork/exit notifications are included in the ring buffer.
1068 .I watermark
1069 If set, have an overflow notification happen when we cross the
1070 .I wakeup_watermark
1071 boundary.
1072 Otherwise, overflow notifications happen after
1073 .I wakeup_events
1074 samples.
1076 .IR precise_ip " (since Linux 2.6.35)"
1077 .\" commit ab608344bcbde4f55ec4cd911b686b0ce3eae076
1078 This controls the amount of skid.
1079 Skid is how many instructions
1080 execute between an event of interest happening and the kernel
1081 being able to stop and record the event.
1082 Smaller skid is
1083 better and allows more accurate reporting of which events
1084 correspond to which instructions, but hardware is often limited
1085 with how small this can be.
1087 The possible values of this field are the following:
1089 .IP 0 3
1090 .B SAMPLE_IP
1091 can have arbitrary skid.
1092 .IP 1
1093 .B SAMPLE_IP
1094 must have constant skid.
1095 .IP 2
1096 .B SAMPLE_IP
1097 requested to have 0 skid.
1098 .IP 3
1099 .B SAMPLE_IP
1100 must have 0 skid.
1101 See also the description of
1102 .BR PERF_RECORD_MISC_EXACT_IP .
1105 .IR mmap_data " (since Linux 2.6.36)"
1106 .\" commit 3af9e859281bda7eb7c20b51879cf43aa788ac2e
1107 This is the counterpart of the
1108 .I mmap
1109 field.
1110 This enables generation of
1111 .B PERF_RECORD_MMAP
1112 samples for
1113 .BR mmap (2)
1114 calls that do not have
1115 .B PROT_EXEC
1116 set (for example data and SysV shared memory).
1118 .IR sample_id_all " (since Linux 2.6.38)"
1119 .\" commit c980d1091810df13f21aabbce545fd98f545bbf7
1120 If set, then TID, TIME, ID, STREAM_ID, and CPU can
1121 additionally be included in
1122 .RB non- PERF_RECORD_SAMPLE s
1123 if the corresponding
1124 .I sample_type
1125 is selected.
1128 .B PERF_SAMPLE_IDENTIFIER
1129 is specified, then an additional ID value is included
1130 as the last value to ease parsing the record stream.
1131 This may lead to the
1132 .I id
1133 value appearing twice.
1135 The layout is described by this pseudo-structure:
1137 .in +4n
1139 struct sample_id {
1140     { u32 pid, tid; }   /* if PERF_SAMPLE_TID set */
1141     { u64 time;     }   /* if PERF_SAMPLE_TIME set */
1142     { u64 id;       }   /* if PERF_SAMPLE_ID set */
1143     { u64 stream_id;}   /* if PERF_SAMPLE_STREAM_ID set  */
1144     { u32 cpu, res; }   /* if PERF_SAMPLE_CPU set */
1145     { u64 id;       }   /* if PERF_SAMPLE_IDENTIFIER set */
1150 .IR exclude_host " (since Linux 3.2)"
1151 .\" commit a240f76165e6255384d4bdb8139895fac7988799
1152 When conducting measurements that include processes running
1153 VM instances (i.e., have executed a
1154 .B KVM_RUN
1155 .BR ioctl (2)),
1156 only measure events happening inside a guest instance.
1157 This is only meaningful outside the guests; this setting does
1158 not change counts gathered inside of a guest.
1159 Currently, this functionality is x86 only.
1161 .IR exclude_guest " (since Linux 3.2)"
1162 .\" commit a240f76165e6255384d4bdb8139895fac7988799
1163 When conducting measurements that include processes running
1164 VM instances (i.e., have executed a
1165 .B KVM_RUN
1166 .BR ioctl (2)),
1167 do not measure events happening inside guest instances.
1168 This is only meaningful outside the guests; this setting does
1169 not change counts gathered inside of a guest.
1170 Currently, this functionality is x86 only.
1172 .IR exclude_callchain_kernel " (since Linux 3.7)"
1173 .\" commit d077526485d5c9b12fe85d0b2b3b7041e6bc5f91
1174 Do not include kernel callchains.
1176 .IR exclude_callchain_user " (since Linux 3.7)"
1177 .\" commit d077526485d5c9b12fe85d0b2b3b7041e6bc5f91
1178 Do not include user callchains.
1180 .IR mmap2 " (since Linux 3.16)"
1181 .\" commit 13d7a2410fa637f450a29ecb515ac318ee40c741
1182 .\" This is tricky; was committed during 3.12 development
1183 .\" but right before release was disabled.
1184 .\" So while you could select mmap2 starting with 3.12
1185 .\" it did not work until 3.16
1186 .\" commit a5a5ba72843dd05f991184d6cb9a4471acce1005
1187 Generate an extended executable mmap record that contains enough
1188 additional information to uniquely identify shared mappings.
1190 .I mmap
1191 flag must also be set for this to work.
1193 .IR comm_exec " (since Linux 3.16)"
1194 .\" commit 82b897782d10fcc4930c9d4a15b175348fdd2871
1195 This is purely a feature-detection flag, it does not change
1196 kernel behavior.
1197 If this flag can successfully be set, then, when
1198 .I comm
1199 is enabled, the
1200 .B PERF_RECORD_MISC_COMM_EXEC
1201 flag will be set in the
1202 .I misc
1203 field of a comm record header if the rename event being
1204 reported was caused by a call to
1205 .BR execve (2).
1206 This allows tools to distinguish between the various
1207 types of process renaming.
1209 .IR use_clockid " (since Linux 4.1)"
1210 .\" commit 34f439278cef7b1177f8ce24f9fc81dfc6221d3b
1211 This allows selecting which internal Linux clock to use
1212 when generating timestamps via the
1213 .I clockid
1214 field.
1215 This can make it easier to correlate perf sample times with
1216 timestamps generated by other tools.
1218 .IR context_switch " (since Linux 4.3)"
1219 .\" commit 45ac1403f564f411c6a383a2448688ba8dd705a4
1220 This enables the generation of
1221 .B PERF_RECORD_SWITCH
1222 records when a context switch occurs.
1223 It also enables the generation of
1224 .B PERF_RECORD_SWITCH_CPU_WIDE
1225 records when sampling in CPU-wide mode.
1226 This functionality is in addition to existing tracepoint and
1227 software events for measuring context switches.
1228 The advantage of this method is that it will give full
1229 information even with strict
1230 .I perf_event_paranoid
1231 settings.
1233 .IR write_backward " (since Linux 4.6)"
1234 .\" commit 9ecda41acb971ebd07c8fb35faf24005c0baea12
1235 This causes the ring buffer to be written from the end to the beginning.
1236 This is to support reading from overwritable ring buffer.
1238 .IR namespaces " (since Linux 4.11)"
1239 .\" commit e422267322cd319e2695a535e47c5b1feeac45eb
1240 This enables the generation of
1241 .B PERF_RECORD_NAMESPACES
1242 records when a task enters a new namespace.
1243 Each namespace has a combination of device and inode numbers.
1245 .IR ksymbol " (since Linux 5.0)"
1246 .\" commit 76193a94522f1d4edf2447a536f3f796ce56343b
1247 This enables the generation of
1248 .B PERF_RECORD_KSYMBOL
1249 records when new kernel symbols are registered or unregistered.
1250 This is analyzing dynamic kernel functions like eBPF.
1252 .IR bpf_event " (since Linux 5.0)"
1253 .\" commit 6ee52e2a3fe4ea35520720736e6791df1fb67106
1254 This enables the generation of
1255 .B PERF_RECORD_BPF_EVENT
1256 records when an eBPF program is loaded or unloaded.
1258 .IR auxevent " (since Linux 5.4)"
1259 .\" commit ab43762ef010967e4ccd53627f70a2eecbeafefb
1260 This allows normal (non-AUX) events to generate data for AUX events
1261 if the hardware supports it.
1263 .IR cgroup " (since Linux 5.7)"
1264 .\" commit 96aaab686505c449e24d76e76507290dcc30e008
1265 This enables the generation of
1266 .B PERF_RECORD_CGROUP
1267 records when a new cgroup is created (and activated).
1269 .IR text_poke " (since Linux 5.8)"
1270 .\" commit e17d43b93e544f5016c0251d2074c15568d5d963
1271 This enables the generation of
1272 .B PERF_RECORD_TEXT_POKE
1273 records when there's a change to the kernel text
1274 (i.e., self-modifying code).
1276 .IR wakeup_events ", " wakeup_watermark
1277 This union sets how many samples
1278 .RI ( wakeup_events )
1279 or bytes
1280 .RI ( wakeup_watermark )
1281 happen before an overflow notification happens.
1282 Which one is used is selected by the
1283 .I watermark
1284 bit flag.
1286 .I wakeup_events
1287 counts only
1288 .B PERF_RECORD_SAMPLE
1289 record types.
1290 To receive overflow notification for all
1291 .B PERF_RECORD
1292 types choose watermark and set
1293 .I wakeup_watermark
1294 to 1.
1296 Prior to Linux 3.0, setting
1297 .\" commit f506b3dc0ec454a16d40cab9ee5d75435b39dc50
1298 .I wakeup_events
1299 to 0 resulted in no overflow notifications;
1300 more recent kernels treat 0 the same as 1.
1302 .IR bp_type " (since Linux 2.6.33)"
1303 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
1304 This chooses the breakpoint type.
1305 It is one of:
1308 .B HW_BREAKPOINT_EMPTY
1309 No breakpoint.
1311 .B HW_BREAKPOINT_R
1312 Count when we read the memory location.
1314 .B HW_BREAKPOINT_W
1315 Count when we write the memory location.
1317 .B HW_BREAKPOINT_RW
1318 Count when we read or write the memory location.
1320 .B HW_BREAKPOINT_X
1321 Count when we execute code at the memory location.
1323 The values can be combined via a bitwise or, but the
1324 combination of
1325 .B HW_BREAKPOINT_R
1327 .B HW_BREAKPOINT_W
1328 with
1329 .B HW_BREAKPOINT_X
1330 is not allowed.
1333 .IR bp_addr " (since Linux 2.6.33)"
1334 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
1335 This is the address of the breakpoint.
1336 For execution breakpoints, this is the memory address of the instruction
1337 of interest; for read and write breakpoints, it is the memory address
1338 of the memory location of interest.
1340 .IR config1 " (since Linux 2.6.39)"
1341 .\" commit a7e3ed1e470116c9d12c2f778431a481a6be8ab6
1342 .I config1
1343 is used for setting events that need an extra register or otherwise
1344 do not fit in the regular config field.
1345 Raw OFFCORE_EVENTS on Nehalem/Westmere/SandyBridge use this field
1346 on Linux 3.3 and later kernels.
1348 .IR bp_len " (since Linux 2.6.33)"
1349 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
1350 .I bp_len
1351 is the length of the breakpoint being measured if
1352 .I type
1354 .BR PERF_TYPE_BREAKPOINT .
1355 Options are
1356 .BR HW_BREAKPOINT_LEN_1 ,
1357 .BR HW_BREAKPOINT_LEN_2 ,
1358 .BR HW_BREAKPOINT_LEN_4 ,
1360 .BR HW_BREAKPOINT_LEN_8 .
1361 For an execution breakpoint, set this to
1362 .IR sizeof(long) .
1364 .IR config2 " (since Linux 2.6.39)"
1365 .\" commit a7e3ed1e470116c9d12c2f778431a481a6be8ab6
1366 .I config2
1367 is a further extension of the
1368 .I config1
1369 field.
1371 .IR branch_sample_type " (since Linux 3.4)"
1372 .\" commit bce38cd53e5ddba9cb6d708c4ef3d04a4016ec7e
1374 .B PERF_SAMPLE_BRANCH_STACK
1375 is enabled, then this specifies what branches to include
1376 in the branch record.
1378 The first part of the value is the privilege level, which
1379 is a combination of one of the values listed below.
1380 If the user does not set privilege level explicitly, the kernel
1381 will use the event's privilege level.
1382 Event and branch privilege levels do not have to match.
1385 .B PERF_SAMPLE_BRANCH_USER
1386 Branch target is in user space.
1388 .B PERF_SAMPLE_BRANCH_KERNEL
1389 Branch target is in kernel space.
1391 .B PERF_SAMPLE_BRANCH_HV
1392 Branch target is in hypervisor.
1394 .B PERF_SAMPLE_BRANCH_PLM_ALL
1395 A convenience value that is the three preceding values ORed together.
1397 In addition to the privilege value, at least one or more of the
1398 following bits must be set.
1400 .B PERF_SAMPLE_BRANCH_ANY
1401 Any branch type.
1403 .B PERF_SAMPLE_BRANCH_ANY_CALL
1404 Any call branch (includes direct calls, indirect calls, and far jumps).
1406 .B PERF_SAMPLE_BRANCH_IND_CALL
1407 Indirect calls.
1409 .BR PERF_SAMPLE_BRANCH_CALL " (since Linux 4.4)"
1410 .\" commit c229bf9dc179d2023e185c0f705bdf68484c1e73
1411 Direct calls.
1413 .B PERF_SAMPLE_BRANCH_ANY_RETURN
1414 Any return branch.
1416 .BR PERF_SAMPLE_BRANCH_IND_JUMP " (since Linux 4.2)"
1417 .\" commit c9fdfa14c3792c0160849c484e83aa57afd80ccc
1418 Indirect jumps.
1420 .BR PERF_SAMPLE_BRANCH_COND " (since Linux 3.16)"
1421 .\" commit bac52139f0b7ab31330e98fd87fc5a2664951050
1422 Conditional branches.
1424 .BR PERF_SAMPLE_BRANCH_ABORT_TX " (since Linux 3.11)"
1425 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
1426 Transactional memory aborts.
1428 .BR PERF_SAMPLE_BRANCH_IN_TX " (since Linux 3.11)"
1429 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
1430 Branch in transactional memory transaction.
1432 .BR PERF_SAMPLE_BRANCH_NO_TX " (since Linux 3.11)"
1433 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
1434 Branch not in transactional memory transaction.
1435 .BR PERF_SAMPLE_BRANCH_CALL_STACK " (since Linux 4.1)"
1436 .\" commit 2c44b1936bb3b135a3fac8b3493394d42e51cf70
1437 Branch is part of a hardware-generated call stack.
1438 This requires hardware support, currently only found
1439 on Intel x86 Haswell or newer.
1442 .IR sample_regs_user " (since Linux 3.7)"
1443 .\" commit 4018994f3d8785275ef0e7391b75c3462c029e56
1444 This bit mask defines the set of user CPU registers to dump on samples.
1445 The layout of the register mask is architecture-specific and
1446 is described in the kernel header file
1447 .IR arch/ARCH/include/uapi/asm/perf_regs.h .
1449 .IR sample_stack_user " (since Linux 3.7)"
1450 .\" commit c5ebcedb566ef17bda7b02686e0d658a7bb42ee7
1451 This defines the size of the user stack to dump if
1452 .B PERF_SAMPLE_STACK_USER
1453 is specified.
1455 .IR clockid " (since Linux 4.1)"
1456 .\" commit 34f439278cef7b1177f8ce24f9fc81dfc6221d3b
1458 .I use_clockid
1459 is set, then this field selects which internal Linux timer to
1460 use for timestamps.
1461 The available timers are defined in
1462 .IR linux/time.h ,
1463 with
1464 .BR CLOCK_MONOTONIC ,
1465 .BR CLOCK_MONOTONIC_RAW ,
1466 .BR CLOCK_REALTIME ,
1467 .BR CLOCK_BOOTTIME ,
1469 .B CLOCK_TAI
1470 currently supported.
1472 .IR aux_watermark " (since Linux 4.1)"
1473 .\" commit 1a5941312414c71dece6717da9a0fa1303127afa
1474 This specifies how much data is required to trigger a
1475 .B PERF_RECORD_AUX
1476 sample.
1478 .IR sample_max_stack " (since Linux 4.8)"
1479 .\" commit 97c79a38cd454602645f0470ffb444b3b75ce574
1480 When
1481 .I sample_type
1482 includes
1483 .BR PERF_SAMPLE_CALLCHAIN ,
1484 this field specifies how many stack frames to report when
1485 generating the callchain.
1486 .SS Reading results
1487 Once a
1488 .BR perf_event_open ()
1489 file descriptor has been opened, the values
1490 of the events can be read from the file descriptor.
1491 The values that are there are specified by the
1492 .I read_format
1493 field in the
1494 .I attr
1495 structure at open time.
1497 If you attempt to read into a buffer that is not big enough to hold the
1498 data, the error
1499 .B ENOSPC
1500 results.
1502 Here is the layout of the data returned by a read:
1503 .IP * 2
1505 .B PERF_FORMAT_GROUP
1506 was specified to allow reading all events in a group at once:
1508 .in +4n
1510 struct read_format {
1511     u64 nr;            /* The number of events */
1512     u64 time_enabled;  /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
1513     u64 time_running;  /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
1514     struct {
1515         u64 value;     /* The value of the event */
1516         u64 id;        /* if PERF_FORMAT_ID */
1517     } values[nr];
1521 .IP *
1523 .B PERF_FORMAT_GROUP
1525 .I not
1526 specified:
1528 .in +4n
1530 struct read_format {
1531     u64 value;         /* The value of the event */
1532     u64 time_enabled;  /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
1533     u64 time_running;  /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
1534     u64 id;            /* if PERF_FORMAT_ID */
1539 The values read are as follows:
1541 .I nr
1542 The number of events in this file descriptor.
1543 Available only if
1544 .B PERF_FORMAT_GROUP
1545 was specified.
1547 .IR time_enabled ", " time_running
1548 Total time the event was enabled and running.
1549 Normally these values are the same.
1550 Multiplexing happens if the number of events is more than the
1551 number of available PMU counter slots.
1552 In that case the events run only part of the time and the
1553 .I time_enabled
1555 .I time running
1556 values can be used to scale an estimated value for the count.
1558 .I value
1559 An unsigned 64-bit value containing the counter result.
1561 .I id
1562 A globally unique value for this particular event; only present if
1563 .B PERF_FORMAT_ID
1564 was specified in
1565 .IR read_format .
1566 .SS MMAP layout
1567 When using
1568 .BR perf_event_open ()
1569 in sampled mode, asynchronous events
1570 (like counter overflow or
1571 .B PROT_EXEC
1572 mmap tracking)
1573 are logged into a ring-buffer.
1574 This ring-buffer is created and accessed through
1575 .BR mmap (2).
1577 The mmap size should be 1+2^n pages, where the first page is a
1578 metadata page
1579 .RI ( "struct perf_event_mmap_page" )
1580 that contains various
1581 bits of information such as where the ring-buffer head is.
1583 Before kernel 2.6.39, there is a bug that means you must allocate an mmap
1584 ring buffer when sampling even if you do not plan to access it.
1586 The structure of the first metadata mmap page is as follows:
1588 .in +4n
1590 struct perf_event_mmap_page {
1591     __u32 version;        /* version number of this structure */
1592     __u32 compat_version; /* lowest version this is compat with */
1593     __u32 lock;           /* seqlock for synchronization */
1594     __u32 index;          /* hardware counter identifier */
1595     __s64 offset;         /* add to hardware counter value */
1596     __u64 time_enabled;   /* time event active */
1597     __u64 time_running;   /* time event on CPU */
1598     union {
1599         __u64   capabilities;
1600         struct {
1601             __u64 cap_usr_time / cap_usr_rdpmc / cap_bit0 : 1,
1602                   cap_bit0_is_deprecated : 1,
1603                   cap_user_rdpmc         : 1,
1604                   cap_user_time          : 1,
1605                   cap_user_time_zero     : 1,
1606         };
1607     };
1608     __u16 pmc_width;
1609     __u16 time_shift;
1610     __u32 time_mult;
1611     __u64 time_offset;
1612     __u64 __reserved[120];   /* Pad to 1 k */
1613     __u64 data_head;         /* head in the data section */
1614     __u64 data_tail;         /* user\-space written tail */
1615     __u64 data_offset;       /* where the buffer starts */
1616     __u64 data_size;         /* data buffer size */
1617     __u64 aux_head;
1618     __u64 aux_tail;
1619     __u64 aux_offset;
1620     __u64 aux_size;
1626 The following list describes the fields in the
1627 .I perf_event_mmap_page
1628 structure in more detail:
1630 .I version
1631 Version number of this structure.
1633 .I compat_version
1634 The lowest version this is compatible with.
1636 .I lock
1637 A seqlock for synchronization.
1639 .I index
1640 A unique hardware counter identifier.
1642 .I offset
1643 When using rdpmc for reads this offset value
1644 must be added to the one returned by rdpmc to get
1645 the current total event count.
1647 .I time_enabled
1648 Time the event was active.
1650 .I time_running
1651 Time the event was running.
1653 .IR cap_usr_time " / " cap_usr_rdpmc " / " cap_bit0 " (since Linux 3.4)"
1654 .\" commit c7206205d00ab375839bd6c7ddb247d600693c09
1655 There was a bug in the definition of
1656 .I cap_usr_time
1658 .I cap_usr_rdpmc
1659 from Linux 3.4 until Linux 3.11.
1660 Both bits were defined to point to the same location, so it was
1661 impossible to know if
1662 .I cap_usr_time
1664 .I cap_usr_rdpmc
1665 were actually set.
1667 Starting with Linux 3.12, these are renamed to
1668 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1669 .I cap_bit0
1670 and you should use the
1671 .I cap_user_time
1673 .I cap_user_rdpmc
1674 fields instead.
1676 .IR cap_bit0_is_deprecated " (since Linux 3.12)"
1677 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1678 If set, this bit indicates that the kernel supports
1679 the properly separated
1680 .I cap_user_time
1682 .I cap_user_rdpmc
1683 bits.
1685 If not-set, it indicates an older kernel where
1686 .I cap_usr_time
1688 .I cap_usr_rdpmc
1689 map to the same bit and thus both features should
1690 be used with caution.
1692 .IR cap_user_rdpmc " (since Linux 3.12)"
1693 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1694 If the hardware supports user-space read of performance counters
1695 without syscall (this is the "rdpmc" instruction on x86), then
1696 the following code can be used to do a read:
1698 .in +4n
1700 u32 seq, time_mult, time_shift, idx, width;
1701 u64 count, enabled, running;
1702 u64 cyc, time_offset;
1704 do {
1705     seq = pc\->lock;
1706     barrier();
1707     enabled = pc\->time_enabled;
1708     running = pc\->time_running;
1710     if (pc\->cap_usr_time && enabled != running) {
1711         cyc = rdtsc();
1712         time_offset = pc\->time_offset;
1713         time_mult   = pc\->time_mult;
1714         time_shift  = pc\->time_shift;
1715     }
1717     idx = pc\->index;
1718     count = pc\->offset;
1720     if (pc\->cap_usr_rdpmc && idx) {
1721         width = pc\->pmc_width;
1722         count += rdpmc(idx \- 1);
1723     }
1725     barrier();
1726 } while (pc\->lock != seq);
1730 .IR cap_user_time " (since Linux 3.12)"
1731 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1732 This bit indicates the hardware has a constant, nonstop
1733 timestamp counter (TSC on x86).
1735 .IR cap_user_time_zero " (since Linux 3.12)"
1736 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1737 Indicates the presence of
1738 .I time_zero
1739 which allows mapping timestamp values to
1740 the hardware clock.
1742 .I pmc_width
1744 .IR cap_usr_rdpmc ,
1745 this field provides the bit-width of the value
1746 read using the rdpmc or equivalent instruction.
1747 This can be used to sign extend the result like:
1749 .in +4n
1751 pmc <<= 64 \- pmc_width;
1752 pmc >>= 64 \- pmc_width; // signed shift right
1753 count += pmc;
1757 .IR time_shift ", " time_mult ", " time_offset
1760 .IR cap_usr_time ,
1761 these fields can be used to compute the time
1762 delta since
1763 .I time_enabled
1764 (in nanoseconds) using rdtsc or similar.
1766 .in +4n
1768 u64 quot, rem;
1769 u64 delta;
1771 quot  = cyc >> time_shift;
1772 rem   = cyc & (((u64)1 << time_shift) \- 1);
1773 delta = time_offset + quot * time_mult +
1774         ((rem * time_mult) >> time_shift);
1778 Where
1779 .IR time_offset ,
1780 .IR time_mult ,
1781 .IR time_shift ,
1783 .I cyc
1784 are read in the
1785 seqcount loop described above.
1786 This delta can then be added to
1787 enabled and possible running (if idx), improving the scaling:
1789 .in +4n
1791 enabled += delta;
1792 if (idx)
1793     running += delta;
1794 quot  = count / running;
1795 rem   = count % running;
1796 count = quot * enabled + (rem * enabled) / running;
1800 .IR time_zero " (since Linux 3.12)"
1801 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1804 .I cap_usr_time_zero
1805 is set, then the hardware clock (the TSC timestamp counter on x86)
1806 can be calculated from the
1807 .IR time_zero ,
1808 .IR time_mult ,
1810 .I time_shift
1811 values:
1813 .in +4n
1815 time = timestamp \- time_zero;
1816 quot = time / time_mult;
1817 rem  = time % time_mult;
1818 cyc  = (quot << time_shift) + (rem << time_shift) / time_mult;
1822 And vice versa:
1824 .in +4n
1826 quot = cyc >> time_shift;
1827 rem  = cyc & (((u64)1 << time_shift) \- 1);
1828 timestamp = time_zero + quot * time_mult +
1829             ((rem * time_mult) >> time_shift);
1833 .I data_head
1834 This points to the head of the data section.
1835 The value continuously increases, it does not wrap.
1836 The value needs to be manually wrapped by the size of the mmap buffer
1837 before accessing the samples.
1839 On SMP-capable platforms, after reading the
1840 .I data_head
1841 value,
1842 user space should issue an rmb().
1844 .I data_tail
1845 When the mapping is
1846 .BR PROT_WRITE ,
1848 .I data_tail
1849 value should be written by user space to reflect the last read data.
1850 In this case, the kernel will not overwrite unread data.
1852 .IR data_offset " (since Linux 4.1)"
1853 .\" commit e8c6deac69629c0cb97c3d3272f8631ef17f8f0f
1854 Contains the offset of the location in the mmap buffer
1855 where perf sample data begins.
1857 .IR data_size " (since Linux 4.1)"
1858 .\" commit e8c6deac69629c0cb97c3d3272f8631ef17f8f0f
1859 Contains the size of the perf sample region within
1860 the mmap buffer.
1862 .IR aux_head ", " aux_tail ", " aux_offset ", " aux_size " (since Linux 4.1)"
1863 .\" commit 45bfb2e50471abbbfd83d40d28c986078b0d24ff
1864 The AUX region allows
1865 .BR mmap (2)-ing
1866 a separate sample buffer for
1867 high-bandwidth data streams (separate from the main perf sample buffer).
1868 An example of a high-bandwidth stream is instruction tracing support,
1869 as is found in newer Intel processors.
1871 To set up an AUX area, first
1872 .I aux_offset
1873 needs to be set with an offset greater than
1874 .IR data_offset + data_size
1876 .I aux_size
1877 needs to be set to the desired buffer size.
1878 The desired offset and size must be page aligned, and the size
1879 must be a power of two.
1880 These values are then passed to mmap in order to map the AUX buffer.
1881 Pages in the AUX buffer are included as part of the
1882 .B RLIMIT_MEMLOCK
1883 resource limit (see
1884 .BR setrlimit (2)),
1885 and also as part of the
1886 .I perf_event_mlock_kb
1887 allowance.
1889 By default, the AUX buffer will be truncated if it will not fit
1890 in the available space in the ring buffer.
1891 If the AUX buffer is mapped as a read only buffer, then it will
1892 operate in ring buffer mode where old data will be overwritten
1893 by new.
1894 In overwrite mode, it might not be possible to infer where the
1895 new data began, and it is the consumer's job to disable
1896 measurement while reading to avoid possible data races.
1899 .I aux_head
1901 .I aux_tail
1902 ring buffer pointers have the same behavior and ordering
1903 rules as the previous described
1904 .I data_head
1906 .IR data_tail .
1908 The following 2^n ring-buffer pages have the layout described below.
1911 .I perf_event_attr.sample_id_all
1912 is set, then all event types will
1913 have the sample_type selected fields related to where/when (identity)
1914 an event took place (TID, TIME, ID, CPU, STREAM_ID) described in
1915 .B PERF_RECORD_SAMPLE
1916 below, it will be stashed just after the
1917 .I perf_event_header
1918 and the fields already present for the existing
1919 fields, that is, at the end of the payload.
1920 This allows a newer perf.data
1921 file to be supported by older perf tools, with the new optional
1922 fields being ignored.
1924 The mmap values start with a header:
1926 .in +4n
1928 struct perf_event_header {
1929     __u32   type;
1930     __u16   misc;
1931     __u16   size;
1936 Below, we describe the
1937 .I perf_event_header
1938 fields in more detail.
1939 For ease of reading,
1940 the fields with shorter descriptions are presented first.
1942 .I size
1943 This indicates the size of the record.
1945 .I misc
1947 .I misc
1948 field contains additional information about the sample.
1950 The CPU mode can be determined from this value by masking with
1951 .B PERF_RECORD_MISC_CPUMODE_MASK
1952 and looking for one of the following (note these are not
1953 bit masks, only one can be set at a time):
1956 .B PERF_RECORD_MISC_CPUMODE_UNKNOWN
1957 Unknown CPU mode.
1959 .B PERF_RECORD_MISC_KERNEL
1960 Sample happened in the kernel.
1962 .B PERF_RECORD_MISC_USER
1963 Sample happened in user code.
1965 .B PERF_RECORD_MISC_HYPERVISOR
1966 Sample happened in the hypervisor.
1968 .BR PERF_RECORD_MISC_GUEST_KERNEL " (since Linux 2.6.35)"
1969 .\" commit 39447b386c846bbf1c56f6403c5282837486200f
1970 Sample happened in the guest kernel.
1972 .B PERF_RECORD_MISC_GUEST_USER " (since Linux 2.6.35)"
1973 .\" commit 39447b386c846bbf1c56f6403c5282837486200f
1974 Sample happened in guest user code.
1978 Since the following three statuses are generated by
1979 different record types, they alias to the same bit:
1981 .BR PERF_RECORD_MISC_MMAP_DATA " (since Linux 3.10)"
1982 .\" commit 2fe85427e3bf65d791700d065132772fc26e4d75
1983 This is set when the mapping is not executable;
1984 otherwise the mapping is executable.
1986 .BR PERF_RECORD_MISC_COMM_EXEC " (since Linux 3.16)"
1987 .\" commit 82b897782d10fcc4930c9d4a15b175348fdd2871
1988 This is set for a
1989 .B PERF_RECORD_COMM
1990 record on kernels more recent than Linux 3.16
1991 if a process name change was caused by an
1992 .BR execve (2)
1993 system call.
1995 .BR PERF_RECORD_MISC_SWITCH_OUT " (since Linux 4.3)"
1996 .\" commit 45ac1403f564f411c6a383a2448688ba8dd705a4
1997 When a
1998 .B PERF_RECORD_SWITCH
2000 .B PERF_RECORD_SWITCH_CPU_WIDE
2001 record is generated, this bit indicates that the
2002 context switch is away from the current process
2003 (instead of into the current process).
2007 In addition, the following bits can be set:
2009 .B PERF_RECORD_MISC_EXACT_IP
2010 This indicates that the content of
2011 .B PERF_SAMPLE_IP
2012 points
2013 to the actual instruction that triggered the event.
2014 See also
2015 .IR perf_event_attr.precise_ip .
2017 .BR PERF_RECORD_MISC_EXT_RESERVED " (since Linux 2.6.35)"
2018 .\" commit 1676b8a077c352085d52578fb4f29350b58b6e74
2019 This indicates there is extended data available (currently not used).
2021 .B PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT
2022 .\" commit 930e6fcd2bcce9bcd9d4aa7e755678d33f3fe6f4
2023 This bit is not set by the kernel.
2024 It is reserved for the user-space perf utility to indicate that
2025 .I /proc/i[pid]/maps
2026 parsing was taking too long and was stopped, and thus the mmap
2027 records may be truncated.
2030 .I type
2032 .I type
2033 value is one of the below.
2034 The values in the corresponding record (that follows the header)
2035 depend on the
2036 .I type
2037 selected as shown.
2039 .TP 4
2040 .B PERF_RECORD_MMAP
2041 The MMAP events record the
2042 .B PROT_EXEC
2043 mappings so that we can correlate
2044 user-space IPs to code.
2045 They have the following structure:
2047 .in +4n
2049 struct {
2050     struct perf_event_header header;
2051     u32    pid, tid;
2052     u64    addr;
2053     u64    len;
2054     u64    pgoff;
2055     char   filename[];
2061 .I pid
2062 is the process ID.
2064 .I tid
2065 is the thread ID.
2067 .I addr
2068 is the address of the allocated memory.
2069 .I len
2070 is the length of the allocated memory.
2071 .I pgoff
2072 is the page offset of the allocated memory.
2073 .I filename
2074 is a string describing the backing of the allocated memory.
2077 .B PERF_RECORD_LOST
2078 This record indicates when events are lost.
2080 .in +4n
2082 struct {
2083     struct perf_event_header header;
2084     u64    id;
2085     u64    lost;
2086     struct sample_id sample_id;
2092 .I id
2093 is the unique event ID for the samples that were lost.
2095 .I lost
2096 is the number of events that were lost.
2099 .B PERF_RECORD_COMM
2100 This record indicates a change in the process name.
2102 .in +4n
2104 struct {
2105     struct perf_event_header header;
2106     u32    pid;
2107     u32    tid;
2108     char   comm[];
2109     struct sample_id sample_id;
2115 .I pid
2116 is the process ID.
2118 .I tid
2119 is the thread ID.
2121 .I comm
2122 is a string containing the new name of the process.
2125 .B PERF_RECORD_EXIT
2126 This record indicates a process exit event.
2128 .in +4n
2130 struct {
2131     struct perf_event_header header;
2132     u32    pid, ppid;
2133     u32    tid, ptid;
2134     u64    time;
2135     struct sample_id sample_id;
2140 .BR PERF_RECORD_THROTTLE ", " PERF_RECORD_UNTHROTTLE
2141 This record indicates a throttle/unthrottle event.
2143 .in +4n
2145 struct {
2146     struct perf_event_header header;
2147     u64    time;
2148     u64    id;
2149     u64    stream_id;
2150     struct sample_id sample_id;
2155 .B PERF_RECORD_FORK
2156 This record indicates a fork event.
2158 .in +4n
2160 struct {
2161     struct perf_event_header header;
2162     u32    pid, ppid;
2163     u32    tid, ptid;
2164     u64    time;
2165     struct sample_id sample_id;
2170 .B PERF_RECORD_READ
2171 This record indicates a read event.
2173 .in +4n
2175 struct {
2176     struct perf_event_header header;
2177     u32    pid, tid;
2178     struct read_format values;
2179     struct sample_id sample_id;
2184 .B PERF_RECORD_SAMPLE
2185 This record indicates a sample.
2187 .in +4n
2189 struct {
2190     struct perf_event_header header;
2191     u64    sample_id;   /* if PERF_SAMPLE_IDENTIFIER */
2192     u64    ip;          /* if PERF_SAMPLE_IP */
2193     u32    pid, tid;    /* if PERF_SAMPLE_TID */
2194     u64    time;        /* if PERF_SAMPLE_TIME */
2195     u64    addr;        /* if PERF_SAMPLE_ADDR */
2196     u64    id;          /* if PERF_SAMPLE_ID */
2197     u64    stream_id;   /* if PERF_SAMPLE_STREAM_ID */
2198     u32    cpu, res;    /* if PERF_SAMPLE_CPU */
2199     u64    period;      /* if PERF_SAMPLE_PERIOD */
2200     struct read_format v;
2201                         /* if PERF_SAMPLE_READ */
2202     u64    nr;          /* if PERF_SAMPLE_CALLCHAIN */
2203     u64    ips[nr];     /* if PERF_SAMPLE_CALLCHAIN */
2204     u32    size;        /* if PERF_SAMPLE_RAW */
2205     char   data[size];  /* if PERF_SAMPLE_RAW */
2206     u64    bnr;         /* if PERF_SAMPLE_BRANCH_STACK */
2207     struct perf_branch_entry lbr[bnr];
2208                         /* if PERF_SAMPLE_BRANCH_STACK */
2209     u64    abi;         /* if PERF_SAMPLE_REGS_USER */
2210     u64    regs[weight(mask)];
2211                         /* if PERF_SAMPLE_REGS_USER */
2212     u64    size;        /* if PERF_SAMPLE_STACK_USER */
2213     char   data[size];  /* if PERF_SAMPLE_STACK_USER */
2214     u64    dyn_size;    /* if PERF_SAMPLE_STACK_USER &&
2215                            size != 0 */
2216     u64    weight;      /* if PERF_SAMPLE_WEIGHT */
2217     u64    data_src;    /* if PERF_SAMPLE_DATA_SRC */
2218     u64    transaction; /* if PERF_SAMPLE_TRANSACTION */
2219     u64    abi;         /* if PERF_SAMPLE_REGS_INTR */
2220     u64    regs[weight(mask)];
2221                         /* if PERF_SAMPLE_REGS_INTR */
2222     u64    phys_addr;   /* if PERF_SAMPLE_PHYS_ADDR */
2223     u64    cgroup;      /* if PERF_SAMPLE_CGROUP */
2227 .RS 4
2228 .TP 4
2229 .I sample_id
2231 .B PERF_SAMPLE_IDENTIFIER
2232 is enabled, a 64-bit unique ID is included.
2233 This is a duplication of the
2234 .B PERF_SAMPLE_ID
2235 .I id
2236 value, but included at the beginning of the sample
2237 so parsers can easily obtain the value.
2239 .I ip
2241 .B PERF_SAMPLE_IP
2242 is enabled, then a 64-bit instruction
2243 pointer value is included.
2245 .IR pid ", " tid
2247 .B PERF_SAMPLE_TID
2248 is enabled, then a 32-bit process ID
2249 and 32-bit thread ID are included.
2251 .I time
2253 .B PERF_SAMPLE_TIME
2254 is enabled, then a 64-bit timestamp
2255 is included.
2256 This is obtained via local_clock() which is a hardware timestamp
2257 if available and the jiffies value if not.
2259 .I addr
2261 .B PERF_SAMPLE_ADDR
2262 is enabled, then a 64-bit address is included.
2263 This is usually the address of a tracepoint,
2264 breakpoint, or software event; otherwise the value is 0.
2266 .I id
2268 .B PERF_SAMPLE_ID
2269 is enabled, a 64-bit unique ID is included.
2270 If the event is a member of an event group, the group leader ID is returned.
2271 This ID is the same as the one returned by
2272 .BR PERF_FORMAT_ID .
2274 .I stream_id
2276 .B PERF_SAMPLE_STREAM_ID
2277 is enabled, a 64-bit unique ID is included.
2278 Unlike
2279 .B PERF_SAMPLE_ID
2280 the actual ID is returned, not the group leader.
2281 This ID is the same as the one returned by
2282 .BR PERF_FORMAT_ID .
2284 .IR cpu ", " res
2286 .B PERF_SAMPLE_CPU
2287 is enabled, this is a 32-bit value indicating
2288 which CPU was being used, in addition to a reserved (unused)
2289 32-bit value.
2291 .I period
2293 .B PERF_SAMPLE_PERIOD
2294 is enabled, a 64-bit value indicating
2295 the current sampling period is written.
2297 .I v
2299 .B PERF_SAMPLE_READ
2300 is enabled, a structure of type read_format
2301 is included which has values for all events in the event group.
2302 The values included depend on the
2303 .I read_format
2304 value used at
2305 .BR perf_event_open ()
2306 time.
2308 .IR nr ", " ips[nr]
2310 .B PERF_SAMPLE_CALLCHAIN
2311 is enabled, then a 64-bit number is included
2312 which indicates how many following 64-bit instruction pointers will
2313 follow.
2314 This is the current callchain.
2316 .IR size ", " data[size]
2318 .B PERF_SAMPLE_RAW
2319 is enabled, then a 32-bit value indicating size
2320 is included followed by an array of 8-bit values of length size.
2321 The values are padded with 0 to have 64-bit alignment.
2323 This RAW record data is opaque with respect to the ABI.
2324 The ABI doesn't make any promises with respect to the stability
2325 of its content, it may vary depending
2326 on event, hardware, and kernel version.
2328 .IR bnr ", " lbr[bnr]
2330 .B PERF_SAMPLE_BRANCH_STACK
2331 is enabled, then a 64-bit value indicating
2332 the number of records is included, followed by
2333 .I bnr
2334 .I perf_branch_entry
2335 structures which each include the fields:
2338 .I from
2339 This indicates the source instruction (may not be a branch).
2341 .I to
2342 The branch target.
2344 .I mispred
2345 The branch target was mispredicted.
2347 .I predicted
2348 The branch target was predicted.
2350 .IR in_tx " (since Linux 3.11)"
2351 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
2352 The branch was in a transactional memory transaction.
2354 .IR abort " (since Linux 3.11)"
2355 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
2356 The branch was in an aborted transactional memory transaction.
2358 .IR cycles " (since Linux 4.3)"
2359 .\" commit 71ef3c6b9d4665ee7afbbe4c208a98917dcfc32f
2360 This reports the number of cycles elapsed since the
2361 previous branch stack update.
2363 The entries are from most to least recent, so the first entry
2364 has the most recent branch.
2366 Support for
2367 .IR mispred ,
2368 .IR predicted ,
2370 .I cycles
2371 is optional; if not supported, those
2372 values will be 0.
2374 The type of branches recorded is specified by the
2375 .I branch_sample_type
2376 field.
2379 .IR abi ", " regs[weight(mask)]
2381 .B PERF_SAMPLE_REGS_USER
2382 is enabled, then the user CPU registers are recorded.
2385 .I abi
2386 field is one of
2387 .BR PERF_SAMPLE_REGS_ABI_NONE ,
2388 .BR PERF_SAMPLE_REGS_ABI_32 ,
2390 .BR PERF_SAMPLE_REGS_ABI_64 .
2393 .I regs
2394 field is an array of the CPU registers that were specified by
2396 .I sample_regs_user
2397 attr field.
2398 The number of values is the number of bits set in the
2399 .I sample_regs_user
2400 bit mask.
2402 .IR size ", " data[size] ", " dyn_size
2404 .B PERF_SAMPLE_STACK_USER
2405 is enabled, then the user stack is recorded.
2406 This can be used to generate stack backtraces.
2407 .I size
2408 is the size requested by the user in
2409 .I sample_stack_user
2410 or else the maximum record size.
2411 .I data
2412 is the stack data (a raw dump of the memory pointed to by the
2413 stack pointer at the time of sampling).
2414 .I dyn_size
2415 is the amount of data actually dumped (can be less than
2416 .IR size ).
2417 Note that
2418 .I dyn_size
2419 is omitted if
2420 .I size
2421 is 0.
2423 .I weight
2425 .B PERF_SAMPLE_WEIGHT
2426 is enabled, then a 64-bit value provided by the hardware
2427 is recorded that indicates how costly the event was.
2428 This allows expensive events to stand out more clearly
2429 in profiles.
2431 .I data_src
2433 .B PERF_SAMPLE_DATA_SRC
2434 is enabled, then a 64-bit value is recorded that is made up of
2435 the following fields:
2437 .TP 4
2438 .I mem_op
2439 Type of opcode, a bitwise combination of:
2441 .PD 0
2443 .TP 24
2444 .B PERF_MEM_OP_NA
2445 Not available
2447 .B PERF_MEM_OP_LOAD
2448 Load instruction
2450 .B PERF_MEM_OP_STORE
2451 Store instruction
2453 .B PERF_MEM_OP_PFETCH
2454 Prefetch
2456 .B PERF_MEM_OP_EXEC
2457 Executable code
2461 .I mem_lvl
2462 Memory hierarchy level hit or miss, a bitwise combination of
2463 the following, shifted left by
2464 .BR PERF_MEM_LVL_SHIFT :
2466 .PD 0
2468 .TP 24
2469 .B PERF_MEM_LVL_NA
2470 Not available
2472 .B PERF_MEM_LVL_HIT
2475 .B PERF_MEM_LVL_MISS
2476 Miss
2478 .B PERF_MEM_LVL_L1
2479 Level 1 cache
2481 .B PERF_MEM_LVL_LFB
2482 Line fill buffer
2484 .B PERF_MEM_LVL_L2
2485 Level 2 cache
2487 .B PERF_MEM_LVL_L3
2488 Level 3 cache
2490 .B PERF_MEM_LVL_LOC_RAM
2491 Local DRAM
2493 .B PERF_MEM_LVL_REM_RAM1
2494 Remote DRAM 1 hop
2496 .B PERF_MEM_LVL_REM_RAM2
2497 Remote DRAM 2 hops
2499 .B PERF_MEM_LVL_REM_CCE1
2500 Remote cache 1 hop
2502 .B PERF_MEM_LVL_REM_CCE2
2503 Remote cache 2 hops
2505 .B PERF_MEM_LVL_IO
2506 I/O memory
2508 .B PERF_MEM_LVL_UNC
2509 Uncached memory
2513 .I mem_snoop
2514 Snoop mode, a bitwise combination of the following, shifted left by
2515 .BR PERF_MEM_SNOOP_SHIFT :
2517 .PD 0
2519 .TP 24
2520 .B PERF_MEM_SNOOP_NA
2521 Not available
2523 .B PERF_MEM_SNOOP_NONE
2524 No snoop
2526 .B PERF_MEM_SNOOP_HIT
2527 Snoop hit
2529 .B PERF_MEM_SNOOP_MISS
2530 Snoop miss
2532 .B PERF_MEM_SNOOP_HITM
2533 Snoop hit modified
2537 .I mem_lock
2538 Lock instruction, a bitwise combination of the following, shifted left by
2539 .BR PERF_MEM_LOCK_SHIFT :
2541 .PD 0
2543 .TP 24
2544 .B PERF_MEM_LOCK_NA
2545 Not available
2547 .B PERF_MEM_LOCK_LOCKED
2548 Locked transaction
2552 .I mem_dtlb
2553 TLB access hit or miss, a bitwise combination of the following, shifted
2554 left by
2555 .BR PERF_MEM_TLB_SHIFT :
2557 .PD 0
2559 .TP 24
2560 .B PERF_MEM_TLB_NA
2561 Not available
2563 .B PERF_MEM_TLB_HIT
2566 .B PERF_MEM_TLB_MISS
2567 Miss
2569 .B PERF_MEM_TLB_L1
2570 Level 1 TLB
2572 .B PERF_MEM_TLB_L2
2573 Level 2 TLB
2575 .B PERF_MEM_TLB_WK
2576 Hardware walker
2578 .B PERF_MEM_TLB_OS
2579 OS fault handler
2584 .I transaction
2585 If the
2586 .B PERF_SAMPLE_TRANSACTION
2587 flag is set, then a 64-bit field is recorded describing
2588 the sources of any transactional memory aborts.
2590 The field is a bitwise combination of the following values:
2593 .B PERF_TXN_ELISION
2594 Abort from an elision type transaction (Intel-CPU-specific).
2596 .B PERF_TXN_TRANSACTION
2597 Abort from a generic transaction.
2599 .B PERF_TXN_SYNC
2600 Synchronous abort (related to the reported instruction).
2602 .B PERF_TXN_ASYNC
2603 Asynchronous abort (not related to the reported instruction).
2605 .B PERF_TXN_RETRY
2606 Retryable abort (retrying the transaction may have succeeded).
2608 .B PERF_TXN_CONFLICT
2609 Abort due to memory conflicts with other threads.
2611 .B PERF_TXN_CAPACITY_WRITE
2612 Abort due to write capacity overflow.
2614 .B PERF_TXN_CAPACITY_READ
2615 Abort due to read capacity overflow.
2618 In addition, a user-specified abort code can be obtained from
2619 the high 32 bits of the field by shifting right by
2620 .B PERF_TXN_ABORT_SHIFT
2621 and masking with the value
2622 .BR PERF_TXN_ABORT_MASK .
2624 .IR abi ", " regs[weight(mask)]
2626 .B PERF_SAMPLE_REGS_INTR
2627 is enabled, then the user CPU registers are recorded.
2630 .I abi
2631 field is one of
2632 .BR PERF_SAMPLE_REGS_ABI_NONE ,
2633 .BR PERF_SAMPLE_REGS_ABI_32 ,
2635 .BR PERF_SAMPLE_REGS_ABI_64 .
2638 .I regs
2639 field is an array of the CPU registers that were specified by
2641 .I sample_regs_intr
2642 attr field.
2643 The number of values is the number of bits set in the
2644 .I sample_regs_intr
2645 bit mask.
2647 .I phys_addr
2648 If the
2649 .B PERF_SAMPLE_PHYS_ADDR
2650 flag is set, then the 64-bit physical address is recorded.
2652 .I cgroup
2653 If the
2654 .B PERF_SAMPLE_CGROUP
2655 flag is set,
2656 then the 64-bit cgroup ID (for the perf_event subsystem) is recorded.
2657 To get the pathname of the cgroup, the ID should match to one in a
2658 .B PERF_RECORD_CGROUP .
2661 .B PERF_RECORD_MMAP2
2662 This record includes extended information on
2663 .BR mmap (2)
2664 calls returning executable mappings.
2665 The format is similar to that of the
2666 .B PERF_RECORD_MMAP
2667 record, but includes extra values that allow uniquely identifying
2668 shared mappings.
2670 .in +4n
2672 struct {
2673     struct perf_event_header header;
2674     u32    pid;
2675     u32    tid;
2676     u64    addr;
2677     u64    len;
2678     u64    pgoff;
2679     u32    maj;
2680     u32    min;
2681     u64    ino;
2682     u64    ino_generation;
2683     u32    prot;
2684     u32    flags;
2685     char   filename[];
2686     struct sample_id sample_id;
2692 .I pid
2693 is the process ID.
2695 .I tid
2696 is the thread ID.
2698 .I addr
2699 is the address of the allocated memory.
2701 .I len
2702 is the length of the allocated memory.
2704 .I pgoff
2705 is the page offset of the allocated memory.
2707 .I maj
2708 is the major ID of the underlying device.
2710 .I min
2711 is the minor ID of the underlying device.
2713 .I ino
2714 is the inode number.
2716 .I ino_generation
2717 is the inode generation.
2719 .I prot
2720 is the protection information.
2722 .I flags
2723 is the flags information.
2725 .I filename
2726 is a string describing the backing of the allocated memory.
2729 .BR PERF_RECORD_AUX " (since Linux 4.1)"
2730 .\" commit 68db7e98c3a6ebe7284b6cf14906ed7c55f3f7f0
2731 This record reports that new data is available in the separate
2732 AUX buffer region.
2734 .in +4n
2736 struct {
2737     struct perf_event_header header;
2738     u64    aux_offset;
2739     u64    aux_size;
2740     u64    flags;
2741     struct sample_id sample_id;
2747 .I aux_offset
2748 offset in the AUX mmap region where the new data begins.
2750 .I aux_size
2751 size of the data made available.
2753 .I flags
2754 describes the AUX update.
2757 .B PERF_AUX_FLAG_TRUNCATED
2758 if set, then the data returned was truncated to fit the available
2759 buffer size.
2761 .B PERF_AUX_FLAG_OVERWRITE
2762 .\" commit 2023a0d2829e521fe6ad6b9907f3f90bfbf57142
2763 if set, then the data returned has overwritten previous data.
2767 .BR PERF_RECORD_ITRACE_START " (since Linux 4.1)"
2768 .\" ec0d7729bbaed4b9d2d3fada693278e13a3d1368
2769 This record indicates which process has initiated an instruction
2770 trace event, allowing tools to properly correlate the instruction
2771 addresses in the AUX buffer with the proper executable.
2773 .in +4n
2775 struct {
2776     struct perf_event_header header;
2777     u32    pid;
2778     u32    tid;
2784 .I pid
2785 process ID of the thread starting an instruction trace.
2787 .I tid
2788 thread ID of the thread starting an instruction trace.
2791 .BR PERF_RECORD_LOST_SAMPLES " (since Linux 4.2)"
2792 .\" f38b0dbb491a6987e198aa6b428db8692a6480f8
2793 When using hardware sampling (such as Intel PEBS) this record
2794 indicates some number of samples that may have been lost.
2796 .in +4n
2798 struct {
2799     struct perf_event_header header;
2800     u64    lost;
2801     struct sample_id sample_id;
2807 .I lost
2808 the number of potentially lost samples.
2811 .BR PERF_RECORD_SWITCH " (since Linux 4.3)"
2812 .\" commit 45ac1403f564f411c6a383a2448688ba8dd705a4
2813 This record indicates a context switch has happened.
2815 .B PERF_RECORD_MISC_SWITCH_OUT
2816 bit in the
2817 .I misc
2818 field indicates whether it was a context switch into
2819 or away from the current process.
2821 .in +4n
2823 struct {
2824     struct perf_event_header header;
2825     struct sample_id sample_id;
2830 .BR PERF_RECORD_SWITCH_CPU_WIDE " (since Linux 4.3)"
2831 .\" commit 45ac1403f564f411c6a383a2448688ba8dd705a4
2832 As with
2833 .B PERF_RECORD_SWITCH
2834 this record indicates a context switch has happened,
2835 but it only occurs when sampling in CPU-wide mode
2836 and provides additional information on the process
2837 being switched to/from.
2839 .B PERF_RECORD_MISC_SWITCH_OUT
2840 bit in the
2841 .I misc
2842 field indicates whether it was a context switch into
2843 or away from the current process.
2845 .in +4n
2847 struct {
2848     struct perf_event_header header;
2849     u32 next_prev_pid;
2850     u32 next_prev_tid;
2851     struct sample_id sample_id;
2857 .I next_prev_pid
2858 The process ID of the previous (if switching in)
2859 or next (if switching out) process on the CPU.
2861 .I next_prev_tid
2862 The thread ID of the previous (if switching in)
2863 or next (if switching out) thread on the CPU.
2866 .BR PERF_RECORD_NAMESPACES " (since Linux 4.11)"
2867 .\" commit e422267322cd319e2695a535e47c5b1feeac45eb
2868 This record includes various namespace information of a process.
2870 .in +4n
2872 struct {
2873     struct perf_event_header header;
2874     u32    pid;
2875     u32    tid;
2876     u64    nr_namespaces;
2877     struct { u64 dev, inode } [nr_namespaces];
2878     struct sample_id sample_id;
2884 .I pid
2885 is the process ID
2887 .I tid
2888 is the thread ID
2890 .I nr_namespace
2891 is the number of namespaces in this record
2894 Each namespace has
2895 .I dev
2897 .I inode
2898 fields and is recorded in the
2899 fixed position like below:
2902 .BR NET_NS_INDEX = 0
2903 Network namespace
2905 .BR UTS_NS_INDEX = 1
2906 UTS namespace
2908 .BR IPC_NS_INDEX = 2
2909 IPC namespace
2911 .BR PID_NS_INDEX = 3
2912 PID namespace
2914 .BR USER_NS_INDEX = 4
2915 User namespace
2917 .BR MNT_NS_INDEX = 5
2918 Mount namespace
2920 .BR CGROUP_NS_INDEX = 6
2921 Cgroup namespace
2924 .BR PERF_RECORD_KSYMBOL " (since Linux 5.0)"
2925 .\" commit 76193a94522f1d4edf2447a536f3f796ce56343b
2926 This record indicates kernel symbol register/unregister events.
2928 .in +4n
2930 struct {
2931     struct perf_event_header header;
2932     u64    addr;
2933     u32    len;
2934     u16    ksym_type;
2935     u16    flags;
2936     char   name[];
2937     struct sample_id sample_id;
2943 .I addr
2944 is the address of the kernel symbol.
2946 .I len
2947 is the length of the kernel symbol.
2949 .I ksym_type
2950 is the type of the kernel symbol.
2951 Currently the following types are available:
2954 .B PERF_RECORD_KSYMBOL_TYPE_BPF
2955 The kernel symbol is a BPF function.
2958 .I flags
2959 If the
2960 .B PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER
2961 is set, then this event is for unregistering the kernel symbol.
2964 .BR PERF_RECORD_BPF_EVENT " (since Linux 5.0)"
2965 .\" commit 6ee52e2a3fe4ea35520720736e6791df1fb67106
2966 This record indicates BPF program is loaded or unloaded.
2968 .in +4n
2970 struct {
2971     struct perf_event_header header;
2972     u16 type;
2973     u16 flags;
2974     u32 id;
2975     u8 tag[BPF_TAG_SIZE];
2976     struct sample_id sample_id;
2982 .I type
2983 is one of the following values:
2986 .B PERF_BPF_EVENT_PROG_LOAD
2987 A BPF program is loaded
2989 .B PERF_BPF_EVENT_PROG_UNLOAD
2990 A BPF program is unloaded
2993 .I id
2994 is the ID of the BPF program.
2996 .I tag
2997 is the tag of the BPF program.
2998 Currently,
2999 .B BPF_TAG_SIZE
3000 is defined as 8.
3003 .BR PERF_RECORD_CGROUP " (since Linux 5.7)"
3004 .\" commit 96aaab686505c449e24d76e76507290dcc30e008
3005 This record indicates a new cgroup is created and activated.
3007 .in +4n
3009 struct {
3010     struct perf_event_header header;
3011     u64    id;
3012     char   path[];
3013     struct sample_id sample_id;
3019 .I id
3020 is the cgroup identifier.
3021 This can be also retrieved by
3022 .BR name_to_handle_at (2)
3023 on the cgroup path (as a file handle).
3025 .I path
3026 is the path of the cgroup from the root.
3029 .BR PERF_RECORD_TEXT_POKE " (since Linux 5.8)"
3030 .\" commit e17d43b93e544f5016c0251d2074c15568d5d963
3031 This record indicates a change in the kernel text.
3032 This includes addition and removal of the text
3033 and the corresponding length is zero in this case.
3035 .in +4n
3037 struct {
3038     struct perf_event_header header;
3039     u64    addr;
3040     u16    old_len;
3041     u16    new_len;
3042     u8     bytes[];
3043     struct sample_id sample_id;
3049 .I addr
3050 is the address of the change
3052 .I old_len
3053 is the old length
3055 .I new_len
3056 is the new length
3058 .I bytes
3059 contains old bytes immediately followed by new bytes.
3062 .SS Overflow handling
3063 Events can be set to notify when a threshold is crossed,
3064 indicating an overflow.
3065 Overflow conditions can be captured by monitoring the
3066 event file descriptor with
3067 .BR poll (2),
3068 .BR select (2),
3070 .BR epoll (7).
3071 Alternatively, the overflow events can be captured via sa signal handler,
3072 by enabling I/O signaling on the file descriptor; see the discussion of the
3073 .BR F_SETOWN
3075 .BR F_SETSIG
3076 operations in
3077 .BR fcntl (2).
3079 Overflows are generated only by sampling events
3080 .RI ( sample_period
3081 must have a nonzero value).
3083 There are two ways to generate overflow notifications.
3085 The first is to set a
3086 .I wakeup_events
3088 .I wakeup_watermark
3089 value that will trigger if a certain number of samples
3090 or bytes have been written to the mmap ring buffer.
3091 In this case,
3092 .B POLL_IN
3093 is indicated.
3095 The other way is by use of the
3096 .B PERF_EVENT_IOC_REFRESH
3097 ioctl.
3098 This ioctl adds to a counter that decrements each time the event overflows.
3099 When nonzero,
3100 .B POLL_IN
3101 is indicated, but
3102 once the counter reaches 0
3103 .B POLL_HUP
3104 is indicated and
3105 the underlying event is disabled.
3107 Refreshing an event group leader refreshes all siblings and
3108 refreshing with a parameter of 0 currently enables infinite
3109 refreshes;
3110 these behaviors are unsupported and should not be relied on.
3111 .\" See https://lkml.org/lkml/2011/5/24/337
3113 Starting with Linux 3.18,
3114 .\" commit 179033b3e064d2cd3f5f9945e76b0a0f0fbf4883
3115 .B POLL_HUP
3116 is indicated if the event being monitored is attached to a different
3117 process and that process exits.
3118 .SS rdpmc instruction
3119 Starting with Linux 3.4 on x86, you can use the
3120 .\" commit c7206205d00ab375839bd6c7ddb247d600693c09
3121 .I rdpmc
3122 instruction to get low-latency reads without having to enter the kernel.
3123 Note that using
3124 .I rdpmc
3125 is not necessarily faster than other methods for reading event values.
3127 Support for this can be detected with the
3128 .I cap_usr_rdpmc
3129 field in the mmap page; documentation on how
3130 to calculate event values can be found in that section.
3132 Originally, when rdpmc support was enabled, any process (not just ones
3133 with an active perf event) could use the rdpmc instruction to access
3134 the counters.
3135 Starting with Linux 4.0,
3136 .\" 7911d3f7af14a614617e38245fedf98a724e46a9
3137 rdpmc support is only allowed if an event is currently enabled
3138 in a process's context.
3139 To restore the old behavior, write the value 2 to
3140 .IR /sys/devices/cpu/rdpmc .
3141 .SS perf_event ioctl calls
3142 Various ioctls act on
3143 .BR perf_event_open ()
3144 file descriptors:
3146 .B PERF_EVENT_IOC_ENABLE
3147 This enables the individual event or event group specified by the
3148 file descriptor argument.
3150 If the
3151 .B PERF_IOC_FLAG_GROUP
3152 bit is set in the ioctl argument, then all events in a group are
3153 enabled, even if the event specified is not the group leader
3154 (but see BUGS).
3156 .B PERF_EVENT_IOC_DISABLE
3157 This disables the individual counter or event group specified by the
3158 file descriptor argument.
3160 Enabling or disabling the leader of a group enables or disables the
3161 entire group; that is, while the group leader is disabled, none of the
3162 counters in the group will count.
3163 Enabling or disabling a member of a group other than the leader
3164 affects only that counter; disabling a non-leader
3165 stops that counter from counting but doesn't affect any other counter.
3167 If the
3168 .B PERF_IOC_FLAG_GROUP
3169 bit is set in the ioctl argument, then all events in a group are
3170 disabled, even if the event specified is not the group leader
3171 (but see BUGS).
3173 .B PERF_EVENT_IOC_REFRESH
3174 Non-inherited overflow counters can use this
3175 to enable a counter for a number of overflows specified by the argument,
3176 after which it is disabled.
3177 Subsequent calls of this ioctl add the argument value to the current
3178 count.
3179 An overflow notification with
3180 .B POLL_IN
3181 set will happen on each overflow until the
3182 count reaches 0; when that happens a notification with
3183 .B POLL_HUP
3184 set is sent and the event is disabled.
3185 Using an argument of 0 is considered undefined behavior.
3187 .B PERF_EVENT_IOC_RESET
3188 Reset the event count specified by the
3189 file descriptor argument to zero.
3190 This resets only the counts; there is no way to reset the
3191 multiplexing
3192 .I time_enabled
3194 .I time_running
3195 values.
3197 If the
3198 .B PERF_IOC_FLAG_GROUP
3199 bit is set in the ioctl argument, then all events in a group are
3200 reset, even if the event specified is not the group leader
3201 (but see BUGS).
3203 .B PERF_EVENT_IOC_PERIOD
3204 This updates the overflow period for the event.
3206 Since Linux 3.7 (on ARM)
3207 .\" commit 3581fe0ef37ce12ac7a4f74831168352ae848edc
3208 and Linux 3.14 (all other architectures),
3209 .\" commit bad7192b842c83e580747ca57104dd51fe08c223
3210 the new period takes effect immediately.
3211 On older kernels, the new period did not take effect until
3212 after the next overflow.
3214 The argument is a pointer to a 64-bit value containing the
3215 desired new period.
3217 Prior to Linux 2.6.36,
3218 .\" commit ad0cf3478de8677f720ee06393b3147819568d6a
3219 this ioctl always failed due to a bug
3220 in the kernel.
3222 .B PERF_EVENT_IOC_SET_OUTPUT
3223 This tells the kernel to report event notifications to the specified
3224 file descriptor rather than the default one.
3225 The file descriptors must all be on the same CPU.
3227 The argument specifies the desired file descriptor, or \-1 if
3228 output should be ignored.
3230 .BR PERF_EVENT_IOC_SET_FILTER " (since Linux 2.6.33)"
3231 .\" commit 6fb2915df7f0747d9044da9dbff5b46dc2e20830
3232 This adds an ftrace filter to this event.
3234 The argument is a pointer to the desired ftrace filter.
3236 .BR PERF_EVENT_IOC_ID " (since Linux 3.12)"
3237 .\" commit cf4957f17f2a89984915ea808876d9c82225b862
3238 This returns the event ID value for the given event file descriptor.
3240 The argument is a pointer to a 64-bit unsigned integer
3241 to hold the result.
3243 .BR PERF_EVENT_IOC_SET_BPF " (since Linux 4.1)"
3244 .\" commit 2541517c32be2531e0da59dfd7efc1ce844644f5
3245 This allows attaching a Berkeley Packet Filter (BPF)
3246 program to an existing kprobe tracepoint event.
3247 You need
3248 .B CAP_PERFMON
3249 (since Linux 5.8) or
3250 .B CAP_SYS_ADMIN
3251 privileges to use this ioctl.
3253 The argument is a BPF program file descriptor that was created by
3254 a previous
3255 .BR bpf (2)
3256 system call.
3258 .BR PERF_EVENT_IOC_PAUSE_OUTPUT " (since Linux 4.7)"
3259 .\" commit 86e7972f690c1017fd086cdfe53d8524e68c661c
3260 This allows pausing and resuming the event's ring-buffer.
3261 A paused ring-buffer does not prevent generation of samples,
3262 but simply discards them.
3263 The discarded samples are considered lost, and cause a
3264 .BR PERF_RECORD_LOST
3265 sample to be generated when possible.
3266 An overflow signal may still be triggered by the discarded sample
3267 even though the ring-buffer remains empty.
3269 The argument is an unsigned 32-bit integer.
3270 A nonzero value pauses the ring-buffer, while a
3271 zero value resumes the ring-buffer.
3273 .BR PERF_EVENT_MODIFY_ATTRIBUTES " (since Linux 4.17)"
3274 .\" commit 32ff77e8cc9e66cc4fb38098f64fd54cc8f54573
3275 This allows modifying an existing event without the overhead
3276 of closing and reopening a new event.
3277 Currently this is supported only for breakpoint events.
3279 The argument is a pointer to a
3280 .I perf_event_attr
3281 structure containing the updated event settings.
3283 .BR PERF_EVENT_IOC_QUERY_BPF " (since Linux 4.16)"
3284 .\" commit f371b304f12e31fe30207c41ca7754564e0ea4dc
3285 This allows querying which Berkeley Packet Filter (BPF)
3286 programs are attached to an existing kprobe tracepoint.
3287 You can only attach one BPF program per event, but you can
3288 have multiple events attached to a tracepoint.
3289 Querying this value on one tracepoint event returns the ID
3290 of all BPF programs in all events attached to the tracepoint.
3291 You need
3292 .B CAP_PERFMON
3293 (since Linux 5.8) or
3294 .B CAP_SYS_ADMIN
3295 privileges to use this ioctl.
3297 The argument is a pointer to a structure
3298 .in +4n
3300 struct perf_event_query_bpf {
3301     __u32    ids_len;
3302     __u32    prog_cnt;
3303     __u32    ids[0];
3309 .I ids_len
3310 field indicates the number of ids that can fit in the provided
3311 .I ids
3312 array.
3314 .I prog_cnt
3315 value is filled in by the kernel with the number of attached
3316 BPF programs.
3318 .I ids
3319 array is filled with the ID of each attached BPF program.
3320 If there are more programs than will fit in the array, then the
3321 kernel will return
3322 .B ENOSPC
3324 .I ids_len
3325 will indicate the number of program IDs that were successfully copied.
3327 .SS Using prctl(2)
3328 A process can enable or disable all currently open event groups
3329 using the
3330 .BR prctl (2)
3331 .B PR_TASK_PERF_EVENTS_ENABLE
3333 .B PR_TASK_PERF_EVENTS_DISABLE
3334 operations.
3335 This applies only to events created locally by the calling process.
3336 This does not apply to events created by other processes attached
3337 to the calling process or inherited events from a parent process.
3338 Only group leaders are enabled and disabled,
3339 not any other members of the groups.
3340 .SS perf_event related configuration files
3341 Files in
3342 .I /proc/sys/kernel/
3343 .RS 4
3345 .I /proc/sys/kernel/perf_event_paranoid
3347 .I perf_event_paranoid
3348 file can be set to restrict access to the performance counters.
3350 .PD 0
3352 .IP 2 4
3353 allow only user-space measurements (default since Linux 4.6).
3354 .\" default changed in commit 0161028b7c8aebef64194d3d73e43bc3b53b5c66
3355 .IP 1
3356 allow both kernel and user measurements (default before Linux 4.6).
3357 .IP 0
3358 allow access to CPU-specific data but not raw tracepoint samples.
3359 .IP \-1
3360 no restrictions.
3364 The existence of the
3365 .I perf_event_paranoid
3366 file is the official method for determining if a kernel supports
3367 .BR perf_event_open ().
3369 .I /proc/sys/kernel/perf_event_max_sample_rate
3370 This sets the maximum sample rate.
3371 Setting this too high can allow
3372 users to sample at a rate that impacts overall machine performance
3373 and potentially lock up the machine.
3374 The default value is
3375 100000 (samples per second).
3377 .I /proc/sys/kernel/perf_event_max_stack
3378 .\" Introduced in c5dfd78eb79851e278b7973031b9ca363da87a7e
3379 This file sets the maximum depth of stack frame entries reported
3380 when generating a call trace.
3382 .I /proc/sys/kernel/perf_event_mlock_kb
3383 Maximum number of pages an unprivileged user can
3384 .BR mlock (2).
3385 The default is 516 (kB).
3388 Files in
3389 .I /sys/bus/event_source/devices/
3391 .RS 4
3392 Since Linux 2.6.34, the kernel supports having multiple PMUs
3393 available for monitoring.
3394 Information on how to program these PMUs can be found under
3395 .IR /sys/bus/event_source/devices/ .
3396 Each subdirectory corresponds to a different PMU.
3398 .IR /sys/bus/event_source/devices/*/type " (since Linux 2.6.38)"
3399 .\" commit abe43400579d5de0078c2d3a760e6598e183f871
3400 This contains an integer that can be used in the
3401 .I type
3402 field of
3403 .I perf_event_attr
3404 to indicate that you wish to use this PMU.
3406 .IR /sys/bus/event_source/devices/cpu/rdpmc " (since Linux 3.4)"
3407 .\" commit 0c9d42ed4cee2aa1dfc3a260b741baae8615744f
3408 If this file is 1, then direct user-space access to the
3409 performance counter registers is allowed via the rdpmc instruction.
3410 This can be disabled by echoing 0 to the file.
3412 As of Linux 4.0
3413 .\" a66734297f78707ce39d756b656bfae861d53f62
3414 .\" 7911d3f7af14a614617e38245fedf98a724e46a9
3415 the behavior has changed, so that 1 now means only allow access
3416 to processes with active perf events, with 2 indicating the old
3417 allow-anyone-access behavior.
3419 .IR /sys/bus/event_source/devices/*/format/ " (since Linux 3.4)"
3420 .\" commit 641cc938815dfd09f8fa1ec72deb814f0938ac33
3421 This subdirectory contains information on the architecture-specific
3422 subfields available for programming the various
3423 .I config
3424 fields in the
3425 .I perf_event_attr
3426 struct.
3428 The content of each file is the name of the config field, followed
3429 by a colon, followed by a series of integer bit ranges separated by
3430 commas.
3431 For example, the file
3432 .I event
3433 may contain the value
3434 .I config1:1,6\-10,44
3435 which indicates that event is an attribute that occupies bits 1,6\(en10, and 44
3437 .IR perf_event_attr::config1 .
3439 .IR /sys/bus/event_source/devices/*/events/ " (since Linux 3.4)"
3440 .\" commit 641cc938815dfd09f8fa1ec72deb814f0938ac33
3441 This subdirectory contains files with predefined events.
3442 The contents are strings describing the event settings
3443 expressed in terms of the fields found in the previously mentioned
3444 .I ./format/
3445 directory.
3446 These are not necessarily complete lists of all events supported by
3447 a PMU, but usually a subset of events deemed useful or interesting.
3449 The content of each file is a list of attribute names
3450 separated by commas.
3451 Each entry has an optional value (either hex or decimal).
3452 If no value is specified, then it is assumed to be a single-bit
3453 field with a value of 1.
3454 An example entry may look like this:
3455 .IR event=0x2,inv,ldlat=3 .
3457 .I /sys/bus/event_source/devices/*/uevent
3458 This file is the standard kernel device interface
3459 for injecting hotplug events.
3461 .IR /sys/bus/event_source/devices/*/cpumask " (since Linux 3.7)"
3462 .\" commit 314d9f63f385096580e9e2a06eaa0745d92fe4ac
3464 .I cpumask
3465 file contains a comma-separated list of integers that
3466 indicate a representative CPU number for each socket (package)
3467 on the motherboard.
3468 This is needed when setting up uncore or northbridge events, as
3469 those PMUs present socket-wide events.
3471 .SH RETURN VALUE
3472 On success,
3473 .BR perf_event_open ()
3474 returns the new file descriptor.
3475 On error, \-1 is returned and
3476 .I errno
3477 is set to indicate the error.
3478 .SH ERRORS
3479 The errors returned by
3480 .BR perf_event_open ()
3481 can be inconsistent, and may
3482 vary across processor architectures and performance monitoring units.
3484 .B E2BIG
3485 Returned if the
3486 .I perf_event_attr
3487 .I size
3488 value is too small
3489 (smaller than
3490 .BR PERF_ATTR_SIZE_VER0 ),
3491 too big (larger than the page size),
3492 or larger than the kernel supports and the extra bytes are not zero.
3493 When
3494 .B E2BIG
3495 is returned, the
3496 .I perf_event_attr
3497 .I size
3498 field is overwritten by the kernel to be the size of the structure
3499 it was expecting.
3501 .B EACCES
3502 Returned when the requested event requires
3503 .B CAP_PERFMON
3504 (since Linux 5.8) or
3505 .B CAP_SYS_ADMIN
3506 permissions (or a more permissive perf_event paranoid setting).
3507 Some common cases where an unprivileged process
3508 may encounter this error:
3509 attaching to a process owned by a different user;
3510 monitoring all processes on a given CPU (i.e., specifying the
3511 .I pid
3512 argument as \-1);
3513 and not setting
3514 .I exclude_kernel
3515 when the paranoid setting requires it.
3517 .B EBADF
3518 Returned if the
3519 .I group_fd
3520 file descriptor is not valid, or, if
3521 .B PERF_FLAG_PID_CGROUP
3522 is set,
3523 the cgroup file descriptor in
3524 .I pid
3525 is not valid.
3527 .BR EBUSY " (since Linux 4.1)"
3528 .\" bed5b25ad9c8a2f5d735ef0bc746ec870c01c1b0
3529 Returned if another event already has exclusive
3530 access to the PMU.
3532 .B EFAULT
3533 Returned if the
3534 .I attr
3535 pointer points at an invalid memory address.
3537 .B EINVAL
3538 Returned if the specified event is invalid.
3539 There are many possible reasons for this.
3540 A not-exhaustive list:
3541 .I sample_freq
3542 is higher than the maximum setting;
3544 .I cpu
3545 to monitor does not exist;
3546 .I read_format
3547 is out of range;
3548 .I sample_type
3549 is out of range;
3551 .I flags
3552 value is out of range;
3553 .I exclusive
3555 .I pinned
3556 set and the event is not a group leader;
3557 the event
3558 .I config
3559 values are out of range or set reserved bits;
3560 the generic event selected is not supported; or
3561 there is not enough room to add the selected event.
3563 .B EINTR
3564 Returned when trying to mix perf and ftrace handling
3565 for a uprobe.
3567 .B EMFILE
3568 Each opened event uses one file descriptor.
3569 If a large number of events are opened,
3570 the per-process limit on the number of open file descriptors will be reached,
3571 and no more events can be created.
3573 .B ENODEV
3574 Returned when the event involves a feature not supported
3575 by the current CPU.
3577 .B ENOENT
3578 Returned if the
3579 .I type
3580 setting is not valid.
3581 This error is also returned for
3582 some unsupported generic events.
3584 .B ENOSPC
3585 Prior to Linux 3.3, if there was not enough room for the event,
3586 .\" commit aa2bc1ade59003a379ffc485d6da2d92ea3370a6
3587 .B ENOSPC
3588 was returned.
3589 In Linux 3.3, this was changed to
3590 .BR EINVAL .
3591 .B ENOSPC
3592 is still returned if you try to add more breakpoint events
3593 than supported by the hardware.
3595 .B ENOSYS
3596 Returned if
3597 .B PERF_SAMPLE_STACK_USER
3598 is set in
3599 .I sample_type
3600 and it is not supported by hardware.
3602 .B EOPNOTSUPP
3603 Returned if an event requiring a specific hardware feature is
3604 requested but there is no hardware support.
3605 This includes requesting low-skid events if not supported,
3606 branch tracing if it is not available, sampling if no PMU
3607 interrupt is available, and branch stacks for software events.
3609 .BR EOVERFLOW " (since Linux 4.8)"
3610 .\" 97c79a38cd454602645f0470ffb444b3b75ce574
3611 Returned if
3612 .B PERF_SAMPLE_CALLCHAIN
3613 is requested and
3614 .I sample_max_stack
3615 is larger than the maximum specified in
3616 .IR /proc/sys/kernel/perf_event_max_stack .
3618 .B EPERM
3619 Returned on many (but not all) architectures when an unsupported
3620 .IR exclude_hv ", " exclude_idle ", " exclude_user ", or " exclude_kernel
3621 setting is specified.
3623 It can also happen, as with
3624 .BR EACCES ,
3625 when the requested event requires
3626 .B CAP_PERFMON
3627 (since Linux 5.8) or
3628 .B CAP_SYS_ADMIN
3629 permissions (or a more permissive perf_event paranoid setting).
3630 This includes setting a breakpoint on a kernel address,
3631 and (since Linux 3.13) setting a kernel function-trace tracepoint.
3632 .\" commit a4e95fc2cbb31d70a65beffeaf8773f881328c34
3634 .B ESRCH
3635 Returned if attempting to attach to a process that does not exist.
3636 .SH VERSION
3637 .BR perf_event_open ()
3638 was introduced in Linux 2.6.31 but was called
3639 .\" commit 0793a61d4df8daeac6492dbf8d2f3e5713caae5e
3640 .BR perf_counter_open ().
3641 It was renamed in Linux 2.6.32.
3642 .\" commit cdd6c482c9ff9c55475ee7392ec8f672eddb7be6
3643 .SH CONFORMING TO
3644 This
3645 .BR perf_event_open ()
3646 system call Linux-specific
3647 and should not be used in programs intended to be portable.
3648 .SH NOTES
3649 The official way of knowing if
3650 .BR perf_event_open ()
3651 support is enabled is checking
3652 for the existence of the file
3653 .IR /proc/sys/kernel/perf_event_paranoid .
3655 .B CAP_PERFMON
3656 capability (since Linux 5.8) provides secure approach to
3657 performance monitoring and observability operations in a system
3658 according to the principal of least privilege (POSIX IEEE 1003.1e).
3659 Accessing system performance monitoring and observability operations
3660 using
3661 .B CAP_PERFMON
3662 rather than the much more powerful
3663 .B CAP_SYS_ADMIN
3664 excludes chances to misuse credentials and makes operations more secure.
3665 .B CAP_SYS_ADMIN
3666 usage for secure system performance monitoring and observability
3667 is discouraged in favor of the
3668 .B CAP_PERFMON
3669 capability.
3670 .SH BUGS
3672 .B F_SETOWN_EX
3673 option to
3674 .BR fcntl (2)
3675 is needed to properly get overflow signals in threads.
3676 This was introduced in Linux 2.6.32.
3677 .\" commit ba0a6c9f6fceed11c6a99e8326f0477fe383e6b5
3679 Prior to Linux 2.6.33 (at least for x86),
3680 .\" commit b690081d4d3f6a23541493f1682835c3cd5c54a1
3681 the kernel did not check
3682 if events could be scheduled together until read time.
3683 The same happens on all known kernels if the NMI watchdog is enabled.
3684 This means to see if a given set of events works you have to
3685 .BR perf_event_open (),
3686 start, then read before you know for sure you
3687 can get valid measurements.
3689 Prior to Linux 2.6.34,
3690 .\" FIXME . cannot find a kernel commit for this one
3691 event constraints were not enforced by the kernel.
3692 In that case, some events would silently return "0" if the kernel
3693 scheduled them in an improper counter slot.
3695 Prior to Linux 2.6.34, there was a bug when multiplexing where the
3696 wrong results could be returned.
3697 .\" commit 45e16a6834b6af098702e5ea6c9a40de42ff77d8
3699 Kernels from Linux 2.6.35 to Linux 2.6.39 can quickly crash the kernel if
3700 "inherit" is enabled and many threads are started.
3701 .\" commit 38b435b16c36b0d863efcf3f07b34a6fac9873fd
3703 Prior to Linux 2.6.35,
3704 .\" commit 050735b08ca8a016bbace4445fa025b88fee770b
3705 .B PERF_FORMAT_GROUP
3706 did not work with attached processes.
3708 There is a bug in the kernel code between
3709 Linux 2.6.36 and Linux 3.0 that ignores the
3710 "watermark" field and acts as if a wakeup_event
3711 was chosen if the union has a
3712 nonzero value in it.
3713 .\" commit 4ec8363dfc1451f8c8f86825731fe712798ada02
3715 From Linux 2.6.31 to Linux 3.4, the
3716 .B PERF_IOC_FLAG_GROUP
3717 ioctl argument was broken and would repeatedly operate
3718 on the event specified rather than iterating across
3719 all sibling events in a group.
3720 .\" commit 724b6daa13e100067c30cfc4d1ad06629609dc4e
3722 From Linux 3.4 to Linux 3.11, the mmap
3723 .\" commit fa7315871046b9a4c48627905691dbde57e51033
3724 .I cap_usr_rdpmc
3726 .I cap_usr_time
3727 bits mapped to the same location.
3728 Code should migrate to the new
3729 .I cap_user_rdpmc
3731 .I cap_user_time
3732 fields instead.
3734 Always double-check your results!
3735 Various generalized events have had wrong values.
3736 For example, retired branches measured
3737 the wrong thing on AMD machines until Linux 2.6.35.
3738 .\" commit f287d332ce835f77a4f5077d2c0ef1e3f9ea42d2
3739 .SH EXAMPLES
3740 The following is a short example that measures the total
3741 instruction count of a call to
3742 .BR printf (3).
3745 #include <stdlib.h>
3746 #include <stdio.h>
3747 #include <unistd.h>
3748 #include <string.h>
3749 #include <sys/ioctl.h>
3750 #include <linux/perf_event.h>
3751 #include <asm/unistd.h>
3753 static long
3754 perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
3755                 int cpu, int group_fd, unsigned long flags)
3757     int ret;
3759     ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
3760                    group_fd, flags);
3761     return ret;
3765 main(int argc, char *argv[])
3767     struct perf_event_attr pe;
3768     long long count;
3769     int fd;
3771     memset(&pe, 0, sizeof(pe));
3772     pe.type = PERF_TYPE_HARDWARE;
3773     pe.size = sizeof(pe);
3774     pe.config = PERF_COUNT_HW_INSTRUCTIONS;
3775     pe.disabled = 1;
3776     pe.exclude_kernel = 1;
3777     pe.exclude_hv = 1;
3779     fd = perf_event_open(&pe, 0, \-1, \-1, 0);
3780     if (fd == \-1) {
3781        fprintf(stderr, "Error opening leader %llx\en", pe.config);
3782        exit(EXIT_FAILURE);
3783     }
3785     ioctl(fd, PERF_EVENT_IOC_RESET, 0);
3786     ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
3788     printf("Measuring instruction count for this printf\en");
3790     ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
3791     read(fd, &count, sizeof(count));
3793     printf("Used %lld instructions\en", count);
3795     close(fd);
3798 .SH SEE ALSO
3799 .BR perf (1),
3800 .BR fcntl (2),
3801 .BR mmap (2),
3802 .BR open (2),
3803 .BR prctl (2),
3804 .BR read (2)
3806 .IR Documentation/admin\-guide/perf\-security.rst
3807 in the kernel source tree