Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to find performance of individual functions in a process using perf tool?

I am trying to get performance of individual functions within a process. How can I do it using perf tool? Is there any other tool for this?

For example, let's say, main function calls functions A , B , C . I want to get performance of main function as well as functions A,B,C individually .

Is there a good document for understating perf source code?

Thank you.

like image 751
Bapi Sekh Avatar asked Jan 02 '15 23:01

Bapi Sekh


People also ask

What can perf do?

The perf tool can be used to collect profiles on per-thread, per-process and per-cpu basis. There are several commands associated with sampling: record, report, annotate. You must first collect the samples using perf record. This generates an output file called perf.

How do I read a perf data file?

There is builtin perf. data parser and printer in perf tool of linux tools with subcommand "script". perf-script - Read perf. data (created by perf record) and display trace output This command reads the input file and displays the trace recorded.

What is perf tool?

Perf is a profiler tool for Linux 2.6+ based systems that abstracts away CPU hardware differences in Linux performance measurements and presents a simple command line interface. It covers hardware level (CPU/PMU, Performance Monitoring Unit) features and software features (software counters, tracepoints) as well.

How does perf work in Linux?

Perf works on the Model Specific Registers of your CPU for measurements like cycles or branch-misses or so. A special Part called PMU(Performance Measurement Unit) is counting all kinds of events.


2 Answers

What you want to do is user-land probing. Perf can only do part of it. Try sudo perf top -p [pid] and then watch the scoreboard. It will show the list of functions sorted by CPU usage. Here is an snapshort of redis during benchmark:

perf top profiling redis

If you want to get more infos of your user-land functions, such as IO usage, latency, memory usage, I strongly suggest you to use Systemtap. It is both scripting language and tool for profiling program on Linux kernel-based operation system. Here is a tutorial about it:

http://qqibrow.github.io/performance-profiling-with-systemtap/

And you don't need to be a expert of systemtap scripting, there are many good script online for you. For example, there is an example about using it to find out the latency of specific function.

https://github.com/openresty/stapxx#func-latency-distr

like image 78
qqibrow Avatar answered Sep 22 '22 05:09

qqibrow


See the Perforator tool, which is built for this: https://github.com/zyedidia/perforator.

Perforator uses the same perf_event_open API that perf uses, but also uses ptrace so that profiling can be selectively enabled only for certain regions of a program (such as functions). See the examples at the Github repository for details.

like image 42
Zach Avatar answered Sep 19 '22 05:09

Zach