]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/perf_event_open.2
getent.1, _syscall.2, acct.2, adjtimex.2, bdflush.2, brk.2, cacheflush.2, getsid...
[thirdparty/man-pages.git] / man2 / perf_event_open.2
CommitLineData
f2b1d720
MK
1.\" Copyright (c) 2012, Vincent Weaver
2.\"
1dd72f9c 3.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
f2b1d720
MK
4.\" This is free documentation; you can redistribute it and/or
5.\" modify it under the terms of the GNU General Public License as
6.\" published by the Free Software Foundation; either version 2 of
7.\" the License, or (at your option) any later version.
8.\"
9.\" The GNU General Public License's references to "object code"
10.\" and "executables" are to be interpreted as the output of any
11.\" document formatting or typesetting system, including
12.\" intermediate and printed output.
13.\"
14.\" This manual is distributed in the hope that it will be useful,
15.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
16.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17.\" GNU General Public License for more details.
18.\"
19.\" You should have received a copy of the GNU General Public
20.\" License along with this manual; if not, see
21.\" <http://www.gnu.org/licenses/>.
6a8d8745 22.\" %%%LICENSE_END
f2b1d720
MK
23.\"
24.\" This document is based on the perf_event.h header file, the
25.\" tools/perf/design.txt file, and a lot of bitter experience.
26.\"
27.TH PERF_EVENT_OPEN 2 2013-02-04 "Linux" "Linux Programmer's Manual"
28.SH NAME
29perf_event_open \- set up performance monitoring
30.SH SYNOPSIS
31.nf
32.B #include <linux/perf_event.h>
33.B #include <linux/hw_breakpoint.h>
34.sp
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 );
38.fi
39
40.IR Note :
41There is no glibc wrapper for this system call; see NOTES.
42.SH DESCRIPTION
43Given a list of parameters,
44.BR perf_event_open ()
45returns a file descriptor, for use in subsequent system calls
46.RB ( read "(2), " mmap "(2), " prctl "(2), " fcntl "(2), etc.)."
47.PP
48A call to
49.BR perf_event_open ()
50creates a file descriptor that allows measuring performance
51information.
52Each file descriptor corresponds to one
53event that is measured; these can be grouped together
54to measure multiple events simultaneously.
55.PP
56Events can be enabled and disabled in two ways: via
57.BR ioctl (2)
58and via
59.BR prctl (2) .
60When an event is disabled it does not count or generate overflows but does
61continue to exist and maintain its count value.
62.PP
63Events come in two flavors: counting and sampled.
64A
65.I counting
66event is one that is used for counting the aggregate number of events
67that occur.
68In general, counting event results are gathered with a
69.BR read (2)
70call.
71A
72.I sampling
73event periodically writes measurements to a buffer that can then
74be accessed via
75.BR mmap (2) .
76.SS Arguments
77.P
78The argument
79.I pid
80allows events to be attached to processes in various ways.
81If
82.I pid
83is 0, measurements happen on the current thread, if
84.I pid
85is greater than 0, the process indicated by
86.I pid
87is measured, and if
88.I pid
89is \-1, all processes are counted.
90
91The
92.I cpu
93argument allows measurements to be specific to a CPU.
94If
95.I cpu
96is greater than or equal to 0,
97measurements are restricted to the specified CPU;
98if
99.I cpu
100is \-1, the events are measured on all CPUs.
101.P
102Note that the combination of
103.IR pid " == \-1"
104and
105.IR cpu " == \-1"
106is not valid.
107.P
108A
109.IR pid " > 0"
110and
111.IR cpu " == \-1"
112setting measures per-process and follows that process to whatever CPU the
113process gets scheduled to.
114Per-process events can be created by any user.
115.P
116A
117.IR pid " == \-1"
118and
119.IR cpu " >= 0"
120setting is per-CPU and measures all processes on the specified CPU.
121Per-CPU events need the
122.B CAP_SYS_ADMIN
123capability or a
124.I /proc/sys/kernel/perf_event_paranoid
125value of less than 1.
126.P
127The
128.I group_fd
129argument allows event groups to be created.
130An event group has one event which is the group leader.
131The leader is created first, with
132.IR group_fd " = \-1."
133The rest of the group members are created with subsequent
134.BR perf_event_open ()
135calls with
136.IR group_fd
137being set to the fd of the group leader.
138(A single event on its own is created with
139.IR group_fd " = \-1"
140and is considered to be a group with only 1 member.)
141An event group is scheduled onto the CPU as a unit: it will only
142be put onto the CPU if all of the events in the group can be put onto
143the CPU.
144This means that the values of the member events can be
145meaningfully compared, added, divided (to get ratios), etc., with each
146other, since they have counted events for the same set of executed
147instructions.
148.P
149The
150.I flags
151argument takes one of the following values:
152.TP
153.BR PERF_FLAG_FD_NO_GROUP
154.\" FIXME The following sentence is unclear
155This flag allows creating an event as part of an event group but
156having no group leader.
157It is unclear why this is useful.
158.\" FIXME So, why is it useful?
159.TP
160.BR PERF_FLAG_FD_OUTPUT
161This flag re-routes the output from an event to the group leader.
162.TP
163.BR PERF_FLAG_PID_CGROUP " (Since Linux 2.6.39)."
164This flag activates per-container system-wide monitoring.
165A container
166is an abstraction that isolates a set of resources for finer grain
167control (CPUs, memory, etc...).
168In this mode, the event is measured
169only if the thread running on the monitored CPU belongs to the designated
170container (cgroup).
171The cgroup is identified by passing a file descriptor
172opened on its directory in the cgroupfs filesystem.
173For instance, if the
174cgroup to monitor is called
175.IR test ,
176then a file descriptor opened on
177.I /dev/cgroup/test
178(assuming cgroupfs is mounted on
179.IR /dev/cgroup )
180must be passed as the
181.I pid
182parameter.
183cgroup monitoring is only available
184for system-wide events and may therefore require extra permissions.
185.P
186The
187.I perf_event_attr
188structure provides detailed configuration information
189for the event being created.
190
191.in +4n
192.nf
193struct perf_event_attr {
194 __u32 type; /* Type of event */
195 __u32 size; /* Size of attribute structure */
196 __u64 config; /* Type-specific configuration */
197
198 union {
199 __u64 sample_period; /* Period of sampling */
200 __u64 sample_freq; /* Frequency of sampling */
201 };
202
203 __u64 sample_type; /* Specifies values included in sample */
204 __u64 read_format; /* Specifies values returned in read */
205
7db515ef
MK
206 __u64 disabled : 1, /* off by default */
207 inherit : 1, /* children inherit it */
208 pinned : 1, /* must always be on PMU */
209 exclusive : 1, /* only group on PMU */
210 exclude_user : 1, /* don't count user */
211 exclude_kernel : 1, /* don't count kernel */
f2b1d720 212 exclude_hv : 1, /* don't count hypervisor */
7db515ef
MK
213 exclude_idle : 1, /* don't count when idle */
214 mmap : 1, /* include mmap data */
215 comm : 1, /* include comm data */
216 freq : 1, /* use freq, not period */
217 inherit_stat : 1, /* per task counts */
218 enable_on_exec : 1, /* next exec enables */
219 task : 1, /* trace fork/exit */
220 watermark : 1, /* wakeup_watermark */
221 precise_ip : 2, /* skid constraint */
222 mmap_data : 1, /* non-exec mmap data */
f2b1d720 223 sample_id_all : 1, /* sample_type all events */
7db515ef
MK
224 exclude_host : 1, /* don't count in host */
225 exclude_guest : 1, /* don't count in guest */
226 exclude_callchain_kernel : 1,
227 /* exclude kernel callchains */
228 exclude_callchain_user : 1,
229 /* exclude user callchains */
f2b1d720
MK
230 __reserved_1 : 41;
231
232 union {
233 __u32 wakeup_events; /* wakeup every n events */
7db515ef 234 __u32 wakeup_watermark; /* bytes before wakeup */
f2b1d720
MK
235 };
236
237 __u32 bp_type; /* breakpoint type */
238
239 union {
240 __u64 bp_addr; /* breakpoint address */
241 __u64 config1; /* extension of config */
242 };
243
244 union {
245 __u64 bp_len; /* breakpoint length */
246 __u64 config2; /* extension of config1 */
247 };
7db515ef
MK
248 __u64 branch_sample_type; /* enum perf_branch_sample_type */
249 __u64 sample_regs_user; /* user regs to dump on samples */
250 __u32 sample_stack_user; /* size of stack to dump on
251 samples */
252 __u32 __reserved_2; /* Align to u64 */
f2b1d720
MK
253
254};
255.fi
256.in
257
258The fields of the
259.I perf_event_attr
260structure are described in more detail below:
f2b1d720
MK
261.TP
262.I type
263This field specifies the overall event type.
264It has one of the following values:
265.RS
266.TP
267.B PERF_TYPE_HARDWARE
268This indicates one of the "generalized" hardware events provided
269by the kernel.
270See the
271.I config
272field definition for more details.
273.TP
274.B PERF_TYPE_SOFTWARE
275This indicates one of the software-defined events provided by the kernel
276(even if no hardware support is available).
277.TP
278.B PERF_TYPE_TRACEPOINT
279This indicates a tracepoint
280provided by the kernel tracepoint infrastructure.
281.TP
282.B PERF_TYPE_HW_CACHE
283This indicates a hardware cache event.
284This has a special encoding, described in the
285.I config
286field definition.
287.TP
288.B PERF_TYPE_RAW
289This indicates a "raw" implementation-specific event in the
290.IR config " field."
291.TP
292.BR PERF_TYPE_BREAKPOINT " (Since Linux 2.6.33)"
293This indicates a hardware breakpoint as provided by the CPU.
294Breakpoints can be read/write accesses to an address as well as
295execution of an instruction address.
296.TP
297.RB "dynamic PMU"
298Since Linux 2.6.39,
7db515ef 299.BR perf_event_open ()
f2b1d720
MK
300can support multiple PMUs.
301To enable this, a value exported by the kernel can be used in the
302.I type
303field to indicate which PMU to use.
304The value to use can be found in the sysfs filesystem:
305there is a subdirectory per PMU instance under
306.IR /sys/bus/event_source/devices .
307In each sub-directory there is a
308.I type
309file whose content is an integer that can be used in the
310.I type
311field.
312For instance,
313.I /sys/bus/event_source/devices/cpu/type
314contains the value for the core CPU PMU, which is usually 4.
315.RE
f2b1d720
MK
316.TP
317.I "size"
318The size of the
319.I perf_event_attr
320structure for forward/backward compatibility.
321Set this using
322.I sizeof(struct perf_event_attr)
323to allow the kernel to see
324the struct size at the time of compilation.
325
326The related define
327.B PERF_ATTR_SIZE_VER0
328is set to 64; this was the size of the first published struct.
329.B PERF_ATTR_SIZE_VER1
330is 72, corresponding to the addition of breakpoints in Linux 2.6.33.
331.B PERF_ATTR_SIZE_VER2
332is 80 corresponding to the addition of branch sampling in Linux 3.4.
333.B PERF_ATR_SIZE_VER3
334is 96 corresponding to the addition
7ede2f66
DP
335of
336.I sample_regs_user
337and
338.I sample_stack_user
339in Linux 3.7.
f2b1d720
MK
340.TP
341.I "config"
342This specifies which event you want, in conjunction with
343the
344.I type
345field.
346The
347.IR config1 " and " config2
348fields are also taken into account in cases where 64 bits is not
349enough to fully specify the event.
350The encoding of these fields are event dependent.
351
352The most significant bit (bit 63) of
353.I config
354signifies CPU-specific (raw) counter configuration data;
355if the most significant bit is unset, the next 7 bits are an event
356type and the rest of the bits are the event identifier.
357
358There are various ways to set the
359.I config
360field that are dependent on the value of the previously
361described
362.I type
363field.
364What follows are various possible settings for
365.I config
366separated out by
367.IR type .
368
369If
370.I type
371is
372.BR PERF_TYPE_HARDWARE ,
373we are measuring one of the generalized hardware CPU events.
374Not all of these are available on all platforms.
375Set
376.I config
377to one of the following:
378.RS 12
379.TP
380.B PERF_COUNT_HW_CPU_CYCLES
381Total cycles.
382Be wary of what happens during CPU frequency scaling
383.TP
384.B PERF_COUNT_HW_INSTRUCTIONS
385Retired instructions.
386Be careful, these can be affected by various
387issues, most notably hardware interrupt counts
388.TP
389.B PERF_COUNT_HW_CACHE_REFERENCES
390Cache accesses.
391Usually this indicates Last Level Cache accesses but this may
392vary depending on your CPU.
393This may include prefetches and coherency messages; again this
394depends on the design of your CPU.
395.TP
396.B PERF_COUNT_HW_CACHE_MISSES
397Cache misses.
398Usually this indicates Last Level Cache misses; this is intended to be
399used in conjunction with the
400.B PERF_COUNT_HW_CACHE_REFERENCES
401event to calculate cache miss rates.
402.TP
403.B PERF_COUNT_HW_BRANCH_INSTRUCTIONS
404Retired branch instructions.
405Prior to Linux 2.6.34, this used
406the wrong event on AMD processors.
407.TP
408.B PERF_COUNT_HW_BRANCH_MISSES
409Mispredicted branch instructions.
410.TP
411.B PERF_COUNT_HW_BUS_CYCLES
412Bus cycles, which can be different from total cycles.
413.TP
414.BR PERF_COUNT_HW_STALLED_CYCLES_FRONTEND " (Since Linux 3.0)"
415Stalled cycles during issue.
416.TP
417.BR PERF_COUNT_HW_STALLED_CYCLES_BACKEND " (Since Linux 3.0)"
418Stalled cycles during retirement.
419.TP
420.BR PERF_COUNT_HW_REF_CPU_CYCLES " (Since Linux 3.3)"
421Total cycles; not affected by CPU frequency scaling.
422.RE
423.IP
424If
425.I type
426is
427.BR PERF_TYPE_SOFTWARE ,
428we are measuring software events provided by the kernel.
429Set
430.I config
431to one of the following:
432.RS 12
433.TP
434.B PERF_COUNT_SW_CPU_CLOCK
435This reports the CPU clock, a high-resolution per-CPU timer.
436.TP
437.B PERF_COUNT_SW_TASK_CLOCK
438This reports a clock count specific to the task that is running.
439.TP
440.B PERF_COUNT_SW_PAGE_FAULTS
441This reports the number of page faults.
442.TP
443.B PERF_COUNT_SW_CONTEXT_SWITCHES
444This counts context switches.
445Until Linux 2.6.34, these were all reported as user-space
446events, after that they are reported as happening in the kernel.
447.TP
448.B PERF_COUNT_SW_CPU_MIGRATIONS
449This reports the number of times the process
450has migrated to a new CPU.
451.TP
452.B PERF_COUNT_SW_PAGE_FAULTS_MIN
453This counts the number of minor page faults.
454These did not require disk I/O to handle.
455.TP
456.B PERF_COUNT_SW_PAGE_FAULTS_MAJ
457This counts the number of major page faults.
458These required disk I/O to handle.
459.TP
460.BR PERF_COUNT_SW_ALIGNMENT_FAULTS " (Since Linux 2.6.33)"
461This counts the number of alignment faults.
462These happen when unaligned memory accesses happen; the kernel
463can handle these but it reduces performance.
464This only happens on some architectures (never on x86).
465.TP
466.BR PERF_COUNT_SW_EMULATION_FAULTS " (Since Linux 2.6.33)"
467This counts the number of emulation faults.
468The kernel sometimes traps on unimplemented instructions
7db515ef 469and emulates them for user space.
f2b1d720
MK
470This can negatively impact performance.
471.RE
472.RE
473
f2b1d720
MK
474.RS
475If
476.I type
477is
478.BR PERF_TYPE_TRACEPOINT ,
479then we are measuring kernel tracepoints.
480The value to use in
481.I config
482can be obtained from under debugfs
483.I tracing/events/*/*/id
484if ftrace is enabled in the kernel.
f2b1d720 485.RE
f2b1d720
MK
486.RS
487If
488.I type
489is
490.BR PERF_TYPE_HW_CACHE ,
491then we are measuring a hardware CPU cache event.
492To calculate the appropriate
493.I config
494value use the following equation:
495.RS 4
496.nf
497
498 (perf_hw_cache_id) | (perf_hw_cache_op_id << 8) |
499 (perf_hw_cache_op_result_id << 16)
500.fi
501.P
502where
503.I perf_hw_cache_id
504is one of:
7db515ef 505.RS 4
f2b1d720
MK
506.TP
507.B PERF_COUNT_HW_CACHE_L1D
508for measuring Level 1 Data Cache
509.TP
510.B PERF_COUNT_HW_CACHE_L1I
511for measuring Level 1 Instruction Cache
512.TP
513.B PERF_COUNT_HW_CACHE_LL
514for measuring Last-Level Cache
515.TP
516.B PERF_COUNT_HW_CACHE_DTLB
517for measuring the Data TLB
518.TP
519.B PERF_COUNT_HW_CACHE_ITLB
520for measuring the Instruction TLB
521.TP
522.B PERF_COUNT_HW_CACHE_BPU
523for measuring the branch prediction unit
524.TP
525.BR PERF_COUNT_HW_CACHE_NODE " (Since Linux 3.0)"
526for measuring local memory accesses
527.RE
f2b1d720
MK
528.P
529and
530.I perf_hw_cache_op_id
531is one of
7db515ef 532.RS 4
f2b1d720
MK
533.TP
534.B PERF_COUNT_HW_CACHE_OP_READ
535for read accesses
536.TP
537.B PERF_COUNT_HW_CACHE_OP_WRITE
538for write accesses
539.TP
540.B PERF_COUNT_HW_CACHE_OP_PREFETCH
541for prefetch accesses
542.RE
f2b1d720
MK
543.P
544and
545.I perf_hw_cache_op_result_id
546is one of
7db515ef 547.RS 4
f2b1d720
MK
548.TP
549.B PERF_COUNT_HW_CACHE_RESULT_ACCESS
550to measure accesses
551.TP
552.B PERF_COUNT_HW_CACHE_RESULT_MISS
553to measure misses
554.RE
555.RE
556
557If
558.I type
559is
560.BR PERF_TYPE_RAW ,
561then a custom "raw"
562.I config
563value is needed.
564Most CPUs support events that are not covered by the "generalized" events.
565These are implementation defined; see your CPU manual (for example
566the Intel Volume 3B documentation or the AMD BIOS and Kernel Developer
567Guide).
568The libpfm4 library can be used to translate from the name in the
569architectural manuals to the raw hex value
570.BR perf_event_open ()
571expects in this field.
572
573If
574.I type
575is
576.BR PERF_TYPE_BREAKPOINT ,
577then leave
578.I config
579set to zero.
580Its parameters are set in other places.
581.RE
582.TP
583.IR sample_period ", " sample_freq
584A "sampling" counter is one that generates an interrupt
585every N events, where N is given by
586.IR sample_period .
587A sampling counter has
588.IR sample_period " > 0."
589When an overflow interrupt occurs, requested data is recorded
590in the mmap buffer.
591The
592.I sample_type
593field controls what data is recorded on each interrupt.
594
595.I sample_freq
596can be used if you wish to use frequency rather than period.
597In this case you set the
598.I freq
599flag.
600The kernel will adjust the sampling period
601to try and achieve the desired rate.
602The rate of adjustment is a
603timer tick.
f2b1d720
MK
604.TP
605.I "sample_type"
606The various bits in this field specify which values to include
607in the sample.
608They will be recorded in a ring-buffer,
ad73a2cc 609which is available to user space using
f2b1d720
MK
610.BR mmap (2).
611The order in which the values are saved in the
612sample are documented in the MMAP Layout subsection below;
613it is not the
614.I "enum perf_event_sample_format"
615order.
616.RS
617.TP
618.B PERF_SAMPLE_IP
619Records instruction pointer.
620.TP
621.B PERF_SAMPLE_TID
7db515ef 622Records the process and thread IDs.
f2b1d720
MK
623.TP
624.B PERF_SAMPLE_TIME
625Records a timestamp.
626.TP
627.B PERF_SAMPLE_ADDR
628Records an address, if applicable.
629.TP
630.B PERF_SAMPLE_READ
631Record counter values for all events in a group, not just the group leader.
632.TP
633.B PERF_SAMPLE_CALLCHAIN
634Records the callchain (stack backtrace).
635.TP
636.B PERF_SAMPLE_ID
637Records a unique ID for the opened event's group leader.
638.TP
639.B PERF_SAMPLE_CPU
640Records CPU number.
641.TP
642.B PERF_SAMPLE_PERIOD
643Records the current sampling period.
644.TP
645.B PERF_SAMPLE_STREAM_ID
646Records a unique ID for the opened event.
647Unlike
648.B PERF_SAMPLE_ID
649the actual ID is returned, not the group leader.
650This ID is the same as the one returned by PERF_FORMAT_ID.
651.TP
652.B PERF_SAMPLE_RAW
653Records additional data, if applicable.
654Usually returned by tracepoint events.
655.TP
656.BR PERF_SAMPLE_BRANCH_STACK " (Since Linux 3.4)"
7db515ef
MK
657Records the branch stack.
658See branch_sample_type.
f2b1d720
MK
659.TP
660.BR PERF_SAMPLE_REGS_USER " (Since Linux 3.7)"
661Records the current register state.
662.TP
663.BR PERF_SAMPLE_STACK_USER " (Since Linux 3.7)"
664[To be documented]
665.RE
f2b1d720
MK
666.TP
667.IR "read_format"
668This field specifies the format of the data returned by
669.BR read (2)
670on a
7db515ef 671.BR perf_event_open ()
f2b1d720
MK
672file descriptor.
673.RS
674.TP
675.B PERF_FORMAT_TOTAL_TIME_ENABLED
7ede2f66
DP
676Adds the 64-bit
677.I time_enabled
678field.
f2b1d720
MK
679This can be used to calculate estimated totals if
680the PMU is overcommitted and multiplexing is happening.
681.TP
682.B PERF_FORMAT_TOTAL_TIME_RUNNING
7ede2f66
DP
683Adds the 64-bit
684.I time_running
685field.
f2b1d720
MK
686This can be used to calculate estimated totals if
687the PMU is overcommitted and multiplexing is happening.
688.TP
689.B PERF_FORMAT_ID
690Adds a 64-bit unique value that corresponds to the event group.
691.TP
692.B PERF_FORMAT_GROUP
693Allows all counter values in an event group to be read with one read.
694.RE
f2b1d720
MK
695.TP
696.IR "disabled"
697The
698.I disabled
699bit specifies whether the counter starts out disabled or enabled.
700If disabled, the event can later be enabled by
701.BR ioctl (2),
702.BR prctl (2),
703or
704.IR enable_on_exec .
f2b1d720
MK
705.TP
706.IR "inherit"
707The
708.I inherit
709bit specifies that this counter should count events of child
710tasks as well as the task specified.
711This only applies to new children, not to any existing children at
712the time the counter is created (nor to any new children of
713existing children).
714
715Inherit does not work for some combinations of
716.IR read_format s,
717such as
718.BR PERF_FORMAT_GROUP .
f2b1d720
MK
719.TP
720.IR "pinned"
721The
722.I pinned
723bit specifies that the counter should always be on the CPU if at all
724possible.
725It only applies to hardware counters and only to group leaders.
726If a pinned counter cannot be put onto the CPU (e.g., because there are
727not enough hardware counters or because of a conflict with some other
728event), then the counter goes into an 'error' state, where reads
729return end-of-file (i.e.,
730.BR read (2)
731returns 0) until the counter is subsequently enabled or disabled.
f2b1d720
MK
732.TP
733.IR "exclusive"
734The
735.I exclusive
736bit specifies that when this counter's group is on the CPU,
737it should be the only group using the CPU's counters.
738In the future this may allow monitoring programs to
739support PMU features that need to run alone so that they do not
740disrupt other hardware counters.
f2b1d720
MK
741.TP
742.IR "exclude_user"
ad73a2cc 743If this bit is set, the count excludes events that happen in user space.
f2b1d720
MK
744.TP
745.IR "exclude_kernel"
746If this bit is set, the count excludes events that happen in kernel-space.
f2b1d720
MK
747.TP
748.IR "exclude_hv"
749If this bit is set, the count excludes events that happen in the
750hypervisor.
751This is mainly for PMUs that have built-in support for handling this
752(such as POWER).
753Extra support is needed for handling hypervisor measurements on most
754machines.
f2b1d720
MK
755.TP
756.IR "exclude_idle"
757If set, don't count when the CPU is idle.
f2b1d720
MK
758.TP
759.IR "mmap"
760The
761.I mmap
762bit enables recording of exec mmap events.
f2b1d720
MK
763.TP
764.IR "comm"
765The
766.I comm
767bit enables tracking of process command name as modified by the
768.IR exec (2)
769and
770.IR prctl (PR_SET_NAME)
771system calls.
772Unfortunately for tools,
773there is no way to distinguish one system call versus the other.
f2b1d720
MK
774.TP
775.IR "freq"
776If this bit is set, then
777.I sample_frequency
778not
779.I sample_period
780is used when setting up the sampling interval.
f2b1d720
MK
781.TP
782.IR "inherit_stat"
783This bit enables saving of event counts on context switch for
784inherited tasks.
785This is only meaningful if the
786.I inherit
787field is set.
f2b1d720
MK
788.TP
789.IR "enable_on_exec"
790If this bit is set, a counter is automatically
791enabled after a call to
792.BR exec (2).
f2b1d720
MK
793.TP
794.IR "task"
795If this bit is set, then
796fork/exit notifications are included in the ring buffer.
f2b1d720
MK
797.TP
798.IR "watermark"
799If set, have a sampling interrupt happen when we cross the
800.I wakeup_watermark
801boundary.
802Otherwise interrupts happen after
803.I wakeup_events
804samples.
f2b1d720
MK
805.TP
806.IR "precise_ip" " (Since Linux 2.6.35)"
807This controls the amount of skid.
808Skid is how many instructions
809execute between an event of interest happening and the kernel
810being able to stop and record the event.
811Smaller skid is
812better and allows more accurate reporting of which events
813correspond to which instructions, but hardware is often limited
814with how small this can be.
815
816The values of this are the following:
817.RS
818.TP
8190 -
820.B SAMPLE_IP
821can have arbitrary skid
822.TP
8231 -
824.B SAMPLE_IP
825must have constant skid
826.TP
8272 -
828.B SAMPLE_IP
829requested to have 0 skid
830.TP
8313 -
832.B SAMPLE_IP
833must have 0 skid.
834See also
835.BR PERF_RECORD_MISC_EXACT_IP .
836.RE
f2b1d720
MK
837.TP
838.IR "mmap_data" " (Since Linux 2.6.36)"
839The counterpart of the
840.I mmap
841field, but enables including data mmap events
842in the ring-buffer.
f2b1d720
MK
843.TP
844.IR "sample_id_all" " (Since Linux 2.6.38)"
845If set, then TID, TIME, ID, CPU, and STREAM_ID can
846additionally be included in
847.RB non- PERF_RECORD_SAMPLE s
848if the corresponding
849.I sample_type
850is selected.
f2b1d720
MK
851.TP
852.IR "exclude_host" " (Since Linux 3.2)"
853Do not measure time spent in VM host
f2b1d720
MK
854.TP
855.IR "exclude_guest" " (Since Linux 3.2)"
856Do not measure time spent in VM guest
f2b1d720
MK
857.TP
858.IR "exclude_callchain_kernel" " (Since Linux 3.7)"
859Do not include kernel callchains.
f2b1d720
MK
860.TP
861.IR "exclude_callchain_user" " (Since Linux 3.7)"
862Do not include user callchains.
f2b1d720
MK
863.TP
864.IR "wakeup_events" ", " "wakeup_watermark"
865This union sets how many samples
866.RI ( wakeup_events )
867or bytes
868.RI ( wakeup_watermark )
869happen before an overflow signal happens.
870Which one is used is selected by the
871.I watermark
872bitflag.
f2b1d720
MK
873.TP
874.IR "bp_type" " (Since Linux 2.6.33)"
875This chooses the breakpoint type.
876It is one of:
877.RS
878.TP
879.BR HW_BREAKPOINT_EMPTY
880no breakpoint
881.TP
882.BR HW_BREAKPOINT_R
883count when we read the memory location
884.TP
885.BR HW_BREAKPOINT_W
886count when we write the memory location
887.TP
888.BR HW_BREAKPOINT_RW
889count when we read or write the memory location
890.TP
891.BR HW_BREAKPOINT_X
892count when we execute code at the memory location
f2b1d720 893.LP
7db515ef 894The values can be combined via a bitwise or, but the
f2b1d720
MK
895combination of
896.B HW_BREAKPOINT_R
897or
898.B HW_BREAKPOINT_W
899with
900.B HW_BREAKPOINT_X
901is not allowed.
902.RE
f2b1d720
MK
903.TP
904.IR "bp_addr" " (Since Linux 2.6.33)"
905.I bp_addr
906address of the breakpoint.
907For execution breakpoints this is the memory address of the instruction
908of interest; for read and write breakpoints it is the memory address
909of the memory location of interest.
f2b1d720
MK
910.TP
911.IR "config1" " (Since Linux 2.6.39)"
912.I config1
913is used for setting events that need an extra register or otherwise
914do not fit in the regular config field.
915Raw OFFCORE_EVENTS on Nehalem/Westmere/SandyBridge use this field
916on 3.3 and later kernels.
f2b1d720
MK
917.TP
918.IR "bp_len" " (Since Linux 2.6.33)"
919.I bp_len
920is the length of the breakpoint being measured if
921.I type
922is
923.BR PERF_TYPE_BREAKPOINT .
924Options are
925.BR HW_BREAKPOINT_LEN_1 ,
926.BR HW_BREAKPOINT_LEN_2 ,
927.BR HW_BREAKPOINT_LEN_4 ,
928.BR HW_BREAKPOINT_LEN_8 .
929For an execution breakpoint, set this to
930.IR sizeof(long) .
f2b1d720
MK
931.TP
932.IR "config2" " (Since Linux 2.6.39)"
933
934.I config2
935is a further extension of the
936.I config1
937field.
f2b1d720
MK
938.TP
939.IR "branch_sample_type" " (Since Linux 3.4)"
940This is used with the CPUs hardware branch sampling, if available.
941It can have one of the following values:
942.RS
943.TP
944.B PERF_SAMPLE_BRANCH_USER
945Branch target is in user space
946.TP
947.B PERF_SAMPLE_BRANCH_KERNEL
948Branch target is in kernel space
949.TP
950.B PERF_SAMPLE_BRANCH_HV
951Branch target is in hypervisor
952.TP
953.B PERF_SAMPLE_BRANCH_ANY
954Any branch type.
955.TP
956.B PERF_SAMPLE_BRANCH_ANY_CALL
957Any call branch
958.TP
959.B PERF_SAMPLE_BRANCH_ANY_RETURN
960Any return branch
961.TP
962.BR PERF_SAMPLE_BRANCH_IND_CALL
963Indirect calls
964.TP
965.BR PERF_SAMPLE_BRANCH_PLM_ALL
966User, kernel, and hv
967.RE
f2b1d720
MK
968.TP
969.IR "sample_regs_user" " (Since Linux 3.7)"
970This defines the set of user registers to dump on samples.
7db515ef 971See
12eb3e64 972.\" FIXME: The following reference seems to be not quite right:
7db515ef 973.IR asm/perf_regs.h .
f2b1d720
MK
974.TP
975.IR "sample_stack_user" " (Since Linux 3.7)"
7ede2f66 976This defines the size of the user stack to dump on samples.
f2b1d720 977.RE
73d8cece 978.SS Reading results
f2b1d720 979Once a
7db515ef 980.BR perf_event_open ()
f2b1d720
MK
981file descriptor has been opened, the values
982of the events can be read from the file descriptor.
983The values that are there are specified by the
984.I read_format
7db515ef
MK
985field in the
986.I attr
987structure at open time.
f2b1d720
MK
988
989If you attempt to read into a buffer that is not big enough to hold the
990data
991.B ENOSPC
992is returned
993
994Here is the layout of the data returned by a read:
995
996If
997.B PERF_FORMAT_GROUP
998was specified to allow reading all events in a group at once:
999
1000.in +4n
1001.nf
1002struct read_format {
1003 u64 nr; /* The number of events */
1004 u64 time_enabled; /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
1005 u64 time_running; /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
1006 struct {
1007 u64 value; /* The value of the event */
1008 u64 id; /* if PERF_FORMAT_ID */
1009 } values[nr];
1010};
1011.fi
1012.in
1013
1014If
1015.B PERF_FORMAT_GROUP
1016was
1017.I not
1018specified, then the read values look as following:
1019
1020.in +4n
1021.nf
1022struct read_format {
1023 u64 value; /* The value of the event */
1024 u64 time_enabled; /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
1025 u64 time_running; /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
1026 u64 id; /* if PERF_FORMAT_ID */
1027};
1028.fi
1029.in
1030
1031The values read are described in more detail below.
1032.RS
1033.TP
1034.I nr
1035The number of events in this file descriptor.
1036Only available if
1037.B PERF_FORMAT_GROUP
1038was specified.
f2b1d720
MK
1039.TP
1040.IR time_enabled ", " time_running
1041Total time the event was enabled and running.
1042Normally these are the same.
1043If more events are started
1044than available counter slots on the PMU, then multiplexing
1045happens and events only run part of the time.
1046In that case the
1047.I time_enabled
1048and
1049.I time running
1050values can be used to scale an estimated value for the count.
f2b1d720
MK
1051.TP
1052.I value
1053An unsigned 64-bit value containing the counter result.
f2b1d720
MK
1054.TP
1055.I id
1056A globally unique value for this particular event, only there if
1057.B PERF_FORMAT_ID
1058was specified in read_format.
f2b1d720
MK
1059.RE
1060.RE
73d8cece 1061.SS MMAP layout
f2b1d720 1062When using
7db515ef 1063.BR perf_event_open ()
f2b1d720
MK
1064in sampled mode, asynchronous events
1065(like counter overflow or
1066.B PROT_EXEC
1067mmap tracking)
1068are logged into a ring-buffer.
1069This ring-buffer is created and accessed through
1070.BR mmap (2).
1071
1072The mmap size should be 1+2^n pages, where the first page is a
1073metadata page
1074.IR ( "struct perf_event_mmap_page" )
1075that contains various
1076bits of information such as where the ring-buffer head is.
1077
1078Before kernel 2.6.39, there is a bug that means you must allocate a mmap
1079ring buffer when sampling even if you do not plan to access it.
1080
1081The structure of the first metadata mmap page is as follows:
1082
1083.in +4n
1084.nf
1085struct perf_event_mmap_page {
7db515ef 1086 __u32 version; /* version number of this structure */
f2b1d720 1087 __u32 compat_version; /* lowest version this is compat with */
7db515ef
MK
1088 __u32 lock; /* seqlock for synchronization */
1089 __u32 index; /* hardware counter identifier */
1090 __s64 offset; /* add to hardware counter value */
1091 __u64 time_enabled; /* time event active */
1092 __u64 time_running; /* time event on CPU */
f2b1d720
MK
1093 union {
1094 __u64 capabilities;
1095 __u64 cap_usr_time : 1,
1096 cap_usr_rdpmc : 1,
1097 };
1098 __u16 pmc_width;
1099 __u16 time_shift;
1100 __u32 time_mult;
1101 __u64 time_offset;
7db515ef 1102 __u64 __reserved[120]; /* Pad to 1k */
f2b1d720 1103 __u64 data_head; /* head in the data section */
7db515ef 1104 __u64 data_tail; /* user-space written tail */
f2b1d720
MK
1105}
1106.fi
1107.in
1108
f2b1d720
MK
1109The following looks at the fields in the
1110.I perf_event_mmap_page
1111structure in more detail.
7db515ef 1112.RS 4
f2b1d720
MK
1113.TP
1114.I version
1115Version number of this structure.
f2b1d720
MK
1116.TP
1117.I compat_version
1118The lowest version this is compatible with.
f2b1d720
MK
1119.TP
1120.I lock
1121A seqlock for synchronization.
f2b1d720
MK
1122.TP
1123.I index
1124A unique hardware counter identifier.
f2b1d720
MK
1125.TP
1126.I offset
1127.\" FIXME clarify
1128Add this to hardware counter value??
f2b1d720
MK
1129.TP
1130.I time_enabled
1131Time the event was active.
f2b1d720
MK
1132.TP
1133.I time_running
1134Time the event was running.
f2b1d720
MK
1135.TP
1136.I cap_usr_time
1137User time capability
f2b1d720
MK
1138.TP
1139.I cap_usr_rdpmc
1140If the hardware supports user-space read of performance counters
1141without syscall (this is the "rdpmc" instruction on x86), then
1142the following code can be used to do a read:
1143
1144.in +4n
1145.nf
1146u32 seq, time_mult, time_shift, idx, width;
1147u64 count, enabled, running;
1148u64 cyc, time_offset;
1149s64 pmc = 0;
1150
1151do {
1152 seq = pc\->lock;
1153 barrier();
1154 enabled = pc\->time_enabled;
1155 running = pc\->time_running;
1156
1157 if (pc\->cap_usr_time && enabled != running) {
1158 cyc = rdtsc();
1159 time_offset = pc\->time_offset;
1160 time_mult = pc\->time_mult;
1161 time_shift = pc\->time_shift;
1162 }
1163
1164 idx = pc\->index;
1165 count = pc\->offset;
1166
1167 if (pc\->cap_usr_rdpmc && idx) {
1168 width = pc\->pmc_width;
1169 pmc = rdpmc(idx \- 1);
1170 }
1171
1172 barrier();
1173} while (pc\->lock != seq);
1174.fi
1175.in
f2b1d720
MK
1176.TP
1177.I pmc_width
1178If
1179.IR cap_usr_rdpmc ,
1180this field provides the bit-width of the value
1181read using the rdpmc or equivalent instruction.
1182This can be used to sign extend the result like:
1183
1184.in +4n
1185.nf
1186pmc <<= 64 \- pmc_width;
1187pmc >>= 64 \- pmc_width; // signed shift right
1188count += pmc;
1189.fi
1190.in
f2b1d720
MK
1191.TP
1192.IR time_shift ", " time_mult ", " time_offset
1193
1194If
1195.IR cap_usr_time ,
1196these fields can be used to compute the time
7db515ef 1197delta since time_enabled (in nanoseconds) using rdtsc or similar.
f2b1d720
MK
1198.nf
1199
1200 u64 quot, rem;
1201 u64 delta;
1202 quot = (cyc >> time_shift);
1203 rem = cyc & ((1 << time_shift) \- 1);
1204 delta = time_offset + quot * time_mult +
1205 ((rem * time_mult) >> time_shift);
1206.fi
1207
7db515ef
MK
1208Where
1209.IR time_offset ,
1210.IR time_mult ,
1211.IR time_shift ,
1212and
1213.IR cyc
1214are read in the
f2b1d720
MK
1215seqcount loop described above.
1216This delta can then be added to
1217enabled and possible running (if idx), improving the scaling:
1218.nf
1219
1220 enabled += delta;
1221 if (idx)
1222 running += delta;
1223 quot = count / running;
1224 rem = count % running;
1225 count = quot * enabled + (rem * enabled) / running;
1226.fi
f2b1d720
MK
1227.TP
1228.I data_head
1229This points to the head of the data section.
7db515ef
MK
1230The value continuously increases, it does not wrap.
1231The value needs to be manually wrapped by the size of the mmap buffer
f2b1d720
MK
1232before accessing the samples.
1233
1234On SMP-capable platforms, after reading the data_head value,
ad73a2cc 1235user space should issue an rmb().
f2b1d720
MK
1236.TP
1237.I data_tail;
1238When the mapping is
1239.BR PROT_WRITE ,
7db515ef
MK
1240the
1241.I data_tail
1242value should be written by user space to reflect the last read data.
f2b1d720 1243In this case the kernel will not over-write unread data.
f2b1d720 1244.RE
f2b1d720
MK
1245The following 2^n ring-buffer pages have the layout described below.
1246
1247If
1248.I perf_event_attr.sample_id_all
1249is set, then all event types will
1250have the sample_type selected fields related to where/when (identity)
1251an event took place (TID, TIME, ID, CPU, STREAM_ID) described in
1252.B PERF_RECORD_SAMPLE
1253below, it will be stashed just after the
7db515ef
MK
1254.I perf_event_header
1255and the fields already present for the existing
f2b1d720
MK
1256fields, i.e., at the end of the payload.
1257That way a newer perf.data
1258file will be supported by older perf tools, with these new optional
1259fields being ignored.
1260
1261The mmap values start with a header:
1262
1263.in +4n
1264.nf
1265struct perf_event_header {
1266 __u32 type;
1267 __u16 misc;
1268 __u16 size;
1269};
1270.fi
1271.in
1272
1273Below, we describe the
1274.I perf_event_header
1275fields in more detail.
f2b1d720
MK
1276.TP
1277.I type
1278The
1279.I type
1280value is one of the below.
1281The values in the corresponding record (that follows the header)
1282depend on the
1283.I type
1284selected as shown.
f2b1d720 1285.RS
7db515ef 1286.TP 4
f2b1d720
MK
1287.B PERF_RECORD_MMAP
1288The MMAP events record the
1289.B PROT_EXEC
1290mappings so that we can correlate
ad73a2cc 1291user-space IPs to code.
f2b1d720
MK
1292They have the following structure:
1293
1294.in +4n
1295.nf
1296struct {
1297 struct perf_event_header header;
1298 u32 pid, tid;
1299 u64 addr;
1300 u64 len;
1301 u64 pgoff;
1302 char filename[];
1303};
1304.fi
1305.in
f2b1d720
MK
1306.TP
1307.B PERF_RECORD_LOST
1308This record indicates when events are lost.
1309
1310.in +4n
1311.nf
1312struct {
1313 struct perf_event_header header;
1314 u64 id;
1315 u64 lost;
1316};
1317.fi
1318.in
f2b1d720
MK
1319.RS
1320.TP
1321.I id
1322is the unique event ID for the samples that were lost.
1323.TP
1324.I lost
1325is the number of events that were lost.
1326.RE
f2b1d720
MK
1327.TP
1328.B PERF_RECORD_COMM
1329This record indicates a change in the process name.
1330
1331.in +4n
1332.nf
1333struct {
1334 struct perf_event_header header;
1335 u32 pid, tid;
1336 char comm[];
1337};
1338.fi
1339.in
f2b1d720
MK
1340.TP
1341.B PERF_RECORD_EXIT
1342This record indicates a process exit event.
1343
1344.in +4n
1345.nf
1346struct {
1347 struct perf_event_header header;
1348 u32 pid, ppid;
1349 u32 tid, ptid;
1350 u64 time;
1351};
1352.fi
1353.in
f2b1d720
MK
1354.TP
1355.BR PERF_RECORD_THROTTLE ", " PERF_RECORD_UNTHROTTLE
1356This record indicates a throttle/unthrottle event.
1357
1358.in +4n
1359.nf
1360struct {
1361 struct perf_event_header header;
1362 u64 time;
1363 u64 id;
1364 u64 stream_id;
1365};
1366.fi
1367.in
f2b1d720
MK
1368.TP
1369.B PERF_RECORD_FORK
1370This record indicates a fork event.
1371
1372.in +4n
1373.nf
1374struct {
1375 struct perf_event_header header;
1376 u32 pid, ppid;
1377 u32 tid, ptid;
1378 u64 time;
1379};
1380.fi
1381.in
f2b1d720
MK
1382.TP
1383.B PERF_RECORD_READ
1384This record indicates a read event.
1385
1386.in +4n
1387.nf
1388struct {
1389 struct perf_event_header header;
1390 u32 pid, tid;
1391 struct read_format values;
1392};
1393.fi
1394.in
f2b1d720
MK
1395.TP
1396.B PERF_RECORD_SAMPLE
1397This record indicates a sample.
1398
1399.in +4n
1400.nf
1401struct {
1402 struct perf_event_header header;
7db515ef
MK
1403 u64 ip; /* if PERF_SAMPLE_IP */
1404 u32 pid, tid; /* if PERF_SAMPLE_TID */
1405 u64 time; /* if PERF_SAMPLE_TIME */
1406 u64 addr; /* if PERF_SAMPLE_ADDR */
1407 u64 id; /* if PERF_SAMPLE_ID */
1408 u64 stream_id; /* if PERF_SAMPLE_STREAM_ID */
1409 u32 cpu, res; /* if PERF_SAMPLE_CPU */
1410 u64 period; /* if PERF_SAMPLE_PERIOD */
f2b1d720 1411 struct read_format v; /* if PERF_SAMPLE_READ */
7db515ef
MK
1412 u64 nr; /* if PERF_SAMPLE_CALLCHAIN */
1413 u64 ips[nr]; /* if PERF_SAMPLE_CALLCHAIN */
1414 u32 size; /* if PERF_SAMPLE_RAW */
1415 char data[size]; /* if PERF_SAMPLE_RAW */
1416 u64 bnr; /* if PERF_SAMPLE_BRANCH_STACK */
1417 struct perf_branch_entry lbr[bnr];
1418 /* if PERF_SAMPLE_BRANCH_STACK */
1419 u64 abi; /* if PERF_SAMPLE_REGS_USER */
1420 u64 regs[weight(mask)];
1421 /* if PERF_SAMPLE_REGS_USER */
1422 u64 size; /* if PERF_SAMPLE_STACK_USER */
1423 char data[size]; /* if PERF_SAMPLE_STACK_USER */
1424 u64 dyn_size; /* if PERF_SAMPLE_STACK_USER */
f2b1d720
MK
1425};
1426.fi
f2b1d720
MK
1427.RS
1428.TP
1429.I ip
7db515ef
MK
1430If
1431.B PERF_SAMPLE_IP
1432is enabled, then a 64-bit instruction
f2b1d720 1433pointer value is included.
f2b1d720 1434.TP
7db515ef
MK
1435.IR pid ", " tid
1436If
1437.B PERF_SAMPLE_TID
1438is enabled, then a 32-bit process ID
1439and 32-bit thread ID are included.
f2b1d720
MK
1440.TP
1441.I time
7db515ef
MK
1442If
1443.B PERF_SAMPLE_TIME
1444is enabled, then a 64-bit timestamp
f2b1d720
MK
1445is included.
1446This is obtained via local_clock() which is a hardware timestamp
1447if available and the jiffies value if not.
f2b1d720
MK
1448.TP
1449.I addr
7db515ef
MK
1450If
1451.B PERF_SAMPLE_ADDR
1452is enabled, then a 64-bit address is included.
f2b1d720
MK
1453This is usually the address of a tracepoint,
1454breakpoint, or software event; otherwise the value is 0.
f2b1d720
MK
1455.TP
1456.I id
7db515ef
MK
1457If
1458.B PERF_SAMPLE_ID
1459is enabled, a 64-bit unique ID is included.
f2b1d720 1460If the event is a member of an event group, the group leader ID is returned.
7db515ef
MK
1461This ID is the same as the one returned by
1462.BR PERF_FORMAT_ID .
f2b1d720
MK
1463.TP
1464.I stream_id
7db515ef
MK
1465If
1466.B PERF_SAMPLE_STREAM_ID
1467is enabled, a 64-bit unique ID is included.
f2b1d720
MK
1468Unlike
1469.B PERF_SAMPLE_ID
1470the actual ID is returned, not the group leader.
7db515ef
MK
1471This ID is the same as the one returned by
1472.BR PERF_FORMAT_ID .
f2b1d720 1473.TP
7db515ef
MK
1474.IR cpu ", " res
1475If
1476.B PERF_SAMPLE_CPU
1477is enabled, this is a 32-bit value indicating
f2b1d720
MK
1478which CPU was being used, in addition to a reserved (unused)
147932-bit value.
f2b1d720
MK
1480.TP
1481.I period
7db515ef
MK
1482If
1483.B PERF_SAMPLE_PERIOD
1484is enabled, a 64-bit value indicating
f2b1d720 1485the current sampling period is written.
f2b1d720
MK
1486.TP
1487.I v
7db515ef
MK
1488If
1489.B PERF_SAMPLE_READ
1490is enabled, a structure of type read_format
f2b1d720
MK
1491is included which has values for all events in the event group.
1492The values included depend on the
1493.I read_format
7db515ef
MK
1494value used at
1495.BR perf_event_open ()
1496time.
f2b1d720 1497.TP
7db515ef
MK
1498.IR nr ", " ips[nr]
1499If
1500.B PERF_SAMPLE_CALLCHAIN
1501is enabled, then a 64-bit number is included
f2b1d720 1502which indicates how many following 64-bit instruction pointers will
7db515ef
MK
1503follow.
1504This is the current callchain.
f2b1d720 1505.TP
7ede2f66 1506.IR size ", " data[size]
7db515ef
MK
1507If
1508.B PERF_SAMPLE_RAW
1509is enabled, then a 32-bit value indicating size
f2b1d720
MK
1510is included followed by an array of 8-bit values of length size.
1511The values are padded with 0 to have 64-bit alignment.
1512
1513This RAW record data is opaque with respect to the ABI.
1514The ABI doesn't make any promises with respect to the stability
1515of its content, it may vary depending
1516on event, hardware, and kernel version.
f2b1d720 1517.TP
7db515ef
MK
1518.IR bnr ", " lbr[bnr]
1519If
1520.B PERF_SAMPLE_BRANCH_STACK
1521is enabled, then a 64-bit value indicating
1522the number of records is included, followed by
1523.I bnr
1524.I perf_branch_entry
1525structures.
1526These structures have from, to, and flags values indicating
f2b1d720 1527the from and to addresses from the branches on the callstack.
f2b1d720 1528.TP
7db515ef
MK
1529.IR abi ", " regs[weight(mask)]
1530If
1531.B PERF_SAMPLE_REGS_USER
1532is enabled, then
f2b1d720
MK
1533[to be documented].
1534
1535The
1536.I abi
1537field is one of
1538.BR PERF_SAMPLE_REGS_ABI_NONE ", " PERF_SAMPLE_REGS_ABI_32 " or "
7db515ef 1539.BR PERF_SAMPLE_REGS_ABI_64 .
f2b1d720 1540.TP
7db515ef
MK
1541.IR size ", " data[size] ", " dyn_size
1542If
1543.B PERF_SAMPLE_STACK_USER
1544is enabled, then
f2b1d720 1545[to be documented].
f2b1d720 1546.RE
f2b1d720 1547.RE
f2b1d720
MK
1548.TP
1549.I misc
1550The
1551.I misc
1552field contains additional information about the sample.
1553
1554The CPU mode can be determined from this value by masking with
1555.B PERF_RECORD_MISC_CPUMODE_MASK
1556and looking for one of the following (note these are not
1557bitmasks, only one can be set at a time):
1558.RS
1559.TP
1560.B PERF_RECORD_MISC_CPUMODE_UNKNOWN
1561Unknown CPU mode.
1562.TP
1563.B PERF_RECORD_MISC_KERNEL
1564Sample happened in the kernel.
1565.TP
1566.B PERF_RECORD_MISC_USER
1567Sample happened in user code.
1568.TP
1569.B PERF_RECORD_MISC_HYPERVISOR
1570Sample happened in the hypervisor.
1571.TP
1572.B PERF_RECORD_MISC_GUEST_KERNEL
1573Sample happened in the guest kernel.
1574.TP
1575.B PERF_RECORD_MISC_GUEST_USER
1576Sample happened in guest user code.
1577.RE
1578
7db515ef 1579In addition, one of the following bits can be set:
f2b1d720
MK
1580.RS
1581.TP
1582.B PERF_RECORD_MISC_EXACT_IP
1583This indicates that the content of
1584.B PERF_SAMPLE_IP
1585points
1586to the actual instruction that triggered the event.
1587See also
1588.IR perf_event_attr.precise_ip .
f2b1d720
MK
1589.TP
1590.B PERF_RECORD_MISC_EXT_RESERVED
1591This indicates there is extended data available (currently not used).
f2b1d720 1592.RE
f2b1d720
MK
1593.TP
1594.I size
1595This indicates the size of the record.
f2b1d720 1596.RE
73d8cece 1597.SS Signal overflow
f2b1d720
MK
1598Events can be set to deliver a signal when a threshold is crossed.
1599The signal handler is set up using the
1600.BR poll (2),
1601.BR select (2),
1602.BR epoll (2)
1603and
1604.BR fcntl (2),
1605system calls.
1606
1607To generate signals, sampling must be enabled
1608.RI ( sample_period
1609must have a non-zero value).
1610
1611There are two ways to generate signals.
1612
1613The first is to set a
1614.I wakeup_events
1615or
1616.I wakeup_watermark
1617value that will generate a signal if a certain number of samples
1618or bytes have been written to the mmap ring buffer.
7db515ef
MK
1619In this case a signal of type
1620.B POLL_IN
1621is sent.
f2b1d720
MK
1622
1623The other way is by use of the
7db515ef 1624.B PERF_EVENT_IOC_REFRESH
f2b1d720
MK
1625ioctl.
1626This ioctl adds to a counter that decrements each time the event overflows.
7db515ef
MK
1627When non-zero, a
1628.B POLL_IN
1629signal is sent on overflow, but
1630once the value reaches 0, a signal is sent of type
1631.B POLL_HUP
1632and
f2b1d720
MK
1633the underlying event is disabled.
1634
1635Note: on newer kernels (definitely noticed with 3.2)
7db515ef 1636.\" FIXME(Vince) : Find out when this was introduced
f2b1d720
MK
1637a signal is provided for every overflow, even if
1638.I wakeup_events
1639is not set.
73d8cece 1640.SS rdpmc instruction
f2b1d720
MK
1641Starting with Linux 3.4 on x86, you can use the
1642.I rdpmc
1643instruction to get low-latency reads without having to enter the kernel.
1644Note that using
1645.I rdpmc
1646is not necessarily faster than other methods for reading event values.
1647
1648Support for this can be detected with the
1649.I cap_usr_rdpmc
1650field in the mmap page; documentation on how
1651to calculate event values can be found in that section.
73d8cece 1652.SS perf_event ioctl calls
f2b1d720
MK
1653.PP
1654Various ioctls act on
7db515ef 1655.BR perf_event_open ()
f2b1d720 1656file descriptors
f2b1d720
MK
1657.TP
1658.B PERF_EVENT_IOC_ENABLE
36127c0e 1659Enables the individual event or event group specified by the
7db515ef 1660file descriptor argument.
f2b1d720
MK
1661
1662The ioctl argument is ignored.
f2b1d720
MK
1663.TP
1664.B PERF_EVENT_IOC_DISABLE
36127c0e 1665Disables the individual counter or event group specified by the
7db515ef 1666file descriptor argument.
f2b1d720
MK
1667
1668Enabling or disabling the leader of a group enables or disables the
1669entire group; that is, while the group leader is disabled, none of the
1670counters in the group will count.
1671Enabling or disabling a member of a group other than the leader only
1672affects that counter; disabling a non-leader
1673stops that counter from counting but doesn't affect any other counter.
1674
1675The ioctl argument is ignored.
f2b1d720
MK
1676.TP
1677.B PERF_EVENT_IOC_REFRESH
1678Non-inherited overflow counters can use this
1679to enable a counter for a number of overflows specified by the argument,
1680after which it is disabled.
1681Subsequent calls of this ioctl add the argument value to the current
1682count.
7db515ef
MK
1683A signal with
1684.B POLL_IN
1685set will happen on each overflow until the
1686count reaches 0; when that happens a signal with
1687POLL_HUP
1688set is sent and the event is disabled.
f2b1d720 1689Using an argument of 0 is considered undefined behavior.
f2b1d720
MK
1690.TP
1691.B PERF_EVENT_IOC_RESET
36127c0e 1692Reset the event count specified by the
7db515ef 1693file descriptor argumentto zero.
f2b1d720
MK
1694This only resets the counts; there is no way to reset the
1695multiplexing
1696.I time_enabled
1697or
1698.I time_running
1699values.
1700When sent to a group leader, only
1701the leader is reset (child events are not).
1702
1703The ioctl argument is ignored.
f2b1d720
MK
1704.TP
1705.B PERF_EVENT_IOC_PERIOD
1706IOC_PERIOD is the command to update the period; it
1707does not update the current period but instead defers until next.
1708
1709The argument is a pointer to a 64-bit value containing the
1710desired new period.
f2b1d720
MK
1711.TP
1712.B PERF_EVENT_IOC_SET_OUTPUT
1713This tells the kernel to report event notifications to the specified
1714file descriptor rather than the default one.
1715The file descriptors must all be on the same CPU.
1716
1717The argument specifies the desired file descriptor, or \-1 if
1718output should be ignored.
f2b1d720
MK
1719.TP
1720.BR PERF_EVENT_IOC_SET_FILTER " (Since Linux 2.6.33)"
1721This adds an ftrace filter to this event.
1722
1723The argument is a pointer to the desired ftrace filter.
73d8cece 1724.SS Using prctl
f2b1d720
MK
1725A process can enable or disable all the event groups that are
1726attached to it using the
1727.BR prctl (2)
1728.B PR_TASK_PERF_EVENTS_ENABLE
1729and
1730.B PR_TASK_PERF_EVENTS_DISABLE
1731operations.
1732This applies to all counters on the current process, whether created by
1733this process or by another, and does not affect any counters that this
1734process has created on other processes.
1735It only enables or disables
1736the group leaders, not any other members in the groups.
f2b1d720 1737.SS perf_event related configuration files
7db515ef
MK
1738Files in
1739.I /proc/sys/kernel/
7db515ef 1740.RS 4
f2b1d720 1741.TP
7db515ef 1742.I /proc/sys/kernel/perf_event_paranoid
f2b1d720
MK
1743
1744The
1745.I perf_event_paranoid
1746file can be set to restrict access to the performance counters.
1747
7db515ef 17482 - only allow user-space measurements
f2b1d720
MK
1749
17501 - (default) allow both kernel and user measurements
1751
17520 - allow access to CPU-specific data but not raw tracepoint samples
1753
1754\-1 - no restrictions
1755
1756The existence of the
1757.I perf_event_paranoid
1758file is the official method for determining if a kernel supports
7db515ef 1759.BR perf_event_open ().
f2b1d720
MK
1760.TP
1761.I /proc/sys/kernel/perf_event_max_sample_rate
1762
7db515ef
MK
1763This sets the maximum sample rate.
1764Setting this too high can allow
f2b1d720 1765users to sample at a rate that impacts overall machine performance
7db515ef
MK
1766and potentially lock up the machine.
1767The default value is
f2b1d720 1768100000 (samples per second).
f2b1d720
MK
1769.TP
1770.I /proc/sys/kernel/perf_event_mlock_kb
1771
7db515ef 1772Maximum number of pages an unprivileged user can mlock (2) .
f2b1d720
MK
1773The default is 516 (kB).
1774.RE
7db515ef
MK
1775Files in
1776.I /sys/bus/event_source/devices/
7db515ef 1777.RS 4
f2b1d720
MK
1778Since Linux 2.6.34 the kernel supports having multiple PMUs
1779available for monitoring.
1780Information on how to program these PMUs can be found under
1781.IR /sys/bus/event_source/devices/ .
1782Each subdirectory corresponds to a different PMU.
f2b1d720
MK
1783.TP
1784.I /sys/bus/event_source/devices/*/type
1785This contains an integer that can be used in the
1786.I type
1787field of perf_event_attr to indicate you wish to use this PMU.
f2b1d720
MK
1788.TP
1789.I /sys/bus/event_source/devices/*/rdpmc
1790[To be documented]
f2b1d720
MK
1791.TP
1792.I /sys/bus/event_source/devices/*/format/
1793This sub-directory contains information on what bits in the
1794.I config
1795field of perf_event_attr correspond to.
f2b1d720
MK
1796.TP
1797.I /sys/bus/event_source/devices/*/events/
1798This sub-directory contains files with pre-defined events.
1799The contents are strings describing the event settings
1800expressed in terms of the fields found in the
1801.I ./format/
1802directory.
1803These are not necessarily complete lists of all events supported by
1804a PMU, but usually a subset of events deemed useful or interesting.
f2b1d720
MK
1805.TP
1806.I /sys/bus/event_source/devices/*/uevent
1807[To be documented]
f2b1d720 1808.RE
47297adb 1809.SH RETURN VALUE
f2b1d720
MK
1810.BR perf_event_open ()
1811returns the new file descriptor, or \-1 if an error occurred
1812(in which case,
1813.I errno
1814is set appropriately).
1815.SH ERRORS
1816.TP
1817.B EINVAL
1818Returned if the specified event is not available.
1819.TP
1820.B ENOSPC
1821Prior to Linux 3.3, if there was not enough room for the event,
1822.B ENOSPC
1823was returned.
1824Linus did not like this, and this was changed to
1825.BR EINVAL .
1826.B ENOSPC
1827is still returned if you try to read results into
1828too small of a buffer.
f2b1d720 1829.SH VERSION
f2b1d720
MK
1830.BR perf_event_open ()
1831was introduced in Linux 2.6.31 but was called
1832.BR perf_counter_open () .
1833It was renamed in Linux 2.6.32.
f2b1d720 1834.SH CONFORMING TO
7db515ef
MK
1835This
1836.BR perf_event_open ()
1837system call Linux- specific
f2b1d720 1838and should not be used in programs intended to be portable.
f2b1d720
MK
1839.SH NOTES
1840Glibc does not provide a wrapper for this system call; call it using
1841.BR syscall (2).
7db515ef 1842See the example below.
f2b1d720
MK
1843
1844The official way of knowing if
7db515ef 1845.BR perf_event_open ()
f2b1d720
MK
1846support is enabled is checking
1847for the existence of the file
7db515ef 1848.IR /proc/sys/kernel/perf_event_paranoid .
f2b1d720 1849.SH BUGS
f2b1d720
MK
1850The
1851.B F_SETOWN_EX
1852option to
7db515ef 1853.BR fcntl (2)
f2b1d720
MK
1854is needed to properly get overflow signals in threads.
1855This was introduced in Linux 2.6.32.
1856
1857Prior to Linux 2.6.33 (at least for x86) the kernel did not check
1858if events could be scheduled together until read time.
1859The same happens on all known kernels if the NMI watchdog is enabled.
1860This means to see if a given set of events works you have to
1861.BR perf_event_open (),
1862start, then read before you know for sure you
1863can get valid measurements.
1864
1865Prior to Linux 2.6.34 event constraints were not enforced by the kernel.
1866In that case, some events would silently return "0" if the kernel
1867scheduled them in an improper counter slot.
1868
1869Prior to Linux 2.6.34 there was a bug when multiplexing where the
1870wrong results could be returned.
1871
1872Kernels from Linux 2.6.35 to Linux 2.6.39 can quickly crash the kernel if
1873"inherit" is enabled and many threads are started.
1874
1875Prior to Linux 2.6.35,
1876.B PERF_FORMAT_GROUP
1877did not work with attached processes.
1878
1879In older Linux 2.6 versions,
1880refreshing an event group leader refreshed all siblings,
1881and refreshing with a parameter of 0 enabled infinite refresh.
1882This behavior is unsupported and should not be relied on.
1883
1884There is a bug in the kernel code between
1885Linux 2.6.36 and Linux 3.0 that ignores the
1886"watermark" field and acts as if a wakeup_event
1887was chosen if the union has a
1888non-zero value in it.
1889
7db515ef
MK
1890Always double-check your results!
1891Various generalized events have had wrong values.
f2b1d720
MK
1892For example, retired branches measured
1893the wrong thing on AMD machines until Linux 2.6.35.
f2b1d720
MK
1894.SH EXAMPLE
1895The following is a short example that measures the total
7db515ef
MK
1896instruction count of a call to
1897.BR printf (3).
f2b1d720
MK
1898.nf
1899
1900#include <stdlib.h>
1901#include <stdio.h>
1902#include <unistd.h>
1903#include <string.h>
1904#include <sys/ioctl.h>
1905#include <linux/perf_event.h>
1906#include <asm/unistd.h>
1907
7db515ef
MK
1908long
1909perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
1910 int cpu, int group_fd, unsigned long flags)
f2b1d720
MK
1911{
1912 int ret;
1913
7db515ef
MK
1914 ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
1915 group_fd, flags);
f2b1d720
MK
1916 return ret;
1917}
1918
f2b1d720
MK
1919int
1920main(int argc, char **argv)
1921{
f2b1d720
MK
1922 struct perf_event_attr pe;
1923 long long count;
1924 int fd;
1925
1926 memset(&pe, 0, sizeof(struct perf_event_attr));
1927 pe.type = PERF_TYPE_HARDWARE;
1928 pe.size = sizeof(struct perf_event_attr);
1929 pe.config = PERF_COUNT_HW_INSTRUCTIONS;
1930 pe.disabled = 1;
1931 pe.exclude_kernel = 1;
1932 pe.exclude_hv = 1;
1933
1934 fd = perf_event_open(&pe, 0, \-1, \-1, 0);
7db515ef 1935 if (fd == \-1) {
f2b1d720 1936 fprintf(stderr, "Error opening leader %llx\\n", pe.config);
7db515ef 1937 exit(EXIT_FAILURE);
f2b1d720
MK
1938 }
1939
1940 ioctl(fd, PERF_EVENT_IOC_RESET, 0);
1941 ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
1942
1943 printf("Measuring instruction count for this printf\\n");
1944
1945 ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
1946 read(fd, &count, sizeof(long long));
1947
1948 printf("Used %lld instructions\\n", count);
1949
1950 close(fd);
1951}
1952.fi
47297adb 1953.SH SEE ALSO
f2b1d720
MK
1954.BR fcntl (2),
1955.BR mmap (2),
1956.BR open (2),
1957.BR prctl (2),
1958.BR read (2)