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 2021-03-22 "Linux" "Linux Programmer's Manual"
29 perf_event_open \- set up performance monitoring
32 .BR "#include <linux/perf_event.h>" " /* Definition of " PERF_* " constants */"
33 .BR "#include <linux/hw_breakpoint.h>" " /* Definition of " HW_* " constants */"
34 .BR "#include <sys/syscall.h>" " /* Definition of " SYS_* " constants */"
35 .B #include <unistd.h>
37 .BI "int syscall(SYS_perf_event_open, struct perf_event_attr *" attr ,
38 .BI " pid_t " pid ", int " cpu ", int " group_fd \
39 ", unsigned long " flags );
43 glibc provides no wrapper for
44 .BR perf_event_open (),
45 necessitating the use of
48 Given a list of parameters,
49 .BR perf_event_open ()
50 returns a file descriptor, for use in subsequent system calls
51 .RB ( read "(2), " mmap "(2), " prctl "(2), " fcntl "(2), etc.)."
54 .BR perf_event_open ()
55 creates a file descriptor that allows measuring performance
57 Each file descriptor corresponds to one
58 event that is measured; these can be grouped together
59 to measure multiple events simultaneously.
61 Events can be enabled and disabled in two ways: via
65 When an event is disabled it does not count or generate overflows but does
66 continue to exist and maintain its count value.
68 Events come in two flavors: counting and sampled.
71 event is one that is used for counting the aggregate number of events
73 In general, counting event results are gathered with a
78 event periodically writes measurements to a buffer that can then
86 arguments allow specifying which process and CPU to monitor:
88 .BR "pid == 0" " and " "cpu == \-1"
89 This measures the calling process/thread on any CPU.
91 .BR "pid == 0" " and " "cpu >= 0"
92 This measures the calling process/thread only
93 when running on the specified CPU.
95 .BR "pid > 0" " and " "cpu == \-1"
96 This measures the specified process/thread on any CPU.
98 .BR "pid > 0" " and " "cpu >= 0"
99 This measures the specified process/thread only
100 when running on the specified CPU.
102 .BR "pid == \-1" " and " "cpu >= 0"
103 This measures all processes/threads on the specified CPU.
109 .I /proc/sys/kernel/perf_event_paranoid
110 value of less than 1.
112 .BR "pid == \-1" " and " "cpu == \-1"
113 This setting is invalid and will return an error.
117 is greater than zero, permission to perform this system call
120 (since Linux 5.9) and a ptrace access mode
121 .B PTRACE_MODE_READ_REALCREDS
122 check on older Linux versions; see
127 argument allows event groups to be created.
128 An event group has one event which is the group leader.
129 The leader is created first, with
130 .IR group_fd " = \-1."
131 The rest of the group members are created with subsequent
132 .BR perf_event_open ()
135 being set to the file descriptor of the group leader.
136 (A single event on its own is created with
137 .IR group_fd " = \-1"
138 and is considered to be a group with only 1 member.)
139 An event group is scheduled onto the CPU as a unit: it will
140 be put onto the CPU only if all of the events in the group can be put onto
142 This means that the values of the member events can be
143 meaningfully compared\(emadded, divided (to get ratios), and so on\(emwith each
144 other, since they have counted events for the same set of executed
149 argument is formed by ORing together zero or more of the following values:
151 .BR PERF_FLAG_FD_CLOEXEC " (since Linux 3.14)"
152 .\" commit a21b0b354d4ac39be691f51c53562e2c24443d9e
153 This flag enables the close-on-exec flag for the created
154 event file descriptor,
155 so that the file descriptor is automatically closed on
157 Setting the close-on-exec flags at creation time, rather than later with
159 avoids potential race conditions where the calling thread invokes
160 .BR perf_event_open ()
163 at the same time as another thread calls
168 .BR PERF_FLAG_FD_NO_GROUP
169 This flag tells the event to ignore the
171 parameter except for the purpose of setting up output redirection
173 .B PERF_FLAG_FD_OUTPUT
176 .BR PERF_FLAG_FD_OUTPUT " (broken since Linux 2.6.35)"
177 .\" commit ac9721f3f54b27a16c7e1afb2481e7ee95a70318
178 This flag re-routes the event's sampled output to instead
179 be included in the mmap buffer of the event specified by
182 .BR PERF_FLAG_PID_CGROUP " (since Linux 2.6.39)"
183 .\" commit e5d1367f17ba6a6fed5fd8b74e4d5720923e0c25
184 This flag activates per-container system-wide monitoring.
186 is an abstraction that isolates a set of resources for finer-grained
187 control (CPUs, memory, etc.).
188 In this mode, the event is measured
189 only if the thread running on the monitored CPU belongs to the designated
191 The cgroup is identified by passing a file descriptor
192 opened on its directory in the cgroupfs filesystem.
194 cgroup to monitor is called
196 then a file descriptor opened on
198 (assuming cgroupfs is mounted on
200 must be passed as the
203 cgroup monitoring is available only
204 for system-wide events and may therefore require extra permissions.
208 structure provides detailed configuration information
209 for the event being created.
213 struct perf_event_attr {
214 __u32 type; /* Type of event */
215 __u32 size; /* Size of attribute structure */
216 __u64 config; /* Type\-specific configuration */
219 __u64 sample_period; /* Period of sampling */
220 __u64 sample_freq; /* Frequency of sampling */
223 __u64 sample_type; /* Specifies values included in sample */
224 __u64 read_format; /* Specifies values returned in read */
226 __u64 disabled : 1, /* off by default */
227 inherit : 1, /* children inherit it */
228 pinned : 1, /* must always be on PMU */
229 exclusive : 1, /* only group on PMU */
230 exclude_user : 1, /* don\(aqt count user */
231 exclude_kernel : 1, /* don\(aqt count kernel */
232 exclude_hv : 1, /* don\(aqt count hypervisor */
233 exclude_idle : 1, /* don\(aqt count when idle */
234 mmap : 1, /* include mmap data */
235 comm : 1, /* include comm data */
236 freq : 1, /* use freq, not period */
237 inherit_stat : 1, /* per task counts */
238 enable_on_exec : 1, /* next exec enables */
239 task : 1, /* trace fork/exit */
240 watermark : 1, /* wakeup_watermark */
241 precise_ip : 2, /* skid constraint */
242 mmap_data : 1, /* non\-exec mmap data */
243 sample_id_all : 1, /* sample_type all events */
244 exclude_host : 1, /* don\(aqt count in host */
245 exclude_guest : 1, /* don\(aqt count in guest */
246 exclude_callchain_kernel : 1,
247 /* exclude kernel callchains */
248 exclude_callchain_user : 1,
249 /* exclude user callchains */
250 mmap2 : 1, /* include mmap with inode data */
251 comm_exec : 1, /* flag comm events that are
253 use_clockid : 1, /* use clockid for time fields */
254 context_switch : 1, /* context switch data */
255 write_backward : 1, /* Write ring buffer from end
257 namespaces : 1, /* include namespaces data */
258 ksymbol : 1, /* include ksymbol events */
259 bpf_event : 1, /* include bpf events */
260 aux_output : 1, /* generate AUX records
262 cgroup : 1, /* include cgroup events */
263 text_poke : 1, /* include text poke events */
268 __u32 wakeup_events; /* wakeup every n events */
269 __u32 wakeup_watermark; /* bytes before wakeup */
272 __u32 bp_type; /* breakpoint type */
275 __u64 bp_addr; /* breakpoint address */
276 __u64 kprobe_func; /* for perf_kprobe */
277 __u64 uprobe_path; /* for perf_uprobe */
278 __u64 config1; /* extension of config */
282 __u64 bp_len; /* breakpoint length */
283 __u64 kprobe_addr; /* with kprobe_func == NULL */
284 __u64 probe_offset; /* for perf_[k,u]probe */
285 __u64 config2; /* extension of config1 */
287 __u64 branch_sample_type; /* enum perf_branch_sample_type */
288 __u64 sample_regs_user; /* user regs to dump on samples */
289 __u32 sample_stack_user; /* size of stack to dump on
291 __s32 clockid; /* clock to use for time fields */
292 __u64 sample_regs_intr; /* regs to dump on samples */
293 __u32 aux_watermark; /* aux bytes before wakeup */
294 __u16 sample_max_stack; /* max frames in callchain */
295 __u16 __reserved_2; /* align to u64 */
303 structure are described in more detail below:
306 This field specifies the overall event type.
307 It has one of the following values:
310 .B PERF_TYPE_HARDWARE
311 This indicates one of the "generalized" hardware events provided
315 field definition for more details.
317 .B PERF_TYPE_SOFTWARE
318 This indicates one of the software-defined events provided by the kernel
319 (even if no hardware support is available).
321 .B PERF_TYPE_TRACEPOINT
322 This indicates a tracepoint
323 provided by the kernel tracepoint infrastructure.
325 .B PERF_TYPE_HW_CACHE
326 This indicates a hardware cache event.
327 This has a special encoding, described in the
332 This indicates a "raw" implementation-specific event in the
335 .BR PERF_TYPE_BREAKPOINT " (since Linux 2.6.33)"
336 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
337 This indicates a hardware breakpoint as provided by the CPU.
338 Breakpoints can be read/write accesses to an address as well as
339 execution of an instruction address.
343 .\" commit 2e80a82a49c4c7eca4e35734380f28298ba5db19
344 .BR perf_event_open ()
345 can support multiple PMUs.
346 To enable this, a value exported by the kernel can be used in the
348 field to indicate which PMU to use.
349 The value to use can be found in the sysfs filesystem:
350 there is a subdirectory per PMU instance under
351 .IR /sys/bus/event_source/devices .
352 In each subdirectory there is a
354 file whose content is an integer that can be used in the
358 .I /sys/bus/event_source/devices/cpu/type
359 contains the value for the core CPU PMU, which is usually 4.
361 .BR kprobe " and " uprobe " (since Linux 4.17)"
362 .\" commit 65074d43fc77bcae32776724b7fa2696923c78e4
363 .\" commit e12f03d7031a977356e3d7b75a68c2185ff8d155
364 .\" commit 33ea4b24277b06dbc55d7f5772a46f029600255e
365 These two dynamic PMUs create a kprobe/uprobe and attach it to the
366 file descriptor generated by perf_event_open.
367 The kprobe/uprobe will be destroyed on the destruction of the file descriptor.
380 structure for forward/backward compatibility.
382 .I sizeof(struct perf_event_attr)
383 to allow the kernel to see
384 the struct size at the time of compilation.
387 .B PERF_ATTR_SIZE_VER0
388 is set to 64; this was the size of the first published struct.
389 .B PERF_ATTR_SIZE_VER1
390 is 72, corresponding to the addition of breakpoints in Linux 2.6.33.
391 .\" commit cb5d76999029ae7a517cb07dfa732c1b5a934fc2
392 .\" this was added much later when PERF_ATTR_SIZE_VER2 happened
393 .\" but the actual attr_size had increased in 2.6.33
394 .B PERF_ATTR_SIZE_VER2
395 is 80 corresponding to the addition of branch sampling in Linux 3.4.
396 .\" commit cb5d76999029ae7a517cb07dfa732c1b5a934fc2
397 .B PERF_ATTR_SIZE_VER3
398 is 96 corresponding to the addition
404 .\" commit 1659d129ed014b715b0b2120e6fd929bdd33ed03
405 .B PERF_ATTR_SIZE_VER4
406 is 104 corresponding to the addition of
409 .\" commit 60e2364e60e86e81bc6377f49779779e6120977f
410 .B PERF_ATTR_SIZE_VER5
411 is 112 corresponding to the addition of
414 .\" commit 1a5941312414c71dece6717da9a0fa1303127afa
417 This specifies which event you want, in conjunction with
425 fields are also taken into account in cases where 64 bits is not
426 enough to fully specify the event.
427 The encoding of these fields are event dependent.
429 There are various ways to set the
431 field that are dependent on the value of the previously
435 What follows are various possible settings for
443 .BR PERF_TYPE_HARDWARE ,
444 we are measuring one of the generalized hardware CPU events.
445 Not all of these are available on all platforms.
448 to one of the following:
451 .B PERF_COUNT_HW_CPU_CYCLES
453 Be wary of what happens during CPU frequency scaling.
455 .B PERF_COUNT_HW_INSTRUCTIONS
456 Retired instructions.
457 Be careful, these can be affected by various
458 issues, most notably hardware interrupt counts.
460 .B PERF_COUNT_HW_CACHE_REFERENCES
462 Usually this indicates Last Level Cache accesses but this may
463 vary depending on your CPU.
464 This may include prefetches and coherency messages; again this
465 depends on the design of your CPU.
467 .B PERF_COUNT_HW_CACHE_MISSES
469 Usually this indicates Last Level Cache misses; this is intended to be
470 used in conjunction with the
471 .B PERF_COUNT_HW_CACHE_REFERENCES
472 event to calculate cache miss rates.
474 .B PERF_COUNT_HW_BRANCH_INSTRUCTIONS
475 Retired branch instructions.
476 Prior to Linux 2.6.35, this used
477 the wrong event on AMD processors.
478 .\" commit f287d332ce835f77a4f5077d2c0ef1e3f9ea42d2
480 .B PERF_COUNT_HW_BRANCH_MISSES
481 Mispredicted branch instructions.
483 .B PERF_COUNT_HW_BUS_CYCLES
484 Bus cycles, which can be different from total cycles.
486 .BR PERF_COUNT_HW_STALLED_CYCLES_FRONTEND " (since Linux 3.0)"
487 .\" commit 8f62242246351b5a4bc0c1f00c0c7003edea128a
488 Stalled cycles during issue.
490 .BR PERF_COUNT_HW_STALLED_CYCLES_BACKEND " (since Linux 3.0)"
491 .\" commit 8f62242246351b5a4bc0c1f00c0c7003edea128a
492 Stalled cycles during retirement.
494 .BR PERF_COUNT_HW_REF_CPU_CYCLES " (since Linux 3.3)"
495 .\" commit c37e17497e01fc0f5d2d6feb5723b210b3ab8890
496 Total cycles; not affected by CPU frequency scaling.
502 .BR PERF_TYPE_SOFTWARE ,
503 we are measuring software events provided by the kernel.
506 to one of the following:
509 .B PERF_COUNT_SW_CPU_CLOCK
510 This reports the CPU clock, a high-resolution per-CPU timer.
512 .B PERF_COUNT_SW_TASK_CLOCK
513 This reports a clock count specific to the task that is running.
515 .B PERF_COUNT_SW_PAGE_FAULTS
516 This reports the number of page faults.
518 .B PERF_COUNT_SW_CONTEXT_SWITCHES
519 This counts context switches.
520 Until Linux 2.6.34, these were all reported as user-space
521 events, after that they are reported as happening in the kernel.
522 .\" commit e49a5bd38159dfb1928fd25b173bc9de4bbadb21
524 .B PERF_COUNT_SW_CPU_MIGRATIONS
525 This reports the number of times the process
526 has migrated to a new CPU.
528 .B PERF_COUNT_SW_PAGE_FAULTS_MIN
529 This counts the number of minor page faults.
530 These did not require disk I/O to handle.
532 .B PERF_COUNT_SW_PAGE_FAULTS_MAJ
533 This counts the number of major page faults.
534 These required disk I/O to handle.
536 .BR PERF_COUNT_SW_ALIGNMENT_FAULTS " (since Linux 2.6.33)"
537 .\" commit f7d7986060b2890fc26db6ab5203efbd33aa2497
538 This counts the number of alignment faults.
539 These happen when unaligned memory accesses happen; the kernel
540 can handle these but it reduces performance.
541 This happens only on some architectures (never on x86).
543 .BR PERF_COUNT_SW_EMULATION_FAULTS " (since Linux 2.6.33)"
544 .\" commit f7d7986060b2890fc26db6ab5203efbd33aa2497
545 This counts the number of emulation faults.
546 The kernel sometimes traps on unimplemented instructions
547 and emulates them for user space.
548 This can negatively impact performance.
550 .BR PERF_COUNT_SW_DUMMY " (since Linux 3.12)"
551 .\" commit fa0097ee690693006ab1aea6c01ad3c851b65c77
552 This is a placeholder event that counts nothing.
553 Informational sample record types such as mmap or comm
554 must be associated with an active event.
555 This dummy event allows gathering such records without requiring
563 .BR PERF_TYPE_TRACEPOINT ,
564 then we are measuring kernel tracepoints.
567 can be obtained from under debugfs
568 .I tracing/events/*/*/id
569 if ftrace is enabled in the kernel.
576 .BR PERF_TYPE_HW_CACHE ,
577 then we are measuring a hardware CPU cache event.
578 To calculate the appropriate
580 value, use the following equation:
585 config = (perf_hw_cache_id) |
586 (perf_hw_cache_op_id << 8) |
587 (perf_hw_cache_op_result_id << 16);
596 .B PERF_COUNT_HW_CACHE_L1D
597 for measuring Level 1 Data Cache
599 .B PERF_COUNT_HW_CACHE_L1I
600 for measuring Level 1 Instruction Cache
602 .B PERF_COUNT_HW_CACHE_LL
603 for measuring Last-Level Cache
605 .B PERF_COUNT_HW_CACHE_DTLB
606 for measuring the Data TLB
608 .B PERF_COUNT_HW_CACHE_ITLB
609 for measuring the Instruction TLB
611 .B PERF_COUNT_HW_CACHE_BPU
612 for measuring the branch prediction unit
614 .BR PERF_COUNT_HW_CACHE_NODE " (since Linux 3.1)"
615 .\" commit 89d6c0b5bdbb1927775584dcf532d98b3efe1477
616 for measuring local memory accesses
620 .I perf_hw_cache_op_id
624 .B PERF_COUNT_HW_CACHE_OP_READ
627 .B PERF_COUNT_HW_CACHE_OP_WRITE
630 .B PERF_COUNT_HW_CACHE_OP_PREFETCH
631 for prefetch accesses
635 .I perf_hw_cache_op_result_id
639 .B PERF_COUNT_HW_CACHE_RESULT_ACCESS
642 .B PERF_COUNT_HW_CACHE_RESULT_MISS
654 Most CPUs support events that are not covered by the "generalized" events.
655 These are implementation defined; see your CPU manual (for example
656 the Intel Volume 3B documentation or the AMD BIOS and Kernel Developer
658 The libpfm4 library can be used to translate from the name in the
659 architectural manuals to the raw hex value
660 .BR perf_event_open ()
661 expects in this field.
666 .BR PERF_TYPE_BREAKPOINT ,
670 Its parameters are set in other places.
683 .IR /sys/bus/event_source/devices/[k,u]probe/format/retprobe )
684 for kretprobe/uretprobe.
694 .IR kprobe_func ", " uprobe_path ", " kprobe_addr ", and " probe_offset
695 These fields describe the kprobe/uprobe for dynamic PMUs
717 .IR sample_period ", " sample_freq
718 A "sampling" event is one that generates an overflow notification
719 every N events, where N is given by
722 .IR sample_period " > 0."
723 When an overflow occurs, requested data is recorded
727 field controls what data is recorded on each overflow.
730 can be used if you wish to use frequency rather than period.
731 In this case, you set the
734 The kernel will adjust the sampling period
735 to try and achieve the desired rate.
736 The rate of adjustment is a
740 The various bits in this field specify which values to include
742 They will be recorded in a ring-buffer,
743 which is available to user space using
745 The order in which the values are saved in the
746 sample are documented in the MMAP Layout subsection below;
748 .I "enum perf_event_sample_format"
753 Records instruction pointer.
756 Records the process and thread IDs.
762 Records an address, if applicable.
765 Record counter values for all events in a group, not just the group leader.
767 .B PERF_SAMPLE_CALLCHAIN
768 Records the callchain (stack backtrace).
771 Records a unique ID for the opened event's group leader.
776 .B PERF_SAMPLE_PERIOD
777 Records the current sampling period.
779 .B PERF_SAMPLE_STREAM_ID
780 Records a unique ID for the opened event.
783 the actual ID is returned, not the group leader.
784 This ID is the same as the one returned by
788 Records additional data, if applicable.
789 Usually returned by tracepoint events.
791 .BR PERF_SAMPLE_BRANCH_STACK " (since Linux 3.4)"
792 .\" commit bce38cd53e5ddba9cb6d708c4ef3d04a4016ec7e
793 This provides a record of recent branches, as provided
794 by CPU branch sampling hardware (such as Intel Last Branch Record).
795 Not all hardware supports this feature.
798 .I branch_sample_type
799 field for how to filter which branches are reported.
801 .BR PERF_SAMPLE_REGS_USER " (since Linux 3.7)"
802 .\" commit 4018994f3d8785275ef0e7391b75c3462c029e56
803 Records the current user-level CPU register state
804 (the values in the process before the kernel was called).
806 .BR PERF_SAMPLE_STACK_USER " (since Linux 3.7)"
807 .\" commit c5ebcedb566ef17bda7b02686e0d658a7bb42ee7
808 Records the user level stack, allowing stack unwinding.
810 .BR PERF_SAMPLE_WEIGHT " (since Linux 3.10)"
811 .\" commit c3feedf2aaf9ac8bad6f19f5d21e4ee0b4b87e9c
812 Records a hardware provided weight value that expresses how
813 costly the sampled event was.
814 This allows the hardware to highlight expensive events in
817 .BR PERF_SAMPLE_DATA_SRC " (since Linux 3.10)"
818 .\" commit d6be9ad6c960f43800a6f118932bc8a5a4eadcd1
819 Records the data source: where in the memory hierarchy
820 the data associated with the sampled instruction came from.
821 This is available only if the underlying hardware
822 supports this feature.
824 .BR PERF_SAMPLE_IDENTIFIER " (since Linux 3.12)"
825 .\" commit ff3d527cebc1fa3707c617bfe9e74f53fcfb0955
828 value in a fixed position in the record,
829 either at the beginning (for sample events) or at the end
830 (if a non-sample event).
832 This was necessary because a sample stream may have
833 records from various different event sources with different
836 Parsing the event stream properly was not possible because the
837 format of the record was needed to find
840 the format could not be found without knowing what
841 event the sample belonged to (causing a circular
845 .B PERF_SAMPLE_IDENTIFIER
846 setting makes the event stream always parsable
849 in a fixed location, even though
850 it means having duplicate
854 .BR PERF_SAMPLE_TRANSACTION " (since Linux 3.13)"
855 .\" commit fdfbbd07e91f8fe387140776f3fd94605f0c89e5
856 Records reasons for transactional memory abort events
857 (for example, from Intel TSX transactional memory support).
861 setting must be greater than 0 and a transactional memory abort
862 event must be measured or no values will be recorded.
863 Also note that some perf_event measurements, such as sampled
864 cycle counting, may cause extraneous aborts (by causing an
865 interrupt during a transaction).
867 .BR PERF_SAMPLE_REGS_INTR " (since Linux 3.19)"
868 .\" commit 60e2364e60e86e81bc6377f49779779e6120977f
869 Records a subset of the current CPU register state
871 .IR sample_regs_intr .
873 .B PERF_SAMPLE_REGS_USER
874 the register values will return kernel register
875 state if the overflow happened while kernel
877 If the CPU supports hardware sampling of
878 register state (i.e., PEBS on Intel x86) and
880 is set higher than zero then the register
881 values returned are those captured by
882 hardware at the time of the sampled
883 instruction's retirement.
885 .BR PERF_SAMPLE_PHYS_ADDR " (since Linux 4.13)"
886 .\" commit fc7ce9c74c3ad232b084d80148654f926d01ece7
887 Records physical address of data like in
888 .BR PERF_SAMPLE_ADDR .
890 .BR PERF_SAMPLE_CGROUP " (since Linux 5.7)"
891 .\" commit 96aaab686505c449e24d76e76507290dcc30e008
892 Records (perf_event) cgroup ID of the process.
893 This corresponds to the
896 .B PERF_RECORD_CGROUP
901 This field specifies the format of the data returned by
904 .BR perf_event_open ()
908 .B PERF_FORMAT_TOTAL_TIME_ENABLED
912 This can be used to calculate estimated totals if
913 the PMU is overcommitted and multiplexing is happening.
915 .B PERF_FORMAT_TOTAL_TIME_RUNNING
919 This can be used to calculate estimated totals if
920 the PMU is overcommitted and multiplexing is happening.
923 Adds a 64-bit unique value that corresponds to the event group.
926 Allows all counter values in an event group to be read with one read.
932 bit specifies whether the counter starts out disabled or enabled.
933 If disabled, the event can later be enabled by
939 When creating an event group, typically the group leader is initialized
942 set to 1 and any child events are initialized with
947 being 0, the child events will not start until the group leader
953 bit specifies that this counter should count events of child
954 tasks as well as the task specified.
955 This applies only to new children, not to any existing children at
956 the time the counter is created (nor to any new children of
959 Inherit does not work for some combinations of
962 .BR PERF_FORMAT_GROUP .
967 bit specifies that the counter should always be on the CPU if at all
969 It applies only to hardware counters and only to group leaders.
970 If a pinned counter cannot be put onto the CPU (e.g., because there are
971 not enough hardware counters or because of a conflict with some other
972 event), then the counter goes into an 'error' state, where reads
973 return end-of-file (i.e.,
975 returns 0) until the counter is subsequently enabled or disabled.
980 bit specifies that when this counter's group is on the CPU,
981 it should be the only group using the CPU's counters.
982 In the future this may allow monitoring programs to
983 support PMU features that need to run alone so that they do not
984 disrupt other hardware counters.
986 Note that many unexpected situations may prevent events with the
988 bit set from ever running.
989 This includes any users running a system-wide
990 measurement as well as any kernel use of the performance counters
991 (including the commonly enabled NMI Watchdog Timer interface).
994 If this bit is set, the count excludes events that happen in user space.
997 If this bit is set, the count excludes events that happen in kernel space.
1000 If this bit is set, the count excludes events that happen in the
1002 This is mainly for PMUs that have built-in support for handling this
1004 Extra support is needed for handling hypervisor measurements on most
1008 If set, don't count when the CPU is running the idle task.
1009 While you can currently enable this for any event type, it is ignored
1010 for all but software events.
1015 bit enables generation of
1022 This allows tools to notice new executable code being mapped into
1023 a program (dynamic shared libraries for example)
1024 so that addresses can be mapped back to the original code.
1029 bit enables tracking of process command name as modified by the
1032 .BR prctl (PR_SET_NAME)
1033 system calls as well as writing to
1034 .IR /proc/self/comm .
1037 flag is also successfully set (possible since Linux 3.16),
1038 .\" commit 82b897782d10fcc4930c9d4a15b175348fdd2871
1040 .B PERF_RECORD_MISC_COMM_EXEC
1041 can be used to differentiate the
1043 case from the others.
1046 If this bit is set, then
1050 is used when setting up the sampling interval.
1053 This bit enables saving of event counts on context switch for
1055 This is meaningful only if the
1060 If this bit is set, a counter is automatically
1061 enabled after a call to
1065 If this bit is set, then
1066 fork/exit notifications are included in the ring buffer.
1069 If set, have an overflow notification happen when we cross the
1072 Otherwise, overflow notifications happen after
1076 .IR precise_ip " (since Linux 2.6.35)"
1077 .\" commit ab608344bcbde4f55ec4cd911b686b0ce3eae076
1078 This controls the amount of skid.
1079 Skid is how many instructions
1080 execute between an event of interest happening and the kernel
1081 being able to stop and record the event.
1083 better and allows more accurate reporting of which events
1084 correspond to which instructions, but hardware is often limited
1085 with how small this can be.
1087 The possible values of this field are the following:
1091 can have arbitrary skid.
1094 must have constant skid.
1097 requested to have 0 skid.
1101 See also the description of
1102 .BR PERF_RECORD_MISC_EXACT_IP .
1105 .IR mmap_data " (since Linux 2.6.36)"
1106 .\" commit 3af9e859281bda7eb7c20b51879cf43aa788ac2e
1107 This is the counterpart of the
1110 This enables generation of
1114 calls that do not have
1116 set (for example data and SysV shared memory).
1118 .IR sample_id_all " (since Linux 2.6.38)"
1119 .\" commit c980d1091810df13f21aabbce545fd98f545bbf7
1120 If set, then TID, TIME, ID, STREAM_ID, and CPU can
1121 additionally be included in
1122 .RB non- PERF_RECORD_SAMPLE s
1123 if the corresponding
1128 .B PERF_SAMPLE_IDENTIFIER
1129 is specified, then an additional ID value is included
1130 as the last value to ease parsing the record stream.
1131 This may lead to the
1133 value appearing twice.
1135 The layout is described by this pseudo-structure:
1140 { u32 pid, tid; } /* if PERF_SAMPLE_TID set */
1141 { u64 time; } /* if PERF_SAMPLE_TIME set */
1142 { u64 id; } /* if PERF_SAMPLE_ID set */
1143 { u64 stream_id;} /* if PERF_SAMPLE_STREAM_ID set */
1144 { u32 cpu, res; } /* if PERF_SAMPLE_CPU set */
1145 { u64 id; } /* if PERF_SAMPLE_IDENTIFIER set */
1150 .IR exclude_host " (since Linux 3.2)"
1151 .\" commit a240f76165e6255384d4bdb8139895fac7988799
1152 When conducting measurements that include processes running
1153 VM instances (i.e., have executed a
1156 only measure events happening inside a guest instance.
1157 This is only meaningful outside the guests; this setting does
1158 not change counts gathered inside of a guest.
1159 Currently, this functionality is x86 only.
1161 .IR exclude_guest " (since Linux 3.2)"
1162 .\" commit a240f76165e6255384d4bdb8139895fac7988799
1163 When conducting measurements that include processes running
1164 VM instances (i.e., have executed a
1167 do not measure events happening inside guest instances.
1168 This is only meaningful outside the guests; this setting does
1169 not change counts gathered inside of a guest.
1170 Currently, this functionality is x86 only.
1172 .IR exclude_callchain_kernel " (since Linux 3.7)"
1173 .\" commit d077526485d5c9b12fe85d0b2b3b7041e6bc5f91
1174 Do not include kernel callchains.
1176 .IR exclude_callchain_user " (since Linux 3.7)"
1177 .\" commit d077526485d5c9b12fe85d0b2b3b7041e6bc5f91
1178 Do not include user callchains.
1180 .IR mmap2 " (since Linux 3.16)"
1181 .\" commit 13d7a2410fa637f450a29ecb515ac318ee40c741
1182 .\" This is tricky; was committed during 3.12 development
1183 .\" but right before release was disabled.
1184 .\" So while you could select mmap2 starting with 3.12
1185 .\" it did not work until 3.16
1186 .\" commit a5a5ba72843dd05f991184d6cb9a4471acce1005
1187 Generate an extended executable mmap record that contains enough
1188 additional information to uniquely identify shared mappings.
1191 flag must also be set for this to work.
1193 .IR comm_exec " (since Linux 3.16)"
1194 .\" commit 82b897782d10fcc4930c9d4a15b175348fdd2871
1195 This is purely a feature-detection flag, it does not change
1197 If this flag can successfully be set, then, when
1200 .B PERF_RECORD_MISC_COMM_EXEC
1201 flag will be set in the
1203 field of a comm record header if the rename event being
1204 reported was caused by a call to
1206 This allows tools to distinguish between the various
1207 types of process renaming.
1209 .IR use_clockid " (since Linux 4.1)"
1210 .\" commit 34f439278cef7b1177f8ce24f9fc81dfc6221d3b
1211 This allows selecting which internal Linux clock to use
1212 when generating timestamps via the
1215 This can make it easier to correlate perf sample times with
1216 timestamps generated by other tools.
1218 .IR context_switch " (since Linux 4.3)"
1219 .\" commit 45ac1403f564f411c6a383a2448688ba8dd705a4
1220 This enables the generation of
1221 .B PERF_RECORD_SWITCH
1222 records when a context switch occurs.
1223 It also enables the generation of
1224 .B PERF_RECORD_SWITCH_CPU_WIDE
1225 records when sampling in CPU-wide mode.
1226 This functionality is in addition to existing tracepoint and
1227 software events for measuring context switches.
1228 The advantage of this method is that it will give full
1229 information even with strict
1230 .I perf_event_paranoid
1233 .IR write_backward " (since Linux 4.6)"
1234 .\" commit 9ecda41acb971ebd07c8fb35faf24005c0baea12
1235 This causes the ring buffer to be written from the end to the beginning.
1236 This is to support reading from overwritable ring buffer.
1238 .IR namespaces " (since Linux 4.11)"
1239 .\" commit e422267322cd319e2695a535e47c5b1feeac45eb
1240 This enables the generation of
1241 .B PERF_RECORD_NAMESPACES
1242 records when a task enters a new namespace.
1243 Each namespace has a combination of device and inode numbers.
1245 .IR ksymbol " (since Linux 5.0)"
1246 .\" commit 76193a94522f1d4edf2447a536f3f796ce56343b
1247 This enables the generation of
1248 .B PERF_RECORD_KSYMBOL
1249 records when new kernel symbols are registered or unregistered.
1250 This is analyzing dynamic kernel functions like eBPF.
1252 .IR bpf_event " (since Linux 5.0)"
1253 .\" commit 6ee52e2a3fe4ea35520720736e6791df1fb67106
1254 This enables the generation of
1255 .B PERF_RECORD_BPF_EVENT
1256 records when an eBPF program is loaded or unloaded.
1258 .IR auxevent " (since Linux 5.4)"
1259 .\" commit ab43762ef010967e4ccd53627f70a2eecbeafefb
1260 This allows normal (non-AUX) events to generate data for AUX events
1261 if the hardware supports it.
1263 .IR cgroup " (since Linux 5.7)"
1264 .\" commit 96aaab686505c449e24d76e76507290dcc30e008
1265 This enables the generation of
1266 .B PERF_RECORD_CGROUP
1267 records when a new cgroup is created (and activated).
1269 .IR text_poke " (since Linux 5.8)"
1270 .\" commit e17d43b93e544f5016c0251d2074c15568d5d963
1271 This enables the generation of
1272 .B PERF_RECORD_TEXT_POKE
1273 records when there's a change to the kernel text
1274 (i.e., self-modifying code).
1276 .IR wakeup_events ", " wakeup_watermark
1277 This union sets how many samples
1278 .RI ( wakeup_events )
1280 .RI ( wakeup_watermark )
1281 happen before an overflow notification happens.
1282 Which one is used is selected by the
1288 .B PERF_RECORD_SAMPLE
1290 To receive overflow notification for all
1292 types choose watermark and set
1296 Prior to Linux 3.0, setting
1297 .\" commit f506b3dc0ec454a16d40cab9ee5d75435b39dc50
1299 to 0 resulted in no overflow notifications;
1300 more recent kernels treat 0 the same as 1.
1302 .IR bp_type " (since Linux 2.6.33)"
1303 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
1304 This chooses the breakpoint type.
1308 .B HW_BREAKPOINT_EMPTY
1312 Count when we read the memory location.
1315 Count when we write the memory location.
1318 Count when we read or write the memory location.
1321 Count when we execute code at the memory location.
1323 The values can be combined via a bitwise or, but the
1333 .IR bp_addr " (since Linux 2.6.33)"
1334 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
1335 This is the address of the breakpoint.
1336 For execution breakpoints, this is the memory address of the instruction
1337 of interest; for read and write breakpoints, it is the memory address
1338 of the memory location of interest.
1340 .IR config1 " (since Linux 2.6.39)"
1341 .\" commit a7e3ed1e470116c9d12c2f778431a481a6be8ab6
1343 is used for setting events that need an extra register or otherwise
1344 do not fit in the regular config field.
1345 Raw OFFCORE_EVENTS on Nehalem/Westmere/SandyBridge use this field
1346 on Linux 3.3 and later kernels.
1348 .IR bp_len " (since Linux 2.6.33)"
1349 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
1351 is the length of the breakpoint being measured if
1354 .BR PERF_TYPE_BREAKPOINT .
1356 .BR HW_BREAKPOINT_LEN_1 ,
1357 .BR HW_BREAKPOINT_LEN_2 ,
1358 .BR HW_BREAKPOINT_LEN_4 ,
1360 .BR HW_BREAKPOINT_LEN_8 .
1361 For an execution breakpoint, set this to
1364 .IR config2 " (since Linux 2.6.39)"
1365 .\" commit a7e3ed1e470116c9d12c2f778431a481a6be8ab6
1367 is a further extension of the
1371 .IR branch_sample_type " (since Linux 3.4)"
1372 .\" commit bce38cd53e5ddba9cb6d708c4ef3d04a4016ec7e
1374 .B PERF_SAMPLE_BRANCH_STACK
1375 is enabled, then this specifies what branches to include
1376 in the branch record.
1378 The first part of the value is the privilege level, which
1379 is a combination of one of the values listed below.
1380 If the user does not set privilege level explicitly, the kernel
1381 will use the event's privilege level.
1382 Event and branch privilege levels do not have to match.
1385 .B PERF_SAMPLE_BRANCH_USER
1386 Branch target is in user space.
1388 .B PERF_SAMPLE_BRANCH_KERNEL
1389 Branch target is in kernel space.
1391 .B PERF_SAMPLE_BRANCH_HV
1392 Branch target is in hypervisor.
1394 .B PERF_SAMPLE_BRANCH_PLM_ALL
1395 A convenience value that is the three preceding values ORed together.
1397 In addition to the privilege value, at least one or more of the
1398 following bits must be set.
1400 .B PERF_SAMPLE_BRANCH_ANY
1403 .B PERF_SAMPLE_BRANCH_ANY_CALL
1404 Any call branch (includes direct calls, indirect calls, and far jumps).
1406 .B PERF_SAMPLE_BRANCH_IND_CALL
1409 .BR PERF_SAMPLE_BRANCH_CALL " (since Linux 4.4)"
1410 .\" commit c229bf9dc179d2023e185c0f705bdf68484c1e73
1413 .B PERF_SAMPLE_BRANCH_ANY_RETURN
1416 .BR PERF_SAMPLE_BRANCH_IND_JUMP " (since Linux 4.2)"
1417 .\" commit c9fdfa14c3792c0160849c484e83aa57afd80ccc
1420 .BR PERF_SAMPLE_BRANCH_COND " (since Linux 3.16)"
1421 .\" commit bac52139f0b7ab31330e98fd87fc5a2664951050
1422 Conditional branches.
1424 .BR PERF_SAMPLE_BRANCH_ABORT_TX " (since Linux 3.11)"
1425 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
1426 Transactional memory aborts.
1428 .BR PERF_SAMPLE_BRANCH_IN_TX " (since Linux 3.11)"
1429 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
1430 Branch in transactional memory transaction.
1432 .BR PERF_SAMPLE_BRANCH_NO_TX " (since Linux 3.11)"
1433 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
1434 Branch not in transactional memory transaction.
1435 .BR PERF_SAMPLE_BRANCH_CALL_STACK " (since Linux 4.1)"
1436 .\" commit 2c44b1936bb3b135a3fac8b3493394d42e51cf70
1437 Branch is part of a hardware-generated call stack.
1438 This requires hardware support, currently only found
1439 on Intel x86 Haswell or newer.
1442 .IR sample_regs_user " (since Linux 3.7)"
1443 .\" commit 4018994f3d8785275ef0e7391b75c3462c029e56
1444 This bit mask defines the set of user CPU registers to dump on samples.
1445 The layout of the register mask is architecture-specific and
1446 is described in the kernel header file
1447 .IR arch/ARCH/include/uapi/asm/perf_regs.h .
1449 .IR sample_stack_user " (since Linux 3.7)"
1450 .\" commit c5ebcedb566ef17bda7b02686e0d658a7bb42ee7
1451 This defines the size of the user stack to dump if
1452 .B PERF_SAMPLE_STACK_USER
1455 .IR clockid " (since Linux 4.1)"
1456 .\" commit 34f439278cef7b1177f8ce24f9fc81dfc6221d3b
1459 is set, then this field selects which internal Linux timer to
1461 The available timers are defined in
1464 .BR CLOCK_MONOTONIC ,
1465 .BR CLOCK_MONOTONIC_RAW ,
1466 .BR CLOCK_REALTIME ,
1467 .BR CLOCK_BOOTTIME ,
1470 currently supported.
1472 .IR aux_watermark " (since Linux 4.1)"
1473 .\" commit 1a5941312414c71dece6717da9a0fa1303127afa
1474 This specifies how much data is required to trigger a
1478 .IR sample_max_stack " (since Linux 4.8)"
1479 .\" commit 97c79a38cd454602645f0470ffb444b3b75ce574
1483 .BR PERF_SAMPLE_CALLCHAIN ,
1484 this field specifies how many stack frames to report when
1485 generating the callchain.
1488 .BR perf_event_open ()
1489 file descriptor has been opened, the values
1490 of the events can be read from the file descriptor.
1491 The values that are there are specified by the
1495 structure at open time.
1497 If you attempt to read into a buffer that is not big enough to hold the
1502 Here is the layout of the data returned by a read:
1505 .B PERF_FORMAT_GROUP
1506 was specified to allow reading all events in a group at once:
1510 struct read_format {
1511 u64 nr; /* The number of events */
1512 u64 time_enabled; /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
1513 u64 time_running; /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
1515 u64 value; /* The value of the event */
1516 u64 id; /* if PERF_FORMAT_ID */
1523 .B PERF_FORMAT_GROUP
1530 struct read_format {
1531 u64 value; /* The value of the event */
1532 u64 time_enabled; /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
1533 u64 time_running; /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
1534 u64 id; /* if PERF_FORMAT_ID */
1539 The values read are as follows:
1542 The number of events in this file descriptor.
1544 .B PERF_FORMAT_GROUP
1547 .IR time_enabled ", " time_running
1548 Total time the event was enabled and running.
1549 Normally these values are the same.
1550 Multiplexing happens if the number of events is more than the
1551 number of available PMU counter slots.
1552 In that case the events run only part of the time and the
1556 values can be used to scale an estimated value for the count.
1559 An unsigned 64-bit value containing the counter result.
1562 A globally unique value for this particular event; only present if
1568 .BR perf_event_open ()
1569 in sampled mode, asynchronous events
1570 (like counter overflow or
1573 are logged into a ring-buffer.
1574 This ring-buffer is created and accessed through
1577 The mmap size should be 1+2^n pages, where the first page is a
1579 .RI ( "struct perf_event_mmap_page" )
1580 that contains various
1581 bits of information such as where the ring-buffer head is.
1583 Before kernel 2.6.39, there is a bug that means you must allocate an mmap
1584 ring buffer when sampling even if you do not plan to access it.
1586 The structure of the first metadata mmap page is as follows:
1590 struct perf_event_mmap_page {
1591 __u32 version; /* version number of this structure */
1592 __u32 compat_version; /* lowest version this is compat with */
1593 __u32 lock; /* seqlock for synchronization */
1594 __u32 index; /* hardware counter identifier */
1595 __s64 offset; /* add to hardware counter value */
1596 __u64 time_enabled; /* time event active */
1597 __u64 time_running; /* time event on CPU */
1601 __u64 cap_usr_time / cap_usr_rdpmc / cap_bit0 : 1,
1602 cap_bit0_is_deprecated : 1,
1605 cap_user_time_zero : 1,
1612 __u64 __reserved[120]; /* Pad to 1 k */
1613 __u64 data_head; /* head in the data section */
1614 __u64 data_tail; /* user\-space written tail */
1615 __u64 data_offset; /* where the buffer starts */
1616 __u64 data_size; /* data buffer size */
1626 The following list describes the fields in the
1627 .I perf_event_mmap_page
1628 structure in more detail:
1631 Version number of this structure.
1634 The lowest version this is compatible with.
1637 A seqlock for synchronization.
1640 A unique hardware counter identifier.
1643 When using rdpmc for reads this offset value
1644 must be added to the one returned by rdpmc to get
1645 the current total event count.
1648 Time the event was active.
1651 Time the event was running.
1653 .IR cap_usr_time " / " cap_usr_rdpmc " / " cap_bit0 " (since Linux 3.4)"
1654 .\" commit c7206205d00ab375839bd6c7ddb247d600693c09
1655 There was a bug in the definition of
1659 from Linux 3.4 until Linux 3.11.
1660 Both bits were defined to point to the same location, so it was
1661 impossible to know if
1667 Starting with Linux 3.12, these are renamed to
1668 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1670 and you should use the
1676 .IR cap_bit0_is_deprecated " (since Linux 3.12)"
1677 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1678 If set, this bit indicates that the kernel supports
1679 the properly separated
1685 If not-set, it indicates an older kernel where
1689 map to the same bit and thus both features should
1690 be used with caution.
1692 .IR cap_user_rdpmc " (since Linux 3.12)"
1693 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1694 If the hardware supports user-space read of performance counters
1695 without syscall (this is the "rdpmc" instruction on x86), then
1696 the following code can be used to do a read:
1700 u32 seq, time_mult, time_shift, idx, width;
1701 u64 count, enabled, running;
1702 u64 cyc, time_offset;
1707 enabled = pc\->time_enabled;
1708 running = pc\->time_running;
1710 if (pc\->cap_usr_time && enabled != running) {
1712 time_offset = pc\->time_offset;
1713 time_mult = pc\->time_mult;
1714 time_shift = pc\->time_shift;
1718 count = pc\->offset;
1720 if (pc\->cap_usr_rdpmc && idx) {
1721 width = pc\->pmc_width;
1722 count += rdpmc(idx \- 1);
1726 } while (pc\->lock != seq);
1730 .IR cap_user_time " (since Linux 3.12)"
1731 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1732 This bit indicates the hardware has a constant, nonstop
1733 timestamp counter (TSC on x86).
1735 .IR cap_user_time_zero " (since Linux 3.12)"
1736 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1737 Indicates the presence of
1739 which allows mapping timestamp values to
1745 this field provides the bit-width of the value
1746 read using the rdpmc or equivalent instruction.
1747 This can be used to sign extend the result like:
1751 pmc <<= 64 \- pmc_width;
1752 pmc >>= 64 \- pmc_width; // signed shift right
1757 .IR time_shift ", " time_mult ", " time_offset
1761 these fields can be used to compute the time
1764 (in nanoseconds) using rdtsc or similar.
1771 quot = cyc >> time_shift;
1772 rem = cyc & (((u64)1 << time_shift) \- 1);
1773 delta = time_offset + quot * time_mult +
1774 ((rem * time_mult) >> time_shift);
1785 seqcount loop described above.
1786 This delta can then be added to
1787 enabled and possible running (if idx), improving the scaling:
1794 quot = count / running;
1795 rem = count % running;
1796 count = quot * enabled + (rem * enabled) / running;
1800 .IR time_zero " (since Linux 3.12)"
1801 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1804 .I cap_usr_time_zero
1805 is set, then the hardware clock (the TSC timestamp counter on x86)
1806 can be calculated from the
1815 time = timestamp \- time_zero;
1816 quot = time / time_mult;
1817 rem = time % time_mult;
1818 cyc = (quot << time_shift) + (rem << time_shift) / time_mult;
1826 quot = cyc >> time_shift;
1827 rem = cyc & (((u64)1 << time_shift) \- 1);
1828 timestamp = time_zero + quot * time_mult +
1829 ((rem * time_mult) >> time_shift);
1834 This points to the head of the data section.
1835 The value continuously increases, it does not wrap.
1836 The value needs to be manually wrapped by the size of the mmap buffer
1837 before accessing the samples.
1839 On SMP-capable platforms, after reading the
1842 user space should issue an rmb().
1849 value should be written by user space to reflect the last read data.
1850 In this case, the kernel will not overwrite unread data.
1852 .IR data_offset " (since Linux 4.1)"
1853 .\" commit e8c6deac69629c0cb97c3d3272f8631ef17f8f0f
1854 Contains the offset of the location in the mmap buffer
1855 where perf sample data begins.
1857 .IR data_size " (since Linux 4.1)"
1858 .\" commit e8c6deac69629c0cb97c3d3272f8631ef17f8f0f
1859 Contains the size of the perf sample region within
1862 .IR aux_head ", " aux_tail ", " aux_offset ", " aux_size " (since Linux 4.1)"
1863 .\" commit 45bfb2e50471abbbfd83d40d28c986078b0d24ff
1864 The AUX region allows
1866 a separate sample buffer for
1867 high-bandwidth data streams (separate from the main perf sample buffer).
1868 An example of a high-bandwidth stream is instruction tracing support,
1869 as is found in newer Intel processors.
1871 To set up an AUX area, first
1873 needs to be set with an offset greater than
1874 .IR data_offset + data_size
1877 needs to be set to the desired buffer size.
1878 The desired offset and size must be page aligned, and the size
1879 must be a power of two.
1880 These values are then passed to mmap in order to map the AUX buffer.
1881 Pages in the AUX buffer are included as part of the
1885 and also as part of the
1886 .I perf_event_mlock_kb
1889 By default, the AUX buffer will be truncated if it will not fit
1890 in the available space in the ring buffer.
1891 If the AUX buffer is mapped as a read only buffer, then it will
1892 operate in ring buffer mode where old data will be overwritten
1894 In overwrite mode, it might not be possible to infer where the
1895 new data began, and it is the consumer's job to disable
1896 measurement while reading to avoid possible data races.
1902 ring buffer pointers have the same behavior and ordering
1903 rules as the previous described
1908 The following 2^n ring-buffer pages have the layout described below.
1911 .I perf_event_attr.sample_id_all
1912 is set, then all event types will
1913 have the sample_type selected fields related to where/when (identity)
1914 an event took place (TID, TIME, ID, CPU, STREAM_ID) described in
1915 .B PERF_RECORD_SAMPLE
1916 below, it will be stashed just after the
1917 .I perf_event_header
1918 and the fields already present for the existing
1919 fields, that is, at the end of the payload.
1920 This allows a newer perf.data
1921 file to be supported by older perf tools, with the new optional
1922 fields being ignored.
1924 The mmap values start with a header:
1928 struct perf_event_header {
1936 Below, we describe the
1937 .I perf_event_header
1938 fields in more detail.
1939 For ease of reading,
1940 the fields with shorter descriptions are presented first.
1943 This indicates the size of the record.
1948 field contains additional information about the sample.
1950 The CPU mode can be determined from this value by masking with
1951 .B PERF_RECORD_MISC_CPUMODE_MASK
1952 and looking for one of the following (note these are not
1953 bit masks, only one can be set at a time):
1956 .B PERF_RECORD_MISC_CPUMODE_UNKNOWN
1959 .B PERF_RECORD_MISC_KERNEL
1960 Sample happened in the kernel.
1962 .B PERF_RECORD_MISC_USER
1963 Sample happened in user code.
1965 .B PERF_RECORD_MISC_HYPERVISOR
1966 Sample happened in the hypervisor.
1968 .BR PERF_RECORD_MISC_GUEST_KERNEL " (since Linux 2.6.35)"
1969 .\" commit 39447b386c846bbf1c56f6403c5282837486200f
1970 Sample happened in the guest kernel.
1972 .B PERF_RECORD_MISC_GUEST_USER " (since Linux 2.6.35)"
1973 .\" commit 39447b386c846bbf1c56f6403c5282837486200f
1974 Sample happened in guest user code.
1978 Since the following three statuses are generated by
1979 different record types, they alias to the same bit:
1981 .BR PERF_RECORD_MISC_MMAP_DATA " (since Linux 3.10)"
1982 .\" commit 2fe85427e3bf65d791700d065132772fc26e4d75
1983 This is set when the mapping is not executable;
1984 otherwise the mapping is executable.
1986 .BR PERF_RECORD_MISC_COMM_EXEC " (since Linux 3.16)"
1987 .\" commit 82b897782d10fcc4930c9d4a15b175348fdd2871
1990 record on kernels more recent than Linux 3.16
1991 if a process name change was caused by an
1995 .BR PERF_RECORD_MISC_SWITCH_OUT " (since Linux 4.3)"
1996 .\" commit 45ac1403f564f411c6a383a2448688ba8dd705a4
1998 .B PERF_RECORD_SWITCH
2000 .B PERF_RECORD_SWITCH_CPU_WIDE
2001 record is generated, this bit indicates that the
2002 context switch is away from the current process
2003 (instead of into the current process).
2007 In addition, the following bits can be set:
2009 .B PERF_RECORD_MISC_EXACT_IP
2010 This indicates that the content of
2013 to the actual instruction that triggered the event.
2015 .IR perf_event_attr.precise_ip .
2017 .BR PERF_RECORD_MISC_EXT_RESERVED " (since Linux 2.6.35)"
2018 .\" commit 1676b8a077c352085d52578fb4f29350b58b6e74
2019 This indicates there is extended data available (currently not used).
2021 .B PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT
2022 .\" commit 930e6fcd2bcce9bcd9d4aa7e755678d33f3fe6f4
2023 This bit is not set by the kernel.
2024 It is reserved for the user-space perf utility to indicate that
2025 .I /proc/i[pid]/maps
2026 parsing was taking too long and was stopped, and thus the mmap
2027 records may be truncated.
2033 value is one of the below.
2034 The values in the corresponding record (that follows the header)
2041 The MMAP events record the
2043 mappings so that we can correlate
2044 user-space IPs to code.
2045 They have the following structure:
2050 struct perf_event_header header;
2068 is the address of the allocated memory.
2070 is the length of the allocated memory.
2072 is the page offset of the allocated memory.
2074 is a string describing the backing of the allocated memory.
2078 This record indicates when events are lost.
2083 struct perf_event_header header;
2086 struct sample_id sample_id;
2093 is the unique event ID for the samples that were lost.
2096 is the number of events that were lost.
2100 This record indicates a change in the process name.
2105 struct perf_event_header header;
2109 struct sample_id sample_id;
2122 is a string containing the new name of the process.
2126 This record indicates a process exit event.
2131 struct perf_event_header header;
2135 struct sample_id sample_id;
2140 .BR PERF_RECORD_THROTTLE ", " PERF_RECORD_UNTHROTTLE
2141 This record indicates a throttle/unthrottle event.
2146 struct perf_event_header header;
2150 struct sample_id sample_id;
2156 This record indicates a fork event.
2161 struct perf_event_header header;
2165 struct sample_id sample_id;
2171 This record indicates a read event.
2176 struct perf_event_header header;
2178 struct read_format values;
2179 struct sample_id sample_id;
2184 .B PERF_RECORD_SAMPLE
2185 This record indicates a sample.
2190 struct perf_event_header header;
2191 u64 sample_id; /* if PERF_SAMPLE_IDENTIFIER */
2192 u64 ip; /* if PERF_SAMPLE_IP */
2193 u32 pid, tid; /* if PERF_SAMPLE_TID */
2194 u64 time; /* if PERF_SAMPLE_TIME */
2195 u64 addr; /* if PERF_SAMPLE_ADDR */
2196 u64 id; /* if PERF_SAMPLE_ID */
2197 u64 stream_id; /* if PERF_SAMPLE_STREAM_ID */
2198 u32 cpu, res; /* if PERF_SAMPLE_CPU */
2199 u64 period; /* if PERF_SAMPLE_PERIOD */
2200 struct read_format v;
2201 /* if PERF_SAMPLE_READ */
2202 u64 nr; /* if PERF_SAMPLE_CALLCHAIN */
2203 u64 ips[nr]; /* if PERF_SAMPLE_CALLCHAIN */
2204 u32 size; /* if PERF_SAMPLE_RAW */
2205 char data[size]; /* if PERF_SAMPLE_RAW */
2206 u64 bnr; /* if PERF_SAMPLE_BRANCH_STACK */
2207 struct perf_branch_entry lbr[bnr];
2208 /* if PERF_SAMPLE_BRANCH_STACK */
2209 u64 abi; /* if PERF_SAMPLE_REGS_USER */
2210 u64 regs[weight(mask)];
2211 /* if PERF_SAMPLE_REGS_USER */
2212 u64 size; /* if PERF_SAMPLE_STACK_USER */
2213 char data[size]; /* if PERF_SAMPLE_STACK_USER */
2214 u64 dyn_size; /* if PERF_SAMPLE_STACK_USER &&
2216 u64 weight; /* if PERF_SAMPLE_WEIGHT */
2217 u64 data_src; /* if PERF_SAMPLE_DATA_SRC */
2218 u64 transaction; /* if PERF_SAMPLE_TRANSACTION */
2219 u64 abi; /* if PERF_SAMPLE_REGS_INTR */
2220 u64 regs[weight(mask)];
2221 /* if PERF_SAMPLE_REGS_INTR */
2222 u64 phys_addr; /* if PERF_SAMPLE_PHYS_ADDR */
2223 u64 cgroup; /* if PERF_SAMPLE_CGROUP */
2231 .B PERF_SAMPLE_IDENTIFIER
2232 is enabled, a 64-bit unique ID is included.
2233 This is a duplication of the
2236 value, but included at the beginning of the sample
2237 so parsers can easily obtain the value.
2242 is enabled, then a 64-bit instruction
2243 pointer value is included.
2248 is enabled, then a 32-bit process ID
2249 and 32-bit thread ID are included.
2254 is enabled, then a 64-bit timestamp
2256 This is obtained via local_clock() which is a hardware timestamp
2257 if available and the jiffies value if not.
2262 is enabled, then a 64-bit address is included.
2263 This is usually the address of a tracepoint,
2264 breakpoint, or software event; otherwise the value is 0.
2269 is enabled, a 64-bit unique ID is included.
2270 If the event is a member of an event group, the group leader ID is returned.
2271 This ID is the same as the one returned by
2272 .BR PERF_FORMAT_ID .
2276 .B PERF_SAMPLE_STREAM_ID
2277 is enabled, a 64-bit unique ID is included.
2280 the actual ID is returned, not the group leader.
2281 This ID is the same as the one returned by
2282 .BR PERF_FORMAT_ID .
2287 is enabled, this is a 32-bit value indicating
2288 which CPU was being used, in addition to a reserved (unused)
2293 .B PERF_SAMPLE_PERIOD
2294 is enabled, a 64-bit value indicating
2295 the current sampling period is written.
2300 is enabled, a structure of type read_format
2301 is included which has values for all events in the event group.
2302 The values included depend on the
2305 .BR perf_event_open ()
2310 .B PERF_SAMPLE_CALLCHAIN
2311 is enabled, then a 64-bit number is included
2312 which indicates how many following 64-bit instruction pointers will
2314 This is the current callchain.
2316 .IR size ", " data[size]
2319 is enabled, then a 32-bit value indicating size
2320 is included followed by an array of 8-bit values of length size.
2321 The values are padded with 0 to have 64-bit alignment.
2323 This RAW record data is opaque with respect to the ABI.
2324 The ABI doesn't make any promises with respect to the stability
2325 of its content, it may vary depending
2326 on event, hardware, and kernel version.
2328 .IR bnr ", " lbr[bnr]
2330 .B PERF_SAMPLE_BRANCH_STACK
2331 is enabled, then a 64-bit value indicating
2332 the number of records is included, followed by
2334 .I perf_branch_entry
2335 structures which each include the fields:
2339 This indicates the source instruction (may not be a branch).
2345 The branch target was mispredicted.
2348 The branch target was predicted.
2350 .IR in_tx " (since Linux 3.11)"
2351 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
2352 The branch was in a transactional memory transaction.
2354 .IR abort " (since Linux 3.11)"
2355 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
2356 The branch was in an aborted transactional memory transaction.
2358 .IR cycles " (since Linux 4.3)"
2359 .\" commit 71ef3c6b9d4665ee7afbbe4c208a98917dcfc32f
2360 This reports the number of cycles elapsed since the
2361 previous branch stack update.
2363 The entries are from most to least recent, so the first entry
2364 has the most recent branch.
2371 is optional; if not supported, those
2374 The type of branches recorded is specified by the
2375 .I branch_sample_type
2379 .IR abi ", " regs[weight(mask)]
2381 .B PERF_SAMPLE_REGS_USER
2382 is enabled, then the user CPU registers are recorded.
2387 .BR PERF_SAMPLE_REGS_ABI_NONE ,
2388 .BR PERF_SAMPLE_REGS_ABI_32 ,
2390 .BR PERF_SAMPLE_REGS_ABI_64 .
2394 field is an array of the CPU registers that were specified by
2398 The number of values is the number of bits set in the
2402 .IR size ", " data[size] ", " dyn_size
2404 .B PERF_SAMPLE_STACK_USER
2405 is enabled, then the user stack is recorded.
2406 This can be used to generate stack backtraces.
2408 is the size requested by the user in
2409 .I sample_stack_user
2410 or else the maximum record size.
2412 is the stack data (a raw dump of the memory pointed to by the
2413 stack pointer at the time of sampling).
2415 is the amount of data actually dumped (can be less than
2425 .B PERF_SAMPLE_WEIGHT
2426 is enabled, then a 64-bit value provided by the hardware
2427 is recorded that indicates how costly the event was.
2428 This allows expensive events to stand out more clearly
2433 .B PERF_SAMPLE_DATA_SRC
2434 is enabled, then a 64-bit value is recorded that is made up of
2435 the following fields:
2439 Type of opcode, a bitwise combination of:
2450 .B PERF_MEM_OP_STORE
2453 .B PERF_MEM_OP_PFETCH
2462 Memory hierarchy level hit or miss, a bitwise combination of
2463 the following, shifted left by
2464 .BR PERF_MEM_LVL_SHIFT :
2475 .B PERF_MEM_LVL_MISS
2490 .B PERF_MEM_LVL_LOC_RAM
2493 .B PERF_MEM_LVL_REM_RAM1
2496 .B PERF_MEM_LVL_REM_RAM2
2499 .B PERF_MEM_LVL_REM_CCE1
2502 .B PERF_MEM_LVL_REM_CCE2
2514 Snoop mode, a bitwise combination of the following, shifted left by
2515 .BR PERF_MEM_SNOOP_SHIFT :
2520 .B PERF_MEM_SNOOP_NA
2523 .B PERF_MEM_SNOOP_NONE
2526 .B PERF_MEM_SNOOP_HIT
2529 .B PERF_MEM_SNOOP_MISS
2532 .B PERF_MEM_SNOOP_HITM
2538 Lock instruction, a bitwise combination of the following, shifted left by
2539 .BR PERF_MEM_LOCK_SHIFT :
2547 .B PERF_MEM_LOCK_LOCKED
2553 TLB access hit or miss, a bitwise combination of the following, shifted
2555 .BR PERF_MEM_TLB_SHIFT :
2566 .B PERF_MEM_TLB_MISS
2586 .B PERF_SAMPLE_TRANSACTION
2587 flag is set, then a 64-bit field is recorded describing
2588 the sources of any transactional memory aborts.
2590 The field is a bitwise combination of the following values:
2594 Abort from an elision type transaction (Intel-CPU-specific).
2596 .B PERF_TXN_TRANSACTION
2597 Abort from a generic transaction.
2600 Synchronous abort (related to the reported instruction).
2603 Asynchronous abort (not related to the reported instruction).
2606 Retryable abort (retrying the transaction may have succeeded).
2608 .B PERF_TXN_CONFLICT
2609 Abort due to memory conflicts with other threads.
2611 .B PERF_TXN_CAPACITY_WRITE
2612 Abort due to write capacity overflow.
2614 .B PERF_TXN_CAPACITY_READ
2615 Abort due to read capacity overflow.
2618 In addition, a user-specified abort code can be obtained from
2619 the high 32 bits of the field by shifting right by
2620 .B PERF_TXN_ABORT_SHIFT
2621 and masking with the value
2622 .BR PERF_TXN_ABORT_MASK .
2624 .IR abi ", " regs[weight(mask)]
2626 .B PERF_SAMPLE_REGS_INTR
2627 is enabled, then the user CPU registers are recorded.
2632 .BR PERF_SAMPLE_REGS_ABI_NONE ,
2633 .BR PERF_SAMPLE_REGS_ABI_32 ,
2635 .BR PERF_SAMPLE_REGS_ABI_64 .
2639 field is an array of the CPU registers that were specified by
2643 The number of values is the number of bits set in the
2649 .B PERF_SAMPLE_PHYS_ADDR
2650 flag is set, then the 64-bit physical address is recorded.
2654 .B PERF_SAMPLE_CGROUP
2656 then the 64-bit cgroup ID (for the perf_event subsystem) is recorded.
2657 To get the pathname of the cgroup, the ID should match to one in a
2658 .B PERF_RECORD_CGROUP .
2661 .B PERF_RECORD_MMAP2
2662 This record includes extended information on
2664 calls returning executable mappings.
2665 The format is similar to that of the
2667 record, but includes extra values that allow uniquely identifying
2673 struct perf_event_header header;
2686 struct sample_id sample_id;
2699 is the address of the allocated memory.
2702 is the length of the allocated memory.
2705 is the page offset of the allocated memory.
2708 is the major ID of the underlying device.
2711 is the minor ID of the underlying device.
2714 is the inode number.
2717 is the inode generation.
2720 is the protection information.
2723 is the flags information.
2726 is a string describing the backing of the allocated memory.
2729 .BR PERF_RECORD_AUX " (since Linux 4.1)"
2730 .\" commit 68db7e98c3a6ebe7284b6cf14906ed7c55f3f7f0
2731 This record reports that new data is available in the separate
2737 struct perf_event_header header;
2741 struct sample_id sample_id;
2748 offset in the AUX mmap region where the new data begins.
2751 size of the data made available.
2754 describes the AUX update.
2757 .B PERF_AUX_FLAG_TRUNCATED
2758 if set, then the data returned was truncated to fit the available
2761 .B PERF_AUX_FLAG_OVERWRITE
2762 .\" commit 2023a0d2829e521fe6ad6b9907f3f90bfbf57142
2763 if set, then the data returned has overwritten previous data.
2767 .BR PERF_RECORD_ITRACE_START " (since Linux 4.1)"
2768 .\" ec0d7729bbaed4b9d2d3fada693278e13a3d1368
2769 This record indicates which process has initiated an instruction
2770 trace event, allowing tools to properly correlate the instruction
2771 addresses in the AUX buffer with the proper executable.
2776 struct perf_event_header header;
2785 process ID of the thread starting an instruction trace.
2788 thread ID of the thread starting an instruction trace.
2791 .BR PERF_RECORD_LOST_SAMPLES " (since Linux 4.2)"
2792 .\" f38b0dbb491a6987e198aa6b428db8692a6480f8
2793 When using hardware sampling (such as Intel PEBS) this record
2794 indicates some number of samples that may have been lost.
2799 struct perf_event_header header;
2801 struct sample_id sample_id;
2808 the number of potentially lost samples.
2811 .BR PERF_RECORD_SWITCH " (since Linux 4.3)"
2812 .\" commit 45ac1403f564f411c6a383a2448688ba8dd705a4
2813 This record indicates a context switch has happened.
2815 .B PERF_RECORD_MISC_SWITCH_OUT
2818 field indicates whether it was a context switch into
2819 or away from the current process.
2824 struct perf_event_header header;
2825 struct sample_id sample_id;
2830 .BR PERF_RECORD_SWITCH_CPU_WIDE " (since Linux 4.3)"
2831 .\" commit 45ac1403f564f411c6a383a2448688ba8dd705a4
2833 .B PERF_RECORD_SWITCH
2834 this record indicates a context switch has happened,
2835 but it only occurs when sampling in CPU-wide mode
2836 and provides additional information on the process
2837 being switched to/from.
2839 .B PERF_RECORD_MISC_SWITCH_OUT
2842 field indicates whether it was a context switch into
2843 or away from the current process.
2848 struct perf_event_header header;
2851 struct sample_id sample_id;
2858 The process ID of the previous (if switching in)
2859 or next (if switching out) process on the CPU.
2862 The thread ID of the previous (if switching in)
2863 or next (if switching out) thread on the CPU.
2866 .BR PERF_RECORD_NAMESPACES " (since Linux 4.11)"
2867 .\" commit e422267322cd319e2695a535e47c5b1feeac45eb
2868 This record includes various namespace information of a process.
2873 struct perf_event_header header;
2877 struct { u64 dev, inode } [nr_namespaces];
2878 struct sample_id sample_id;
2891 is the number of namespaces in this record
2898 fields and is recorded in the
2899 fixed position like below:
2902 .BR NET_NS_INDEX = 0
2905 .BR UTS_NS_INDEX = 1
2908 .BR IPC_NS_INDEX = 2
2911 .BR PID_NS_INDEX = 3
2914 .BR USER_NS_INDEX = 4
2917 .BR MNT_NS_INDEX = 5
2920 .BR CGROUP_NS_INDEX = 6
2924 .BR PERF_RECORD_KSYMBOL " (since Linux 5.0)"
2925 .\" commit 76193a94522f1d4edf2447a536f3f796ce56343b
2926 This record indicates kernel symbol register/unregister events.
2931 struct perf_event_header header;
2937 struct sample_id sample_id;
2944 is the address of the kernel symbol.
2947 is the length of the kernel symbol.
2950 is the type of the kernel symbol.
2951 Currently the following types are available:
2954 .B PERF_RECORD_KSYMBOL_TYPE_BPF
2955 The kernel symbol is a BPF function.
2960 .B PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER
2961 is set, then this event is for unregistering the kernel symbol.
2964 .BR PERF_RECORD_BPF_EVENT " (since Linux 5.0)"
2965 .\" commit 6ee52e2a3fe4ea35520720736e6791df1fb67106
2966 This record indicates BPF program is loaded or unloaded.
2971 struct perf_event_header header;
2975 u8 tag[BPF_TAG_SIZE];
2976 struct sample_id sample_id;
2983 is one of the following values:
2986 .B PERF_BPF_EVENT_PROG_LOAD
2987 A BPF program is loaded
2989 .B PERF_BPF_EVENT_PROG_UNLOAD
2990 A BPF program is unloaded
2994 is the ID of the BPF program.
2997 is the tag of the BPF program.
3003 .BR PERF_RECORD_CGROUP " (since Linux 5.7)"
3004 .\" commit 96aaab686505c449e24d76e76507290dcc30e008
3005 This record indicates a new cgroup is created and activated.
3010 struct perf_event_header header;
3013 struct sample_id sample_id;
3020 is the cgroup identifier.
3021 This can be also retrieved by
3022 .BR name_to_handle_at (2)
3023 on the cgroup path (as a file handle).
3026 is the path of the cgroup from the root.
3029 .BR PERF_RECORD_TEXT_POKE " (since Linux 5.8)"
3030 .\" commit e17d43b93e544f5016c0251d2074c15568d5d963
3031 This record indicates a change in the kernel text.
3032 This includes addition and removal of the text
3033 and the corresponding length is zero in this case.
3038 struct perf_event_header header;
3043 struct sample_id sample_id;
3050 is the address of the change
3059 contains old bytes immediately followed by new bytes.
3062 .SS Overflow handling
3063 Events can be set to notify when a threshold is crossed,
3064 indicating an overflow.
3065 Overflow conditions can be captured by monitoring the
3066 event file descriptor with
3071 Alternatively, the overflow events can be captured via sa signal handler,
3072 by enabling I/O signaling on the file descriptor; see the discussion of the
3079 Overflows are generated only by sampling events
3081 must have a nonzero value).
3083 There are two ways to generate overflow notifications.
3085 The first is to set a
3089 value that will trigger if a certain number of samples
3090 or bytes have been written to the mmap ring buffer.
3095 The other way is by use of the
3096 .B PERF_EVENT_IOC_REFRESH
3098 This ioctl adds to a counter that decrements each time the event overflows.
3102 once the counter reaches 0
3105 the underlying event is disabled.
3107 Refreshing an event group leader refreshes all siblings and
3108 refreshing with a parameter of 0 currently enables infinite
3110 these behaviors are unsupported and should not be relied on.
3111 .\" See https://lkml.org/lkml/2011/5/24/337
3113 Starting with Linux 3.18,
3114 .\" commit 179033b3e064d2cd3f5f9945e76b0a0f0fbf4883
3116 is indicated if the event being monitored is attached to a different
3117 process and that process exits.
3118 .SS rdpmc instruction
3119 Starting with Linux 3.4 on x86, you can use the
3120 .\" commit c7206205d00ab375839bd6c7ddb247d600693c09
3122 instruction to get low-latency reads without having to enter the kernel.
3125 is not necessarily faster than other methods for reading event values.
3127 Support for this can be detected with the
3129 field in the mmap page; documentation on how
3130 to calculate event values can be found in that section.
3132 Originally, when rdpmc support was enabled, any process (not just ones
3133 with an active perf event) could use the rdpmc instruction to access
3135 Starting with Linux 4.0,
3136 .\" 7911d3f7af14a614617e38245fedf98a724e46a9
3137 rdpmc support is only allowed if an event is currently enabled
3138 in a process's context.
3139 To restore the old behavior, write the value 2 to
3140 .IR /sys/devices/cpu/rdpmc .
3141 .SS perf_event ioctl calls
3142 Various ioctls act on
3143 .BR perf_event_open ()
3146 .B PERF_EVENT_IOC_ENABLE
3147 This enables the individual event or event group specified by the
3148 file descriptor argument.
3151 .B PERF_IOC_FLAG_GROUP
3152 bit is set in the ioctl argument, then all events in a group are
3153 enabled, even if the event specified is not the group leader
3156 .B PERF_EVENT_IOC_DISABLE
3157 This disables the individual counter or event group specified by the
3158 file descriptor argument.
3160 Enabling or disabling the leader of a group enables or disables the
3161 entire group; that is, while the group leader is disabled, none of the
3162 counters in the group will count.
3163 Enabling or disabling a member of a group other than the leader
3164 affects only that counter; disabling a non-leader
3165 stops that counter from counting but doesn't affect any other counter.
3168 .B PERF_IOC_FLAG_GROUP
3169 bit is set in the ioctl argument, then all events in a group are
3170 disabled, even if the event specified is not the group leader
3173 .B PERF_EVENT_IOC_REFRESH
3174 Non-inherited overflow counters can use this
3175 to enable a counter for a number of overflows specified by the argument,
3176 after which it is disabled.
3177 Subsequent calls of this ioctl add the argument value to the current
3179 An overflow notification with
3181 set will happen on each overflow until the
3182 count reaches 0; when that happens a notification with
3184 set is sent and the event is disabled.
3185 Using an argument of 0 is considered undefined behavior.
3187 .B PERF_EVENT_IOC_RESET
3188 Reset the event count specified by the
3189 file descriptor argument to zero.
3190 This resets only the counts; there is no way to reset the
3198 .B PERF_IOC_FLAG_GROUP
3199 bit is set in the ioctl argument, then all events in a group are
3200 reset, even if the event specified is not the group leader
3203 .B PERF_EVENT_IOC_PERIOD
3204 This updates the overflow period for the event.
3206 Since Linux 3.7 (on ARM)
3207 .\" commit 3581fe0ef37ce12ac7a4f74831168352ae848edc
3208 and Linux 3.14 (all other architectures),
3209 .\" commit bad7192b842c83e580747ca57104dd51fe08c223
3210 the new period takes effect immediately.
3211 On older kernels, the new period did not take effect until
3212 after the next overflow.
3214 The argument is a pointer to a 64-bit value containing the
3217 Prior to Linux 2.6.36,
3218 .\" commit ad0cf3478de8677f720ee06393b3147819568d6a
3219 this ioctl always failed due to a bug
3222 .B PERF_EVENT_IOC_SET_OUTPUT
3223 This tells the kernel to report event notifications to the specified
3224 file descriptor rather than the default one.
3225 The file descriptors must all be on the same CPU.
3227 The argument specifies the desired file descriptor, or \-1 if
3228 output should be ignored.
3230 .BR PERF_EVENT_IOC_SET_FILTER " (since Linux 2.6.33)"
3231 .\" commit 6fb2915df7f0747d9044da9dbff5b46dc2e20830
3232 This adds an ftrace filter to this event.
3234 The argument is a pointer to the desired ftrace filter.
3236 .BR PERF_EVENT_IOC_ID " (since Linux 3.12)"
3237 .\" commit cf4957f17f2a89984915ea808876d9c82225b862
3238 This returns the event ID value for the given event file descriptor.
3240 The argument is a pointer to a 64-bit unsigned integer
3243 .BR PERF_EVENT_IOC_SET_BPF " (since Linux 4.1)"
3244 .\" commit 2541517c32be2531e0da59dfd7efc1ce844644f5
3245 This allows attaching a Berkeley Packet Filter (BPF)
3246 program to an existing kprobe tracepoint event.
3249 (since Linux 5.8) or
3251 privileges to use this ioctl.
3253 The argument is a BPF program file descriptor that was created by
3258 .BR PERF_EVENT_IOC_PAUSE_OUTPUT " (since Linux 4.7)"
3259 .\" commit 86e7972f690c1017fd086cdfe53d8524e68c661c
3260 This allows pausing and resuming the event's ring-buffer.
3261 A paused ring-buffer does not prevent generation of samples,
3262 but simply discards them.
3263 The discarded samples are considered lost, and cause a
3264 .BR PERF_RECORD_LOST
3265 sample to be generated when possible.
3266 An overflow signal may still be triggered by the discarded sample
3267 even though the ring-buffer remains empty.
3269 The argument is an unsigned 32-bit integer.
3270 A nonzero value pauses the ring-buffer, while a
3271 zero value resumes the ring-buffer.
3273 .BR PERF_EVENT_MODIFY_ATTRIBUTES " (since Linux 4.17)"
3274 .\" commit 32ff77e8cc9e66cc4fb38098f64fd54cc8f54573
3275 This allows modifying an existing event without the overhead
3276 of closing and reopening a new event.
3277 Currently this is supported only for breakpoint events.
3279 The argument is a pointer to a
3281 structure containing the updated event settings.
3283 .BR PERF_EVENT_IOC_QUERY_BPF " (since Linux 4.16)"
3284 .\" commit f371b304f12e31fe30207c41ca7754564e0ea4dc
3285 This allows querying which Berkeley Packet Filter (BPF)
3286 programs are attached to an existing kprobe tracepoint.
3287 You can only attach one BPF program per event, but you can
3288 have multiple events attached to a tracepoint.
3289 Querying this value on one tracepoint event returns the ID
3290 of all BPF programs in all events attached to the tracepoint.
3293 (since Linux 5.8) or
3295 privileges to use this ioctl.
3297 The argument is a pointer to a structure
3300 struct perf_event_query_bpf {
3310 field indicates the number of ids that can fit in the provided
3315 value is filled in by the kernel with the number of attached
3319 array is filled with the ID of each attached BPF program.
3320 If there are more programs than will fit in the array, then the
3325 will indicate the number of program IDs that were successfully copied.
3328 A process can enable or disable all currently open event groups
3331 .B PR_TASK_PERF_EVENTS_ENABLE
3333 .B PR_TASK_PERF_EVENTS_DISABLE
3335 This applies only to events created locally by the calling process.
3336 This does not apply to events created by other processes attached
3337 to the calling process or inherited events from a parent process.
3338 Only group leaders are enabled and disabled,
3339 not any other members of the groups.
3340 .SS perf_event related configuration files
3342 .I /proc/sys/kernel/
3345 .I /proc/sys/kernel/perf_event_paranoid
3347 .I perf_event_paranoid
3348 file can be set to restrict access to the performance counters.
3353 allow only user-space measurements (default since Linux 4.6).
3354 .\" default changed in commit 0161028b7c8aebef64194d3d73e43bc3b53b5c66
3356 allow both kernel and user measurements (default before Linux 4.6).
3358 allow access to CPU-specific data but not raw tracepoint samples.
3364 The existence of the
3365 .I perf_event_paranoid
3366 file is the official method for determining if a kernel supports
3367 .BR perf_event_open ().
3369 .I /proc/sys/kernel/perf_event_max_sample_rate
3370 This sets the maximum sample rate.
3371 Setting this too high can allow
3372 users to sample at a rate that impacts overall machine performance
3373 and potentially lock up the machine.
3374 The default value is
3375 100000 (samples per second).
3377 .I /proc/sys/kernel/perf_event_max_stack
3378 .\" Introduced in c5dfd78eb79851e278b7973031b9ca363da87a7e
3379 This file sets the maximum depth of stack frame entries reported
3380 when generating a call trace.
3382 .I /proc/sys/kernel/perf_event_mlock_kb
3383 Maximum number of pages an unprivileged user can
3385 The default is 516 (kB).
3389 .I /sys/bus/event_source/devices/
3392 Since Linux 2.6.34, the kernel supports having multiple PMUs
3393 available for monitoring.
3394 Information on how to program these PMUs can be found under
3395 .IR /sys/bus/event_source/devices/ .
3396 Each subdirectory corresponds to a different PMU.
3398 .IR /sys/bus/event_source/devices/*/type " (since Linux 2.6.38)"
3399 .\" commit abe43400579d5de0078c2d3a760e6598e183f871
3400 This contains an integer that can be used in the
3404 to indicate that you wish to use this PMU.
3406 .IR /sys/bus/event_source/devices/cpu/rdpmc " (since Linux 3.4)"
3407 .\" commit 0c9d42ed4cee2aa1dfc3a260b741baae8615744f
3408 If this file is 1, then direct user-space access to the
3409 performance counter registers is allowed via the rdpmc instruction.
3410 This can be disabled by echoing 0 to the file.
3413 .\" a66734297f78707ce39d756b656bfae861d53f62
3414 .\" 7911d3f7af14a614617e38245fedf98a724e46a9
3415 the behavior has changed, so that 1 now means only allow access
3416 to processes with active perf events, with 2 indicating the old
3417 allow-anyone-access behavior.
3419 .IR /sys/bus/event_source/devices/*/format/ " (since Linux 3.4)"
3420 .\" commit 641cc938815dfd09f8fa1ec72deb814f0938ac33
3421 This subdirectory contains information on the architecture-specific
3422 subfields available for programming the various
3428 The content of each file is the name of the config field, followed
3429 by a colon, followed by a series of integer bit ranges separated by
3431 For example, the file
3433 may contain the value
3434 .I config1:1,6\-10,44
3435 which indicates that event is an attribute that occupies bits 1,6\(en10, and 44
3437 .IR perf_event_attr::config1 .
3439 .IR /sys/bus/event_source/devices/*/events/ " (since Linux 3.4)"
3440 .\" commit 641cc938815dfd09f8fa1ec72deb814f0938ac33
3441 This subdirectory contains files with predefined events.
3442 The contents are strings describing the event settings
3443 expressed in terms of the fields found in the previously mentioned
3446 These are not necessarily complete lists of all events supported by
3447 a PMU, but usually a subset of events deemed useful or interesting.
3449 The content of each file is a list of attribute names
3450 separated by commas.
3451 Each entry has an optional value (either hex or decimal).
3452 If no value is specified, then it is assumed to be a single-bit
3453 field with a value of 1.
3454 An example entry may look like this:
3455 .IR event=0x2,inv,ldlat=3 .
3457 .I /sys/bus/event_source/devices/*/uevent
3458 This file is the standard kernel device interface
3459 for injecting hotplug events.
3461 .IR /sys/bus/event_source/devices/*/cpumask " (since Linux 3.7)"
3462 .\" commit 314d9f63f385096580e9e2a06eaa0745d92fe4ac
3465 file contains a comma-separated list of integers that
3466 indicate a representative CPU number for each socket (package)
3468 This is needed when setting up uncore or northbridge events, as
3469 those PMUs present socket-wide events.
3473 .BR perf_event_open ()
3474 returns the new file descriptor.
3475 On error, \-1 is returned and
3477 is set to indicate the error.
3479 The errors returned by
3480 .BR perf_event_open ()
3481 can be inconsistent, and may
3482 vary across processor architectures and performance monitoring units.
3490 .BR PERF_ATTR_SIZE_VER0 ),
3491 too big (larger than the page size),
3492 or larger than the kernel supports and the extra bytes are not zero.
3498 field is overwritten by the kernel to be the size of the structure
3502 Returned when the requested event requires
3504 (since Linux 5.8) or
3506 permissions (or a more permissive perf_event paranoid setting).
3507 Some common cases where an unprivileged process
3508 may encounter this error:
3509 attaching to a process owned by a different user;
3510 monitoring all processes on a given CPU (i.e., specifying the
3515 when the paranoid setting requires it.
3520 file descriptor is not valid, or, if
3521 .B PERF_FLAG_PID_CGROUP
3523 the cgroup file descriptor in
3527 .BR EBUSY " (since Linux 4.1)"
3528 .\" bed5b25ad9c8a2f5d735ef0bc746ec870c01c1b0
3529 Returned if another event already has exclusive
3535 pointer points at an invalid memory address.
3538 Returned if the specified event is invalid.
3539 There are many possible reasons for this.
3540 A not-exhaustive list:
3542 is higher than the maximum setting;
3545 to monitor does not exist;
3552 value is out of range;
3556 set and the event is not a group leader;
3559 values are out of range or set reserved bits;
3560 the generic event selected is not supported; or
3561 there is not enough room to add the selected event.
3564 Returned when trying to mix perf and ftrace handling
3568 Each opened event uses one file descriptor.
3569 If a large number of events are opened,
3570 the per-process limit on the number of open file descriptors will be reached,
3571 and no more events can be created.
3574 Returned when the event involves a feature not supported
3580 setting is not valid.
3581 This error is also returned for
3582 some unsupported generic events.
3585 Prior to Linux 3.3, if there was not enough room for the event,
3586 .\" commit aa2bc1ade59003a379ffc485d6da2d92ea3370a6
3589 In Linux 3.3, this was changed to
3592 is still returned if you try to add more breakpoint events
3593 than supported by the hardware.
3597 .B PERF_SAMPLE_STACK_USER
3600 and it is not supported by hardware.
3603 Returned if an event requiring a specific hardware feature is
3604 requested but there is no hardware support.
3605 This includes requesting low-skid events if not supported,
3606 branch tracing if it is not available, sampling if no PMU
3607 interrupt is available, and branch stacks for software events.
3609 .BR EOVERFLOW " (since Linux 4.8)"
3610 .\" 97c79a38cd454602645f0470ffb444b3b75ce574
3612 .B PERF_SAMPLE_CALLCHAIN
3615 is larger than the maximum specified in
3616 .IR /proc/sys/kernel/perf_event_max_stack .
3619 Returned on many (but not all) architectures when an unsupported
3620 .IR exclude_hv ", " exclude_idle ", " exclude_user ", or " exclude_kernel
3621 setting is specified.
3623 It can also happen, as with
3625 when the requested event requires
3627 (since Linux 5.8) or
3629 permissions (or a more permissive perf_event paranoid setting).
3630 This includes setting a breakpoint on a kernel address,
3631 and (since Linux 3.13) setting a kernel function-trace tracepoint.
3632 .\" commit a4e95fc2cbb31d70a65beffeaf8773f881328c34
3635 Returned if attempting to attach to a process that does not exist.
3637 .BR perf_event_open ()
3638 was introduced in Linux 2.6.31 but was called
3639 .\" commit 0793a61d4df8daeac6492dbf8d2f3e5713caae5e
3640 .BR perf_counter_open ().
3641 It was renamed in Linux 2.6.32.
3642 .\" commit cdd6c482c9ff9c55475ee7392ec8f672eddb7be6
3645 .BR perf_event_open ()
3646 system call Linux-specific
3647 and should not be used in programs intended to be portable.
3649 The official way of knowing if
3650 .BR perf_event_open ()
3651 support is enabled is checking
3652 for the existence of the file
3653 .IR /proc/sys/kernel/perf_event_paranoid .
3656 capability (since Linux 5.8) provides secure approach to
3657 performance monitoring and observability operations in a system
3658 according to the principal of least privilege (POSIX IEEE 1003.1e).
3659 Accessing system performance monitoring and observability operations
3662 rather than the much more powerful
3664 excludes chances to misuse credentials and makes operations more secure.
3666 usage for secure system performance monitoring and observability
3667 is discouraged in favor of the
3675 is needed to properly get overflow signals in threads.
3676 This was introduced in Linux 2.6.32.
3677 .\" commit ba0a6c9f6fceed11c6a99e8326f0477fe383e6b5
3679 Prior to Linux 2.6.33 (at least for x86),
3680 .\" commit b690081d4d3f6a23541493f1682835c3cd5c54a1
3681 the kernel did not check
3682 if events could be scheduled together until read time.
3683 The same happens on all known kernels if the NMI watchdog is enabled.
3684 This means to see if a given set of events works you have to
3685 .BR perf_event_open (),
3686 start, then read before you know for sure you
3687 can get valid measurements.
3689 Prior to Linux 2.6.34,
3690 .\" FIXME . cannot find a kernel commit for this one
3691 event constraints were not enforced by the kernel.
3692 In that case, some events would silently return "0" if the kernel
3693 scheduled them in an improper counter slot.
3695 Prior to Linux 2.6.34, there was a bug when multiplexing where the
3696 wrong results could be returned.
3697 .\" commit 45e16a6834b6af098702e5ea6c9a40de42ff77d8
3699 Kernels from Linux 2.6.35 to Linux 2.6.39 can quickly crash the kernel if
3700 "inherit" is enabled and many threads are started.
3701 .\" commit 38b435b16c36b0d863efcf3f07b34a6fac9873fd
3703 Prior to Linux 2.6.35,
3704 .\" commit 050735b08ca8a016bbace4445fa025b88fee770b
3705 .B PERF_FORMAT_GROUP
3706 did not work with attached processes.
3708 There is a bug in the kernel code between
3709 Linux 2.6.36 and Linux 3.0 that ignores the
3710 "watermark" field and acts as if a wakeup_event
3711 was chosen if the union has a
3712 nonzero value in it.
3713 .\" commit 4ec8363dfc1451f8c8f86825731fe712798ada02
3715 From Linux 2.6.31 to Linux 3.4, the
3716 .B PERF_IOC_FLAG_GROUP
3717 ioctl argument was broken and would repeatedly operate
3718 on the event specified rather than iterating across
3719 all sibling events in a group.
3720 .\" commit 724b6daa13e100067c30cfc4d1ad06629609dc4e
3722 From Linux 3.4 to Linux 3.11, the mmap
3723 .\" commit fa7315871046b9a4c48627905691dbde57e51033
3727 bits mapped to the same location.
3728 Code should migrate to the new
3734 Always double-check your results!
3735 Various generalized events have had wrong values.
3736 For example, retired branches measured
3737 the wrong thing on AMD machines until Linux 2.6.35.
3738 .\" commit f287d332ce835f77a4f5077d2c0ef1e3f9ea42d2
3740 The following is a short example that measures the total
3741 instruction count of a call to
3749 #include <sys/ioctl.h>
3750 #include <linux/perf_event.h>
3751 #include <asm/unistd.h>
3754 perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
3755 int cpu, int group_fd, unsigned long flags)
3759 ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
3765 main(int argc, char *argv[])
3767 struct perf_event_attr pe;
3771 memset(&pe, 0, sizeof(pe));
3772 pe.type = PERF_TYPE_HARDWARE;
3773 pe.size = sizeof(pe);
3774 pe.config = PERF_COUNT_HW_INSTRUCTIONS;
3776 pe.exclude_kernel = 1;
3779 fd = perf_event_open(&pe, 0, \-1, \-1, 0);
3781 fprintf(stderr, "Error opening leader %llx\en", pe.config);
3785 ioctl(fd, PERF_EVENT_IOC_RESET, 0);
3786 ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
3788 printf("Measuring instruction count for this printf\en");
3790 ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
3791 read(fd, &count, sizeof(count));
3793 printf("Used %lld instructions\en", count);
3806 .IR Documentation/admin\-guide/perf\-security.rst
3807 in the kernel source tree