GProf的使用
-
1、安装gprof2dot.py
下载地址 http://jrfonseca.googlecode.com/svn/trunk/gprof2dot/gprof2dot.py 得到 gprof2dot-2021.2.21.tar.gz 解压 tar -zvxf gprof2dot-2021.2.21.tar.gz
-
2、配置gprof2dot.py环境变量
-
1、当前终端单次有效 执行命令 export PATH=$PATH:/home/gzrobot/gprof_dir/gprof2dot-2021.2.21
-
2、当前用户永久有效
vi ~/.profile
通过 “:” 分割追加PATH
-
-
3、配置所有用户有效
vi /etc/profile
在最后加入 export PATH="=$PATH:/home/gzrobot/gprof_dir/gprof2dot-2021.2.21"
再使用 source /etc/profile 使配置生效
#include <iostream>
#include <thread>
using namespace std;
int add(int a, int b)
{
for(int i = 0; i < 10000; ++i)
{
;
}
return a + b;
}
int sub(int a, int b)
{
for(int i = 0; i < 5000; ++i)
{;}
return a - b;
}
int call()
{
cout << add(1, 2) << endl;
cout << sub(2, 4) << endl;
}
int main()
{
for(int i = 0; i < 10000; ++i)
call();
return 0;
}
-
编译
g++ -o test_gprof test_gprof.cpp -pg -std=c++11
-
运行程序,生成gmon.out
-
查看输出
gprof ./prog gmon.out -b gzrobot@gzrobot:~/Gprof$ gprof ./test_gprof gmon.out -b Flat profile: Each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls us/call us/call name 57.13 0.30 0.30 10000 30.28 30.28 add(int, int) 43.80 0.53 0.23 10000 23.21 23.21 sub(int, int) 0.00 0.53 0.00 10000 0.00 53.50 call() 0.00 0.53 0.00 1 0.00 0.00 _GLOBAL__sub_I__Z3addii 0.00 0.53 0.00 1 0.00 0.00 __static_initialization_and_destruction_0(int, int) Call graph granularity: each sample hit covers 2 byte(s) for 1.87% of 0.53 seconds index % time self children called name <spontaneous> [1] 100.0 0.00 0.53 main [1] 0.00 0.53 10000/10000 call() [2] ----------------------------------------------- 0.00 0.53 10000/10000 main [1] [2] 100.0 0.00 0.53 10000 call() [2] 0.30 0.00 10000/10000 add(int, int) [3] 0.23 0.00 10000/10000 sub(int, int) [4] ----------------------------------------------- 0.30 0.00 10000/10000 call() [2] [3] 56.6 0.30 0.00 10000 add(int, int) [3] ----------------------------------------------- 0.23 0.00 10000/10000 call() [2] [4] 43.4 0.23 0.00 10000 sub(int, int) [4] ----------------------------------------------- 0.00 0.00 1/1 __libc_csu_init [18] [11] 0.0 0.00 0.00 1 _GLOBAL__sub_I__Z3addii [11] 0.00 0.00 1/1 __static_initialization_and_destruction_0(int, int) [12] ----------------------------------------------- 0.00 0.00 1/1 _GLOBAL__sub_I__Z3addii [11] [12] 0.0 0.00 0.00 1 __static_initialization_and_destruction_0(int, int) [12] ----------------------------------------------- Index by function name [11] _GLOBAL__sub_I__Z3addii [4] sub(int, int) [2] call() [3] add(int, int) [12] __static_initialization_and_destruction_0(int, int) , int)
-
生成图表
gprof ./tmp | gprof2dot.py |dot -Tpng -o report.png