Ubuntu18.04系统+ROS节点
记录下C++代码性能分析工具的简单使用方式,包括valgrind、gprof、gperftools。
<node pkg="this_pkg" type="this_pkg_node" name="this_pkg_node_name" output="screen" **launch-prefix**="valgrind --log-file=valReport --leak-check=full --show-reachable=yes --leak-resolution=low"/>
生成的检测报告是主目录下的“.ros/valReport ”
launch-prefix="valgrind --tool=massif --time-unit=B
--massif-out-file=massif_report.out "
然后通过ms_print分析采样文件
ms_print ./massif_report.out
massif-visualizer 可视化分析采样文件
massif-visualizer ./lio_node_massif_report.out
分析内存。
add_compile_options(-pg)
set(catkin_LIBRARIES ${catkin_LIBRARIES} -pg)
然后正常运行launch文件启动程序,ctrl+c终止程序。
cd .ros
gprof /home/robot/catkin/devel/lib/this_pkg/this_pkg_node gmon.out
gprof /home/robot/catkin/devel/lib/this_pkg/this_pkg_node gmon.out -b > node_mem.txt
-b:可选参数。
sudo pip install gprof2dot
可视化命令:
gprof2dot -n 7 -s ./node_mem.txt | dot -Tpng -o output_dg.png
字段含义:
https://zhuanlan.zhihu.com/p/150527364
指定输出文件名:
https://blog.csdn.net/yangtao420902/article/details/105575491
git clone https://github.com/gperftools/gperftools.git
cd gperftools/
./autogen.sh
./configure
make -j4
sudo make install
# 检查下安装结果
ls /usr/local/lib/libprofiler.*
target_link_libraries(this_pkg_node
tcmalloc
profiler)
launch-prefix="env CPUPROFILE=/tmp/my_executable.prof LD_PRELOAD=/usr/local/lib/libprofiler.so.0"
launch-prefix="env HEAPPROFILE=/tmp/node_heap_profile.hprof LD_PRELOAD=/usr/local/lib/libtcmalloc.so LD_PRELOAD=/usr/local/lib/libprofiler.so.0"
profile查看:
pprof --pdf /path/to/your/node_exe /tmp/node_heap_profile.hprof.0001.heap > node_heap_profiling_00.pdf
多文件比较看内存差值:
pprof --pdf /path/to/your/node_exe --base=/tmp/node_heap_profile.hprof.0001.heap /tmp/node_heap_profile.hprof.0002.heap > node_heap_profiling_01_2.pdf
–base参数值是基准文件,可以得到0002.heap时刻相对于0001.heap的变化。
以上就是这三种工具的调用方法和结果分析命令。其中,gperftools的内存使用分析heap profile还挺好的,通过对比不同时刻内存占用的差值,可以看到内存的变化趋势;可视化做得不错,可定位到具体函数。