C++ 性能分析工具 valgrind

今天学一下C++内存分析工具,对于C++开发人员来说,必不可少。

valgrind 介绍

valgrind是非侵入式的,直接作用于可执行文件上,在检查前不需要重新编译、链接和修改你得程序。valgrind包含几个标准工具:
1、memcheck
2、callgrind
3、cachegrind
4、heigrind

官网:www.valgrind.org
使用方法:https://valgrind.org/docs/manual/manual.html

valgrind install

下载地址:https://www.valgrind.org/downloads/current.html

tar -jxvf  valgrind-3.17.0.tar.bz2
./configure && make && make install

memcheck

话不多说先写个demo小试牛刀,最近在学智能指针shared_ptr,正好借着这个机会试一下和new的区别;

new operator

// new_operator_test.cpp
#include <string>
#include <iostream>

int main(){
        std::string* tmp = new std::string("new operator demo");
        std::cout << *tmp << std::endl;
        return 0;
}

g++编译时,一定要加上-g参数,表示生成debug信息,否则后面的valgrind不能显示出出错行号。
执行 valgrind --tool=memcheck --leak-check=yes ./new_operator_test,结果如下:

[root@ouweiqi cpp_prime]# valgrind --tool=memcheck --leak-check=yes ./new_operator_test
==4205== Memcheck, a memory error detector
==4205== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==4205== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==4205== Command: ./new_operator_test
==4205== 
new operator demo
==4205== 
==4205== HEAP SUMMARY:
==4205==     in use at exit: 72,754 bytes in 3 blocks
==4205==   total heap usage: 3 allocs, 0 frees, 72,754 bytes allocated
==4205== 
==4205== 50 (32 direct, 18 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 3
==4205==    at 0x4C2B69B: operator new(unsigned long) (vg_replace_malloc.c:422)
==4205==    by 0x400ADA: main (new_operator_test.cpp:5)
==4205== 
==4205== LEAK SUMMARY:
==4205==    definitely lost: 32 bytes in 1 blocks
==4205==    indirectly lost: 18 bytes in 1 blocks
==4205==      possibly lost: 0 bytes in 0 blocks
==4205==    still reachable: 72,704 bytes in 1 blocks
==4205==         suppressed: 0 bytes in 0 blocks
==4205== Reachable blocks (those to which a pointer was found) are not shown.
==4205== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==4205== 
==4205== For lists of detected and suppressed errors, rerun with: -s
==4205== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

解释:

  • definitely lost : 确认丢失。存在内存泄露,应该修复。当程序结束时如果一块动态分配的内存没有被释放且通过程序内的指针变量均无法访问这块内存则会报出这个错误;
  • indirectly lost : 间接丢失。当试用了含有指针成员的类可能会报这个错误。这类错误总是与definitely lost 一起出现,只需修复definitely lost 即可。
  • possibly lost :可能丢失。大多数情况下应视大多数情况下应视为与"definitely lost"一样需要尽快修复,除非你的程序让一个指针指向一块动态分配的内存(但不是这块内存起始地址),然后通过运算得到这块内存起始地址,再释放它。例子可参考我的例程。当程序结束时如果一块动态分配的内存没有被释放且通过程序内的指针变量均无法访问这块内存的起始地址,但可以访问其中的某一部分数据,则会报这个错误。
  • still reachable:可以访问,未丢失但也未释放。如果程序是正常结束的,那么它可能不会造成程序崩溃,但长时间运行有可能耗尽系统资源,因此笔者建议修复它。如果程序是崩溃(如访问非法的地址而崩溃)而非正常结束的,则应当暂时忽略它,先修复导致程序崩溃的错误,然后重新检测。
  • suppressed:已被解决。出现了内存泄露但系统自动处理了。可以无视这类错误。这类错误我没能用例程触发,看官方的解释也不太清楚是操作系统处理的还是valgrind,也没有遇到过。所以无视他吧~
    摘自:http://valgrind.org/docs/manual/faq.html#faq.deflost

shared_ptr

// shared_ptr_test.cpp
#include <memory>
#include <string>
#include <iostream>

int main() {
    // effective c++ the 17th rule: Store newed objects in smart pointers in standalone statements.
    std::shared_ptr<std::string> name(new std::string("shared_ptr first demo"));
    std::cout << *name.get() << std::endl;
    return 0;
}

执行 valgrind --tool=memcheck --leak-check=yes ./new_operator_test,结果如下:

[root@ouweiqi cpp_prime]# valgrind --tool=memcheck --leak-check=yes ./shared_ptr_test
==3276== Memcheck, a memory error detector
==3276== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==3276== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==3276== Command: ./shared_ptr_test
==3276== 
shared_ptr first demo
==3276== 
==3276== HEAP SUMMARY:
==3276==     in use at exit: 72,704 bytes in 1 blocks
==3276==   total heap usage: 4 allocs, 3 frees, 72,782 bytes allocated
==3276== 
==3276== LEAK SUMMARY:
==3276==    definitely lost: 0 bytes in 0 blocks
==3276==    indirectly lost: 0 bytes in 0 blocks
==3276==      possibly lost: 0 bytes in 0 blocks
==3276==    still reachable: 72,704 bytes in 1 blocks
==3276==         suppressed: 0 bytes in 0 blocks
==3276== Reachable blocks (those to which a pointer was found) are not shown.
==3276== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==3276== 
==3276== For lists of detected and suppressed errors, rerun with: -s
==3276== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

callgrind

callgrind通过x86和amd64平台明确的返回和调用指令,能否发现函数调用的关系,可以用来统计程序的函数调用关系以及执行耗时;

// malloc_test.cc
#include <stdio.h>
#include <stdlib.h>
 
void f1() {
  int i;
  int *p;
  for (i = 0; i < 10; i++) {
    p = (int*)malloc(sizeof(int));
    *p = 10;
    free(p);
  }
}
 
void f2() {
  int i;
  int *p;
  for (i = 0; i < 20; i++) {
    p = (int*)malloc(sizeof(int));
    *p = 10;
    free(p);
  }
}
 
void f3() {
  int i;
  int *p;
  for (i = 0; i < 30; i++) {
    p = (int*)malloc(sizeof(int));
    *p = 10;
    free(p);
  }
}
 
int main() {
  int i;
  for (i = 0; i < 1000; i++) {
    f1();
    f2();
    f3();
  }
  return 0;
}

编译:gcc -g malloc_test.cc -o malloc_test
然后执行valgrind --tool=callgrind ./malloc_test --dump-instr=yes --trace-jump=yes,生成callgrind.out.2606性能分析文件(2606表示进程号)

gprof2dot

该工具把profile结果生成图片,工具来自https://github.com/jrfonseca/gprof2dot

install

执行gprof2dot.py脚本需要安装python2或Python3,以及graphviz

yum install python3 graphviz
pip install --upgrade pip

use

gprof2dot -f callgrind -n10 -s callgrind.out.2606 > valgrind.dot
dot -Tpng valgrind.dot -o valgrind.png

在这里插入图片描述
参考:https://www.cnblogs.com/willhua/p/9818530.html

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
google-perftools 简介 google-perftools 是一款针对 C/C++ 程序的性能分析工具,它是一个遵守 BSD 协议的开源项目。使用该工具可以对 CPU 时间片、内存等系统资源的分配和使用进行分析,本文将重点介绍如何进行 CPU 时间片的剖析。 google-perftools 对一个程序的 CPU 性能剖析包括以下几个步骤。 1. 编译目标程序,加入对 google-perftools 库的依赖。 2. 运行目标程序,并用某种方式启动 / 终止剖析函数并产生剖析结果。 3. 运行剖结果转换工具,将不可读的结果数据转化成某种格式的文档(例如 pdf,txt,gv 等)。 安装 您可以在 google-perftools 的网站 (http://code.google.com/p/google-perftools/downloads/list) 上下载最新版的安装包。为完成步骤 3 的工作,您还需要一个将剖析结果转化为程序员可读文档的工具,例如 gv(http://www.gnu.org/software/gv/)。 编译与运行 您需要在原有的编译选项中加入对 libprofiler.so 的引用,这样在目标程序运行时会加载工具的动态库。例如本例中作者的系统中,libprofiler.so 安装在"/usr/lib"目录下,所以需要在 makefile 文件中的编译选项加入“-L/usr/lib -lprofiler”。 google-perftools 需要在目标代码的开始和结尾点分别调用剖析模块的启动和终止函数,这样在目标程序运行时就可以对这段时间内程序实际占用的 CPU 时间片进行统计和分析工具的启动和终止可以采用以下两种方式。 a. 使用调试工具 gdb 在程序中手动运行性能工具的启动 / 终止函数。 gdb 是 Linux 上广泛使用的调试工具,它提供了强大的命令行功能,使我们可以在程序运行时插入断点并在断点处执行其他函数。具体的文档请参照 http://www.gnu.org/software/gdb/,本文中将只对用到的几个基本功能进行简单介绍。使用以下几个功能就可以满足我们性能调试的基本需求,具体使用请参见下文示例。 命令 功能 ctrl+c 暂停程序的运行 c 继续程序的运行 b 添加函数断点(参数可以是源代码中的行号或者一个函数名) p 打印某个量的值或者执行一个函数调用 b. 在目标代码中直接加入性能工具函数的调用,该方法就是在程序代码中直接加入调试函数的调用。 两种方式都需要对目标程序重新编译,加入对性能工具的库依赖。对于前者,他的好处是使用比较灵活,但工具的启动和终止依赖于程序员的手动操作,常常需要一些暂停函数(比如休眠 sleep)的支持才能达到控制程序的目的,因此精度可能受到影响。对于后者,它需要对目标代码的进行修改,需要处理函数声明等问题,但得到的结果精度较高,缺点是每次重新设置启动点都需要重新编译,灵活度不高,读者可以根据自己的实际需求采用有效的方式。 示例详解 该程序是一个简单的例子,文中有两处耗时的无用操作,并且二者间有一定的调用关系。 清单 1. 示例程序 void consumeSomeCPUTime1(int input){ int i = 0; input++; while(i++ < 10000){ i--; i++; i--; i++; } }; void consumeSomeCPUTime2(int input){ input++; consumeSomeCPUTime1(input); int i = 0; while(i++ < 10000){ i--; i++; i--; i++; } }; int stupidComputing(int a, int b){ int i = 0; while( i++ < 10000){ consumeSomeCPUTime1(i); } int j = 0; while(j++ < 5000){ consumeSomeCPUTime2(j); } return a+b; }; int smartComputing(int a, int b){ return a+b; }; void main(){ int i = 0; printf("reached the start point of performance bottle neck\n"); sleep(5); //ProfilerStart("CPUProfile"); while( i++ MyProfile.pdf 转换后产生的结果文档如下图。图中的数字和框体的大小代表了的某个函数的运行时间占整个剖析时间的比例。由代码的逻辑可知,stupidComputing,stupidComputing2 都是费时操作并且它们和 consumeSomeCPUTime 存在着一定的调用关系。 图 1. 剖析结果 结束语 本文介绍了一个 Linux 平台上的性能剖析工具 google-perftools,并结合实例向读者展示了如何使用该工具配置、使用及分析性能瓶颈。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值