Monday, May 12, 2014

How to profile your code using valgrind?



There are various ways that you can profile the code. I will talk about Callgrind and Massif. You can get a lot more information from these two profilers, but I will just mention about my purposes for using them.
Use Callgrind to find out which methods get called the most, and use Massif to find out which methods use the most memory.

Callgrind records the call history and the program's calls graph. Here I am using it to find which methods get called the most.

Example:
>valgrind --tool=callgrind  --callgrind-out-file=callgrind_prof.out validaterpd -s -o consichk.txt -r coreapplication_OH452963239_v326.rpd -pAdmin123

Use kcachegrind to view the output from callgrind.

>kcachegrind callgrind_prof.out





Massif is a heap profiler. Here I am using it to find out the peak memory usage of my program.
I can also find out which method uses the most memory as well.
For details on now to interpret the results of your profile, please check this page

http://valgrind.org/docs/manual/ms-manual.html


Occasionally you may running into issues where valgrind fails to run complaining about the
VG_N_SEGMENTS segment being too low.

If you run into this error, you need to change the default value and recompile valgrind.
You can search for the cpp file defining this  VG_N_SEGMENTS value and change it.
For example, I have changed it from 30000 to 90000.



Example:

>valgrind --depth=200 --tool=massif  --massif-out-file=main_mem_prof.out validaterpd -s -o consichk.txt -r coreapplication_OH452963239_v326.rpd -pAdmin123

>ms_print main_mem_prof.out > main_depth200.txt


From the screenshot, you can tell that the peak is at snapshot 4 and the peak memory usage is around 4.2GB.

No comments: