perf stat for part of program

↘锁芯ラ 提交于 2019-11-29 04:32:02
Mohamed Husain

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);
     }
}

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!