使用 perf 探针监视特定功能期间的性能统计信息

Sun*_*ake 5 linux linux-kernel perf

我正在尝试使用 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;
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

有人能指出我哪里做错了吗?

[我使用的是 linux 内核 3.11.0-12-generic]

Man*_*lva 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,然后使用此支持编译并运行内核。

关于2,我对内核版本和开发完全不熟悉,我认为这个解决方案使用和维护可能相当复杂。也许您应该在perf 用户邮件列表上询问是否有计划将切换功能集成到主线内核中。