xref: /aosp_15_r20/kernel/tests/tools/coverage_howto.md (revision 2f2c4c7ab4226c71756b9c31670392fdd6887c4f)
1HOW TO COLLECT KERNEL CODE COVERAGE FROM A TRADEFED TEST RUN
2============================================================
3
4
5## Build and use a kernel with GCOV profile enabled
6Build your kernel with the [`--gcov`](https://android.googlesource.com/kernel/build/+/refs/heads/main/kleaf/docs/gcov.md) option to enable
7GCOV profiling from the kernel. This will also trigger the build to save the required *.gcno files needed to viewing the collected count data.
8
9For example to build a Cuttlefish (CF) kernel with GCOV profiling enabled run:
10```
11$ bazel run --gcov //common-modules/virtual-device:virtual_device_x86_64_dist
12```
13
14## Run your test(s) using tradefed.sh with kernel coverage collection enabled
15'tradefed.sh' can be used to run a number of different types of tests. Adding the appropriate coverage flags
16to the tradefed call will trigger tradefed to take care of mounting debugfs, reseting the gcov counts prior
17to test run, and the collection of gcov data files from debugfs after test completion.
18
19These coverage arguments are:
20```
21--coverage --coverage-toolchain GCOV_KERNEL --auto-collect GCOV_KERNEL_COVERAGE
22```
23
24The following is a full example call running just the `kselftest_net_socket` test in the
25selftests test suite that exists under the 'bazel-bin/common/testcases' directory. The artifact
26output has been redirected to 'tf-logs' for easier reference needed in the next step.
27```
28$ prebuilts/tradefed/filegroups/tradefed/tradefed.sh run commandAndExit \
29    template/local_min --template:map test=suite/test_mapping_suite     \
30    --include-filter 'selftests kselftest_net_socket' --tests-dir=bazel-bin/common/testcases/  \
31    --primary-abi-only --log-file-path tf-logs                          \
32    --coverage --coverage-toolchain GCOV_KERNEL                         \
33    --auto-collect GCOV_KERNEL_COVERAGE
34```
35
36## Create an lcov tracefile out of the gcov tar artifact from test run
37The previously mentioned tradefed run will produce a tar file artifact in the
38tradefed log folder with a name similar to <test>_kernel_coverage_*.tar.gz.
39This tar file is an archive of all the gcov data files collected into debugfs/
40from the profiled device. In order to make it easier to work with this data,
41it needs to be converted to a single lcov tracefile.
42
43The script 'create-tracefile.py' facilitates this generation by handling the
44required unpacking, file path corrections and ultimate 'lcov' call.
45
46An example where we generate a tracefile only including results from net/socket.c.
47(If no source files are specified as included, then all source file data is used):
48```
49$ ./kernel/tests/tools/create-tracefile.py -t tf-logs/ --include net/socket.c
50```
51
52This will create a local tracefile named 'cov.info'.
53
54
55## Visualizing Results
56With the created tracefile there a number of different ways to view coverage data from it.
57Check out 'man lcov' for more options.
58### 1. Text Options
59#### 1.1 Summary
60```
61$ lcov --summary --rc lcov_branch_coverage=1 cov.info
62Reading tracefile cov.info_fix
63Summary coverage rate:
64  lines......: 6.0% (81646 of 1370811 lines)
65  functions..: 9.6% (10285 of 107304 functions)
66  branches...: 3.7% (28639 of 765538 branches)
67```
68#### 1.2 List
69```
70$ lcov --list --rc lcov_branch_coverage=1 cov.info
71Reading tracefile cov.info_fix
72                                               |Lines      |Functions|Branches
73Filename                                       |Rate    Num|Rate  Num|Rate   Num
74================================================================================
75[/usr/local/google/home/joefradley/dev/common-android-mainline-2/common/]
76arch/x86/crypto/aesni-intel_glue.c             |23.9%   623|22.2%  36|15.0%  240
77arch/x86/crypto/blake2s-glue.c                 |50.0%    28|50.0%   2|16.7%   30
78arch/x86/crypto/chacha_glue.c                  | 0.0%   157| 0.0%  10| 0.0%   80
79<truncated>
80virt/lib/irqbypass.c                           | 0.0%   137| 0.0%   6| 0.0%   88
81================================================================================
82                                         Total:| 6.0% 1369k| 9.6%  0M| 3.7% 764k
83```
84### 2. HTML
85The `lcov` tool `genhtml` is used to generate html. To create html with the default settings:
86
87```
88$ genhtml --branch-coverage -o html cov.info
89```
90
91The page can be viewed at `html\index.html`.
92
93Options of interest:
94 * `--frame`: Creates a left hand macro view in a source file view.
95 * `--missed`: Helpful if you want to sort by what source is missing the most as opposed to the default coverage percentages.
96
97
98
99