Skip to content

Debugging & Profiling Tips

Tomas Mlcoch edited this page Mar 3, 2014 · 16 revisions

Tools

Additional compilation options

For example for gperftools use:

cmake -DCMAKE_BUILD_TYPE:STRING=DEBUG -DCMAKE_C_FLAGS:STRING=" -ltcmalloc " ..

Quick howto

Valgrind

valgrind prog

If the program uses Glib2 library, there can be a lot of false positives. So use the command instead:

G_SLICE=always-malloc G_DEBUG=gc-friendly valgrind prog

Tip: Add this line to your ~/.bashrc: alias gvalgrind='G_SLICE=always-malloc G_DEBUG=gc-friendly valgrind' and use gvalgrind instead of valgrind.

Useful options

  • --leak-check=full - show details for each definitely lost or possibly lost block, including where it was allocated

Valgrind - Massif

A heap profiler

valgrind --tool=massif prog
# And then
ms_print massif.out.12345

Useful options for ms_print

  • --threshold=50.0 - Allocation tree entries that account for less than this will be aggregated. (e.g. ms_print --threshold=50.0 massif.out.12345)

gperftools

HEAPCHECK=normal prog           # Heap checker
HEAPPROFILE=/tmp/netheap prog   # Heap profiler
CPUPROFILE=/tmp/profile prog    # CPU profiler

Note: If it complain that PPROF_PATH is not set and memory addresses are not translated to symbols properly, prepend PPROF_PATH=/usr/bin/pprof (of course with path that match your system - use whereis pprof to obtain the path)

Analyzing gperftools outputs

pprof --text prog /tmp/netheap.0001.heap 

Another debug tools/techniques available on any Linux system

Top

You can run your program prog and then set watch its memory run-time CPU and memory usage.

top -d 0.2 -p $(ps -A | grep prog | awk '{print $1}')

Notes

  • VIRT -- Virtual Image (kb) The total amount of virtual memory used by the task. It includes all code, data and shared libraries plus pages that have been swapped out and pages that have been mapped but not used.
  • RES -- Resident size (kb) The non-swapped physical memory a task has used.
  • SHR -- Shared Mem size (kb) The amount of shared memory used by a task. It simply reflects memory that could be potentially shared with other processes.

Time

Simple resource usage. Its returns three time values:

  • real - Elapsed real time between invocation and termination.
  • user - User CPU time (the sum of the tms_utime and tms_cutime values in a struct tms as returned by times(2))
  • sys - System CPU time (the sum of the tms_stime and tms_cstime values in a struct tms as returned by times(2))
time prog

Useful links

Clone this wiki locally