使用perf探针在执行特定函数期间监测性能统计数据

5
我将尝试使用Linux perf工具监视特定函数的性能统计信息。
我正在按照https://perf.wiki.kernel.org/index.php/Jolsa_Features_Togle_Event#Example_-_using_u.28ret.29probes中提供的说明进行操作。
我尝试获取一个简单C程序的指令计数。(如下所示)
1)我的简单C代码:
#include<stdio.h>

int sum=0;
int i=0;

void func(void)
{
   for(i=0;i<100;i++)
   {
     sum=sum+i;
   }
}

int main(void)
{
   func();
   return 0;
}

2) 编译和添加探针

root@sunimal-laptop:/home/sunimal/temp# gcc -o ex source.c 
root@sunimal-laptop:/home/sunimal/temp# perf probe -x ./ex entry=func
Added new event:
  probe_ex:entry       (on 0x4ed)

You can now use it in all perf tools, such as:

        perf record -e probe_ex:entry -aR sleep 1

root@sunimal-laptop:/home/sunimal/temp# perf probe -x ./ex exit=func%return
Added new event:
  probe_ex:exit        (on 0x4ed%return)

You can now use it in all perf tools, such as:

        perf record -e probe_ex:exit -aR sleep 1

3) 尝试使用perf stat测量func()函数内的指令计数。这会导致错误。

root@sunimal-laptop:/home/sunimal/temp# perf stat -e instructions:u,probe_ex:entry/on=instructions/,probe_ex:exit/off=instructions/ ./ex
invalid or unsupported event: 'instructions:u,probe_ex:entry/on=instructions/,probe_ex:exit/off=instructions/'
Run 'perf list' for a list of valid events

请问我哪里做错了?

[我正在使用Linux内核3.11.0-12-generic]

1个回答

3
我认为你所遵循的指令还未包含在主线Linux内核中。因此,perf告诉你这些事件不受支持:perf不知道本页提到的“切换”机制。
我可以看到两个解决方法:
1. 如果你可以访问要分析的源代码,则可以直接从源代码使用perf_event_open系统调用,在函数进入和退出时开始和停止计数。
2. 克隆jolsa存储库git clone https://kernel.googlesource.com/pub/scm/linux/kernel/git/jolsa/perf,切换到core_toggle分支git co remotes/origin/perf/core_toggle,然后编译并运行具有此支持的内核。
关于第二种方法,我对内核版本和开发一点也不熟悉,我认为这个解决方案可能相当复杂,难以使用和维护。也许你应该在perf用户邮件列表上询问是否有将切换功能集成到主线内核的计划。

Manuel,在默认的perf_event_open中有一个切换开关:http://man7.org/linux/man-pages/man2/perf_event_open.2.html“perf_event ioctl calls”-PERF_EVENT_IOC_ENABLE/PERF_EVENT_IOC_DISABLE;但是它们只能与从perf_event_open获取的fd一起使用... - osgx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接