Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perf stat for part of program

Tags:

linux

perf

likwid

Is it possible with perf to collect hardware counter statistics for only part of a program's execution? If so, how?

likwid offers the feature of being able to define named regions, but it would be great if this was possible on systems with just perf installed.

Some previous questions have returned relevant answers, but there are still some shortcomings:

  • Using probe I get the same error and I'm using a slightly newer kernel (3.13). Are these fixes available in a newer version?
  • Using perf_event_open I would like to maintain the flexibility to define events on the command line. I also took a peek at the code for perf stat itself, but it seems it doesn't set things up by calling perf_event_open.
like image 527
user2548418 Avatar asked Oct 08 '14 22:10

user2548418


People also ask

What is perf record?

A perf record command was used to trace the block:block_rq_issue probe, which fires when a block device I/O request is issued (disk I/O). Options included -a to trace all CPUs, and -g to capture call graphs (stack traces). Trace data is written to a perf. data file, and tracing ended when Ctrl-C was hit.

What is perf in programming?

Userspace controlling utility, named perf , is accessed from the command line and provides a number of subcommands; it is capable of statistical profiling of the entire system (both kernel and userland code).

What is overhead in perf report?

Overhead: the percentage of overall samples collected in the corresponding function. Command: the command to which the samples belong. Shared object: the name of the image from where the samples came. Symbol: the symbol name which constitutes the sample, and the privilege level at which the sample was taken.

What can perf do?

The perf tool can be used to collect profiles on per-thread, per-process and per-cpu basis. There are several commands associated with sampling: record, report, annotate. You must first collect the samples using perf record. This generates an output file called perf.


2 Answers

Spawn a child process to run perf stat.
Attach perf stat to the parent.
Kill the child process from parent as and when required.

#include <unistd.h>
#include <stdio.h>
#include <signal.h>

int main()
{

    int pid= getpid();
    int cpid = fork();


    if( cpid == 0)
    {
        // child process .  Run your perf stat
        char buf[50];
        sprintf(buf, "perf stat -p %d   > stat.log 2>&1",pid);
        execl("/bin/sh", "sh", "-c", buf, NULL);

    }
    else
    {
        // set the child the leader of its process group
        setpgid(cpid, 0);

        //////////////////////////////////////////////
        // part of program you wanted to perf stat
        sleep(3);
        ////////////////////////////////////////////////


        ////////////////////////////////////////////////////////////////
        // stop perf stat by killing child process and all its descendants(sh, perf stat etc )
        kill(-cpid, SIGINT);
        ////////////////////////////////////////////////////////////////////


        // rest of the program
        sleep(2);
     }
}
like image 116
Mohamed Husain Avatar answered Oct 20 '22 09:10

Mohamed Husain


You could use libpfc or jevents both of which are Linux-compatible libraries that allow programming and reading of performance counters via rdpmc at arbitrary points in the userland program.

This won't help directly with your request to specify events on the command line, but you could back something together perhaps based on the ocperf.py code, or libpfm4.

like image 1
BeeOnRope Avatar answered Oct 20 '22 09:10

BeeOnRope