Content-type: text/html PERF_EVENT_OPEN

Section: Linux Programmer's Manual (2) Updated: 2012-08-21 Index Return to Main Contents


perf_event_open - setup performance monitoring SYNOPSIS

  1. include <linux/perf_event.h>
  2. include <linux/hw_breakpoint.h>

int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags);


Given a list of parameters perf_event_open() returns a file descriptor, a small, nonnegative integer for use in subsequent system calls (read(2), mmap(2), prctl(2), fcntl(2), etc.). A call to perf_event_open() creates a file descriptor that allows measuring 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 eventset is disabled it does not count or generate events 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).


The argument pid allows events to be attached to processes in various ways. If pid is 0 measurements happen on the current task, if pid is greater than 0 the process indicated by pid is measured, and if pid is less than 0 all processes are counted. The cpu argument allows measurements to be specific to a CPU. If cpu is greater than or equal to 0 measurements are restricted to the specified CPU; if cpu is -1 the events are measured on all CPUs. Note that the combination of pid == -1 and cpu == -1 is not valid. A pid > 0 and cpu == -1 setting measures per-process and follows that process to whatever CPU the process gets scheduled to. Per-process events can be created by any user. A pid == -1 and cpu >= 0 setting is per-CPU and measures all processes on the specified CPU. Per-CPU events need CAP_SYS_ADMIN privileges. The group_fd argument allows counter groups to be set up. A counter group has one counter which is the group leader. The leader is created first, with group_fd = -1 in the perf_event_open() call that creates it. The rest of the group members are created subsequently, with group_fd giving the fd of the group leader. (A single counter on its own is created with group_fd = -1 and is considered to be a group with only 1 member). A counter group is scheduled onto the CPU as a unit: it will only be put onto the CPU if all of the counters in the group can be put onto the CPU. This means that the values of the member counters can be meaningfully compared, added, divided (to get ratios), etc., with each other, since they have counted events for the same set of executed instructions. The flags argument takes one of the following values:

PERF_FLAG_FD_NO_GROUP This flag allows creating an event as part of an event group but having no group leader. It is unclear why this is useful. PERF_FLAG_FD_OUTPUT This flag re-routes the output from an event to the group leader. PERF_FLAG_PID_CGROUP (added in 2.6.39). This flag activates per-container system-wide monitoring. A container is an abstraction that isolates a set of resources for finer grain 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 monitor 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 only available for system-wide events and may therefore require extra permissions. The perf_event_attr structure is what is passed into the perf_event_open() syscall. It is large and has a complicated set of dependent fields.

struct perf_event_attr {

    __u32    type;      /* Type of event */
    __u32    size;      /* Size of attribute structure */
    __u64    config;    /* Type-specific configuration */

    union {
        __u64    sample_period;    /* Period of sampling */
        __u64    sample_freq;      /* Frequency of sampling */

    __u64    sample_type;    /* Specifies values included in sample */
    __u64    read_format;    /* 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      */
                 exclude_user   : 1, /* don't count user       */
                 exclude_kernel : 1, /* don't count kernel     */
                 exclude_hv     : 1, /* don't count hypervisor */
                 exclude_idle   : 1, /* don't count when idle  */
                 mmap           : 1, /* include mmap data      */
                 comm           : 1, /* include comm data      */
                 freq           : 1, /* use freq, not period   */
                 inherit_stat   : 1, /* per task counts        */
                 enable_on_exec : 1, /* next exec enables      */
                 task           : 1, /* trace fork/exit        */
                 watermark      : 1, /* wakeup_watermark       */
                 precise_ip     : 2, /* skid constraint        */
                 mmap_data      : 1, /* non-exec mmap data     */
                 sample_id_all  : 1, /* sample_type all events */
                 exclude_host   : 1, /* don't count in host    */
                 exclude_guest  : 1, /* don't count in guest   */
                 __reserved_1   : 43;

        union {
            __u32    wakeup_events;    /* wakeup every n events */
            __u32    wakeup_watermark; /* bytes before wakeup   */

        __u32    bp_type;     /* breakpoint type */

        union {
            __u64    bp_addr; /* breakpoint address */
            __u64    config1; /* extension of config */

        union {
            __u64    bp_len;  /* breakpoint length */
            __u64    config2; /* extension of config1 */
        __u64   branch_sample_type; /* enum branch_sample_type */

The fields of the attr structure are described in more detail below:

type This field specifies the overall event type. It has one of the following values: PERF_TYPE_HARDWARE This indicates one of the "generalized" hardware events provided by the kernel. See the config field definition for more details. PERF_TYPE_SOFTWARE This indicates one of the software-defined events provided by the kernel (even if no HW support available). PERF_TYPE_TRACEPOINT This indicates a tracepoint provided by the kernel tracepoint infrastructure. PERF_TYPE_HW_CACHE This indicates a hardware cache event. This has a special encoding, described in the config field definition. PERF_TYPE_RAW This indicates a "raw" implementation-specific event in the config field. PERF_TYPE_BREAKPOINT (Added in 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 As of 2.6.39 perf_event can support multiple PMUs. Each PMU is uniquely identified by its type fields. The value for this field is exported by the kernel in the sysfs filesystem. There is a subdir per PMU instance under /sys/devices. In each subdir, there is a type file. The content of this file is the type value for the PMU. For instance, /sys/devices/cpu/type contains the value for the core CPU PMU which is usually 4. size The size of the perf_event_attr structure for forward/backward compatibility. Set this using sizeof(struct perf_event_attr) to allow the kernel to see the struct size at the time of compilation. The related define PERF_ATTR_SIZE_VER0 is set to 64; this was the size of the first published struct. PERF_ATTR_SIZE_VER1 is 72, corresponding to the addition of breakpoints in 2.6.33. PERF_ATTR_SIZE_VER2 is 80 corresponding to the addition of branch sampling in 3.4.

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.

The most significant bit (bit 63) of config signifies cpu specific (raw) counter configuration data; if the most significant bit is unset, the next 7 bits are an event type and the rest of the bits are the event identifier.

There are various ways to set the config field that are dependent on the value of the previously described type field. What follows are various possible settings for config separated out by type.

If type is PERF_TYPE_HARDWARE we are measuring one of the generalized hardware CPU events. Not all of these are available on all platforms. Set config to one of the following: PERF_COUNT_HW_CPU_CYCLES Total cycles. Be wary of what happens during cpu frequency scaling PERF_COUNT_HW_INSTRUCTIONS Retired instructions. Be careful, these can be affected by various issues, most notably hardware interrupt counts PERF_COUNT_HW_CACHE_REFERENCES Usually Last Level Cache. Unclear if this includes prefetches and coherency messages. PERF_COUNT_HW_CACHE_MISSES Usually Last Level Cache. Unclear if this includes prefetches and coherency messages. PERF_COUNT_HW_BRANCH_INSTRUCTIONS Retired branch instructions. Prior to 2.6.34 this used the wrong event on AMD processors. PERF_COUNT_HW_BRANCH_MISSES Mispredicted branch instructions. PERF_COUNT_HW_BUS_CYCLES Bus cycles, which can be different than total cycles. PERF_COUNT_HW_STALLED_CYCLES_FRONTEND (Added in 3.0) Stalled cycles during issue. PERF_COUNT_HW_STALLED_CYCLES_BACKEND (Added in 3.0) Stalled cycles during retirement. PERF_COUNT_HW_REF_CPU_CYCLES (Added in 3.3) Total cycles; not affected by CPU frequency scaling. If type is PERF_TYPE_SOFTWARE we are measuring software events provided by the kernel. Set config to one of the following: PERF_COUNT_SW_CPU_CLOCK This reports the CPU clock, a high-resolution per-cpu timer. PERF_COUNT_SW_TASK_CLOCK This reports a clock count specific to the task that is running. PERF_COUNT_SW_PAGE_FAULTS This reports the number of page faults. PERF_COUNT_SW_CONTEXT_SWITCHES This counts context switches. Until 2.6.34 these were all reported as user-space events, after that they are reported as happening in the kernel. PERF_COUNT_SW_CPU_MIGRATIONS This reports the number of times the process has migrated to a new CPU. PERF_COUNT_SW_PAGE_FAULTS_MIN This counts the number of minor page faults. These did not require disk I/O to handle. PERF_COUNT_SW_PAGE_FAULTS_MAJ This counts the number of major page faults. These required disk I/O to handle. PERF_COUNT_SW_ALIGNMENT_FAULTS (Added in 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 only happens on some architectures (never on x86). PERF_COUNT_SW_EMULATION_FAULTS (Added in 2.6.33) This counts the number of emulation faults. The kernel sometimes traps on unimplemented instructions and emulates them for userspace. This can negatively impact performance. If type is PERF_TYPE_TRACEPOINT 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 PERF_TYPE_HW_CACHE then we are measuring a hardware CPU cache event. To calculate the appropriate config value use the following equation: (perf_hw_cache_id) | (perf_hw_cache_op_id << 8) | (perf_hw_cache_op_result_id << 16) where perf_hw_cache_id is one of: PERF_COUNT_HW_CACHE_L1D for measuring Level 1 Data Cache PERF_COUNT_HW_CACHE_L1I for measuring Level 1 Instruction Cache PERF_COUNT_HW_CACHE_LL for measuring Last-Level Cache PERF_COUNT_HW_CACHE_DTLB for measuring the Data TLB PERF_COUNT_HW_CACHE_ITLB for measuring the Instruction TLB PERF_COUNT_HW_CACHE_BPU for measuring the branch prediction unit PERF_COUNT_HW_CACHE_NODE (Added in 3.0) for measuring local memory accesses and perf_hw_cache_op_id is one of

PERF_COUNT_HW_CACHE_OP_READ for read accesses PERF_COUNT_HW_CACHE_OP_WRITE for write accesses PERF_COUNT_HW_CACHE_OP_PREFETCH for prefetch accesses and perf_hw_cache_op_result_id is one of

PERF_COUNT_HW_CACHE_RESULT_ACCESS to measure accesses PERF_COUNT_HW_CACHE_RESULT_MISS to measure misses If type is PERF_TYPE_RAW 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 architectural manuals to the raw hex value perf_event expects in this field. If type is PERF_TYPE_BREAKPOINT then leave config set to zero. Its parameters are set in other places. sample_period, sample_freq A "sampling" counter is one that generates an interrupt every N events, where N is given by sample_period. A sampling counter has sample_period > 0. The sample_type field controls what data is recorded on each interrupt. sample_freq 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.

sample_type The various bits in this field specify which values to include in the overflow packets. 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 overflow packets as documented in the MMAP Layout subsection below; it is not the enum perf_event_sample_format order. PERF_SAMPLE_IP instruction pointer PERF_SAMPLE_TID thread id PERF_SAMPLE_TIME time PERF_SAMPLE_ADDR address PERF_SAMPLE_READ [To be documented] PERF_SAMPLE_CALLCHAIN [To be documented] PERF_SAMPLE_ID [To be documented] PERF_SAMPLE_CPU [To be documented] PERF_SAMPLE_PERIOD [To be documented] PERF_SAMPLE_STREAM_ID [To be documented] PERF_SAMPLE_RAW [To be documented] PERF_SAMPLE_BRANCH_STACK (Added in 3.4) [To be documented] read_format This field specifies the format of the data returned by read(2) on a perf_event file descriptor. PERF_FORMAT_TOTAL_TIME_ENABLED Adds the 64-bit "time_enabled" field. This can be used to calculate estimated totals if the PMU is overcommitted and multiplexing is happening. PERF_FORMAT_TOTAL_TIME_RUNNING Adds the 64-bit "time_running" field. This can be used to calculate estimated totals if the PMU is overcommitted and multiplexing is happening. PERF_FORMAT_ID Adds a 64-bit unique value that corresponds to the event-group. PERF_FORMAT_GROUP Allows all counter values in an event-group to be read with one read. disabled The disabled bit specifies whether the counter starts out disabled or enabled. If disabled, the event can later be enabled by ioctl(2), prctl(2), or enable_on_exec. inherit The inherit bit specifies that this counter should count events of child tasks as well as the task specified. This only applies to new children, not to any existing children at the time the counter is created (nor to any new children of existing children). Inherit does not work for some combinations of read_formats, such as PERF_FORMAT_GROUP.

pinned The pinned bit specifies that the counter should always be on the CPU if at all possible. It only applies to hardware counters 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 subsequently 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. exclude_user If this bit is set the count excludes events that happen in user-space. exclude_kernel If this bit is set the count excludes events that happen in kernel-space. exclude_hv 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. exclude_idle If set don't count when the CPU is idle. mmap The mmap bit enables recording of extra information to a mmap'd ring-buffer. This is described below in subsection MMAP Layout. comm The comm bit enables tracking of process command name as modified by the exec(2) and prctl(PR_SET_NAME) system calls. Unfortunately for tools, there is no way to distinguish one system call vs. the other. freq If this bit is set then sample_frequency not sample_period is used when setting up the sampling interval. inherit_stat This bit enables Per task counts? It is unclear how this is different from the inherit field. enable_on_exec 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 a sampling interrupt happen when we cross the wakeup_watermark boundary. precise_ip (Added in 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 values of this are the following:

0 - SAMPLE_IP can have arbitrary skid 1 - SAMPLE_IP must have constant skid 2 - SAMPLE_IP requested to have 0 skid 3 - SAMPLE_IP must have 0 skid. See also PERF_RECORD_MISC_EXACT_IP. mmap_data (Added in 2.6.36) Include mmap events in the ring_buffer. sample_id_all (Added in 2.6.38) If set then all sample ID info (TID, TIME, ID, CPU, STREAM_ID) will be provided. exclude_host (Added in 3.2) Do not measure time spent in VM host exclude_guest (Added in 3.2) Do not measure time spent in VM guest wakeup_events, wakeup_watermark This union sets how many events (wakeup_events) or bytes (wakeup_watermark) happen before an overflow signal happens. Which one is used is selected by the watermark bitflag. bp_type (Added in 2.6.33) This chooses the breakpoint type. It is one of: HW_BREAKPOINT_EMPTY no breakpoint HW_BREAKPOINT_R count when we read the memory location HW_BREAKPOINT_W count when we write the memory location HW_BREAKPOINT_RW count when we read or write the memory location HW_BREAKPOINT_X count when we execute code at the memory location HW_BREAKPOINT_INVALID invalid breakpoint? bp_addr (added in 2.6.33) bp_addr address of the breakpoint. config1 (added in 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 OFFCORE_EVENTS on Nehalem/Westmere/SandyBridge use this field on 3.3 and later kernels. bp_len (added in 2.6.33) bp_len is the length of the breakpoint being measured if type is PERF_TYPE_BREAKPOINT. Options are HW_BREAKPOINT_LEN_1, HW_BREAKPOINT_LEN_2, HW_BREAKPOINT_LEN_4, HW_BREAKPOINT_LEN_8. For an execution breakpoint set this to sizeof(long). config2 (added in 2.6.39) config2 is a further extension of the config1 field.

branch_sample_type (added in 3.4) This is used with the CPUs hardware branch sampling, if available. It can have one of the following values: PERF_SAMPLE_BRANCH_USER Branch target is in user space PERF_SAMPLE_BRANCH_KERNEL Branch target is in kernel space PERF_SAMPLE_BRANCH_HV Branch target is in hypervisor PERF_SAMPLE_BRANCH_ANY Any branch type. PERF_SAMPLE_BRANCH_ANY_CALL Any call branch PERF_SAMPLE_BRANCH_ANY_RETURN Any return branch PERF_SAMPLE_BRANCH_IND_CALL Indirect calls PERF_SAMPLE_BRANCH_PLM_ALL User, kernel, and hv

MMAP Layout

When using perf_event is sampled mode, asynchronous events (like counter overflow or PROT_EXEC 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 meta-data page (struct perf_event_mmap_page) that contains various bits of information such as where the ring-buffer head is.

There is a bug previous to 2.6.39 where you have to allocate a mmap ring buffer when sampling even if you do not plan to access it.

The structure of the first meta-data mmap page is as follows:

struct perf_event_mmap_page {

    __u32 version;          /* version number of this structure   */
    __u32 compat_version;   /* lowest version this is compat with */
    __u32 lock;             /* seqlock for synchronization        */
    __u32 index;            /* hardware counter identifier        */
    __s64 offset;           /* add to hardware counter value      */
    __u64 time_enabled;     /* time event active                  */
    __u64 time_running;     /* time event on CPU                  */
    union {
        __u64   capabilities; 
        __u64   cap_usr_time  : 1, 
                cap_usr_rdpmc : 1,
    __u16   pmc_width;
    __u16   time_shift;
    __u32   time_mult;
    __u64   time_offset;
    __u64   __reserved[120];   /* Pad to 1k                */
    __u64   data_head;         /* head in the data section */
    __u64   data_tail;         /* user-space written tail  */

} The following looks at the fields in the perf_event_mmap_page structure in more detail.

version Version number of this structure. compat_version The lowest version this is compatible with. lock A seqlock for synchronization. index; A unique hardware counter identifier. offset Add this to hardware counter value?? time_enabled Time the event was active. time_running Time the event was running. cap_usr_time User time capability cap_usr_rdpmc 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, time_mult, time_shift, idx, width; u64 count, enabled, running; u64 cyc, time_offset; s64 pmc = 0;

do {

    seq = pc->lock;
    enabled = pc->time_enabled;
    running = pc->time_running;
    if (pc->cap_usr_time && enabled != running) {
        cyc = rdtsc();
        time_offset = pc->time_offset;
        time_mult   = pc->time_mult;
        time_shift  = pc->time_shift;
    idx = pc->index;
    count = pc->offset;
    if (pc->cap_usr_rdpmc && idx) {
        width = pc->pmc_width;
        pmc = rdpmc(idx - 1);

} while (pc->lock != seq);

pmc_width If cap_usr_rdpmc 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 - pmc_width; pmc >>= 64 - pmc_width; // signed shift right count += pmc; time_shift, time_mult, time_offset If cap_usr_time these fields can be used to compute the time delta since time_enabled (in ns) using rdtsc or similar.

    u64 quot, rem;
    u64 delta;
    quot = (cyc >> time_shift);
    rem = cyc & ((1 << time_shift) - 1);
    delta = time_offset + quot * time_mult +
            ((rem * time_mult) >> time_shift);

Where time_offset,time_mult,time_shift 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;

data_head This points to the head of the data section. On SMP capable platforms after reading the data_head value user-space should issue an rmb(). data_tail; When the mapping is PROT_WRITE the data_tail value should be written by userspace to reflect the last read data. In this case the kernel will not over-write unread data. The following 2^n ring-buffer pages have the layout described below.

If perf_event_attr.sample_id_all is set then all event types will have the sample_type selected fields related to where/when (identity) an event took place (TID, TIME, ID, CPU, STREAM_ID) described in PERF_RECORD_SAMPLE below, it will be stashed just after the perf_event_header and the fields already present for the existing fields, i.e. at the end of the payload. That way a newer file will be supported by older perf tools, with these new optional fields being ignored.

The mmap values start with a header:

struct perf_event_header {

    __u32   type;
    __u16   misc;
    __u16   size;

}; Below we describe the perf_event_header fields in more detail.

type The type value is one of the below. The values in the corresponding record (that follows the header) depend on the type selected as shown.

PERF_RECORD_MMAP The MMAP events record the PROT_EXEC mappings so that we can correlate userspace IPs to code. They have the following structure:

struct {

    struct perf_event_header header;
    u32    pid, tid;
    u64    addr;
    u64    len;
    u64    pgoff;
    char   filename[]; 


PERF_RECORD_LOST This record indicates when events are lost.

struct {

    struct perf_event_header header;
    u64 id;
    u64 lost; 

}; PERF_RECORD_COMM This record indicates a change in the process name.

struct {

    struct perf_event_header header;
    u32 pid, tid;
    char comm[]; 

}; PERF_RECORD_EXIT This record indicates a process exit event.

struct {

    struct perf_event_header header;
    u32 pid, ppid;
    u32 tid, ptid;
    u64 time; 

}; PERF_RECORD_THROTTLE, PERF_RECORD_UNTHROTTLE This record indicates a throttle/unthrottle event.

struct {

    struct perf_event_header header;
    u64 time;
    u64 id;
    u64 stream_id; 

}; PERF_RECORD_FORK This record indicates a fork event.

struct {

    struct perf_event_header header;
    u32 pid, ppid;
    u32 tid, ptid;
    u64 time; 

}; PERF_RECORD_READ This record indicates a read event.

struct {

    struct perf_event_header header;
    u32 pid, tid;
    struct read_format values; 

}; PERF_RECORD_SAMPLE This record indicates a sample.

struct {

    struct perf_event_header header;
    u64   ip;          /* if PERF_SAMPLE_IP      */
    u32   pid, tid;    /* if PERF_SAMPLE_TID     */
    u64   time;        /* if PERF_SAMPLE_TIME    */
    u64   addr;        /* if PERF_SAMPLE_ADDR    */
    u64   id;          /* if PERF_SAMPLE_ID      */
    u64   stream_id; /* if PERF_SAMPLE_STREAM_ID */
    u32   cpu, res;    /* if PERF_SAMPLE_CPU     */
    u64   period;      /* if PERF_SAMPLE_PERIOD  */
    struct read_format v; /* if PERF_SAMPLE_READ */
    u64   nr;        /* if PERF_SAMPLE_CALLCHAIN */
    u64   ips[nr];   /* if PERF_SAMPLE_CALLCHAIN */
    u32   size;        /* if PERF_SAMPLE_RAW     */
    char  data[size];  /* if PERF_SAMPLE_RAW     */
    u64   from;   /* if PERF_SAMPLE_BRANCH_STACK */
    u64   to;     /* if PERF_SAMPLE_BRANCH_STACK */
    u64   flags;  /* if PERF_SAMPLE_BRANCH_STACK */
    u64   lbr[nr];/* if PERF_SAMPLE_BRANCH_STACK */

}; The 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.

misc The misc field is one of the following: PERF_RECORD_MISC_CPUMODE_MASK [To be documented] PERF_RECORD_MISC_CPUMODE_UNKNOWN [To be documented] PERF_RECORD_MISC_KERNEL [To be documented] PERF_RECORD_MISC_USER [To be documented] PERF_RECORD_MISC_HYPERVISOR [To be documented] PERF_RECORD_MISC_GUEST_KERNEL [To be documented] PERF_RECORD_MISC_GUEST_USER [To be documented] PERF_RECORD_MISC_EXACT_IP This indicates that the content of PERF_SAMPLE_IP points to the actual instruction that triggered the event. See also perf_event_attr.precise_ip. size This indicates the size of the record.

Signal Overflow

Counters can be set to signal when a threshold is crossed. This is set up using traditional poll(2), select(2), epoll(2) and fcntl(2) syscalls.

Normally a notification is generated for every page filled, however one can additionally set perf_event_attr.wakeup_events to generate one every so many counter overflow events.

Reading Results

Once a perf_event 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 read_format 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, an error is returned (ENOSPC).

Here is the layout of the data returned by a read.

If PERF_FORMAT_GROUP was specified to allow reading all events in a group at once:

struct {

    u64 nr;             /* The number of events */
    u64 time_enabled;   /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
    u64 time_running;   /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
    struct {
        u64 value;      /* The value of the event. */
        u64 id;         /* if PERF_FORMAT_ID */
    } values[nr];

}; If PERF_FORMAT_GROUP was not specified the the read values look as following:

struct {

    u64 value;         /* The value of the event. */
    u64 time_enabled;  /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
    u64 time_running;  /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
    u64 id;            /* if PERF_FORMAT_ID */

}; The values read are described in more detail below.

nr The number of events in this file descriptor. Only available if PERF_FORMAT_GROUP was specified. time_enabled, time_running Total time the event was enabled and running. Normally these are the same. If more events are started than available counter slots on the PMU, then multiplexing happens and events only run part of the time. In that case the time_enabled 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 there if PERF_FORMAT_ID was specified in read_format.

rdpmc instruction

Starting with 3.4 on x86 you can use the rdpmc instruction to get low-latency reads without having to enter the kernel.

perf_event ioctl calls

Various ioctls act on perf_event file descriptors

PERF_EVENT_IOC_ENABLE Enables an individual counter or counter group. PERF_EVENT_IOC_DISABLE Disables an individual counter or counter group. 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 disabling a member of a group other than the leader only affects that counter - disabling a non-leader stops that counter from counting but doesn't affect any other counter.

PERF_EVENT_IOC_REFRESH Non-inherited overflow counters can use this to enable a counter for 'nr' events, after which it gets disabled again. I think the goal of IOC_REFRESH is not to reload the period but simply to adjust the number of events before the next notifications. PERF_EVENT_IOC_RESET Reset the event count to zero. This only resets the counts; there is no way to reset the multiplexing time_enabled or time_running

 values. When sent to a group leader, only the leader is reset (child events are not).

PERF_EVENT_IOC_PERIOD IOC_PERIOD is the command to update the period; it does not update the current period but instead defers until next. PERF_EVENT_IOC_SET_OUTPUT This tells the kernel to report event notifications to the specified file descriptor rather than the default one. The file descriptors must all be on the same CPU. PERF_EVENT_IOC_SET_FILTER (Added in 2.6.33) This adds a ftrace filter to this event.

Using prctl

A process can enable or disable all the counter groups that are attached to it using prctl. This applies to all counters on the current process, whether created by this process or by another, and does not affect any counters that this process has created on other processes. It only enables or disables the group leaders, not any other members in the groups. prctl(PR_TASK_PERF_EVENTS_ENABLE) prctl(PR_TASK_PERF_EVENTS_DISABLE)


The /proc/sys/kernel/perf_event_paranoid file can be set to restrict access to the performance counters. 2 means no measurements allowed, 1 means normal counter access 0 means you can access CPU-specific data, and -1 means no restrictions.

The existence of the perf_event_paranoid file is the official method for determining if a kernel supports perf_event.


perf_event_open() returns the new file descriptor, or -1 if an error occurred (in which case, errno is set appropriately). ERRORS

EINVAL Returned if the specified event is not available. ENOSPC Prior to 3.3 if there was no counter room ENOSPC was returned. Linus did not like this, and this was changed to EINVAL. ENOSPC is still returned if you try to read results into too small of a buffer.


perf_event_open() was introduced in 2.6.31 but was called perf_counter_open(). It was renamed in 2.6.32.


This call is specific to Linux and should not be used in programs intended to be portable.


The official way of knowing if perf_event support is enabled is checking for the existence of the file /proc/sys/kernel/perf_event_paranoid


The F_SETOWN_EX option to fcntl(2) is needed to properly get overflow signals in threads. This was introduced in 2.6.32.

Prior to 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 eventset works you have to perf_event_open(), start, then read before you know for sure you can get value measurements.

Prior to 2.6.34 event constraints were not enforced by the kernel. In that case, some events would silently return "0" if the kernel scheduled them in an improper counter slot.

Prior to 2.6.34 there was a bug when multiplexing where the wrong results could be returned.

Kernels from 2.6.35 to 2.6.39 can quickly crash the kernel if "inherit" is enabled and many threads are started.

Prior to 2.6.35 PERF_FORMAT_GROUP did not work with attached processes.

In older 2.6 versions refreshing an event group leader refreshed all siblings, and refreshing with a parameter of 0 enabled infinite refresh. This behavior is unsupported and should not be relied on.

There is a bug in the kernel code between 2.6.36 and 3.0 that ignores the "watermark" field and acts as if a wakeup_event was chosen if the union has a non-zero value in it.

Always double-check your results! Various generalized events have had wrong values. For example, retired branches measured the wrong thing on AMD machines until 2.6.35.


The following is a short example that measures the total instruction count of a call to printf().

  1. include <stdlib.h>
  2. include <stdio.h>
  3. include <unistd.h>
  4. include <string.h>
  5. include <sys/ioctl.h>
  6. include <linux/perf_event.h>
  7. include <asm/unistd.h>

long perf_event_open( struct perf_event_attr *hw_event, pid_t pid,

                      int cpu, int group_fd, unsigned long flags ) 


    int ret;

    ret = syscall( __NR_perf_event_open, hw_event, pid, cpu, 
                   group_fd, flags );
    return ret;


int main(int argc, char **argv) {

    struct perf_event_attr pe;
    long long count;
    int fd;

    memset(&pe, 0, sizeof(struct perf_event_attr));
    pe.type = PERF_TYPE_HARDWARE;
    pe.size = sizeof(struct perf_event_attr);
    pe.disabled = 1;
    pe.exclude_kernel = 1;
    pe.exclude_hv = 1;

    fd=perf_event_open(&pe, 0, -1, -1, 0);
    if (fd < 0) {
       fprintf(stderr, "Error opening leader %llx\n", pe.config);

    ioctl(fd, PERF_EVENT_IOC_RESET, 0);
    ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);

    printf("Measuring instruction count for this printf\n");

    ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
    read(fd, &count, sizeof(long long));

    printf("Used %lld instructions\n",count);




fcntl(2), mmap(2), open(2), prctl(2), read(2)


NAME SYNOPSIS DESCRIPTION Arguments MMAP Layout Signal Overflow Reading Results rdpmc instruction perf_event ioctl calls Using prctl /proc/sys/kernel/perf_event_paranoid RETURN VALUE ERRORS VERSION CONFORMING TO NOTES BUGS EXAMPLE SEE ALSO This document was created by man2html, using the manual pages. Time: 22:04:13 GMT, August 21, 2012