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 2015-07-23 "Linux" "Linux Programmer's Manual"
29 perf_event_open \- set up performance monitoring
32 .B #include <linux/perf_event.h>
33 .B #include <linux/hw_breakpoint.h>
35 .BI "int perf_event_open(struct perf_event_attr *" attr ,
36 .BI " pid_t " pid ", int " cpu ", int " group_fd ,
37 .BI " unsigned long " flags );
41 There is no glibc wrapper for this system call; see NOTES.
43 Given a list of parameters,
44 .BR perf_event_open ()
45 returns a file descriptor, for use in subsequent system calls
46 .RB ( read "(2), " mmap "(2), " prctl "(2), " fcntl "(2), etc.)."
49 .BR perf_event_open ()
50 creates a file descriptor that allows measuring performance
52 Each file descriptor corresponds to one
53 event that is measured; these can be grouped together
54 to measure multiple events simultaneously.
56 Events can be enabled and disabled in two ways: via
60 When an event is disabled it does not count or generate overflows but does
61 continue to exist and maintain its count value.
63 Events come in two flavors: counting and sampled.
66 event is one that is used for counting the aggregate number of events
68 In general, counting event results are gathered with a
73 event periodically writes measurements to a buffer that can then
82 arguments allow specifying which process and CPU to monitor:
84 .BR "pid == 0" " and " "cpu == \-1"
85 This measures the calling process/thread on any CPU.
87 .BR "pid == 0" " and " "cpu >= 0"
88 This measures the calling process/thread only
89 when running on the specified CPU.
91 .BR "pid > 0" " and " "cpu == \-1"
92 This measures the specified process/thread on any CPU.
94 .BR "pid > 0" " and " "cpu >= 0"
95 This measures the specified process/thread only
96 when running on the specified CPU.
98 .BR "pid == \-1" " and " "cpu >= 0"
99 This measures all processes/threads on the specified CPU.
103 .I /proc/sys/kernel/perf_event_paranoid
104 value of less than 1.
106 .BR "pid == \-1" " and " "cpu == \-1"
107 This setting is invalid and will return an error.
111 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: it will
124 be put onto the CPU only if all of the events in the group can be put onto
126 This means that the values of the member events can be
127 meaningfully compared\(emadded, divided (to get ratios), and so on\(emwith each
128 other, since they have counted events for the same set of executed
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 .BR 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't count user */
215 exclude_kernel : 1, /* don't count kernel */
216 exclude_hv : 1, /* don't count hypervisor */
217 exclude_idle : 1, /* don'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't count in host */
229 exclude_guest : 1, /* don'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 due to exec */
236 use_clockid : 1, /* use clockid for time fields */
241 __u32 wakeup_events; /* wakeup every n events */
242 __u32 wakeup_watermark; /* bytes before wakeup */
245 __u32 bp_type; /* breakpoint type */
248 __u64 bp_addr; /* breakpoint address */
249 __u64 config1; /* extension of config */
253 __u64 bp_len; /* breakpoint length */
254 __u64 config2; /* extension of config1 */
256 __u64 branch_sample_type; /* enum perf_branch_sample_type */
257 __u64 sample_regs_user; /* user regs to dump on samples */
258 __u32 sample_stack_user; /* size of stack to dump on
260 __s32 clockid; /* clock to use for time fields */
261 __u64 sample_regs_intr; /* regs to dump on samples */
268 structure are described in more detail below:
271 This field specifies the overall event type.
272 It has one of the following values:
275 .B PERF_TYPE_HARDWARE
276 This indicates one of the "generalized" hardware events provided
280 field definition for more details.
282 .B PERF_TYPE_SOFTWARE
283 This indicates one of the software-defined events provided by the kernel
284 (even if no hardware support is available).
286 .B PERF_TYPE_TRACEPOINT
287 This indicates a tracepoint
288 provided by the kernel tracepoint infrastructure.
290 .B PERF_TYPE_HW_CACHE
291 This indicates a hardware cache event.
292 This has a special encoding, described in the
297 This indicates a "raw" implementation-specific event in the
300 .BR PERF_TYPE_BREAKPOINT " (since Linux 2.6.33)"
301 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
302 This indicates a hardware breakpoint as provided by the CPU.
303 Breakpoints can be read/write accesses to an address as well as
304 execution of an instruction address.
308 .\" commit 2e80a82a49c4c7eca4e35734380f28298ba5db19
309 .BR perf_event_open ()
310 can support multiple PMUs.
311 To enable this, a value exported by the kernel can be used in the
313 field to indicate which PMU to use.
314 The value to use can be found in the sysfs filesystem:
315 there is a subdirectory per PMU instance under
316 .IR /sys/bus/event_source/devices .
317 In each subdirectory there is a
319 file whose content is an integer that can be used in the
323 .I /sys/bus/event_source/devices/cpu/type
324 contains the value for the core CPU PMU, which is usually 4.
330 structure for forward/backward compatibility.
332 .I sizeof(struct perf_event_attr)
333 to allow the kernel to see
334 the struct size at the time of compilation.
337 .B PERF_ATTR_SIZE_VER0
338 is set to 64; this was the size of the first published struct.
339 .B PERF_ATTR_SIZE_VER1
340 is 72, corresponding to the addition of breakpoints in Linux 2.6.33.
341 .\" commit cb5d76999029ae7a517cb07dfa732c1b5a934fc2
342 .\" this was added much later when PERF_ATTR_SIZE_VER2 happened
343 .\" but the actual attr_size had increased in 2.6.33
344 .B PERF_ATTR_SIZE_VER2
345 is 80 corresponding to the addition of branch sampling in Linux 3.4.
346 .\" commit cb5d76999029ae7a517cb07dfa732c1b5a934fc2
347 .B PERF_ATTR_SIZE_VER3
348 is 96 corresponding to the addition
354 .\" commit 1659d129ed014b715b0b2120e6fd929bdd33ed03
355 .B PERF_ATTR_SIZE_VER4
356 is 104 corresponding to the addition of
359 .\" commit 60e2364e60e86e81bc6377f49779779e6120977f
362 This specifies which event you want, in conjunction with
367 .IR config1 " and " config2
368 fields are also taken into account in cases where 64 bits is not
369 enough to fully specify the event.
370 The encoding of these fields are event dependent.
372 There are various ways to set the
374 field that are dependent on the value of the previously
378 What follows are various possible settings for
386 .BR PERF_TYPE_HARDWARE ,
387 we are measuring one of the generalized hardware CPU events.
388 Not all of these are available on all platforms.
391 to one of the following:
394 .B PERF_COUNT_HW_CPU_CYCLES
396 Be wary of what happens during CPU frequency scaling.
398 .B PERF_COUNT_HW_INSTRUCTIONS
399 Retired instructions.
400 Be careful, these can be affected by various
401 issues, most notably hardware interrupt counts.
403 .B PERF_COUNT_HW_CACHE_REFERENCES
405 Usually this indicates Last Level Cache accesses but this may
406 vary depending on your CPU.
407 This may include prefetches and coherency messages; again this
408 depends on the design of your CPU.
410 .B PERF_COUNT_HW_CACHE_MISSES
412 Usually this indicates Last Level Cache misses; this is intended to be
413 used in conjunction with the
414 .B PERF_COUNT_HW_CACHE_REFERENCES
415 event to calculate cache miss rates.
417 .B PERF_COUNT_HW_BRANCH_INSTRUCTIONS
418 Retired branch instructions.
419 Prior to Linux 2.6.35, this used
420 the wrong event on AMD processors.
421 .\" commit f287d332ce835f77a4f5077d2c0ef1e3f9ea42d2
423 .B PERF_COUNT_HW_BRANCH_MISSES
424 Mispredicted branch instructions.
426 .B PERF_COUNT_HW_BUS_CYCLES
427 Bus cycles, which can be different from total cycles.
429 .BR PERF_COUNT_HW_STALLED_CYCLES_FRONTEND " (since Linux 3.0)"
430 .\" commit 8f62242246351b5a4bc0c1f00c0c7003edea128a
431 Stalled cycles during issue.
433 .BR PERF_COUNT_HW_STALLED_CYCLES_BACKEND " (since Linux 3.0)"
434 .\" commit 8f62242246351b5a4bc0c1f00c0c7003edea128a
435 Stalled cycles during retirement.
437 .BR PERF_COUNT_HW_REF_CPU_CYCLES " (since Linux 3.3)"
438 .\" commit c37e17497e01fc0f5d2d6feb5723b210b3ab8890
439 Total cycles; not affected by CPU frequency scaling.
445 .BR PERF_TYPE_SOFTWARE ,
446 we are measuring software events provided by the kernel.
449 to one of the following:
452 .B PERF_COUNT_SW_CPU_CLOCK
453 This reports the CPU clock, a high-resolution per-CPU timer.
455 .B PERF_COUNT_SW_TASK_CLOCK
456 This reports a clock count specific to the task that is running.
458 .B PERF_COUNT_SW_PAGE_FAULTS
459 This reports the number of page faults.
461 .B PERF_COUNT_SW_CONTEXT_SWITCHES
462 This counts context switches.
463 Until Linux 2.6.34, these were all reported as user-space
464 events, after that they are reported as happening in the kernel.
465 .\" commit e49a5bd38159dfb1928fd25b173bc9de4bbadb21
467 .B PERF_COUNT_SW_CPU_MIGRATIONS
468 This reports the number of times the process
469 has migrated to a new CPU.
471 .B PERF_COUNT_SW_PAGE_FAULTS_MIN
472 This counts the number of minor page faults.
473 These did not require disk I/O to handle.
475 .B PERF_COUNT_SW_PAGE_FAULTS_MAJ
476 This counts the number of major page faults.
477 These required disk I/O to handle.
479 .BR PERF_COUNT_SW_ALIGNMENT_FAULTS " (since Linux 2.6.33)"
480 .\" commit f7d7986060b2890fc26db6ab5203efbd33aa2497
481 This counts the number of alignment faults.
482 These happen when unaligned memory accesses happen; the kernel
483 can handle these but it reduces performance.
484 This happens only on some architectures (never on x86).
486 .BR PERF_COUNT_SW_EMULATION_FAULTS " (since Linux 2.6.33)"
487 .\" commit f7d7986060b2890fc26db6ab5203efbd33aa2497
488 This counts the number of emulation faults.
489 The kernel sometimes traps on unimplemented instructions
490 and emulates them for user space.
491 This can negatively impact performance.
493 .BR PERF_COUNT_SW_DUMMY " (since Linux 3.12)"
494 .\" commit fa0097ee690693006ab1aea6c01ad3c851b65c77
495 This is a placeholder event that counts nothing.
496 Informational sample record types such as mmap or comm
497 must be associated with an active event.
498 This dummy event allows gathering such records without requiring
506 .BR PERF_TYPE_TRACEPOINT ,
507 then we are measuring kernel tracepoints.
510 can be obtained from under debugfs
511 .I tracing/events/*/*/id
512 if ftrace is enabled in the kernel.
519 .BR PERF_TYPE_HW_CACHE ,
520 then we are measuring a hardware CPU cache event.
521 To calculate the appropriate
523 value use the following equation:
527 (perf_hw_cache_id) | (perf_hw_cache_op_id << 8) |
528 (perf_hw_cache_op_result_id << 16)
536 .B PERF_COUNT_HW_CACHE_L1D
537 for measuring Level 1 Data Cache
539 .B PERF_COUNT_HW_CACHE_L1I
540 for measuring Level 1 Instruction Cache
542 .B PERF_COUNT_HW_CACHE_LL
543 for measuring Last-Level Cache
545 .B PERF_COUNT_HW_CACHE_DTLB
546 for measuring the Data TLB
548 .B PERF_COUNT_HW_CACHE_ITLB
549 for measuring the Instruction TLB
551 .B PERF_COUNT_HW_CACHE_BPU
552 for measuring the branch prediction unit
554 .BR PERF_COUNT_HW_CACHE_NODE " (since Linux 3.1)"
555 .\" commit 89d6c0b5bdbb1927775584dcf532d98b3efe1477
556 for measuring local memory accesses
560 .I perf_hw_cache_op_id
564 .B PERF_COUNT_HW_CACHE_OP_READ
567 .B PERF_COUNT_HW_CACHE_OP_WRITE
570 .B PERF_COUNT_HW_CACHE_OP_PREFETCH
571 for prefetch accesses
575 .I perf_hw_cache_op_result_id
579 .B PERF_COUNT_HW_CACHE_RESULT_ACCESS
582 .B PERF_COUNT_HW_CACHE_RESULT_MISS
594 Most CPUs support events that are not covered by the "generalized" events.
595 These are implementation defined; see your CPU manual (for example
596 the Intel Volume 3B documentation or the AMD BIOS and Kernel Developer
598 The libpfm4 library can be used to translate from the name in the
599 architectural manuals to the raw hex value
600 .BR perf_event_open ()
601 expects in this field.
606 .BR PERF_TYPE_BREAKPOINT ,
610 Its parameters are set in other places.
613 .IR sample_period ", " sample_freq
614 A "sampling" event is one that generates an overflow notification
615 every N events, where N is given by
618 .IR sample_period " > 0."
619 When an overflow occurs, requested data is recorded
623 field controls what data is recorded on each overflow.
626 can be used if you wish to use frequency rather than period.
627 In this case, you set the
630 The kernel will adjust the sampling period
631 to try and achieve the desired rate.
632 The rate of adjustment is a
636 The various bits in this field specify which values to include
638 They will be recorded in a ring-buffer,
639 which is available to user space using
641 The order in which the values are saved in the
642 sample are documented in the MMAP Layout subsection below;
644 .I "enum perf_event_sample_format"
649 Records instruction pointer.
652 Records the process and thread IDs.
658 Records an address, if applicable.
661 Record counter values for all events in a group, not just the group leader.
663 .B PERF_SAMPLE_CALLCHAIN
664 Records the callchain (stack backtrace).
667 Records a unique ID for the opened event's group leader.
672 .B PERF_SAMPLE_PERIOD
673 Records the current sampling period.
675 .B PERF_SAMPLE_STREAM_ID
676 Records a unique ID for the opened event.
679 the actual ID is returned, not the group leader.
680 This ID is the same as the one returned by
684 Records additional data, if applicable.
685 Usually returned by tracepoint events.
687 .BR PERF_SAMPLE_BRANCH_STACK " (since Linux 3.4)"
688 .\" commit bce38cd53e5ddba9cb6d708c4ef3d04a4016ec7e
689 This provides a record of recent branches, as provided
690 by CPU branch sampling hardware (such as Intel Last Branch Record).
691 Not all hardware supports this feature.
694 .I branch_sample_type
695 field for how to filter which branches are reported.
697 .BR PERF_SAMPLE_REGS_USER " (since Linux 3.7)"
698 .\" commit 4018994f3d8785275ef0e7391b75c3462c029e56
699 Records the current user-level CPU register state
700 (the values in the process before the kernel was called).
702 .BR PERF_SAMPLE_STACK_USER " (since Linux 3.7)"
703 .\" commit c5ebcedb566ef17bda7b02686e0d658a7bb42ee7
704 Records the user level stack, allowing stack unwinding.
706 .BR PERF_SAMPLE_WEIGHT " (since Linux 3.10)"
707 .\" commit c3feedf2aaf9ac8bad6f19f5d21e4ee0b4b87e9c
708 Records a hardware provided weight value that expresses how
709 costly the sampled event was.
710 This allows the hardware to highlight expensive events in
713 .BR PERF_SAMPLE_DATA_SRC " (since Linux 3.10)"
714 .\" commit d6be9ad6c960f43800a6f118932bc8a5a4eadcd1
715 Records the data source: where in the memory hierarchy
716 the data associated with the sampled instruction came from.
717 This is available only if the underlying hardware
718 supports this feature.
720 .BR PERF_SAMPLE_IDENTIFIER " (since Linux 3.12)"
721 .\" commit ff3d527cebc1fa3707c617bfe9e74f53fcfb0955
724 value in a fixed position in the record,
725 either at the beginning (for sample events) or at the end
726 (if a non-sample event).
728 This was necessary because a sample stream may have
729 records from various different event sources with different
732 Parsing the event stream properly was not possible because the
733 format of the record was needed to find
736 the format could not be found without knowing what
737 event the sample belonged to (causing a circular
741 .B PERF_SAMPLE_IDENTIFIER
742 setting makes the event stream always parsable
745 in a fixed location, even though
746 it means having duplicate
750 .BR PERF_SAMPLE_TRANSACTION " (since Linux 3.13)"
751 .\" commit fdfbbd07e91f8fe387140776f3fd94605f0c89e5
752 Records reasons for transactional memory abort events
753 (for example, from Intel TSX transactional memory support).
757 setting must be greater than 0 and a transactional memory abort
758 event must be measured or no values will be recorded.
759 Also note that some perf_event measurements, such as sampled
760 cycle counting, may cause extraneous aborts (by causing an
761 interrupt during a transaction).
763 .BR PERF_SAMPLE_REGS_INTR " (since Linux 3.19)"
764 .\" commit 60e2364e60e86e81bc6377f49779779e6120977f
765 Records a subset of the current CPU register state
767 .IR sample_regs_intr .
769 .B PERF_SAMPLE_REGS_USER
770 the register values will return kernel register
771 state if the overflow happened while kernel
773 If the CPU supports hardware sampling of
774 register state (i.e. PEBS on Intel x86) and
776 is set higher than zero then the register
777 values returned are those captured by
778 hardware at the time of the sampled
779 instruction's retirement.
783 This field specifies the format of the data returned by
786 .BR perf_event_open ()
790 .B PERF_FORMAT_TOTAL_TIME_ENABLED
794 This can be used to calculate estimated totals if
795 the PMU is overcommitted and multiplexing is happening.
797 .B PERF_FORMAT_TOTAL_TIME_RUNNING
801 This can be used to calculate estimated totals if
802 the PMU is overcommitted and multiplexing is happening.
805 Adds a 64-bit unique value that corresponds to the event group.
808 Allows all counter values in an event group to be read with one read.
814 bit specifies whether the counter starts out disabled or enabled.
815 If disabled, the event can later be enabled by
821 When creating an event group, typically the group leader is initialized
824 set to 1 and any child events are initialized with
829 being 0, the child events will not start until the group leader
835 bit specifies that this counter should count events of child
836 tasks as well as the task specified.
837 This applies only to new children, not to any existing children at
838 the time the counter is created (nor to any new children of
841 Inherit does not work for some combinations of
844 .BR PERF_FORMAT_GROUP .
849 bit specifies that the counter should always be on the CPU if at all
851 It applies only to hardware counters and only to group leaders.
852 If a pinned counter cannot be put onto the CPU (e.g., because there are
853 not enough hardware counters or because of a conflict with some other
854 event), then the counter goes into an 'error' state, where reads
855 return end-of-file (i.e.,
857 returns 0) until the counter is subsequently enabled or disabled.
862 bit specifies that when this counter's group is on the CPU,
863 it should be the only group using the CPU's counters.
864 In the future this may allow monitoring programs to
865 support PMU features that need to run alone so that they do not
866 disrupt other hardware counters.
868 Note that many unexpected situations may prevent events with the
870 bit set from ever running.
871 This includes any users running a system-wide
872 measurement as well as any kernel use of the performance counters
873 (including the commonly enabled NMI Watchdog Timer interface).
876 If this bit is set, the count excludes events that happen in user space.
879 If this bit is set, the count excludes events that happen in kernel-space.
882 If this bit is set, the count excludes events that happen in the
884 This is mainly for PMUs that have built-in support for handling this
886 Extra support is needed for handling hypervisor measurements on most
890 If set, don't count when the CPU is idle.
895 bit enables generation of
902 This allows tools to notice new executable code being mapped into
903 a program (dynamic shared libraries for example)
904 so that addresses can be mapped back to the original code.
909 bit enables tracking of process command name as modified by the
912 .BR prctl (PR_SET_NAME)
913 system calls as well as writing to
914 .IR /proc/self/comm .
917 flag is also successfully set (possible since Linux 3.16),
918 .\" commit 82b897782d10fcc4930c9d4a15b175348fdd2871
920 .B PERF_RECORD_MISC_COMM_EXEC
921 can be used to differentiate the
923 case from the others.
926 If this bit is set, then
930 is used when setting up the sampling interval.
933 This bit enables saving of event counts on context switch for
935 This is meaningful only if the
940 If this bit is set, a counter is automatically
941 enabled after a call to
945 If this bit is set, then
946 fork/exit notifications are included in the ring buffer.
949 If set, have an overflow notification happen when we cross the
952 Otherwise, overflow notifications happen after
956 .IR "precise_ip" " (since Linux 2.6.35)"
957 .\" commit ab608344bcbde4f55ec4cd911b686b0ce3eae076
958 This controls the amount of skid.
959 Skid is how many instructions
960 execute between an event of interest happening and the kernel
961 being able to stop and record the event.
963 better and allows more accurate reporting of which events
964 correspond to which instructions, but hardware is often limited
965 with how small this can be.
967 The values of this are the following:
972 can have arbitrary skid.
976 must have constant skid.
980 requested to have 0 skid.
986 .BR PERF_RECORD_MISC_EXACT_IP .
989 .IR "mmap_data" " (since Linux 2.6.36)"
990 .\" commit 3af9e859281bda7eb7c20b51879cf43aa788ac2e
991 The counterpart of the
994 This enables generation of
998 calls that do not have
1000 set (for example data and SysV shared memory).
1002 .IR "sample_id_all" " (since Linux 2.6.38)"
1003 .\" commit c980d1091810df13f21aabbce545fd98f545bbf7
1004 If set, then TID, TIME, ID, STREAM_ID, and CPU can
1005 additionally be included in
1006 .RB non- PERF_RECORD_SAMPLE s
1007 if the corresponding
1012 .B PERF_SAMPLE_IDENTIFIER
1013 is specified, then an additional ID value is included
1014 as the last value to ease parsing the record stream.
1015 This may lead to the
1017 value appearing twice.
1019 The layout is described by this pseudo-structure:
1023 { u32 pid, tid; } /* if PERF_SAMPLE_TID set */
1024 { u64 time; } /* if PERF_SAMPLE_TIME set */
1025 { u64 id; } /* if PERF_SAMPLE_ID set */
1026 { u64 stream_id;} /* if PERF_SAMPLE_STREAM_ID set */
1027 { u32 cpu, res; } /* if PERF_SAMPLE_CPU set */
1028 { u64 id; } /* if PERF_SAMPLE_IDENTIFIER set */
1032 .IR "exclude_host" " (since Linux 3.2)"
1033 .\" commit a240f76165e6255384d4bdb8139895fac7988799
1034 When conducting measurements that include processes running
1035 VM instances (i.e. have executed a
1038 ) only measure events happening inside a guest instance.
1039 This is only meaningful outside the guests; this setting does
1040 not change counts gathered inside of a guest.
1041 Currently this functionality is x86 only.
1043 .IR "exclude_guest" " (since Linux 3.2)"
1044 .\" commit a240f76165e6255384d4bdb8139895fac7988799
1045 When conducting measurements that include processes running
1046 VM instances (i.e. have executed a
1049 ) do not measure events happening inside guest instances.
1050 This is only meaningful outside the guests; this setting does
1051 not change counts gathered inside of a guest.
1052 Currently this functionality is x86 only.
1054 .IR "exclude_callchain_kernel" " (since Linux 3.7)"
1055 .\" commit d077526485d5c9b12fe85d0b2b3b7041e6bc5f91
1056 Do not include kernel callchains.
1058 .IR "exclude_callchain_user" " (since Linux 3.7)"
1059 .\" commit d077526485d5c9b12fe85d0b2b3b7041e6bc5f91
1060 Do not include user callchains.
1062 .IR "mmap2" " (since Linux 3.16)"
1063 .\" commit 13d7a2410fa637f450a29ecb515ac318ee40c741
1064 .\" This is tricky; was committed during 3.12 development
1065 .\" but right before release was disabled.
1066 .\" So while you could select mmap2 starting with 3.12
1067 .\" it did not work until 3.16
1068 .\" commit a5a5ba72843dd05f991184d6cb9a4471acce1005
1069 Generate an extended executable mmap record that contains enough
1070 additional information to uniquely identify shared mappings.
1073 flag must also be set for this to work.
1075 .IR "comm_exec" " (since Linux 3.16)"
1076 .\" commit 82b897782d10fcc4930c9d4a15b175348fdd2871
1077 This is purely a feature-detection flag, it does not change
1079 If this flag can successfully be set, then, when
1082 .B PERF_RECORD_MISC_COMM_EXEC
1083 flag will be set in the
1085 field of a comm record header if the rename event being
1086 reported was caused by a call to
1088 This allows tools to distinguish between the various
1089 types of process renaming.
1091 .IR "use_clockid" " (since Linux 4.1)"
1092 .\" commit 34f439278cef7b1177f8ce24f9fc81dfc6221d3b
1093 This allows selecting which internal Linux clock to use
1094 when generating timestamps via the
1097 This can make it easier to correlate perf sample times with
1098 timestamps generated by other tools.
1100 .IR "wakeup_events" ", " "wakeup_watermark"
1101 This union sets how many samples
1102 .RI ( wakeup_events )
1104 .RI ( wakeup_watermark )
1105 happen before an overflow notification happens.
1106 Which one is used is selected by the
1112 .B PERF_RECORD_SAMPLE
1114 To receive overflow notification for all
1116 types choose watermark and set
1120 Prior to Linux 3.0 setting
1121 .\" commit f506b3dc0ec454a16d40cab9ee5d75435b39dc50
1123 to 0 resulted in no overflow notifications;
1124 more recent kernels treat 0 the same as 1.
1126 .IR "bp_type" " (since Linux 2.6.33)"
1127 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
1128 This chooses the breakpoint type.
1132 .BR HW_BREAKPOINT_EMPTY
1136 Count when we read the memory location.
1139 Count when we write the memory location.
1141 .BR HW_BREAKPOINT_RW
1142 Count when we read or write the memory location.
1145 Count when we execute code at the memory location.
1147 The values can be combined via a bitwise or, but the
1157 .IR "bp_addr" " (since Linux 2.6.33)"
1158 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
1160 address of the breakpoint.
1161 For execution breakpoints this is the memory address of the instruction
1162 of interest; for read and write breakpoints it is the memory address
1163 of the memory location of interest.
1165 .IR "config1" " (since Linux 2.6.39)"
1166 .\" commit a7e3ed1e470116c9d12c2f778431a481a6be8ab6
1168 is used for setting events that need an extra register or otherwise
1169 do not fit in the regular config field.
1170 Raw OFFCORE_EVENTS on Nehalem/Westmere/SandyBridge use this field
1171 on 3.3 and later kernels.
1173 .IR "bp_len" " (since Linux 2.6.33)"
1174 .\" commit 24f1e32c60c45c89a997c73395b69c8af6f0a84e
1176 is the length of the breakpoint being measured if
1179 .BR PERF_TYPE_BREAKPOINT .
1181 .BR HW_BREAKPOINT_LEN_1 ,
1182 .BR HW_BREAKPOINT_LEN_2 ,
1183 .BR HW_BREAKPOINT_LEN_4 ,
1184 .BR HW_BREAKPOINT_LEN_8 .
1185 For an execution breakpoint, set this to
1188 .IR "config2" " (since Linux 2.6.39)"
1189 .\" commit a7e3ed1e470116c9d12c2f778431a481a6be8ab6
1192 is a further extension of the
1196 .IR "branch_sample_type" " (since Linux 3.4)"
1197 .\" commit bce38cd53e5ddba9cb6d708c4ef3d04a4016ec7e
1199 .B PERF_SAMPLE_BRANCH_STACK
1200 is enabled, then this specifies what branches to include
1201 in the branch record.
1203 The first part of the value is the privilege level, which
1204 is a combination of one of the following values.
1205 If the user does not set privilege level explicitly, the kernel
1206 will use the event's privilege level.
1207 Event and branch privilege levels do not have to match.
1210 .B PERF_SAMPLE_BRANCH_USER
1211 Branch target is in user space.
1213 .B PERF_SAMPLE_BRANCH_KERNEL
1214 Branch target is in kernel space.
1216 .B PERF_SAMPLE_BRANCH_HV
1217 Branch target is in hypervisor.
1219 .B PERF_SAMPLE_BRANCH_PLM_ALL
1220 A convenience value that is the three preceding values ORed together.
1223 In addition to the privilege value, at least one or more of the
1224 following bits must be set.
1227 .B PERF_SAMPLE_BRANCH_ANY
1230 .B PERF_SAMPLE_BRANCH_ANY_CALL
1233 .B PERF_SAMPLE_BRANCH_ANY_RETURN
1236 .B PERF_SAMPLE_BRANCH_IND_CALL
1239 .BR PERF_SAMPLE_BRANCH_COND " (since Linux 3.16)"
1240 .\" commit bac52139f0b7ab31330e98fd87fc5a2664951050
1241 Conditional branches.
1243 .BR PERF_SAMPLE_BRANCH_ABORT_TX " (since Linux 3.11)"
1244 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
1245 Transactional memory aborts.
1247 .BR PERF_SAMPLE_BRANCH_IN_TX " (since Linux 3.11)"
1248 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
1249 Branch in transactional memory transaction.
1251 .BR PERF_SAMPLE_BRANCH_NO_TX " (since Linux 3.11)"
1252 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
1253 Branch not in transactional memory transaction.
1254 .BR PERF_SAMPLE_BRANCH_CALL_STACK " (since Linux 4.1)"
1255 .\" commit 2c44b1936bb3b135a3fac8b3493394d42e51cf70
1256 Branch is part of a hardware generated call stack.
1257 This requires hardware support, currently only found
1258 on Intel x86 Haswell or newer.
1262 .IR "sample_regs_user" " (since Linux 3.7)"
1263 .\" commit 4018994f3d8785275ef0e7391b75c3462c029e56
1264 This bit mask defines the set of user CPU registers to dump on samples.
1265 The layout of the register mask is architecture-specific and
1266 described in the kernel header
1267 .IR arch/ARCH/include/uapi/asm/perf_regs.h .
1269 .IR "sample_stack_user" " (since Linux 3.7)"
1270 .\" commit c5ebcedb566ef17bda7b02686e0d658a7bb42ee7
1271 This defines the size of the user stack to dump if
1272 .B PERF_SAMPLE_STACK_USER
1275 .IR "clockid" " (since Linux 4.1)"
1276 .\" commit 34f439278cef7b1177f8ce24f9fc81dfc6221d3b
1279 is set, then this field selects which internal Linux timer to
1281 The available timers are defined in
1284 .BR CLOCK_MONOTONIC , CLOCK_MONOTONIC_RAW , CLOCK_REALTIME ,
1285 .BR CLOCK_BOOTTIME ", and " CLOCK_TAI
1286 currently supported.
1289 .BR perf_event_open ()
1290 file descriptor has been opened, the values
1291 of the events can be read from the file descriptor.
1292 The values that are there are specified by the
1296 structure at open time.
1298 If you attempt to read into a buffer that is not big enough to hold the
1303 Here is the layout of the data returned by a read:
1306 .B PERF_FORMAT_GROUP
1307 was specified to allow reading all events in a group at once:
1311 struct read_format {
1312 u64 nr; /* The number of events */
1313 u64 time_enabled; /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
1314 u64 time_running; /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
1316 u64 value; /* The value of the event */
1317 u64 id; /* if PERF_FORMAT_ID */
1324 .B PERF_FORMAT_GROUP
1331 struct read_format {
1332 u64 value; /* The value of the event */
1333 u64 time_enabled; /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
1334 u64 time_running; /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
1335 u64 id; /* if PERF_FORMAT_ID */
1340 The values read are as follows:
1343 The number of events in this file descriptor.
1345 .B PERF_FORMAT_GROUP
1348 .IR time_enabled ", " time_running
1349 Total time the event was enabled and running.
1350 Normally these are the same.
1351 If more events are started,
1352 then available counter slots on the PMU, then multiplexing
1353 happens and events run only part of the time.
1358 values can be used to scale an estimated value for the count.
1361 An unsigned 64-bit value containing the counter result.
1364 A globally unique value for this particular event, only present if
1370 .BR perf_event_open ()
1371 in sampled mode, asynchronous events
1372 (like counter overflow or
1375 are logged into a ring-buffer.
1376 This ring-buffer is created and accessed through
1379 The mmap size should be 1+2^n pages, where the first page is a
1381 .RI ( "struct perf_event_mmap_page" )
1382 that contains various
1383 bits of information such as where the ring-buffer head is.
1385 Before kernel 2.6.39, there is a bug that means you must allocate a mmap
1386 ring buffer when sampling even if you do not plan to access it.
1388 The structure of the first metadata mmap page is as follows:
1392 struct perf_event_mmap_page {
1393 __u32 version; /* version number of this structure */
1394 __u32 compat_version; /* lowest version this is compat with */
1395 __u32 lock; /* seqlock for synchronization */
1396 __u32 index; /* hardware counter identifier */
1397 __s64 offset; /* add to hardware counter value */
1398 __u64 time_enabled; /* time event active */
1399 __u64 time_running; /* time event on CPU */
1403 __u64 cap_usr_time / cap_usr_rdpmc / cap_bit0 : 1,
1404 cap_bit0_is_deprecated : 1,
1407 cap_user_time_zero : 1,
1414 __u64 __reserved[120]; /* Pad to 1k */
1415 __u64 data_head; /* head in the data section */
1416 __u64 data_tail; /* user-space written tail */
1417 __u64 data_offset; /* where the buffer starts */
1418 __u64 data_size; /* data buffer size */
1428 The following list describes the fields in the
1429 .I perf_event_mmap_page
1430 structure in more detail:
1433 Version number of this structure.
1436 The lowest version this is compatible with.
1439 A seqlock for synchronization.
1442 A unique hardware counter identifier.
1445 When using rdpmc for reads this offset value
1446 must be added to the one returned by rdpmc to get
1447 the current total event count.
1450 Time the event was active.
1453 Time the event was running.
1455 .IR cap_usr_time " / " cap_usr_rdpmc " / " cap_bit0 " (since Linux 3.4)"
1456 .\" commit c7206205d00ab375839bd6c7ddb247d600693c09
1457 There was a bug in the definition of
1461 from Linux 3.4 until Linux 3.11.
1462 Both bits were defined to point to the same location, so it was
1463 impossible to know if
1469 Starting with Linux 3.12, these are renamed to
1470 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1472 and you should use the
1479 .IR cap_bit0_is_deprecated " (since Linux 3.12)"
1480 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1481 If set, this bit indicates that the kernel supports
1482 the properly separated
1488 If not-set, it indicates an older kernel where
1492 map to the same bit and thus both features should
1493 be used with caution.
1496 .IR cap_user_rdpmc " (since Linux 3.12)"
1497 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1498 If the hardware supports user-space read of performance counters
1499 without syscall (this is the "rdpmc" instruction on x86), then
1500 the following code can be used to do a read:
1504 u32 seq, time_mult, time_shift, idx, width;
1505 u64 count, enabled, running;
1506 u64 cyc, time_offset;
1511 enabled = pc\->time_enabled;
1512 running = pc\->time_running;
1514 if (pc\->cap_usr_time && enabled != running) {
1516 time_offset = pc\->time_offset;
1517 time_mult = pc\->time_mult;
1518 time_shift = pc\->time_shift;
1522 count = pc\->offset;
1524 if (pc\->cap_usr_rdpmc && idx) {
1525 width = pc\->pmc_width;
1526 count += rdpmc(idx \- 1);
1530 } while (pc\->lock != seq);
1534 .IR cap_user_time " (since Linux 3.12)"
1535 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1536 This bit indicates the hardware has a constant, nonstop
1537 timestamp counter (TSC on x86).
1539 .IR cap_user_time_zero " (since Linux 3.12)"
1540 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1541 Indicates the presence of
1543 which allows mapping timestamp values to
1549 this field provides the bit-width of the value
1550 read using the rdpmc or equivalent instruction.
1551 This can be used to sign extend the result like:
1555 pmc <<= 64 \- pmc_width;
1556 pmc >>= 64 \- pmc_width; // signed shift right
1561 .IR time_shift ", " time_mult ", " time_offset
1565 these fields can be used to compute the time
1566 delta since time_enabled (in nanoseconds) using rdtsc or similar.
1571 quot = (cyc >> time_shift);
1572 rem = cyc & ((1 << time_shift) \- 1);
1573 delta = time_offset + quot * time_mult +
1574 ((rem * time_mult) >> time_shift);
1584 seqcount loop described above.
1585 This delta can then be added to
1586 enabled and possible running (if idx), improving the scaling:
1592 quot = count / running;
1593 rem = count % running;
1594 count = quot * enabled + (rem * enabled) / running;
1597 .IR time_zero " (since Linux 3.12)"
1598 .\" commit fa7315871046b9a4c48627905691dbde57e51033
1601 .I cap_usr_time_zero
1602 is set, then the hardware clock (the TSC timestamp counter on x86)
1603 can be calculated from the
1604 .IR time_zero ", " time_mult ", and " time_shift " values:"
1607 time = timestamp - time_zero;
1608 quot = time / time_mult;
1609 rem = time % time_mult;
1610 cyc = (quot << time_shift) + (rem << time_shift) / time_mult;
1616 quot = cyc >> time_shift;
1617 rem = cyc & ((1 << time_shift) - 1);
1618 timestamp = time_zero + quot * time_mult +
1619 ((rem * time_mult) >> time_shift);
1623 This points to the head of the data section.
1624 The value continuously increases, it does not wrap.
1625 The value needs to be manually wrapped by the size of the mmap buffer
1626 before accessing the samples.
1628 On SMP-capable platforms, after reading the
1631 user space should issue an rmb().
1638 value should be written by user space to reflect the last read data.
1639 In this case, the kernel will not overwrite unread data.
1641 .IR data_offset " (since Linux 4.1)"
1642 .\" commit e8c6deac69629c0cb97c3d3272f8631ef17f8f0f
1643 Contains the offset of the location in the mmap buffer
1644 where perf sample data begins.
1646 .IR data_size " (since Linux 4.1)"
1647 .\" commit e8c6deac69629c0cb97c3d3272f8631ef17f8f0f
1648 Contains the size of the perf sample region within
1651 .IR aux_head ", " aux_tail ", " aux_offset ", " aux_size " (since Linux 4.1)
1652 .\" commit 45bfb2e50471abbbfd83d40d28c986078b0d24ff
1653 The AUX region allows mmaping a separate sample buffer for high
1654 bandwidth data streams (separate from the main perf sample buffer).
1655 An example of a high bandwidth stream is instruction tracing support,
1656 as is found in newer Intel processors.
1658 To set up an AUX area, first
1660 needs to be set with an offset greater than
1661 .IR data_offset + data_size
1664 needs to be set to the desired buffer size.
1665 The desired offset and size must be page aligned, and the size
1666 must be a power of two.
1667 These values are then passed to mmap in order to map the AUX buffer.
1668 Pages in the AUX buffer are included as part of the user mlock
1669 rlimit as well as the
1670 .I perf_event_mlock_kb
1674 .IR aux_head " and " aux_tail
1675 ring buffer pointers have the same behavior and ordering
1676 rules as the previous described
1677 .IR data_head " and " data_tail .
1679 The following 2^n ring-buffer pages have the layout described below.
1682 .I perf_event_attr.sample_id_all
1683 is set, then all event types will
1684 have the sample_type selected fields related to where/when (identity)
1685 an event took place (TID, TIME, ID, CPU, STREAM_ID) described in
1686 .B PERF_RECORD_SAMPLE
1687 below, it will be stashed just after the
1688 .I perf_event_header
1689 and the fields already present for the existing
1690 fields, that is, at the end of the payload.
1691 That way a newer perf.data
1692 file will be supported by older perf tools, with these new optional
1693 fields being ignored.
1695 The mmap values start with a header:
1699 struct perf_event_header {
1707 Below, we describe the
1708 .I perf_event_header
1709 fields in more detail.
1710 For ease of reading,
1711 the fields with shorter descriptions are presented first.
1714 This indicates the size of the record.
1719 field contains additional information about the sample.
1721 The CPU mode can be determined from this value by masking with
1722 .B PERF_RECORD_MISC_CPUMODE_MASK
1723 and looking for one of the following (note these are not
1724 bit masks, only one can be set at a time):
1727 .B PERF_RECORD_MISC_CPUMODE_UNKNOWN
1730 .B PERF_RECORD_MISC_KERNEL
1731 Sample happened in the kernel.
1733 .B PERF_RECORD_MISC_USER
1734 Sample happened in user code.
1736 .B PERF_RECORD_MISC_HYPERVISOR
1737 Sample happened in the hypervisor.
1739 .BR PERF_RECORD_MISC_GUEST_KERNEL " (since Linux 2.6.35)"
1740 .\" commit 39447b386c846bbf1c56f6403c5282837486200f
1741 Sample happened in the guest kernel.
1743 .B PERF_RECORD_MISC_GUEST_USER " (since Linux 2.6.35)"
1744 .\" commit 39447b386c846bbf1c56f6403c5282837486200f
1745 Sample happened in guest user code.
1749 In addition, one of the following bits can be set:
1751 .BR PERF_RECORD_MISC_MMAP_DATA " (since Linux 3.10)"
1752 .\" commit 2fe85427e3bf65d791700d065132772fc26e4d75
1753 This is set when the mapping is not executable;
1754 otherwise the mapping is executable.
1756 .BR PERF_RECORD_MISC_COMM_EXEC " (since Linux 3.16)"
1757 .\" commit 82b897782d10fcc4930c9d4a15b175348fdd2871
1760 record on kernels more recent than Linux 3.16
1761 if a process name change was caused by an
1765 .B PERF_RECORD_MISC_MMAP_DATA
1766 since the two values would not be set in the same record.
1768 .B PERF_RECORD_MISC_EXACT_IP
1769 This indicates that the content of
1772 to the actual instruction that triggered the event.
1774 .IR perf_event_attr.precise_ip .
1776 .BR PERF_RECORD_MISC_EXT_RESERVED " (since Linux 2.6.35)"
1777 .\" commit 1676b8a077c352085d52578fb4f29350b58b6e74
1778 This indicates there is extended data available (currently not used).
1784 value is one of the below.
1785 The values in the corresponding record (that follows the header)
1793 The MMAP events record the
1795 mappings so that we can correlate
1796 user-space IPs to code.
1797 They have the following structure:
1802 struct perf_event_header header;
1820 is the address of the allocated memory.
1822 is the length of the allocated memory.
1824 is the page offset of the allocated memory.
1826 is a string describing the backing of the allocated memory.
1830 This record indicates when events are lost.
1835 struct perf_event_header header;
1838 struct sample_id sample_id;
1845 is the unique event ID for the samples that were lost.
1848 is the number of events that were lost.
1852 This record indicates a change in the process name.
1857 struct perf_event_header header;
1861 struct sample_id sample_id;
1874 is a string containing the new name of the process.
1878 This record indicates a process exit event.
1883 struct perf_event_header header;
1887 struct sample_id sample_id;
1892 .BR PERF_RECORD_THROTTLE ", " PERF_RECORD_UNTHROTTLE
1893 This record indicates a throttle/unthrottle event.
1898 struct perf_event_header header;
1902 struct sample_id sample_id;
1908 This record indicates a fork event.
1913 struct perf_event_header header;
1917 struct sample_id sample_id;
1923 This record indicates a read event.
1928 struct perf_event_header header;
1930 struct read_format values;
1931 struct sample_id sample_id;
1936 .B PERF_RECORD_SAMPLE
1937 This record indicates a sample.
1942 struct perf_event_header header;
1943 u64 sample_id; /* if PERF_SAMPLE_IDENTIFIER */
1944 u64 ip; /* if PERF_SAMPLE_IP */
1945 u32 pid, tid; /* if PERF_SAMPLE_TID */
1946 u64 time; /* if PERF_SAMPLE_TIME */
1947 u64 addr; /* if PERF_SAMPLE_ADDR */
1948 u64 id; /* if PERF_SAMPLE_ID */
1949 u64 stream_id; /* if PERF_SAMPLE_STREAM_ID */
1950 u32 cpu, res; /* if PERF_SAMPLE_CPU */
1951 u64 period; /* if PERF_SAMPLE_PERIOD */
1952 struct read_format v; /* if PERF_SAMPLE_READ */
1953 u64 nr; /* if PERF_SAMPLE_CALLCHAIN */
1954 u64 ips[nr]; /* if PERF_SAMPLE_CALLCHAIN */
1955 u32 size; /* if PERF_SAMPLE_RAW */
1956 char data[size]; /* if PERF_SAMPLE_RAW */
1957 u64 bnr; /* if PERF_SAMPLE_BRANCH_STACK */
1958 struct perf_branch_entry lbr[bnr];
1959 /* if PERF_SAMPLE_BRANCH_STACK */
1960 u64 abi; /* if PERF_SAMPLE_REGS_USER */
1961 u64 regs[weight(mask)];
1962 /* if PERF_SAMPLE_REGS_USER */
1963 u64 size; /* if PERF_SAMPLE_STACK_USER */
1964 char data[size]; /* if PERF_SAMPLE_STACK_USER */
1965 u64 dyn_size; /* if PERF_SAMPLE_STACK_USER */
1966 u64 weight; /* if PERF_SAMPLE_WEIGHT */
1967 u64 data_src; /* if PERF_SAMPLE_DATA_SRC */
1968 u64 transaction;/* if PERF_SAMPLE_TRANSACTION */
1969 u64 abi; /* if PERF_SAMPLE_REGS_INTR */
1970 u64 regs[weight(mask)];
1971 /* if PERF_SAMPLE_REGS_INTR */
1978 .B PERF_SAMPLE_IDENTIFIER
1979 is enabled, a 64-bit unique ID is included.
1980 This is a duplication of the
1983 value, but included at the beginning of the sample
1984 so parsers can easily obtain the value.
1989 is enabled, then a 64-bit instruction
1990 pointer value is included.
1995 is enabled, then a 32-bit process ID
1996 and 32-bit thread ID are included.
2001 is enabled, then a 64-bit timestamp
2003 This is obtained via local_clock() which is a hardware timestamp
2004 if available and the jiffies value if not.
2009 is enabled, then a 64-bit address is included.
2010 This is usually the address of a tracepoint,
2011 breakpoint, or software event; otherwise the value is 0.
2016 is enabled, a 64-bit unique ID is included.
2017 If the event is a member of an event group, the group leader ID is returned.
2018 This ID is the same as the one returned by
2019 .BR PERF_FORMAT_ID .
2023 .B PERF_SAMPLE_STREAM_ID
2024 is enabled, a 64-bit unique ID is included.
2027 the actual ID is returned, not the group leader.
2028 This ID is the same as the one returned by
2029 .BR PERF_FORMAT_ID .
2034 is enabled, this is a 32-bit value indicating
2035 which CPU was being used, in addition to a reserved (unused)
2040 .B PERF_SAMPLE_PERIOD
2041 is enabled, a 64-bit value indicating
2042 the current sampling period is written.
2047 is enabled, a structure of type read_format
2048 is included which has values for all events in the event group.
2049 The values included depend on the
2052 .BR perf_event_open ()
2057 .B PERF_SAMPLE_CALLCHAIN
2058 is enabled, then a 64-bit number is included
2059 which indicates how many following 64-bit instruction pointers will
2061 This is the current callchain.
2063 .IR size ", " data[size]
2066 is enabled, then a 32-bit value indicating size
2067 is included followed by an array of 8-bit values of length size.
2068 The values are padded with 0 to have 64-bit alignment.
2070 This RAW record data is opaque with respect to the ABI.
2071 The ABI doesn't make any promises with respect to the stability
2072 of its content, it may vary depending
2073 on event, hardware, and kernel version.
2075 .IR bnr ", " lbr[bnr]
2077 .B PERF_SAMPLE_BRANCH_STACK
2078 is enabled, then a 64-bit value indicating
2079 the number of records is included, followed by
2081 .I perf_branch_entry
2082 structures which each include the fields:
2086 This indicates the source instruction (may not be a branch).
2092 The branch target was mispredicted.
2095 The branch target was predicted.
2097 .IR in_tx " (since Linux 3.11)"
2098 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
2099 The branch was in a transactional memory transaction.
2101 .IR abort " (since Linux 3.11)"
2102 .\" commit 135c5612c460f89657c4698fe2ea753f6f667963
2103 The branch was in an aborted transactional memory transaction.
2106 The entries are from most to least recent, so the first entry
2107 has the most recent branch.
2113 is optional; if not supported, both
2116 The type of branches recorded is specified by the
2117 .I branch_sample_type
2122 .IR abi ", " regs[weight(mask)]
2124 .B PERF_SAMPLE_REGS_USER
2125 is enabled, then the user CPU registers are recorded.
2130 .BR PERF_SAMPLE_REGS_ABI_NONE ", " PERF_SAMPLE_REGS_ABI_32 " or "
2131 .BR PERF_SAMPLE_REGS_ABI_64 .
2135 field is an array of the CPU registers that were specified by
2139 The number of values is the number of bits set in the
2143 .IR size ", " data[size] ", " dyn_size
2145 .B PERF_SAMPLE_STACK_USER
2146 is enabled, then the user stack is recorded.
2147 This can be used to generate stack backtraces.
2149 is the size requested by the user in
2150 .I sample_stack_user
2151 or else the maximum record size.
2153 is the stack data (a raw dump of the memory pointed to by the
2154 stack pointer at the time of sampling).
2156 is the amount of data actually dumped (can be less than
2161 .B PERF_SAMPLE_WEIGHT
2162 is enabled, then a 64-bit value provided by the hardware
2163 is recorded that indicates how costly the event was.
2164 This allows expensive events to stand out more clearly
2169 .B PERF_SAMPLE_DATA_SRC
2170 is enabled, then a 64-bit value is recorded that is made up of
2171 the following fields:
2175 Type of opcode, a bitwise combination of:
2186 .B PERF_MEM_OP_STORE
2189 .B PERF_MEM_OP_PFETCH
2198 Memory hierarchy level hit or miss, a bitwise combination of
2199 the following, shifted left by
2200 .BR PERF_MEM_LVL_SHIFT :
2211 .B PERF_MEM_LVL_MISS
2226 .B PERF_MEM_LVL_LOC_RAM
2229 .B PERF_MEM_LVL_REM_RAM1
2232 .B PERF_MEM_LVL_REM_RAM2
2235 .B PERF_MEM_LVL_REM_CCE1
2238 .B PERF_MEM_LVL_REM_CCE2
2250 Snoop mode, a bitwise combination of the following, shifted left by
2251 .BR PERF_MEM_SNOOP_SHIFT :
2256 .B PERF_MEM_SNOOP_NA
2259 .B PERF_MEM_SNOOP_NONE
2262 .B PERF_MEM_SNOOP_HIT
2265 .B PERF_MEM_SNOOP_MISS
2268 .B PERF_MEM_SNOOP_HITM
2274 Lock instruction, a bitwise combination of the following, shifted left by
2275 .BR PERF_MEM_LOCK_SHIFT :
2283 .B PERF_MEM_LOCK_LOCKED
2289 TLB access hit or miss, a bitwise combination of the following, shifted
2291 .BR PERF_MEM_TLB_SHIFT :
2302 .B PERF_MEM_TLB_MISS
2322 .B PERF_SAMPLE_TRANSACTION
2323 flag is set, then a 64-bit field is recorded describing
2324 the sources of any transactional memory aborts.
2326 The field is a bitwise combination of the following values:
2330 Abort from an elision type transaction (Intel-CPU-specific).
2332 .B PERF_TXN_TRANSACTION
2333 Abort from a generic transaction.
2336 Synchronous abort (related to the reported instruction).
2339 Asynchronous abort (not related to the reported instruction).
2342 Retryable abort (retrying the transaction may have succeeded).
2344 .B PERF_TXN_CONFLICT
2345 Abort due to memory conflicts with other threads.
2347 .B PERF_TXN_CAPACITY_WRITE
2348 Abort due to write capacity overflow.
2350 .B PERF_TXN_CAPACITY_READ
2351 Abort due to read capacity overflow.
2354 In addition, a user-specified abort code can be obtained from
2355 the high 32 bits of the field by shifting right by
2356 .B PERF_TXN_ABORT_SHIFT
2358 .BR PERF_TXN_ABORT_MASK .
2360 .IR abi ", " regs[weight(mask)]
2362 .B PERF_SAMPLE_REGS_INTR
2363 is enabled, then the user CPU registers are recorded.
2368 .BR PERF_SAMPLE_REGS_ABI_NONE ", " PERF_SAMPLE_REGS_ABI_32 " or "
2369 .BR PERF_SAMPLE_REGS_ABI_64 .
2373 field is an array of the CPU registers that were specified by
2377 The number of values is the number of bits set in the
2382 .B PERF_RECORD_MMAP2
2383 This record includes extended information on
2385 calls returning executable mappings.
2386 The format is similar to that of the
2388 record, but includes extra values that allow uniquely identifying
2394 struct perf_event_header header;
2407 struct sample_id sample_id;
2419 is the address of the allocated memory.
2422 is the length of the allocated memory.
2425 is the page offset of the allocated memory.
2428 is the major ID of the underlying device.
2431 is the minor ID of the underlying device.
2434 is the inode number.
2437 is the inode generation.
2440 is the protection information.
2443 is the flags information.
2446 is a string describing the backing of the allocated memory.
2449 .SS Overflow handling
2450 Events can be set to notify when a threshold is crossed,
2451 indicating an overflow.
2452 Overflow conditions can be captured by monitoring the
2453 event file descriptor with
2458 Alternately, a SIGIO signal handler can be created and
2459 the event configured with
2461 to generate SIGIO signals.
2463 Overflows are generated only by sampling events
2465 must have a nonzero value).
2467 There are two ways to generate overflow notifications.
2469 The first is to set a
2473 value that will trigger if a certain number of samples
2474 or bytes have been written to the mmap ring buffer.
2479 The other way is by use of the
2480 .B PERF_EVENT_IOC_REFRESH
2482 This ioctl adds to a counter that decrements each time the event overflows.
2486 once the counter reaches 0
2489 the underlying event is disabled.
2491 Refreshing an event group leader refreshes all siblings and
2492 refreshing with a parameter of 0 currently enables infinite
2494 these behaviors are unsupported and should not be relied on.
2495 .\" See https://lkml.org/lkml/2011/5/24/337
2497 Starting with Linux 3.18,
2498 .\" commit 179033b3e064d2cd3f5f9945e76b0a0f0fbf4883
2500 is indicated if the event being monitored is attached to a different
2501 process and that process exits.
2502 .SS rdpmc instruction
2503 Starting with Linux 3.4 on x86, you can use the
2504 .\" commit c7206205d00ab375839bd6c7ddb247d600693c09
2506 instruction to get low-latency reads without having to enter the kernel.
2509 is not necessarily faster than other methods for reading event values.
2511 Support for this can be detected with the
2513 field in the mmap page; documentation on how
2514 to calculate event values can be found in that section.
2515 .SS perf_event ioctl calls
2517 Various ioctls act on
2518 .BR perf_event_open ()
2521 .B PERF_EVENT_IOC_ENABLE
2522 This enables the individual event or event group specified by the
2523 file descriptor argument.
2526 .B PERF_IOC_FLAG_GROUP
2527 bit is set in the ioctl argument, then all events in a group are
2528 enabled, even if the event specified is not the group leader
2531 .B PERF_EVENT_IOC_DISABLE
2532 This disables the individual counter or event group specified by the
2533 file descriptor argument.
2535 Enabling or disabling the leader of a group enables or disables the
2536 entire group; that is, while the group leader is disabled, none of the
2537 counters in the group will count.
2538 Enabling or disabling a member of a group other than the leader
2539 affects only that counter; disabling a non-leader
2540 stops that counter from counting but doesn't affect any other counter.
2543 .B PERF_IOC_FLAG_GROUP
2544 bit is set in the ioctl argument, then all events in a group are
2545 disabled, even if the event specified is not the group leader
2548 .B PERF_EVENT_IOC_REFRESH
2549 Non-inherited overflow counters can use this
2550 to enable a counter for a number of overflows specified by the argument,
2551 after which it is disabled.
2552 Subsequent calls of this ioctl add the argument value to the current
2554 An overflow notification with
2556 set will happen on each overflow until the
2557 count reaches 0; when that happens a notification with
2559 set is sent and the event is disabled.
2560 Using an argument of 0 is considered undefined behavior.
2562 .B PERF_EVENT_IOC_RESET
2563 Reset the event count specified by the
2564 file descriptor argument to zero.
2565 This resets only the counts; there is no way to reset the
2573 .B PERF_IOC_FLAG_GROUP
2574 bit is set in the ioctl argument, then all events in a group are
2575 reset, even if the event specified is not the group leader
2578 .B PERF_EVENT_IOC_PERIOD
2579 This updates the overflow period for the event.
2581 Since Linux 3.7 (on ARM)
2582 .\" commit 3581fe0ef37ce12ac7a4f74831168352ae848edc
2583 and Linux 3.14 (all other architectures),
2584 .\" commit bad7192b842c83e580747ca57104dd51fe08c223
2585 the new period takes effect immediately.
2586 On older kernels, the new period did not take effect until
2587 after the next overflow.
2589 The argument is a pointer to a 64-bit value containing the
2592 Prior to Linux 2.6.36
2593 .\" commit ad0cf3478de8677f720ee06393b3147819568d6a
2594 this ioctl always failed due to a bug
2598 .B PERF_EVENT_IOC_SET_OUTPUT
2599 This tells the kernel to report event notifications to the specified
2600 file descriptor rather than the default one.
2601 The file descriptors must all be on the same CPU.
2603 The argument specifies the desired file descriptor, or \-1 if
2604 output should be ignored.
2606 .BR PERF_EVENT_IOC_SET_FILTER " (since Linux 2.6.33)"
2607 .\" commit 6fb2915df7f0747d9044da9dbff5b46dc2e20830
2608 This adds an ftrace filter to this event.
2610 The argument is a pointer to the desired ftrace filter.
2612 .BR PERF_EVENT_IOC_ID " (since Linux 3.12)"
2613 .\" commit cf4957f17f2a89984915ea808876d9c82225b862
2614 This returns the event ID value for the given event file descriptor.
2616 The argument is a pointer to a 64-bit unsigned integer
2619 .BR PERF_EVENT_IOC_SET_BPF " (since Linux 4.1)"
2620 .\" commit 2541517c32be2531e0da59dfd7efc1ce844644f5
2621 This allows attaching a Berkeley Packet Filter (BPF)
2622 program to an existing kprobe tracepoint event.
2625 privileges to use this ioctl.
2627 The argument is a BPF program file descriptor that was created by
2632 A process can enable or disable all the event groups that are
2633 attached to it using the
2635 .B PR_TASK_PERF_EVENTS_ENABLE
2637 .B PR_TASK_PERF_EVENTS_DISABLE
2639 This applies to all counters on the calling process, whether created by
2640 this process or by another, and does not affect any counters that this
2641 process has created on other processes.
2642 It enables or disables only
2643 the group leaders, not any other members in the groups.
2644 .SS perf_event related configuration files
2646 .I /proc/sys/kernel/
2649 .I /proc/sys/kernel/perf_event_paranoid
2652 .I perf_event_paranoid
2653 file can be set to restrict access to the performance counters.
2656 allow only user-space measurements.
2658 allow both kernel and user measurements (default).
2660 allow access to CPU-specific data but not raw tracepoint samples.
2665 The existence of the
2666 .I perf_event_paranoid
2667 file is the official method for determining if a kernel supports
2668 .BR perf_event_open ().
2670 .I /proc/sys/kernel/perf_event_max_sample_rate
2672 This sets the maximum sample rate.
2673 Setting this too high can allow
2674 users to sample at a rate that impacts overall machine performance
2675 and potentially lock up the machine.
2676 The default value is
2677 100000 (samples per second).
2679 .I /proc/sys/kernel/perf_event_mlock_kb
2681 Maximum number of pages an unprivileged user can
2683 The default is 516 (kB).
2687 .I /sys/bus/event_source/devices/
2689 Since Linux 2.6.34, the kernel supports having multiple PMUs
2690 available for monitoring.
2691 Information on how to program these PMUs can be found under
2692 .IR /sys/bus/event_source/devices/ .
2693 Each subdirectory corresponds to a different PMU.
2695 .IR /sys/bus/event_source/devices/*/type " (since Linux 2.6.38)"
2696 .\" commit abe43400579d5de0078c2d3a760e6598e183f871
2697 This contains an integer that can be used in the
2701 to indicate that you wish to use this PMU.
2703 .IR /sys/bus/event_source/devices/*/rdpmc " (since Linux 3.4)"
2704 .\" commit 0c9d42ed4cee2aa1dfc3a260b741baae8615744f
2705 If this file is 1, then direct user-space access to the
2706 performance counter registers is allowed via the rdpmc instruction.
2707 This can be disabled by echoing 0 to the file.
2709 .IR /sys/bus/event_source/devices/*/format/ " (since Linux 3.4)"
2710 .\" commit 641cc938815dfd09f8fa1ec72deb814f0938ac33
2711 This subdirectory contains information on the architecture-specific
2712 subfields available for programming the various
2718 The content of each file is the name of the config field, followed
2719 by a colon, followed by a series of integer bit ranges separated by
2721 For example, the file
2723 may contain the value
2724 .I config1:1,6-10,44
2725 which indicates that event is an attribute that occupies bits 1,6-10, and 44
2727 .IR perf_event_attr::config1 .
2729 .IR /sys/bus/event_source/devices/*/events/ " (since Linux 3.4)"
2730 .\" commit 641cc938815dfd09f8fa1ec72deb814f0938ac33
2731 This subdirectory contains files with predefined events.
2732 The contents are strings describing the event settings
2733 expressed in terms of the fields found in the previously mentioned
2736 These are not necessarily complete lists of all events supported by
2737 a PMU, but usually a subset of events deemed useful or interesting.
2739 The content of each file is a list of attribute names
2740 separated by commas.
2741 Each entry has an optional value (either hex or decimal).
2742 If no value is specified, then it is assumed to be a single-bit
2743 field with a value of 1.
2744 An example entry may look like this:
2745 .IR event=0x2,inv,ldlat=3 .
2747 .I /sys/bus/event_source/devices/*/uevent
2748 This file is the standard kernel device interface
2749 for injecting hotplug events.
2751 .IR /sys/bus/event_source/devices/*/cpumask " (since Linux 3.7)"
2752 .\" commit 314d9f63f385096580e9e2a06eaa0745d92fe4ac
2755 file contains a comma-separated list of integers that
2756 indicate a representative CPU number for each socket (package)
2758 This is needed when setting up uncore or northbridge events, as
2759 those PMUs present socket-wide events.
2762 .BR perf_event_open ()
2763 returns the new file descriptor, or \-1 if an error occurred
2766 is set appropriately).
2768 The errors returned by
2769 .BR perf_event_open ()
2770 can be inconsistent, and may
2771 vary across processor architectures and performance monitoring units.
2779 .BR PERF_ATTR_SIZE_VER0 ),
2780 too big (larger than the page size),
2781 or larger than the kernel supports and the extra bytes are not zero.
2787 field is overwritten by the kernel to be the size of the structure
2791 Returned when the requested event requires
2793 permissions (or a more permissive perf_event paranoid setting).
2794 Some common cases where an unprivileged process
2795 may encounter this error:
2796 attaching to a process owned by a different user;
2797 monitoring all processes on a given CPU (i.e., specifying the
2802 when the paranoid setting requires it.
2807 file descriptor is not valid, or, if
2808 .B PERF_FLAG_PID_CGROUP
2810 the cgroup file descriptor in
2817 pointer points at an invalid memory address.
2820 Returned if the specified event is invalid.
2821 There are many possible reasons for this.
2822 A not-exhaustive list:
2824 is higher than the maximum setting;
2827 to monitor does not exist;
2834 value is out of range;
2838 set and the event is not a group leader;
2841 values are out of range or set reserved bits;
2842 the generic event selected is not supported; or
2843 there is not enough room to add the selected event.
2846 Each opened event uses one file descriptor.
2847 If a large number of events are opened the per-user file
2848 descriptor limit (often 1024) will be hit and no more
2849 events can be created.
2852 Returned when the event involves a feature not supported
2858 setting is not valid.
2859 This error is also returned for
2860 some unsupported generic events.
2863 Prior to Linux 3.3, if there was not enough room for the event,
2864 .\" commit aa2bc1ade59003a379ffc485d6da2d92ea3370a6
2867 In Linux 3.3, this was changed to
2870 is still returned if you try to add more breakpoint events
2871 than supported by the hardware.
2875 .B PERF_SAMPLE_STACK_USER
2878 and it is not supported by hardware.
2881 Returned if an event requiring a specific hardware feature is
2882 requested but there is no hardware support.
2883 This includes requesting low-skid events if not supported,
2884 branch tracing if it is not available, sampling if no PMU
2885 interrupt is available, and branch stacks for software events.
2888 Returned on many (but not all) architectures when an unsupported
2889 .IR exclude_hv ", " exclude_idle ", " exclude_user ", or " exclude_kernel
2890 setting is specified.
2892 It can also happen, as with
2894 when the requested event requires
2896 permissions (or a more permissive perf_event paranoid setting).
2897 This includes setting a breakpoint on a kernel address,
2898 and (since Linux 3.13) setting a kernel function-trace tracepoint.
2899 .\" commit a4e95fc2cbb31d70a65beffeaf8773f881328c34
2902 Returned if attempting to attach to a process that does not exist.
2904 .BR perf_event_open ()
2905 was introduced in Linux 2.6.31 but was called
2906 .\" commit 0793a61d4df8daeac6492dbf8d2f3e5713caae5e
2907 .BR perf_counter_open ().
2908 It was renamed in Linux 2.6.32.
2909 .\" commit cdd6c482c9ff9c55475ee7392ec8f672eddb7be6
2912 .BR perf_event_open ()
2913 system call Linux- specific
2914 and should not be used in programs intended to be portable.
2916 Glibc does not provide a wrapper for this system call; call it using
2918 See the example below.
2920 The official way of knowing if
2921 .BR perf_event_open ()
2922 support is enabled is checking
2923 for the existence of the file
2924 .IR /proc/sys/kernel/perf_event_paranoid .
2930 is needed to properly get overflow signals in threads.
2931 This was introduced in Linux 2.6.32.
2932 .\" commit ba0a6c9f6fceed11c6a99e8326f0477fe383e6b5
2934 Prior to Linux 2.6.33 (at least for x86),
2935 .\" commit b690081d4d3f6a23541493f1682835c3cd5c54a1
2936 the kernel did not check
2937 if events could be scheduled together until read time.
2938 The same happens on all known kernels if the NMI watchdog is enabled.
2939 This means to see if a given set of events works you have to
2940 .BR perf_event_open (),
2941 start, then read before you know for sure you
2942 can get valid measurements.
2944 Prior to Linux 2.6.34, event constraints were not enforced by the kernel.
2945 In that case, some events would silently return "0" if the kernel
2946 scheduled them in an improper counter slot.
2947 .\" FIXME: cannot find a kernel commit for this one
2949 Prior to Linux 2.6.34, there was a bug when multiplexing where the
2950 wrong results could be returned.
2951 .\" commit 45e16a6834b6af098702e5ea6c9a40de42ff77d8
2953 Kernels from Linux 2.6.35 to Linux 2.6.39 can quickly crash the kernel if
2954 "inherit" is enabled and many threads are started.
2955 .\" commit 38b435b16c36b0d863efcf3f07b34a6fac9873fd
2957 Prior to Linux 2.6.35,
2958 .\" commit 050735b08ca8a016bbace4445fa025b88fee770b
2959 .B PERF_FORMAT_GROUP
2960 did not work with attached processes.
2962 There is a bug in the kernel code between
2963 Linux 2.6.36 and Linux 3.0 that ignores the
2964 "watermark" field and acts as if a wakeup_event
2965 was chosen if the union has a
2966 nonzero value in it.
2967 .\" commit 4ec8363dfc1451f8c8f86825731fe712798ada02
2969 From Linux 2.6.31 to Linux 3.4, the
2970 .B PERF_IOC_FLAG_GROUP
2971 ioctl argument was broken and would repeatedly operate
2972 on the event specified rather than iterating across
2973 all sibling events in a group.
2974 .\" commit 724b6daa13e100067c30cfc4d1ad06629609dc4e
2976 From Linux 3.4 to Linux 3.11, the mmap
2977 .\" commit fa7315871046b9a4c48627905691dbde57e51033
2981 bits mapped to the same location.
2982 Code should migrate to the new
2988 Always double-check your results!
2989 Various generalized events have had wrong values.
2990 For example, retired branches measured
2991 the wrong thing on AMD machines until Linux 2.6.35.
2992 .\" commit f287d332ce835f77a4f5077d2c0ef1e3f9ea42d2
2994 The following is a short example that measures the total
2995 instruction count of a call to
3003 #include <sys/ioctl.h>
3004 #include <linux/perf_event.h>
3005 #include <asm/unistd.h>
3008 perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
3009 int cpu, int group_fd, unsigned long flags)
3013 ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
3019 main(int argc, char **argv)
3021 struct perf_event_attr pe;
3025 memset(&pe, 0, sizeof(struct perf_event_attr));
3026 pe.type = PERF_TYPE_HARDWARE;
3027 pe.size = sizeof(struct perf_event_attr);
3028 pe.config = PERF_COUNT_HW_INSTRUCTIONS;
3030 pe.exclude_kernel = 1;
3033 fd = perf_event_open(&pe, 0, \-1, \-1, 0);
3035 fprintf(stderr, "Error opening leader %llx\\n", pe.config);
3039 ioctl(fd, PERF_EVENT_IOC_RESET, 0);
3040 ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
3042 printf("Measuring instruction count for this printf\\n");
3044 ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
3045 read(fd, &count, sizeof(long long));
3047 printf("Used %lld instructions\\n", count);