Skip to content

Profiling with Valgrind

Rafi Kamal edited this page Jun 21, 2013 · 4 revisions

Valgrind is a memory analysis tool, and together with callgrind and kcachegrind it can be used as a profiler.

Note: Valgrind is very slow, while profiling with it you will see your program is running 20x-50x times slower.

First, you have the download the necessary tools. All of these are open source. In Linux, you can issue the following command:

sudo apt-get install valgrind kcachegrind

Valgrind works on binary, and to get the best output, debug symbols should be present on that binary. When you make mothur from the source using default makefile, it doesn't place the debug informations on the generated binary. So you have to change the makefile as of the following:

  1. Change the line CXXFLAGS += -O3 to CXXFLAGS += -g -O0 -fno-inline (line 24) : to turn of optimization and generate debug info.
  2. Remove the line strip mothur (line 101): otherwise it will strip the debug symbols from the generated binary.

Compile mothur with the new makefile. Make sure every source file has been compiled. Then launch the terminal and go to the project folder. Type the following command in the terminal:

valgrind --tool=callgrind ./mothur

This will launch mothur inside Valgrind. You can now do the whatever you want to profile (say, trying the command: classify.shared(shared=dataset.shared, design=dataset.design, numtrees=10)). Then quit mothur. You will see a new file is generated in the project folder: callgrind.out.pid.

Then run kcachegrind. Open the callgrind.out.pid file. This will present you with various information. On left, you will see a summary of various function call made during the run of the program, sorted in the increasing order of their execution time. You may want to sort them by self attribute, which denotes what percentage of time has been spent inside only that function. Click any of the function and you will see a more detailed overview on the right. To see the call graph, click on the Call Graph tab at the bottom right. It will show the functions executed in order, which functions took how much time, and how many times it have been called. To see which line of your code took how much time, click on the Source Code tab at upper right. By default, all times are shown in relative percentage. You can deactivate it from the toolbar.

For more info:

Clone this wiki locally