Yuke Liao | bb571bd6 | 2018-10-31 21:51:52 | [diff] [blame] | 1 | # The Clang Code Coverage Wrapper |
| 2 | |
| 3 | The Clang code coverage wrapper |
| 4 | ([//build/toolchain/clang_code_coverage_wrapper.py]) is a compiler wrapper that |
| 5 | adds code coverage flags to the invocations of Clang C/C++ compiler, which can |
| 6 | be used to instrument and generate code coverage data for a subset of the source |
| 7 | files. The main use case of this wrapper is to generate code coverage reports |
| 8 | for changed files at a per-CL level during try jobs. |
| 9 | |
| 10 | One might ask: why this compiler wrapper instead of just instrumenting all the |
| 11 | source files? |
| 12 | |
| 13 | Efficiency is the main consideration because comparing to instrument everything, |
| 14 | only instrument what-is-needed takes less time to build the test targets, |
| 15 | produces binaries that are 10 times smaller, run tests twice faster and |
| 16 | generates coverage reports significantly faster. An experiment was done to |
| 17 | evaluate the effectiveness of instrument what-is-needed, and in the experiment, |
| 18 | `unit_tests` is used as the sample test target and 8 randomly chosen C++ source |
| 19 | files are used as the samples files to instrument. The results are presented in |
| 20 | the following table: |
| 21 | |
| 22 | | Type | Build time | Binary size | Tests run time | Reports generation time | |
| 23 | |--------------------|------------|-------------|----------------|-------------------------| |
| 24 | | No instrumentation | 43m | 240M | 40s | 0s | |
| 25 | | What-is-needed | 43m | 241M | 40s | 0.14s | |
| 26 | | Everything | 53m | 2.3G | 80s | 1m10s | |
| 27 | |
| 28 | It can be seen from the results that the overhead introduced by instrument |
| 29 | everything is huge, while it's negligible for instrument what-is-needed. |
| 30 | |
| 31 | ## How to use the coverage wrapper? |
| 32 | To get the coverage wrapper hook into your build, add the following flags to |
Yuke Liao | 9619c71 | 2018-11-02 07:18:09 | [diff] [blame] | 33 | your `args.gn` file, assuming build directory is `out/Release`. |
Yuke Liao | bb571bd6 | 2018-10-31 21:51:52 | [diff] [blame] | 34 | |
| 35 | ``` |
| 36 | use_clang_coverage = true |
Yuke Liao | 9619c71 | 2018-11-02 07:18:09 | [diff] [blame] | 37 | coverage_instrumentation_input_file = "//out/Release/coverage_instrumentation_input.txt" |
Yuke Liao | bb571bd6 | 2018-10-31 21:51:52 | [diff] [blame] | 38 | ``` |
| 39 | |
Yuke Liao | 9619c71 | 2018-11-02 07:18:09 | [diff] [blame] | 40 | The path to the coverage instrumentation input file should be a source root |
| 41 | absolute path, and the file consists of multiple lines where each line |
Yuke Liao | bb571bd6 | 2018-10-31 21:51:52 | [diff] [blame] | 42 | represents a path to a source file, and the specified paths must be relative to |
| 43 | the root build directory. e.g. `../../base/task/post_task.cc` for build |
| 44 | directory `out/Release`. |
| 45 | |
| 46 | Then, use the [//tools/code_coverage/coverage.py] script to build, run tests and |
| 47 | generate code coverage reports for the exercised files. |
| 48 | |
| 49 | ## Caveats |
| 50 | One caveat with this compiler wrapper is that it may introduce unexpected |
| 51 | behaviors in incremental builds when the file path to the coverage |
| 52 | instrumentation input file changes between consecutive runs, and the reason is |
| 53 | that the coverage instrumentation input file is explicitly passed to the |
| 54 | coverage wrapper as a command-line argument, which makes the path part of the |
| 55 | Ninja commands used to compile the source files, so change of the path between |
| 56 | consecutive runs causes Ninja to perform a full rebuild. |
| 57 | |
| 58 | Due to the reasons mentioned above, users of this script are strongly advised to |
| 59 | always use the same path such as |
| 60 | `${root_build_dir}/coverage_instrumentation_input.txt`. |
| 61 | |
| 62 | ## Want to learn more about code coverage in Chromium? |
| 63 | For more background information on how code coverage works in Chromium, please |
| 64 | refer to [code_coverage.md] |
| 65 | |
| 66 | ## Contacts |
| 67 | |
| 68 | ### Reporting problems |
| 69 | We're still evaluating the tools, for any breakage report and feature requests, |
| 70 | please [file a bug]. |
| 71 | |
| 72 | ### Mailing list |
| 73 | For questions and general discussions, please join [code-coverage group]. |
| 74 | |
| 75 | [//build/toolchain/clang_code_coverage_wrapper.py]: ../build/toolchain/clang_code_coverage_wrapper.py |
Yuke Liao | 43bbbcd5 | 2019-06-21 19:34:50 | [diff] [blame] | 76 | [code_coverage.md]: testing/code_coverage.md |
Yuke Liao | bb571bd6 | 2018-10-31 21:51:52 | [diff] [blame] | 77 | [//tools/code_coverage/coverage.py]: ../tools/code_coverage/coverage.py |
Yuke Liao | 03c64407 | 2019-07-30 18:33:40 | [diff] [blame] | 78 | [file a bug]: https://bugs.chromium.org/p/chromium/issues/entry?components=Infra%3ETest%3ECodeCoverage |
Yuke Liao | bb571bd6 | 2018-10-31 21:51:52 | [diff] [blame] | 79 | [code-coverage group]: https://groups.google.com/a/chromium.org/forum/#!forum/code-coverage |