Callgrind介绍

  • 用来对统计程序的函数调用之间的关系, 并统计每个函数的耗时
  • Callgrind之所以能够发现函数调用的关系, 依赖于平台的明确返回和调用指令. 在x86和amd64平台上works best, 但在PowerPC ARM Thumb以及MIPS上无法运行
  • 可以使用gprof2dot来把profile结果生成图片
  • SOF上一个关于C++ profile的很好的问题

对整个程序进行profile

  1. 先执行valgrind --tool=callgrind ./prog_name, 跑完之后会生成一个callgrind.out.X的profile文件, X为线程号
  2. 使用KCachegrind分析结果文件:kcachegrind callgrind.out.X. KCachegrind文档

只对程序某个片段进行profile

使用上面的方式的一大缺点就是会对整个程序都进行profile, 这样会导致过程很慢. 如果我们只想对程序的某个部分进行profile, 那么可以如下使用:

  1. 在一个shell中输入命令valgrind --tool=callgrind --dump-instr=yes -v --instr-atstart=no ./prog_name > log.txt, 其中, --dump-instr=yes表示生成汇编指令注释, --instr-atstart=no表示不是程序启动时就启动profile, 方便控制节点.
  2. 当程序运行到我们想要profile的片段时, 在另一个shell中输入callgrind_control -i on
  3. 当想要profile部分结束之后, 输入callgrind_control -k
  4. 使用KCachegrind分析Callgrind.out文件
更先进做法

上面的做法也就只能大概控制profile片段, 实操性不强. 根据文档说明, 可以使用指令来控制具体的Callgrind进行profile起止时间:


#include <valgrind/callgrind.h> //codes... //request callgrind to start full profile
CALLGRIND_START_INSTRUMENTATION; //codes... //request callgrind to stop full profile
CALLGRIND_STOP_INSTRUMENTATION;

callgrind.h头文件见这里. 上面的两个请求命令其实是在头文件里面定义两个宏.

/* Start full callgrind instrumentation if not already switched on.
When cache simulation is done, it will flush the simulated cache;
this will lead to an artifical cache warmup phase afterwards with
cache misses which would not have happened in reality. */
#define CALLGRIND_START_INSTRUMENTATION \
VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__START_INSTRUMENTATION, \
0, 0, 0, 0, 0) /* Stop full callgrind instrumentation if not already switched off.
This flushes Valgrinds translation cache, and does no additional
instrumentation afterwards, which effectivly will run at the same
speed as the "none" tool (ie. at minimal slowdown).
Use this to bypass Callgrind aggregation for uninteresting code parts.
To start Callgrind in this mode to ignore the setup phase, use
the option "--instr-atstart=no". */
#define CALLGRIND_STOP_INSTRUMENTATION \
VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__STOP_INSTRUMENTATION, \
0, 0, 0, 0, 0)

在一次程序运行中dump多次

使用命令CALLGRIND_DUMP_STATS;可以让Callgrind立即生成一个dump. 每次遇到这个命令都会生成一个dump, 即使在比如for循环里面, 那么就会生成循环次数相等的dump. 注意,对于CALLGRIND_START_INSTRUMENTATIONCALLGRIND_STOP_INSTRUMENTATION这对组合控制的是让Callgrind只统计命令区间内的代码, 即使这对组合放在比如for循环中, 如果没有CALLGRIND_DUMP_STATS;, 那么也只会生成一个dump.

使用CALLGRIND_ZERO_STATS;可以清除Callgrind当前的数据状态.

使用KCachegrind打开的profile dump界面:



通过按Sl排列, 就可以很容易的看出哪个函数耗时占比最大, 从而针对性的优化

Valgrind.Callgrind使用的更多相关文章

  1. valgrind的memchk和callgrind

    一.安装valgrind 安装valgrind,正常的三部曲configure/make/make install就行. 二.memchk使用 1.执行命令 [root@10g-host4 tools ...

  2. valgrind的callgrind工具进行多线程性能分析

    1.http://valgrind.org/downloads/old.html 2.yum install valgrind Valgrind的主要作者Julian Seward刚获得了今年的Goo ...

  3. 使用valgrind检查内存

    Valgrind是运行在Linux上一套基于仿真技术的程序调试和分析工具,是公认的最接近Purify的产品,它包含一个内核——一个软件合成的CPU,和一系列的小工具,每个工具都可以完成一项任务——调试 ...

  4. 【转】 如何使用Valgrind memcheck工具进行C/C++的内存泄漏检测

    系统编程中一个重要的方面就是有效地处理与内存相关的问题.你的工作越接近系统,你就需要面对越多的内存问题.有时这些问题非常琐碎,而更多时候它会演变成一个调试内存问题的恶梦.所以,在实践中会用到很多工具来 ...

  5. linux下内存泄露检测工具Valgrind介绍

    目前在linux开发一个分析实时路况的应用程序,在联合测试中发现程序存在内存泄露的情况. 这下着急了,马上就要上线了,还好发现了一款Valgrind工具,完美的解决了内存泄露的问题. 推荐大家可以使用 ...

  6. Valgrind简单用法

    Valgrind的主要作者Julian Seward刚获得了今年的Google-O'Reilly开源大奖之一──Best Tool Maker.让我们一起来看一下他的作品.Valgrind是运行在Li ...

  7. VALGRIND

    系统编程中一个重要的方面就是有效地处理与内存相关的问题.你的工作越接近系统,你就需要面对越多的内存问题.有时这些问题非常琐碎,而更多时候它会演变成一个调试内存问题的恶梦.所以,在实践中会用到很多工具来 ...

  8. 内存泄漏检测工具Valgrind

    1概述 1.1 介绍 Valgrind是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合.Valgrind由内核(core)以及基于内核的其他调试工具组成.内核类似于一个框架(fram ...

  9. [转]Valgrind简单用法

    [转]Valgrind简单用法 http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html Valgrind的主要作者Julian S ...

随机推荐

  1. travis-ci 中运行 puppeteer

    通过 travis-ci 可以构建基于 puppeteer 的自动化任务,基于此构建的一个 计划任务 puppeteer中调用需要禁用沙箱环境 https://github.com/GoogleChr ...

  2. 漫画:什么是HTTPS?

    什么是HTTP协议? HTTP协议全称Hyper Text Transfer Protocol,翻译过来就是超文本传输协议,位于TCP/IP四层模型当中的应用层. HTTP协议通过请求/响应的方式,在 ...

  3. [Swift]LeetCode56. 合并区间 | Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...

  4. [Swift]LeetCode973. 最接近原点的 K 个点 | K Closest Points to Origin

    We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the d ...

  5. 一文掌握 Linux 性能分析之网络篇(续)

    本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. 这是 Linu ...

  6. 跳槽 & 思维导图

    个人博客原文: 跳槽 & 思维导图 今年的冬天有点"冷".给大家来点实在的东西. 不知道大家在跳槽的时候是怎么做的?直接投简历面试?还是准备了一段时间,复习一波知识点后再投 ...

  7. BootStrap格栅系统

    格栅参数分为超小屏幕 手机 (<768px) 小屏幕 平板 (≥768px) 中等屏幕 桌面显示器 (≥992px) 大屏幕 大桌面显示器 (≥1200px) 栅格系统行为 总是水平排列 开始是 ...

  8. 带着萌新看springboot源码8(spring ioc源码 完)

    上一节说到实例化了所有的单实例Bean,后面还有一步遍历 12.完成容器刷新(finishRefresh();) 那个和生命周期有关的后置处理器类型是LifecycleProcessor:监听器原理我 ...

  9. [Leetcode]237. Delete Node in a Linked List -David_Lin

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  10. Kafka Producer源码简述

    接着上文kafka的简述,这一章我们一探kafka生产者是如何发送消息到消息服务器的. 代码的入口还是从 kafkaTemplate.send开始 最终我们就会到 org.springframewor ...