gperftools
git clone https://github.com/gperftools/gperftools.git
cd gperftools
./autogen.sh
./configure
make
sudo make install
c++ -o <executable-file> <source-files> -Wl,--no-as-needed,-lprofiler,--as-needed
CPUPROFILE=<path-to-profile-file> <path-to-executable-file>
pprof [options] <path-to-executable-file> <path-to-profile-file(s)>
# options:
# --text Generate text report
# --gv Generate Postscript and display
# --evince Generate PDF and display
# --web Generate SVG and display
gprof
⚠️ 仅在 Linux 系统中可用。
编译、链接时,开启 -pg
选项:
cc -c myprog.c -g -pg
cc -o myprog myprog.o -pg
# 或
cc -o myprog myprog.c -g -pg
运行程序,生成二进制文件 gmon.out
:
./myprog
gprof options [executable-file [profile-data-files...]] [> outfile]
# 纯文字报告:-p 或 --flat-profile
gprof -p myprog gmon.out > flat-report
# 图形化报告:-q 或 --graph
gprof -q myprog gmon.out > graph-report
perf
git clone https://github.com/brendangregg/FlameGraph # or download it from github
cd FlameGraph
FLAMEGRAPH=$PWD
cd <directory-of-exe>
perf record -g ./<exe> [argv] # 生成 perf.data
perf script | $FLAMEGRAPH/stackcollapse-perf.pl > out.perf-folded
$FLAMEGRAPH/flamegraph.pl out.perf-folded > perf.svg