OSDN Git Service

LDP: Update original to LDP v3.78-git-80a7408
[linuxjm/LDP_man-pages.git] / original / man2 / perf_event_open.2
1 .\" Copyright (c) 2012, Vincent Weaver
2 .\"
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.
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/>.
22 .\" %%%LICENSE_END
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 2015-01-10 "Linux" "Linux Programmer's Manual"
28 .SH NAME
29 perf_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 :
41 There is no glibc wrapper for this system call; see NOTES.
42 .SH DESCRIPTION
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.)."
47 .PP
48 A call to
49 .BR perf_event_open ()
50 creates a file descriptor that allows measuring performance
51 information.
52 Each file descriptor corresponds to one
53 event that is measured; these can be grouped together
54 to measure multiple events simultaneously.
55 .PP
56 Events can be enabled and disabled in two ways: via
57 .BR ioctl (2)
58 and via
59 .BR prctl (2).
60 When an event is disabled it does not count or generate overflows but does
61 continue to exist and maintain its count value.
62 .PP
63 Events come in two flavors: counting and sampled.
64 A
65 .I counting
66 event is one that is used for counting the aggregate number of events
67 that occur.
68 In general, counting event results are gathered with a
69 .BR read (2)
70 call.
71 A
72 .I sampling
73 event periodically writes measurements to a buffer that can then
74 be accessed via
75 .BR mmap (2).
76 .SS Arguments
77 .P
78 The
79 .I pid
80 and
81 .I cpu
82 arguments allow specifying which process and CPU to monitor:
83 .TP
84 .BR "pid == 0" " and " "cpu == \-1"
85 This measures the calling process/thread on any CPU.
86 .TP
87 .BR "pid == 0" " and " "cpu >= 0"
88 This measures the calling process/thread only
89 when running on the specified CPU.
90 .TP
91 .BR "pid > 0" " and " "cpu == \-1"
92 This measures the specified process/thread on any CPU.
93 .TP
94 .BR "pid > 0" " and " "cpu >= 0"
95 This measures the specified process/thread only
96 when running on the specified CPU.
97 .TP
98 .BR "pid == \-1" " and " "cpu >= 0"
99 This measures all processes/threads on the specified CPU.
100 This requires
101 .B CAP_SYS_ADMIN
102 capability or a
103 .I /proc/sys/kernel/perf_event_paranoid
104 value of less than 1.
105 .TP
106 .BR "pid == \-1" " and " "cpu == \-1"
107 This setting is invalid and will return an error.
108 .P
109 The
110 .I group_fd
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 ()
117 calls with
118 .IR group_fd
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
125 the CPU.
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
129 instructions.
130 .P
131 The
132 .I flags
133 argument is formed by ORing together zero or more of the following values:
134 .TP
135 .BR PERF_FLAG_FD_CLOEXEC " (since Linux 3.14)."
136 This flag enables the close-on-exec flag for the created
137 event file descriptor,
138 so that the file descriptor is automatically closed on
139 .BR execve (2).
140 Setting the close-on-exec flags at creation time, rather than later with
141 .BR fcntl (2),
142 avoids potential race conditions where the calling thread invokes
143 .BR perf_event_open ()
144 and
145 .BR fcntl (2)
146 at the same time as another thread calls
147 .BR fork (2)
148 then
149 .BR execve (2).
150 .TP
151 .BR PERF_FLAG_FD_NO_GROUP
152 This flag tells the event to ignore the
153 .IR group_fd
154 parameter except for the purpose of setting up output redirection
155 using the
156 .B PERF_FLAG_FD_OUTPUT
157 flag.
158 .TP
159 .BR PERF_FLAG_FD_OUTPUT " (broken since Linux 2.6.35)."
160 This flag re-routes the event's sampled output to instead
161 be included in the mmap buffer of the event specified by
162 .IR group_fd .
163 .TP
164 .BR PERF_FLAG_PID_CGROUP " (since Linux 2.6.39)."
165 This flag activates per-container system-wide monitoring.
166 A container
167 is an abstraction that isolates a set of resources for finer-grained
168 control (CPUs, memory, etc.).
169 In this mode, the event is measured
170 only if the thread running on the monitored CPU belongs to the designated
171 container (cgroup).
172 The cgroup is identified by passing a file descriptor
173 opened on its directory in the cgroupfs filesystem.
174 For instance, if the
175 cgroup to monitor is called
176 .IR test ,
177 then a file descriptor opened on
178 .I /dev/cgroup/test
179 (assuming cgroupfs is mounted on
180 .IR /dev/cgroup )
181 must be passed as the
182 .I pid
183 parameter.
184 cgroup monitoring is available only
185 for system-wide events and may therefore require extra permissions.
186 .P
187 The
188 .I perf_event_attr
189 structure provides detailed configuration information
190 for the event being created.
191
192 .in +4n
193 .nf
194 struct perf_event_attr {
195     __u32 type;         /* Type of event */
196     __u32 size;         /* Size of attribute structure */
197     __u64 config;       /* Type-specific configuration */
198
199     union {
200         __u64 sample_period;    /* Period of sampling */
201         __u64 sample_freq;      /* Frequency of sampling */
202     };
203
204     __u64 sample_type;  /* Specifies values included in sample */
205     __u64 read_format;  /* Specifies values returned in read */
206
207     __u64 disabled       : 1,   /* off by default */
208           inherit        : 1,   /* children inherit it */
209           pinned         : 1,   /* must always be on PMU */
210           exclusive      : 1,   /* only group on PMU */
211           exclude_user   : 1,   /* don't count user */
212           exclude_kernel : 1,   /* don't count kernel */
213           exclude_hv     : 1,   /* don't count hypervisor */
214           exclude_idle   : 1,   /* don't count when idle */
215           mmap           : 1,   /* include mmap data */
216           comm           : 1,   /* include comm data */
217           freq           : 1,   /* use freq, not period */
218           inherit_stat   : 1,   /* per task counts */
219           enable_on_exec : 1,   /* next exec enables */
220           task           : 1,   /* trace fork/exit */
221           watermark      : 1,   /* wakeup_watermark */
222           precise_ip     : 2,   /* skid constraint */
223           mmap_data      : 1,   /* non-exec mmap data */
224           sample_id_all  : 1,   /* sample_type all events */
225           exclude_host   : 1,   /* don't count in host */
226           exclude_guest  : 1,   /* don't count in guest */
227           exclude_callchain_kernel : 1,
228                                 /* exclude kernel callchains */
229           exclude_callchain_user   : 1,
230                                 /* exclude user callchains */
231           mmap2          :  1,  /* include mmap with inode data */
232           comm_exec      :  1,  /* flag comm events that are due to exec */
233           __reserved_1   : 39;
234
235     union {
236         __u32 wakeup_events;    /* wakeup every n events */
237         __u32 wakeup_watermark; /* bytes before wakeup */
238     };
239
240     __u32     bp_type;          /* breakpoint type */
241
242     union {
243         __u64 bp_addr;          /* breakpoint address */
244         __u64 config1;          /* extension of config */
245     };
246
247     union {
248         __u64 bp_len;           /* breakpoint length */
249         __u64 config2;          /* extension of config1 */
250     };
251     __u64 branch_sample_type;   /* enum perf_branch_sample_type */
252     __u64 sample_regs_user;     /* user regs to dump on samples */
253     __u32 sample_stack_user;    /* size of stack to dump on
254                                    samples */
255     __u32 __reserved_2;         /* Align to u64 */
256
257 };
258 .fi
259 .in
260
261 The fields of the
262 .I perf_event_attr
263 structure are described in more detail below:
264 .TP
265 .I type
266 This field specifies the overall event type.
267 It has one of the following values:
268 .RS
269 .TP
270 .B PERF_TYPE_HARDWARE
271 This indicates one of the "generalized" hardware events provided
272 by the kernel.
273 See the
274 .I config
275 field definition for more details.
276 .TP
277 .B PERF_TYPE_SOFTWARE
278 This indicates one of the software-defined events provided by the kernel
279 (even if no hardware support is available).
280 .TP
281 .B PERF_TYPE_TRACEPOINT
282 This indicates a tracepoint
283 provided by the kernel tracepoint infrastructure.
284 .TP
285 .B PERF_TYPE_HW_CACHE
286 This indicates a hardware cache event.
287 This has a special encoding, described in the
288 .I config
289 field definition.
290 .TP
291 .B PERF_TYPE_RAW
292 This indicates a "raw" implementation-specific event in the
293 .IR config " field."
294 .TP
295 .BR PERF_TYPE_BREAKPOINT " (since Linux 2.6.33)"
296 This indicates a hardware breakpoint as provided by the CPU.
297 Breakpoints can be read/write accesses to an address as well as
298 execution of an instruction address.
299 .TP
300 .RB "dynamic PMU"
301 Since Linux 2.6.39,
302 .BR perf_event_open ()
303 can support multiple PMUs.
304 To enable this, a value exported by the kernel can be used in the
305 .I type
306 field to indicate which PMU to use.
307 The value to use can be found in the sysfs filesystem:
308 there is a subdirectory per PMU instance under
309 .IR /sys/bus/event_source/devices .
310 In each subdirectory there is a
311 .I type
312 file whose content is an integer that can be used in the
313 .I type
314 field.
315 For instance,
316 .I /sys/bus/event_source/devices/cpu/type
317 contains the value for the core CPU PMU, which is usually 4.
318 .RE
319 .TP
320 .I "size"
321 The size of the
322 .I perf_event_attr
323 structure for forward/backward compatibility.
324 Set this using
325 .I sizeof(struct perf_event_attr)
326 to allow the kernel to see
327 the struct size at the time of compilation.
328
329 The related define
330 .B PERF_ATTR_SIZE_VER0
331 is set to 64; this was the size of the first published struct.
332 .B PERF_ATTR_SIZE_VER1
333 is 72, corresponding to the addition of breakpoints in Linux 2.6.33.
334 .B PERF_ATTR_SIZE_VER2
335 is 80 corresponding to the addition of branch sampling in Linux 3.4.
336 .B PERF_ATTR_SIZE_VER3
337 is 96 corresponding to the addition
338 of
339 .I sample_regs_user
340 and
341 .I sample_stack_user
342 in Linux 3.7.
343 .TP
344 .I "config"
345 This specifies which event you want, in conjunction with
346 the
347 .I type
348 field.
349 The
350 .IR config1 " and " config2
351 fields are also taken into account in cases where 64 bits is not
352 enough to fully specify the event.
353 The encoding of these fields are event dependent.
354
355 The most significant bit (bit 63) of
356 .I config
357 signifies CPU-specific (raw) counter configuration data;
358 if the most significant bit is unset, the next 7 bits are an event
359 type and the rest of the bits are the event identifier.
360
361 There are various ways to set the
362 .I config
363 field that are dependent on the value of the previously
364 described
365 .I type
366 field.
367 What follows are various possible settings for
368 .I config
369 separated out by
370 .IR type .
371
372 If
373 .I type
374 is
375 .BR PERF_TYPE_HARDWARE ,
376 we are measuring one of the generalized hardware CPU events.
377 Not all of these are available on all platforms.
378 Set
379 .I config
380 to one of the following:
381 .RS 12
382 .TP
383 .B PERF_COUNT_HW_CPU_CYCLES
384 Total cycles.
385 Be wary of what happens during CPU frequency scaling.
386 .TP
387 .B PERF_COUNT_HW_INSTRUCTIONS
388 Retired instructions.
389 Be careful, these can be affected by various
390 issues, most notably hardware interrupt counts.
391 .TP
392 .B PERF_COUNT_HW_CACHE_REFERENCES
393 Cache accesses.
394 Usually this indicates Last Level Cache accesses but this may
395 vary depending on your CPU.
396 This may include prefetches and coherency messages; again this
397 depends on the design of your CPU.
398 .TP
399 .B PERF_COUNT_HW_CACHE_MISSES
400 Cache misses.
401 Usually this indicates Last Level Cache misses; this is intended to be
402 used in conjunction with the
403 .B PERF_COUNT_HW_CACHE_REFERENCES
404 event to calculate cache miss rates.
405 .TP
406 .B PERF_COUNT_HW_BRANCH_INSTRUCTIONS
407 Retired branch instructions.
408 Prior to Linux 2.6.34, this used
409 the wrong event on AMD processors.
410 .TP
411 .B PERF_COUNT_HW_BRANCH_MISSES
412 Mispredicted branch instructions.
413 .TP
414 .B PERF_COUNT_HW_BUS_CYCLES
415 Bus cycles, which can be different from total cycles.
416 .TP
417 .BR PERF_COUNT_HW_STALLED_CYCLES_FRONTEND " (since Linux 3.0)"
418 Stalled cycles during issue.
419 .TP
420 .BR PERF_COUNT_HW_STALLED_CYCLES_BACKEND  " (since Linux 3.0)"
421 Stalled cycles during retirement.
422 .TP
423 .BR PERF_COUNT_HW_REF_CPU_CYCLES  " (since Linux 3.3)"
424 Total cycles; not affected by CPU frequency scaling.
425 .RE
426 .IP
427 If
428 .I type
429 is
430 .BR PERF_TYPE_SOFTWARE ,
431 we are measuring software events provided by the kernel.
432 Set
433 .I config
434 to one of the following:
435 .RS 12
436 .TP
437 .B PERF_COUNT_SW_CPU_CLOCK
438 This reports the CPU clock, a high-resolution per-CPU timer.
439 .TP
440 .B PERF_COUNT_SW_TASK_CLOCK
441 This reports a clock count specific to the task that is running.
442 .TP
443 .B PERF_COUNT_SW_PAGE_FAULTS
444 This reports the number of page faults.
445 .TP
446 .B PERF_COUNT_SW_CONTEXT_SWITCHES
447 This counts context switches.
448 Until Linux 2.6.34, these were all reported as user-space
449 events, after that they are reported as happening in the kernel.
450 .TP
451 .B PERF_COUNT_SW_CPU_MIGRATIONS
452 This reports the number of times the process
453 has migrated to a new CPU.
454 .TP
455 .B PERF_COUNT_SW_PAGE_FAULTS_MIN
456 This counts the number of minor page faults.
457 These did not require disk I/O to handle.
458 .TP
459 .B PERF_COUNT_SW_PAGE_FAULTS_MAJ
460 This counts the number of major page faults.
461 These required disk I/O to handle.
462 .TP
463 .BR PERF_COUNT_SW_ALIGNMENT_FAULTS " (since Linux 2.6.33)"
464 This counts the number of alignment faults.
465 These happen when unaligned memory accesses happen; the kernel
466 can handle these but it reduces performance.
467 This happens only on some architectures (never on x86).
468 .TP
469 .BR PERF_COUNT_SW_EMULATION_FAULTS " (since Linux 2.6.33)"
470 This counts the number of emulation faults.
471 The kernel sometimes traps on unimplemented instructions
472 and emulates them for user space.
473 This can negatively impact performance.
474 .TP
475 .BR PERF_COUNT_SW_DUMMY " (since Linux 3.12)"
476 This is a placeholder event that counts nothing.
477 Informational sample record types such as mmap or comm
478 must be associated with an active event.
479 This dummy event allows gathering such records without requiring
480 a counting event.
481 .RE
482
483 .RS
484 If
485 .I type
486 is
487 .BR PERF_TYPE_TRACEPOINT ,
488 then we are measuring kernel tracepoints.
489 The value to use in
490 .I config
491 can be obtained from under debugfs
492 .I tracing/events/*/*/id
493 if ftrace is enabled in the kernel.
494 .RE
495
496 .RS
497 If
498 .I type
499 is
500 .BR PERF_TYPE_HW_CACHE ,
501 then we are measuring a hardware CPU cache event.
502 To calculate the appropriate
503 .I config
504 value use the following equation:
505 .RS 4
506 .nf
507
508     (perf_hw_cache_id) | (perf_hw_cache_op_id << 8) |
509     (perf_hw_cache_op_result_id << 16)
510 .fi
511 .P
512 where
513 .I perf_hw_cache_id
514 is one of:
515 .RS 4
516 .TP
517 .B PERF_COUNT_HW_CACHE_L1D
518 for measuring Level 1 Data Cache
519 .TP
520 .B PERF_COUNT_HW_CACHE_L1I
521 for measuring Level 1 Instruction Cache
522 .TP
523 .B PERF_COUNT_HW_CACHE_LL
524 for measuring Last-Level Cache
525 .TP
526 .B PERF_COUNT_HW_CACHE_DTLB
527 for measuring the Data TLB
528 .TP
529 .B PERF_COUNT_HW_CACHE_ITLB
530 for measuring the Instruction TLB
531 .TP
532 .B PERF_COUNT_HW_CACHE_BPU
533 for measuring the branch prediction unit
534 .TP
535 .BR PERF_COUNT_HW_CACHE_NODE " (since Linux 3.0)"
536 for measuring local memory accesses
537 .RE
538 .P
539 and
540 .I perf_hw_cache_op_id
541 is one of
542 .RS 4
543 .TP
544 .B PERF_COUNT_HW_CACHE_OP_READ
545 for read accesses
546 .TP
547 .B PERF_COUNT_HW_CACHE_OP_WRITE
548 for write accesses
549 .TP
550 .B PERF_COUNT_HW_CACHE_OP_PREFETCH
551 for prefetch accesses
552 .RE
553 .P
554 and
555 .I perf_hw_cache_op_result_id
556 is one of
557 .RS 4
558 .TP
559 .B PERF_COUNT_HW_CACHE_RESULT_ACCESS
560 to measure accesses
561 .TP
562 .B PERF_COUNT_HW_CACHE_RESULT_MISS
563 to measure misses
564 .RE
565 .RE
566
567 If
568 .I type
569 is
570 .BR PERF_TYPE_RAW ,
571 then a custom "raw"
572 .I config
573 value is needed.
574 Most CPUs support events that are not covered by the "generalized" events.
575 These are implementation defined; see your CPU manual (for example
576 the Intel Volume 3B documentation or the AMD BIOS and Kernel Developer
577 Guide).
578 The libpfm4 library can be used to translate from the name in the
579 architectural manuals to the raw hex value
580 .BR perf_event_open ()
581 expects in this field.
582
583 If
584 .I type
585 is
586 .BR PERF_TYPE_BREAKPOINT ,
587 then leave
588 .I config
589 set to zero.
590 Its parameters are set in other places.
591 .RE
592 .TP
593 .IR sample_period ", " sample_freq
594 A "sampling" counter is one that generates an interrupt
595 every N events, where N is given by
596 .IR sample_period .
597 A sampling counter has
598 .IR sample_period " > 0."
599 When an overflow interrupt occurs, requested data is recorded
600 in the mmap buffer.
601 The
602 .I sample_type
603 field controls what data is recorded on each interrupt.
604
605 .I sample_freq
606 can be used if you wish to use frequency rather than period.
607 In this case, you set the
608 .I freq
609 flag.
610 The kernel will adjust the sampling period
611 to try and achieve the desired rate.
612 The rate of adjustment is a
613 timer tick.
614 .TP
615 .I "sample_type"
616 The various bits in this field specify which values to include
617 in the sample.
618 They will be recorded in a ring-buffer,
619 which is available to user space using
620 .BR mmap (2).
621 The order in which the values are saved in the
622 sample are documented in the MMAP Layout subsection below;
623 it is not the
624 .I "enum perf_event_sample_format"
625 order.
626 .RS
627 .TP
628 .B PERF_SAMPLE_IP
629 Records instruction pointer.
630 .TP
631 .B PERF_SAMPLE_TID
632 Records the process and thread IDs.
633 .TP
634 .B PERF_SAMPLE_TIME
635 Records a timestamp.
636 .TP
637 .B PERF_SAMPLE_ADDR
638 Records an address, if applicable.
639 .TP
640 .B PERF_SAMPLE_READ
641 Record counter values for all events in a group, not just the group leader.
642 .TP
643 .B PERF_SAMPLE_CALLCHAIN
644 Records the callchain (stack backtrace).
645 .TP
646 .B PERF_SAMPLE_ID
647 Records a unique ID for the opened event's group leader.
648 .TP
649 .B PERF_SAMPLE_CPU
650 Records CPU number.
651 .TP
652 .B PERF_SAMPLE_PERIOD
653 Records the current sampling period.
654 .TP
655 .B PERF_SAMPLE_STREAM_ID
656 Records a unique ID for the opened event.
657 Unlike
658 .B PERF_SAMPLE_ID
659 the actual ID is returned, not the group leader.
660 This ID is the same as the one returned by
661 .BR PERF_FORMAT_ID .
662 .TP
663 .B PERF_SAMPLE_RAW
664 Records additional data, if applicable.
665 Usually returned by tracepoint events.
666 .TP
667 .BR PERF_SAMPLE_BRANCH_STACK " (since Linux 3.4)"
668 This provides a record of recent branches, as provided
669 by CPU branch sampling hardware (such as Intel Last Branch Record).
670 Not all hardware supports this feature.
671
672 See the
673 .I branch_sample_type
674 field for how to filter which branches are reported.
675 .TP
676 .BR PERF_SAMPLE_REGS_USER " (since Linux 3.7)"
677 Records the current user-level CPU register state
678 (the values in the process before the kernel was called).
679 .TP
680 .BR PERF_SAMPLE_STACK_USER " (since Linux 3.7)"
681 Records the user level stack, allowing stack unwinding.
682 .TP
683 .BR PERF_SAMPLE_WEIGHT " (since Linux 3.10)"
684 Records a hardware provided weight value that expresses how
685 costly the sampled event was.
686 This allows the hardware to highlight expensive events in
687 a profile.
688 .TP
689 .BR PERF_SAMPLE_DATA_SRC " (since Linux 3.10)"
690 Records the data source: where in the memory hierarchy
691 the data associated with the sampled instruction came from.
692 This is only available if the underlying hardware
693 supports this feature.
694 .TP
695 .BR PERF_SAMPLE_IDENTIFIER " (since Linux 3.12)"
696 Places the
697 .B SAMPLE_ID
698 value in a fixed position in the record,
699 either at the beginning (for sample events) or at the end
700 (if a non-sample event).
701
702 This was necessary because a sample stream may have
703 records from various different event sources with different
704 .I sample_type
705 settings.
706 Parsing the event stream properly was not possible because the
707 format of the record was needed to find
708 .BR SAMPLE_ID ,
709 but
710 the format could not be found without knowing what
711 event the sample belonged to (causing a circular
712 dependency).
713
714 This new
715 .B PERF_SAMPLE_IDENTIFIER
716 setting makes the event stream always parsable
717 by putting
718 .B SAMPLE_ID
719 in a fixed location, even though
720 it means having duplicate
721 .B SAMPLE_ID
722 values in records.
723 .TP
724 .BR PERF_SAMPLE_TRANSACTION " (Since Linux 3.13)"
725 Records reasons for transactional memory abort events
726 (for example, from Intel TSX transactional memory support).
727
728 The
729 .I precise_ip
730 setting must be greater than 0 and a transactional memory abort
731 event must be measured or no values will be recorded.
732 Also note that some perf_event measurements, such as sampled
733 cycle counting, may cause extraneous aborts (by causing an
734 interrupt during a transaction).
735 .RE
736 .TP
737 .IR "read_format"
738 This field specifies the format of the data returned by
739 .BR read (2)
740 on a
741 .BR perf_event_open ()
742 file descriptor.
743 .RS
744 .TP
745 .B PERF_FORMAT_TOTAL_TIME_ENABLED
746 Adds the 64-bit
747 .I time_enabled
748 field.
749 This can be used to calculate estimated totals if
750 the PMU is overcommitted and multiplexing is happening.
751 .TP
752 .B PERF_FORMAT_TOTAL_TIME_RUNNING
753 Adds the 64-bit
754 .I time_running
755 field.
756 This can be used to calculate estimated totals if
757 the PMU is overcommitted and multiplexing is happening.
758 .TP
759 .B PERF_FORMAT_ID
760 Adds a 64-bit unique value that corresponds to the event group.
761 .TP
762 .B PERF_FORMAT_GROUP
763 Allows all counter values in an event group to be read with one read.
764 .RE
765 .TP
766 .IR "disabled"
767 The
768 .I disabled
769 bit specifies whether the counter starts out disabled or enabled.
770 If disabled, the event can later be enabled by
771 .BR ioctl (2),
772 .BR prctl (2),
773 or
774 .IR enable_on_exec .
775
776 When creating an event group, typically the group leader is initialized
777 with
778 .I disabled
779 set to 1 and any child events are initialized with
780 .I disabled
781 set to 0.
782 Despite
783 .I disabled
784 being 0, the child events will not start until the group leader
785 is enabled.
786 .TP
787 .IR "inherit"
788 The
789 .I inherit
790 bit specifies that this counter should count events of child
791 tasks as well as the task specified.
792 This applies only to new children, not to any existing children at
793 the time the counter is created (nor to any new children of
794 existing children).
795
796 Inherit does not work for some combinations of
797 .IR read_format s,
798 such as
799 .BR PERF_FORMAT_GROUP .
800 .TP
801 .IR "pinned"
802 The
803 .I pinned
804 bit specifies that the counter should always be on the CPU if at all
805 possible.
806 It applies only to hardware counters and only to group leaders.
807 If a pinned counter cannot be put onto the CPU (e.g., because there are
808 not enough hardware counters or because of a conflict with some other
809 event), then the counter goes into an 'error' state, where reads
810 return end-of-file (i.e.,
811 .BR read (2)
812 returns 0) until the counter is subsequently enabled or disabled.
813 .TP
814 .IR "exclusive"
815 The
816 .I exclusive
817 bit specifies that when this counter's group is on the CPU,
818 it should be the only group using the CPU's counters.
819 In the future this may allow monitoring programs to
820 support PMU features that need to run alone so that they do not
821 disrupt other hardware counters.
822
823 Note that many unexpected situations may prevent events with the
824 .I exclusive
825 bit set from ever running.
826 This includes any users running a system-wide
827 measurement as well as any kernel use of the performance counters
828 (including the commonly enabled NMI Watchdog Timer interface).
829 .TP
830 .IR "exclude_user"
831 If this bit is set, the count excludes events that happen in user space.
832 .TP
833 .IR "exclude_kernel"
834 If this bit is set, the count excludes events that happen in kernel-space.
835 .TP
836 .IR "exclude_hv"
837 If this bit is set, the count excludes events that happen in the
838 hypervisor.
839 This is mainly for PMUs that have built-in support for handling this
840 (such as POWER).
841 Extra support is needed for handling hypervisor measurements on most
842 machines.
843 .TP
844 .IR "exclude_idle"
845 If set, don't count when the CPU is idle.
846 .TP
847 .IR "mmap"
848 The
849 .I mmap
850 bit enables generation of
851 .B PERF_RECORD_MMAP
852 samples for every
853 .BR mmap (2)
854 call that has
855 .B PROT_EXEC
856 set.
857 This allows tools to notice new executable code being mapped into
858 a program (dynamic shared libraries for example)
859 so that addresses can be mapped back to the original code.
860 .TP
861 .IR "comm"
862 The
863 .I comm
864 bit enables tracking of process command name as modified by the
865 .BR exec (2)
866 and
867 .BR prctl (PR_SET_NAME)
868 system calls as well as writing to
869 .IR /proc/self/comm .
870 If the
871 .I comm_exec
872 flag is also successfully set (possible since Linux 3.16),
873 then the misc flag
874 .B PERF_RECORD_MISC_COMM_EXEC
875 can be used to differentiate the
876 .BR exec (2)
877 case from the others.
878 .TP
879 .IR "freq"
880 If this bit is set, then
881 .I sample_frequency
882 not
883 .I sample_period
884 is used when setting up the sampling interval.
885 .TP
886 .IR "inherit_stat"
887 This bit enables saving of event counts on context switch for
888 inherited tasks.
889 This is meaningful only if the
890 .I inherit
891 field is set.
892 .TP
893 .IR "enable_on_exec"
894 If this bit is set, a counter is automatically
895 enabled after a call to
896 .BR exec (2).
897 .TP
898 .IR "task"
899 If this bit is set, then
900 fork/exit notifications are included in the ring buffer.
901 .TP
902 .IR "watermark"
903 If set, have a sampling interrupt happen when we cross the
904 .I wakeup_watermark
905 boundary.
906 Otherwise, interrupts happen after
907 .I wakeup_events
908 samples.
909 .TP
910 .IR "precise_ip" " (since Linux 2.6.35)"
911 This controls the amount of skid.
912 Skid is how many instructions
913 execute between an event of interest happening and the kernel
914 being able to stop and record the event.
915 Smaller skid is
916 better and allows more accurate reporting of which events
917 correspond to which instructions, but hardware is often limited
918 with how small this can be.
919
920 The values of this are the following:
921 .RS
922 .TP
923 0 -
924 .B SAMPLE_IP
925 can have arbitrary skid.
926 .TP
927 1 -
928 .B SAMPLE_IP
929 must have constant skid.
930 .TP
931 2 -
932 .B SAMPLE_IP
933 requested to have 0 skid.
934 .TP
935 3 -
936 .B SAMPLE_IP
937 must have 0 skid.
938 See also
939 .BR PERF_RECORD_MISC_EXACT_IP .
940 .RE
941 .TP
942 .IR "mmap_data" " (since Linux 2.6.36)"
943 The counterpart of the
944 .I mmap
945 field.
946 This enables generation of
947 .B PERF_RECORD_MMAP
948 samples for
949 .BR mmap (2)
950 calls that do not have
951 .B PROT_EXEC
952 set (for example data and SysV shared memory).
953 .TP
954 .IR "sample_id_all" " (since Linux 2.6.38)"
955 If set, then TID, TIME, ID, STREAM_ID, and CPU can
956 additionally be included in
957 .RB non- PERF_RECORD_SAMPLE s
958 if the corresponding
959 .I sample_type
960 is selected.
961
962 If
963 .B PERF_SAMPLE_IDENTIFIER
964 is specified, then an additional ID value is included
965 as the last value to ease parsing the record stream.
966 This may lead to the
967 .I id
968 value appearing twice.
969
970 The layout is described by this pseudo-structure:
971 .in +4n
972 .nf
973 struct sample_id {
974     { u32 pid, tid; } /* if PERF_SAMPLE_TID set        */
975     { u64 time;     } /* if PERF_SAMPLE_TIME set       */
976     { u64 id;       } /* if PERF_SAMPLE_ID set         */
977     { u64 stream_id;} /* if PERF_SAMPLE_STREAM_ID set  */
978     { u32 cpu, res; } /* if PERF_SAMPLE_CPU set        */
979     { u64 id;       } /* if PERF_SAMPLE_IDENTIFIER set */
980 };
981 .fi
982 .TP
983 .IR "exclude_host" " (since Linux 3.2)"
984 Do not measure time spent in VM host.
985 .TP
986 .IR "exclude_guest" " (since Linux 3.2)"
987 Do not measure time spent in VM guest.
988 .TP
989 .IR "exclude_callchain_kernel" " (since Linux 3.7)"
990 Do not include kernel callchains.
991 .TP
992 .IR "exclude_callchain_user" " (since Linux 3.7)"
993 Do not include user callchains.
994 .TP
995 .IR "mmap2" " (since Linux 3.16)"
996 Generate an extended executable mmap record that contains enough
997 additional information to uniquely identify shared mappings.
998 The
999 .I mmap
1000 flag must also be set for this to work.
1001 .TP
1002 .IR "comm_exec" " (since Linux 3.16)"
1003 This is purely a feature-detection flag, it does not change
1004 kernel behavior.
1005 If this flag can successfully be set, then, when
1006 .I comm
1007 is enabled, the
1008 .B PERF_RECORD_MISC_COMM_EXEC
1009 flag will be set in the
1010 .I misc
1011 field of a comm record header if the rename event being
1012 reported was caused by a call to
1013 .BR exec (2).
1014 This allows tools to distinguish between the various
1015 types of process renaming.
1016 .TP
1017 .IR "wakeup_events" ", " "wakeup_watermark"
1018 This union sets how many samples
1019 .RI ( wakeup_events )
1020 or bytes
1021 .RI ( wakeup_watermark )
1022 happen before an overflow signal happens.
1023 Which one is used is selected by the
1024 .I watermark
1025 bit flag.
1026
1027 .I wakeup_events
1028 only counts
1029 .B PERF_RECORD_SAMPLE
1030 record types.
1031 To receive a signal for every incoming
1032 .B PERF_RECORD
1033 type set
1034 .I wakeup_watermark
1035 to 1.
1036 .TP
1037 .IR "bp_type" " (since Linux 2.6.33)"
1038 This chooses the breakpoint type.
1039 It is one of:
1040 .RS
1041 .TP
1042 .BR HW_BREAKPOINT_EMPTY
1043 No breakpoint.
1044 .TP
1045 .BR HW_BREAKPOINT_R
1046 Count when we read the memory location.
1047 .TP
1048 .BR HW_BREAKPOINT_W
1049 Count when we write the memory location.
1050 .TP
1051 .BR HW_BREAKPOINT_RW
1052 Count when we read or write the memory location.
1053 .TP
1054 .BR HW_BREAKPOINT_X
1055 Count when we execute code at the memory location.
1056 .LP
1057 The values can be combined via a bitwise or, but the
1058 combination of
1059 .B HW_BREAKPOINT_R
1060 or
1061 .B HW_BREAKPOINT_W
1062 with
1063 .B HW_BREAKPOINT_X
1064 is not allowed.
1065 .RE
1066 .TP
1067 .IR "bp_addr" " (since Linux 2.6.33)"
1068 .I bp_addr
1069 address of the breakpoint.
1070 For execution breakpoints this is the memory address of the instruction
1071 of interest; for read and write breakpoints it is the memory address
1072 of the memory location of interest.
1073 .TP
1074 .IR "config1" " (since Linux 2.6.39)"
1075 .I config1
1076 is used for setting events that need an extra register or otherwise
1077 do not fit in the regular config field.
1078 Raw OFFCORE_EVENTS on Nehalem/Westmere/SandyBridge use this field
1079 on 3.3 and later kernels.
1080 .TP
1081 .IR "bp_len" " (since Linux 2.6.33)"
1082 .I bp_len
1083 is the length of the breakpoint being measured if
1084 .I type
1085 is
1086 .BR PERF_TYPE_BREAKPOINT .
1087 Options are
1088 .BR HW_BREAKPOINT_LEN_1 ,
1089 .BR HW_BREAKPOINT_LEN_2 ,
1090 .BR HW_BREAKPOINT_LEN_4 ,
1091 .BR HW_BREAKPOINT_LEN_8 .
1092 For an execution breakpoint, set this to
1093 .IR sizeof(long) .
1094 .TP
1095 .IR "config2" " (since Linux 2.6.39)"
1096
1097 .I config2
1098 is a further extension of the
1099 .I config1
1100 field.
1101 .TP
1102 .IR "branch_sample_type" " (since Linux 3.4)"
1103 If
1104 .B PERF_SAMPLE_BRANCH_STACK
1105 is enabled, then this specifies what branches to include
1106 in the branch record.
1107
1108 The first part of the value is the privilege level, which
1109 is a combination of one of the following values.
1110 If the user does not set privilege level explicitly, the kernel
1111 will use the event's privilege level.
1112 Event and branch privilege levels do not have to match.
1113 .RS
1114 .TP
1115 .B PERF_SAMPLE_BRANCH_USER
1116 Branch target is in user space.
1117 .TP
1118 .B PERF_SAMPLE_BRANCH_KERNEL
1119 Branch target is in kernel space.
1120 .TP
1121 .B PERF_SAMPLE_BRANCH_HV
1122 Branch target is in hypervisor.
1123 .TP
1124 .B PERF_SAMPLE_BRANCH_PLM_ALL
1125 A convenience value that is the three preceding values ORed together.
1126
1127 .P
1128 In addition to the privilege value, at least one or more of the
1129 following bits must be set.
1130
1131 .TP
1132 .B PERF_SAMPLE_BRANCH_ANY
1133 Any branch type.
1134 .TP
1135 .B PERF_SAMPLE_BRANCH_ANY_CALL
1136 Any call branch.
1137 .TP
1138 .B PERF_SAMPLE_BRANCH_ANY_RETURN
1139 Any return branch.
1140 .TP
1141 .B PERF_SAMPLE_BRANCH_IND_CALL
1142 Indirect calls.
1143 .TP
1144 .BR PERF_SAMPLE_BRANCH_COND " (since Linux 3.16)"
1145 Conditional branches.
1146 .TP
1147 .BR PERF_SAMPLE_BRANCH_ABORT_TX " (since Linux 3.11)"
1148 Transactional memory aborts.
1149 .TP
1150 .BR PERF_SAMPLE_BRANCH_IN_TX " (since Linux 3.11)"
1151 Branch in transactional memory transaction.
1152 .TP
1153 .BR PERF_SAMPLE_BRANCH_NO_TX " (since Linux 3.11)"
1154 Branch not in transactional memory transaction.
1155 .RE
1156
1157 .TP
1158 .IR "sample_regs_user" " (since Linux 3.7)"
1159 This bit mask defines the set of user CPU registers to dump on samples.
1160 The layout of the register mask is architecture-specific and
1161 described in the kernel header
1162 .IR arch/ARCH/include/uapi/asm/perf_regs.h .
1163 .TP
1164 .IR "sample_stack_user" " (since Linux 3.7)"
1165 This defines the size of the user stack to dump if
1166 .B PERF_SAMPLE_STACK_USER
1167 is specified.
1168 .SS Reading results
1169 Once a
1170 .BR perf_event_open ()
1171 file descriptor has been opened, the values
1172 of the events can be read from the file descriptor.
1173 The values that are there are specified by the
1174 .I read_format
1175 field in the
1176 .I attr
1177 structure at open time.
1178
1179 If you attempt to read into a buffer that is not big enough to hold the
1180 data
1181 .B ENOSPC
1182 is returned
1183
1184 Here is the layout of the data returned by a read:
1185 .IP * 2
1186 If
1187 .B PERF_FORMAT_GROUP
1188 was specified to allow reading all events in a group at once:
1189
1190 .in +4n
1191 .nf
1192 struct read_format {
1193     u64 nr;            /* The number of events */
1194     u64 time_enabled;  /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
1195     u64 time_running;  /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
1196     struct {
1197         u64 value;     /* The value of the event */
1198         u64 id;        /* if PERF_FORMAT_ID */
1199     } values[nr];
1200 };
1201 .fi
1202 .in
1203 .IP *
1204 If
1205 .B PERF_FORMAT_GROUP
1206 was
1207 .I not
1208 specified:
1209
1210 .in +4n
1211 .nf
1212 struct read_format {
1213     u64 value;         /* The value of the event */
1214     u64 time_enabled;  /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
1215     u64 time_running;  /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
1216     u64 id;            /* if PERF_FORMAT_ID */
1217 };
1218 .fi
1219 .in
1220 .PP
1221 The values read are as follows:
1222 .TP
1223 .I nr
1224 The number of events in this file descriptor.
1225 Only available if
1226 .B PERF_FORMAT_GROUP
1227 was specified.
1228 .TP
1229 .IR time_enabled ", " time_running
1230 Total time the event was enabled and running.
1231 Normally these are the same.
1232 If more events are started,
1233 then available counter slots on the PMU, then multiplexing
1234 happens and events run only part of the time.
1235 In that case, the
1236 .I time_enabled
1237 and
1238 .I time running
1239 values can be used to scale an estimated value for the count.
1240 .TP
1241 .I value
1242 An unsigned 64-bit value containing the counter result.
1243 .TP
1244 .I id
1245 A globally unique value for this particular event, only there if
1246 .B PERF_FORMAT_ID
1247 was specified in
1248 .IR read_format .
1249 .SS MMAP layout
1250 When using
1251 .BR perf_event_open ()
1252 in sampled mode, asynchronous events
1253 (like counter overflow or
1254 .B PROT_EXEC
1255 mmap tracking)
1256 are logged into a ring-buffer.
1257 This ring-buffer is created and accessed through
1258 .BR mmap (2).
1259
1260 The mmap size should be 1+2^n pages, where the first page is a
1261 metadata page
1262 .RI ( "struct perf_event_mmap_page" )
1263 that contains various
1264 bits of information such as where the ring-buffer head is.
1265
1266 Before kernel 2.6.39, there is a bug that means you must allocate a mmap
1267 ring buffer when sampling even if you do not plan to access it.
1268
1269 The structure of the first metadata mmap page is as follows:
1270
1271 .in +4n
1272 .nf
1273 struct perf_event_mmap_page {
1274     __u32 version;        /* version number of this structure */
1275     __u32 compat_version; /* lowest version this is compat with */
1276     __u32 lock;           /* seqlock for synchronization */
1277     __u32 index;          /* hardware counter identifier */
1278     __s64 offset;         /* add to hardware counter value */
1279     __u64 time_enabled;   /* time event active */
1280     __u64 time_running;   /* time event on CPU */
1281     union {
1282         __u64   capabilities;
1283         struct {
1284             __u64 cap_usr_time / cap_usr_rdpmc / cap_bit0 : 1,
1285                   cap_bit0_is_deprecated : 1,
1286                   cap_user_rdpmc         : 1,
1287                   cap_user_time          : 1,
1288                   cap_user_time_zero     : 1,
1289         };
1290     };
1291     __u16 pmc_width;
1292     __u16 time_shift;
1293     __u32 time_mult;
1294     __u64 time_offset;
1295     __u64 __reserved[120];   /* Pad to 1k */
1296     __u64 data_head;         /* head in the data section */
1297     __u64 data_tail;         /* user-space written tail */
1298 }
1299 .fi
1300 .in
1301
1302 The following list describes the fields in the
1303 .I perf_event_mmap_page
1304 structure in more detail:
1305 .TP
1306 .I version
1307 Version number of this structure.
1308 .TP
1309 .I compat_version
1310 The lowest version this is compatible with.
1311 .TP
1312 .I lock
1313 A seqlock for synchronization.
1314 .TP
1315 .I index
1316 A unique hardware counter identifier.
1317 .TP
1318 .I offset
1319 When using rdpmc for reads this offset value
1320 must be added to the one returned by rdpmc to get
1321 the current total event count.
1322 .TP
1323 .I time_enabled
1324 Time the event was active.
1325 .TP
1326 .I time_running
1327 Time the event was running.
1328 .TP
1329 .IR cap_usr_time " / " cap_usr_rdpmc " / " cap_bit0 " (since Linux 3.4)"
1330 There was a bug in the definition of
1331 .I cap_usr_time
1332 and
1333 .I cap_usr_rdpmc
1334 from Linux 3.4 until Linux 3.11.
1335 Both bits were defined to point to the same location, so it was
1336 impossible to know if
1337 .I cap_usr_time
1338 or
1339 .I cap_usr_rdpmc
1340 were actually set.
1341
1342 Starting with 3.12 these are renamed to
1343 .I cap_bit0
1344 and you should use the new
1345 .I cap_user_time
1346 and
1347 .I cap_user_rdpmc
1348 fields instead.
1349
1350 .TP
1351 .IR cap_bit0_is_deprecated " (since Linux 3.12)"
1352 If set, this bit indicates that the kernel supports
1353 the properly separated
1354 .I cap_user_time
1355 and
1356 .I cap_user_rdpmc
1357 bits.
1358
1359 If not-set, it indicates an older kernel where
1360 .I cap_usr_time
1361 and
1362 .I cap_usr_rdpmc
1363 map to the same bit and thus both features should
1364 be used with caution.
1365
1366 .TP
1367 .IR cap_user_rdpmc " (since Linux 3.12)"
1368 If the hardware supports user-space read of performance counters
1369 without syscall (this is the "rdpmc" instruction on x86), then
1370 the following code can be used to do a read:
1371
1372 .in +4n
1373 .nf
1374 u32 seq, time_mult, time_shift, idx, width;
1375 u64 count, enabled, running;
1376 u64 cyc, time_offset;
1377
1378 do {
1379     seq = pc\->lock;
1380     barrier();
1381     enabled = pc\->time_enabled;
1382     running = pc\->time_running;
1383
1384     if (pc\->cap_usr_time && enabled != running) {
1385         cyc = rdtsc();
1386         time_offset = pc\->time_offset;
1387         time_mult   = pc\->time_mult;
1388         time_shift  = pc\->time_shift;
1389     }
1390
1391     idx = pc\->index;
1392     count = pc\->offset;
1393
1394     if (pc\->cap_usr_rdpmc && idx) {
1395         width = pc\->pmc_width;
1396         count += rdpmc(idx \- 1);
1397     }
1398
1399     barrier();
1400 } while (pc\->lock != seq);
1401 .fi
1402 .in
1403 .TP
1404 .IR cap_user_time " (since Linux 3.12)"
1405 This bit indicates the hardware has a constant, nonstop
1406 timestamp counter (TSC on x86).
1407 .TP
1408 .IR cap_user_time_zero " (since Linux 3.12)"
1409 Indicates the presence of
1410 .I time_zero
1411 which allows mapping timestamp values to
1412 the hardware clock.
1413 .TP
1414 .I pmc_width
1415 If
1416 .IR cap_usr_rdpmc ,
1417 this field provides the bit-width of the value
1418 read using the rdpmc or equivalent instruction.
1419 This can be used to sign extend the result like:
1420
1421 .in +4n
1422 .nf
1423 pmc <<= 64 \- pmc_width;
1424 pmc >>= 64 \- pmc_width; // signed shift right
1425 count += pmc;
1426 .fi
1427 .in
1428 .TP
1429 .IR time_shift ", " time_mult ", " time_offset
1430
1431 If
1432 .IR cap_usr_time ,
1433 these fields can be used to compute the time
1434 delta since time_enabled (in nanoseconds) using rdtsc or similar.
1435 .nf
1436
1437     u64 quot, rem;
1438     u64 delta;
1439     quot = (cyc >> time_shift);
1440     rem = cyc & ((1 << time_shift) \- 1);
1441     delta = time_offset + quot * time_mult +
1442             ((rem * time_mult) >> time_shift);
1443 .fi
1444
1445 Where
1446 .IR time_offset ,
1447 .IR time_mult ,
1448 .IR time_shift ,
1449 and
1450 .IR cyc
1451 are read in the
1452 seqcount loop described above.
1453 This delta can then be added to
1454 enabled and possible running (if idx), improving the scaling:
1455 .nf
1456
1457     enabled += delta;
1458     if (idx)
1459         running += delta;
1460     quot = count / running;
1461     rem  = count % running;
1462     count = quot * enabled + (rem * enabled) / running;
1463 .fi
1464 .TP
1465 .IR time_zero " (since Linux 3.12)"
1466
1467 If
1468 .I cap_usr_time_zero
1469 is set, then the hardware clock (the TSC timestamp counter on x86)
1470 can be calculated from the
1471 .IR time_zero ", " time_mult ", and " time_shift " values:"
1472
1473 .nf
1474     time = timestamp - time_zero;
1475     quot = time / time_mult;
1476     rem  = time % time_mult;
1477     cyc = (quot << time_shift) + (rem << time_shift) / time_mult;
1478 .fi
1479
1480 And vice versa:
1481
1482 .nf
1483     quot = cyc >> time_shift;
1484     rem  = cyc & ((1 << time_shift) - 1);
1485     timestamp = time_zero + quot * time_mult +
1486         ((rem * time_mult) >> time_shift);
1487 .fi
1488 .TP
1489 .I data_head
1490 This points to the head of the data section.
1491 The value continuously increases, it does not wrap.
1492 The value needs to be manually wrapped by the size of the mmap buffer
1493 before accessing the samples.
1494
1495 On SMP-capable platforms, after reading the
1496 .I data_head
1497 value,
1498 user space should issue an rmb().
1499 .TP
1500 .I data_tail
1501 When the mapping is
1502 .BR PROT_WRITE ,
1503 the
1504 .I data_tail
1505 value should be written by user space to reflect the last read data.
1506 In this case, the kernel will not overwrite unread data.
1507 .PP
1508 The following 2^n ring-buffer pages have the layout described below.
1509
1510 If
1511 .I perf_event_attr.sample_id_all
1512 is set, then all event types will
1513 have the sample_type selected fields related to where/when (identity)
1514 an event took place (TID, TIME, ID, CPU, STREAM_ID) described in
1515 .B PERF_RECORD_SAMPLE
1516 below, it will be stashed just after the
1517 .I perf_event_header
1518 and the fields already present for the existing
1519 fields, that is, at the end of the payload.
1520 That way a newer perf.data
1521 file will be supported by older perf tools, with these new optional
1522 fields being ignored.
1523
1524 The mmap values start with a header:
1525
1526 .in +4n
1527 .nf
1528 struct perf_event_header {
1529     __u32   type;
1530     __u16   misc;
1531     __u16   size;
1532 };
1533 .fi
1534 .in
1535
1536 Below, we describe the
1537 .I perf_event_header
1538 fields in more detail.
1539 For ease of reading,
1540 the fields with shorter descriptions are presented first.
1541 .TP
1542 .I size
1543 This indicates the size of the record.
1544 .TP
1545 .I misc
1546 The
1547 .I misc
1548 field contains additional information about the sample.
1549
1550 The CPU mode can be determined from this value by masking with
1551 .B PERF_RECORD_MISC_CPUMODE_MASK
1552 and looking for one of the following (note these are not
1553 bit masks, only one can be set at a time):
1554 .RS
1555 .TP
1556 .B PERF_RECORD_MISC_CPUMODE_UNKNOWN
1557 Unknown CPU mode.
1558 .TP
1559 .B PERF_RECORD_MISC_KERNEL
1560 Sample happened in the kernel.
1561 .TP
1562 .B PERF_RECORD_MISC_USER
1563 Sample happened in user code.
1564 .TP
1565 .B PERF_RECORD_MISC_HYPERVISOR
1566 Sample happened in the hypervisor.
1567 .TP
1568 .B PERF_RECORD_MISC_GUEST_KERNEL
1569 Sample happened in the guest kernel.
1570 .TP
1571 .B PERF_RECORD_MISC_GUEST_USER
1572 Sample happened in guest user code.
1573 .RE
1574
1575 .RS
1576 In addition, one of the following bits can be set:
1577 .TP
1578 .B PERF_RECORD_MISC_MMAP_DATA
1579 This is set when the mapping is not executable;
1580 otherwise the mapping is executable.
1581 .TP
1582 .B PERF_RECORD_MISC_COMM_EXEC
1583 This is set for a
1584 .B PERF_RECORD_COMM
1585 record on kernels more recent than Linux 3.16
1586 if a process name change was caused by an
1587 .BR exec (2)
1588 system call.
1589 It is an alias for
1590 .B PERF_RECORD_MISC_MMAP_DATA
1591 since the two values would not be set in the same record.
1592 .TP
1593 .B PERF_RECORD_MISC_EXACT_IP
1594 This indicates that the content of
1595 .B PERF_SAMPLE_IP
1596 points
1597 to the actual instruction that triggered the event.
1598 See also
1599 .IR perf_event_attr.precise_ip .
1600 .TP
1601 .B PERF_RECORD_MISC_EXT_RESERVED
1602 This indicates there is extended data available (currently not used).
1603 .RE
1604 .TP
1605 .I type
1606 The
1607 .I type
1608 value is one of the below.
1609 The values in the corresponding record (that follows the header)
1610 depend on the
1611 .I type
1612 selected as shown.
1613
1614 .RS
1615 .TP 4
1616 .B PERF_RECORD_MMAP
1617 The MMAP events record the
1618 .B PROT_EXEC
1619 mappings so that we can correlate
1620 user-space IPs to code.
1621 They have the following structure:
1622
1623 .in +4n
1624 .nf
1625 struct {
1626     struct perf_event_header header;
1627     u32    pid, tid;
1628     u64    addr;
1629     u64    len;
1630     u64    pgoff;
1631     char   filename[];
1632 };
1633 .fi
1634 .in
1635 .RS
1636 .TP
1637 .I pid
1638 is the process ID.
1639 .TP
1640 .I tid
1641 is the thread ID.
1642 .TP
1643 .I addr
1644 is the address of the allocated memory.
1645 .I len
1646 is the length of the allocated memory.
1647 .I pgoff
1648 is the page offset of the allocated memory.
1649 .I filename
1650 is a string describing the backing of the allocated memory.
1651 .RE
1652 .TP
1653 .B PERF_RECORD_LOST
1654 This record indicates when events are lost.
1655
1656 .in +4n
1657 .nf
1658 struct {
1659     struct perf_event_header header;
1660     u64 id;
1661     u64 lost;
1662     struct sample_id sample_id;
1663 };
1664 .fi
1665 .in
1666 .RS
1667 .TP
1668 .I id
1669 is the unique event ID for the samples that were lost.
1670 .TP
1671 .I lost
1672 is the number of events that were lost.
1673 .RE
1674 .TP
1675 .B PERF_RECORD_COMM
1676 This record indicates a change in the process name.
1677
1678 .in +4n
1679 .nf
1680 struct {
1681     struct perf_event_header header;
1682     u32 pid;
1683     u32 tid;
1684     char comm[];
1685     struct sample_id sample_id;
1686 };
1687 .fi
1688 .in
1689 .RS
1690 .TP
1691 .I pid
1692 is the process ID.
1693 .TP
1694 .I tid
1695 is the thread ID.
1696 .TP
1697 .I comm
1698 is a string containing the new name of the process.
1699 .RE
1700 .TP
1701 .B PERF_RECORD_EXIT
1702 This record indicates a process exit event.
1703
1704 .in +4n
1705 .nf
1706 struct {
1707     struct perf_event_header header;
1708     u32 pid, ppid;
1709     u32 tid, ptid;
1710     u64 time;
1711     struct sample_id sample_id;
1712 };
1713 .fi
1714 .in
1715 .TP
1716 .BR PERF_RECORD_THROTTLE ", " PERF_RECORD_UNTHROTTLE
1717 This record indicates a throttle/unthrottle event.
1718
1719 .in +4n
1720 .nf
1721 struct {
1722     struct perf_event_header header;
1723     u64 time;
1724     u64 id;
1725     u64 stream_id;
1726     struct sample_id sample_id;
1727 };
1728 .fi
1729 .in
1730 .TP
1731 .B PERF_RECORD_FORK
1732 This record indicates a fork event.
1733
1734 .in +4n
1735 .nf
1736 struct {
1737     struct perf_event_header header;
1738     u32 pid, ppid;
1739     u32 tid, ptid;
1740     u64 time;
1741     struct sample_id sample_id;
1742 };
1743 .fi
1744 .in
1745 .TP
1746 .B PERF_RECORD_READ
1747 This record indicates a read event.
1748
1749 .in +4n
1750 .nf
1751 struct {
1752     struct perf_event_header header;
1753     u32 pid, tid;
1754     struct read_format values;
1755     struct sample_id sample_id;
1756 };
1757 .fi
1758 .in
1759 .TP
1760 .B PERF_RECORD_SAMPLE
1761 This record indicates a sample.
1762
1763 .in +4n
1764 .nf
1765 struct {
1766     struct perf_event_header header;
1767     u64   sample_id;  /* if PERF_SAMPLE_IDENTIFIER */
1768     u64   ip;         /* if PERF_SAMPLE_IP */
1769     u32   pid, tid;   /* if PERF_SAMPLE_TID */
1770     u64   time;       /* if PERF_SAMPLE_TIME */
1771     u64   addr;       /* if PERF_SAMPLE_ADDR */
1772     u64   id;         /* if PERF_SAMPLE_ID */
1773     u64   stream_id;  /* if PERF_SAMPLE_STREAM_ID */
1774     u32   cpu, res;   /* if PERF_SAMPLE_CPU */
1775     u64   period;     /* if PERF_SAMPLE_PERIOD */
1776     struct read_format v; /* if PERF_SAMPLE_READ */
1777     u64   nr;         /* if PERF_SAMPLE_CALLCHAIN */
1778     u64   ips[nr];    /* if PERF_SAMPLE_CALLCHAIN */
1779     u32   size;       /* if PERF_SAMPLE_RAW */
1780     char  data[size]; /* if PERF_SAMPLE_RAW */
1781     u64   bnr;        /* if PERF_SAMPLE_BRANCH_STACK */
1782     struct perf_branch_entry lbr[bnr];
1783                       /* if PERF_SAMPLE_BRANCH_STACK */
1784     u64   abi;        /* if PERF_SAMPLE_REGS_USER */
1785     u64   regs[weight(mask)];
1786                       /* if PERF_SAMPLE_REGS_USER */
1787     u64   size;       /* if PERF_SAMPLE_STACK_USER */
1788     char  data[size]; /* if PERF_SAMPLE_STACK_USER */
1789     u64   dyn_size;   /* if PERF_SAMPLE_STACK_USER */
1790     u64   weight;     /* if PERF_SAMPLE_WEIGHT */
1791     u64   data_src;   /* if PERF_SAMPLE_DATA_SRC */
1792     u64   transaction;/* if PERF_SAMPLE_TRANSACTION */
1793 };
1794 .fi
1795 .RS 4
1796 .TP 4
1797 .I sample_id
1798 If
1799 .B PERF_SAMPLE_IDENTIFIER
1800 is enabled, a 64-bit unique ID is included.
1801 This is a duplication of the
1802 .B PERF_SAMPLE_ID
1803 .I id
1804 value, but included at the beginning of the sample
1805 so parsers can easily obtain the value.
1806 .TP
1807 .I ip
1808 If
1809 .B PERF_SAMPLE_IP
1810 is enabled, then a 64-bit instruction
1811 pointer value is included.
1812 .TP
1813 .IR pid ", " tid
1814 If
1815 .B PERF_SAMPLE_TID
1816 is enabled, then a 32-bit process ID
1817 and 32-bit thread ID are included.
1818 .TP
1819 .I time
1820 If
1821 .B PERF_SAMPLE_TIME
1822 is enabled, then a 64-bit timestamp
1823 is included.
1824 This is obtained via local_clock() which is a hardware timestamp
1825 if available and the jiffies value if not.
1826 .TP
1827 .I addr
1828 If
1829 .B PERF_SAMPLE_ADDR
1830 is enabled, then a 64-bit address is included.
1831 This is usually the address of a tracepoint,
1832 breakpoint, or software event; otherwise the value is 0.
1833 .TP
1834 .I id
1835 If
1836 .B PERF_SAMPLE_ID
1837 is enabled, a 64-bit unique ID is included.
1838 If the event is a member of an event group, the group leader ID is returned.
1839 This ID is the same as the one returned by
1840 .BR PERF_FORMAT_ID .
1841 .TP
1842 .I stream_id
1843 If
1844 .B PERF_SAMPLE_STREAM_ID
1845 is enabled, a 64-bit unique ID is included.
1846 Unlike
1847 .B PERF_SAMPLE_ID
1848 the actual ID is returned, not the group leader.
1849 This ID is the same as the one returned by
1850 .BR PERF_FORMAT_ID .
1851 .TP
1852 .IR cpu ", " res
1853 If
1854 .B PERF_SAMPLE_CPU
1855 is enabled, this is a 32-bit value indicating
1856 which CPU was being used, in addition to a reserved (unused)
1857 32-bit value.
1858 .TP
1859 .I period
1860 If
1861 .B PERF_SAMPLE_PERIOD
1862 is enabled, a 64-bit value indicating
1863 the current sampling period is written.
1864 .TP
1865 .I v
1866 If
1867 .B PERF_SAMPLE_READ
1868 is enabled, a structure of type read_format
1869 is included which has values for all events in the event group.
1870 The values included depend on the
1871 .I read_format
1872 value used at
1873 .BR perf_event_open ()
1874 time.
1875 .TP
1876 .IR nr ", " ips[nr]
1877 If
1878 .B PERF_SAMPLE_CALLCHAIN
1879 is enabled, then a 64-bit number is included
1880 which indicates how many following 64-bit instruction pointers will
1881 follow.
1882 This is the current callchain.
1883 .TP
1884 .IR size ", " data[size]
1885 If
1886 .B PERF_SAMPLE_RAW
1887 is enabled, then a 32-bit value indicating size
1888 is included followed by an array of 8-bit values of length size.
1889 The values are padded with 0 to have 64-bit alignment.
1890
1891 This RAW record data is opaque with respect to the ABI.
1892 The ABI doesn't make any promises with respect to the stability
1893 of its content, it may vary depending
1894 on event, hardware, and kernel version.
1895 .TP
1896 .IR bnr ", " lbr[bnr]
1897 If
1898 .B PERF_SAMPLE_BRANCH_STACK
1899 is enabled, then a 64-bit value indicating
1900 the number of records is included, followed by
1901 .I bnr
1902 .I perf_branch_entry
1903 structures which each include the fields:
1904 .RS
1905 .TP
1906 .I from
1907 This indicates the source instruction (may not be a branch).
1908 .TP
1909 .I to
1910 The branch target.
1911 .TP
1912 .I mispred
1913 The branch target was mispredicted.
1914 .TP
1915 .I predicted
1916 The branch target was predicted.
1917 .TP
1918 .IR in_tx " (since Linux 3.11)"
1919 The branch was in a transactional memory transaction.
1920 .TP
1921 .IR abort " (since Linux 3.11)"
1922 The branch was in an aborted transactional memory transaction.
1923
1924 .P
1925 The entries are from most to least recent, so the first entry
1926 has the most recent branch.
1927
1928 Support for
1929 .I mispred
1930 and
1931 .I predicted
1932 is optional; if not supported, both
1933 values will be 0.
1934
1935 The type of branches recorded is specified by the
1936 .I branch_sample_type
1937 field.
1938 .RE
1939
1940 .TP
1941 .IR abi ", " regs[weight(mask)]
1942 If
1943 .B PERF_SAMPLE_REGS_USER
1944 is enabled, then the user CPU registers are recorded.
1945
1946 The
1947 .I abi
1948 field is one of
1949 .BR PERF_SAMPLE_REGS_ABI_NONE ", " PERF_SAMPLE_REGS_ABI_32 " or "
1950 .BR PERF_SAMPLE_REGS_ABI_64 .
1951
1952 The
1953 .I regs
1954 field is an array of the CPU registers that were specified by
1955 the
1956 .I sample_regs_user
1957 attr field.
1958 The number of values is the number of bits set in the
1959 .I sample_regs_user
1960 bit mask.
1961 .TP
1962 .IR size ", " data[size] ", " dyn_size
1963 If
1964 .B PERF_SAMPLE_STACK_USER
1965 is enabled, then the user stack is recorded.
1966 This can be used to generate stack backtraces.
1967 .I size
1968 is the size requested by the user in
1969 .I sample_stack_user
1970 or else the maximum record size.
1971 .I data
1972 is the stack data (a raw dump of the memory pointed to by the
1973 stack pointer at the time of sampling).
1974 .I dyn_size
1975 is the amount of data actually dumped (can be less than
1976 .IR size ).
1977 .TP
1978 .I weight
1979 If
1980 .B PERF_SAMPLE_WEIGHT
1981 is enabled, then a 64-bit value provided by the hardware
1982 is recorded that indicates how costly the event was.
1983 This allows expensive events to stand out more clearly
1984 in profiles.
1985 .TP
1986 .I data_src
1987 If
1988 .B PERF_SAMPLE_DATA_SRC
1989 is enabled, then a 64-bit value is recorded that is made up of
1990 the following fields:
1991 .RS
1992 .TP 4
1993 .I mem_op
1994 Type of opcode, a bitwise combination of:
1995
1996 .PD 0
1997 .RS
1998 .TP 24
1999 .B PERF_MEM_OP_NA
2000 Not available
2001 .TP
2002 .B PERF_MEM_OP_LOAD
2003 Load instruction
2004 .TP
2005 .B PERF_MEM_OP_STORE
2006 Store instruction
2007 .TP
2008 .B PERF_MEM_OP_PFETCH
2009 Prefetch
2010 .TP
2011 .B PERF_MEM_OP_EXEC
2012 Executable code
2013 .RE
2014 .PD
2015 .TP
2016 .I mem_lvl
2017 Memory hierarchy level hit or miss, a bitwise combination of
2018 the following, shifted left by
2019 .BR PERF_MEM_LVL_SHIFT :
2020
2021 .PD 0
2022 .RS
2023 .TP 24
2024 .B PERF_MEM_LVL_NA
2025 Not available
2026 .TP
2027 .B PERF_MEM_LVL_HIT
2028 Hit
2029 .TP
2030 .B PERF_MEM_LVL_MISS
2031 Miss
2032 .TP
2033 .B PERF_MEM_LVL_L1
2034 Level 1 cache
2035 .TP
2036 .B PERF_MEM_LVL_LFB
2037 Line fill buffer
2038 .TP
2039 .B PERF_MEM_LVL_L2
2040 Level 2 cache
2041 .TP
2042 .B PERF_MEM_LVL_L3
2043 Level 3 cache
2044 .TP
2045 .B PERF_MEM_LVL_LOC_RAM
2046 Local DRAM
2047 .TP
2048 .B PERF_MEM_LVL_REM_RAM1
2049 Remote DRAM 1 hop
2050 .TP
2051 .B PERF_MEM_LVL_REM_RAM2
2052 Remote DRAM 2 hops
2053 .TP
2054 .B PERF_MEM_LVL_REM_CCE1
2055 Remote cache 1 hop
2056 .TP
2057 .B PERF_MEM_LVL_REM_CCE2
2058 Remote cache 2 hops
2059 .TP
2060 .B PERF_MEM_LVL_IO
2061 I/O memory
2062 .TP
2063 .B PERF_MEM_LVL_UNC
2064 Uncached memory
2065 .RE
2066 .PD
2067 .TP
2068 .I mem_snoop
2069 Snoop mode, a bitwise combination of the following, shifted left by
2070 .BR PERF_MEM_SNOOP_SHIFT :
2071
2072 .PD 0
2073 .RS
2074 .TP 24
2075 .B PERF_MEM_SNOOP_NA
2076 Not available
2077 .TP
2078 .B PERF_MEM_SNOOP_NONE
2079 No snoop
2080 .TP
2081 .B PERF_MEM_SNOOP_HIT
2082 Snoop hit
2083 .TP
2084 .B PERF_MEM_SNOOP_MISS
2085 Snoop miss
2086 .TP
2087 .B PERF_MEM_SNOOP_HITM
2088 Snoop hit modified
2089 .RE
2090 .PD
2091 .TP
2092 .I mem_lock
2093 Lock instruction, a bitwise combination of the following, shifted left by
2094 .BR PERF_MEM_LOCK_SHIFT :
2095
2096 .PD 0
2097 .RS
2098 .TP 24
2099 .B PERF_MEM_LOCK_NA
2100 Not available
2101 .TP
2102 .B PERF_MEM_LOCK_LOCKED
2103 Locked transaction
2104 .RE
2105 .PD
2106 .TP
2107 .I mem_dtlb
2108 TLB access hit or miss, a bitwise combination of the following, shifted
2109 left by
2110 .BR PERF_MEM_TLB_SHIFT :
2111
2112 .PD 0
2113 .RS
2114 .TP 24
2115 .B PERF_MEM_TLB_NA
2116 Not available
2117 .TP
2118 .B PERF_MEM_TLB_HIT
2119 Hit
2120 .TP
2121 .B PERF_MEM_TLB_MISS
2122 Miss
2123 .TP
2124 .B PERF_MEM_TLB_L1
2125 Level 1 TLB
2126 .TP
2127 .B PERF_MEM_TLB_L2
2128 Level 2 TLB
2129 .TP
2130 .B PERF_MEM_TLB_WK
2131 Hardware walker
2132 .TP
2133 .B PERF_MEM_TLB_OS
2134 OS fault handler
2135 .RE
2136 .PD
2137 .RE
2138 .TP
2139 .I transaction
2140 If the
2141 .B PERF_SAMPLE_TRANSACTION
2142 flag is set, then a 64-bit field is recorded describing
2143 the sources of any transactional memory aborts.
2144
2145 The field is a bitwise combination of the following values:
2146 .RS
2147 .TP
2148 .B PERF_TXN_ELISION
2149 Abort from an elision type transaction (Intel-CPU-specific).
2150 .TP
2151 .B PERF_TXN_TRANSACTION
2152 Abort from a generic transaction.
2153 .TP
2154 .B PERF_TXN_SYNC
2155 Synchronous abort (related to the reported instruction).
2156 .TP
2157 .B PERF_TXN_ASYNC
2158 Asynchronous abort (not related to the reported instruction).
2159 .TP
2160 .B PERF_TXN_RETRY
2161 Retryable abort (retrying the transaction may have succeeded).
2162 .TP
2163 .B PERF_TXN_CONFLICT
2164 Abort due to memory conflicts with other threads.
2165 .TP
2166 .B PERF_TXN_CAPACITY_WRITE
2167 Abort due to write capacity overflow.
2168 .TP
2169 .B PERF_TXN_CAPACITY_READ
2170 Abort due to read capacity overflow.
2171 .RE
2172 .IP
2173 In addition, a user-specified abort code can be obtained from
2174 the high 32 bits of the field by shifting right by
2175 .B PERF_TXN_ABORT_SHIFT
2176 and masking with
2177 .BR PERF_TXN_ABORT_MASK .
2178 .RE
2179 .TP
2180 .B PERF_RECORD_MMAP2
2181 This record includes extended information on
2182 .BR mmap (2)
2183 calls returning executable mappings.
2184 The format is similar to that of the
2185 .B PERF_RECORD_MMAP
2186 record, but includes extra values that allow uniquely identifying
2187 shared mappings.
2188
2189 .in +4n
2190 .nf
2191 struct {
2192     struct perf_event_header header;
2193     u32 pid;
2194     u32 tid;
2195     u64 addr;
2196     u64 len;
2197     u64 pgoff;
2198     u32 maj;
2199     u32 min;
2200     u64 ino;
2201     u64 ino_generation;
2202     u32 prot;
2203     u32 flags;
2204     char filename[];
2205     struct sample_id sample_id;
2206 };
2207 .fi
2208 .RS
2209 .TP
2210 .I pid
2211 is the process ID.
2212 .TP
2213 .I tid
2214 is the thread ID.
2215 .TP
2216 .I addr
2217 is the address of the allocated memory.
2218 .TP
2219 .I len
2220 is the length of the allocated memory.
2221 .TP
2222 .I pgoff
2223 is the page offset of the allocated memory.
2224 .TP
2225 .I maj
2226 is the major ID of the underlying device.
2227 .TP
2228 .I min
2229 is the minor ID of the underlying device.
2230 .TP
2231 .I ino
2232 is the inode number.
2233 .TP
2234 .I ino_generation
2235 is the inode generation.
2236 .TP
2237 .I prot
2238 is the protection information.
2239 .TP
2240 .I flags
2241 is the flags information.
2242 .TP
2243 .I filename
2244 is a string describing the backing of the allocated memory.
2245 .RE
2246 .RE
2247 .SS Signal overflow
2248 Events can be set to deliver a signal when a threshold is crossed.
2249 .\" FIXME .
2250 .\" The following sentence doesn't seem to make sense.
2251 .\" These system calls do not set up signal handlers.
2252 The signal handler is set up using the
2253 .BR poll (2),
2254 .BR select (2),
2255 .BR epoll (2)
2256 and
2257 .BR fcntl (2),
2258 system calls.
2259
2260 To generate signals, sampling must be enabled
2261 .RI ( sample_period
2262 must have a nonzero value).
2263
2264 There are two ways to generate signals.
2265
2266 The first is to set a
2267 .I wakeup_events
2268 or
2269 .I wakeup_watermark
2270 value that will generate a signal if a certain number of samples
2271 or bytes have been written to the mmap ring buffer.
2272 In this case, a signal of type
2273 .B POLL_IN
2274 is sent.
2275
2276 The other way is by use of the
2277 .B PERF_EVENT_IOC_REFRESH
2278 ioctl.
2279 This ioctl adds to a counter that decrements each time the event overflows.
2280 When nonzero, a
2281 .B POLL_IN
2282 signal is sent on overflow, but
2283 once the value reaches 0, a signal is sent of type
2284 .B POLL_HUP
2285 and
2286 the underlying event is disabled.
2287
2288 Note: on newer kernels (since at least as early as Linux 3.2),
2289 .\" FIXME . Find out when this was introduced
2290 a signal is provided for every overflow, even if
2291 .I wakeup_events
2292 is not set.
2293 .SS rdpmc instruction
2294 Starting with Linux 3.4 on x86, you can use the
2295 .I rdpmc
2296 instruction to get low-latency reads without having to enter the kernel.
2297 Note that using
2298 .I rdpmc
2299 is not necessarily faster than other methods for reading event values.
2300
2301 Support for this can be detected with the
2302 .I cap_usr_rdpmc
2303 field in the mmap page; documentation on how
2304 to calculate event values can be found in that section.
2305 .SS perf_event ioctl calls
2306 .PP
2307 Various ioctls act on
2308 .BR perf_event_open ()
2309 file descriptors:
2310 .TP
2311 .B PERF_EVENT_IOC_ENABLE
2312 This enables the individual event or event group specified by the
2313 file descriptor argument.
2314
2315 If the
2316 .B PERF_IOC_FLAG_GROUP
2317 bit is set in the ioctl argument, then all events in a group are
2318 enabled, even if the event specified is not the group leader
2319 (but see BUGS).
2320 .TP
2321 .B PERF_EVENT_IOC_DISABLE
2322 This disables the individual counter or event group specified by the
2323 file descriptor argument.
2324
2325 Enabling or disabling the leader of a group enables or disables the
2326 entire group; that is, while the group leader is disabled, none of the
2327 counters in the group will count.
2328 Enabling or disabling a member of a group other than the leader
2329 affects only that counter; disabling a non-leader
2330 stops that counter from counting but doesn't affect any other counter.
2331
2332 If the
2333 .B PERF_IOC_FLAG_GROUP
2334 bit is set in the ioctl argument, then all events in a group are
2335 disabled, even if the event specified is not the group leader
2336 (but see BUGS).
2337 .TP
2338 .B PERF_EVENT_IOC_REFRESH
2339 Non-inherited overflow counters can use this
2340 to enable a counter for a number of overflows specified by the argument,
2341 after which it is disabled.
2342 Subsequent calls of this ioctl add the argument value to the current
2343 count.
2344 A signal with
2345 .B POLL_IN
2346 set will happen on each overflow until the
2347 count reaches 0; when that happens a signal with
2348 POLL_HUP
2349 set is sent and the event is disabled.
2350 Using an argument of 0 is considered undefined behavior.
2351 .TP
2352 .B PERF_EVENT_IOC_RESET
2353 Reset the event count specified by the
2354 file descriptor argument to zero.
2355 This resets only the counts; there is no way to reset the
2356 multiplexing
2357 .I time_enabled
2358 or
2359 .I time_running
2360 values.
2361
2362 If the
2363 .B PERF_IOC_FLAG_GROUP
2364 bit is set in the ioctl argument, then all events in a group are
2365 reset, even if the event specified is not the group leader
2366 (but see BUGS).
2367 .TP
2368 .B PERF_EVENT_IOC_PERIOD
2369 This updates the overflow period for the event.
2370
2371 Since Linux 3.7 (on ARM) and Linux 3.14 (all other architectures),
2372 the new period takes effect immediately.
2373 On older kernels, the new period did not take effect until
2374 after the next overflow.
2375
2376 The argument is a pointer to a 64-bit value containing the
2377 desired new period.
2378
2379 Prior to Linux 2.6.36 this ioctl always failed due to a bug
2380 in the kernel.
2381
2382 .TP
2383 .B PERF_EVENT_IOC_SET_OUTPUT
2384 This tells the kernel to report event notifications to the specified
2385 file descriptor rather than the default one.
2386 The file descriptors must all be on the same CPU.
2387
2388 The argument specifies the desired file descriptor, or \-1 if
2389 output should be ignored.
2390 .TP
2391 .BR PERF_EVENT_IOC_SET_FILTER " (since Linux 2.6.33)"
2392 This adds an ftrace filter to this event.
2393
2394 The argument is a pointer to the desired ftrace filter.
2395 .TP
2396 .BR PERF_EVENT_IOC_ID " (since Linux 3.12)"
2397 This returns the event ID value for the given event file descriptor.
2398
2399 The argument is a pointer to a 64-bit unsigned integer
2400 to hold the result.
2401 .SS Using prctl
2402 A process can enable or disable all the event groups that are
2403 attached to it using the
2404 .BR prctl (2)
2405 .B PR_TASK_PERF_EVENTS_ENABLE
2406 and
2407 .B PR_TASK_PERF_EVENTS_DISABLE
2408 operations.
2409 This applies to all counters on the calling process, whether created by
2410 this process or by another, and does not affect any counters that this
2411 process has created on other processes.
2412 It enables or disables only
2413 the group leaders, not any other members in the groups.
2414 .SS perf_event related configuration files
2415 Files in
2416 .I /proc/sys/kernel/
2417 .RS 4
2418 .TP
2419 .I /proc/sys/kernel/perf_event_paranoid
2420
2421 The
2422 .I perf_event_paranoid
2423 file can be set to restrict access to the performance counters.
2424 .RS
2425 .IP 2 4
2426 only allow user-space measurements.
2427 .IP 1
2428 allow both kernel and user measurements (default).
2429 .IP 0
2430 allow access to CPU-specific data but not raw tracepoint samples.
2431 .IP \-1
2432 no restrictions.
2433 .RE
2434 .IP
2435 The existence of the
2436 .I perf_event_paranoid
2437 file is the official method for determining if a kernel supports
2438 .BR perf_event_open ().
2439 .TP
2440 .I /proc/sys/kernel/perf_event_max_sample_rate
2441
2442 This sets the maximum sample rate.
2443 Setting this too high can allow
2444 users to sample at a rate that impacts overall machine performance
2445 and potentially lock up the machine.
2446 The default value is
2447 100000 (samples per second).
2448 .TP
2449 .I /proc/sys/kernel/perf_event_mlock_kb
2450
2451 Maximum number of pages an unprivileged user can
2452 .BR mlock (2).
2453 The default is 516 (kB).
2454
2455 .RE
2456 Files in
2457 .I /sys/bus/event_source/devices/
2458 .RS 4
2459 Since Linux 2.6.34, the kernel supports having multiple PMUs
2460 available for monitoring.
2461 Information on how to program these PMUs can be found under
2462 .IR /sys/bus/event_source/devices/ .
2463 Each subdirectory corresponds to a different PMU.
2464 .TP
2465 .IR /sys/bus/event_source/devices/*/type " (since Linux 2.6.38)"
2466 This contains an integer that can be used in the
2467 .I type
2468 field of
2469 .I perf_event_attr
2470 to indicate that you wish to use this PMU.
2471 .TP
2472 .IR /sys/bus/event_source/devices/*/rdpmc " (since Linux 3.4)"
2473 If this file is 1, then direct user-space access to the
2474 performance counter registers is allowed via the rdpmc instruction.
2475 This can be disabled by echoing 0 to the file.
2476 .TP
2477 .IR /sys/bus/event_source/devices/*/format/ " (since Linux 3.4)"
2478 This subdirectory contains information on the architecture-specific
2479 subfields available for programming the various
2480 .I config
2481 fields in the
2482 .I perf_event_attr
2483 struct.
2484
2485 The content of each file is the name of the config field, followed
2486 by a colon, followed by a series of integer bit ranges separated by
2487 commas.
2488 For example, the file
2489 .I event
2490 may contain the value
2491 .I config1:1,6-10,44
2492 which indicates that event is an attribute that occupies bits 1,6-10, and 44
2493 of
2494 .IR perf_event_attr::config1 .
2495 .TP
2496 .IR /sys/bus/event_source/devices/*/events/ " (since Linux 3.4)"
2497 This subdirectory contains files with predefined events.
2498 The contents are strings describing the event settings
2499 expressed in terms of the fields found in the previously mentioned
2500 .I ./format/
2501 directory.
2502 These are not necessarily complete lists of all events supported by
2503 a PMU, but usually a subset of events deemed useful or interesting.
2504
2505 The content of each file is a list of attribute names
2506 separated by commas.
2507 Each entry has an optional value (either hex or decimal).
2508 If no value is specified, then it is assumed to be a single-bit
2509 field with a value of 1.
2510 An example entry may look like this:
2511 .IR event=0x2,inv,ldlat=3 .
2512 .TP
2513 .I /sys/bus/event_source/devices/*/uevent
2514 This file is the standard kernel device interface
2515 for injecting hotplug events.
2516 .TP
2517 .IR /sys/bus/event_source/devices/*/cpumask " (since Linux 3.7)"
2518 The
2519 .I cpumask
2520 file contains a comma-separated list of integers that
2521 indicate a representative CPU number for each socket (package)
2522 on the motherboard.
2523 This is needed when setting up uncore or northbridge events, as
2524 those PMUs present socket-wide events.
2525 .RE
2526 .SH RETURN VALUE
2527 .BR perf_event_open ()
2528 returns the new file descriptor, or \-1 if an error occurred
2529 (in which case,
2530 .I errno
2531 is set appropriately).
2532 .SH ERRORS
2533 The errors returned by
2534 .BR perf_event_open ()
2535 can be inconsistent, and may
2536 vary across processor architectures and performance monitoring units.
2537 .TP
2538 .B E2BIG
2539 Returned if the
2540 .I perf_event_attr
2541 .I size
2542 value is too small
2543 (smaller than
2544 .BR PERF_ATTR_SIZE_VER0 ),
2545 too big (larger than the page size),
2546 or larger than the kernel supports and the extra bytes are not zero.
2547 When
2548 .B E2BIG
2549 is returned, the
2550 .I perf_event_attr
2551 .I size
2552 field is overwritten by the kernel to be the size of the structure
2553 it was expecting.
2554 .TP
2555 .B EACCES
2556 Returned when the requested event requires
2557 .B CAP_SYS_ADMIN
2558 permissions (or a more permissive perf_event paranoid setting).
2559 Some common cases where an unprivileged process
2560 may encounter this error:
2561 attaching to a process owned by a different user;
2562 monitoring all processes on a given CPU (i.e., specifying the
2563 .I pid
2564 argument as \-1);
2565 and not setting
2566 .I exclude_kernel
2567 when the paranoid setting requires it.
2568 .TP
2569 .B EBADF
2570 Returned if the
2571 .I group_fd
2572 file descriptor is not valid, or, if
2573 .B PERF_FLAG_PID_CGROUP
2574 is set,
2575 the cgroup file descriptor in
2576 .I pid
2577 is not valid.
2578 .TP
2579 .B EFAULT
2580 Returned if the
2581 .I attr
2582 pointer points at an invalid memory address.
2583 .TP
2584 .B EINVAL
2585 Returned if the specified event is invalid.
2586 There are many possible reasons for this.
2587 A not-exhaustive list:
2588 .I sample_freq
2589 is higher than the maximum setting;
2590 the
2591 .I cpu
2592 to monitor does not exist;
2593 .I read_format
2594 is out of range;
2595 .I sample_type
2596 is out of range;
2597 the
2598 .I flags
2599 value is out of range;
2600 .I exclusive
2601 or
2602 .I pinned
2603 set and the event is not a group leader;
2604 the event
2605 .I config
2606 values are out of range or set reserved bits;
2607 the generic event selected is not supported; or
2608 there is not enough room to add the selected event.
2609 .TP
2610 .B EMFILE
2611 Each opened event uses one file descriptor.
2612 If a large number of events are opened the per-user file
2613 descriptor limit (often 1024) will be hit and no more
2614 events can be created.
2615 .TP
2616 .B ENODEV
2617 Returned when the event involves a feature not supported
2618 by the current CPU.
2619 .TP
2620 .B ENOENT
2621 Returned if the
2622 .I type
2623 setting is not valid.
2624 This error is also returned for
2625 some unsupported generic events.
2626 .TP
2627 .B ENOSPC
2628 Prior to Linux 3.3, if there was not enough room for the event,
2629 .B ENOSPC
2630 was returned.
2631 In Linux 3.3, this was changed to
2632 .BR EINVAL .
2633 .B ENOSPC
2634 is still returned if you try to add more breakpoint events
2635 than supported by the hardware.
2636 .TP
2637 .B ENOSYS
2638 Returned if
2639 .B PERF_SAMPLE_STACK_USER
2640 is set in
2641 .I sample_type
2642 and it is not supported by hardware.
2643 .TP
2644 .B EOPNOTSUPP
2645 Returned if an event requiring a specific hardware feature is
2646 requested but there is no hardware support.
2647 This includes requesting low-skid events if not supported,
2648 branch tracing if it is not available, sampling if no PMU
2649 interrupt is available, and branch stacks for software events.
2650 .TP
2651 .B EPERM
2652 Returned on many (but not all) architectures when an unsupported
2653 .IR exclude_hv ", " exclude_idle ", " exclude_user ", or " exclude_kernel
2654 setting is specified.
2655
2656 It can also happen, as with
2657 .BR EACCES ,
2658 when the requested event requires
2659 .B CAP_SYS_ADMIN
2660 permissions (or a more permissive perf_event paranoid setting).
2661 This includes setting a breakpoint on a kernel address,
2662 and (since Linux 3.13) setting a kernel function-trace tracepoint.
2663 .TP
2664 .B ESRCH
2665 Returned if attempting to attach to a process that does not exist.
2666 .SH VERSION
2667 .BR perf_event_open ()
2668 was introduced in Linux 2.6.31 but was called
2669 .BR perf_counter_open ().
2670 It was renamed in Linux 2.6.32.
2671 .SH CONFORMING TO
2672 This
2673 .BR perf_event_open ()
2674 system call Linux- specific
2675 and should not be used in programs intended to be portable.
2676 .SH NOTES
2677 Glibc does not provide a wrapper for this system call; call it using
2678 .BR syscall (2).
2679 See the example below.
2680
2681 The official way of knowing if
2682 .BR perf_event_open ()
2683 support is enabled is checking
2684 for the existence of the file
2685 .IR /proc/sys/kernel/perf_event_paranoid .
2686 .SH BUGS
2687 The
2688 .B F_SETOWN_EX
2689 option to
2690 .BR fcntl (2)
2691 is needed to properly get overflow signals in threads.
2692 This was introduced in Linux 2.6.32.
2693
2694 Prior to Linux 2.6.33 (at least for x86), the kernel did not check
2695 if events could be scheduled together until read time.
2696 The same happens on all known kernels if the NMI watchdog is enabled.
2697 This means to see if a given set of events works you have to
2698 .BR perf_event_open (),
2699 start, then read before you know for sure you
2700 can get valid measurements.
2701
2702 Prior to Linux 2.6.34, event constraints were not enforced by the kernel.
2703 In that case, some events would silently return "0" if the kernel
2704 scheduled them in an improper counter slot.
2705
2706 Prior to Linux 2.6.34, there was a bug when multiplexing where the
2707 wrong results could be returned.
2708
2709 Kernels from Linux 2.6.35 to Linux 2.6.39 can quickly crash the kernel if
2710 "inherit" is enabled and many threads are started.
2711
2712 Prior to Linux 2.6.35,
2713 .B PERF_FORMAT_GROUP
2714 did not work with attached processes.
2715
2716 In older Linux 2.6 versions,
2717 refreshing an event group leader refreshed all siblings,
2718 and refreshing with a parameter of 0 enabled infinite refresh.
2719 This behavior is unsupported and should not be relied on.
2720
2721 There is a bug in the kernel code between
2722 Linux 2.6.36 and Linux 3.0 that ignores the
2723 "watermark" field and acts as if a wakeup_event
2724 was chosen if the union has a
2725 nonzero value in it.
2726
2727 From Linux 2.6.31 to Linux 3.4, the
2728 .B PERF_IOC_FLAG_GROUP
2729 ioctl argument was broken and would repeatedly operate
2730 on the event specified rather than iterating across
2731 all sibling events in a group.
2732
2733 From Linux 3.4 to Linux 3.11, the mmap
2734 .I cap_usr_rdpmc
2735 and
2736 .I cap_usr_time
2737 bits mapped to the same location.
2738 Code should migrate to the new
2739 .I cap_user_rdpmc
2740 and
2741 .I cap_user_time
2742 fields instead.
2743
2744 Always double-check your results!
2745 Various generalized events have had wrong values.
2746 For example, retired branches measured
2747 the wrong thing on AMD machines until Linux 2.6.35.
2748 .SH EXAMPLE
2749 The following is a short example that measures the total
2750 instruction count of a call to
2751 .BR printf (3).
2752 .nf
2753
2754 #include <stdlib.h>
2755 #include <stdio.h>
2756 #include <unistd.h>
2757 #include <string.h>
2758 #include <sys/ioctl.h>
2759 #include <linux/perf_event.h>
2760 #include <asm/unistd.h>
2761
2762 static long
2763 perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
2764                 int cpu, int group_fd, unsigned long flags)
2765 {
2766     int ret;
2767
2768     ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
2769                    group_fd, flags);
2770     return ret;
2771 }
2772
2773 int
2774 main(int argc, char **argv)
2775 {
2776     struct perf_event_attr pe;
2777     long long count;
2778     int fd;
2779
2780     memset(&pe, 0, sizeof(struct perf_event_attr));
2781     pe.type = PERF_TYPE_HARDWARE;
2782     pe.size = sizeof(struct perf_event_attr);
2783     pe.config = PERF_COUNT_HW_INSTRUCTIONS;
2784     pe.disabled = 1;
2785     pe.exclude_kernel = 1;
2786     pe.exclude_hv = 1;
2787
2788     fd = perf_event_open(&pe, 0, \-1, \-1, 0);
2789     if (fd == \-1) {
2790        fprintf(stderr, "Error opening leader %llx\\n", pe.config);
2791        exit(EXIT_FAILURE);
2792     }
2793
2794     ioctl(fd, PERF_EVENT_IOC_RESET, 0);
2795     ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
2796
2797     printf("Measuring instruction count for this printf\\n");
2798
2799     ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
2800     read(fd, &count, sizeof(long long));
2801
2802     printf("Used %lld instructions\\n", count);
2803
2804     close(fd);
2805 }
2806 .fi
2807 .SH SEE ALSO
2808 .BR fcntl (2),
2809 .BR mmap (2),
2810 .BR open (2),
2811 .BR prctl (2),
2812 .BR read (2)