部分程序的性能统计 [英] perf stat for part of program

查看:69
本文介绍了部分程序的性能统计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使用perf收集仅针对程序执行部分的硬件计数器统计信息?如果可以,怎么办?

Is it possible with perf to collect hardware counter statistics for only part of a program's execution? If so, how?

likwid提供了能够定义命名区域的功能,但是如果在仅安装了perf的系统上可以做到这一点,那就太好了.

likwid offers the feature of being able to define named regions, but it would be great if this was possible on systems with just perf installed.

以前的一些问题已经返回了相关的答案,但是仍然存在一些缺点:

Some previous questions have returned relevant answers, but there are still some shortcomings:

  • 使用探针我遇到了相同的错误,并且我使用的内核稍新(3.13).这些修复程序是否有更新的版本?
  • 使用perf_event_open 我想保持在命令行上定义事件的灵活性.我还查看了perf stat
  • Using probe I get the same error and I'm using a slightly newer kernel (3.13). Are these fixes available in a newer version?
  • Using perf_event_open I would like to maintain the flexibility to define events on the command line. I also took a peek at the code for perf stat itself, but it seems it doesn't set things up by calling perf_event_open.

推荐答案

产生一个子进程以运行perf stat.
perf stat附加到父级.
在需要时从父进程中终止子进程.

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

这篇关于部分程序的性能统计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆