gprof

gprof 可视化

gprof 是 GNU 工具之一,它在编译的时候在每个函数的出入口加入了 profiling 的代码,运行时统计程序在用户态的执行信息,可以得到每个函数的调用次数,执行时间,调用关系等信息,简单易懂。适合于查找用户级程序的性能瓶颈,对于很多时间都在内核态执行的程序,gprof 不适合(内核调用分析可以考虑使用 strace)

使用方法

给 GNU Tools 传递 PG 选项,编译器和连接器都需要加上-pg 选项

g++ -g -pg ...

启动程序,待程序执行完成之后(可手动停止)将自动生成 gmon.out 文件,当前文件包含了函数调用信息

以文本的形式显示调用分析

示例命令:

# pwd #/data/logs/tars/app_log
gprof /usr/.../***Server gmon.out > t.txt

输出示例:

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  ms/call  ms/call  name    
  9.78      1.58     1.58 16852032     0.00     0.00  std::pair<std::_Rb_tree_iterator<std::pair<std::string const, YUAccessor::ProfileTag> >, bool> std::_Rb_tree<std::string, std::pair<std::string const, YUAccessor::ProfileTag>, std::_Select1st<std::pair<std::string const, YUAccessor::ProfileTag> >, std::less<std::string>, std::allocator<std::pair<std::string const, YUAccessor::ProfileTag> > >::_M_insert_unique<std::pair<std::string, YUAccessor::ProfileTag> >(std::pair<std::string, YUAccessor::ProfileTag>&&)
  9.56      3.13     1.55 219949847     0.00     0.00  tars::TarsInputStream<tars::BufferReader>::read(long&, unsigned char, bool)
......
......
......

可视化方式

安装工具

# ubuntu
sudo apt-get install python graphviz
sudo pip install gprof2dot

假设 t.txt 文件由上面的 gprof 生成,执行下面命令可以生成调用图

gprof2dot -n 7 -s ./t.txt | dot -Tpng -o output_dg.png

gprof2dot 输出控制命令可以参考 https://github.com/jrfonseca/gprof2dot#documentation

输出节点解释

+------------------------------+
|        function name         |
| total time % ( self time % ) |
|         total calls          |
+------------------------------+
  • total time % is the percentage of the running time spent in this function and all its children;
  • self time % is the percentage of the running time spent in this function alone;
  • total calls is the total number of times this function was called (including recursive calls).

节点连接线解释

           total time %
              calls
parent --------------------> children
  • total time % is the percentage of the running time transfered from the children to this parent (if available);
  • calls is the number of calls the parent function called the children.

猜你喜欢

转载自www.cnblogs.com/jiahu-Blog/p/13399923.html