1fc6ff07fSWei Liu# Code coverage 2fc6ff07fSWei Liu 3fc6ff07fSWei LiuLLVM provides a set of tools to collect code coverage data and present the data 4fc6ff07fSWei Liuin human-consumable forms. 5fc6ff07fSWei Liu 6fc6ff07fSWei Liu## Building a suitable binary 7fc6ff07fSWei Liu 8fc6ff07fSWei LiuThe compiler flag to generate code coverage data has been stabilized since Rust 9fc6ff07fSWei Liu1.60. 10fc6ff07fSWei Liu 11fc6ff07fSWei LiuAn instrumented binary can be built with the following command: 12fc6ff07fSWei Liu 13fc6ff07fSWei Liu```shell 14fc6ff07fSWei Liucargo clean && RUSTFLAGS='-C instrument-coverage' cargo build 15fc6ff07fSWei Liu``` 16fc6ff07fSWei Liu 17fc6ff07fSWei LiuUsing either `debug` or `release` profile is fine. You will need to adjust 18fc6ff07fSWei Liuthe path for some commands. 19fc6ff07fSWei Liu 20fc6ff07fSWei Liu## Running the binary 21fc6ff07fSWei Liu 22fc6ff07fSWei LiuRun the binary as you normally would. When the process exits, you will see 23fc6ff07fSWei Liufiles with the prefix `profraw`. 24fc6ff07fSWei Liu 25fc6ff07fSWei LiuMultiple runs of the same binary will produce multiple `profraw` files. 26fc6ff07fSWei Liu 27fc6ff07fSWei LiuThe more diverse the runs are, the better. Try to exercise different features 28fc6ff07fSWei Liuas much as possible. 29fc6ff07fSWei Liu 30fc6ff07fSWei Liu## Combining raw data 31fc6ff07fSWei Liu 32fc6ff07fSWei LiuRaw data files can be combined with `llvm-profdata`. 33fc6ff07fSWei Liu 34fc6ff07fSWei Liu```shell 35fc6ff07fSWei Liurustup component add llvm-tools-preview 36fc6ff07fSWei Liu# Assuming profraw files reside in the current directory and its children directories 37fc6ff07fSWei Liufind . -name '*.profraw' -exec llvm-profdata merge -sparse {} -o coverage.profdata \; 38fc6ff07fSWei Liu``` 39fc6ff07fSWei Liu 40fc6ff07fSWei LiuA file named `coverage.profdata` will be generated. 41fc6ff07fSWei Liu 42fc6ff07fSWei Liu## Generating HTML files for human consumption 43fc6ff07fSWei Liu 44fc6ff07fSWei LiuThis can be done either with LLVM or `grcov`. 45fc6ff07fSWei Liu 46fc6ff07fSWei LiuHere is an example using grcov. 47fc6ff07fSWei Liu 48fc6ff07fSWei Liu```shell 49fc6ff07fSWei Liucargo install grcov 50fc6ff07fSWei Liu# Assuming the profdata file is in the top level directory of the Cloud Hypervisor repository 51fc6ff07fSWei Liugrcov . --binary-path ./target/x86_64-unknown-linux-gnu/release -s . -t html --branch --ignore-not-existing -o coverage-html-output/ 52fc6ff07fSWei Liu``` 53fc6ff07fSWei Liu 54fc6ff07fSWei LiuYou can then open the `index.html` file under coverage-html-output to see the 55fc6ff07fSWei Liuresults. 5661134833SWei Liu 5761134833SWei Liu## Notes on running the in-tree integration tests and unit tests 5861134833SWei Liu 5961134833SWei LiuPlease set RUSTFLAGS the same way while invoking `dev_cli.sh`. The script will 6061134833SWei Liupass RUSTFLAGS to the container. 6161134833SWei Liu 6261134833SWei LiuSince the `profraw` files are generated from within the container, the file 6361134833SWei Liupaths embedded in the data files are going to be different. It is easier to do 6461134833SWei Liuthe data processing from within the container if you don't want to fight the 6561134833SWei Liutool chain. 6661134833SWei Liu 6761134833SWei Liu```shell 68*cc5e463eSSongqian Li# Set env to enable code coverage 69*cc5e463eSSongqian Liexport RUSTFLAGS="-Cinstrument-coverage" 70*cc5e463eSSongqian Liexport LLVM_PROFILE_FILE="ch-%p-%m.profraw" 7161134833SWei Liu 72*cc5e463eSSongqian Li# Run unit tests 73*cc5e463eSSongqian Liscripts/dev_cli.sh tests --unit --libc gnu 7461134833SWei Liu 75*cc5e463eSSongqian Li# Run integration tests 76*cc5e463eSSongqian Liscripts/dev_cli.sh tests --integration --libc gnu 77*cc5e463eSSongqian Liscripts/dev_cli.sh tests --integration-live-migration --libc gnu 78*cc5e463eSSongqian Li 79*cc5e463eSSongqian Li# Export code coverage report 80*cc5e463eSSongqian Liscripts/dev_cli.sh tests --coverage -- -- html 8161134833SWei Liu``` 82