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