是否可以通过perf收集硬件计数器统计信息,仅用于程序执行的一部分?如果是这样,怎么样?
likwid提供了能够定义命名区域的功能,但如果只在安装了perf的系统上实现这一点,那将会很棒.
以前的一些问题已经返回相关答案,但仍有一些缺点:
小智 11
生成一个子进程来运行 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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1106 次 |
| 最近记录: |