Advantages compared with gprof
- it runs on an unmodified executable (using a virtual machine).
- You can compile postgres the regular way, start the system up, and then create a short shell script called "postgres" that you put in place of the original executable that invokes valgrind on the original executable.
- Valgrind does 100% tracing of the program rather than statistical sampling, and since it runs in a pure virtual machine, it can detect almost all memory corruption and leaks.
Disadvatages compared with gprof
- The big disadvantage of valgrind is that it slows the process WAY down, like by a factor of 5-10 on CPU. For a pure CPU process, it doesn't screw up your stats, but if a process is mixed CPU and I/O, the CPU will appear to dominate.
How to use
# see https://stackoverflow.com/a/44989219/8966765 for deatails.
valgrind --leak-check=full \
--show-leak-kinds=all \
--track-origins=yes \
--verbose \
--log-file=valgrind-out.txt \
./executable exampleParam1
# https://web.stanford.edu/class/archive/cs/cs107/cs107.1226/resources/callgrind#:~:text=The%20Valgrind%20profiling%20tools%20are,and%20builds%20a%20function%20callgraph.
valgrind --tool=callgrind program-to-run program-arguments
callgrind_annotate --auto=yes callgrind.out.pid