• linux下搭建gperftools工具分析程序瓶颈


    1. 先安装 unwind

    //使用root
    wget https://github.com/libunwind/libunwind/archive/v0.99.tar.gz  
    tar -xvf v0.99.tar.gz  
    cd libunwind-0.99  
    autoreconf --force -v --install  
    ./configure
    make   
    sudo make install
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2. 安装gperftools

    wget https://github.com/gperftools/gperftools/releases/download/gperftools-2.6.1/gperftools-2.6.1.tar.gz
    
    autoreconf -i
    ./configure
    make
    sudo make install
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    以上都ok后,依赖库就好了。 到/usr/local/lib下找一下库有没有。

    3. 代码里封装gperftools入口

    #include "gperftools/profiler.h"
    #include "gperftools/heap-profiler.h"
    
    void gperfCpuStart()
    {
    #ifdef OPEN_PROFILE
            UtilFile::makeDirectoryRecursive("perf");
            ProfilerStart("perf/cpu.perf");
    #endif
    };
    
    void gperfCpuStop()
    {
    #ifdef OPEN_PROFILE
            ProfilerStop();
    #endif
    };
    
    void gperfCpuFlush()
    {
    #ifdef OPEN_PROFILE
            ProfilerFlush();
    #endif
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在外部添加一个开关调用上述方法,目的就是可以随时开随时关,方便监控一段时间内的性能。

    4. 修改CMake

    ADD_LIBRARY(tcmalloc_and_profiler STATIC IMPORTED)
    SET_TARGET_PROPERTIES(tcmalloc_and_profiler PROPERTIES IMPORTED_LOCATION /usr/local/lib/libtcmalloc_and_profiler.so)
    
    ADD_LIBRARY(unwind STATIC IMPORTED)
    SET_TARGET_PROPERTIES(unwind PROPERTIES IMPORTED_LOCATION /usr/lib64/libunwind.so)
    
    TARGET_LINK_LIBRARIES(Server tcmalloc_and_profiler unwind lzma redis util zlib ss
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    差不多类似这样,具体情况按照具体的修改。

    • 随后编译程序,在合适的地方开启性能监测,合适的地方再关闭性能检测
    • 关闭后,在程序运行目录下能找到cpu.perf文件

    5. 生成可视化文件

    pprof --callgrind Server cpu.perf > callgrind.perf.cpu
    
    • 1

    几分钟后生成callgrind.perf.cpu文件

    6. 在window下装个可视化工具

    KCachegrind这个挺难找的,直接搜:QCachegrind这个。
    用可视化工具打开就能看到了函数调用情况。在这里插入图片描述

  • 相关阅读:
    LeetCode每日一题(2226. Maximum Candies Allocated to K Children)
    【Shell学习笔记】Bash的模式扩展
    L2-013 红色警报(Python3)
    VScode配置 github 上传代码
    前端项目实战182-ant design Cascader实现自定义字段
    算法——排序
    前端工程化之:webpack4-2(babel预设+babel插件+webpack中运行)
    复制功能插件clipboard
    Qt 串口通信(C++)
    【算法之路】高精度算法(实现加减乘除)
  • 原文地址:https://blog.csdn.net/I_can_/article/details/134337414