[Tools] GCOV & LCOV 初探
[Tools] gcov & lcov
gcov 是一套代碼覆蓋率的工具,可以有效地查閱分析程式,找出程式中重載的部分,它也是 gcc 中的內建工具。
lcov 則是可以將 gcov 所分析完的結果,轉譯成 html 。
Required Packages
$ apt update
$ apt upgrade -y
$ apt install gcov lcov
gcov
$ gcov -h
Usage: gcov [OPTION]... SOURCE|OBJ...
Print code coverage information.
-h, --help Print this help, then exit
-v, --version Print version number, then exit
-a, --all-blocks Show information for every basic block
-b, --branch-probabilities Include branch probabilities in output
-c, --branch-counts Given counts of branches taken
rather than percentages
-n, --no-output Do not create an output file
-l, --long-file-names Use long output file names for included
source files
-f, --function-summaries Output summaries for each function
-o, --object-directory DIR|FILE Search for object files in DIR or called FILE
-s, --source-prefix DIR Source prefix to elide
-r, --relative-only Only show data for relative sources
-p, --preserve-paths Preserve all pathname components
-u, --unconditional-branches Show unconditional branch counts too
-d, --display-progress Display progress information
For bug reporting instructions, please see:
<file:///usr/share/doc/gcc-4.8/README.Bugs>.
Example
練習一下怎麼使用 gcov 與 lcov
// test.c
#include <stdio.h>
int main(int argc, char *argv[])
{
int i = 0;
for(i = 0; i < 10; i++)
printf("%d\r\n", i);
return 0;
}
To generate the requirement file for gcov.
透過 gcc 並搭配參數 -ftest-coverage -fprofile-arcs
,產生 .gcno 與 .o
檔案
$ gcc -c test.c -ftest-coverage -fprofile-arcs
To produce executable file test
利用上一步驟產生的 .o
,產生可分析的執行檔
$ gcc test.o -o test --coverage -lgcov -fprofile-arcs
To Execute program and produce .gcda file
執行程式時,系統會自動產生一份 .gcda
檔案
$ ./test
To generate .gcov file
使用 gcov 命令,並搭配參數,產生 .gcov
檔案,該檔案會記錄詳細程式碼執行的次數。
$ gcov test.c -a -b -f
test.gcno:version '409*', prefer '408*'
test.gcda:version '409*', prefer version '408*'
Function 'main'
Lines executed:100.00% of 5
No branches
No calls
File 'test.c'
Lines executed:100.00% of 5
Branches executed:100.00% of 2
Taken at least once:100.00% of 2
Calls executed:100.00% of 1
Creating 'test.c.gcov'
To Open .gcov
-: 0:Source:test.c
-: 0:Graph:test.gcno
-: 0:Data:test.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include <stdio.h>
-: 2:
function main called 1 returned 100% blocks executed 100%
1: 3:int main(int argc, char *argv[])
1: 3-block 0
-: 4:{
1: 5: int i = 0;
11: 6: for(i = 0; i < 10; i++)
1: 6-block 0
11: 6-block 1
branch 0 taken 91%
branch 1 taken 9% (fallthrough)
10: 7: printf("%d\r\n", i);
10: 7-block 0
call 0 returned 100%
1: 8: return 0;
1: 8-block 0
-: 9:}
To Generate test.info
$ lcov -c -o test.info -d .
Capturing coverage data from .
Found gcov version: 4.8.5
Scanning . for .gcda files ...
Found 1 data files in .
Processing test.gcda
/home/robert/Project/test/test/test.gcno:version '409*', prefer '408*'
/home/robert/Project/test/test/test.gcda:version '409*', prefer version '408*'
Finished .info-file creation
To convert it to html report
$ genhtml test.info -o result
Reading data file test.info
Found 1 entries.
Found common filename prefix "/home/robert/Project/test"
Writing .css and .png files.
Generating output.
Processing file test/test.c
Writing directory view page.
Overall coverage rate:
lines......: 100.0% (5 of 5 lines)
functions..: 100.0% (1 of 1 function)
To view the tree of converted files.
$ tree
.
├── amber.png
├── emerald.png
├── gcov.css
├── glass.png
├── index.html
├── index-sort-f.html
├── index-sort-l.html
├── ruby.png
├── snow.png
├── test
│ ├── index.html
│ ├── index-sort-f.html
│ ├── index-sort-l.html
│ ├── test.c.func.html
│ ├── test.c.func-sort-c.html
│ └── test.c.gcov.html
└── updown.png
Analysis the program
If you want to analy the code, please do not optimize it.
Please Not to use -O2
with GCC. You should use -O0
Summary
透過該分析工具,找出系統中重載的部分,查看是否可以將此優化,讓系統運行時,可以達到降低系統負載。
留言
張貼留言