Manual Pages for Linux CentOS command on man perf_event_open
MyWebUniversity

Manual Pages for Linux CentOS command on man perf_event_open

PERFEVENTOPEN(2) Linux Programmer's Manual PERFEVENTOPEN(2)

NAME

perfeventopen - set up performance monitoring SYNOPSIS

#include

#include int perfeventopen(struct perfeventattr *attr, pidt pid, int cpu, int groupfd, unsigned long flags); Note: There is no glibc wrapper for this system call; see NOTES. DESCRIPTION Given a list of parameters, perfeventopen() returns a file descrip‐ tor, for use in subsequent system calls (read(2), mmap(2), prctl(2), fcntl(2), etc.). A call to perfeventopen() creates a file descriptor that allows mea‐ suring performance information. Each file descriptor corresponds to one event that is measured; these can be grouped together to measure multiple events simultaneously. Events can be enabled and disabled in two ways: via ioctl(2) and via prctl(2). When an event is disabled it does not count or generate overflows but does continue to exist and maintain its count value. Events come in two flavors: counting and sampled. A counting event is one that is used for counting the aggregate number of events that occur. In general, counting event results are gathered with a read(2) call. A sampling event periodically writes measurements to a buffer that can then be accessed via mmap(2). Arguments The pid and cpu arguments allow specifying which process and CPU to monitor:

pid == 0 and cpu == -1 This measures the calling process/thread on any CPU. pid == 0 and cpu >= 0 This measures the calling process/thread only when running on the specified CPU.

pid > 0 and cpu == -1 This measures the specified process/thread on any CPU. pid > 0 and cpu >= 0 This measures the specified process/thread only when running on the specified CPU.

pid == -1 and cpu >= 0 This measures all processes/threads on the specified CPU. This requires CAPSYSADMIN capability or a /proc/sys/ker‐ nel/perfeventparanoid value of less than 1.

pid == -1 and cpu == -1 This setting is invalid and will return an error. When pid is greater than zero, permission to perform this system call is governed by a ptrace access mode PTRACEMODEREADREALCREDS check; see ptrace(2). The groupfd argument allows event groups to be created. An event group has one event which is the group leader. The leader is created

first, with groupfd = -1. The rest of the group members are created with subsequent perfeventopen() calls with groupfd being set to the file descriptor of the group leader. (A single event on its own is

created with groupfd = -1 and is considered to be a group with only 1 member.) An event group is scheduled onto the CPU as a unit: it will be put onto the CPU only if all of the events in the group can be put onto the CPU. This means that the values of the member events can be meaningfully compared—added, divided (to get ratios), and so on—with each other, since they have counted events for the same set of executed instructions. The flags argument is formed by ORing together zero or more of the fol‐ lowing values: PERFFLAGFDCLOEXEC (since Linux 3.14)

This flag enables the close-on-exec flag for the created event file descriptor, so that the file descriptor is automatically

closed on execve(2). Setting the close-on-exec flags at cre‐ ation time, rather than later with fcntl(2), avoids potential race conditions where the calling thread invokes perfeventopen() and fcntl(2) at the same time as another thread calls fork(2) then execve(2). PERFFLAGFDNOGROUP This flag tells the event to ignore the groupfd parameter except for the purpose of setting up output redirection using the PERFFLAGFDOUTPUT flag. PERFFLAGFDOUTPUT (broken since Linux 2.6.35)

This flag re-routes the event's sampled output to instead be included in the mmap buffer of the event specified by groupfd. PERFFLAGPIDCGROUP (since Linux 2.6.39)

This flag activates per-container system-wide monitoring. A container is an abstraction that isolates a set of resources for

finer-grained control (CPUs, memory, etc.). In this mode, the event is measured only if the thread running on the monitored CPU belongs to the designated container (cgroup). The cgroup is identified by passing a file descriptor opened on its directory in the cgroupfs filesystem. For instance, if the cgroup to mon‐ itor is called test, then a file descriptor opened on /dev/cgroup/test (assuming cgroupfs is mounted on /dev/cgroup) must be passed as the pid parameter. cgroup monitoring is

available only for system-wide events and may therefore require extra permissions. The perfeventattr structure provides detailed configuration informa‐ tion for the event being created. struct perfeventattr { u32 type; /* Type of event */ u32 size; /* Size of attribute structure */

u64 config; /* Type-specific configuration */ union { u64 sampleperiod; /* Period of sampling */ u64 samplefreq; /* Frequency of sampling */ }; u64 sampletype; /* Specifies values included in sample */ u64 readformat; /* Specifies values returned in read */ u64 disabled : 1, /* off by default */ inherit : 1, /* children inherit it */ pinned : 1, /* must always be on PMU */ exclusive : 1, /* only group on PMU */ excludeuser : 1, /* don't count user */ excludekernel : 1, /* don't count kernel */ excludehv : 1, /* don't count hypervisor */ excludeidle : 1, /* don't count when idle */ mmap : 1, /* include mmap data */ comm : 1, /* include comm data */ freq : 1, /* use freq, not period */ inheritstat : 1, /* per task counts */ enableonexec : 1, /* next exec enables */ task : 1, /* trace fork/exit */ watermark : 1, /* wakeupwatermark */ preciseip : 2, /* skid constraint */

mmapdata : 1, /* non-exec mmap data */ sampleidall : 1, /* sampletype all events */ excludehost : 1, /* don't count in host */ excludeguest : 1, /* don't count in guest */ excludecallchainkernel : 1, /* exclude kernel callchains */ excludecallchainuser : 1, /* exclude user callchains */ mmap2 : 1, /* include mmap with inode data */ commexec : 1, /* flag comm events that are due to exec */ useclockid : 1, /* use clockid for time fields */ contextswitch : 1, /* context switch data */ reserved1 : 37; union { u32 wakeupevents; /* wakeup every n events */ u32 wakeupwatermark; /* bytes before wakeup */ }; u32 bptype; /* breakpoint type */ union { u64 bpaddr; /* breakpoint address */ u64 kprobefunc; /* for perfkprobe */ u64 uprobepath; /* for perfuprobe */ u64 config1; /* extension of config */ }; union { u64 bplen; /* breakpoint length */ u64 kprobeaddr; /* with kprobefunc == NULL */ u64 probeoffset; /* for perf[k,u]probe */ u64 config2; /* extension of config1 */ }; u64 branchsampletype; /* enum perfbranchsampletype */ u64 sampleregsuser; /* user regs to dump on samples */ u32 samplestackuser; /* size of stack to dump on samples */ s32 clockid; /* clock to use for time fields */ u64 sampleregsintr; /* regs to dump on samples */ u32 auxwatermark; /* aux bytes before wakeup */ u16 samplemaxstack; /* max frames in callchain */ u16 reserved2; /* align to u64 */ }; The fields of the perfeventattr structure are described in more detail below: type This field specifies the overall event type. It has one of the following values: PERFTYPEHARDWARE This indicates one of the "generalized" hardware events provided by the kernel. See the config field definition for more details. PERFTYPESOFTWARE

This indicates one of the software-defined events pro‐ vided by the kernel (even if no hardware support is available). PERFTYPETRACEPOINT This indicates a tracepoint provided by the kernel trace‐ point infrastructure. PERFTYPEHWCACHE This indicates a hardware cache event. This has a spe‐ cial encoding, described in the config field definition. PERFTYPERAW

This indicates a "raw" implementation-specific event in the config field. PERFTYPEBREAKPOINT (since Linux 2.6.33) This indicates a hardware breakpoint as provided by the CPU. Breakpoints can be read/write accesses to an address as well as execution of an instruction address. dynamic PMU Since Linux 2.6.38, perfeventopen() can support multi‐ ple PMUs. To enable this, a value exported by the kernel can be used in the type field to indicate which PMU to use. The value to use can be found in the sysfs filesys‐ tem: there is a subdirectory per PMU instance under /sys/bus/eventsource/devices. In each subdirectory there is a type file whose content is an integer that can be used in the type field. For instance, /sys/bus/eventsource/devices/cpu/type contains the value for the core CPU PMU, which is usually 4. kprobe and uprobe (since Linux 4.17) These two dynamic PMUs create a kprobe/uprobe and attach it to the file descriptor generated by perfeventopen. The kprobe/uprobe will be destroyed on the destruction of the file descriptor. See fields kprobefunc, uprobepath, kprobeaddr, and probeoffset for more details. size The size of the perfeventattr structure for forward/backward compatibility. Set this using sizeof(struct perfeventattr) to allow the kernel to see the struct size at the time of compila‐ tion. The related define PERFATTRSIZEVER0 is set to 64; this was the size of the first published struct. PERFATTRSIZEVER1 is 72, corresponding to the addition of breakpoints in Linux 2.6.33. PERFATTRSIZEVER2 is 80 corresponding to the addition of branch sampling in Linux 3.4. PERFATTRSIZEVER3 is 96 cor‐ responding to the addition of sampleregsuser and sam‐ plestackuser in Linux 3.7. PERFATTRSIZEVER4 is 104 corre‐ sponding to the addition of sampleregsintr in Linux 3.19. PERFATTRSIZEVER5 is 112 corresponding to the addition of auxwatermark in Linux 4.1. config This specifies which event you want, in conjunction with the type field. The config1 and config2 fields are also taken into account in cases where 64 bits is not enough to fully specify the event. The encoding of these fields are event dependent. There are various ways to set the config field that are depen‐ dent on the value of the previously described type field. What follows are various possible settings for config separated out by type. If type is PERFTYPEHARDWARE, we are measuring one of the gen‐ eralized hardware CPU events. Not all of these are available on all platforms. Set config to one of the following: PERFCOUNTHWCPUCYCLES Total cycles. Be wary of what happens during CPU frequency scaling. PERFCOUNTHWINSTRUCTIONS Retired instructions. Be careful, these can be affected by various issues, most notably hardware interrupt counts. PERFCOUNTHWCACHEREFERENCES Cache accesses. Usually this indicates Last Level Cache accesses but this may vary depending on your CPU. This may include prefetches and coherency mes‐ sages; again this depends on the design of your CPU. PERFCOUNTHWCACHEMISSES Cache misses. Usually this indicates Last Level Cache misses; this is intended to be used in con‐ junction with the PERFCOUNTHWCACHEREFERENCES event to calculate cache miss rates. PERFCOUNTHWBRANCHINSTRUCTIONS Retired branch instructions. Prior to Linux 2.6.35, this used the wrong event on AMD processors. PERFCOUNTHWBRANCHMISSES Mispredicted branch instructions. PERFCOUNTHWBUSCYCLES Bus cycles, which can be different from total cycles. PERFCOUNTHWSTALLEDCYCLESFRONTEND (since Linux 3.0) Stalled cycles during issue. PERFCOUNTHWSTALLEDCYCLESBACKEND (since Linux 3.0) Stalled cycles during retirement. PERFCOUNTHWREFCPUCYCLES (since Linux 3.3) Total cycles; not affected by CPU frequency scaling. If type is PERFTYPESOFTWARE, we are measuring software events provided by the kernel. Set config to one of the following: PERFCOUNTSWCPUCLOCK

This reports the CPU clock, a high-resolution per- CPU timer. PERFCOUNTSWTASKCLOCK This reports a clock count specific to the task that is running. PERFCOUNTSWPAGEFAULTS This reports the number of page faults. PERFCOUNTSWCONTEXTSWITCHES This counts context switches. Until Linux 2.6.34,

these were all reported as user-space events, after that they are reported as happening in the kernel. PERFCOUNTSWCPUMIGRATIONS This reports the number of times the process has migrated to a new CPU. PERFCOUNTSWPAGEFAULTSMIN This counts the number of minor page faults. These did not require disk I/O to handle. PERFCOUNTSWPAGEFAULTSMAJ This counts the number of major page faults. These required disk I/O to handle. PERFCOUNTSWALIGNMENTFAULTS (since Linux 2.6.33) This counts the number of alignment faults. These happen when unaligned memory accesses happen; the kernel can handle these but it reduces performance. This happens only on some architectures (never on x86). PERFCOUNTSWEMULATIONFAULTS (since Linux 2.6.33) This counts the number of emulation faults. The kernel sometimes traps on unimplemented instructions and emulates them for user space. This can nega‐ tively impact performance. PERFCOUNTSWDUMMY (since Linux 3.12) This is a placeholder event that counts nothing. Informational sample record types such as mmap or comm must be associated with an active event. This dummy event allows gathering such records without requiring a counting event. If type is PERFTYPETRACEPOINT, then we are measuring kernel tracepoints. The value to use in config can be obtained from under debugfs tracing/events/*/*/id if ftrace is enabled in the kernel. If type is PERFTYPEHWCACHE, then we are measuring a hardware CPU cache event. To calculate the appropriate config value use the following equation: (perfhwcacheid) | (perfhwcacheopid << 8) | (perfhwcacheopresultid << 16) where perfhwcacheid is one of: PERFCOUNTHWCACHEL1D for measuring Level 1 Data Cache PERFCOUNTHWCACHEL1I for measuring Level 1 Instruction Cache PERFCOUNTHWCACHELL

for measuring Last-Level Cache PERFCOUNTHWCACHEDTLB for measuring the Data TLB PERFCOUNTHWCACHEITLB for measuring the Instruction TLB PERFCOUNTHWCACHEBPU for measuring the branch prediction unit PERFCOUNTHWCACHENODE (since Linux 3.1) for measuring local memory accesses and perfhwcacheopid is one of: PERFCOUNTHWCACHEOPREAD for read accesses PERFCOUNTHWCACHEOPWRITE for write accesses PERFCOUNTHWCACHEOPPREFETCH for prefetch accesses and perfhwcacheopresultid is one of: PERFCOUNTHWCACHERESULTACCESS to measure accesses PERFCOUNTHWCACHERESULTMISS to measure misses If type is PERFTYPERAW, then a custom "raw" config value is needed. Most CPUs support events that are not covered by the "generalized" events. These are implementation defined; see your CPU manual (for example the Intel Volume 3B documentation or the AMD BIOS and Kernel Developer Guide). The libpfm4 library can be used to translate from the name in the architec‐ tural manuals to the raw hex value perfeventopen() expects in this field. If type is PERFTYPEBREAKPOINT, then leave config set to zero. Its parameters are set in other places. If type is kprobe or uprobe, set retprobe (bit 0 of config, see /sys/bus/eventsource/devices/[k,u]probe/format/retprobe) for kretprobe/uretprobe. See fields kprobefunc, uprobepath, kprobeaddr, and probeoffset for more details. kprobefunc, uprobepath, kprobeaddr, and probeoffset These fields describe the kprobe/uprobe for dynamic PMUs kprobe and uprobe. For kprobe: use kprobefunc and probeoffset, or use kprobeaddr and leave kprobefunc as NULL. For uprobe: use uprobepath and probeoffset. sampleperiod, samplefreq A "sampling" event is one that generates an overflow notifica‐ tion every N events, where N is given by sampleperiod. A sam‐ pling event has sampleperiod > 0. When an overflow occurs, requested data is recorded in the mmap buffer. The sampletype field controls what data is recorded on each overflow. samplefreq can be used if you wish to use frequency rather than period. In this case, you set the freq flag. The kernel will adjust the sampling period to try and achieve the desired rate. The rate of adjustment is a timer tick. sampletype The various bits in this field specify which values to include

in the sample. They will be recorded in a ring-buffer, which is available to user space using mmap(2). The order in which the values are saved in the sample are documented in the MMAP Layout subsection below; it is not the enum perfeventsampleformat order. PERFSAMPLEIP Records instruction pointer. PERFSAMPLETID Records the process and thread IDs. PERFSAMPLETIME Records a timestamp. PERFSAMPLEADDR Records an address, if applicable. PERFSAMPLEREAD Record counter values for all events in a group, not just the group leader. PERFSAMPLECALLCHAIN Records the callchain (stack backtrace). PERFSAMPLEID Records a unique ID for the opened event's group leader. PERFSAMPLECPU Records CPU number. PERFSAMPLEPERIOD Records the current sampling period. PERFSAMPLESTREAMID Records a unique ID for the opened event. Unlike PERFSAMPLEID the actual ID is returned, not the group leader. This ID is the same as the one returned by PERFFORMATID. PERFSAMPLERAW Records additional data, if applicable. Usually returned by tracepoint events. PERFSAMPLEBRANCHSTACK (since Linux 3.4) This provides a record of recent branches, as provided by CPU branch sampling hardware (such as Intel Last Branch Record). Not all hardware supports this feature. See the branchsampletype field for how to filter which branches are reported. PERFSAMPLEREGSUSER (since Linux 3.7)

Records the current user-level CPU register state (the values in the process before the kernel was called). PERFSAMPLESTACKUSER (since Linux 3.7) Records the user level stack, allowing stack unwinding. PERFSAMPLEWEIGHT (since Linux 3.10) Records a hardware provided weight value that expresses how costly the sampled event was. This allows the hard‐ ware to highlight expensive events in a profile. PERFSAMPLEDATASRC (since Linux 3.10) Records the data source: where in the memory hierarchy the data associated with the sampled instruction came from. This is available only if the underlying hardware supports this feature. PERFSAMPLEIDENTIFIER (since Linux 3.12) Places the SAMPLEID value in a fixed position in the record, either at the beginning (for sample events) or at

the end (if a non-sample event). This was necessary because a sample stream may have records from various different event sources with differ‐ ent sampletype settings. Parsing the event stream prop‐ erly was not possible because the format of the record was needed to find SAMPLEID, but the format could not be found without knowing what event the sample belonged to (causing a circular dependency). The PERFSAMPLEIDENTIFIER setting makes the event stream always parsable by putting SAMPLEID in a fixed location, even though it means having duplicate SAMPLEID values in records. PERFSAMPLETRANSACTION (since Linux 3.13) Records reasons for transactional memory abort events (for example, from Intel TSX transactional memory sup‐ port). The preciseip setting must be greater than 0 and a transactional memory abort event must be measured or no values will be recorded. Also note that some perfevent measurements, such as sampled cycle counting, may cause extraneous aborts (by causing an interrupt during a transaction). PERFSAMPLEREGSINTR (since Linux 3.19) Records a subset of the current CPU register state as specified by sampleregsintr. Unlike PERFSAM‐ PLEREGSUSER the register values will return kernel reg‐ ister state if the overflow happened while kernel code is running. If the CPU supports hardware sampling of regis‐ ter state (i.e., PEBS on Intel x86) and preciseip is set higher than zero then the register values returned are those captured by hardware at the time of the sampled instruction's retirement. readformat This field specifies the format of the data returned by read(2) on a perfeventopen() file descriptor. PERFFORMATTOTALTIMEENABLED

Adds the 64-bit timeenabled field. This can be used to calculate estimated totals if the PMU is overcommitted and multiplexing is happening. PERFFORMATTOTALTIMERUNNING

Adds the 64-bit timerunning field. This can be used to calculate estimated totals if the PMU is overcommitted and multiplexing is happening. PERFFORMATID

Adds a 64-bit unique value that corresponds to the event group. PERFFORMATGROUP Allows all counter values in an event group to be read with one read. disabled The disabled bit specifies whether the counter starts out dis‐ abled or enabled. If disabled, the event can later be enabled by ioctl(2), prctl(2), or enableonexec. When creating an event group, typically the group leader is ini‐ tialized with disabled set to 1 and any child events are ini‐ tialized with disabled set to 0. Despite disabled being 0, the child events will not start until the group leader is enabled. inherit The inherit bit specifies that this counter should count events of child tasks as well as the task specified. This applies only to new children, not to any existing children at the time the counter is created (nor to any new children of existing chil‐ dren). Inherit does not work for some combinations of readformat val‐ ues, such as PERFFORMATGROUP. pinned The pinned bit specifies that the counter should always be on the CPU if at all possible. It applies only to hardware coun‐ ters and only to group leaders. If a pinned counter cannot be put onto the CPU (e.g., because there are not enough hardware counters or because of a conflict with some other event), then

the counter goes into an 'error' state, where reads return end-

of-file (i.e., read(2) returns 0) until the counter is subse‐ quently enabled or disabled. exclusive The exclusive bit specifies that when this counter's group is on the CPU, it should be the only group using the CPU's counters. In the future this may allow monitoring programs to support PMU features that need to run alone so that they do not disrupt other hardware counters. Note that many unexpected situations may prevent events with the exclusive bit set from ever running. This includes any users

running a system-wide measurement as well as any kernel use of the performance counters (including the commonly enabled NMI Watchdog Timer interface). excludeuser If this bit is set, the count excludes events that happen in user space. excludekernel If this bit is set, the count excludes events that happen in kernel space. excludehv If this bit is set, the count excludes events that happen in the

hypervisor. This is mainly for PMUs that have built-in support for handling this (such as POWER). Extra support is needed for handling hypervisor measurements on most machines. excludeidle If set, don't count when the CPU is running the idle task. While you can currently enable this for any event type, it is ignored for all but software events. mmap The mmap bit enables generation of PERFRECORDMMAP samples for every mmap(2) call that has PROTEXEC set. This allows tools to notice new executable code being mapped into a program (dynamic shared libraries for example) so that addresses can be mapped back to the original code. comm The comm bit enables tracking of process command name as modi‐ fied by the exec(2) and prctl(PRSETNAME) system calls as well as writing to /proc/self/comm. If the commexec flag is also successfully set (possible since Linux 3.16), then the misc flag PERFRECORDMISCCOMMEXEC can be used to differentiate the exec(2) case from the others. freq If this bit is set, then samplefrequency not sampleperiod is used when setting up the sampling interval. inheritstat This bit enables saving of event counts on context switch for inherited tasks. This is meaningful only if the inherit field is set. enableonexec If this bit is set, a counter is automatically enabled after a call to exec(2). task If this bit is set, then fork/exit notifications are included in the ring buffer. watermark If set, have an overflow notification happen when we cross the wakeupwatermark boundary. Otherwise, overflow notifications happen after wakeupevents samples. preciseip (since Linux 2.6.35) This controls the amount of skid. Skid is how many instructions execute between an event of interest happening and the kernel being able to stop and record the event. Smaller skid is better and allows more accurate reporting of which events correspond to which instructions, but hardware is often limited with how small this can be. The possible values of this field are the following: 0 SAMPLEIP can have arbitrary skid. 1 SAMPLEIP must have constant skid. 2 SAMPLEIP requested to have 0 skid. 3 SAMPLEIP must have 0 skid. See also the description of PERFRECORDMISCEXACTIP. mmapdata (since Linux 2.6.36) This is the counterpart of the mmap field. This enables genera‐ tion of PERFRECORDMMAP samples for mmap(2) calls that do not have PROTEXEC set (for example data and SysV shared memory). sampleidall (since Linux 2.6.38) If set, then TID, TIME, ID, STREAMID, and CPU can additionally

be included in non-PERFRECORDSAMPLEs if the corresponding sam‐ pletype is selected. If PERFSAMPLEIDENTIFIER is specified, then an additional ID value is included as the last value to ease parsing the record stream. This may lead to the id value appearing twice.

The layout is described by this pseudo-structure: struct sampleid { { u32 pid, tid; } /* if PERFSAMPLETID set */ { u64 time; } /* if PERFSAMPLETIME set */ { u64 id; } /* if PERFSAMPLEID set */ { u64 streamid;} /* if PERFSAMPLESTREAMID set */ { u32 cpu, res; } /* if PERFSAMPLECPU set */ { u64 id; } /* if PERFSAMPLEIDENTIFIER set */ }; excludehost (since Linux 3.2) When conducting measurements that include processes running VM instances (i.e., have executed a KVMRUN ioctl(2)), only measure events happening inside a guest instance. This is only meaning‐ ful outside the guests; this setting does not change counts gathered inside of a guest. Currently, this functionality is x86 only. excludeguest (since Linux 3.2) When conducting measurements that include processes running VM instances (i.e., have executed a KVMRUN ioctl(2)), do not mea‐ sure events happening inside guest instances. This is only meaningful outside the guests; this setting does not change counts gathered inside of a guest. Currently, this functional‐ ity is x86 only. excludecallchainkernel (since Linux 3.7) Do not include kernel callchains. excludecallchainuser (since Linux 3.7) Do not include user callchains. mmap2 (since Linux 3.16) Generate an extended executable mmap record that contains enough additional information to uniquely identify shared mappings. The mmap flag must also be set for this to work. commexec (since Linux 3.16)

This is purely a feature-detection flag, it does not change ker‐ nel behavior. If this flag can successfully be set, then, when comm is enabled, the PERFRECORDMISCCOMMEXEC flag will be set in the misc field of a comm record header if the rename event being reported was caused by a call to exec(2). This allows tools to distinguish between the various types of process renam‐ ing. useclockid (since Linux 4.1) This allows selecting which internal Linux clock to use when generating timestamps via the clockid field. This can make it easier to correlate perf sample times with timestamps generated by other tools. contextswitch (since Linux 4.3) This enables the generation of PERFRECORDSWITCH records when a context switch occurs. It also enables the generation of

PERFRECORDSWITCHCPUWIDE records when sampling in CPU-wide mode. This functionality is in addition to existing tracepoint and software events for measuring context switches. The advan‐ tage of this method is that it will give full information even with strict perfeventparanoid settings. wakeupevents, wakeupwatermark This union sets how many samples (wakeupevents) or bytes (wakeupwatermark) happen before an overflow notification hap‐ pens. Which one is used is selected by the watermark bit flag. wakeupevents counts only PERFRECORDSAMPLE record types. To receive overflow notification for all PERFRECORD types choose watermark and set wakeupwatermark to 1. Prior to Linux 3.0, setting wakeupevents to 0 resulted in no overflow notifications; more recent kernels treat 0 the same as 1. bptype (since Linux 2.6.33) This chooses the breakpoint type. It is one of: HWBREAKPOINTEMPTY No breakpoint. HWBREAKPOINTR Count when we read the memory location. HWBREAKPOINTW Count when we write the memory location. HWBREAKPOINTRW Count when we read or write the memory location. HWBREAKPOINTX Count when we execute code at the memory location. The values can be combined via a bitwise or, but the combination of HWBREAKPOINTR or HWBREAKPOINTW with HWBREAKPOINTX is not allowed. bpaddr (since Linux 2.6.33) This is the address of the breakpoint. For execution break‐ points, this is the memory address of the instruction of inter‐ est; for read and write breakpoints, it is the memory address of the memory location of interest. config1 (since Linux 2.6.39) config1 is used for setting events that need an extra register or otherwise do not fit in the regular config field. Raw OFF‐ COREEVENTS on Nehalem/Westmere/SandyBridge use this field on Linux 3.3 and later kernels. bplen (since Linux 2.6.33) bplen is the length of the breakpoint being measured if type is PERFTYPEBREAKPOINT. Options are HWBREAKPOINTLEN1, HWBREAKPOINTLEN2, HWBREAKPOINTLEN4, and HWBREAK‐ POINTLEN8. For an execution breakpoint, set this to sizeof(long). config2 (since Linux 2.6.39) config2 is a further extension of the config1 field. branchsampletype (since Linux 3.4) If PERFSAMPLEBRANCHSTACK is enabled, then this specifies what branches to include in the branch record. The first part of the value is the privilege level, which is a combination of one of the values listed below. If the user does not set privilege level explicitly, the kernel will use the event's privilege level. Event and branch privilege levels do not have to match. PERFSAMPLEBRANCHUSER Branch target is in user space. PERFSAMPLEBRANCHKERNEL Branch target is in kernel space. PERFSAMPLEBRANCHHV Branch target is in hypervisor. PERFSAMPLEBRANCHPLMALL A convenience value that is the three preceding values ORed together. In addition to the privilege value, at least one or more of the following bits must be set. PERFSAMPLEBRANCHANY Any branch type. PERFSAMPLEBRANCHANYCALL Any call branch (includes direct calls, indirect calls, and far jumps). PERFSAMPLEBRANCHINDCALL Indirect calls. PERFSAMPLEBRANCHCALL (since Linux 4.4) Direct calls. PERFSAMPLEBRANCHANYRETURN Any return branch. PERFSAMPLEBRANCHINDJUMP (since Linux 4.2) Indirect jumps. PERFSAMPLEBRANCHCOND (since Linux 3.16) Conditional branches. PERFSAMPLEBRANCHABORTTX (since Linux 3.11) Transactional memory aborts. PERFSAMPLEBRANCHINTX (since Linux 3.11) Branch in transactional memory transaction. PERFSAMPLEBRANCHNOTX (since Linux 3.11) Branch not in transactional memory transaction. PERFSAMPLEBRANCHCALLSTACK (since Linux 4.1) Branch is

part of a hardware-generated call stack. This requires hardware support, currently only found on Intel x86 Haswell or newer. sampleregsuser (since Linux 3.7) This bit mask defines the set of user CPU registers to dump on

samples. The layout of the register mask is architecture-spe‐ cific and is described in the kernel header file arch/ARCH/include/uapi/asm/perfregs.h. samplestackuser (since Linux 3.7) This defines the size of the user stack to dump if PERFSAM‐ PLESTACKUSER is specified. clockid (since Linux 4.1) If useclockid is set, then this field selects which internal Linux timer to use for timestamps. The available timers are defined in linux/time.h, with CLOCKMONOTONIC, CLOCKMONO‐ TONICRAW, CLOCKREALTIME, CLOCKBOOTTIME, and CLOCKTAI cur‐ rently supported. auxwatermark (since Linux 4.1) This specifies how much data is required to trigger a PERFRECORDAUX sample. samplemaxstack (since Linux 4.8) When sampletype includes PERFSAMPLECALLCHAIN, this field specifies how many stack frames to report when generating the callchain. Reading results Once a perfeventopen() file descriptor has been opened, the values of the events can be read from the file descriptor. The values that are there are specified by the readformat field in the attr structure at open time. If you attempt to read into a buffer that is not big enough to hold the data, the error ENOSPC results. Here is the layout of the data returned by a read: * If PERFFORMATGROUP was specified to allow reading all events in a group at once: struct readformat { u64 nr; /* The number of events */ u64 timeenabled; /* if PERFFORMATTOTALTIMEENABLED */ u64 timerunning; /* if PERFFORMATTOTALTIMERUNNING */ struct { u64 value; /* The value of the event */ u64 id; /* if PERFFORMATID */ } values[nr]; }; * If PERFFORMATGROUP was not specified: struct readformat { u64 value; /* The value of the event */ u64 timeenabled; /* if PERFFORMATTOTALTIMEENABLED */ u64 timerunning; /* if PERFFORMATTOTALTIMERUNNING */ u64 id; /* if PERFFORMATID */ }; The values read are as follows: nr The number of events in this file descriptor. Available only if PERFFORMATGROUP was specified. timeenabled, timerunning Total time the event was enabled and running. Normally these values are the same. If more events are started, then available counter slots on the PMU, then multiplexing happens and events run only part of the time. In that case, the timeenabled and time running values can be used to scale an estimated value for the count.

value An unsigned 64-bit value containing the counter result. id A globally unique value for this particular event; only present if PERFFORMATID was specified in readformat. MMAP layout When using perfeventopen() in sampled mode, asynchronous events (like

counter overflow or PROTEXEC mmap tracking) are logged into a ring-

buffer. This ring-buffer is created and accessed through mmap(2). The mmap size should be 1+2^n pages, where the first page is a metadata page (struct perfeventmmappage) that contains various bits of infor‐

mation such as where the ring-buffer head is. Before kernel 2.6.39, there is a bug that means you must allocate an mmap ring buffer when sampling even if you do not plan to access it. The structure of the first metadata mmap page is as follows: struct perfeventmmappage { u32 version; /* version number of this structure */ u32 compatversion; /* lowest version this is compat with */ u32 lock; /* seqlock for synchronization */ u32 index; /* hardware counter identifier */ s64 offset; /* add to hardware counter value */ u64 timeenabled; /* time event active */ u64 timerunning; /* time event on CPU */ union { u64 capabilities; struct { u64 capusrtime / capusrrdpmc / capbit0 : 1, capbit0isdeprecated : 1, capuserrdpmc : 1, capusertime : 1, capusertimezero : 1, }; }; u16 pmcwidth; u16 timeshift; u32 timemult; u64 timeoffset; u64 reserved[120]; /* Pad to 1 k */ u64 datahead; /* head in the data section */

u64 datatail; /* user-space written tail */ u64 dataoffset; /* where the buffer starts */ u64 datasize; /* data buffer size */ u64 auxhead; u64 auxtail; u64 auxoffset; u64 auxsize; } The following list describes the fields in the perfeventmmappage structure in more detail: version Version number of this structure. compatversion The lowest version this is compatible with. lock A seqlock for synchronization. index A unique hardware counter identifier. offset When using rdpmc for reads this offset value must be added to the one returned by rdpmc to get the current total event count. timeenabled Time the event was active. timerunning Time the event was running. capusrtime / capusrrdpmc / capbit0 (since Linux 3.4) There was a bug in the definition of capusrtime and capusrrdpmc from Linux 3.4 until Linux 3.11. Both bits were defined to point to the same location, so it was impossible to know if capusrtime or capusrrdpmc were actually set. Starting with Linux 3.12, these are renamed to capbit0 and you should use the capusertime and capuserrdpmc fields instead. capbit0isdeprecated (since Linux 3.12) If set, this bit indicates that the kernel supports the properly separated capusertime and capuserrdpmc bits.

If not-set, it indicates an older kernel where capusrtime and capusrrdpmc map to the same bit and thus both features should be used with caution. capuserrdpmc (since Linux 3.12)

If the hardware supports user-space read of performance counters without syscall (this is the "rdpmc" instruction on x86), then the following code can be used to do a read: u32 seq, timemult, timeshift, idx, width; u64 count, enabled, running; u64 cyc, timeoffset; do {

seq = pc->lock; barrier();

enabled = pc->timeenabled;

running = pc->timerunning;

if (pc->capusrtime && enabled != running) { cyc = rdtsc();

timeoffset = pc->timeoffset;

timemult = pc->timemult;

timeshift = pc->timeshift; }

idx = pc->index;

count = pc->offset;

if (pc->capusrrdpmc && idx) {

width = pc->pmcwidth;

count += rdpmc(idx - 1); } barrier();

} while (pc->lock != seq); capusertime (since Linux 3.12) This bit indicates the hardware has a constant, nonstop time‐ stamp counter (TSC on x86). capusertimezero (since Linux 3.12) Indicates the presence of timezero which allows mapping time‐ stamp values to the hardware clock. pmcwidth

If capusrrdpmc, this field provides the bit-width of the value read using the rdpmc or equivalent instruction. This can be used to sign extend the result like:

pmc <<= 64 - pmcwidth;

pmc >>= 64 - pmcwidth; // signed shift right count += pmc; timeshift, timemult, timeoffset If capusrtime, these fields can be used to compute the time delta since timeenabled (in nanoseconds) using rdtsc or simi‐ lar. u64 quot, rem; u64 delta; quot = (cyc >> timeshift);

rem = cyc & (((u64)1 << timeshift) - 1); delta = timeoffset + quot * timemult + ((rem * timemult) >> timeshift); Where timeoffset, timemult, timeshift, and cyc are read in the seqcount loop described above. This delta can then be added to enabled and possible running (if idx), improving the scaling: enabled += delta; if (idx) running += delta; quot = count / running;

rem = count % running; count = quot * enabled + (rem * enabled) / running; timezero (since Linux 3.12) If capusrtimezero is set, then the hardware clock (the TSC timestamp counter on x86) can be calculated from the timezero, timemult, and timeshift values:

time = timestamp - timezero; quot = time / timemult;

rem = time % timemult; cyc = (quot << timeshift) + (rem << timeshift) / timemult; And vice versa: quot = cyc >> timeshift;

rem = cyc & (((u64)1 << timeshift) - 1); timestamp = timezero + quot * timemult + ((rem * timemult) >> timeshift); datahead This points to the head of the data section. The value continu‐ ously increases, it does not wrap. The value needs to be manu‐ ally wrapped by the size of the mmap buffer before accessing the samples.

On SMP-capable platforms, after reading the datahead value, user space should issue an rmb(). datatail When the mapping is PROTWRITE, the datatail value should be written by user space to reflect the last read data. In this case, the kernel will not overwrite unread data. dataoffset (since Linux 4.1) Contains the offset of the location in the mmap buffer where perf sample data begins. datasize (since Linux 4.1) Contains the size of the perf sample region within the mmap buf‐ fer. auxhead, auxtail, auxoffset, auxsize (since Linux 4.1)

The AUX region allows mmaping a separate sample buffer for high- bandwidth data streams (separate from the main perf sample buf‐

fer). An example of a high-bandwidth stream is instruction tracing support, as is found in newer Intel processors. To set up an AUX area, first auxoffset needs to be set with an offset greater than dataoffset+datasize and auxsize needs to be set to the desired buffer size. The desired offset and size must be page aligned, and the size must be a power of two. These values are then passed to mmap in order to map the AUX buffer. Pages in the AUX buffer are included as part of the RLIMITMEMLOCK resource limit (see setrlimit(2)), and also as part of the perfeventmlockkb allowance. By default, the AUX buffer will be truncated if it will not fit in the available space in the ring buffer. If the AUX buffer is mapped as a read only buffer, then it will operate in ring buf‐ fer mode where old data will be overwritten by new. In over‐ write mode, it might not be possible to infer where the new data began, and it is the consumer's job to disable measurement while reading to avoid possible data races. The auxhead and auxtail ring buffer pointers have the same behavior and ordering rules as the previous described datahead and datatail.

The following 2^n ring-buffer pages have the layout described below. If perfeventattr.sampleidall is set, then all event types will have the sampletype selected fields related to where/when (identity) an event took place (TID, TIME, ID, CPU, STREAMID) described in PERFRECORDSAMPLE below, it will be stashed just after the perfeventheader and the fields already present for the existing fields, that is, at the end of the payload. This allows a newer perf.data file to be supported by older perf tools, with the new optional fields being ignored. The mmap values start with a header: struct perfeventheader { u32 type; u16 misc; u16 size; }; Below, we describe the perfeventheader fields in more detail. For ease of reading, the fields with shorter descriptions are presented first. size This indicates the size of the record. misc The misc field contains additional information about the sample. The CPU mode can be determined from this value by masking with PERFRECORDMISCCPUMODEMASK and looking for one of the follow‐ ing (note these are not bit masks, only one can be set at a time): PERFRECORDMISCCPUMODEUNKNOWN Unknown CPU mode. PERFRECORDMISCKERNEL Sample happened in the kernel. PERFRECORDMISCUSER Sample happened in user code. PERFRECORDMISCHYPERVISOR Sample happened in the hypervisor. PERFRECORDMISCGUESTKERNEL (since Linux 2.6.35) Sample happened in the guest kernel. PERFRECORDMISCGUESTUSER (since Linux 2.6.35) Sample happened in guest user code. Since the following three statuses are generated by different record types, they alias to the same bit: PERFRECORDMISCMMAPDATA (since Linux 3.10) This is set when the mapping is not executable; otherwise the mapping is executable. PERFRECORDMISCCOMMEXEC (since Linux 3.16) This is set for a PERFRECORDCOMM record on kernels more recent than Linux 3.16 if a process name change was caused by an exec(2) system call. PERFRECORDMISCSWITCHOUT (since Linux 4.3) When a PERFRECORDSWITCH or PERFRECORDSWITCHCPUWIDE record is generated, this bit indicates that the context switch is away from the current process (instead of into the current process). In addition, the following bits can be set: PERFRECORDMISCEXACTIP This indicates that the content of PERFSAMPLEIP points to the actual instruction that triggered the event. See also perfeventattr.preciseip. PERFRECORDMISCEXTRESERVED (since Linux 2.6.35) This indicates there is extended data available (cur‐ rently not used). PERFRECORDMISCPROCMAPPARSETIMEOUT This bit is not set by the kernel. It is reserved for

the user-space perf utility to indicate that /proc/i[pid]/maps parsing was taking too long and was stopped, and thus the mmap records may be truncated. type The type value is one of the below. The values in the corre‐ sponding record (that follows the header) depend on the type selected as shown. PERFRECORDMMAP The MMAP events record the PROTEXEC mappings so that we can

correlate user-space IPs to code. They have the following structure: struct { struct perfeventheader header; u32 pid, tid; u64 addr; u64 len; u64 pgoff; char filename[]; }; pid is the process ID. tid is the thread ID. addr is the address of the allocated memory. len is the length of the allocated memory. pgoff is the page offset of the allocated memory. filename is a string describing the backing of the allocated memory. PERFRECORDLOST This record indicates when events are lost. struct { struct perfeventheader header; u64 id; u64 lost; struct sampleid sampleid; }; id is the unique event ID for the samples that were lost. lost is the number of events that were lost. PERFRECORDCOMM This record indicates a change in the process name. struct { struct perfeventheader header; u32 pid; u32 tid; char comm[]; struct sampleid sampleid; }; pid is the process ID. tid is the thread ID. comm is a string containing the new name of the process. PERFRECORDEXIT This record indicates a process exit event. struct { struct perfeventheader header; u32 pid, ppid; u32 tid, ptid; u64 time; struct sampleid sampleid; }; PERFRECORDTHROTTLE, PERFRECORDUNTHROTTLE This record indicates a throttle/unthrottle event. struct { struct perfeventheader header; u64 time; u64 id; u64 streamid; struct sampleid sampleid; }; PERFRECORDFORK This record indicates a fork event. struct { struct perfeventheader header; u32 pid, ppid; u32 tid, ptid; u64 time; struct sampleid sampleid; }; PERFRECORDREAD This record indicates a read event. struct { struct perfeventheader header; u32 pid, tid; struct readformat values; struct sampleid sampleid; }; PERFRECORDSAMPLE This record indicates a sample. struct { struct perfeventheader header; u64 sampleid; /* if PERFSAMPLEIDENTIFIER */ u64 ip; /* if PERFSAMPLEIP */ u32 pid, tid; /* if PERFSAMPLETID */ u64 time; /* if PERFSAMPLETIME */ u64 addr; /* if PERFSAMPLEADDR */ u64 id; /* if PERFSAMPLEID */ u64 streamid; /* if PERFSAMPLESTREAMID */ u32 cpu, res; /* if PERFSAMPLECPU */ u64 period; /* if PERFSAMPLEPERIOD */ struct readformat v; /* if PERFSAMPLEREAD */ u64 nr; /* if PERFSAMPLECALLCHAIN */ u64 ips[nr]; /* if PERFSAMPLECALLCHAIN */ u32 size; /* if PERFSAMPLERAW */ char data[size]; /* if PERFSAMPLERAW */ u64 bnr; /* if PERFSAMPLEBRANCHSTACK */ struct perfbranchentry lbr[bnr]; /* if PERFSAMPLEBRANCHSTACK */ u64 abi; /* if PERFSAMPLEREGSUSER */ u64 regs[weight(mask)]; /* if PERFSAMPLEREGSUSER */ u64 size; /* if PERFSAMPLESTACKUSER */ char data[size]; /* if PERFSAMPLESTACKUSER */ u64 dynsize; /* if PERFSAMPLESTACKUSER && size != 0 */ u64 weight; /* if PERFSAMPLEWEIGHT */ u64 datasrc; /* if PERFSAMPLEDATASRC */ u64 transaction; /* if PERFSAMPLETRANSACTION */ u64 abi; /* if PERFSAMPLEREGSINTR */ u64 regs[weight(mask)]; /* if PERFSAMPLEREGSINTR */ }; sampleid

If PERFSAMPLEIDENTIFIER is enabled, a 64-bit unique ID is included. This is a duplication of the PERFSAM‐ PLEID id value, but included at the beginning of the sample so parsers can easily obtain the value.

ip If PERFSAMPLEIP is enabled, then a 64-bit instruction pointer value is included. pid, tid

If PERFSAMPLETID is enabled, then a 32-bit process ID

and 32-bit thread ID are included. time

If PERFSAMPLETIME is enabled, then a 64-bit timestamp is included. This is obtained via localclock() which is a hardware timestamp if available and the jiffies value if not. addr

If PERFSAMPLEADDR is enabled, then a 64-bit address is included. This is usually the address of a tracepoint, breakpoint, or software event; otherwise the value is 0.

id If PERFSAMPLEID is enabled, a 64-bit unique ID is included. If the event is a member of an event group, the group leader ID is returned. This ID is the same as the one returned by PERFFORMATID. streamid

If PERFSAMPLESTREAMID is enabled, a 64-bit unique ID is included. Unlike PERFSAMPLEID the actual ID is returned, not the group leader. This ID is the same as the one returned by PERFFORMATID. cpu, res

If PERFSAMPLECPU is enabled, this is a 32-bit value indicating which CPU was being used, in addition to a

reserved (unused) 32-bit value. period

If PERFSAMPLEPERIOD is enabled, a 64-bit value indi‐ cating the current sampling period is written. v If PERFSAMPLEREAD is enabled, a structure of type readformat is included which has values for all events in the event group. The values included depend on the readformat value used at perfeventopen() time. nr, ips[nr]

If PERFSAMPLECALLCHAIN is enabled, then a 64-bit num‐ ber is included which indicates how many following

64-bit instruction pointers will follow. This is the current callchain. size, data[size]

If PERFSAMPLERAW is enabled, then a 32-bit value indi‐

cating size is included followed by an array of 8-bit values of length size. The values are padded with 0 to

have 64-bit alignment. This RAW record data is opaque with respect to the ABI. The ABI doesn't make any promises with respect to the stability of its content, it may vary depending on event, hardware, and kernel version. bnr, lbr[bnr]

If PERFSAMPLEBRANCHSTACK is enabled, then a 64-bit value indicating the number of records is included, fol‐ lowed by bnr perfbranchentry structures which each include the fields: from This indicates the source instruction (may not be a branch). to The branch target. mispred The branch target was mispredicted. predicted The branch target was predicted. intx (since Linux 3.11) The branch was in a transactional memory transac‐ tion. abort (since Linux 3.11) The branch was in an aborted transactional memory transaction. cycles (since Linux 4.3) This reports the number of cycles elapsed since the previous branch stack update. The entries are from most to least recent, so the first entry has the most recent branch. Support for mispred, predicted, and cycles is optional; if not supported, those values will be 0. The type of branches recorded is specified by the branchsampletype field. abi, regs[weight(mask)] If PERFSAMPLEREGSUSER is enabled, then the user CPU registers are recorded. The abi field is one of PERFSAMPLEREGSABINONE, PERFSAMPLEREGSABI32 or PERFSAMPLEREGSABI64. The regs field is an array of the CPU registers that were specified by the sampleregsuser attr field. The number of values is the number of bits set in the sam‐ pleregsuser bit mask. size, data[size], dynsize If PERFSAMPLESTACKUSER is enabled, then the user stack is recorded. This can be used to generate stack backtraces. size is the size requested by the user in samplestackuser or else the maximum record size. data is the stack data (a raw dump of the memory pointed to by the stack pointer at the time of sampling). dynsize is the amount of data actually dumped (can be less than size). Note that dynsize is omitted if size is 0. weight

If PERFSAMPLEWEIGHT is enabled, then a 64-bit value provided by the hardware is recorded that indicates how costly the event was. This allows expensive events to stand out more clearly in profiles. datasrc

If PERFSAMPLEDATASRC is enabled, then a 64-bit value is recorded that is made up of the following fields: memop Type of opcode, a bitwise combination of: PERFMEMOPNA Not available PERFMEMOPLOAD Load instruction PERFMEMOPSTORE Store instruction PERFMEMOPPFETCH Prefetch PERFMEMOPEXEC Executable code memlvl Memory hierarchy level hit or miss, a bitwise combi‐ nation of the following, shifted left by PERFMEMLVLSHIFT: PERFMEMLVLNA Not available PERFMEMLVLHIT Hit PERFMEMLVLMISS Miss PERFMEMLVLL1 Level 1 cache PERFMEMLVLLFB Line fill buffer PERFMEMLVLL2 Level 2 cache PERFMEMLVLL3 Level 3 cache PERFMEMLVLLOCRAM Local DRAM PERFMEMLVLREMRAM1 Remote DRAM 1 hop PERFMEMLVLREMRAM2 Remote DRAM 2 hops PERFMEMLVLREMCCE1 Remote cache 1 hop PERFMEMLVLREMCCE2 Remote cache 2 hops PERFMEMLVLIO I/O memory PERFMEMLVLUNC Uncached memory memsnoop Snoop mode, a bitwise combination of the following, shifted left by PERFMEMSNOOPSHIFT: PERFMEMSNOOPNA Not available PERFMEMSNOOPNONE No snoop PERFMEMSNOOPHIT Snoop hit PERFMEMSNOOPMISS Snoop miss PERFMEMSNOOPHITM Snoop hit modified memlock Lock instruction, a bitwise combination of the fol‐ lowing, shifted left by PERFMEMLOCKSHIFT: PERFMEMLOCKNA Not available PERFMEMLOCKLOCKED Locked transaction memdtlb TLB access hit or miss, a bitwise combination of the following, shifted left by PERFMEMTLBSHIFT: PERFMEMTLBNA Not available PERFMEMTLBHIT Hit PERFMEMTLBMISS Miss PERFMEMTLBL1 Level 1 TLB PERFMEMTLBL2 Level 2 TLB PERFMEMTLBWK Hardware walker PERFMEMTLBOS OS fault handler transaction If the PERFSAMPLETRANSACTION flag is set, then a

64-bit field is recorded describing the sources of any transactional memory aborts. The field is a bitwise combination of the following val‐ ues: PERFTXNELISION

Abort from an elision type transaction (Intel-

CPU-specific). PERFTXNTRANSACTION Abort from a generic transaction. PERFTXNSYNC Synchronous abort (related to the reported instruction). PERFTXNASYNC Asynchronous abort (not related to the reported instruction). PERFTXNRETRY Retryable abort (retrying the transaction may have succeeded). PERFTXNCONFLICT Abort due to memory conflicts with other threads. PERFTXNCAPACITYWRITE Abort due to write capacity overflow. PERFTXNCAPACITYREAD Abort due to read capacity overflow.

In addition, a user-specified abort code can be obtained from the high 32 bits of the field by shifting right by PERFTXNABORTSHIFT and masking with the value PERFTXNABORTMASK. abi, regs[weight(mask)] If PERFSAMPLEREGSINTR is enabled, then the user CPU registers are recorded. The abi field is one of PERFSAMPLEREGSABINONE, PERFSAMPLEREGSABI32, or PERFSAMPLEREGSABI64. The regs field is an array of the CPU registers that were specified by the sampleregsintr attr field. The number of values is the number of bits set in the sam‐ pleregsintr bit mask. PERFRECORDMMAP2 This record includes extended information on mmap(2) calls returning executable mappings. The format is similar to that of the PERFRECORDMMAP record, but includes extra val‐ ues that allow uniquely identifying shared mappings. struct { struct perfeventheader header; u32 pid; u32 tid; u64 addr; u64 len; u64 pgoff; u32 maj; u32 min; u64 ino; u64 inogeneration; u32 prot; u32 flags; char filename[]; struct sampleid sampleid; }; pid is the process ID. tid is the thread ID. addr is the address of the allocated memory. len is the length of the allocated memory. pgoff is the page offset of the allocated memory. maj is the major ID of the underlying device. min is the minor ID of the underlying device. ino is the inode number. inogeneration is the inode generation. prot is the protection information. flags is the flags information. filename is a string describing the backing of the allocated memory. PERFRECORDAUX (since Linux 4.1) This record reports that new data is available in the sepa‐ rate AUX buffer region. struct { struct perfeventheader header; u64 auxoffset; u64 auxsize; u64 flags; struct sampleid sampleid; }; auxoffset offset in the AUX mmap region where the new data begins. auxsize size of the data made available. flags describes the AUX update. PERFAUXFLAGTRUNCATED if set, then the data returned was truncated to fit the available buffer size. PERFAUXFLAGOVERWRITE if set, then the data returned has overwritten previous data. PERFRECORDITRACESTART (since Linux 4.1) This record indicates which process has initiated an instruction trace event, allowing tools to properly corre‐ late the instruction addresses in the AUX buffer with the proper executable. struct { struct perfeventheader header; u32 pid; u32 tid; }; pid process ID of the thread starting an instruction trace. tid thread ID of the thread starting an instruction trace. PERFRECORDLOSTSAMPLES (since Linux 4.2) When using hardware sampling (such as Intel PEBS) this record indicates some number of samples that may have been lost. struct { struct perfeventheader header; u64 lost; struct sampleid sampleid; }; lost the number of potentially lost samples. PERFRECORDSWITCH (since Linux 4.3) This record indicates a context switch has happened. The PERFRECORDMISCSWITCHOUT bit in the misc field indicates whether it was a context switch into or away from the cur‐ rent process. struct { struct perfeventheader header; struct sampleid sampleid; }; PERFRECORDSWITCHCPUWIDE (since Linux 4.3) As with PERFRECORDSWITCH this record indicates a context switch has happened, but it only occurs when sampling in

CPU-wide mode and provides additional information on the process being switched to/from. The PERFRECORDMISCSWITCHOUT bit in the misc field indicates whether it was a context switch into or away from the cur‐ rent process. struct { struct perfeventheader header; u32 nextprevpid; u32 nextprevtid; struct sampleid sampleid; }; nextprevpid The process ID of the previous (if switching in) or next (if switching out) process on the CPU. nextprevtid The thread ID of the previous (if switching in) or next (if switching out) thread on the CPU. Overflow handling Events can be set to notify when a threshold is crossed, indicating an overflow. Overflow conditions can be captured by monitoring the event file descriptor with poll(2), select(2), or epoll(7). Alternatively, the overflow events can be captured via sa signal handler, by enabling I/O signaling on the file descriptor; see the discussion of the FSETOWN and FSETSIG operations in fcntl(2). Overflows are generated only by sampling events (sampleperiod must have a nonzero value). There are two ways to generate overflow notifications. The first is to set a wakeupevents or wakeupwatermark value that will trigger if a certain number of samples or bytes have been written to the mmap ring buffer. In this case, POLLIN is indicated. The other way is by use of the PERFEVENTIOCREFRESH ioctl. This ioctl adds to a counter that decrements each time the event overflows. When nonzero, POLLIN is indicated, but once the counter reaches 0 POLLHUP is indicated and the underlying event is disabled. Refreshing an event group leader refreshes all siblings and refreshing with a parameter of 0 currently enables infinite refreshes; these behaviors are unsupported and should not be relied on. Starting with Linux 3.18, POLLHUP is indicated if the event being mon‐ itored is attached to a different process and that process exits. rdpmc instruction Starting with Linux 3.4 on x86, you can use the rdpmc instruction to

get low-latency reads without having to enter the kernel. Note that using rdpmc is not necessarily faster than other methods for reading event values. Support for this can be detected with the capusrrdpmc field in the mmap page; documentation on how to calculate event values can be found in that section. Originally, when rdpmc support was enabled, any process (not just ones with an active perf event) could use the rdpmc instruction to access the counters. Starting with Linux 4.0, rdpmc support is only allowed if an event is currently enabled in a process's context. To restore the old behavior, write the value 2 to /sys/devices/cpu/rdpmc. perfevent ioctl calls Various ioctls act on perfeventopen() file descriptors: PERFEVENTIOCENABLE This enables the individual event or event group specified by the file descriptor argument. If the PERFIOCFLAGGROUP bit is set in the ioctl argument, then all events in a group are enabled, even if the event speci‐ fied is not the group leader (but see BUGS). PERFEVENTIOCDISABLE This disables the individual counter or event group specified by the file descriptor argument. Enabling or disabling the leader of a group enables or disables the entire group; that is, while the group leader is disabled, none of the counters in the group will count. Enabling or dis‐ abling a member of a group other than the leader affects only

that counter; disabling a non-leader stops that counter from counting but doesn't affect any other counter. If the PERFIOCFLAGGROUP bit is set in the ioctl argument, then all events in a group are disabled, even if the event spec‐ ified is not the group leader (but see BUGS). PERFEVENTIOCREFRESH

Non-inherited overflow counters can use this to enable a counter for a number of overflows specified by the argument, after which it is disabled. Subsequent calls of this ioctl add the argument value to the current count. An overflow notification with POLLIN set will happen on each overflow until the count reaches 0; when that happens a notification with POLLHUP set is sent and the event is disabled. Using an argument of 0 is considered undefined behavior. PERFEVENTIOCRESET Reset the event count specified by the file descriptor argument to zero. This resets only the counts; there is no way to reset the multiplexing timeenabled or timerunning values. If the PERFIOCFLAGGROUP bit is set in the ioctl argument, then all events in a group are reset, even if the event speci‐ fied is not the group leader (but see BUGS). PERFEVENTIOCPERIOD This updates the overflow period for the event. Since Linux 3.7 (on ARM) and Linux 3.14 (all other architec‐ tures), the new period takes effect immediately. On older ker‐ nels, the new period did not take effect until after the next overflow.

The argument is a pointer to a 64-bit value containing the desired new period. Prior to Linux 2.6.36, this ioctl always failed due to a bug in the kernel. PERFEVENTIOCSETOUTPUT This tells the kernel to report event notifications to the spec‐ ified file descriptor rather than the default one. The file descriptors must all be on the same CPU.

The argument specifies the desired file descriptor, or -1 if output should be ignored. PERFEVENTIOCSETFILTER (since Linux 2.6.33) This adds an ftrace filter to this event. The argument is a pointer to the desired ftrace filter. PERFEVENTIOCID (since Linux 3.12) This returns the event ID value for the given event file descriptor.

The argument is a pointer to a 64-bit unsigned integer to hold the result. PERFEVENTIOCSETBPF (since Linux 4.1) This allows attaching a Berkeley Packet Filter (BPF) program to an existing kprobe tracepoint event. You need CAPSYSADMIN privileges to use this ioctl. The argument is a BPF program file descriptor that was created by a previous bpf(2) system call. PERFEVENTIOCPAUSEOUTPUT (since Linux 4.7)

This allows pausing and resuming the event's ring-buffer. A

paused ring-buffer does not prevent generation of samples, but simply discards them. The discarded samples are considered lost, and cause a PERFRECORDLOST sample to be generated when possible. An overflow signal may still be triggered by the dis‐

carded sample even though the ring-buffer remains empty.

The argument is an unsigned 32-bit integer. A nonzero value

pauses the ring-buffer, while a zero value resumes the ring-buf‐ fer. PERFEVENTMODIFYATTRIBUTES (since Linux 4.17) This allows modifying an existing event without the overhead of closing and reopening a new event. Currently this is supported only for breakpoint events. The argument is a pointer to a perfeventattr structure con‐ taining the updated event settings. PERFEVENTIOCQUERYBPF (since Linux 4.16) This allows querying which Berkeley Packet Filter (BPF) programs are attached to an existing kprobe tracepoint. You can only attach one BPF program per event, but you can have multiple events attached to a tracepoint. Querying this value on one tracepoint event returns the id of all BPF programs in all events attached to the tracepoint. You need CAPSYSADMIN priv‐ ileges to use this ioctl. The argument is a pointer to a structure struct perfeventquerybpf { u32 idslen; u32 progcnt; u32 ids[0]; }; The idslen field indicates the number of ids that can fit in the provided ids array. The progcnt value is filled in by the kernel with the number of attached BPF programs. The ids array is filled with the id of each attached BPF program. If there are more programs than will fit in the array, then the kernel will return ENOSPC and idslen will indicate the number of pro‐ gram IDs that were successfully copied. Using prctl(2) A process can enable or disable all currently open event groups using the prctl(2) PRTASKPERFEVENTSENABLE and PRTASKPERFEVENTSDISABLE operations. This applies only to events created locally by the calling process. This does not apply to events created by other processes attached to the calling process or inherited events from a parent process. Only group leaders are enabled and disabled, not any other members of the groups. perfevent related configuration files Files in /proc/sys/kernel/ /proc/sys/kernel/perfeventparanoid The perfeventparanoid file can be set to restrict access to the performance counters.

2 allow only user-space measurements (default since Linux 4.6). 1 allow both kernel and user measurements (default before Linux 4.6).

0 allow access to CPU-specific data but not raw tracepoint samples.

-1 no restrictions. The existence of the perfeventparanoid file is the offi‐ cial method for determining if a kernel supports perfeventopen(). /proc/sys/kernel/perfeventmaxsamplerate This sets the maximum sample rate. Setting this too high can allow users to sample at a rate that impacts overall machine performance and potentially lock up the machine. The default value is 100000 (samples per second). /proc/sys/kernel/perfeventmaxstack This file sets the maximum depth of stack frame entries reported when generating a call trace. /proc/sys/kernel/perfeventmlockkb Maximum number of pages an unprivileged user can mlock(2). The default is 516 (kB). Files in /sys/bus/eventsource/devices/ Since Linux 2.6.34, the kernel supports having multiple PMUs avail‐ able for monitoring. Information on how to program these PMUs can be found under /sys/bus/eventsource/devices/. Each subdirectory corresponds to a different PMU. /sys/bus/eventsource/devices/*/type (since Linux 2.6.38) This contains an integer that can be used in the type field of perfeventattr to indicate that you wish to use this PMU. /sys/bus/eventsource/devices/cpu/rdpmc (since Linux 3.4)

If this file is 1, then direct user-space access to the per‐ formance counter registers is allowed via the rdpmc instruc‐ tion. This can be disabled by echoing 0 to the file. As of Linux 4.0 the behavior has changed, so that 1 now means only allow access to processes with active perf

events, with 2 indicating the old allow-anyone-access behav‐ ior. /sys/bus/eventsource/devices/*/format/ (since Linux 3.4)

This subdirectory contains information on the architecture- specific subfields available for programming the various config fields in the perfeventattr struct. The content of each file is the name of the config field, followed by a colon, followed by a series of integer bit ranges separated by commas. For example, the file event may

contain the value config1:1,6-10,44 which indicates that event is an attribute that occupies bits 1,6–10, and 44 of perfeventattr::config1. /sys/bus/eventsource/devices/*/events/ (since Linux 3.4) This subdirectory contains files with predefined events. The contents are strings describing the event settings expressed in terms of the fields found in the previously mentioned ./format/ directory. These are not necessarily complete lists of all events supported by a PMU, but usually a subset of events deemed useful or interesting. The content of each file is a list of attribute names sepa‐ rated by commas. Each entry has an optional value (either hex or decimal). If no value is specified, then it is

assumed to be a single-bit field with a value of 1. An example entry may look like this: event=0x2,inv,ldlat=3. /sys/bus/eventsource/devices/*/uevent This file is the standard kernel device interface for injecting hotplug events. /sys/bus/eventsource/devices/*/cpumask (since Linux 3.7)

The cpumask file contains a comma-separated list of integers that indicate a representative CPU number for each socket (package) on the motherboard. This is needed when setting up uncore or northbridge events, as those PMUs present

socket-wide events. RETURN VALUE

perfeventopen() returns the new file descriptor, or -1 if an error occurred (in which case, errno is set appropriately). ERRORS The errors returned by perfeventopen() can be inconsistent, and may vary across processor architectures and performance monitoring units. E2BIG Returned if the perfeventattr size value is too small (smaller than PERFATTRSIZEVER0), too big (larger than the page size), or larger than the kernel supports and the extra bytes are not zero. When E2BIG is returned, the perfeventattr size field is overwritten by the kernel to be the size of the structure it was expecting. EACCES Returned when the requested event requires CAPSYSADMIN permis‐ sions (or a more permissive perfevent paranoid setting). Some common cases where an unprivileged process may encounter this error: attaching to a process owned by a different user; moni‐ toring all processes on a given CPU (i.e., specifying the pid

argument as -1); and not setting excludekernel when the para‐ noid setting requires it. EBADF Returned if the groupfd file descriptor is not valid, or, if PERFFLAGPIDCGROUP is set, the cgroup file descriptor in pid is not valid. EBUSY (since Linux 4.1) Returned if another event already has exclusive access to the PMU. EFAULT Returned if the attr pointer points at an invalid memory address. EINVAL Returned if the specified event is invalid. There are many pos‐

sible reasons for this. A not-exhaustive list: samplefreq is higher than the maximum setting; the cpu to monitor does not exist; readformat is out of range; sampletype is out of range; the flags value is out of range; exclusive or pinned set and the event is not a group leader; the event config values are out of range or set reserved bits; the generic event selected is not supported; or there is not enough room to add the selected event. EMFILE Each opened event uses one file descriptor. If a large number

of events are opened, the per-process limit on the number of open file descriptors will be reached, and no more events can be created. ENODEV Returned when the event involves a feature not supported by the current CPU. ENOENT Returned if the type setting is not valid. This error is also returned for some unsupported generic events. ENOSPC Prior to Linux 3.3, if there was not enough room for the event, ENOSPC was returned. In Linux 3.3, this was changed to EINVAL. ENOSPC is still returned if you try to add more breakpoint events than supported by the hardware. ENOSYS Returned if PERFSAMPLESTACKUSER is set in sampletype and it is not supported by hardware. EOPNOTSUPP Returned if an event requiring a specific hardware feature is requested but there is no hardware support. This includes

requesting low-skid events if not supported, branch tracing if it is not available, sampling if no PMU interrupt is available, and branch stacks for software events. EOVERFLOW (since Linux 4.8) Returned if PERFSAMPLECALLCHAIN is requested and sam‐ plemaxstack is larger than the maximum specified in /proc/sys/kernel/perfeventmaxstack. EPERM Returned on many (but not all) architectures when an unsupported excludehv, excludeidle, excludeuser, or excludekernel set‐ ting is specified. It can also happen, as with EACCES, when the requested event requires CAPSYSADMIN permissions (or a more permissive perfevent paranoid setting). This includes setting a break‐ point on a kernel address, and (since Linux 3.13) setting a ker‐

nel function-trace tracepoint. ESRCH Returned if attempting to attach to a process that does not exist. VERSION perfeventopen() was introduced in Linux 2.6.31 but was called perfcounteropen(). It was renamed in Linux 2.6.32. CONFORMING TO

This perfeventopen() system call Linux-specific and should not be used in programs intended to be portable. NOTES Glibc does not provide a wrapper for this system call; call it using syscall(2). See the example below. The official way of knowing if perfeventopen() support is enabled is checking for the existence of the file /proc/sys/ker‐ nel/perfeventparanoid. BUGS The FSETOWNEX option to fcntl(2) is needed to properly get overflow signals in threads. This was introduced in Linux 2.6.32. Prior to Linux 2.6.33 (at least for x86), the kernel did not check if events could be scheduled together until read time. The same happens on all known kernels if the NMI watchdog is enabled. This means to see if a given set of events works you have to perfeventopen(), start, then read before you know for sure you can get valid measurements. Prior to Linux 2.6.34, event constraints were not enforced by the ker‐ nel. In that case, some events would silently return "0" if the kernel scheduled them in an improper counter slot. Prior to Linux 2.6.34, there was a bug when multiplexing where the wrong results could be returned. Kernels from Linux 2.6.35 to Linux 2.6.39 can quickly crash the kernel if "inherit" is enabled and many threads are started. Prior to Linux 2.6.35, PERFFORMATGROUP did not work with attached processes. There is a bug in the kernel code between Linux 2.6.36 and Linux 3.0 that ignores the "watermark" field and acts as if a wakeupevent was chosen if the union has a nonzero value in it. From Linux 2.6.31 to Linux 3.4, the PERFIOCFLAGGROUP ioctl argument was broken and would repeatedly operate on the event specified rather than iterating across all sibling events in a group. From Linux 3.4 to Linux 3.11, the mmap capusrrdpmc and capusrtime bits mapped to the same location. Code should migrate to the new capuserrdpmc and capusertime fields instead.

Always double-check your results! Various generalized events have had wrong values. For example, retired branches measured the wrong thing on AMD machines until Linux 2.6.35. EXAMPLE The following is a short example that measures the total instruction count of a call to printf(3).

#include

#include

#include

#include

#include

#include

#include static long perfeventopen(struct perfeventattr *hwevent, pidt pid, int cpu, int groupfd, unsigned long flags) { int ret; ret = syscall(NRperfeventopen, hwevent, pid, cpu, groupfd, flags); return ret; } int main(int argc, char **argv) { struct perfeventattr pe; long long count; int fd; memset(&pe, 0, sizeof(struct perfeventattr)); pe.type = PERFTYPEHARDWARE; pe.size = sizeof(struct perfeventattr); pe.config = PERFCOUNTHWINSTRUCTIONS; pe.disabled = 1; pe.excludekernel = 1; pe.excludehv = 1;

fd = perfeventopen(&pe, 0, -1, -1, 0);

if (fd == -1) {

fprintf(stderr, "Error opening leader %llx\n", pe.config); exit(EXITFAILURE); } ioctl(fd, PERFEVENTIOCRESET, 0); ioctl(fd, PERFEVENTIOCENABLE, 0); printf("Measuring instruction count for this printf\n"); ioctl(fd, PERFEVENTIOCDISABLE, 0); read(fd, &count, sizeof(long long));

printf("Used %lld instructions\n", count); close(fd); } SEE ALSO perf(1), fcntl(2), mmap(2), open(2), prctl(2), read(2) COLOPHON

This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can

be found at http://www.kernel.org/doc/man-pages/.

Linux 2018-02-02 PERFEVENTOPEN(2)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™