【valgrind】How to get hotspot in 10 mins
1. Overview
Assume you know nothing about the source code in detal, but you have a test/profile case in your hand. How can you find the hotspot in 10 mins? The answer is Calgrind in Valgrind!!
Here is a very good introduction about Callgrind from http://baptiste-wicht.com/posts/2011/09/profile-c-application-with-callgrind-kcachegrind.html
"Callgrind is a tool in part of the Valgrind toolchain. It is running in Valgrind framework. The principle is not the same. When you use Callgrind to profile an application, your application is transformed in an intermediate language and then ran in a virtual processor emulated by valgrind. This has a huge run-time overhead, but the precision is really good and your profiling data is complete. An application running in Callgrind can be 10 to 50 times slower than normally."
2. Install
sudo apt-get install valgrind kcachegrind graphviz
Valgrind: a tool box which include memory leak checker, Calgrind and so on.
kcachegrind: "the profile data visualization" (ref from: http://kcachegrind.sourceforge.net/html/Home.html)
graphviz: in order to view the call graph in KCachegrind.(ref from: http://baptiste-wicht.com/posts/2011/09/profile-c-application-with-callgrind-kcachegrind.html)
2.1. specific for Putty@Windows
If you are working on Putty@Windows to remote to a Linux server, You also need:
- install Xming on Windows:
- "Enable X11 forwarding" in Putty config.
3. Using Callgrind and Kcachegrind
3.1. Example code:
3.2. Shell command to profile with Callgrind and Kcachegrind:
josh@josh-VirtualBox:~/test_profile$ gcc test_profile.c add_mul.c -O3 -o x josh@josh-VirtualBox:~/test_profile$ valgrind --tool=callgrind ./x ==7066== Callgrind, a call-graph generating cache profiler ==7066== Copyright (C) 2002-2011, and GNU GPL'd, by Josef Weidendorfer et al. ==7066== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info ==7066== Command: ./x ==7066== ==7066== For interactive control, run 'callgrind_control -h'. ==7066== ==7066== Events : Ir ==7066== Collected : 568917 ==7066== ==7066== I refs: 568,917 josh@josh-VirtualBox:~/test_profile$ kcachegrind callgrind.out.70
3.3. Result:
Here I only paste the "call graph". See more details yourself
4. Others
You can even profile a Shell command, Python script using this method.
profile Shell command:
$ valgrind --tool=callgrind ls -l
profile Python script:
josh@josh-VirtualBox:~$ cat test.py #!/usr/bin/python r=range(0,10000) for x in r: print x josh@josh-VirtualBox:~$ valgrind --tool=callgrind ./test.py