7

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

likwid 提供了能够定义命名区域的功能,但如果这在只安装了 perf 的系统上是可能的,那就太好了。

之前的一些问题已经返回了相关答案,但还是存在一些不足:

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

2 回答 2

3

生成一个子进程来运行 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);
     }
}
于 2018-08-04T21:11:54.383 回答
1

您可以使用libpfcjevents,它们都是与 Linux 兼容的库,允许通过rdpmc用户态程序中的任意点编程和读取性能计数器。

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

于 2018-08-07T03:26:21.347 回答