Will PERF_EVENT_IOC_RESET affect other applications which are using perf_event_open?

  arm, c++, linux, perf

I am using perf_event_open to count the number of CPU cycles it takes to execute portions of my application on an arm processor in linux. Using the man page, I put together a few utility functions.

// Initialize perf's collection of the CPU cycles hardware counter.
// You shall call this before all other perf_* functions.
int perf_init(int & perf_fd)
{
    struct perf_event_attr pe;
    memset(&pe, 0, sizeof(struct perf_event_attr));
    pe.type           = PERF_TYPE_HARDWARE;
    pe.size           = sizeof(struct perf_event_attr);
    pe.config         = PERF_COUNT_HW_CPU_CYCLES;
    pe.disabled       = 1;
    pe.exclude_kernel = 1;
    pe.exclude_hv     = 1;

    // Count cycles for this process (0) on all CPUs (-1) with no group (-1) and no flags (0)
    perf_fd = syscall(__NR_perf_event_open, &pe, 0, -1, -1, 0);

    if (perf_fd == -1)
    {
        fprintf(stderr, "[ERROR] Failed to open perf event, %dn", errno);
        exit(EXIT_FAILURE);
    }
    return perf_fd;
}

// Reset the CPU cycles counter.
// Call this immediately before the code to be profiled.
void perf_reset(int perf_fd)
{
    ioctl(perf_fd, PERF_EVENT_IOC_RESET, 0);
    ioctl(perf_fd, PERF_EVENT_IOC_ENABLE, 0);
}

// Read the CPU cycles counter.
// Stores the cycle count since the last call to perf_reset in count.
void perf_read(int perf_fd, long long & count)
{
    ioctl(perf_fd, PERF_EVENT_IOC_DISABLE, 0);
    if (read(perf_fd, &count, sizeof(long long)) == -1)
    {
        fprintf(stderr, "[ERROR] Failed to read perf counter, %dn", errno);
        exit(EXIT_FAILURE);
    }
}

// Cleanup perf's collection of the CPU cycles hardware counter.
// You shall call this after all other perf_* functions.
// CLOSES FILE DESCRIPTOR.
void perf_cleanup(int perf_fd)
{
    ioctl(perf_fd, PERF_EVENT_IOC_DISABLE, 0);
    close(perf_fd);
}

There will be other applications running simultaneously which also use perf_event_open to monitor PERF_COUNT_HW_CPU_CYCLES. My question is, do I need to be concerned that issuing PERF_EVENT_IOC_RESET could interfere with these other applications?

The man page states:

PERF_EVENT_IOC_RESET
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 time_enabled or
time_running values.

I can’t decipher from this exactly what the ioctl is accomplishing and whether I should be concerned. I also haven’t been able to find any other information addressing this concern.

(Note 1: I know there are more user-friendly means of accessing performance counters which probably handle any real concerns, but I don’t have access to those in this case.
Note 2: I’m using C++, but I presume the answer is not different for C.)

Source: Windows Questions C++

LEAVE A COMMENT