- 1、下载及安装libunwind
下载地址: http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz
安装:
./configure --prefix=/home/your_name/tools/libunwind/ CFLAGS=-U_FORTRIFY_SOURCE
make
make install
- 2、下载及安装gperftools
git地址:https://github.com/gperftools/gperftools
安装:
./configure --prefix=/home/your_name/tools/gperftools LDFLAGS=-L/home/your_name/tools/libunwind/lib CPPFLAGS=-I/home/your_name/tools/libunwind/include
make
make install
- 3、安装kcachegrind
系统ubuntu16.04
sudo apt-get install kcachegrind
- 4、使用方法
参考:https://dirtysalt.github.io/gperftools.html#orgheadline4
举例:
env LD_PRELOAD="/home/your-name/tools/gperftools/lib/libprofiler.so" CPUPROFILE_FREQUENCY=100 CPUPROFILE=cpu_perf.prof ./Test
运行完成后会在当前目录下生成cpu_perf.prof文件,结合pprof工具(gperftools/bin)可生成txt或pdf等:
~/tools/gperftools/bin/pprof --pdf ./test cpu_perf.prof > cpu_perf.pdf
~/tools/gperftools/bin/pprof --txt ./test cpu_perf.prof > cpu_perf.txt
或者在代码中加入google/profiler.h头文件并结合相应函数使用。