1 .\" Copyright (c) 2012, Vincent Weaver
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.
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.
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.
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/>.
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.
27 .TH PERF_EVENT_OPEN 2 2020-02-09 "Linux" "Linux Programmer's Manual"
29 perf_event_open \- set up performance monitoring
32 .B #include <linux/perf_event.h>
33 .B #include <linux/hw_breakpoint.h>
35 .BI "int perf_event_open(struct perf_event_attr *" attr ,
36 .BI " pid_t " pid ", int " cpu ", int " group_fd ,
37 .BI " unsigned long " flags );
41 There is no glibc wrapper for this system call; see NOTES.
43 Given a list of parameters,
44 .BR perf_event_open ()
45 returns a file descriptor, for use in subsequent system calls
46 .RB ( read "(2), " mmap "(2), " prctl "(2), " fcntl "(2), etc.)."
49 .BR perf_event_open ()
50 creates a file descriptor that allows measuring performance
52 Each file descriptor corresponds to one
53 event that is measured; these can be grouped together
54 to measure multiple events simultaneously.
56 Events can be enabled and disabled in two ways: via
60 When an event is disabled it does not count or generate overflows but does
61 continue to exist and maintain its count value.
63 Events come in two flavors: counting and sampled.
66 event is one that is used for counting the aggregate number of events
68 In general, counting event results are gathered with a
73 event periodically writes measurements to a buffer that can then
82 arguments allow specifying which process and CPU to monitor:
84 .BR "pid == 0" " and " "cpu == \-1"
85 This measures the calling process/thread on any CPU.
87 .BR "pid == 0" " and " "cpu >= 0"
88 This measures the calling process/thread only
89 when running on the specified CPU.
91 .BR "pid > 0" " and " "cpu == \-1"
92 This measures the specified process/thread on any CPU.
94 .BR "pid > 0" " and " "cpu >= 0"
95 This measures the specified process/thread only
96 when running on the specified CPU.
98 .BR "pid == \-1" " and " "cpu >= 0"
99 This measures all processes/threads on the specified CPU.
103 .I /proc/sys/kernel/perf_event_paranoid
104 value of less than 1.
106 .BR "pid == \-1" " and " "cpu == \-1"
107 This setting is invalid and will return an error.
111 is greater than zero, permission to perform this system call
112 is governed by a ptrace access mode
113 .B PTRACE_MODE_READ_REALCREDS
119 argument allows event groups to be created.
120 An event group has one event which is the group leader.
121 The leader is created first, with
122 .IR group_fd " = \-1."
123 The rest of the group members are created with subsequent
124 .BR perf_event_open ()
127 being set to the file descriptor of the group leader.
128 (A single event on its own is created with
129 .IR group_fd " = \-1"
130 and is considered to be a group with only 1 member.)
131 An event group is scheduled onto the CPU as a unit: it will
132 be put onto the CPU only if all of the events in the group can be put onto
134 This means that the values of the member events can be
135 meaningfully compared\(emadded, divided (to get ratios), and so on\(emwith each
136 other, since they have counted events for the same set of executed
141 argument is formed by ORing together zero or more of the following values:
143 .BR PERF_FLAG_FD_CLOEXEC " (since Linux 3.14)"
144 .\" commit a21b0b354d4ac39be691f51c53562e2c24443d9e
145 This flag enables the close-on-exec flag for the created
146 event file descriptor,
147 so that the file descriptor is automatically closed on
149 Setting the close-on-exec flags at creation time, rather than later with
151 avoids potential race conditions where the calling thread invokes
152 .BR perf_event_open ()
155 at the same time as another thread calls
160 .BR PERF_FLAG_FD_NO_GROUP
161 This flag tells the event to ignore the
163 parameter except for the purpose of setting up output redirection
165 .B PERF_FLAG_FD_OUTPUT
168 .BR PERF_FLAG_FD_OUTPUT " (broken since Linux 2.6.35)"
169 .\" commit ac9721f3f54b27a16c7e1afb2481e7ee95a70318
170 This flag re-routes the event's sampled output to instead
171 be included in the mmap buffer of the event specified by
174 .BR PERF_FLAG_PID_CGROUP " (since Linux 2.6.39)"
175 .\" commit e5d1367f17ba6a6fed5fd8b74e4d5720923e0c25
176 This flag activates per-container system-wide monitoring.
178 is an abstraction that isolates a set of resources for finer-grained
179 control (CPUs, memory, etc.).
180 In this mode, the event is measured
181 only if the thread running on the monitored CPU belongs to the designated
183 The cgroup is identified by passing a file descriptor
184 opened on its directory in the cgroupfs filesystem.
186 cgroup to monitor is called
188 then a file descriptor opened on
190 (assuming cgroupfs is mounted on
192 must be passed as the
195 cgroup monitoring is available only
196 for system-wide events and may therefore require extra permissions.
200 structure provides detailed configuration information
201 for the event being created.
205 struct perf_event_attr {
206 __u32 type; /* Type of event */
207 __u32 size; /* Size of attribute structure */
208 __u64 config; /* Type-specific configuration */
211 __u64 sample_period; /* Period of sampling */
212 __u64 sample_freq; /* Frequency of sampling */
215 __u64 sample_type; /* Specifies values included in sample */
216 __u64 read_format; /* Specifies values returned in read */
218 __u64 disabled : 1, /* off by default */
219 inherit : 1, /* children inherit it */
220 pinned : 1, /* must always be on PMU */
221 exclusive : 1, /* only group on PMU */
222 exclude_user : 1, /* don't count user */
223 exclude_kernel : 1, /* don't count kernel */
224 exclude_hv : 1, /* don't count hypervisor */
225 exclude_idle : 1, /* don't count when idle */
226 mmap : 1, /* include mmap data */
227 comm : 1, /* include comm data */
228 freq : 1, /* use freq, not period */
229 inherit_stat : 1, /* per task counts */
230 enable_on_exec : 1, /* next exec enables */
231 task : 1, /* trace fork/exit */
232 watermark : 1, /* wakeup_watermark */
233 precise_ip : 2, /* skid constraint */
234 mmap_data : 1, /* non-exec mmap data */
235 sample_id_all : 1, /* sample_type all events */
236 exclude_host : 1, /* don't count in host */
237 exclude_guest : 1, /* don't count in guest */
238 exclude_callchain_kernel : 1,
239 /* exclude kernel callchains */
240 exclude_callchain_user : 1,
241 /* exclude user callchains */
242 mmap2 : 1, /* include mmap with inode data */
243 comm_exec : 1, /* flag comm events that are
245 use_clockid : 1, /* use clockid for time fields */
246 context_switch : 1, /* context switch data */
251 __u32 wakeup_events; /* wakeup every n events */
252 __u32 wakeup_watermark; /* bytes before wakeup */
255 __u32 bp_type; /* breakpoint type */
258 __u64 bp_addr; /* breakpoint address */
259 __u64 kprobe_func; /* for perf_kprobe */
260 __u64 uprobe_path; /* for perf_uprobe */
261 __u64 config1; /* extension of config */
265 __u64 bp_len; /* breakpoint length */
266 __u64 kprobe_addr; /* with kprobe_func == NULL */
267 __u64 probe_offset; /* for perf_[k,u]probe */
268 __u64 config2; /* extension of config1 */
270 __u64 branch_sample_type; /* enum perf_branch_sample_type */
271 __u64 sample_regs_user; /* user regs to dump on samples */
272 __u32 sample_stack_user; /* size of stack to dump on
274 __s32 clockid; /* clock to use for time fields */
275 __u64 sample_regs_intr; /* regs to dump on samples */
276 __u32 aux_watermark; /* aux bytes before wakeup */
277 __u16 sample_max_stack; /* max frames in callchain */
278 __u16 __reserved_2; /* align to u64 */
286 structure are described in more detail below:
289 This field specifies the overall event type.
290 It has one of the following values:
293 .B PERF_TYPE_HARDWARE
294 This indicates one of the "generalized" hardware events provided
298 field definition for more details.
300 .B PERF_TYPE_SOFTWARE
301 This indicates one of the software-defined events provided by the kernel
302 (even if no hardware support is available).
304 .B PERF_TYPE_TRACEPOINT
305 This indicates a tracepoint
306 provided by the kernel tracepoint infrastructure.
308 .B PERF_TYPE_HW_CACHE
309 This indicates a hardware cache event.
310 This has a special encoding, described in the
315 This indicates a "raw" implementation-specific event in the
318 .BR PERF_TYPE_BREAKPOINT " (since Linux 2.6.33)"
319 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
320 This indicates a hardware breakpoint as provided by the CPU.
321 Breakpoints can be read/write accesses to an address as well as
322 execution of an instruction address.
326 .\" commit 2e80a82a49c4c7eca4e35734380f28298ba5db19
327 .BR perf_event_open ()
328 can support multiple PMUs.
329 To enable this, a value exported by the kernel can be used in the
331 field to indicate which PMU to use.
332 The value to use can be found in the sysfs filesystem:
333 there is a subdirectory per PMU instance under
334 .IR /sys/bus/event_source/devices .
335 In each subdirectory there is a
337 file whose content is an integer that can be used in the
341 .I /sys/bus/event_source/devices/cpu/type
342 contains the value for the core CPU PMU, which is usually 4.
344 .BR kprobe " and " uprobe " (since Linux 4.17)"
345 .\" commit 65074d43fc77bcae32776724b7fa2696923c78e4
346 .\" commit e12f03d7031a977356e3d7b75a68c2185ff8d155
347 .\" commit 33ea4b24277b06dbc55d7f5772a46f029600255e
348 These two dynamic PMUs create a kprobe/uprobe and attach it to the
349 file descriptor generated by perf_event_open.
350 The kprobe/uprobe will be destroyed on the destruction of the file descriptor.
352 .IR kprobe_func ", " uprobe_path ", " kprobe_addr ", and " probe_offset
359 structure for forward/backward compatibility.
361 .I sizeof(struct perf_event_attr)
362 to allow the kernel to see
363 the struct size at the time of compilation.
366 .B PERF_ATTR_SIZE_VER0
367 is set to 64; this was the size of the first published struct.
368 .B PERF_ATTR_SIZE_VER1
369 is 72, corresponding to the addition of breakpoints in Linux 2.6.33.
370 .\" commit cb5d76999029ae7a517cb07dfa732c1b5a934fc2
371 .\" this was added much later when PERF_ATTR_SIZE_VER2 happened
372 .\" but the actual attr_size had increased in 2.6.33
373 .B PERF_ATTR_SIZE_VER2
374 is 80 corresponding to the addition of branch sampling in Linux 3.4.
375 .\" commit cb5d76999029ae7a517cb07dfa732c1b5a934fc2
376 .B PERF_ATTR_SIZE_VER3
377 is 96 corresponding to the addition
383 .\" commit 1659d129ed014b715b0b2120e6fd929bdd33ed03
384 .B PERF_ATTR_SIZE_VER4
385 is 104 corresponding to the addition of
388 .\" commit 60e2364e60e86e81bc6377f49779779e6120977f
389 .B PERF_ATTR_SIZE_VER5
390 is 112 corresponding to the addition of
393 .\" commit 1a5941312414c71dece6717da9a0fa1303127afa
396 This specifies which event you want, in conjunction with
401 .IR config1 " and " config2
402 fields are also taken into account in cases where 64 bits is not
403 enough to fully specify the event.
404 The encoding of these fields are event dependent.
406 There are various ways to set the
408 field that are dependent on the value of the previously
412 What follows are various possible settings for
420 .BR PERF_TYPE_HARDWARE ,
421 we are measuring one of the generalized hardware CPU events.
422 Not all of these are available on all platforms.
425 to one of the following:
428 .B PERF_COUNT_HW_CPU_CYCLES
430 Be wary of what happens during CPU frequency scaling.
432 .B PERF_COUNT_HW_INSTRUCTIONS
433 Retired instructions.
434 Be careful, these can be affected by various
435 issues, most notably hardware interrupt counts.
437 .B PERF_COUNT_HW_CACHE_REFERENCES
439 Usually this indicates Last Level Cache accesses but this may
440 vary depending on your CPU.
441 This may include prefetches and coherency messages; again this
442 depends on the design of your CPU.
444 .B PERF_COUNT_HW_CACHE_MISSES
446 Usually this indicates Last Level Cache misses; this is intended to be
447 used in conjunction with the
448 .B PERF_COUNT_HW_CACHE_REFERENCES
449 event to calculate cache miss rates.
451 .B PERF_COUNT_HW_BRANCH_INSTRUCTIONS
452 Retired branch instructions.
453 Prior to Linux 2.6.35, this used
454 the wrong event on AMD processors.
455 .\" commit f287d332ce835f77a4f5077d2c0ef1e3f9ea42d2
457 .B PERF_COUNT_HW_BRANCH_MISSES
458 Mispredicted branch instructions.
460 .B PERF_COUNT_HW_BUS_CYCLES
461 Bus cycles, which can be different from total cycles.
463 .BR PERF_COUNT_HW_STALLED_CYCLES_FRONTEND " (since Linux 3.0)"
464 .\" commit 8f62242246351b5a4bc0c1f00c0c7003edea128a
465 Stalled cycles during issue.
467 .BR PERF_COUNT_HW_STALLED_CYCLES_BACKEND " (since Linux 3.0)"
468 .\" commit 8f62242246351b5a4bc0c1f00c0c7003edea128a
469 Stalled cycles during retirement.
471 .BR PERF_COUNT_HW_REF_CPU_CYCLES " (since Linux 3.3)"
472 .\" commit c37e17497e01fc0f5d2d6feb5723b210b3ab8890
473 Total cycles; not affected by CPU frequency scaling.
479 .BR PERF_TYPE_SOFTWARE ,
480 we are measuring software events provided by the kernel.
483 to one of the following:
486 .B PERF_COUNT_SW_CPU_CLOCK
487 This reports the CPU clock, a high-resolution per-CPU timer.
489 .B PERF_COUNT_SW_TASK_CLOCK
490 This reports a clock count specific to the task that is running.
492 .B PERF_COUNT_SW_PAGE_FAULTS
493 This reports the number of page faults.
495 .B PERF_COUNT_SW_CONTEXT_SWITCHES
496 This counts context switches.
497 Until Linux 2.6.34, these were all reported as user-space
498 events, after that they are reported as happening in the kernel.
499 .\" commit e49a5bd38159dfb1928fd25b173bc9de4bbadb21
501 .B PERF_COUNT_SW_CPU_MIGRATIONS
502 This reports the number of times the process
503 has migrated to a new CPU.
505 .B PERF_COUNT_SW_PAGE_FAULTS_MIN
506 This counts the number of minor page faults.
507 These did not require disk I/O to handle.
509 .B PERF_COUNT_SW_PAGE_FAULTS_MAJ
510 This counts the number of major page faults.
511 These required disk I/O to handle.
513 .BR PERF_COUNT_SW_ALIGNMENT_FAULTS " (since Linux 2.6.33)"
514 .\" commit f7d7986060b2890fc26db6ab5203efbd33aa2497
515 This counts the number of alignment faults.
516 These happen when unaligned memory accesses happen; the kernel
517 can handle these but it reduces performance.
518 This happens only on some architectures (never on x86).
520 .BR PERF_COUNT_SW_EMULATION_FAULTS " (since Linux 2.6.33)"
521 .\" commit f7d7986060b2890fc26db6ab5203efbd33aa2497
522 This counts the number of emulation faults.
523 The kernel sometimes traps on unimplemented instructions
524 and emulates them for user space.
525 This can negatively impact performance.
527 .BR PERF_COUNT_SW_DUMMY " (since Linux 3.12)"
528 .\" commit fa0097ee690693006ab1aea6c01ad3c851b65c77
529 This is a placeholder event that counts nothing.
530 Informational sample record types such as mmap or comm
531 must be associated with an active event.
532 This dummy event allows gathering such records without requiring
540 .BR PERF_TYPE_TRACEPOINT ,
541 then we are measuring kernel tracepoints.
544 can be obtained from under debugfs
545 .I tracing/events/*/*/id
546 if ftrace is enabled in the kernel.
553 .BR PERF_TYPE_HW_CACHE ,
554 then we are measuring a hardware CPU cache event.
555 To calculate the appropriate
557 value use the following equation:
561 (perf_hw_cache_id) | (perf_hw_cache_op_id << 8) |
562 (perf_hw_cache_op_result_id << 16)
570 .B PERF_COUNT_HW_CACHE_L1D
571 for measuring Level 1 Data Cache
573 .B PERF_COUNT_HW_CACHE_L1I
574 for measuring Level 1 Instruction Cache
576 .B PERF_COUNT_HW_CACHE_LL
577 for measuring Last-Level Cache
579 .B PERF_COUNT_HW_CACHE_DTLB
580 for measuring the Data TLB
582 .B PERF_COUNT_HW_CACHE_ITLB
583 for measuring the Instruction TLB
585 .B PERF_COUNT_HW_CACHE_BPU
586 for measuring the branch prediction unit
588 .BR PERF_COUNT_HW_CACHE_NODE " (since Linux 3.1)"
589 .\" commit 89d6c0b5bdbb1927775584dcf532d98b3efe1477
590 for measuring local memory accesses
594 .I perf_hw_cache_op_id
598 .B PERF_COUNT_HW_CACHE_OP_READ
601 .B PERF_COUNT_HW_CACHE_OP_WRITE
604 .B PERF_COUNT_HW_CACHE_OP_PREFETCH
605 for prefetch accesses
609 .I perf_hw_cache_op_result_id
613 .B PERF_COUNT_HW_CACHE_RESULT_ACCESS
616 .B PERF_COUNT_HW_CACHE_RESULT_MISS
628 Most CPUs support events that are not covered by the "generalized" events.
629 These are implementation defined; see your CPU manual (for example
630 the Intel Volume 3B documentation or the AMD BIOS and Kernel Developer
632 The libpfm4 library can be used to translate from the name in the
633 architectural manuals to the raw hex value
634 .BR perf_event_open ()
635 expects in this field.
640 .BR PERF_TYPE_BREAKPOINT ,
644 Its parameters are set in other places.
657 .IR /sys/bus/event_source/devices/[k,u]probe/format/retprobe )
658 for kretprobe/uretprobe.
660 .IR kprobe_func ", " uprobe_path ", " kprobe_addr ", and " probe_offset
664 .IR kprobe_func ", " uprobe_path ", " kprobe_addr ", and " probe_offset
665 These fields describe the kprobe/uprobe for dynamic PMUs
687 .IR sample_period ", " sample_freq
688 A "sampling" event is one that generates an overflow notification
689 every N events, where N is given by
692 .IR sample_period " > 0."
693 When an overflow occurs, requested data is recorded
697 field controls what data is recorded on each overflow.
700 can be used if you wish to use frequency rather than period.
701 In this case, you set the
704 The kernel will adjust the sampling period
705 to try and achieve the desired rate.
706 The rate of adjustment is a
710 The various bits in this field specify which values to include
712 They will be recorded in a ring-buffer,
713 which is available to user space using
715 The order in which the values are saved in the
716 sample are documented in the MMAP Layout subsection below;
718 .I "enum perf_event_sample_format"
723 Records instruction pointer.
726 Records the process and thread IDs.
732 Records an address, if applicable.
735 Record counter values for all events in a group, not just the group leader.
737 .B PERF_SAMPLE_CALLCHAIN
738 Records the callchain (stack backtrace).
741 Records a unique ID for the opened event's group leader.
746 .B PERF_SAMPLE_PERIOD
747 Records the current sampling period.
749 .B PERF_SAMPLE_STREAM_ID
750 Records a unique ID for the opened event.
753 the actual ID is returned, not the group leader.
754 This ID is the same as the one returned by
758 Records additional data, if applicable.
759 Usually returned by tracepoint events.
761 .BR PERF_SAMPLE_BRANCH_STACK " (since Linux 3.4)"
762 .\" commit bce38cd53e5ddba9cb6d708c4ef3d04a4016ec7e
763 This provides a record of recent branches, as provided
764 by CPU branch sampling hardware (such as Intel Last Branch Record).
765 Not all hardware supports this feature.
768 .I branch_sample_type
769 field for how to filter which branches are reported.
771 .BR PERF_SAMPLE_REGS_USER " (since Linux 3.7)"
772 .\" commit 4018994f3d8785275ef0e7391b75c3462c029e56
773 Records the current user-level CPU register state
774 (the values in the process before the kernel was called).
776 .BR PERF_SAMPLE_STACK_USER " (since Linux 3.7)"
777 .\" commit c5ebcedb566ef17bda7b02686e0d658a7bb42ee7
778 Records the user level stack, allowing stack unwinding.
780 .BR PERF_SAMPLE_WEIGHT " (since Linux 3.10)"
781 .\" commit c3feedf2aaf9ac8bad6f19f5d21e4ee0b4b87e9c
782 Records a hardware provided weight value that expresses how
783 costly the sampled event was.
784 This allows the hardware to highlight expensive events in
787 .BR PERF_SAMPLE_DATA_SRC " (since Linux 3.10)"
788 .\" commit d6be9ad6c960f43800a6f118932bc8a5a4eadcd1
789 Records the data source: where in the memory hierarchy
790 the data associated with the sampled instruction came from.
791 This is available only if the underlying hardware
792 supports this feature.
794 .BR PERF_SAMPLE_IDENTIFIER " (since Linux 3.12)"
795 .\" commit ff3d527cebc1fa3707c617bfe9e74f53fcfb0955
798 value in a fixed position in the record,
799 either at the beginning (for sample events) or at the end
800 (if a non-sample event).
802 This was necessary because a sample stream may have
803 records from various different event sources with different
806 Parsing the event stream properly was not possible because the
807 format of the record was needed to find
810 the format could not be found without knowing what
811 event the sample belonged to (causing a circular
815 .B PERF_SAMPLE_IDENTIFIER
816 setting makes the event stream always parsable
819 in a fixed location, even though
820 it means having duplicate
824 .BR PERF_SAMPLE_TRANSACTION " (since Linux 3.13)"
825 .\" commit fdfbbd07e91f8fe387140776f3fd94605f0c89e5
826 Records reasons for transactional memory abort events
827 (for example, from Intel TSX transactional memory support).
831 setting must be greater than 0 and a transactional memory abort
832 event must be measured or no values will be recorded.
833 Also note that some perf_event measurements, such as sampled
834 cycle counting, may cause extraneous aborts (by causing an
835 interrupt during a transaction).
837 .BR PERF_SAMPLE_REGS_INTR " (since Linux 3.19)"
838 .\" commit 60e2364e60e86e81bc6377f49779779e6120977f
839 Records a subset of the current CPU register state
841 .IR sample_regs_intr .
843 .B PERF_SAMPLE_REGS_USER
844 the register values will return kernel register
845 state if the overflow happened while kernel
847 If the CPU supports hardware sampling of
848 register state (i.e., PEBS on Intel x86) and
850 is set higher than zero then the register
851 values returned are those captured by
852 hardware at the time of the sampled
853 instruction's retirement.
857 This field specifies the format of the data returned by
860 .BR perf_event_open ()
864 .B PERF_FORMAT_TOTAL_TIME_ENABLED
868 This can be used to calculate estimated totals if
869 the PMU is overcommitted and multiplexing is happening.
871 .B PERF_FORMAT_TOTAL_TIME_RUNNING
875 This can be used to calculate estimated totals if
876 the PMU is overcommitted and multiplexing is happening.
879 Adds a 64-bit unique value that corresponds to the event group.
882 Allows all counter values in an event group to be read with one read.
888 bit specifies whether the counter starts out disabled or enabled.
889 If disabled, the event can later be enabled by
895 When creating an event group, typically the group leader is initialized
898 set to 1 and any child events are initialized with
903 being 0, the child events will not start until the group leader
909 bit specifies that this counter should count events of child
910 tasks as well as the task specified.
911 This applies only to new children, not to any existing children at
912 the time the counter is created (nor to any new children of
915 Inherit does not work for some combinations of
918 .BR PERF_FORMAT_GROUP .
923 bit specifies that the counter should always be on the CPU if at all
925 It applies only to hardware counters and only to group leaders.
926 If a pinned counter cannot be put onto the CPU (e.g., because there are
927 not enough hardware counters or because of a conflict with some other
928 event), then the counter goes into an 'error' state, where reads
929 return end-of-file (i.e.,
931 returns 0) until the counter is subsequently enabled or disabled.
936 bit specifies that when this counter's group is on the CPU,
937 it should be the only group using the CPU's counters.
938 In the future this may allow monitoring programs to
939 support PMU features that need to run alone so that they do not
940 disrupt other hardware counters.
942 Note that many unexpected situations may prevent events with the
944 bit set from ever running.
945 This includes any users running a system-wide
946 measurement as well as any kernel use of the performance counters
947 (including the commonly enabled NMI Watchdog Timer interface).
950 If this bit is set, the count excludes events that happen in user space.
953 If this bit is set, the count excludes events that happen in kernel space.
956 If this bit is set, the count excludes events that happen in the
958 This is mainly for PMUs that have built-in support for handling this
960 Extra support is needed for handling hypervisor measurements on most
964 If set, don't count when the CPU is running the idle task.
965 While you can currently enable this for any event type, it is ignored
966 for all but software events.
971 bit enables generation of
978 This allows tools to notice new executable code being mapped into
979 a program (dynamic shared libraries for example)
980 so that addresses can be mapped back to the original code.
985 bit enables tracking of process command name as modified by the
988 .BR prctl (PR_SET_NAME)
989 system calls as well as writing to
990 .IR /proc/self/comm .
993 flag is also successfully set (possible since Linux 3.16),
994 .\" commit 82b897782d10fcc4930c9d4a15b175348fdd2871
996 .B PERF_RECORD_MISC_COMM_EXEC
997 can be used to differentiate the
999 case from the others.
1002 If this bit is set, then
1006 is used when setting up the sampling interval.
1009 This bit enables saving of event counts on context switch for
1011 This is meaningful only if the
1015 .IR "enable_on_exec"
1016 If this bit is set, a counter is automatically
1017 enabled after a call to
1021 If this bit is set, then
1022 fork/exit notifications are included in the ring buffer.
1025 If set, have an overflow notification happen when we cross the
1028 Otherwise, overflow notifications happen after
1032 .IR "precise_ip" " (since Linux 2.6.35)"
1033 .\" commit ab608344bcbde4f55ec4cd911b686b0ce3eae076
1034 This controls the amount of skid.
1035 Skid is how many instructions
1036 execute between an event of interest happening and the kernel
1037 being able to stop and record the event.
1039 better and allows more accurate reporting of which events
1040 correspond to which instructions, but hardware is often limited
1041 with how small this can be.
1043 The possible values of this field are the following:
1047 can have arbitrary skid.
1050 must have constant skid.
1053 requested to have 0 skid.
1057 See also the description of
1058 .BR PERF_RECORD_MISC_EXACT_IP .
1061 .IR "mmap_data" " (since Linux 2.6.36)"
1062 .\" commit 3af9e859281bda7eb7c20b51879cf43aa788ac2e
1063 This is the counterpart of the
1066 This enables generation of
1070 calls that do not have
1072 set (for example data and SysV shared memory).
1074 .IR "sample_id_all" " (since Linux 2.6.38)"
1075 .\" commit c980d1091810df13f21aabbce545fd98f545bbf7
1076 If set, then TID, TIME, ID, STREAM_ID, and CPU can
1077 additionally be included in
1078 .RB non- PERF_RECORD_SAMPLE s
1079 if the corresponding
1084 .B PERF_SAMPLE_IDENTIFIER
1085 is specified, then an additional ID value is included
1086 as the last value to ease parsing the record stream.
1087 This may lead to the
1089 value appearing twice.
1091 The layout is described by this pseudo-structure:
1096 { u32 pid, tid; } /* if PERF_SAMPLE_TID set */
1097 { u64 time; } /* if PERF_SAMPLE_TIME set */
1098 { u64 id; } /* if PERF_SAMPLE_ID set */
1099 { u64 stream_id;} /* if PERF_SAMPLE_STREAM_ID set */
1100 { u32 cpu, res; } /* if PERF_SAMPLE_CPU set */
1101 { u64 id; } /* if PERF_SAMPLE_IDENTIFIER set */
1106 .IR "exclude_host" " (since Linux 3.2)"
1107 .\" commit a240f76165e6255384d4bdb8139895fac7988799
1108 When conducting measurements that include processes running
1109 VM instances (i.e., have executed a
1112 only measure events happening inside a guest instance.
1113 This is only meaningful outside the guests; this setting does
1114 not change counts gathered inside of a guest.
1115 Currently, this functionality is x86 only.
1117 .IR "exclude_guest" " (since Linux 3.2)"
1118 .\" commit a240f76165e6255384d4bdb8139895fac7988799
1119 When conducting measurements that include processes running
1120 VM instances (i.e., have executed a
1123 do not measure events happening inside guest instances.
1124 This is only meaningful outside the guests; this setting does
1125 not change counts gathered inside of a guest.
1126 Currently, this functionality is x86 only.
1128 .IR "exclude_callchain_kernel" " (since Linux 3.7)"
1129 .\" commit d077526485d5c9b12fe85d0b2b3b7041e6bc5f91
1130 Do not include kernel callchains.
1132 .IR "exclude_callchain_user" " (since Linux 3.7)"
1133 .\" commit d077526485d5c9b12fe85d0b2b3b7041e6bc5f91
1134 Do not include user callchains.
1136 .IR "mmap2" " (since Linux 3.16)"
1137 .\" commit 13d7a2410fa637f450a29ecb515ac318ee40c741
1138 .\" This is tricky; was committed during 3.12 development
1139 .\" but right before release was disabled.
1140 .\" So while you could select mmap2 starting with 3.12
1141 .\" it did not work until 3.16
1142 .\" commit a5a5ba72843dd05f991184d6cb9a4471acce1005
1143 Generate an extended executable mmap record that contains enough
1144 additional information to uniquely identify shared mappings.
1147 flag must also be set for this to work.
1149 .IR "comm_exec" " (since Linux 3.16)"
1150 .\" commit 82b897782d10fcc4930c9d4a15b175348fdd2871
1151 This is purely a feature-detection flag, it does not change
1153 If this flag can successfully be set, then, when
1156 .B PERF_RECORD_MISC_COMM_EXEC
1157 flag will be set in the
1159 field of a comm record header if the rename event being
1160 reported was caused by a call to
1162 This allows tools to distinguish between the various
1163 types of process renaming.
1165 .IR "use_clockid" " (since Linux 4.1)"
1166 .\" commit 34f439278cef7b1177f8ce24f9fc81dfc6221d3b
1167 This allows selecting which internal Linux clock to use
1168 when generating timestamps via the
1171 This can make it easier to correlate perf sample times with
1172 timestamps generated by other tools.
1174 .IR "context_switch" " (since Linux 4.3)"
1175 .\" commit 45ac1403f564f411c6a383a2448688ba8dd705a4
1176 This enables the generation of
1177 .B PERF_RECORD_SWITCH
1178 records when a context switch occurs.
1179 It also enables the generation of
1180 .B PERF_RECORD_SWITCH_CPU_WIDE
1181 records when sampling in CPU-wide mode.
1182 This functionality is in addition to existing tracepoint and
1183 software events for measuring context switches.
1184 The advantage of this method is that it will give full
1185 information even with strict
1186 .I perf_event_paranoid
1189 .IR "wakeup_events" ", " "wakeup_watermark"
1190 This union sets how many samples
1191 .RI ( wakeup_events )
1193 .RI ( wakeup_watermark )
1194 happen before an overflow notification happens.
1195 Which one is used is selected by the
1201 .B PERF_RECORD_SAMPLE
1203 To receive overflow notification for all
1205 types choose watermark and set
1209 Prior to Linux 3.0, setting
1210 .\" commit f506b3dc0ec454a16d40cab9ee5d75435b39dc50
1212 to 0 resulted in no overflow notifications;
1213 more recent kernels treat 0 the same as 1.
1215 .IR "bp_type" " (since Linux 2.6.33)"
1216 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
1217 This chooses the breakpoint type.
1221 .BR HW_BREAKPOINT_EMPTY
1225 Count when we read the memory location.
1228 Count when we write the memory location.
1230 .BR HW_BREAKPOINT_RW
1231 Count when we read or write the memory location.
1234 Count when we execute code at the memory location.
1236 The values can be combined via a bitwise or, but the
1246 .IR "bp_addr" " (since Linux 2.6.33)"
1247 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
1248 This is the address of the breakpoint.
1249 For execution breakpoints, this is the memory address of the instruction
1250 of interest; for read and write breakpoints, it is the memory address
1251 of the memory location of interest.
1253 .IR "config1" " (since Linux 2.6.39)"
1254 .\" commit a7e3ed1e470116c9d12c2f778431a481a6be8ab6
1256 is used for setting events that need an extra register or otherwise
1257 do not fit in the regular config field.
1258 Raw OFFCORE_EVENTS on Nehalem/Westmere/SandyBridge use this field
1259 on Linux 3.3 and later kernels.
1261 .IR "bp_len" " (since Linux 2.6.33)"
1262 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
1264 is the length of the breakpoint being measured if
1267 .BR PERF_TYPE_BREAKPOINT .
1269 .BR HW_BREAKPOINT_LEN_1 ,
1270 .BR HW_BREAKPOINT_LEN_2 ,
1271 .BR HW_BREAKPOINT_LEN_4 ,
1273 .BR HW_BREAKPOINT_LEN_8 .
1274 For an execution breakpoint, set this to
1277 .IR "config2" " (since Linux 2.6.39)"
1278 .\" commit a7e3ed1e470116c9d12c2f778431a481a6be8ab6
1280 is a further extension of the
1284 .IR "branch_sample_type" " (since Linux 3.4)"
1285 .\" commit bce38cd53e5ddba9cb6d708c4ef3d04a4016ec7e
1287 .B PERF_SAMPLE_BRANCH_STACK
1288 is enabled, then this specifies what branches to include
1289 in the branch record.
1291 The first part of the value is the privilege level, which
1292 is a combination of one of the values listed below.
1293 If the user does not set privilege level explicitly, the kernel
1294 will use the event's privilege level.
1295 Event and branch privilege levels do not have to match.
1298 .B PERF_SAMPLE_BRANCH_USER
1299 Branch target is in user space.
1301 .B PERF_SAMPLE_BRANCH_KERNEL
1302 Branch target is in kernel space.
1304 .B PERF_SAMPLE_BRANCH_HV
1305 Branch target is in hypervisor.
1307 .B PERF_SAMPLE_BRANCH_PLM_ALL
1308 A convenience value that is the three preceding values ORed together.
1310 In addition to the privilege value, at least one or more of the
1311 following bits must be set.
1313 .B PERF_SAMPLE_BRANCH_ANY
1316 .B PERF_SAMPLE_BRANCH_ANY_CALL
1317 Any call branch (includes direct calls, indirect calls, and far jumps).
1319 .B PERF_SAMPLE_BRANCH_IND_CALL
1322 .BR PERF_SAMPLE_BRANCH_CALL " (since Linux 4.4)"
1323 .\" commit c229bf9dc179d2023e185c0f705bdf68484c1e73
1326 .B PERF_SAMPLE_BRANCH_ANY_RETURN
1329 .BR PERF_SAMPLE_BRANCH_IND_JUMP " (since Linux 4.2)"
1330 .\" commit c9fdfa14c3792c0160849c484e83aa57afd80ccc
1333 .BR PERF_SAMPLE_BRANCH_COND " (since Linux 3.16)"
1334 .\" commit bac52139f0b7ab31330e98fd87fc5a2664951050
1335 Conditional branches.
1337 .BR PERF_SAMPLE_BRANCH_ABORT_TX " (since Linux 3.11)"
1338 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
1339 Transactional memory aborts.
1341 .BR PERF_SAMPLE_BRANCH_IN_TX " (since Linux 3.11)"
1342 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
1343 Branch in transactional memory transaction.
1345 .BR PERF_SAMPLE_BRANCH_NO_TX " (since Linux 3.11)"
1346 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
1347 Branch not in transactional memory transaction.
1348 .BR PERF_SAMPLE_BRANCH_CALL_STACK " (since Linux 4.1)"
1349 .\" commit 2c44b1936bb3b135a3fac8b3493394d42e51cf70
1350 Branch is part of a hardware-generated call stack.
1351 This requires hardware support, currently only found
1352 on Intel x86 Haswell or newer.
1355 .IR "sample_regs_user" " (since Linux 3.7)"
1356 .\" commit 4018994f3d8785275ef0e7391b75c3462c029e56
1357 This bit mask defines the set of user CPU registers to dump on samples.
1358 The layout of the register mask is architecture-specific and
1359 is described in the kernel header file
1360 .IR arch/ARCH/include/uapi/asm/perf_regs.h .
1362 .IR "sample_stack_user" " (since Linux 3.7)"
1363 .\" commit c5ebcedb566ef17bda7b02686e0d658a7bb42ee7
1364 This defines the size of the user stack to dump if
1365 .B PERF_SAMPLE_STACK_USER
1368 .IR "clockid" " (since Linux 4.1)"
1369 .\" commit 34f439278cef7b1177f8ce24f9fc81dfc6221d3b
1372 is set, then this field selects which internal Linux timer to
1374 The available timers are defined in
1377 .BR CLOCK_MONOTONIC ,
1378 .BR CLOCK_MONOTONIC_RAW ,
1379 .BR CLOCK_REALTIME ,
1380 .BR CLOCK_BOOTTIME ,
1383 currently supported.
1385 .IR "aux_watermark" " (since Linux 4.1)"
1386 .\" commit 1a5941312414c71dece6717da9a0fa1303127afa
1387 This specifies how much data is required to trigger a
1391 .IR "sample_max_stack" " (since Linux 4.8)"
1392 .\" commit 97c79a38cd454602645f0470ffb444b3b75ce574
1396 .BR PERF_SAMPLE_CALLCHAIN ,
1397 this field specifies how many stack frames to report when
1398 generating the callchain.
1401 .BR perf_event_open ()
1402 file descriptor has been opened, the values
1403 of the events can be read from the file descriptor.
1404 The values that are there are specified by the
1408 structure at open time.
1410 If you attempt to read into a buffer that is not big enough to hold the
1415 Here is the layout of the data returned by a read:
1418 .B PERF_FORMAT_GROUP
1419 was specified to allow reading all events in a group at once:
1423 struct read_format {
1424 u64 nr; /* The number of events */
1425 u64 time_enabled; /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
1426 u64 time_running; /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
1428 u64 value; /* The value of the event */
1429 u64 id; /* if PERF_FORMAT_ID */
1436 .B PERF_FORMAT_GROUP
1443 struct read_format {
1444 u64 value; /* The value of the event */
1445 u64 time_enabled; /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
1446 u64 time_running; /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
1447 u64 id; /* if PERF_FORMAT_ID */
1452 The values read are as follows:
1455 The number of events in this file descriptor.
1457 .B PERF_FORMAT_GROUP
1460 .IR time_enabled ", " time_running
1461 Total time the event was enabled and running.
1462 Normally these values are the same.
1463 Multiplexing happens if the number of events is more than the
1464 number of available PMU counter slots.
1465 In that case the events run only part of the time and the
1469 values can be used to scale an estimated value for the count.
1472 An unsigned 64-bit value containing the counter result.
1475 A globally unique value for this particular event; only present if
1481 .BR perf_event_open ()
1482 in sampled mode, asynchronous events
1483 (like counter overflow or
1486 are logged into a ring-buffer.
1487 This ring-buffer is created and accessed through
1490 The mmap size should be 1+2^n pages, where the first page is a
1492 .RI ( "struct perf_event_mmap_page" )
1493 that contains various
1494 bits of information such as where the ring-buffer head is.
1496 Before kernel 2.6.39, there is a bug that means you must allocate an mmap
1497 ring buffer when sampling even if you do not plan to access it.
1499 The structure of the first metadata mmap page is as follows:
1503 struct perf_event_mmap_page {
1504 __u32 version; /* version number of this structure */
1505 __u32 compat_version; /* lowest version this is compat with */
1506 __u32 lock; /* seqlock for synchronization */
1507 __u32 index; /* hardware counter identifier */
1508 __s64 offset; /* add to hardware counter value */
1509 __u64 time_enabled; /* time event active */
1510 __u64 time_running; /* time event on CPU */
1514 __u64 cap_usr_time / cap_usr_rdpmc / cap_bit0 : 1,
1515 cap_bit0_is_deprecated : 1,
1518 cap_user_time_zero : 1,
1525 __u64 __reserved[120]; /* Pad to 1 k */
1526 __u64 data_head; /* head in the data section */
1527 __u64 data_tail; /* user-space written tail */
1528 __u64 data_offset; /* where the buffer starts */
1529 __u64 data_size; /* data buffer size */
1539 The following list describes the fields in the
1540 .I perf_event_mmap_page
1541 structure in more detail:
1544 Version number of this structure.
1547 The lowest version this is compatible with.
1550 A seqlock for synchronization.
1553 A unique hardware counter identifier.
1556 When using rdpmc for reads this offset value
1557 must be added to the one returned by rdpmc to get
1558 the current total event count.
1561 Time the event was active.
1564 Time the event was running.
1566 .IR cap_usr_time " / " cap_usr_rdpmc " / " cap_bit0 " (since Linux 3.4)"
1567 .\" commit c7206205d00ab375839bd6c7ddb247d600693c09
1568 There was a bug in the definition of
1572 from Linux 3.4 until Linux 3.11.
1573 Both bits were defined to point to the same location, so it was
1574 impossible to know if
1580 Starting with Linux 3.12, these are renamed to
1581 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1583 and you should use the
1589 .IR cap_bit0_is_deprecated " (since Linux 3.12)"
1590 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1591 If set, this bit indicates that the kernel supports
1592 the properly separated
1598 If not-set, it indicates an older kernel where
1602 map to the same bit and thus both features should
1603 be used with caution.
1605 .IR cap_user_rdpmc " (since Linux 3.12)"
1606 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1607 If the hardware supports user-space read of performance counters
1608 without syscall (this is the "rdpmc" instruction on x86), then
1609 the following code can be used to do a read:
1613 u32 seq, time_mult, time_shift, idx, width;
1614 u64 count, enabled, running;
1615 u64 cyc, time_offset;
1620 enabled = pc\->time_enabled;
1621 running = pc\->time_running;
1623 if (pc\->cap_usr_time && enabled != running) {
1625 time_offset = pc\->time_offset;
1626 time_mult = pc\->time_mult;
1627 time_shift = pc\->time_shift;
1631 count = pc\->offset;
1633 if (pc\->cap_usr_rdpmc && idx) {
1634 width = pc\->pmc_width;
1635 count += rdpmc(idx \- 1);
1639 } while (pc\->lock != seq);
1643 .IR cap_user_time " (since Linux 3.12)"
1644 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1645 This bit indicates the hardware has a constant, nonstop
1646 timestamp counter (TSC on x86).
1648 .IR cap_user_time_zero " (since Linux 3.12)"
1649 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1650 Indicates the presence of
1652 which allows mapping timestamp values to
1658 this field provides the bit-width of the value
1659 read using the rdpmc or equivalent instruction.
1660 This can be used to sign extend the result like:
1664 pmc <<= 64 \- pmc_width;
1665 pmc >>= 64 \- pmc_width; // signed shift right
1670 .IR time_shift ", " time_mult ", " time_offset
1674 these fields can be used to compute the time
1677 (in nanoseconds) using rdtsc or similar.
1682 quot = (cyc >> time_shift);
1683 rem = cyc & (((u64)1 << time_shift) \- 1);
1684 delta = time_offset + quot * time_mult +
1685 ((rem * time_mult) >> time_shift);
1695 seqcount loop described above.
1696 This delta can then be added to
1697 enabled and possible running (if idx), improving the scaling:
1703 quot = count / running;
1704 rem = count % running;
1705 count = quot * enabled + (rem * enabled) / running;
1708 .IR time_zero " (since Linux 3.12)"
1709 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1712 .I cap_usr_time_zero
1713 is set, then the hardware clock (the TSC timestamp counter on x86)
1714 can be calculated from the
1715 .IR time_zero ", " time_mult ", and " time_shift " values:"
1718 time = timestamp - time_zero;
1719 quot = time / time_mult;
1720 rem = time % time_mult;
1721 cyc = (quot << time_shift) + (rem << time_shift) / time_mult;
1727 quot = cyc >> time_shift;
1728 rem = cyc & (((u64)1 << time_shift) - 1);
1729 timestamp = time_zero + quot * time_mult +
1730 ((rem * time_mult) >> time_shift);
1734 This points to the head of the data section.
1735 The value continuously increases, it does not wrap.
1736 The value needs to be manually wrapped by the size of the mmap buffer
1737 before accessing the samples.
1739 On SMP-capable platforms, after reading the
1742 user space should issue an rmb().
1749 value should be written by user space to reflect the last read data.
1750 In this case, the kernel will not overwrite unread data.
1752 .IR data_offset " (since Linux 4.1)"
1753 .\" commit e8c6deac69629c0cb97c3d3272f8631ef17f8f0f
1754 Contains the offset of the location in the mmap buffer
1755 where perf sample data begins.
1757 .IR data_size " (since Linux 4.1)"
1758 .\" commit e8c6deac69629c0cb97c3d3272f8631ef17f8f0f
1759 Contains the size of the perf sample region within
1762 .IR aux_head ", " aux_tail ", " aux_offset ", " aux_size " (since Linux 4.1)
1763 .\" commit 45bfb2e50471abbbfd83d40d28c986078b0d24ff
1764 The AUX region allows mmaping a separate sample buffer for
1765 high-bandwidth data streams (separate from the main perf sample buffer).
1766 An example of a high-bandwidth stream is instruction tracing support,
1767 as is found in newer Intel processors.
1769 To set up an AUX area, first
1771 needs to be set with an offset greater than
1772 .IR data_offset + data_size
1775 needs to be set to the desired buffer size.
1776 The desired offset and size must be page aligned, and the size
1777 must be a power of two.
1778 These values are then passed to mmap in order to map the AUX buffer.
1779 Pages in the AUX buffer are included as part of the
1783 and also as part of the
1784 .I perf_event_mlock_kb
1787 By default, the AUX buffer will be truncated if it will not fit
1788 in the available space in the ring buffer.
1789 If the AUX buffer is mapped as a read only buffer, then it will
1790 operate in ring buffer mode where old data will be overwritten
1792 In overwrite mode, it might not be possible to infer where the
1793 new data began, and it is the consumer's job to disable
1794 measurement while reading to avoid possible data races.
1797 .IR aux_head " and " aux_tail
1798 ring buffer pointers have the same behavior and ordering
1799 rules as the previous described
1800 .IR data_head " and " data_tail .
1802 The following 2^n ring-buffer pages have the layout described below.
1805 .I perf_event_attr.sample_id_all
1806 is set, then all event types will
1807 have the sample_type selected fields related to where/when (identity)
1808 an event took place (TID, TIME, ID, CPU, STREAM_ID) described in
1809 .B PERF_RECORD_SAMPLE
1810 below, it will be stashed just after the
1811 .I perf_event_header
1812 and the fields already present for the existing
1813 fields, that is, at the end of the payload.
1814 This allows a newer perf.data
1815 file to be supported by older perf tools, with the new optional
1816 fields being ignored.
1818 The mmap values start with a header:
1822 struct perf_event_header {
1830 Below, we describe the
1831 .I perf_event_header
1832 fields in more detail.
1833 For ease of reading,
1834 the fields with shorter descriptions are presented first.
1837 This indicates the size of the record.
1842 field contains additional information about the sample.
1844 The CPU mode can be determined from this value by masking with
1845 .B PERF_RECORD_MISC_CPUMODE_MASK
1846 and looking for one of the following (note these are not
1847 bit masks, only one can be set at a time):
1850 .B PERF_RECORD_MISC_CPUMODE_UNKNOWN
1853 .B PERF_RECORD_MISC_KERNEL
1854 Sample happened in the kernel.
1856 .B PERF_RECORD_MISC_USER
1857 Sample happened in user code.
1859 .B PERF_RECORD_MISC_HYPERVISOR
1860 Sample happened in the hypervisor.
1862 .BR PERF_RECORD_MISC_GUEST_KERNEL " (since Linux 2.6.35)"
1863 .\" commit 39447b386c846bbf1c56f6403c5282837486200f
1864 Sample happened in the guest kernel.
1866 .B PERF_RECORD_MISC_GUEST_USER " (since Linux 2.6.35)"
1867 .\" commit 39447b386c846bbf1c56f6403c5282837486200f
1868 Sample happened in guest user code.
1872 Since the following three statuses are generated by
1873 different record types, they alias to the same bit:
1875 .BR PERF_RECORD_MISC_MMAP_DATA " (since Linux 3.10)"
1876 .\" commit 2fe85427e3bf65d791700d065132772fc26e4d75
1877 This is set when the mapping is not executable;
1878 otherwise the mapping is executable.
1880 .BR PERF_RECORD_MISC_COMM_EXEC " (since Linux 3.16)"
1881 .\" commit 82b897782d10fcc4930c9d4a15b175348fdd2871
1884 record on kernels more recent than Linux 3.16
1885 if a process name change was caused by an
1889 .BR PERF_RECORD_MISC_SWITCH_OUT " (since Linux 4.3)"
1890 .\" commit 45ac1403f564f411c6a383a2448688ba8dd705a4
1892 .BR PERF_RECORD_SWITCH
1894 .BR PERF_RECORD_SWITCH_CPU_WIDE
1895 record is generated, this bit indicates that the
1896 context switch is away from the current process
1897 (instead of into the current process).
1901 In addition, the following bits can be set:
1903 .B PERF_RECORD_MISC_EXACT_IP
1904 This indicates that the content of
1907 to the actual instruction that triggered the event.
1909 .IR perf_event_attr.precise_ip .
1911 .BR PERF_RECORD_MISC_EXT_RESERVED " (since Linux 2.6.35)"
1912 .\" commit 1676b8a077c352085d52578fb4f29350b58b6e74
1913 This indicates there is extended data available (currently not used).
1915 .B PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT
1916 .\" commit 930e6fcd2bcce9bcd9d4aa7e755678d33f3fe6f4
1917 This bit is not set by the kernel.
1918 It is reserved for the user-space perf utility to indicate that
1919 .I /proc/i[pid]/maps
1920 parsing was taking too long and was stopped, and thus the mmap
1921 records may be truncated.
1927 value is one of the below.
1928 The values in the corresponding record (that follows the header)
1935 The MMAP events record the
1937 mappings so that we can correlate
1938 user-space IPs to code.
1939 They have the following structure:
1944 struct perf_event_header header;
1962 is the address of the allocated memory.
1964 is the length of the allocated memory.
1966 is the page offset of the allocated memory.
1968 is a string describing the backing of the allocated memory.
1972 This record indicates when events are lost.
1977 struct perf_event_header header;
1980 struct sample_id sample_id;
1987 is the unique event ID for the samples that were lost.
1990 is the number of events that were lost.
1994 This record indicates a change in the process name.
1999 struct perf_event_header header;
2003 struct sample_id sample_id;
2016 is a string containing the new name of the process.
2020 This record indicates a process exit event.
2025 struct perf_event_header header;
2029 struct sample_id sample_id;
2034 .BR PERF_RECORD_THROTTLE ", " PERF_RECORD_UNTHROTTLE
2035 This record indicates a throttle/unthrottle event.
2040 struct perf_event_header header;
2044 struct sample_id sample_id;
2050 This record indicates a fork event.
2055 struct perf_event_header header;
2059 struct sample_id sample_id;
2065 This record indicates a read event.
2070 struct perf_event_header header;
2072 struct read_format values;
2073 struct sample_id sample_id;
2078 .B PERF_RECORD_SAMPLE
2079 This record indicates a sample.
2084 struct perf_event_header header;
2085 u64 sample_id; /* if PERF_SAMPLE_IDENTIFIER */
2086 u64 ip; /* if PERF_SAMPLE_IP */
2087 u32 pid, tid; /* if PERF_SAMPLE_TID */
2088 u64 time; /* if PERF_SAMPLE_TIME */
2089 u64 addr; /* if PERF_SAMPLE_ADDR */
2090 u64 id; /* if PERF_SAMPLE_ID */
2091 u64 stream_id; /* if PERF_SAMPLE_STREAM_ID */
2092 u32 cpu, res; /* if PERF_SAMPLE_CPU */
2093 u64 period; /* if PERF_SAMPLE_PERIOD */
2094 struct read_format v;
2095 /* if PERF_SAMPLE_READ */
2096 u64 nr; /* if PERF_SAMPLE_CALLCHAIN */
2097 u64 ips[nr]; /* if PERF_SAMPLE_CALLCHAIN */
2098 u32 size; /* if PERF_SAMPLE_RAW */
2099 char data[size]; /* if PERF_SAMPLE_RAW */
2100 u64 bnr; /* if PERF_SAMPLE_BRANCH_STACK */
2101 struct perf_branch_entry lbr[bnr];
2102 /* if PERF_SAMPLE_BRANCH_STACK */
2103 u64 abi; /* if PERF_SAMPLE_REGS_USER */
2104 u64 regs[weight(mask)];
2105 /* if PERF_SAMPLE_REGS_USER */
2106 u64 size; /* if PERF_SAMPLE_STACK_USER */
2107 char data[size]; /* if PERF_SAMPLE_STACK_USER */
2108 u64 dyn_size; /* if PERF_SAMPLE_STACK_USER &&
2110 u64 weight; /* if PERF_SAMPLE_WEIGHT */
2111 u64 data_src; /* if PERF_SAMPLE_DATA_SRC */
2112 u64 transaction; /* if PERF_SAMPLE_TRANSACTION */
2113 u64 abi; /* if PERF_SAMPLE_REGS_INTR */
2114 u64 regs[weight(mask)];
2115 /* if PERF_SAMPLE_REGS_INTR */
2122 .B PERF_SAMPLE_IDENTIFIER
2123 is enabled, a 64-bit unique ID is included.
2124 This is a duplication of the
2127 value, but included at the beginning of the sample
2128 so parsers can easily obtain the value.
2133 is enabled, then a 64-bit instruction
2134 pointer value is included.
2139 is enabled, then a 32-bit process ID
2140 and 32-bit thread ID are included.
2145 is enabled, then a 64-bit timestamp
2147 This is obtained via local_clock() which is a hardware timestamp
2148 if available and the jiffies value if not.
2153 is enabled, then a 64-bit address is included.
2154 This is usually the address of a tracepoint,
2155 breakpoint, or software event; otherwise the value is 0.
2160 is enabled, a 64-bit unique ID is included.
2161 If the event is a member of an event group, the group leader ID is returned.
2162 This ID is the same as the one returned by
2163 .BR PERF_FORMAT_ID .
2167 .B PERF_SAMPLE_STREAM_ID
2168 is enabled, a 64-bit unique ID is included.
2171 the actual ID is returned, not the group leader.
2172 This ID is the same as the one returned by
2173 .BR PERF_FORMAT_ID .
2178 is enabled, this is a 32-bit value indicating
2179 which CPU was being used, in addition to a reserved (unused)
2184 .B PERF_SAMPLE_PERIOD
2185 is enabled, a 64-bit value indicating
2186 the current sampling period is written.
2191 is enabled, a structure of type read_format
2192 is included which has values for all events in the event group.
2193 The values included depend on the
2196 .BR perf_event_open ()
2201 .B PERF_SAMPLE_CALLCHAIN
2202 is enabled, then a 64-bit number is included
2203 which indicates how many following 64-bit instruction pointers will
2205 This is the current callchain.
2207 .IR size ", " data[size]
2210 is enabled, then a 32-bit value indicating size
2211 is included followed by an array of 8-bit values of length size.
2212 The values are padded with 0 to have 64-bit alignment.
2214 This RAW record data is opaque with respect to the ABI.
2215 The ABI doesn't make any promises with respect to the stability
2216 of its content, it may vary depending
2217 on event, hardware, and kernel version.
2219 .IR bnr ", " lbr[bnr]
2221 .B PERF_SAMPLE_BRANCH_STACK
2222 is enabled, then a 64-bit value indicating
2223 the number of records is included, followed by
2225 .I perf_branch_entry
2226 structures which each include the fields:
2230 This indicates the source instruction (may not be a branch).
2236 The branch target was mispredicted.
2239 The branch target was predicted.
2241 .IR in_tx " (since Linux 3.11)"
2242 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
2243 The branch was in a transactional memory transaction.
2245 .IR abort " (since Linux 3.11)"
2246 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
2247 The branch was in an aborted transactional memory transaction.
2249 .IR cycles " (since Linux 4.3)"
2250 .\" commit 71ef3c6b9d4665ee7afbbe4c208a98917dcfc32f
2251 This reports the number of cycles elapsed since the
2252 previous branch stack update.
2254 The entries are from most to least recent, so the first entry
2255 has the most recent branch.
2262 is optional; if not supported, those
2265 The type of branches recorded is specified by the
2266 .I branch_sample_type
2270 .IR abi ", " regs[weight(mask)]
2272 .B PERF_SAMPLE_REGS_USER
2273 is enabled, then the user CPU registers are recorded.
2278 .BR PERF_SAMPLE_REGS_ABI_NONE ", " PERF_SAMPLE_REGS_ABI_32 " or "
2279 .BR PERF_SAMPLE_REGS_ABI_64 .
2283 field is an array of the CPU registers that were specified by
2287 The number of values is the number of bits set in the
2291 .IR size ", " data[size] ", " dyn_size
2293 .B PERF_SAMPLE_STACK_USER
2294 is enabled, then the user stack is recorded.
2295 This can be used to generate stack backtraces.
2297 is the size requested by the user in
2298 .I sample_stack_user
2299 or else the maximum record size.
2301 is the stack data (a raw dump of the memory pointed to by the
2302 stack pointer at the time of sampling).
2304 is the amount of data actually dumped (can be less than
2314 .B PERF_SAMPLE_WEIGHT
2315 is enabled, then a 64-bit value provided by the hardware
2316 is recorded that indicates how costly the event was.
2317 This allows expensive events to stand out more clearly
2322 .B PERF_SAMPLE_DATA_SRC
2323 is enabled, then a 64-bit value is recorded that is made up of
2324 the following fields:
2328 Type of opcode, a bitwise combination of:
2339 .B PERF_MEM_OP_STORE
2342 .B PERF_MEM_OP_PFETCH
2351 Memory hierarchy level hit or miss, a bitwise combination of
2352 the following, shifted left by
2353 .BR PERF_MEM_LVL_SHIFT :
2364 .B PERF_MEM_LVL_MISS
2379 .B PERF_MEM_LVL_LOC_RAM
2382 .B PERF_MEM_LVL_REM_RAM1
2385 .B PERF_MEM_LVL_REM_RAM2
2388 .B PERF_MEM_LVL_REM_CCE1
2391 .B PERF_MEM_LVL_REM_CCE2
2403 Snoop mode, a bitwise combination of the following, shifted left by
2404 .BR PERF_MEM_SNOOP_SHIFT :
2409 .B PERF_MEM_SNOOP_NA
2412 .B PERF_MEM_SNOOP_NONE
2415 .B PERF_MEM_SNOOP_HIT
2418 .B PERF_MEM_SNOOP_MISS
2421 .B PERF_MEM_SNOOP_HITM
2427 Lock instruction, a bitwise combination of the following, shifted left by
2428 .BR PERF_MEM_LOCK_SHIFT :
2436 .B PERF_MEM_LOCK_LOCKED
2442 TLB access hit or miss, a bitwise combination of the following, shifted
2444 .BR PERF_MEM_TLB_SHIFT :
2455 .B PERF_MEM_TLB_MISS
2475 .B PERF_SAMPLE_TRANSACTION
2476 flag is set, then a 64-bit field is recorded describing
2477 the sources of any transactional memory aborts.
2479 The field is a bitwise combination of the following values:
2483 Abort from an elision type transaction (Intel-CPU-specific).
2485 .B PERF_TXN_TRANSACTION
2486 Abort from a generic transaction.
2489 Synchronous abort (related to the reported instruction).
2492 Asynchronous abort (not related to the reported instruction).
2495 Retryable abort (retrying the transaction may have succeeded).
2497 .B PERF_TXN_CONFLICT
2498 Abort due to memory conflicts with other threads.
2500 .B PERF_TXN_CAPACITY_WRITE
2501 Abort due to write capacity overflow.
2503 .B PERF_TXN_CAPACITY_READ
2504 Abort due to read capacity overflow.
2507 In addition, a user-specified abort code can be obtained from
2508 the high 32 bits of the field by shifting right by
2509 .B PERF_TXN_ABORT_SHIFT
2510 and masking with the value
2511 .BR PERF_TXN_ABORT_MASK .
2513 .IR abi ", " regs[weight(mask)]
2515 .B PERF_SAMPLE_REGS_INTR
2516 is enabled, then the user CPU registers are recorded.
2521 .BR PERF_SAMPLE_REGS_ABI_NONE ,
2522 .BR PERF_SAMPLE_REGS_ABI_32 ,
2524 .BR PERF_SAMPLE_REGS_ABI_64 .
2528 field is an array of the CPU registers that were specified by
2532 The number of values is the number of bits set in the
2537 .B PERF_RECORD_MMAP2
2538 This record includes extended information on
2540 calls returning executable mappings.
2541 The format is similar to that of the
2543 record, but includes extra values that allow uniquely identifying
2549 struct perf_event_header header;
2562 struct sample_id sample_id;
2574 is the address of the allocated memory.
2577 is the length of the allocated memory.
2580 is the page offset of the allocated memory.
2583 is the major ID of the underlying device.
2586 is the minor ID of the underlying device.
2589 is the inode number.
2592 is the inode generation.
2595 is the protection information.
2598 is the flags information.
2601 is a string describing the backing of the allocated memory.
2604 .BR PERF_RECORD_AUX " (since Linux 4.1)"
2605 \" commit 68db7e98c3a6ebe7284b6cf14906ed7c55f3f7f0
2606 This record reports that new data is available in the separate
2612 struct perf_event_header header;
2616 struct sample_id sample_id;
2622 offset in the AUX mmap region where the new data begins.
2625 size of the data made available.
2628 describes the AUX update.
2631 .B PERF_AUX_FLAG_TRUNCATED
2632 if set, then the data returned was truncated to fit the available
2635 .B PERF_AUX_FLAG_OVERWRITE
2636 .\" commit 2023a0d2829e521fe6ad6b9907f3f90bfbf57142
2637 if set, then the data returned has overwritten previous data.
2641 .BR PERF_RECORD_ITRACE_START " (since Linux 4.1)"
2642 \" ec0d7729bbaed4b9d2d3fada693278e13a3d1368
2643 This record indicates which process has initiated an instruction
2644 trace event, allowing tools to properly correlate the instruction
2645 addresses in the AUX buffer with the proper executable.
2650 struct perf_event_header header;
2658 process ID of the thread starting an instruction trace.
2661 thread ID of the thread starting an instruction trace.
2664 .BR PERF_RECORD_LOST_SAMPLES " (since Linux 4.2)"
2665 \" f38b0dbb491a6987e198aa6b428db8692a6480f8
2666 When using hardware sampling (such as Intel PEBS) this record
2667 indicates some number of samples that may have been lost.
2672 struct perf_event_header header;
2674 struct sample_id sample_id;
2680 the number of potentially lost samples.
2683 .BR PERF_RECORD_SWITCH " (since Linux 4.3)"
2684 \" commit 45ac1403f564f411c6a383a2448688ba8dd705a4
2685 This record indicates a context switch has happened.
2687 .B PERF_RECORD_MISC_SWITCH_OUT
2690 field indicates whether it was a context switch into
2691 or away from the current process.
2696 struct perf_event_header header;
2697 struct sample_id sample_id;
2701 .BR PERF_RECORD_SWITCH_CPU_WIDE " (since Linux 4.3)"
2702 \" commit 45ac1403f564f411c6a383a2448688ba8dd705a4
2704 .B PERF_RECORD_SWITCH
2705 this record indicates a context switch has happened,
2706 but it only occurs when sampling in CPU-wide mode
2707 and provides additional information on the process
2708 being switched to/from.
2710 .B PERF_RECORD_MISC_SWITCH_OUT
2713 field indicates whether it was a context switch into
2714 or away from the current process.
2719 struct perf_event_header header;
2722 struct sample_id sample_id;
2728 The process ID of the previous (if switching in)
2729 or next (if switching out) process on the CPU.
2732 The thread ID of the previous (if switching in)
2733 or next (if switching out) thread on the CPU.
2736 .SS Overflow handling
2737 Events can be set to notify when a threshold is crossed,
2738 indicating an overflow.
2739 Overflow conditions can be captured by monitoring the
2740 event file descriptor with
2745 Alternatively, the overflow events can be captured via sa signal handler,
2746 by enabling I/O signaling on the file descriptor; see the discussion of the
2753 Overflows are generated only by sampling events
2755 must have a nonzero value).
2757 There are two ways to generate overflow notifications.
2759 The first is to set a
2763 value that will trigger if a certain number of samples
2764 or bytes have been written to the mmap ring buffer.
2769 The other way is by use of the
2770 .B PERF_EVENT_IOC_REFRESH
2772 This ioctl adds to a counter that decrements each time the event overflows.
2776 once the counter reaches 0
2779 the underlying event is disabled.
2781 Refreshing an event group leader refreshes all siblings and
2782 refreshing with a parameter of 0 currently enables infinite
2784 these behaviors are unsupported and should not be relied on.
2785 .\" See https://lkml.org/lkml/2011/5/24/337
2787 Starting with Linux 3.18,
2788 .\" commit 179033b3e064d2cd3f5f9945e76b0a0f0fbf4883
2790 is indicated if the event being monitored is attached to a different
2791 process and that process exits.
2792 .SS rdpmc instruction
2793 Starting with Linux 3.4 on x86, you can use the
2794 .\" commit c7206205d00ab375839bd6c7ddb247d600693c09
2796 instruction to get low-latency reads without having to enter the kernel.
2799 is not necessarily faster than other methods for reading event values.
2801 Support for this can be detected with the
2803 field in the mmap page; documentation on how
2804 to calculate event values can be found in that section.
2806 Originally, when rdpmc support was enabled, any process (not just ones
2807 with an active perf event) could use the rdpmc instruction to access
2809 Starting with Linux 4.0,
2810 .\" 7911d3f7af14a614617e38245fedf98a724e46a9
2811 rdpmc support is only allowed if an event is currently enabled
2812 in a process's context.
2813 To restore the old behavior, write the value 2 to
2814 .IR /sys/devices/cpu/rdpmc .
2815 .SS perf_event ioctl calls
2817 Various ioctls act on
2818 .BR perf_event_open ()
2821 .B PERF_EVENT_IOC_ENABLE
2822 This enables the individual event or event group specified by the
2823 file descriptor argument.
2826 .B PERF_IOC_FLAG_GROUP
2827 bit is set in the ioctl argument, then all events in a group are
2828 enabled, even if the event specified is not the group leader
2831 .B PERF_EVENT_IOC_DISABLE
2832 This disables the individual counter or event group specified by the
2833 file descriptor argument.
2835 Enabling or disabling the leader of a group enables or disables the
2836 entire group; that is, while the group leader is disabled, none of the
2837 counters in the group will count.
2838 Enabling or disabling a member of a group other than the leader
2839 affects only that counter; disabling a non-leader
2840 stops that counter from counting but doesn't affect any other counter.
2843 .B PERF_IOC_FLAG_GROUP
2844 bit is set in the ioctl argument, then all events in a group are
2845 disabled, even if the event specified is not the group leader
2848 .B PERF_EVENT_IOC_REFRESH
2849 Non-inherited overflow counters can use this
2850 to enable a counter for a number of overflows specified by the argument,
2851 after which it is disabled.
2852 Subsequent calls of this ioctl add the argument value to the current
2854 An overflow notification with
2856 set will happen on each overflow until the
2857 count reaches 0; when that happens a notification with
2859 set is sent and the event is disabled.
2860 Using an argument of 0 is considered undefined behavior.
2862 .B PERF_EVENT_IOC_RESET
2863 Reset the event count specified by the
2864 file descriptor argument to zero.
2865 This resets only the counts; there is no way to reset the
2873 .B PERF_IOC_FLAG_GROUP
2874 bit is set in the ioctl argument, then all events in a group are
2875 reset, even if the event specified is not the group leader
2878 .B PERF_EVENT_IOC_PERIOD
2879 This updates the overflow period for the event.
2881 Since Linux 3.7 (on ARM)
2882 .\" commit 3581fe0ef37ce12ac7a4f74831168352ae848edc
2883 and Linux 3.14 (all other architectures),
2884 .\" commit bad7192b842c83e580747ca57104dd51fe08c223
2885 the new period takes effect immediately.
2886 On older kernels, the new period did not take effect until
2887 after the next overflow.
2889 The argument is a pointer to a 64-bit value containing the
2892 Prior to Linux 2.6.36,
2893 .\" commit ad0cf3478de8677f720ee06393b3147819568d6a
2894 this ioctl always failed due to a bug
2897 .B PERF_EVENT_IOC_SET_OUTPUT
2898 This tells the kernel to report event notifications to the specified
2899 file descriptor rather than the default one.
2900 The file descriptors must all be on the same CPU.
2902 The argument specifies the desired file descriptor, or \-1 if
2903 output should be ignored.
2905 .BR PERF_EVENT_IOC_SET_FILTER " (since Linux 2.6.33)"
2906 .\" commit 6fb2915df7f0747d9044da9dbff5b46dc2e20830
2907 This adds an ftrace filter to this event.
2909 The argument is a pointer to the desired ftrace filter.
2911 .BR PERF_EVENT_IOC_ID " (since Linux 3.12)"
2912 .\" commit cf4957f17f2a89984915ea808876d9c82225b862
2913 This returns the event ID value for the given event file descriptor.
2915 The argument is a pointer to a 64-bit unsigned integer
2918 .BR PERF_EVENT_IOC_SET_BPF " (since Linux 4.1)"
2919 .\" commit 2541517c32be2531e0da59dfd7efc1ce844644f5
2920 This allows attaching a Berkeley Packet Filter (BPF)
2921 program to an existing kprobe tracepoint event.
2924 privileges to use this ioctl.
2926 The argument is a BPF program file descriptor that was created by
2931 .BR PERF_EVENT_IOC_PAUSE_OUTPUT " (since Linux 4.7)"
2932 .\" commit 86e7972f690c1017fd086cdfe53d8524e68c661c
2933 This allows pausing and resuming the event's ring-buffer.
2934 A paused ring-buffer does not prevent generation of samples,
2935 but simply discards them.
2936 The discarded samples are considered lost, and cause a
2937 .BR PERF_RECORD_LOST
2938 sample to be generated when possible.
2939 An overflow signal may still be triggered by the discarded sample
2940 even though the ring-buffer remains empty.
2942 The argument is an unsigned 32-bit integer.
2943 A nonzero value pauses the ring-buffer, while a
2944 zero value resumes the ring-buffer.
2946 .BR PERF_EVENT_MODIFY_ATTRIBUTES " (since Linux 4.17)"
2947 .\" commit 32ff77e8cc9e66cc4fb38098f64fd54cc8f54573
2948 This allows modifying an existing event without the overhead
2949 of closing and reopening a new event.
2950 Currently this is supported only for breakpoint events.
2952 The argument is a pointer to a
2954 structure containing the updated event settings.
2956 .BR PERF_EVENT_IOC_QUERY_BPF " (since Linux 4.16)"
2957 .\" commit f371b304f12e31fe30207c41ca7754564e0ea4dc
2958 This allows querying which Berkeley Packet Filter (BPF)
2959 programs are attached to an existing kprobe tracepoint.
2960 You can only attach one BPF program per event, but you can
2961 have multiple events attached to a tracepoint.
2962 Querying this value on one tracepoint event returns the id
2963 of all BPF programs in all events attached to the tracepoint.
2966 privileges to use this ioctl.
2968 The argument is a pointer to a structure
2971 struct perf_event_query_bpf {
2980 field indicates the number of ids that can fit in the provided
2985 value is filled in by the kernel with the number of attached
2989 array is filled with the id of each attached BPF program.
2990 If there are more programs than will fit in the array, then the
2995 will indicate the number of program IDs that were successfully copied.
2998 A process can enable or disable all currently open event groups
3001 .B PR_TASK_PERF_EVENTS_ENABLE
3003 .B PR_TASK_PERF_EVENTS_DISABLE
3005 This applies only to events created locally by the calling process.
3006 This does not apply to events created by other processes attached
3007 to the calling process or inherited events from a parent process.
3008 Only group leaders are enabled and disabled,
3009 not any other members of the groups.
3010 .SS perf_event related configuration files
3013 .I /proc/sys/kernel/
3016 .I /proc/sys/kernel/perf_event_paranoid
3018 .I perf_event_paranoid
3019 file can be set to restrict access to the performance counters.
3024 allow only user-space measurements (default since Linux 4.6).
3025 .\" default changed in commit 0161028b7c8aebef64194d3d73e43bc3b53b5c66
3027 allow both kernel and user measurements (default before Linux 4.6).
3029 allow access to CPU-specific data but not raw tracepoint samples.
3035 The existence of the
3036 .I perf_event_paranoid
3037 file is the official method for determining if a kernel supports
3038 .BR perf_event_open ().
3040 .I /proc/sys/kernel/perf_event_max_sample_rate
3041 This sets the maximum sample rate.
3042 Setting this too high can allow
3043 users to sample at a rate that impacts overall machine performance
3044 and potentially lock up the machine.
3045 The default value is
3046 100000 (samples per second).
3048 .I /proc/sys/kernel/perf_event_max_stack
3049 .\" Introduced in c5dfd78eb79851e278b7973031b9ca363da87a7e
3050 This file sets the maximum depth of stack frame entries reported
3051 when generating a call trace.
3053 .I /proc/sys/kernel/perf_event_mlock_kb
3054 Maximum number of pages an unprivileged user can
3056 The default is 516 (kB).
3060 .I /sys/bus/event_source/devices/
3063 Since Linux 2.6.34, the kernel supports having multiple PMUs
3064 available for monitoring.
3065 Information on how to program these PMUs can be found under
3066 .IR /sys/bus/event_source/devices/ .
3067 Each subdirectory corresponds to a different PMU.
3069 .IR /sys/bus/event_source/devices/*/type " (since Linux 2.6.38)"
3070 .\" commit abe43400579d5de0078c2d3a760e6598e183f871
3071 This contains an integer that can be used in the
3075 to indicate that you wish to use this PMU.
3077 .IR /sys/bus/event_source/devices/cpu/rdpmc " (since Linux 3.4)"
3078 .\" commit 0c9d42ed4cee2aa1dfc3a260b741baae8615744f
3079 If this file is 1, then direct user-space access to the
3080 performance counter registers is allowed via the rdpmc instruction.
3081 This can be disabled by echoing 0 to the file.
3084 .\" a66734297f78707ce39d756b656bfae861d53f62
3085 .\" 7911d3f7af14a614617e38245fedf98a724e46a9
3086 the behavior has changed, so that 1 now means only allow access
3087 to processes with active perf events, with 2 indicating the old
3088 allow-anyone-access behavior.
3090 .IR /sys/bus/event_source/devices/*/format/ " (since Linux 3.4)"
3091 .\" commit 641cc938815dfd09f8fa1ec72deb814f0938ac33
3092 This subdirectory contains information on the architecture-specific
3093 subfields available for programming the various
3099 The content of each file is the name of the config field, followed
3100 by a colon, followed by a series of integer bit ranges separated by
3102 For example, the file
3104 may contain the value
3105 .I config1:1,6\-10,44
3106 which indicates that event is an attribute that occupies bits 1,6\(en10, and 44
3108 .IR perf_event_attr::config1 .
3110 .IR /sys/bus/event_source/devices/*/events/ " (since Linux 3.4)"
3111 .\" commit 641cc938815dfd09f8fa1ec72deb814f0938ac33
3112 This subdirectory contains files with predefined events.
3113 The contents are strings describing the event settings
3114 expressed in terms of the fields found in the previously mentioned
3117 These are not necessarily complete lists of all events supported by
3118 a PMU, but usually a subset of events deemed useful or interesting.
3120 The content of each file is a list of attribute names
3121 separated by commas.
3122 Each entry has an optional value (either hex or decimal).
3123 If no value is specified, then it is assumed to be a single-bit
3124 field with a value of 1.
3125 An example entry may look like this:
3126 .IR event=0x2,inv,ldlat=3 .
3128 .I /sys/bus/event_source/devices/*/uevent
3129 This file is the standard kernel device interface
3130 for injecting hotplug events.
3132 .IR /sys/bus/event_source/devices/*/cpumask " (since Linux 3.7)"
3133 .\" commit 314d9f63f385096580e9e2a06eaa0745d92fe4ac
3136 file contains a comma-separated list of integers that
3137 indicate a representative CPU number for each socket (package)
3139 This is needed when setting up uncore or northbridge events, as
3140 those PMUs present socket-wide events.
3143 .BR perf_event_open ()
3144 returns the new file descriptor, or \-1 if an error occurred
3147 is set appropriately).
3149 The errors returned by
3150 .BR perf_event_open ()
3151 can be inconsistent, and may
3152 vary across processor architectures and performance monitoring units.
3160 .BR PERF_ATTR_SIZE_VER0 ),
3161 too big (larger than the page size),
3162 or larger than the kernel supports and the extra bytes are not zero.
3168 field is overwritten by the kernel to be the size of the structure
3172 Returned when the requested event requires
3174 permissions (or a more permissive perf_event paranoid setting).
3175 Some common cases where an unprivileged process
3176 may encounter this error:
3177 attaching to a process owned by a different user;
3178 monitoring all processes on a given CPU (i.e., specifying the
3183 when the paranoid setting requires it.
3188 file descriptor is not valid, or, if
3189 .B PERF_FLAG_PID_CGROUP
3191 the cgroup file descriptor in
3195 .BR EBUSY " (since Linux 4.1)"
3196 .\" bed5b25ad9c8a2f5d735ef0bc746ec870c01c1b0
3197 Returned if another event already has exclusive
3203 pointer points at an invalid memory address.
3206 Returned if the specified event is invalid.
3207 There are many possible reasons for this.
3208 A not-exhaustive list:
3210 is higher than the maximum setting;
3213 to monitor does not exist;
3220 value is out of range;
3224 set and the event is not a group leader;
3227 values are out of range or set reserved bits;
3228 the generic event selected is not supported; or
3229 there is not enough room to add the selected event.
3232 Returned when trying to mix perf and ftrace handling
3236 Each opened event uses one file descriptor.
3237 If a large number of events are opened,
3238 the per-process limit on the number of open file descriptors will be reached,
3239 and no more events can be created.
3242 Returned when the event involves a feature not supported
3248 setting is not valid.
3249 This error is also returned for
3250 some unsupported generic events.
3253 Prior to Linux 3.3, if there was not enough room for the event,
3254 .\" commit aa2bc1ade59003a379ffc485d6da2d92ea3370a6
3257 In Linux 3.3, this was changed to
3260 is still returned if you try to add more breakpoint events
3261 than supported by the hardware.
3265 .B PERF_SAMPLE_STACK_USER
3268 and it is not supported by hardware.
3271 Returned if an event requiring a specific hardware feature is
3272 requested but there is no hardware support.
3273 This includes requesting low-skid events if not supported,
3274 branch tracing if it is not available, sampling if no PMU
3275 interrupt is available, and branch stacks for software events.
3277 .BR EOVERFLOW " (since Linux 4.8)"
3278 .\" 97c79a38cd454602645f0470ffb444b3b75ce574
3280 .B PERF_SAMPLE_CALLCHAIN
3283 is larger than the maximum specified in
3284 .IR /proc/sys/kernel/perf_event_max_stack .
3287 Returned on many (but not all) architectures when an unsupported
3288 .IR exclude_hv ", " exclude_idle ", " exclude_user ", or " exclude_kernel
3289 setting is specified.
3291 It can also happen, as with
3293 when the requested event requires
3295 permissions (or a more permissive perf_event paranoid setting).
3296 This includes setting a breakpoint on a kernel address,
3297 and (since Linux 3.13) setting a kernel function-trace tracepoint.
3298 .\" commit a4e95fc2cbb31d70a65beffeaf8773f881328c34
3301 Returned if attempting to attach to a process that does not exist.
3303 .BR perf_event_open ()
3304 was introduced in Linux 2.6.31 but was called
3305 .\" commit 0793a61d4df8daeac6492dbf8d2f3e5713caae5e
3306 .BR perf_counter_open ().
3307 It was renamed in Linux 2.6.32.
3308 .\" commit cdd6c482c9ff9c55475ee7392ec8f672eddb7be6
3311 .BR perf_event_open ()
3312 system call Linux-specific
3313 and should not be used in programs intended to be portable.
3315 Glibc does not provide a wrapper for this system call; call it using
3317 See the example below.
3319 The official way of knowing if
3320 .BR perf_event_open ()
3321 support is enabled is checking
3322 for the existence of the file
3323 .IR /proc/sys/kernel/perf_event_paranoid .
3329 is needed to properly get overflow signals in threads.
3330 This was introduced in Linux 2.6.32.
3331 .\" commit ba0a6c9f6fceed11c6a99e8326f0477fe383e6b5
3333 Prior to Linux 2.6.33 (at least for x86),
3334 .\" commit b690081d4d3f6a23541493f1682835c3cd5c54a1
3335 the kernel did not check
3336 if events could be scheduled together until read time.
3337 The same happens on all known kernels if the NMI watchdog is enabled.
3338 This means to see if a given set of events works you have to
3339 .BR perf_event_open (),
3340 start, then read before you know for sure you
3341 can get valid measurements.
3343 Prior to Linux 2.6.34,
3344 .\" FIXME . cannot find a kernel commit for this one
3345 event constraints were not enforced by the kernel.
3346 In that case, some events would silently return "0" if the kernel
3347 scheduled them in an improper counter slot.
3349 Prior to Linux 2.6.34, there was a bug when multiplexing where the
3350 wrong results could be returned.
3351 .\" commit 45e16a6834b6af098702e5ea6c9a40de42ff77d8
3353 Kernels from Linux 2.6.35 to Linux 2.6.39 can quickly crash the kernel if
3354 "inherit" is enabled and many threads are started.
3355 .\" commit 38b435b16c36b0d863efcf3f07b34a6fac9873fd
3357 Prior to Linux 2.6.35,
3358 .\" commit 050735b08ca8a016bbace4445fa025b88fee770b
3359 .B PERF_FORMAT_GROUP
3360 did not work with attached processes.
3362 There is a bug in the kernel code between
3363 Linux 2.6.36 and Linux 3.0 that ignores the
3364 "watermark" field and acts as if a wakeup_event
3365 was chosen if the union has a
3366 nonzero value in it.
3367 .\" commit 4ec8363dfc1451f8c8f86825731fe712798ada02
3369 From Linux 2.6.31 to Linux 3.4, the
3370 .B PERF_IOC_FLAG_GROUP
3371 ioctl argument was broken and would repeatedly operate
3372 on the event specified rather than iterating across
3373 all sibling events in a group.
3374 .\" commit 724b6daa13e100067c30cfc4d1ad06629609dc4e
3376 From Linux 3.4 to Linux 3.11, the mmap
3377 .\" commit fa7315871046b9a4c48627905691dbde57e51033
3381 bits mapped to the same location.
3382 Code should migrate to the new
3388 Always double-check your results!
3389 Various generalized events have had wrong values.
3390 For example, retired branches measured
3391 the wrong thing on AMD machines until Linux 2.6.35.
3392 .\" commit f287d332ce835f77a4f5077d2c0ef1e3f9ea42d2
3394 The following is a short example that measures the total
3395 instruction count of a call to
3403 #include <sys/ioctl.h>
3404 #include <linux/perf_event.h>
3405 #include <asm/unistd.h>
3408 perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
3409 int cpu, int group_fd, unsigned long flags)
3413 ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
3419 main(int argc, char **argv)
3421 struct perf_event_attr pe;
3425 memset(&pe, 0, sizeof(struct perf_event_attr));
3426 pe.type = PERF_TYPE_HARDWARE;
3427 pe.size = sizeof(struct perf_event_attr);
3428 pe.config = PERF_COUNT_HW_INSTRUCTIONS;
3430 pe.exclude_kernel = 1;
3433 fd = perf_event_open(&pe, 0, \-1, \-1, 0);
3435 fprintf(stderr, "Error opening leader %llx\en", pe.config);
3439 ioctl(fd, PERF_EVENT_IOC_RESET, 0);
3440 ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
3442 printf("Measuring instruction count for this printf\en");
3444 ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
3445 read(fd, &count, sizeof(long long));
3447 printf("Used %lld instructions\en", count);
3460 .IR Documentation/admin-guide/perf-security.rst
3461 in the kernel source tree