1 .\" Copyright (c) 2012, Vincent Weaver
3 .\" SPDX-License-Identifier: GPL-2.0-or-later
5 .\" This document is based on the perf_event.h header file, the
6 .\" tools/perf/design.txt file, and a lot of bitter experience.
8 .TH perf_event_open 2 (date) "Linux man-pages (unreleased)"
10 perf_event_open \- set up performance monitoring
13 .RI ( libc ", " \-lc )
16 .BR "#include <linux/perf_event.h>" " /* Definition of " PERF_* " constants */"
17 .BR "#include <linux/hw_breakpoint.h>" " /* Definition of " HW_* " constants */"
18 .BR "#include <sys/syscall.h>" " /* Definition of " SYS_* " constants */"
19 .B #include <unistd.h>
21 .BI "int syscall(SYS_perf_event_open, struct perf_event_attr *" attr ,
22 .BI " pid_t " pid ", int " cpu ", int " group_fd \
23 ", unsigned long " flags );
27 glibc provides no wrapper for
28 .BR perf_event_open (),
29 necessitating the use of
32 Given a list of parameters,
33 .BR perf_event_open ()
34 returns a file descriptor, for use in subsequent system calls
35 .RB ( read "(2), " mmap "(2), " prctl "(2), " fcntl "(2), etc.)."
38 .BR perf_event_open ()
39 creates a file descriptor that allows measuring performance
41 Each file descriptor corresponds to one
42 event that is measured; these can be grouped together
43 to measure multiple events simultaneously.
45 Events can be enabled and disabled in two ways: via
49 When an event is disabled it does not count or generate overflows but does
50 continue to exist and maintain its count value.
52 Events come in two flavors: counting and sampled.
55 event is one that is used for counting the aggregate number of events
57 In general, counting event results are gathered with a
62 event periodically writes measurements to a buffer that can then
70 arguments allow specifying which process and CPU to monitor:
72 .BR "pid == 0" " and " "cpu == \-1"
73 This measures the calling process/thread on any CPU.
75 .BR "pid == 0" " and " "cpu >= 0"
76 This measures the calling process/thread only
77 when running on the specified CPU.
79 .BR "pid > 0" " and " "cpu == \-1"
80 This measures the specified process/thread on any CPU.
82 .BR "pid > 0" " and " "cpu >= 0"
83 This measures the specified process/thread only
84 when running on the specified CPU.
86 .BR "pid == \-1" " and " "cpu >= 0"
87 This measures all processes/threads on the specified CPU.
93 .I /proc/sys/kernel/perf_event_paranoid
96 .BR "pid == \-1" " and " "cpu == \-1"
97 This setting is invalid and will return an error.
101 is greater than zero, permission to perform this system call
104 (since Linux 5.9) and a ptrace access mode
105 .B PTRACE_MODE_READ_REALCREDS
106 check on older Linux versions; see
111 argument allows event groups to be created.
112 An event group has one event which is the group leader.
113 The leader is created first, with
114 .IR group_fd " = \-1."
115 The rest of the group members are created with subsequent
116 .BR perf_event_open ()
119 being set to the file descriptor of the group leader.
120 (A single event on its own is created with
121 .IR group_fd " = \-1"
122 and is considered to be a group with only 1 member.)
123 An event group is scheduled onto the CPU as a unit:
124 it will be put onto the CPU
125 only if all of the events in the group can be put onto the CPU.
126 This means that the values of the member events can be meaningfully compared
127 \[em]added, divided (to get ratios), and so on\[em]
129 since they have counted events for the same set of executed instructions.
133 argument is formed by ORing together zero or more of the following values:
135 .BR PERF_FLAG_FD_CLOEXEC " (since Linux 3.14)"
136 .\" commit a21b0b354d4ac39be691f51c53562e2c24443d9e
137 This flag enables the close-on-exec flag for the created
138 event file descriptor,
139 so that the file descriptor is automatically closed on
141 Setting the close-on-exec flags at creation time, rather than later with
143 avoids potential race conditions where the calling thread invokes
144 .BR perf_event_open ()
147 at the same time as another thread calls
152 .B PERF_FLAG_FD_NO_GROUP
153 This flag tells the event to ignore the
155 parameter except for the purpose of setting up output redirection
157 .B PERF_FLAG_FD_OUTPUT
160 .BR PERF_FLAG_FD_OUTPUT " (broken since Linux 2.6.35)"
161 .\" commit ac9721f3f54b27a16c7e1afb2481e7ee95a70318
162 This flag re-routes the event's sampled output to instead
163 be included in the mmap buffer of the event specified by
166 .BR PERF_FLAG_PID_CGROUP " (since Linux 2.6.39)"
167 .\" commit e5d1367f17ba6a6fed5fd8b74e4d5720923e0c25
168 This flag activates per-container system-wide monitoring.
170 is an abstraction that isolates a set of resources for finer-grained
171 control (CPUs, memory, etc.).
172 In this mode, the event is measured
173 only if the thread running on the monitored CPU belongs to the designated
175 The cgroup is identified by passing a file descriptor
176 opened on its directory in the cgroupfs filesystem.
178 cgroup to monitor is called
180 then a file descriptor opened on
182 (assuming cgroupfs is mounted on
184 must be passed as the
187 cgroup monitoring is available only
188 for system-wide events and may therefore require extra permissions.
192 structure provides detailed configuration information
193 for the event being created.
197 struct perf_event_attr {
198 __u32 type; /* Type of event */
199 __u32 size; /* Size of attribute structure */
200 __u64 config; /* Type\-specific configuration */
203 __u64 sample_period; /* Period of sampling */
204 __u64 sample_freq; /* Frequency of sampling */
207 __u64 sample_type; /* Specifies values included in sample */
208 __u64 read_format; /* Specifies values returned in read */
210 __u64 disabled : 1, /* off by default */
211 inherit : 1, /* children inherit it */
212 pinned : 1, /* must always be on PMU */
213 exclusive : 1, /* only group on PMU */
214 exclude_user : 1, /* don\[aq]t count user */
215 exclude_kernel : 1, /* don\[aq]t count kernel */
216 exclude_hv : 1, /* don\[aq]t count hypervisor */
217 exclude_idle : 1, /* don\[aq]t count when idle */
218 mmap : 1, /* include mmap data */
219 comm : 1, /* include comm data */
220 freq : 1, /* use freq, not period */
221 inherit_stat : 1, /* per task counts */
222 enable_on_exec : 1, /* next exec enables */
223 task : 1, /* trace fork/exit */
224 watermark : 1, /* wakeup_watermark */
225 precise_ip : 2, /* skid constraint */
226 mmap_data : 1, /* non\-exec mmap data */
227 sample_id_all : 1, /* sample_type all events */
228 exclude_host : 1, /* don\[aq]t count in host */
229 exclude_guest : 1, /* don\[aq]t count in guest */
230 exclude_callchain_kernel : 1,
231 /* exclude kernel callchains */
232 exclude_callchain_user : 1,
233 /* exclude user callchains */
234 mmap2 : 1, /* include mmap with inode data */
235 comm_exec : 1, /* flag comm events that are
237 use_clockid : 1, /* use clockid for time fields */
238 context_switch : 1, /* context switch data */
239 write_backward : 1, /* Write ring buffer from end
241 namespaces : 1, /* include namespaces data */
242 ksymbol : 1, /* include ksymbol events */
243 bpf_event : 1, /* include bpf events */
244 aux_output : 1, /* generate AUX records
246 cgroup : 1, /* include cgroup events */
247 text_poke : 1, /* include text poke events */
248 build_id : 1, /* use build id in mmap2 events */
249 inherit_thread : 1, /* children only inherit */
250 /* if cloned with CLONE_THREAD */
251 remove_on_exec : 1, /* event is removed from task
253 sigtrap : 1, /* send synchronous SIGTRAP
259 __u32 wakeup_events; /* wakeup every n events */
260 __u32 wakeup_watermark; /* bytes before wakeup */
263 __u32 bp_type; /* breakpoint type */
266 __u64 bp_addr; /* breakpoint address */
267 __u64 kprobe_func; /* for perf_kprobe */
268 __u64 uprobe_path; /* for perf_uprobe */
269 __u64 config1; /* extension of config */
273 __u64 bp_len; /* breakpoint length */
274 __u64 kprobe_addr; /* with kprobe_func == NULL */
275 __u64 probe_offset; /* for perf_[k,u]probe */
276 __u64 config2; /* extension of config1 */
278 __u64 branch_sample_type; /* enum perf_branch_sample_type */
279 __u64 sample_regs_user; /* user regs to dump on samples */
280 __u32 sample_stack_user; /* size of stack to dump on
282 __s32 clockid; /* clock to use for time fields */
283 __u64 sample_regs_intr; /* regs to dump on samples */
284 __u32 aux_watermark; /* aux bytes before wakeup */
285 __u16 sample_max_stack; /* max frames in callchain */
286 __u16 __reserved_2; /* align to u64 */
287 __u32 aux_sample_size; /* max aux sample size */
288 __u32 __reserved_3; /* align to u64 */
289 __u64 sig_data; /* user data for sigtrap */
297 structure are described in more detail below:
300 This field specifies the overall event type.
301 It has one of the following values:
304 .B PERF_TYPE_HARDWARE
305 This indicates one of the "generalized" hardware events provided
309 field definition for more details.
311 .B PERF_TYPE_SOFTWARE
312 This indicates one of the software-defined events provided by the kernel
313 (even if no hardware support is available).
315 .B PERF_TYPE_TRACEPOINT
316 This indicates a tracepoint
317 provided by the kernel tracepoint infrastructure.
319 .B PERF_TYPE_HW_CACHE
320 This indicates a hardware cache event.
321 This has a special encoding, described in the
326 This indicates a "raw" implementation-specific event in the
329 .BR PERF_TYPE_BREAKPOINT " (since Linux 2.6.33)"
330 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
331 This indicates a hardware breakpoint as provided by the CPU.
332 Breakpoints can be read/write accesses to an address as well as
333 execution of an instruction address.
337 .\" commit 2e80a82a49c4c7eca4e35734380f28298ba5db19
338 .BR perf_event_open ()
339 can support multiple PMUs.
340 To enable this, a value exported by the kernel can be used in the
342 field to indicate which PMU to use.
343 The value to use can be found in the sysfs filesystem:
344 there is a subdirectory per PMU instance under
345 .IR /sys/bus/event_source/devices .
346 In each subdirectory there is a
348 file whose content is an integer that can be used in the
352 .I /sys/bus/event_source/devices/cpu/type
353 contains the value for the core CPU PMU, which is usually 4.
355 .BR kprobe " and " uprobe " (since Linux 4.17)"
356 .\" commit 65074d43fc77bcae32776724b7fa2696923c78e4
357 .\" commit e12f03d7031a977356e3d7b75a68c2185ff8d155
358 .\" commit 33ea4b24277b06dbc55d7f5772a46f029600255e
359 These two dynamic PMUs create a kprobe/uprobe and attach it to the
360 file descriptor generated by perf_event_open.
361 The kprobe/uprobe will be destroyed on the destruction of the file descriptor.
374 structure for forward/backward compatibility.
376 .I sizeof(struct perf_event_attr)
377 to allow the kernel to see
378 the struct size at the time of compilation.
381 .B PERF_ATTR_SIZE_VER0
382 is set to 64; this was the size of the first published struct.
383 .B PERF_ATTR_SIZE_VER1
384 is 72, corresponding to the addition of breakpoints in Linux 2.6.33.
385 .\" commit cb5d76999029ae7a517cb07dfa732c1b5a934fc2
386 .\" this was added much later when PERF_ATTR_SIZE_VER2 happened
387 .\" but the actual attr_size had increased in Linux 2.6.33
388 .B PERF_ATTR_SIZE_VER2
389 is 80 corresponding to the addition of branch sampling in Linux 3.4.
390 .\" commit cb5d76999029ae7a517cb07dfa732c1b5a934fc2
391 .B PERF_ATTR_SIZE_VER3
392 is 96 corresponding to the addition
398 .\" commit 1659d129ed014b715b0b2120e6fd929bdd33ed03
399 .B PERF_ATTR_SIZE_VER4
400 is 104 corresponding to the addition of
403 .\" commit 60e2364e60e86e81bc6377f49779779e6120977f
404 .B PERF_ATTR_SIZE_VER5
405 is 112 corresponding to the addition of
408 .\" commit 1a5941312414c71dece6717da9a0fa1303127afa
411 This specifies which event you want, in conjunction with
419 fields are also taken into account in cases where 64 bits is not
420 enough to fully specify the event.
421 The encoding of these fields are event dependent.
423 There are various ways to set the
425 field that are dependent on the value of the previously
429 What follows are various possible settings for
437 .BR PERF_TYPE_HARDWARE ,
438 we are measuring one of the generalized hardware CPU events.
439 Not all of these are available on all platforms.
442 to one of the following:
445 .B PERF_COUNT_HW_CPU_CYCLES
447 Be wary of what happens during CPU frequency scaling.
449 .B PERF_COUNT_HW_INSTRUCTIONS
450 Retired instructions.
451 Be careful, these can be affected by various
452 issues, most notably hardware interrupt counts.
454 .B PERF_COUNT_HW_CACHE_REFERENCES
456 Usually this indicates Last Level Cache accesses but this may
457 vary depending on your CPU.
458 This may include prefetches and coherency messages; again this
459 depends on the design of your CPU.
461 .B PERF_COUNT_HW_CACHE_MISSES
463 Usually this indicates Last Level Cache misses; this is intended to be
464 used in conjunction with the
465 .B PERF_COUNT_HW_CACHE_REFERENCES
466 event to calculate cache miss rates.
468 .B PERF_COUNT_HW_BRANCH_INSTRUCTIONS
469 Retired branch instructions.
470 Prior to Linux 2.6.35, this used
471 the wrong event on AMD processors.
472 .\" commit f287d332ce835f77a4f5077d2c0ef1e3f9ea42d2
474 .B PERF_COUNT_HW_BRANCH_MISSES
475 Mispredicted branch instructions.
477 .B PERF_COUNT_HW_BUS_CYCLES
478 Bus cycles, which can be different from total cycles.
480 .BR PERF_COUNT_HW_STALLED_CYCLES_FRONTEND " (since Linux 3.0)"
481 .\" commit 8f62242246351b5a4bc0c1f00c0c7003edea128a
482 Stalled cycles during issue.
484 .BR PERF_COUNT_HW_STALLED_CYCLES_BACKEND " (since Linux 3.0)"
485 .\" commit 8f62242246351b5a4bc0c1f00c0c7003edea128a
486 Stalled cycles during retirement.
488 .BR PERF_COUNT_HW_REF_CPU_CYCLES " (since Linux 3.3)"
489 .\" commit c37e17497e01fc0f5d2d6feb5723b210b3ab8890
490 Total cycles; not affected by CPU frequency scaling.
496 .BR PERF_TYPE_SOFTWARE ,
497 we are measuring software events provided by the kernel.
500 to one of the following:
503 .B PERF_COUNT_SW_CPU_CLOCK
504 This reports the CPU clock, a high-resolution per-CPU timer.
506 .B PERF_COUNT_SW_TASK_CLOCK
507 This reports a clock count specific to the task that is running.
509 .B PERF_COUNT_SW_PAGE_FAULTS
510 This reports the number of page faults.
512 .B PERF_COUNT_SW_CONTEXT_SWITCHES
513 This counts context switches.
514 Until Linux 2.6.34, these were all reported as user-space
515 events, after that they are reported as happening in the kernel.
516 .\" commit e49a5bd38159dfb1928fd25b173bc9de4bbadb21
518 .B PERF_COUNT_SW_CPU_MIGRATIONS
519 This reports the number of times the process
520 has migrated to a new CPU.
522 .B PERF_COUNT_SW_PAGE_FAULTS_MIN
523 This counts the number of minor page faults.
524 These did not require disk I/O to handle.
526 .B PERF_COUNT_SW_PAGE_FAULTS_MAJ
527 This counts the number of major page faults.
528 These required disk I/O to handle.
530 .BR PERF_COUNT_SW_ALIGNMENT_FAULTS " (since Linux 2.6.33)"
531 .\" commit f7d7986060b2890fc26db6ab5203efbd33aa2497
532 This counts the number of alignment faults.
533 These happen when unaligned memory accesses happen; the kernel
534 can handle these but it reduces performance.
535 This happens only on some architectures (never on x86).
537 .BR PERF_COUNT_SW_EMULATION_FAULTS " (since Linux 2.6.33)"
538 .\" commit f7d7986060b2890fc26db6ab5203efbd33aa2497
539 This counts the number of emulation faults.
540 The kernel sometimes traps on unimplemented instructions
541 and emulates them for user space.
542 This can negatively impact performance.
544 .BR PERF_COUNT_SW_DUMMY " (since Linux 3.12)"
545 .\" commit fa0097ee690693006ab1aea6c01ad3c851b65c77
546 This is a placeholder event that counts nothing.
547 Informational sample record types such as mmap or comm
548 must be associated with an active event.
549 This dummy event allows gathering such records without requiring
552 .BR PERF_COUNT_SW_BPF_OUTPUT " (since Linux 4.4)"
553 .\" commit a43eec304259a6c637f4014a6d4767159b6a3aa3
554 This is used to generate raw sample data from BPF.
555 BPF programs can write to this event using
556 .B bpf_perf_event_output
559 .BR PERF_COUNT_SW_CGROUP_SWITCHES " (since Linux 5.13)"
560 .\" commit d0d1dd628527c77db2391ce0293c1ed344b2365f
561 This counts context switches to a task in a different cgroup.
562 In other words, if the next task is in the same cgroup,
563 it won't count the switch.
570 .BR PERF_TYPE_TRACEPOINT ,
571 then we are measuring kernel tracepoints.
574 can be obtained from under debugfs
575 .I tracing/events/*/*/id
576 if ftrace is enabled in the kernel.
583 .BR PERF_TYPE_HW_CACHE ,
584 then we are measuring a hardware CPU cache event.
585 To calculate the appropriate
587 value, use the following equation:
592 config = (perf_hw_cache_id) |
593 (perf_hw_cache_op_id << 8) |
594 (perf_hw_cache_op_result_id << 16);
603 .B PERF_COUNT_HW_CACHE_L1D
604 for measuring Level 1 Data Cache
606 .B PERF_COUNT_HW_CACHE_L1I
607 for measuring Level 1 Instruction Cache
609 .B PERF_COUNT_HW_CACHE_LL
610 for measuring Last-Level Cache
612 .B PERF_COUNT_HW_CACHE_DTLB
613 for measuring the Data TLB
615 .B PERF_COUNT_HW_CACHE_ITLB
616 for measuring the Instruction TLB
618 .B PERF_COUNT_HW_CACHE_BPU
619 for measuring the branch prediction unit
621 .BR PERF_COUNT_HW_CACHE_NODE " (since Linux 3.1)"
622 .\" commit 89d6c0b5bdbb1927775584dcf532d98b3efe1477
623 for measuring local memory accesses
627 .I perf_hw_cache_op_id
631 .B PERF_COUNT_HW_CACHE_OP_READ
634 .B PERF_COUNT_HW_CACHE_OP_WRITE
637 .B PERF_COUNT_HW_CACHE_OP_PREFETCH
638 for prefetch accesses
642 .I perf_hw_cache_op_result_id
646 .B PERF_COUNT_HW_CACHE_RESULT_ACCESS
649 .B PERF_COUNT_HW_CACHE_RESULT_MISS
661 Most CPUs support events that are not covered by the "generalized" events.
662 These are implementation defined; see your CPU manual (for example
663 the Intel Volume 3B documentation or the AMD BIOS and Kernel Developer
665 The libpfm4 library can be used to translate from the name in the
666 architectural manuals to the raw hex value
667 .BR perf_event_open ()
668 expects in this field.
673 .BR PERF_TYPE_BREAKPOINT ,
677 Its parameters are set in other places.
690 .IR /sys/bus/event_source/devices/[k,u]probe/format/retprobe )
691 for kretprobe/uretprobe.
701 .IR kprobe_func ", " uprobe_path ", " kprobe_addr ", and " probe_offset
702 These fields describe the kprobe/uprobe for dynamic PMUs
724 .IR sample_period ", " sample_freq
725 A "sampling" event is one that generates an overflow notification
726 every N events, where N is given by
729 .IR sample_period " > 0."
730 When an overflow occurs, requested data is recorded
734 field controls what data is recorded on each overflow.
737 can be used if you wish to use frequency rather than period.
738 In this case, you set the
741 The kernel will adjust the sampling period
742 to try and achieve the desired rate.
743 The rate of adjustment is a
747 The various bits in this field specify which values to include
749 They will be recorded in a ring-buffer,
750 which is available to user space using
752 The order in which the values are saved in the
753 sample are documented in the MMAP Layout subsection below;
755 .I "enum perf_event_sample_format"
760 Records instruction pointer.
763 Records the process and thread IDs.
769 Records an address, if applicable.
772 Record counter values for all events in a group, not just the group leader.
774 .B PERF_SAMPLE_CALLCHAIN
775 Records the callchain (stack backtrace).
778 Records a unique ID for the opened event's group leader.
783 .B PERF_SAMPLE_PERIOD
784 Records the current sampling period.
786 .B PERF_SAMPLE_STREAM_ID
787 Records a unique ID for the opened event.
790 the actual ID is returned, not the group leader.
791 This ID is the same as the one returned by
795 Records additional data, if applicable.
796 Usually returned by tracepoint events.
798 .BR PERF_SAMPLE_BRANCH_STACK " (since Linux 3.4)"
799 .\" commit bce38cd53e5ddba9cb6d708c4ef3d04a4016ec7e
800 This provides a record of recent branches, as provided
801 by CPU branch sampling hardware (such as Intel Last Branch Record).
802 Not all hardware supports this feature.
805 .I branch_sample_type
806 field for how to filter which branches are reported.
808 .BR PERF_SAMPLE_REGS_USER " (since Linux 3.7)"
809 .\" commit 4018994f3d8785275ef0e7391b75c3462c029e56
810 Records the current user-level CPU register state
811 (the values in the process before the kernel was called).
813 .BR PERF_SAMPLE_STACK_USER " (since Linux 3.7)"
814 .\" commit c5ebcedb566ef17bda7b02686e0d658a7bb42ee7
815 Records the user level stack, allowing stack unwinding.
817 .BR PERF_SAMPLE_WEIGHT " (since Linux 3.10)"
818 .\" commit c3feedf2aaf9ac8bad6f19f5d21e4ee0b4b87e9c
819 Records a hardware provided weight value that expresses how
820 costly the sampled event was.
821 This allows the hardware to highlight expensive events in
824 .BR PERF_SAMPLE_DATA_SRC " (since Linux 3.10)"
825 .\" commit d6be9ad6c960f43800a6f118932bc8a5a4eadcd1
826 Records the data source: where in the memory hierarchy
827 the data associated with the sampled instruction came from.
828 This is available only if the underlying hardware
829 supports this feature.
831 .BR PERF_SAMPLE_IDENTIFIER " (since Linux 3.12)"
832 .\" commit ff3d527cebc1fa3707c617bfe9e74f53fcfb0955
835 value in a fixed position in the record,
836 either at the beginning (for sample events) or at the end
837 (if a non-sample event).
839 This was necessary because a sample stream may have
840 records from various different event sources with different
843 Parsing the event stream properly was not possible because the
844 format of the record was needed to find
847 the format could not be found without knowing what
848 event the sample belonged to (causing a circular
852 .B PERF_SAMPLE_IDENTIFIER
853 setting makes the event stream always parsable
856 in a fixed location, even though
857 it means having duplicate
861 .BR PERF_SAMPLE_TRANSACTION " (since Linux 3.13)"
862 .\" commit fdfbbd07e91f8fe387140776f3fd94605f0c89e5
863 Records reasons for transactional memory abort events
864 (for example, from Intel TSX transactional memory support).
868 setting must be greater than 0 and a transactional memory abort
869 event must be measured or no values will be recorded.
870 Also note that some perf_event measurements, such as sampled
871 cycle counting, may cause extraneous aborts (by causing an
872 interrupt during a transaction).
874 .BR PERF_SAMPLE_REGS_INTR " (since Linux 3.19)"
875 .\" commit 60e2364e60e86e81bc6377f49779779e6120977f
876 Records a subset of the current CPU register state
878 .IR sample_regs_intr .
880 .B PERF_SAMPLE_REGS_USER
881 the register values will return kernel register
882 state if the overflow happened while kernel
884 If the CPU supports hardware sampling of
885 register state (i.e., PEBS on Intel x86) and
887 is set higher than zero then the register
888 values returned are those captured by
889 hardware at the time of the sampled
890 instruction's retirement.
892 .BR PERF_SAMPLE_PHYS_ADDR " (since Linux 4.13)"
893 .\" commit fc7ce9c74c3ad232b084d80148654f926d01ece7
894 Records physical address of data like in
895 .BR PERF_SAMPLE_ADDR .
897 .BR PERF_SAMPLE_CGROUP " (since Linux 5.7)"
898 .\" commit 96aaab686505c449e24d76e76507290dcc30e008
899 Records (perf_event) cgroup ID of the process.
900 This corresponds to the
903 .B PERF_RECORD_CGROUP
906 .BR PERF_SAMPLE_DATA_PAGE_SIZE " (since Linux 5.11)"
907 .\" commit 8d97e71811aaafe4abf611dc24822fd6e73df1a1
908 Records page size of data like in
909 .BR PERF_SAMPLE_ADDR .
911 .BR PERF_SAMPLE_CODE_PAGE_SIZE " (since Linux 5.11)"
912 .\" commit 995f088efebe1eba0282a6ffa12411b37f8990c2
913 Records page size of ip like in
916 .BR PERF_SAMPLE_WEIGHT_STRUCT " (since Linux 5.12)"
917 .\" commit 2a6c6b7d7ad346f0679d0963cb19b3f0ea7ef32c
918 Records hardware provided weight values like in
919 .BR PERF_SAMPLE_WEIGHT ,
920 but it can represent multiple values in a struct.
921 This shares the same space as
922 .BR PERF_SAMPLE_WEIGHT ,
923 so users can apply either of those,
925 It has the following format and
926 the meaning of each field is
927 dependent on the hardware implementation.
931 union perf_sample_weight {
932 u64 full; /* PERF_SAMPLE_WEIGHT */
933 struct { /* PERF_SAMPLE_WEIGHT_STRUCT */
944 This field specifies the format of the data returned by
947 .BR perf_event_open ()
951 .B PERF_FORMAT_TOTAL_TIME_ENABLED
955 This can be used to calculate estimated totals if
956 the PMU is overcommitted and multiplexing is happening.
958 .B PERF_FORMAT_TOTAL_TIME_RUNNING
962 This can be used to calculate estimated totals if
963 the PMU is overcommitted and multiplexing is happening.
966 Adds a 64-bit unique value that corresponds to the event group.
969 Allows all counter values in an event group to be read with one read.
971 .B PERF_FORMAT_LOST " (since Linux 6.0)"
972 .\" commit 119a784c81270eb88e573174ed2209225d646656
973 Adds a 64-bit value that is the number of lost samples for this event.
974 This would be only meaningful when
984 bit specifies whether the counter starts out disabled or enabled.
985 If disabled, the event can later be enabled by
991 When creating an event group, typically the group leader is initialized
994 set to 1 and any child events are initialized with
999 being 0, the child events will not start until the group leader
1005 bit specifies that this counter should count events of child
1006 tasks as well as the task specified.
1007 This applies only to new children, not to any existing children at
1008 the time the counter is created (nor to any new children of
1011 Inherit does not work for some combinations of
1014 .BR PERF_FORMAT_GROUP .
1019 bit specifies that the counter should always be on the CPU if at all
1021 It applies only to hardware counters and only to group leaders.
1022 If a pinned counter cannot be put onto the CPU (e.g., because there are
1023 not enough hardware counters or because of a conflict with some other
1024 event), then the counter goes into an 'error' state, where reads
1025 return end-of-file (i.e.,
1027 returns 0) until the counter is subsequently enabled or disabled.
1032 bit specifies that when this counter's group is on the CPU,
1033 it should be the only group using the CPU's counters.
1034 In the future this may allow monitoring programs to
1035 support PMU features that need to run alone so that they do not
1036 disrupt other hardware counters.
1038 Note that many unexpected situations may prevent events with the
1040 bit set from ever running.
1041 This includes any users running a system-wide
1042 measurement as well as any kernel use of the performance counters
1043 (including the commonly enabled NMI Watchdog Timer interface).
1046 If this bit is set, the count excludes events that happen in user space.
1049 If this bit is set, the count excludes events that happen in kernel space.
1052 If this bit is set, the count excludes events that happen in the
1054 This is mainly for PMUs that have built-in support for handling this
1056 Extra support is needed for handling hypervisor measurements on most
1060 If set, don't count when the CPU is running the idle task.
1061 While you can currently enable this for any event type, it is ignored
1062 for all but software events.
1067 bit enables generation of
1074 This allows tools to notice new executable code being mapped into
1075 a program (dynamic shared libraries for example)
1076 so that addresses can be mapped back to the original code.
1081 bit enables tracking of process command name as modified by the
1084 .BR prctl (PR_SET_NAME)
1085 system calls as well as writing to
1086 .IR /proc/self/comm .
1089 flag is also successfully set (possible since Linux 3.16),
1090 .\" commit 82b897782d10fcc4930c9d4a15b175348fdd2871
1092 .B PERF_RECORD_MISC_COMM_EXEC
1093 can be used to differentiate the
1095 case from the others.
1098 If this bit is set, then
1102 is used when setting up the sampling interval.
1105 This bit enables saving of event counts on context switch for
1107 This is meaningful only if the
1112 If this bit is set, a counter is automatically
1113 enabled after a call to
1117 If this bit is set, then
1118 fork/exit notifications are included in the ring buffer.
1121 If set, have an overflow notification happen when we cross the
1124 Otherwise, overflow notifications happen after
1128 .IR precise_ip " (since Linux 2.6.35)"
1129 .\" commit ab608344bcbde4f55ec4cd911b686b0ce3eae076
1130 This controls the amount of skid.
1131 Skid is how many instructions
1132 execute between an event of interest happening and the kernel
1133 being able to stop and record the event.
1135 better and allows more accurate reporting of which events
1136 correspond to which instructions, but hardware is often limited
1137 with how small this can be.
1139 The possible values of this field are the following:
1144 can have arbitrary skid.
1148 must have constant skid.
1152 requested to have 0 skid.
1157 See also the description of
1158 .BR PERF_RECORD_MISC_EXACT_IP .
1161 .IR mmap_data " (since Linux 2.6.36)"
1162 .\" commit 3af9e859281bda7eb7c20b51879cf43aa788ac2e
1163 This is the counterpart of the
1166 This enables generation of
1170 calls that do not have
1172 set (for example data and SysV shared memory).
1174 .IR sample_id_all " (since Linux 2.6.38)"
1175 .\" commit c980d1091810df13f21aabbce545fd98f545bbf7
1176 If set, then TID, TIME, ID, STREAM_ID, and CPU can
1177 additionally be included in
1178 .RB non- PERF_RECORD_SAMPLE s
1179 if the corresponding
1184 .B PERF_SAMPLE_IDENTIFIER
1185 is specified, then an additional ID value is included
1186 as the last value to ease parsing the record stream.
1187 This may lead to the
1189 value appearing twice.
1191 The layout is described by this pseudo-structure:
1196 { u32 pid, tid; } /* if PERF_SAMPLE_TID set */
1197 { u64 time; } /* if PERF_SAMPLE_TIME set */
1198 { u64 id; } /* if PERF_SAMPLE_ID set */
1199 { u64 stream_id;} /* if PERF_SAMPLE_STREAM_ID set */
1200 { u32 cpu, res; } /* if PERF_SAMPLE_CPU set */
1201 { u64 id; } /* if PERF_SAMPLE_IDENTIFIER set */
1206 .IR exclude_host " (since Linux 3.2)"
1207 .\" commit a240f76165e6255384d4bdb8139895fac7988799
1208 When conducting measurements that include processes running
1209 VM instances (i.e., have executed a
1212 only measure events happening inside a guest instance.
1213 This is only meaningful outside the guests; this setting does
1214 not change counts gathered inside of a guest.
1215 Currently, this functionality is x86 only.
1217 .IR exclude_guest " (since Linux 3.2)"
1218 .\" commit a240f76165e6255384d4bdb8139895fac7988799
1219 When conducting measurements that include processes running
1220 VM instances (i.e., have executed a
1223 do not measure events happening inside guest instances.
1224 This is only meaningful outside the guests; this setting does
1225 not change counts gathered inside of a guest.
1226 Currently, this functionality is x86 only.
1228 .IR exclude_callchain_kernel " (since Linux 3.7)"
1229 .\" commit d077526485d5c9b12fe85d0b2b3b7041e6bc5f91
1230 Do not include kernel callchains.
1232 .IR exclude_callchain_user " (since Linux 3.7)"
1233 .\" commit d077526485d5c9b12fe85d0b2b3b7041e6bc5f91
1234 Do not include user callchains.
1236 .IR mmap2 " (since Linux 3.16)"
1237 .\" commit 13d7a2410fa637f450a29ecb515ac318ee40c741
1238 .\" This is tricky; was committed during 3.12 development
1239 .\" but right before release was disabled.
1240 .\" So while you could select mmap2 starting with Linux 3.12
1241 .\" it did not work until Linux 3.16
1242 .\" commit a5a5ba72843dd05f991184d6cb9a4471acce1005
1243 Generate an extended executable mmap record that contains enough
1244 additional information to uniquely identify shared mappings.
1247 flag must also be set for this to work.
1249 .IR comm_exec " (since Linux 3.16)"
1250 .\" commit 82b897782d10fcc4930c9d4a15b175348fdd2871
1251 This is purely a feature-detection flag, it does not change
1253 If this flag can successfully be set, then, when
1256 .B PERF_RECORD_MISC_COMM_EXEC
1257 flag will be set in the
1259 field of a comm record header if the rename event being
1260 reported was caused by a call to
1262 This allows tools to distinguish between the various
1263 types of process renaming.
1265 .IR use_clockid " (since Linux 4.1)"
1266 .\" commit 34f439278cef7b1177f8ce24f9fc81dfc6221d3b
1267 This allows selecting which internal Linux clock to use
1268 when generating timestamps via the
1271 This can make it easier to correlate perf sample times with
1272 timestamps generated by other tools.
1274 .IR context_switch " (since Linux 4.3)"
1275 .\" commit 45ac1403f564f411c6a383a2448688ba8dd705a4
1276 This enables the generation of
1277 .B PERF_RECORD_SWITCH
1278 records when a context switch occurs.
1279 It also enables the generation of
1280 .B PERF_RECORD_SWITCH_CPU_WIDE
1281 records when sampling in CPU-wide mode.
1282 This functionality is in addition to existing tracepoint and
1283 software events for measuring context switches.
1284 The advantage of this method is that it will give full
1285 information even with strict
1286 .I perf_event_paranoid
1289 .IR write_backward " (since Linux 4.6)"
1290 .\" commit 9ecda41acb971ebd07c8fb35faf24005c0baea12
1291 This causes the ring buffer to be written from the end to the beginning.
1292 This is to support reading from overwritable ring buffer.
1294 .IR namespaces " (since Linux 4.11)"
1295 .\" commit e422267322cd319e2695a535e47c5b1feeac45eb
1296 This enables the generation of
1297 .B PERF_RECORD_NAMESPACES
1298 records when a task enters a new namespace.
1299 Each namespace has a combination of device and inode numbers.
1301 .IR ksymbol " (since Linux 5.0)"
1302 .\" commit 76193a94522f1d4edf2447a536f3f796ce56343b
1303 This enables the generation of
1304 .B PERF_RECORD_KSYMBOL
1305 records when new kernel symbols are registered or unregistered.
1306 This is analyzing dynamic kernel functions like eBPF.
1308 .IR bpf_event " (since Linux 5.0)"
1309 .\" commit 6ee52e2a3fe4ea35520720736e6791df1fb67106
1310 This enables the generation of
1311 .B PERF_RECORD_BPF_EVENT
1312 records when an eBPF program is loaded or unloaded.
1314 .IR aux_output " (since Linux 5.4)"
1315 .\" commit ab43762ef010967e4ccd53627f70a2eecbeafefb
1316 This allows normal (non-AUX) events to generate data for AUX events
1317 if the hardware supports it.
1319 .IR cgroup " (since Linux 5.7)"
1320 .\" commit 96aaab686505c449e24d76e76507290dcc30e008
1321 This enables the generation of
1322 .B PERF_RECORD_CGROUP
1323 records when a new cgroup is created (and activated).
1325 .IR text_poke " (since Linux 5.8)"
1326 .\" commit e17d43b93e544f5016c0251d2074c15568d5d963
1327 This enables the generation of
1328 .B PERF_RECORD_TEXT_POKE
1329 records when there's a change to the kernel text
1330 (i.e., self-modifying code).
1332 .IR build_id " (since Linux 5.12)"
1333 .\" commit 88a16a1309333e43d328621ece3e9fa37027e8eb
1334 This changes the contents in the
1335 .B PERF_RECORD_MMAP2
1336 to have a build-id instead of device and inode numbers.
1338 .IR inherit_thread " (since Linux 5.13)"
1339 .\" commit 2b26f0aa004995f49f7b6f4100dd0e4c39a9ed5f
1340 This disables the inheritance of the event to a child process.
1341 Only new threads in the same process
1342 (which is cloned with
1344 will inherit the event.
1346 .IR remove_on_exec " (since Linux 5.13)"
1347 .\" commit 2e498d0a74e5b88a6689ae1b811f247f91ff188e
1348 This closes the event when it starts a new process image by
1351 .IR sigtrap " (since Linux 5.13)"
1352 .\" commit 97ba62b278674293762c3d91f724f1bb922f04e0
1353 This enables synchronous signal delivery of
1357 .IR wakeup_events ", " wakeup_watermark
1358 This union sets how many samples
1359 .RI ( wakeup_events )
1361 .RI ( wakeup_watermark )
1362 happen before an overflow notification happens.
1363 Which one is used is selected by the
1369 .B PERF_RECORD_SAMPLE
1371 To receive overflow notification for all
1373 types choose watermark and set
1377 Prior to Linux 3.0, setting
1378 .\" commit f506b3dc0ec454a16d40cab9ee5d75435b39dc50
1380 to 0 resulted in no overflow notifications;
1381 more recent kernels treat 0 the same as 1.
1383 .IR bp_type " (since Linux 2.6.33)"
1384 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
1385 This chooses the breakpoint type.
1389 .B HW_BREAKPOINT_EMPTY
1393 Count when we read the memory location.
1396 Count when we write the memory location.
1399 Count when we read or write the memory location.
1402 Count when we execute code at the memory location.
1404 The values can be combined via a bitwise or, but the
1414 .IR bp_addr " (since Linux 2.6.33)"
1415 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
1416 This is the address of the breakpoint.
1417 For execution breakpoints, this is the memory address of the instruction
1418 of interest; for read and write breakpoints, it is the memory address
1419 of the memory location of interest.
1421 .IR config1 " (since Linux 2.6.39)"
1422 .\" commit a7e3ed1e470116c9d12c2f778431a481a6be8ab6
1424 is used for setting events that need an extra register or otherwise
1425 do not fit in the regular config field.
1426 Raw OFFCORE_EVENTS on Nehalem/Westmere/SandyBridge use this field
1427 on Linux 3.3 and later kernels.
1429 .IR bp_len " (since Linux 2.6.33)"
1430 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
1432 is the length of the breakpoint being measured if
1435 .BR PERF_TYPE_BREAKPOINT .
1437 .BR HW_BREAKPOINT_LEN_1 ,
1438 .BR HW_BREAKPOINT_LEN_2 ,
1439 .BR HW_BREAKPOINT_LEN_4 ,
1441 .BR HW_BREAKPOINT_LEN_8 .
1442 For an execution breakpoint, set this to
1445 .IR config2 " (since Linux 2.6.39)"
1446 .\" commit a7e3ed1e470116c9d12c2f778431a481a6be8ab6
1448 is a further extension of the
1452 .IR branch_sample_type " (since Linux 3.4)"
1453 .\" commit bce38cd53e5ddba9cb6d708c4ef3d04a4016ec7e
1455 .B PERF_SAMPLE_BRANCH_STACK
1456 is enabled, then this specifies what branches to include
1457 in the branch record.
1459 The first part of the value is the privilege level, which
1460 is a combination of one of the values listed below.
1461 If the user does not set privilege level explicitly, the kernel
1462 will use the event's privilege level.
1463 Event and branch privilege levels do not have to match.
1466 .B PERF_SAMPLE_BRANCH_USER
1467 Branch target is in user space.
1469 .B PERF_SAMPLE_BRANCH_KERNEL
1470 Branch target is in kernel space.
1472 .B PERF_SAMPLE_BRANCH_HV
1473 Branch target is in hypervisor.
1475 .B PERF_SAMPLE_BRANCH_PLM_ALL
1476 A convenience value that is the three preceding values ORed together.
1478 In addition to the privilege value, at least one or more of the
1479 following bits must be set.
1481 .B PERF_SAMPLE_BRANCH_ANY
1484 .B PERF_SAMPLE_BRANCH_ANY_CALL
1485 Any call branch (includes direct calls, indirect calls, and far jumps).
1487 .B PERF_SAMPLE_BRANCH_IND_CALL
1490 .BR PERF_SAMPLE_BRANCH_CALL " (since Linux 4.4)"
1491 .\" commit c229bf9dc179d2023e185c0f705bdf68484c1e73
1494 .B PERF_SAMPLE_BRANCH_ANY_RETURN
1497 .BR PERF_SAMPLE_BRANCH_IND_JUMP " (since Linux 4.2)"
1498 .\" commit c9fdfa14c3792c0160849c484e83aa57afd80ccc
1501 .BR PERF_SAMPLE_BRANCH_COND " (since Linux 3.16)"
1502 .\" commit bac52139f0b7ab31330e98fd87fc5a2664951050
1503 Conditional branches.
1505 .BR PERF_SAMPLE_BRANCH_ABORT_TX " (since Linux 3.11)"
1506 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
1507 Transactional memory aborts.
1509 .BR PERF_SAMPLE_BRANCH_IN_TX " (since Linux 3.11)"
1510 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
1511 Branch in transactional memory transaction.
1513 .BR PERF_SAMPLE_BRANCH_NO_TX " (since Linux 3.11)"
1514 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
1515 Branch not in transactional memory transaction.
1516 .BR PERF_SAMPLE_BRANCH_CALL_STACK " (since Linux 4.1)"
1517 .\" commit 2c44b1936bb3b135a3fac8b3493394d42e51cf70
1518 Branch is part of a hardware-generated call stack.
1519 This requires hardware support, currently only found
1520 on Intel x86 Haswell or newer.
1523 .IR sample_regs_user " (since Linux 3.7)"
1524 .\" commit 4018994f3d8785275ef0e7391b75c3462c029e56
1525 This bit mask defines the set of user CPU registers to dump on samples.
1526 The layout of the register mask is architecture-specific and
1527 is described in the kernel header file
1528 .IR arch/ARCH/include/uapi/asm/perf_regs.h .
1530 .IR sample_stack_user " (since Linux 3.7)"
1531 .\" commit c5ebcedb566ef17bda7b02686e0d658a7bb42ee7
1532 This defines the size of the user stack to dump if
1533 .B PERF_SAMPLE_STACK_USER
1536 .IR clockid " (since Linux 4.1)"
1537 .\" commit 34f439278cef7b1177f8ce24f9fc81dfc6221d3b
1540 is set, then this field selects which internal Linux timer to
1542 The available timers are defined in
1545 .BR CLOCK_MONOTONIC ,
1546 .BR CLOCK_MONOTONIC_RAW ,
1547 .BR CLOCK_REALTIME ,
1548 .BR CLOCK_BOOTTIME ,
1551 currently supported.
1553 .IR aux_watermark " (since Linux 4.1)"
1554 .\" commit 1a5941312414c71dece6717da9a0fa1303127afa
1555 This specifies how much data is required to trigger a
1559 .IR sample_max_stack " (since Linux 4.8)"
1560 .\" commit 97c79a38cd454602645f0470ffb444b3b75ce574
1564 .BR PERF_SAMPLE_CALLCHAIN ,
1565 this field specifies how many stack frames to report when
1566 generating the callchain.
1568 .IR aux_sample_size " (since Linux 5.5)"
1569 .\" commit a4faf00d994c40e64f656805ac375c65e324eefb
1573 specify the desired size of AUX data.
1574 Note that it can get smaller data than the specified size.
1576 .IR sig_data " (since Linux 5.13)"
1577 .\" commit 97ba62b278674293762c3d91f724f1bb922f04e0
1578 This data will be copied to user's signal handler
1583 to disambiguate which event triggered the signal.
1586 .BR perf_event_open ()
1587 file descriptor has been opened, the values
1588 of the events can be read from the file descriptor.
1589 The values that are there are specified by the
1593 structure at open time.
1595 If you attempt to read into a buffer that is not big enough to hold the
1600 Here is the layout of the data returned by a read:
1603 .B PERF_FORMAT_GROUP
1604 was specified to allow reading all events in a group at once:
1608 struct read_format {
1609 u64 nr; /* The number of events */
1610 u64 time_enabled; /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
1611 u64 time_running; /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
1613 u64 value; /* The value of the event */
1614 u64 id; /* if PERF_FORMAT_ID */
1615 u64 lost; /* if PERF_FORMAT_LOST */
1622 .B PERF_FORMAT_GROUP
1629 struct read_format {
1630 u64 value; /* The value of the event */
1631 u64 time_enabled; /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
1632 u64 time_running; /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
1633 u64 id; /* if PERF_FORMAT_ID */
1634 u64 lost; /* if PERF_FORMAT_LOST */
1639 The values read are as follows:
1642 The number of events in this file descriptor.
1644 .B PERF_FORMAT_GROUP
1647 .IR time_enabled ", " time_running
1648 Total time the event was enabled and running.
1649 Normally these values are the same.
1650 Multiplexing happens if the number of events is more than the
1651 number of available PMU counter slots.
1652 In that case the events run only part of the time and the
1656 values can be used to scale an estimated value for the count.
1659 An unsigned 64-bit value containing the counter result.
1662 A globally unique value for this particular event; only present if
1668 The number of lost samples of this event;
1675 .BR perf_event_open ()
1676 in sampled mode, asynchronous events
1677 (like counter overflow or
1680 are logged into a ring-buffer.
1681 This ring-buffer is created and accessed through
1684 The mmap size should be 1+2\[ha]n pages, where the first page is a
1686 .RI ( "struct perf_event_mmap_page" )
1687 that contains various
1688 bits of information such as where the ring-buffer head is.
1690 Before Linux 2.6.39, there is a bug that means you must allocate an mmap
1691 ring buffer when sampling even if you do not plan to access it.
1693 The structure of the first metadata mmap page is as follows:
1697 struct perf_event_mmap_page {
1698 __u32 version; /* version number of this structure */
1699 __u32 compat_version; /* lowest version this is compat with */
1700 __u32 lock; /* seqlock for synchronization */
1701 __u32 index; /* hardware counter identifier */
1702 __s64 offset; /* add to hardware counter value */
1703 __u64 time_enabled; /* time event active */
1704 __u64 time_running; /* time event on CPU */
1708 __u64 cap_usr_time / cap_usr_rdpmc / cap_bit0 : 1,
1709 cap_bit0_is_deprecated : 1,
1712 cap_user_time_zero : 1,
1719 __u64 __reserved[120]; /* Pad to 1 k */
1720 __u64 data_head; /* head in the data section */
1721 __u64 data_tail; /* user\-space written tail */
1722 __u64 data_offset; /* where the buffer starts */
1723 __u64 data_size; /* data buffer size */
1733 The following list describes the fields in the
1734 .I perf_event_mmap_page
1735 structure in more detail:
1738 Version number of this structure.
1741 The lowest version this is compatible with.
1744 A seqlock for synchronization.
1747 A unique hardware counter identifier.
1750 When using rdpmc for reads this offset value
1751 must be added to the one returned by rdpmc to get
1752 the current total event count.
1755 Time the event was active.
1758 Time the event was running.
1760 .IR cap_usr_time " / " cap_usr_rdpmc " / " cap_bit0 " (since Linux 3.4)"
1761 .\" commit c7206205d00ab375839bd6c7ddb247d600693c09
1762 There was a bug in the definition of
1766 from Linux 3.4 until Linux 3.11.
1767 Both bits were defined to point to the same location, so it was
1768 impossible to know if
1774 Starting with Linux 3.12, these are renamed to
1775 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1777 and you should use the
1783 .IR cap_bit0_is_deprecated " (since Linux 3.12)"
1784 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1785 If set, this bit indicates that the kernel supports
1786 the properly separated
1792 If not-set, it indicates an older kernel where
1796 map to the same bit and thus both features should
1797 be used with caution.
1799 .IR cap_user_rdpmc " (since Linux 3.12)"
1800 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1801 If the hardware supports user-space read of performance counters
1802 without syscall (this is the "rdpmc" instruction on x86), then
1803 the following code can be used to do a read:
1807 u32 seq, time_mult, time_shift, idx, width;
1808 u64 count, enabled, running;
1809 u64 cyc, time_offset;
1814 enabled = pc\->time_enabled;
1815 running = pc\->time_running;
1817 if (pc\->cap_usr_time && enabled != running) {
1819 time_offset = pc\->time_offset;
1820 time_mult = pc\->time_mult;
1821 time_shift = pc\->time_shift;
1825 count = pc\->offset;
1827 if (pc\->cap_usr_rdpmc && idx) {
1828 width = pc\->pmc_width;
1829 count += rdpmc(idx \- 1);
1833 } while (pc\->lock != seq);
1837 .IR cap_user_time " (since Linux 3.12)"
1838 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1839 This bit indicates the hardware has a constant, nonstop
1840 timestamp counter (TSC on x86).
1842 .IR cap_user_time_zero " (since Linux 3.12)"
1843 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1844 Indicates the presence of
1846 which allows mapping timestamp values to
1852 this field provides the bit-width of the value
1853 read using the rdpmc or equivalent instruction.
1854 This can be used to sign extend the result like:
1858 pmc <<= 64 \- pmc_width;
1859 pmc >>= 64 \- pmc_width; // signed shift right
1864 .IR time_shift ", " time_mult ", " time_offset
1868 these fields can be used to compute the time
1871 (in nanoseconds) using rdtsc or similar.
1878 quot = cyc >> time_shift;
1879 rem = cyc & (((u64)1 << time_shift) \- 1);
1880 delta = time_offset + quot * time_mult +
1881 ((rem * time_mult) >> time_shift);
1892 seqcount loop described above.
1893 This delta can then be added to
1894 enabled and possible running (if idx), improving the scaling:
1901 quot = count / running;
1902 rem = count % running;
1903 count = quot * enabled + (rem * enabled) / running;
1907 .IR time_zero " (since Linux 3.12)"
1908 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1911 .I cap_usr_time_zero
1912 is set, then the hardware clock (the TSC timestamp counter on x86)
1913 can be calculated from the
1922 time = timestamp \- time_zero;
1923 quot = time / time_mult;
1924 rem = time % time_mult;
1925 cyc = (quot << time_shift) + (rem << time_shift) / time_mult;
1933 quot = cyc >> time_shift;
1934 rem = cyc & (((u64)1 << time_shift) \- 1);
1935 timestamp = time_zero + quot * time_mult +
1936 ((rem * time_mult) >> time_shift);
1941 This points to the head of the data section.
1942 The value continuously increases, it does not wrap.
1943 The value needs to be manually wrapped by the size of the mmap buffer
1944 before accessing the samples.
1946 On SMP-capable platforms, after reading the
1949 user space should issue an rmb().
1956 value should be written by user space to reflect the last read data.
1957 In this case, the kernel will not overwrite unread data.
1959 .IR data_offset " (since Linux 4.1)"
1960 .\" commit e8c6deac69629c0cb97c3d3272f8631ef17f8f0f
1961 Contains the offset of the location in the mmap buffer
1962 where perf sample data begins.
1964 .IR data_size " (since Linux 4.1)"
1965 .\" commit e8c6deac69629c0cb97c3d3272f8631ef17f8f0f
1966 Contains the size of the perf sample region within
1969 .IR aux_head ", " aux_tail ", " aux_offset ", " aux_size " (since Linux 4.1)"
1970 .\" commit 45bfb2e50471abbbfd83d40d28c986078b0d24ff
1971 The AUX region allows
1973 a separate sample buffer for
1974 high-bandwidth data streams (separate from the main perf sample buffer).
1975 An example of a high-bandwidth stream is instruction tracing support,
1976 as is found in newer Intel processors.
1978 To set up an AUX area, first
1980 needs to be set with an offset greater than
1981 .IR data_offset + data_size
1984 needs to be set to the desired buffer size.
1985 The desired offset and size must be page aligned, and the size
1986 must be a power of two.
1987 These values are then passed to mmap in order to map the AUX buffer.
1988 Pages in the AUX buffer are included as part of the
1992 and also as part of the
1993 .I perf_event_mlock_kb
1996 By default, the AUX buffer will be truncated if it will not fit
1997 in the available space in the ring buffer.
1998 If the AUX buffer is mapped as a read only buffer, then it will
1999 operate in ring buffer mode where old data will be overwritten
2001 In overwrite mode, it might not be possible to infer where the
2002 new data began, and it is the consumer's job to disable
2003 measurement while reading to avoid possible data races.
2009 ring buffer pointers have the same behavior and ordering
2010 rules as the previous described
2015 The following 2^n ring-buffer pages have the layout described below.
2018 .I perf_event_attr.sample_id_all
2019 is set, then all event types will
2020 have the sample_type selected fields related to where/when (identity)
2021 an event took place (TID, TIME, ID, CPU, STREAM_ID) described in
2022 .B PERF_RECORD_SAMPLE
2023 below, it will be stashed just after the
2024 .I perf_event_header
2025 and the fields already present for the existing
2026 fields, that is, at the end of the payload.
2027 This allows a newer perf.data
2028 file to be supported by older perf tools, with the new optional
2029 fields being ignored.
2031 The mmap values start with a header:
2035 struct perf_event_header {
2043 Below, we describe the
2044 .I perf_event_header
2045 fields in more detail.
2046 For ease of reading,
2047 the fields with shorter descriptions are presented first.
2050 This indicates the size of the record.
2055 field contains additional information about the sample.
2057 The CPU mode can be determined from this value by masking with
2058 .B PERF_RECORD_MISC_CPUMODE_MASK
2059 and looking for one of the following (note these are not
2060 bit masks, only one can be set at a time):
2063 .B PERF_RECORD_MISC_CPUMODE_UNKNOWN
2066 .B PERF_RECORD_MISC_KERNEL
2067 Sample happened in the kernel.
2069 .B PERF_RECORD_MISC_USER
2070 Sample happened in user code.
2072 .B PERF_RECORD_MISC_HYPERVISOR
2073 Sample happened in the hypervisor.
2075 .BR PERF_RECORD_MISC_GUEST_KERNEL " (since Linux 2.6.35)"
2076 .\" commit 39447b386c846bbf1c56f6403c5282837486200f
2077 Sample happened in the guest kernel.
2079 .B PERF_RECORD_MISC_GUEST_USER " (since Linux 2.6.35)"
2080 .\" commit 39447b386c846bbf1c56f6403c5282837486200f
2081 Sample happened in guest user code.
2085 Since the following three statuses are generated by
2086 different record types, they alias to the same bit:
2088 .BR PERF_RECORD_MISC_MMAP_DATA " (since Linux 3.10)"
2089 .\" commit 2fe85427e3bf65d791700d065132772fc26e4d75
2090 This is set when the mapping is not executable;
2091 otherwise the mapping is executable.
2093 .BR PERF_RECORD_MISC_COMM_EXEC " (since Linux 3.16)"
2094 .\" commit 82b897782d10fcc4930c9d4a15b175348fdd2871
2097 record on kernels more recent than Linux 3.16
2098 if a process name change was caused by an
2102 .BR PERF_RECORD_MISC_SWITCH_OUT " (since Linux 4.3)"
2103 .\" commit 45ac1403f564f411c6a383a2448688ba8dd705a4
2105 .B PERF_RECORD_SWITCH
2107 .B PERF_RECORD_SWITCH_CPU_WIDE
2108 record is generated, this bit indicates that the
2109 context switch is away from the current process
2110 (instead of into the current process).
2114 In addition, the following bits can be set:
2116 .B PERF_RECORD_MISC_EXACT_IP
2117 This indicates that the content of
2120 to the actual instruction that triggered the event.
2122 .IR perf_event_attr.precise_ip .
2124 .BR PERF_RECORD_MISC_SWITCH_OUT_PREEMPT " (since Linux 4.17)"
2125 .\" commit 101592b4904ecf6b8ed2a4784d41d180319d95a1
2127 .B PERF_RECORD_SWITCH
2129 .B PERF_RECORD_SWITCH_CPU_WIDE
2130 record is generated,
2131 this indicates the context switch was a preemption.
2133 .BR PERF_RECORD_MISC_MMAP_BUILD_ID " (since Linux 5.12)"
2134 .\" commit 88a16a1309333e43d328621ece3e9fa37027e8eb
2135 This indicates that the content of
2136 .B PERF_SAMPLE_MMAP2
2137 contains build-ID data instead of device major and minor numbers
2138 as well as the inode number.
2140 .BR PERF_RECORD_MISC_EXT_RESERVED " (since Linux 2.6.35)"
2141 .\" commit 1676b8a077c352085d52578fb4f29350b58b6e74
2142 This indicates there is extended data available (currently not used).
2144 .B PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT
2145 .\" commit 930e6fcd2bcce9bcd9d4aa7e755678d33f3fe6f4
2146 This bit is not set by the kernel.
2147 It is reserved for the user-space perf utility to indicate that
2148 .I /proc/i[pid]/maps
2149 parsing was taking too long and was stopped, and thus the mmap
2150 records may be truncated.
2156 value is one of the below.
2157 The values in the corresponding record (that follows the header)
2164 The MMAP events record the
2166 mappings so that we can correlate
2167 user-space IPs to code.
2168 They have the following structure:
2173 struct perf_event_header header;
2191 is the address of the allocated memory.
2193 is the length of the allocated memory.
2195 is the page offset of the allocated memory.
2197 is a string describing the backing of the allocated memory.
2201 This record indicates when events are lost.
2206 struct perf_event_header header;
2209 struct sample_id sample_id;
2216 is the unique event ID for the samples that were lost.
2219 is the number of events that were lost.
2223 This record indicates a change in the process name.
2228 struct perf_event_header header;
2232 struct sample_id sample_id;
2245 is a string containing the new name of the process.
2249 This record indicates a process exit event.
2254 struct perf_event_header header;
2258 struct sample_id sample_id;
2263 .BR PERF_RECORD_THROTTLE ", " PERF_RECORD_UNTHROTTLE
2264 This record indicates a throttle/unthrottle event.
2269 struct perf_event_header header;
2273 struct sample_id sample_id;
2279 This record indicates a fork event.
2284 struct perf_event_header header;
2288 struct sample_id sample_id;
2294 This record indicates a read event.
2299 struct perf_event_header header;
2301 struct read_format values;
2302 struct sample_id sample_id;
2307 .B PERF_RECORD_SAMPLE
2308 This record indicates a sample.
2313 struct perf_event_header header;
2314 u64 sample_id; /* if PERF_SAMPLE_IDENTIFIER */
2315 u64 ip; /* if PERF_SAMPLE_IP */
2316 u32 pid, tid; /* if PERF_SAMPLE_TID */
2317 u64 time; /* if PERF_SAMPLE_TIME */
2318 u64 addr; /* if PERF_SAMPLE_ADDR */
2319 u64 id; /* if PERF_SAMPLE_ID */
2320 u64 stream_id; /* if PERF_SAMPLE_STREAM_ID */
2321 u32 cpu, res; /* if PERF_SAMPLE_CPU */
2322 u64 period; /* if PERF_SAMPLE_PERIOD */
2323 struct read_format v;
2324 /* if PERF_SAMPLE_READ */
2325 u64 nr; /* if PERF_SAMPLE_CALLCHAIN */
2326 u64 ips[nr]; /* if PERF_SAMPLE_CALLCHAIN */
2327 u32 size; /* if PERF_SAMPLE_RAW */
2328 char data[size]; /* if PERF_SAMPLE_RAW */
2329 u64 bnr; /* if PERF_SAMPLE_BRANCH_STACK */
2330 struct perf_branch_entry lbr[bnr];
2331 /* if PERF_SAMPLE_BRANCH_STACK */
2332 u64 abi; /* if PERF_SAMPLE_REGS_USER */
2333 u64 regs[weight(mask)];
2334 /* if PERF_SAMPLE_REGS_USER */
2335 u64 size; /* if PERF_SAMPLE_STACK_USER */
2336 char data[size]; /* if PERF_SAMPLE_STACK_USER */
2337 u64 dyn_size; /* if PERF_SAMPLE_STACK_USER &&
2339 union perf_sample_weight weight;
2340 /* if PERF_SAMPLE_WEIGHT */
2341 /* || PERF_SAMPLE_WEIGHT_STRUCT */
2342 u64 data_src; /* if PERF_SAMPLE_DATA_SRC */
2343 u64 transaction; /* if PERF_SAMPLE_TRANSACTION */
2344 u64 abi; /* if PERF_SAMPLE_REGS_INTR */
2345 u64 regs[weight(mask)];
2346 /* if PERF_SAMPLE_REGS_INTR */
2347 u64 phys_addr; /* if PERF_SAMPLE_PHYS_ADDR */
2348 u64 cgroup; /* if PERF_SAMPLE_CGROUP */
2350 /* if PERF_SAMPLE_DATA_PAGE_SIZE */
2352 /* if PERF_SAMPLE_CODE_PAGE_SIZE */
2353 u64 size; /* if PERF_SAMPLE_AUX */
2354 char data[size]; /* if PERF_SAMPLE_AUX */
2362 .B PERF_SAMPLE_IDENTIFIER
2363 is enabled, a 64-bit unique ID is included.
2364 This is a duplication of the
2367 value, but included at the beginning of the sample
2368 so parsers can easily obtain the value.
2373 is enabled, then a 64-bit instruction
2374 pointer value is included.
2379 is enabled, then a 32-bit process ID
2380 and 32-bit thread ID are included.
2385 is enabled, then a 64-bit timestamp
2387 This is obtained via local_clock() which is a hardware timestamp
2388 if available and the jiffies value if not.
2393 is enabled, then a 64-bit address is included.
2394 This is usually the address of a tracepoint,
2395 breakpoint, or software event; otherwise the value is 0.
2400 is enabled, a 64-bit unique ID is included.
2401 If the event is a member of an event group, the group leader ID is returned.
2402 This ID is the same as the one returned by
2403 .BR PERF_FORMAT_ID .
2407 .B PERF_SAMPLE_STREAM_ID
2408 is enabled, a 64-bit unique ID is included.
2411 the actual ID is returned, not the group leader.
2412 This ID is the same as the one returned by
2413 .BR PERF_FORMAT_ID .
2418 is enabled, this is a 32-bit value indicating
2419 which CPU was being used, in addition to a reserved (unused)
2424 .B PERF_SAMPLE_PERIOD
2425 is enabled, a 64-bit value indicating
2426 the current sampling period is written.
2431 is enabled, a structure of type read_format
2432 is included which has values for all events in the event group.
2433 The values included depend on the
2436 .BR perf_event_open ()
2441 .B PERF_SAMPLE_CALLCHAIN
2442 is enabled, then a 64-bit number is included
2443 which indicates how many following 64-bit instruction pointers will
2445 This is the current callchain.
2447 .IR size ", " data[size]
2450 is enabled, then a 32-bit value indicating size
2451 is included followed by an array of 8-bit values of length size.
2452 The values are padded with 0 to have 64-bit alignment.
2454 This RAW record data is opaque with respect to the ABI.
2455 The ABI doesn't make any promises with respect to the stability
2456 of its content, it may vary depending
2457 on event, hardware, and kernel version.
2459 .IR bnr ", " lbr[bnr]
2461 .B PERF_SAMPLE_BRANCH_STACK
2462 is enabled, then a 64-bit value indicating
2463 the number of records is included, followed by
2465 .I perf_branch_entry
2466 structures which each include the fields:
2470 This indicates the source instruction (may not be a branch).
2476 The branch target was mispredicted.
2479 The branch target was predicted.
2481 .IR in_tx " (since Linux 3.11)"
2482 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
2483 The branch was in a transactional memory transaction.
2485 .IR abort " (since Linux 3.11)"
2486 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
2487 The branch was in an aborted transactional memory transaction.
2489 .IR cycles " (since Linux 4.3)"
2490 .\" commit 71ef3c6b9d4665ee7afbbe4c208a98917dcfc32f
2491 This reports the number of cycles elapsed since the
2492 previous branch stack update.
2494 The entries are from most to least recent, so the first entry
2495 has the most recent branch.
2502 is optional; if not supported, those
2505 The type of branches recorded is specified by the
2506 .I branch_sample_type
2510 .IR abi ", " regs[weight(mask)]
2512 .B PERF_SAMPLE_REGS_USER
2513 is enabled, then the user CPU registers are recorded.
2518 .BR PERF_SAMPLE_REGS_ABI_NONE ,
2519 .BR PERF_SAMPLE_REGS_ABI_32 ,
2521 .BR PERF_SAMPLE_REGS_ABI_64 .
2525 field is an array of the CPU registers that were specified by
2529 The number of values is the number of bits set in the
2533 .IR size ", " data[size] ", " dyn_size
2535 .B PERF_SAMPLE_STACK_USER
2536 is enabled, then the user stack is recorded.
2537 This can be used to generate stack backtraces.
2539 is the size requested by the user in
2540 .I sample_stack_user
2541 or else the maximum record size.
2543 is the stack data (a raw dump of the memory pointed to by the
2544 stack pointer at the time of sampling).
2546 is the amount of data actually dumped (can be less than
2556 .B PERF_SAMPLE_WEIGHT
2558 .B PERF_SAMPLE_WEIGHT_STRUCT
2559 is enabled, then a 64-bit value provided by the hardware
2560 is recorded that indicates how costly the event was.
2561 This allows expensive events to stand out more clearly
2566 .B PERF_SAMPLE_DATA_SRC
2567 is enabled, then a 64-bit value is recorded that is made up of
2568 the following fields:
2572 Type of opcode, a bitwise combination of:
2583 .B PERF_MEM_OP_STORE
2586 .B PERF_MEM_OP_PFETCH
2595 Memory hierarchy level hit or miss, a bitwise combination of
2596 the following, shifted left by
2597 .BR PERF_MEM_LVL_SHIFT :
2608 .B PERF_MEM_LVL_MISS
2623 .B PERF_MEM_LVL_LOC_RAM
2626 .B PERF_MEM_LVL_REM_RAM1
2629 .B PERF_MEM_LVL_REM_RAM2
2632 .B PERF_MEM_LVL_REM_CCE1
2635 .B PERF_MEM_LVL_REM_CCE2
2647 Snoop mode, a bitwise combination of the following, shifted left by
2648 .BR PERF_MEM_SNOOP_SHIFT :
2653 .B PERF_MEM_SNOOP_NA
2656 .B PERF_MEM_SNOOP_NONE
2659 .B PERF_MEM_SNOOP_HIT
2662 .B PERF_MEM_SNOOP_MISS
2665 .B PERF_MEM_SNOOP_HITM
2671 Lock instruction, a bitwise combination of the following, shifted left by
2672 .BR PERF_MEM_LOCK_SHIFT :
2680 .B PERF_MEM_LOCK_LOCKED
2686 TLB access hit or miss, a bitwise combination of the following, shifted
2688 .BR PERF_MEM_TLB_SHIFT :
2699 .B PERF_MEM_TLB_MISS
2719 .B PERF_SAMPLE_TRANSACTION
2720 flag is set, then a 64-bit field is recorded describing
2721 the sources of any transactional memory aborts.
2723 The field is a bitwise combination of the following values:
2727 Abort from an elision type transaction (Intel-CPU-specific).
2729 .B PERF_TXN_TRANSACTION
2730 Abort from a generic transaction.
2733 Synchronous abort (related to the reported instruction).
2736 Asynchronous abort (not related to the reported instruction).
2739 Retryable abort (retrying the transaction may have succeeded).
2741 .B PERF_TXN_CONFLICT
2742 Abort due to memory conflicts with other threads.
2744 .B PERF_TXN_CAPACITY_WRITE
2745 Abort due to write capacity overflow.
2747 .B PERF_TXN_CAPACITY_READ
2748 Abort due to read capacity overflow.
2751 In addition, a user-specified abort code can be obtained from
2752 the high 32 bits of the field by shifting right by
2753 .B PERF_TXN_ABORT_SHIFT
2754 and masking with the value
2755 .BR PERF_TXN_ABORT_MASK .
2757 .IR abi ", " regs[weight(mask)]
2759 .B PERF_SAMPLE_REGS_INTR
2760 is enabled, then the user CPU registers are recorded.
2765 .BR PERF_SAMPLE_REGS_ABI_NONE ,
2766 .BR PERF_SAMPLE_REGS_ABI_32 ,
2768 .BR PERF_SAMPLE_REGS_ABI_64 .
2772 field is an array of the CPU registers that were specified by
2776 The number of values is the number of bits set in the
2782 .B PERF_SAMPLE_PHYS_ADDR
2783 flag is set, then the 64-bit physical address is recorded.
2787 .B PERF_SAMPLE_CGROUP
2789 then the 64-bit cgroup ID (for the perf_event subsystem) is recorded.
2790 To get the pathname of the cgroup, the ID should match to one in a
2791 .BR PERF_RECORD_CGROUP .
2795 .B PERF_SAMPLE_DATA_PAGE_SIZE
2797 then the 64-bit page size value of the
2799 address is recorded.
2803 .B PERF_SAMPLE_CODE_PAGE_SIZE
2805 then the 64-bit page size value of the
2807 address is recorded.
2815 a snapshot of the aux buffer is recorded.
2818 .B PERF_RECORD_MMAP2
2819 This record includes extended information on
2821 calls returning executable mappings.
2822 The format is similar to that of the
2824 record, but includes extra values that allow uniquely identifying
2827 .B PERF_RECORD_MISC_MMAP_BUILD_ID
2829 the extra values have different layout and meanings.
2834 struct perf_event_header header;
2847 struct { /* if PERF_RECORD_MISC_MMAP_BUILD_ID */
2857 struct sample_id sample_id;
2870 is the address of the allocated memory.
2873 is the length of the allocated memory.
2876 is the page offset of the allocated memory.
2879 is the major ID of the underlying device.
2882 is the minor ID of the underlying device.
2885 is the inode number.
2888 is the inode generation.
2891 is the actual size of
2896 is a raw data to identify a binary.
2899 is the protection information.
2902 is the flags information.
2905 is a string describing the backing of the allocated memory.
2908 .BR PERF_RECORD_AUX " (since Linux 4.1)"
2909 .\" commit 68db7e98c3a6ebe7284b6cf14906ed7c55f3f7f0
2910 This record reports that new data is available in the separate
2916 struct perf_event_header header;
2920 struct sample_id sample_id;
2927 offset in the AUX mmap region where the new data begins.
2930 size of the data made available.
2933 describes the AUX update.
2936 .B PERF_AUX_FLAG_TRUNCATED
2937 if set, then the data returned was truncated to fit the available
2940 .B PERF_AUX_FLAG_OVERWRITE
2941 .\" commit 2023a0d2829e521fe6ad6b9907f3f90bfbf57142
2942 if set, then the data returned has overwritten previous data.
2946 .BR PERF_RECORD_ITRACE_START " (since Linux 4.1)"
2947 .\" ec0d7729bbaed4b9d2d3fada693278e13a3d1368
2948 This record indicates which process has initiated an instruction
2949 trace event, allowing tools to properly correlate the instruction
2950 addresses in the AUX buffer with the proper executable.
2955 struct perf_event_header header;
2964 process ID of the thread starting an instruction trace.
2967 thread ID of the thread starting an instruction trace.
2970 .BR PERF_RECORD_LOST_SAMPLES " (since Linux 4.2)"
2971 .\" f38b0dbb491a6987e198aa6b428db8692a6480f8
2972 When using hardware sampling (such as Intel PEBS) this record
2973 indicates some number of samples that may have been lost.
2978 struct perf_event_header header;
2980 struct sample_id sample_id;
2987 the number of potentially lost samples.
2990 .BR PERF_RECORD_SWITCH " (since Linux 4.3)"
2991 .\" commit 45ac1403f564f411c6a383a2448688ba8dd705a4
2992 This record indicates a context switch has happened.
2994 .B PERF_RECORD_MISC_SWITCH_OUT
2997 field indicates whether it was a context switch into
2998 or away from the current process.
3003 struct perf_event_header header;
3004 struct sample_id sample_id;
3009 .BR PERF_RECORD_SWITCH_CPU_WIDE " (since Linux 4.3)"
3010 .\" commit 45ac1403f564f411c6a383a2448688ba8dd705a4
3012 .B PERF_RECORD_SWITCH
3013 this record indicates a context switch has happened,
3014 but it only occurs when sampling in CPU-wide mode
3015 and provides additional information on the process
3016 being switched to/from.
3018 .B PERF_RECORD_MISC_SWITCH_OUT
3021 field indicates whether it was a context switch into
3022 or away from the current process.
3027 struct perf_event_header header;
3030 struct sample_id sample_id;
3037 The process ID of the previous (if switching in)
3038 or next (if switching out) process on the CPU.
3041 The thread ID of the previous (if switching in)
3042 or next (if switching out) thread on the CPU.
3045 .BR PERF_RECORD_NAMESPACES " (since Linux 4.11)"
3046 .\" commit e422267322cd319e2695a535e47c5b1feeac45eb
3047 This record includes various namespace information of a process.
3052 struct perf_event_header header;
3056 struct { u64 dev, inode } [nr_namespaces];
3057 struct sample_id sample_id;
3070 is the number of namespaces in this record
3077 fields and is recorded in the
3078 fixed position like below:
3081 .BR NET_NS_INDEX = 0
3084 .BR UTS_NS_INDEX = 1
3087 .BR IPC_NS_INDEX = 2
3090 .BR PID_NS_INDEX = 3
3093 .BR USER_NS_INDEX = 4
3096 .BR MNT_NS_INDEX = 5
3099 .BR CGROUP_NS_INDEX = 6
3103 .BR PERF_RECORD_KSYMBOL " (since Linux 5.0)"
3104 .\" commit 76193a94522f1d4edf2447a536f3f796ce56343b
3105 This record indicates kernel symbol register/unregister events.
3110 struct perf_event_header header;
3116 struct sample_id sample_id;
3123 is the address of the kernel symbol.
3126 is the length of the kernel symbol.
3129 is the type of the kernel symbol.
3130 Currently the following types are available:
3133 .B PERF_RECORD_KSYMBOL_TYPE_BPF
3134 The kernel symbol is a BPF function.
3139 .B PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER
3140 is set, then this event is for unregistering the kernel symbol.
3143 .BR PERF_RECORD_BPF_EVENT " (since Linux 5.0)"
3144 .\" commit 6ee52e2a3fe4ea35520720736e6791df1fb67106
3145 This record indicates BPF program is loaded or unloaded.
3150 struct perf_event_header header;
3154 u8 tag[BPF_TAG_SIZE];
3155 struct sample_id sample_id;
3162 is one of the following values:
3165 .B PERF_BPF_EVENT_PROG_LOAD
3166 A BPF program is loaded
3168 .B PERF_BPF_EVENT_PROG_UNLOAD
3169 A BPF program is unloaded
3173 is the ID of the BPF program.
3176 is the tag of the BPF program.
3182 .BR PERF_RECORD_CGROUP " (since Linux 5.7)"
3183 .\" commit 96aaab686505c449e24d76e76507290dcc30e008
3184 This record indicates a new cgroup is created and activated.
3189 struct perf_event_header header;
3192 struct sample_id sample_id;
3199 is the cgroup identifier.
3200 This can be also retrieved by
3201 .BR name_to_handle_at (2)
3202 on the cgroup path (as a file handle).
3205 is the path of the cgroup from the root.
3208 .BR PERF_RECORD_TEXT_POKE " (since Linux 5.8)"
3209 .\" commit e17d43b93e544f5016c0251d2074c15568d5d963
3210 This record indicates a change in the kernel text.
3211 This includes addition and removal of the text
3212 and the corresponding length is zero in this case.
3217 struct perf_event_header header;
3222 struct sample_id sample_id;
3229 is the address of the change
3238 contains old bytes immediately followed by new bytes.
3241 .SS Overflow handling
3242 Events can be set to notify when a threshold is crossed,
3243 indicating an overflow.
3244 Overflow conditions can be captured by monitoring the
3245 event file descriptor with
3250 Alternatively, the overflow events can be captured via sa signal handler,
3251 by enabling I/O signaling on the file descriptor; see the discussion of the
3258 Overflows are generated only by sampling events
3260 must have a nonzero value).
3262 There are two ways to generate overflow notifications.
3264 The first is to set a
3268 value that will trigger if a certain number of samples
3269 or bytes have been written to the mmap ring buffer.
3274 The other way is by use of the
3275 .B PERF_EVENT_IOC_REFRESH
3277 This ioctl adds to a counter that decrements each time the event overflows.
3281 once the counter reaches 0
3284 the underlying event is disabled.
3286 Refreshing an event group leader refreshes all siblings and
3287 refreshing with a parameter of 0 currently enables infinite
3289 these behaviors are unsupported and should not be relied on.
3290 .\" See https://lkml.org/lkml/2011/5/24/337
3292 Starting with Linux 3.18,
3293 .\" commit 179033b3e064d2cd3f5f9945e76b0a0f0fbf4883
3295 is indicated if the event being monitored is attached to a different
3296 process and that process exits.
3297 .SS rdpmc instruction
3298 Starting with Linux 3.4 on x86, you can use the
3299 .\" commit c7206205d00ab375839bd6c7ddb247d600693c09
3301 instruction to get low-latency reads without having to enter the kernel.
3304 is not necessarily faster than other methods for reading event values.
3306 Support for this can be detected with the
3308 field in the mmap page; documentation on how
3309 to calculate event values can be found in that section.
3311 Originally, when rdpmc support was enabled, any process (not just ones
3312 with an active perf event) could use the rdpmc instruction to access
3314 Starting with Linux 4.0,
3315 .\" 7911d3f7af14a614617e38245fedf98a724e46a9
3316 rdpmc support is only allowed if an event is currently enabled
3317 in a process's context.
3318 To restore the old behavior, write the value 2 to
3319 .IR /sys/devices/cpu/rdpmc .
3320 .SS perf_event ioctl calls
3321 Various ioctls act on
3322 .BR perf_event_open ()
3325 .B PERF_EVENT_IOC_ENABLE
3326 This enables the individual event or event group specified by the
3327 file descriptor argument.
3330 .B PERF_IOC_FLAG_GROUP
3331 bit is set in the ioctl argument, then all events in a group are
3332 enabled, even if the event specified is not the group leader
3335 .B PERF_EVENT_IOC_DISABLE
3336 This disables the individual counter or event group specified by the
3337 file descriptor argument.
3339 Enabling or disabling the leader of a group enables or disables the
3340 entire group; that is, while the group leader is disabled, none of the
3341 counters in the group will count.
3342 Enabling or disabling a member of a group other than the leader
3343 affects only that counter; disabling a non-leader
3344 stops that counter from counting but doesn't affect any other counter.
3347 .B PERF_IOC_FLAG_GROUP
3348 bit is set in the ioctl argument, then all events in a group are
3349 disabled, even if the event specified is not the group leader
3352 .B PERF_EVENT_IOC_REFRESH
3353 Non-inherited overflow counters can use this
3354 to enable a counter for a number of overflows specified by the argument,
3355 after which it is disabled.
3356 Subsequent calls of this ioctl add the argument value to the current
3358 An overflow notification with
3360 set will happen on each overflow until the
3361 count reaches 0; when that happens a notification with
3363 set is sent and the event is disabled.
3364 Using an argument of 0 is considered undefined behavior.
3366 .B PERF_EVENT_IOC_RESET
3367 Reset the event count specified by the
3368 file descriptor argument to zero.
3369 This resets only the counts; there is no way to reset the
3377 .B PERF_IOC_FLAG_GROUP
3378 bit is set in the ioctl argument, then all events in a group are
3379 reset, even if the event specified is not the group leader
3382 .B PERF_EVENT_IOC_PERIOD
3383 This updates the overflow period for the event.
3385 Since Linux 3.7 (on ARM)
3386 .\" commit 3581fe0ef37ce12ac7a4f74831168352ae848edc
3387 and Linux 3.14 (all other architectures),
3388 .\" commit bad7192b842c83e580747ca57104dd51fe08c223
3389 the new period takes effect immediately.
3390 On older kernels, the new period did not take effect until
3391 after the next overflow.
3393 The argument is a pointer to a 64-bit value containing the
3396 Prior to Linux 2.6.36,
3397 .\" commit ad0cf3478de8677f720ee06393b3147819568d6a
3398 this ioctl always failed due to a bug
3401 .B PERF_EVENT_IOC_SET_OUTPUT
3402 This tells the kernel to report event notifications to the specified
3403 file descriptor rather than the default one.
3404 The file descriptors must all be on the same CPU.
3406 The argument specifies the desired file descriptor, or \-1 if
3407 output should be ignored.
3409 .BR PERF_EVENT_IOC_SET_FILTER " (since Linux 2.6.33)"
3410 .\" commit 6fb2915df7f0747d9044da9dbff5b46dc2e20830
3411 This adds an ftrace filter to this event.
3413 The argument is a pointer to the desired ftrace filter.
3415 .BR PERF_EVENT_IOC_ID " (since Linux 3.12)"
3416 .\" commit cf4957f17f2a89984915ea808876d9c82225b862
3417 This returns the event ID value for the given event file descriptor.
3419 The argument is a pointer to a 64-bit unsigned integer
3422 .BR PERF_EVENT_IOC_SET_BPF " (since Linux 4.1)"
3423 .\" commit 2541517c32be2531e0da59dfd7efc1ce844644f5
3424 This allows attaching a Berkeley Packet Filter (BPF)
3425 program to an existing kprobe tracepoint event.
3428 (since Linux 5.8) or
3430 privileges to use this ioctl.
3432 The argument is a BPF program file descriptor that was created by
3437 .BR PERF_EVENT_IOC_PAUSE_OUTPUT " (since Linux 4.7)"
3438 .\" commit 86e7972f690c1017fd086cdfe53d8524e68c661c
3439 This allows pausing and resuming the event's ring-buffer.
3440 A paused ring-buffer does not prevent generation of samples,
3441 but simply discards them.
3442 The discarded samples are considered lost, and cause a
3444 sample to be generated when possible.
3445 An overflow signal may still be triggered by the discarded sample
3446 even though the ring-buffer remains empty.
3448 The argument is an unsigned 32-bit integer.
3449 A nonzero value pauses the ring-buffer, while a
3450 zero value resumes the ring-buffer.
3452 .BR PERF_EVENT_MODIFY_ATTRIBUTES " (since Linux 4.17)"
3453 .\" commit 32ff77e8cc9e66cc4fb38098f64fd54cc8f54573
3454 This allows modifying an existing event without the overhead
3455 of closing and reopening a new event.
3456 Currently this is supported only for breakpoint events.
3458 The argument is a pointer to a
3460 structure containing the updated event settings.
3462 .BR PERF_EVENT_IOC_QUERY_BPF " (since Linux 4.16)"
3463 .\" commit f371b304f12e31fe30207c41ca7754564e0ea4dc
3464 This allows querying which Berkeley Packet Filter (BPF)
3465 programs are attached to an existing kprobe tracepoint.
3466 You can only attach one BPF program per event, but you can
3467 have multiple events attached to a tracepoint.
3468 Querying this value on one tracepoint event returns the ID
3469 of all BPF programs in all events attached to the tracepoint.
3472 (since Linux 5.8) or
3474 privileges to use this ioctl.
3476 The argument is a pointer to a structure
3479 struct perf_event_query_bpf {
3489 field indicates the number of ids that can fit in the provided
3494 value is filled in by the kernel with the number of attached
3498 array is filled with the ID of each attached BPF program.
3499 If there are more programs than will fit in the array, then the
3504 will indicate the number of program IDs that were successfully copied.
3507 A process can enable or disable all currently open event groups
3510 .B PR_TASK_PERF_EVENTS_ENABLE
3512 .B PR_TASK_PERF_EVENTS_DISABLE
3514 This applies only to events created locally by the calling process.
3515 This does not apply to events created by other processes attached
3516 to the calling process or inherited events from a parent process.
3517 Only group leaders are enabled and disabled,
3518 not any other members of the groups.
3519 .SS perf_event related configuration files
3521 .I /proc/sys/kernel/
3524 .I /proc/sys/kernel/perf_event_paranoid
3526 .I perf_event_paranoid
3527 file can be set to restrict access to the performance counters.
3533 allow only user-space measurements (default since Linux 4.6).
3534 .\" default changed in commit 0161028b7c8aebef64194d3d73e43bc3b53b5c66
3537 allow both kernel and user measurements (default before Linux 4.6).
3540 allow access to CPU-specific data but not raw tracepoint samples.
3547 The existence of the
3548 .I perf_event_paranoid
3549 file is the official method for determining if a kernel supports
3550 .BR perf_event_open ().
3552 .I /proc/sys/kernel/perf_event_max_sample_rate
3553 This sets the maximum sample rate.
3554 Setting this too high can allow
3555 users to sample at a rate that impacts overall machine performance
3556 and potentially lock up the machine.
3557 The default value is
3558 100000 (samples per second).
3560 .I /proc/sys/kernel/perf_event_max_stack
3561 .\" Introduced in c5dfd78eb79851e278b7973031b9ca363da87a7e
3562 This file sets the maximum depth of stack frame entries reported
3563 when generating a call trace.
3565 .I /proc/sys/kernel/perf_event_mlock_kb
3566 Maximum number of pages an unprivileged user can
3568 The default is 516 (kB).
3572 .I /sys/bus/event_source/devices/
3575 Since Linux 2.6.34, the kernel supports having multiple PMUs
3576 available for monitoring.
3577 Information on how to program these PMUs can be found under
3578 .IR /sys/bus/event_source/devices/ .
3579 Each subdirectory corresponds to a different PMU.
3581 .IR /sys/bus/event_source/devices/*/type " (since Linux 2.6.38)"
3582 .\" commit abe43400579d5de0078c2d3a760e6598e183f871
3583 This contains an integer that can be used in the
3587 to indicate that you wish to use this PMU.
3589 .IR /sys/bus/event_source/devices/cpu/rdpmc " (since Linux 3.4)"
3590 .\" commit 0c9d42ed4cee2aa1dfc3a260b741baae8615744f
3591 If this file is 1, then direct user-space access to the
3592 performance counter registers is allowed via the rdpmc instruction.
3593 This can be disabled by echoing 0 to the file.
3596 .\" a66734297f78707ce39d756b656bfae861d53f62
3597 .\" 7911d3f7af14a614617e38245fedf98a724e46a9
3598 the behavior has changed, so that 1 now means only allow access
3599 to processes with active perf events, with 2 indicating the old
3600 allow-anyone-access behavior.
3602 .IR /sys/bus/event_source/devices/*/format/ " (since Linux 3.4)"
3603 .\" commit 641cc938815dfd09f8fa1ec72deb814f0938ac33
3604 This subdirectory contains information on the architecture-specific
3605 subfields available for programming the various
3611 The content of each file is the name of the config field, followed
3612 by a colon, followed by a series of integer bit ranges separated by
3614 For example, the file
3616 may contain the value
3617 .I config1:1,6\-10,44
3618 which indicates that event is an attribute that occupies bits 1,6\[en]10, and 44
3620 .IR perf_event_attr::config1 .
3622 .IR /sys/bus/event_source/devices/*/events/ " (since Linux 3.4)"
3623 .\" commit 641cc938815dfd09f8fa1ec72deb814f0938ac33
3624 This subdirectory contains files with predefined events.
3625 The contents are strings describing the event settings
3626 expressed in terms of the fields found in the previously mentioned
3629 These are not necessarily complete lists of all events supported by
3630 a PMU, but usually a subset of events deemed useful or interesting.
3632 The content of each file is a list of attribute names
3633 separated by commas.
3634 Each entry has an optional value (either hex or decimal).
3635 If no value is specified, then it is assumed to be a single-bit
3636 field with a value of 1.
3637 An example entry may look like this:
3638 .IR event=0x2,inv,ldlat=3 .
3640 .I /sys/bus/event_source/devices/*/uevent
3641 This file is the standard kernel device interface
3642 for injecting hotplug events.
3644 .IR /sys/bus/event_source/devices/*/cpumask " (since Linux 3.7)"
3645 .\" commit 314d9f63f385096580e9e2a06eaa0745d92fe4ac
3648 file contains a comma-separated list of integers that
3649 indicate a representative CPU number for each socket (package)
3651 This is needed when setting up uncore or northbridge events, as
3652 those PMUs present socket-wide events.
3656 .BR perf_event_open ()
3657 returns the new file descriptor.
3658 On error, \-1 is returned and
3660 is set to indicate the error.
3662 The errors returned by
3663 .BR perf_event_open ()
3664 can be inconsistent, and may
3665 vary across processor architectures and performance monitoring units.
3673 .BR PERF_ATTR_SIZE_VER0 ),
3674 too big (larger than the page size),
3675 or larger than the kernel supports and the extra bytes are not zero.
3681 field is overwritten by the kernel to be the size of the structure
3685 Returned when the requested event requires
3687 (since Linux 5.8) or
3689 permissions (or a more permissive perf_event paranoid setting).
3690 Some common cases where an unprivileged process
3691 may encounter this error:
3692 attaching to a process owned by a different user;
3693 monitoring all processes on a given CPU (i.e., specifying the
3698 when the paranoid setting requires it.
3703 file descriptor is not valid, or, if
3704 .B PERF_FLAG_PID_CGROUP
3706 the cgroup file descriptor in
3710 .BR EBUSY " (since Linux 4.1)"
3711 .\" bed5b25ad9c8a2f5d735ef0bc746ec870c01c1b0
3712 Returned if another event already has exclusive
3718 pointer points at an invalid memory address.
3721 Returned when trying to mix perf and ftrace handling
3725 Returned if the specified event is invalid.
3726 There are many possible reasons for this.
3727 A not-exhaustive list:
3729 is higher than the maximum setting;
3732 to monitor does not exist;
3739 value is out of range;
3743 set and the event is not a group leader;
3746 values are out of range or set reserved bits;
3747 the generic event selected is not supported; or
3748 there is not enough room to add the selected event.
3751 Each opened event uses one file descriptor.
3752 If a large number of events are opened,
3753 the per-process limit on the number of open file descriptors will be reached,
3754 and no more events can be created.
3757 Returned when the event involves a feature not supported
3763 setting is not valid.
3764 This error is also returned for
3765 some unsupported generic events.
3768 Prior to Linux 3.3, if there was not enough room for the event,
3769 .\" commit aa2bc1ade59003a379ffc485d6da2d92ea3370a6
3772 In Linux 3.3, this was changed to
3775 is still returned if you try to add more breakpoint events
3776 than supported by the hardware.
3780 .B PERF_SAMPLE_STACK_USER
3783 and it is not supported by hardware.
3786 Returned if an event requiring a specific hardware feature is
3787 requested but there is no hardware support.
3788 This includes requesting low-skid events if not supported,
3789 branch tracing if it is not available, sampling if no PMU
3790 interrupt is available, and branch stacks for software events.
3792 .BR EOVERFLOW " (since Linux 4.8)"
3793 .\" 97c79a38cd454602645f0470ffb444b3b75ce574
3795 .B PERF_SAMPLE_CALLCHAIN
3798 is larger than the maximum specified in
3799 .IR /proc/sys/kernel/perf_event_max_stack .
3802 Returned on many (but not all) architectures when an unsupported
3803 .IR exclude_hv ", " exclude_idle ", " exclude_user ", or " exclude_kernel
3804 setting is specified.
3806 It can also happen, as with
3808 when the requested event requires
3810 (since Linux 5.8) or
3812 permissions (or a more permissive perf_event paranoid setting).
3813 This includes setting a breakpoint on a kernel address,
3814 and (since Linux 3.13) setting a kernel function-trace tracepoint.
3815 .\" commit a4e95fc2cbb31d70a65beffeaf8773f881328c34
3818 Returned if attempting to attach to a process that does not exist.
3820 .BR perf_event_open ()
3821 was introduced in Linux 2.6.31 but was called
3822 .\" commit 0793a61d4df8daeac6492dbf8d2f3e5713caae5e
3823 .BR perf_counter_open ().
3824 It was renamed in Linux 2.6.32.
3825 .\" commit cdd6c482c9ff9c55475ee7392ec8f672eddb7be6
3828 .BR perf_event_open ()
3829 system call Linux-specific
3830 and should not be used in programs intended to be portable.
3832 The official way of knowing if
3833 .BR perf_event_open ()
3834 support is enabled is checking
3835 for the existence of the file
3836 .IR /proc/sys/kernel/perf_event_paranoid .
3839 capability (since Linux 5.8) provides secure approach to
3840 performance monitoring and observability operations in a system
3841 according to the principal of least privilege (POSIX IEEE 1003.1e).
3842 Accessing system performance monitoring and observability operations
3845 rather than the much more powerful
3847 excludes chances to misuse credentials and makes operations more secure.
3849 usage for secure system performance monitoring and observability
3850 is discouraged in favor of the
3858 is needed to properly get overflow signals in threads.
3859 This was introduced in Linux 2.6.32.
3860 .\" commit ba0a6c9f6fceed11c6a99e8326f0477fe383e6b5
3862 Prior to Linux 2.6.33 (at least for x86),
3863 .\" commit b690081d4d3f6a23541493f1682835c3cd5c54a1
3864 the kernel did not check
3865 if events could be scheduled together until read time.
3866 The same happens on all known kernels if the NMI watchdog is enabled.
3867 This means to see if a given set of events works you have to
3868 .BR perf_event_open (),
3869 start, then read before you know for sure you
3870 can get valid measurements.
3872 Prior to Linux 2.6.34,
3873 .\" FIXME . cannot find a kernel commit for this one
3874 event constraints were not enforced by the kernel.
3875 In that case, some events would silently return "0" if the kernel
3876 scheduled them in an improper counter slot.
3878 Prior to Linux 2.6.34, there was a bug when multiplexing where the
3879 wrong results could be returned.
3880 .\" commit 45e16a6834b6af098702e5ea6c9a40de42ff77d8
3882 Kernels from Linux 2.6.35 to Linux 2.6.39 can quickly crash the kernel if
3883 "inherit" is enabled and many threads are started.
3884 .\" commit 38b435b16c36b0d863efcf3f07b34a6fac9873fd
3886 Prior to Linux 2.6.35,
3887 .\" commit 050735b08ca8a016bbace4445fa025b88fee770b
3888 .B PERF_FORMAT_GROUP
3889 did not work with attached processes.
3891 There is a bug in the kernel code between
3892 Linux 2.6.36 and Linux 3.0 that ignores the
3893 "watermark" field and acts as if a wakeup_event
3894 was chosen if the union has a
3895 nonzero value in it.
3896 .\" commit 4ec8363dfc1451f8c8f86825731fe712798ada02
3898 From Linux 2.6.31 to Linux 3.4, the
3899 .B PERF_IOC_FLAG_GROUP
3900 ioctl argument was broken and would repeatedly operate
3901 on the event specified rather than iterating across
3902 all sibling events in a group.
3903 .\" commit 724b6daa13e100067c30cfc4d1ad06629609dc4e
3905 From Linux 3.4 to Linux 3.11, the mmap
3906 .\" commit fa7315871046b9a4c48627905691dbde57e51033
3910 bits mapped to the same location.
3911 Code should migrate to the new
3917 Always double-check your results!
3918 Various generalized events have had wrong values.
3919 For example, retired branches measured
3920 the wrong thing on AMD machines until Linux 2.6.35.
3921 .\" commit f287d332ce835f77a4f5077d2c0ef1e3f9ea42d2
3923 The following is a short example that measures the total
3924 instruction count of a call to
3927 .\" SRC BEGIN (perf_event_open.c)
3929 #include <linux/perf_event.h>
3933 #include <sys/ioctl.h>
3934 #include <sys/syscall.h>
3938 perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
3939 int cpu, int group_fd, unsigned long flags)
3943 ret = syscall(SYS_perf_event_open, hw_event, pid, cpu,
3953 struct perf_event_attr pe;
3955 memset(&pe, 0, sizeof(pe));
3956 pe.type = PERF_TYPE_HARDWARE;
3957 pe.size = sizeof(pe);
3958 pe.config = PERF_COUNT_HW_INSTRUCTIONS;
3960 pe.exclude_kernel = 1;
3963 fd = perf_event_open(&pe, 0, \-1, \-1, 0);
3965 fprintf(stderr, "Error opening leader %llx\en", pe.config);
3969 ioctl(fd, PERF_EVENT_IOC_RESET, 0);
3970 ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
3972 printf("Measuring instruction count for this printf\en");
3974 ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
3975 read(fd, &count, sizeof(count));
3977 printf("Used %lld instructions\en", count);
3991 .I Documentation/admin\-guide/perf\-security.rst
3992 in the kernel source tree