Thursday, April 11, 2013


Instrumentation using gprof

gprof is a free performance analysis tool on Linux. One is always concerned about the efficiency of the code developed. Also, timing it manually might be tedious. To elevate this we could use performance analysis tools to time the code automatically and remove possible bottlenecks. This process is called profiling and there are other advanced tools which are available. However, gprof is a start point for any beginner or early analysis.

Ok, quickly getting to the topic. The following steps could be followed for profiling a simple C or FORTRAN code.

Step 1:
Enabling profiling is as simple as adding -pg to the gcc compile flags. T

gcc mycode.c -pg

Step 2:
Now run the code normally for sufficiently large number of samples. The code could be setup to run for few samples rather than the full runtime. However, it is good to keep the execution running for few seconds for gprof to collect statistics accurately.

./a.out [arguments if any] 

Step 3:
Finally, the profiler is called and the two inputs for grpof are, the name of the executable and a file gmon.out which is the monitor file generated while execution.

gprof example1 gmon.out -p

Here -p option refers to flat graph, alternatively, -q refers to call graph.

Flat graph, prints the functions called and the runtime of each function or subroutine.
Whereas, call graph in addition gives out the detail of each function separately.
Additionally, -A option print out the code bits overlayed with the timing and number of calls. 

One could simply check the validity of the timings using time command as

time ./a.out [arguments if any]

It is to be noted that system commands in the code should be avoided as they are not timed by gprof. A discussion is found hereFor a longer and definitely better understanding, see article.





Subscribe to RSS Feed Follow me on Twitter!