繁体   English   中英

程序部分的perf stat

[英]perf stat for part of program

是否可以通过perf收集硬件计数器统计信息,仅用于程序执行的一部分? 如果是这样,怎么样?

likwid提供了能够定义命名区域的功能,但如果只在安装了perf的系统上实现这一点,那将会很棒。

以前的一些问题已经返回相关答案,但仍有一些缺点:

  • 使用探针我得到相同的错误,我使用的是稍微更新的内核(3.13)。 这些修补程序是否在较新版本中可用?
  • 使用perf_event_open我想保持在命令行上定义事件的灵活性。 我还看了一下perf stat 本身的代码,但似乎没有通过调用perf_event_open进行设置。

生成子进程以运行perf stat。
perf stat附加到父级。
在需要时从父级杀死子进程。

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

您可以使用libpfcjevents ,这两个库都是兼容Linux的库,允许通过rdpmc在userland程序中的任意点编程和读取性能计数器。

这不会直接帮助您在命令行上指定事件的请求,但您可以根据ocperf.py代码或libpfm4将某些内容放在一起。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM