gcov 和 perf 使用的基本套路备忘 ubuntu

一、源代码

$ cat helloSeven.c
#include <stdio.h>

void timeSeven(int count){
        for(int i=0; i<count; i++){
                if(i%7 == 0){
                        printf("i=%d is times of seven.\n",i);
                }else{
                        printf("i=%d is not times of 7.\n",i);

                }
        }
}


int main(){

        timeSeven(100);
        return 0;
}

二、编译命令

$ gcc -fprofile-arcs -ftest-coverage helloSeven.c -o helloSeven

$ ls
helloSeven  helloSeven.c  helloSeven.gcno

#运行一下binary文件,这是必要的
$ ./helloSeven

#会生成要给.gcda文件,接下来有用的
$ ls
helloSeven  helloSeven.c  helloSeven.gcda  helloSeven.gcno

$ gcov helloSeven.c
File 'helloSeven.c'
Lines executed:100.00% of 9
Creating 'helloSeven.c.gcov'

#有生成了一个文件 .c.gcov
$ ls
helloSeven  helloSeven.c  helloSeven.c.gcov  helloSeven.gcda  helloSeven.gcno


$ cat helloSeven.c.gcov

三、查看 .c.gcov 文件内容

$ cat helloSeven.c.gcov
        -:    0:Source:helloSeven.c
        -:    0:Graph:helloSeven.gcno
        -:    0:Data:helloSeven.gcda
        -:    0:Runs:1
        -:    1:#include <stdio.h>
        -:    2:
        1:    3:void timeSeven(int count){
      101:    4:        for(int i=0; i<count; i++){
      100:    5:                if(i%7 == 0){
       15:    6:                        printf("i=%d is times of seven.\n",i);
        -:    7:                }else{
       85:    8:                        printf("i=%d is not times of 7.\n",i);
        -:    9:
        -:   10:                }
        -:   11:        }
        1:   12:}
        -:   13:
        -:   14:
        1:   15:int main(){
        -:   16:
        1:   17:        timeSeven(100);
        1:   18:        return 0;
        -:   19:}
        -:   20:
        -:   21:
        -:   22:
        -:   23:
        -:   24:

每一行起头都是大概有个冒号组合  100:  或 -:

100:  表示该语句执行了100次;

-:       表示本行不可执行;

0:      表示本行可执行,但是本次测试中,没有执行到,没有覆盖到的逻辑。

四、增加可读性,使用lcov

1. 安装lcov,lcov会调用gcov

$ sudo apt install lcov

2.使用 lcov 生成 .info 文件

$ lcov -d . -t 'Test coverage' -o 'helloSeven.info' -b . -c
Capturing coverage data from .
Found gcov version: 9.4.0
Using intermediate gcov format
geninfo: WARNING: invalid characters removed from testname!
Scanning . for .gcda files ...
Found 1 data files in .
Processing helloSeven.gcda
Finished .info-file creation


$ ls
helloSeven  helloSeven.c  helloSeven.c.gcov  helloSeven.gcda  helloSeven.gcno  helloSeven.info

3. 将 .info 生成 .html 报告形式,便于查看

$ genhtml -o helloSeven_result helloSeven.info


$ ls
helloSeven  helloSeven.c  helloSeven.c.gcov  helloSeven.gcda  helloSeven.gcno  helloSeven.info  helloSeven_result
$ ls helloSeven_result/
amber.png  emerald.png  gcov.css  glass.png  hello_gcov  index.html  index-sort-f.html  index-sort-l.html  ruby.png  snow.png  updown.png
$

——————————————————————————————————————————

与上半部分无关联

五、Ubuntu 中从 Linux 源码安装perf

1.安装:

$ sudo apt-get install linux-source
$ cd /usr/src/linux-source-5.4.0/
$ sudo tar -jxf linux-source-5.4.0.tar.bz2
opencl@opencl-PC:/usr/src/linux-source-5.4.0$ cd linux-source-5.4.0/
$ cd tools/perf/
$ make
$ sudo make install


将 perf perf-*  trace 等 复制到 /usr/local/bin/中去,使其可使用超级用户权限执行

$ perf list


$ perf list

List of pre-defined events (to be used in -e):

  duration_time                                      [Tool event]

  branch-instructions OR cpu/branch-instructions/    [Kernel PMU event]
  branch-misses OR cpu/branch-misses/                [Kernel PMU event]
  bus-cycles OR cpu/bus-cycles/                      [Kernel PMU event]
  cache-misses OR cpu/cache-misses/                  [Kernel PMU event]
  cache-references OR cpu/cache-references/          [Kernel PMU event]


...

/*****************************************************

使用perf list命令可以列出所有能够触发perf采样点的事件。

(1)不同的系统会列出不同的结果该列表很。但是按照事件类型都可以归为以下三类:

1)Hardware Event:由PMU硬件产生的事件,比如cache命中;

2)Software Event:内核软件产生的事件,比如进程切换;

3)Trachpoint Event:内核中的静态 tracepoint 所触发的事件,这些 tracepoint 用来判断程序运行期间内核的行为细节,比如 slab 分配器的分配次数等。

(2)同时还可以显示特定模块支持的perf事件:hw/cache/pmu都是硬件相关的;tracepoint基于内核的ftrace;sw实际上是内核计数器。

1)hw/hardware显示支持的硬件事件相关,如: perf list hardware

2)sw/software显示支持的软件事件列表: perf list sw

3)cache/hwcache显示硬件cache相关事件列表: perf list cache

4)pmu显示支持的PMU事件列表: perf list pmu  
5) tracepoint显示支持的所有tracepoint列表,这个列表就比较庞大: perf list tracepoint
————————————————
版权声明:本文为CSDN博主「焱齿」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/mijichui2153/article/details/103225756

*******************************************************/


2.perf top 

查看整个系统的性能信息:

$ sudo perf top

查看某个进程的性能信息,通过pid:

$ perf top --pid 12345
$ perf top -p 12345

1. Annotate  函数的反汇编和汇编指令的热点
2. Zoom into perf DSO  当前DSO的热点
3. Expand callchain  一级调用关系
4. 函数符号内存映射

$ sudo perf top --call-graph graph


3.perf stat


$ sudo perf stat -e cycles        ....
$ sudo perf stat -e instructions  ....


4.perf record & perf report


编译执行:

$ gcc -g -O0 helloSeven.c -o helloSeven
$ sudo perf record -a -g ./helloSeven
$ ls
helloSeven  helloSeven.c  perf.data

$ perf report


5.其他命令:

$ perf bench
$ perf mem
$ perf timechart

6.perf 原理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值