linux – 用于部分程序的perf stat

linux – 用于部分程序的perf stat,第1张

概述是否可以通过perf收集硬件计数器统计信息,仅用于程序执行的一部分?如果是这样,怎么样? likwid提供了能够定义命名区域的功能,但如果在安装了perf的系统上可以实现这一点,那将会很棒. 以前的一些问题已经回复了相关答案,但仍有一些缺点: > Using probe我得到了同样的错误,我使用的是一个稍新的内核(3.13).这些修补程序是否在较新版本中可用? > Using perf_event 是否可以通过perf收集硬件计数器统计信息,仅用于程序执行的一部分?如果是这样,怎么样?

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

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

> Using probe我得到了同样的错误,我使用的是一个稍新的内核(3.13).这些修补程序是否在较新版本中可用?
> Using perf_event_open我想保持在命令行上定义事件的灵活性.我还看了一眼perf stat itself的代码,但似乎没有通过调用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);     }}
总结

以上是内存溢出为你收集整理的linux – 用于部分程序的perf stat全部内容,希望文章能够帮你解决linux – 用于部分程序的perf stat所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1047596.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存