前言
今天在查资料的时候突然看到Kcachegrind性能图,我就想起来之前没什么机会玩,而且我即将进入驱动及应用开发,那么本次我还需要关注性能,所以呢!Valgrind及perf我就先安装在虚拟机的ubuntu中体验下,将来再安装到PC机的ubuntu中。
Valgrind
1.官网下载
https://www.valgrind.org/downloads/
Valgrind包含下列工具:
1、memcheck:检查程序中的内存问题,如泄漏、越界、非法指针等。
2、callgrind:检测程序代码的运行时间和调用过程,以及分析程序性能。
3、cachegrind:分析CPU的cache命中率、丢失率,用于进行代码优化。
4、helgrind:用于检查多线程程序的竞态条件。
5、massif:堆栈分析器,指示程序中使用了多少堆内存等信息。
6、lackey:
7、nulgrind:
2.安装及使用
安装参考网址:https://www.cnblogs.com/defen/p/5560926.html
此网址中的测试故障代码如下
#include <stdlib.h>
void f(void)
{
int* x = malloc(10 * sizeof(int));
x[10] = 0; // problem 1: heap block overrun
} // problem 2: memory leak -- x not freed
int main(void)
{
f();
return 0;
}
我的运行结果
root@ubuntu:/study/makefile/test# subl test1.c
root@ubuntu:/study/makefile/test# gcc test1.c -o test1
root@ubuntu:/study/makefile/test# valgrind --leak-check=yes ./test1
==51530== Memcheck, a memory error detector
==51530== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==51530== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==51530== Command: ./test1
==51530==
==51530== Invalid write of size 4
==51530== at 0x108668: f (in /study/makefile/test/test1)
==51530== by 0x108679: main (in /study/makefile/test/test1)
==51530== Address 0x522e068 is 0 bytes after a block of size 40 alloc'd
==51530== at 0x4C2FECB: malloc (vg_replace_malloc.c:307)
==51530== by 0x10865B: f (in /study/makefile/test/test1)
==51530== by 0x108679: main (in /study/makefile/test/test1)
==51530==
==51530==
==51530== HEAP SUMMARY:
==51530== in use at exit: 40 bytes in 1 blocks
==51530== total heap usage: 1 allocs, 0 frees, 40 bytes allocated
==51530==
==51530== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==51530== at 0x4C2FECB: malloc (vg_replace_malloc.c:307)
==51530== by 0x10865B: f (in /study/makefile/test/test1)
==51530== by 0x108679: main (in /study/makefile/test/test1)
==51530==
==51530== LEAK SUMMARY:
==51530== definitely lost: 40 bytes in 1 blocks
==51530== indirectly lost: 0 bytes in 0 blocks
==51530== possibly lost: 0 bytes in 0 blocks
==51530== still reachable: 0 bytes in 0 blocks
==51530== suppressed: 0 bytes in 0 blocks
==51530==
==51530== For lists of detected and suppressed errors, rerun with: -s
==51530== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
二,使用callgrind生成Kcachegrind图
root@ubuntu:/study/makefile/test# valgrind --tool=callgrind ./testperf
==61310== Callgrind, a call-graph generating cache profiler
==61310== Copyright (C) 2002-2017, and GNU GPL'd, by Josef Weidendorfer et al.
==61310== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==61310== Command: ./testperf
==61310==
==61310== For interactive control, run 'callgrind_control -h'.
hello world!
hello world!
hello world!
test
hello world!
test
hello world!
test
==61310==
==61310== Events : Ir
==61310== Collected : 173794
==61310==
==61310== I refs: 173,794
root@ubuntu:/study/makefile/test# ls
callgrind.out.61310 test1 test1.c testperf testperf.c
我下载了Kcachegrind安装需要qt比较麻烦。所以网上搜索到一个qcachegrind已经打包了QT及dot等,所以在windows上分析callgrind.out.61310,效果不错。
下载网址:
https://nchc.dl.sourceforge.net/project/qcachegrindwin/0.7.4/qcachegrind074-32bit-x86.zip
![](https://img.haomeiwen.com/i12010880/b2ec711223555677.png)
三,perf安装和使用
Perf安装主要是下载速度太慢,参考如下网址,直接换源再安装
https://blog.csdn.net/xue1065540183/article/details/108312293
Perf很快安装完成,并且可以使用了。
![](https://img.haomeiwen.com/i12010880/d729137654c1d64b.png)
./fork
在新的终端下运行:
sudo perf timechart record
几秒后ctl+c中断
sudo perf timechart
结果输出在output.svg下面
![](https://img.haomeiwen.com/i12010880/a9a4997c64e8326f.png)
四,参考网址
带你玩转Visual Studio——性能分析与优化
perf安装1
perf安装2
perf官网
perf教程
网友评论