Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
811 views
in Technique[技术] by (71.8m points)

linux - Using perf probe to monitor performance stats during a particular function

I'm trying to monitor performance stats during a particular function using linux perf tool.

I was following the instructions given at https://perf.wiki.kernel.org/index.php/Jolsa_Features_Togle_Event#Example_-_using_u.28ret.29probes

I tried to get instruction count of a simple C program. (As shown below)

1) My simple C code

#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) Compiling and adding probes

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) Trying to use perf stat to measure the instruction count within the func() function. This leads to an error.

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

Could someone point me where I did wrong?

[I'm using linux kernel 3.11.0-12-generic]

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think that the instructions you are following are not yet included into the mainline Linux kernel. As a consequence, perf is telling you that the events are not supported: perf doesn't know the "toggle" mechanism mentioned on this page.

I can see two workarounds:

  1. If you have access to the source code you want to profile you can use the perf_event_open system call directly from your source code to start and stop counting on function entry and exit.
  2. Clone jolsa repository git clone https://kernel.googlesource.com/pub/scm/linux/kernel/git/jolsa/perf switch the core_toggle branch git co remotes/origin/perf/core_toggle and then compile and run the kernel with this support.

Regarding 2, I am not familiar at all with kernel versions and development and I think that this solution may be quie complex to use and maintain. Maybe you should ask on the perf users mailing list if there are any plans for the toggle feature to be integrated into the mainline kernel.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...