blob: 5097ef36ecc333a6ac9a8a915c3784844fb417db [file] [log] [blame] [view]
Max Moroz3a928902018-05-15 14:39:501# Code Coverage in Chromium
2
Roberto Carrillo5221fc12019-01-30 21:34:503### Coverage Dashboard: [link](https://analysis.chromium.org/p/chromium/coverage)
Yuke Liaod3b46272018-03-14 18:25:144
Yuke Liao1ffc8cb62018-04-06 19:09:075Table of contents:
Max Moroz3a928902018-05-15 14:39:506
Roberto Carrillo3b567862019-01-30 19:01:257- [Coverage Infrastructure](#coverage-infra)
8 * [Coverage Builders](#coverage-builders)
9 * [Coverage Service](#coverage-service)
10 * [Coverage Clients](#coverage-clients)
Roberto Carrillobc1560e2019-01-30 20:08:5811- [Local Coverage Script](#local-coverage-script)
Yuke Liao1ffc8cb62018-04-06 19:09:0712 * [Step 0 Download Tooling](#step-0-download-tooling)
13 * [Step 1 Build](#step-1-build)
14 * [Step 2 Create Raw Profiles](#step-2-create-raw-profiles)
15 * [Step 3 Create Indexed Profile](#step-3-create-indexed-profile)
16 * [Step 4 Create Coverage Reports](#step-4-create-coverage-reports)
Max Morozd73e45f2018-04-24 18:32:4717- [Contacts](#contacts)
18- [FAQ](#faq)
Yuke Liaod3b46272018-03-14 18:25:1419
Roberto Carrillo3b567862019-01-30 19:01:2520Chromium uses source-based code coverage for clang-compiled languages such as
21C++. This [documentation] explains how to use Clangs source-based coverage
22features in general.
Yuke Liaod3b46272018-03-14 18:25:1423
Roberto Carrillo3b567862019-01-30 19:01:2524In this document, we first introduce the code coverage infrastructure that
25continuously generates code coverage information for the whole codebase and for
26specific CLs in Gerrit. For the latter, refer to
Roberto Carrillo5221fc12019-01-30 21:34:5027[code\_coverage\_in\_gerrit.md](code_coverage_in_gerrit.md).
Roberto Carrillo3b567862019-01-30 19:01:2528We then present a script that can be used to locally generate code coverage
29reports with one command, and finally we provide a description of the
30process of producing these reports.
31
32## Coverage Infrastructure
33
34![coverage infra diagram]
35
36There are 3 layers in the system:
37
38### Coverage Builders
39
40The first layer is the LUCI builders that
41 - build instrumented targets,
42 - run the instrumented tests,
43 - merge the results into single streams,
44 - upload data to cloud storage.
45
46There are two types of builder:
47
48CI Builder
49
50The Code-coverage CI Builders periodically build all the test targets and fuzzer
51targets for a given platform and instrument all available source files. Then
52save the coverage data to a dedicated storage bucket.
53
54CQ Builder
55
56The code coverage CQ builders instrument only the files changed for a given CL.
57more information about per-cl coverage info in [this
Roberto Carrillo5221fc12019-01-30 21:34:5058doc](code_coverage_in_gerrit.md).
Roberto Carrillo3b567862019-01-30 19:01:2559
60### Coverage Service
61
62The second layer in the system consists of an AppEngine application that
63consumes the coverage data from the builders above, structures it and stores it
64in cloud datastore. It then serves the information to the clients below.
65
66### Coverage Clients
67
68In the last layer we currently have two clients that consume the service:
69
70#### Coverage Dashboard
71
72The [coverage dashboard] front end is hosted in the same application as the
73service above.
74It shows the full-code coverage reports with links to the builds that generated
75them, as well as per-directory and per-component aggregation, and can be drilled
76down to the single line of code level of detail.
77
78Refer tho the following screenshots, or this [18-second video
Roberto Carrillo5221fc12019-01-30 21:34:5079tutorial](https://www.youtube.com/watch?v=eX7im2_3YfA).
Roberto Carrillo3b567862019-01-30 19:01:2580
81##### Project View
82
83![coverage dashboard main view]
84
85##### Directory View
86
87![coverage dashboard directory view]
88
89##### Component View
90
91![coverage dashboard component view]
92
93##### Source View
94
95When you click on a particular source file in one of the views above, you can check
96per-line coverage information such as
97
98- Uncovered / Covered line fragments, lines and code blocks. This information can be
99useful to identify areas of code that lack test coverage.
100- Per-line hit counts indicating how many times this line was hit by all tested targets.
101This information can be useful to determine hot spots in your code.
102- Potentially dead code. See [dead code example].
103
104![coverage dashboard file view]
105
106#### Gerrit Coverage View
107
108The other client supported at the moment is the gerrit plugin for code coverage.
109
110![gerrit coverage view]
111
112See [this doc](code_coverage_in_gerrit.md) for information about the feature
113that allows gerrit to display code coverage information generated for a given CL
114by CQ bot. Or see this
Roberto Carrillo5221fc12019-01-30 21:34:50115[15-second video tutorial](https://www.youtube.com/watch?v=cxXlYcSgIPE).
Yuke Liaod3b46272018-03-14 18:25:14116
Roberto Carrillobc1560e2019-01-30 20:08:58117## Local Coverage Script
Yuke Liao1ffc8cb62018-04-06 19:09:07118The [coverage script] automates the process described below and provides a
Roberto Carrillo3b567862019-01-30 19:01:25119one-stop service to generate code coverage reports locally in just one command.
Yuke Liaod3b46272018-03-14 18:25:14120
Yuke Liao1ffc8cb62018-04-06 19:09:07121This script is currently supported on Linux, Mac, iOS and ChromeOS platforms.
122
123Here is an example usage:
124
Yuke Liaod3b46272018-03-14 18:25:14125```
Yuke Liao1ffc8cb62018-04-06 19:09:07126$ gn gen out/coverage \
Max Moroza5a95272018-08-31 16:20:55127 --args='use_clang_coverage=true is_component_build=false dcheck_always_on=true'
Yuke Liao1ffc8cb62018-04-06 19:09:07128$ python tools/code_coverage/coverage.py \
129 crypto_unittests url_unittests \
130 -b out/coverage -o out/report \
131 -c 'out/coverage/crypto_unittests' \
132 -c 'out/coverage/url_unittests --gtest_filter=URLParser.PathURL' \
133 -f url/ -f crypto/
134```
135The command above builds `crypto_unittests` and `url_unittests` targets and then
Max Moroza5a95272018-08-31 16:20:55136runs them individually with their commands and arguments specified by the `-c` flag.
Abhishek Aryaaf9811f22018-05-11 22:17:48137For `url_unittests`, it only runs the test `URLParser.PathURL`. The coverage report
Yuke Liao1ffc8cb62018-04-06 19:09:07138is filtered to include only files and sub-directories under `url/` and `crypto/`
139directories.
140
Abhishek Aryaaf9811f22018-05-11 22:17:48141Aside from automating the process, this script provides visualization features to
Roberto Carrillo3b567862019-01-30 19:01:25142view code coverage breakdown by directories and by components, similar to the
143views in the [coverage dashboard](#coverage-dashboard) above.
Abhishek Aryaaf9811f22018-05-11 22:17:48144
Yuke Liao1ffc8cb62018-04-06 19:09:07145## Workflow
146This section presents the workflow of generating code coverage reports using two
147unit test targets in Chromium repo as an example: `crypto_unittests` and
148`url_unittests`, and the following diagram shows a step-by-step overview of the
149process.
150
151![code coverage generation workflow](images/code_coverage_workflow.png)
152
153### Step 0 Download Tooling
154Generating code coverage reports requires llvm-profdata and llvm-cov tools.
155Currently, these two tools are not part of Chromiums Clang bundle,
156[coverage script] downloads and updates them automatically, you can also
Roberto Carrillo3b567862019-01-30 19:01:25157download the tools manually ([tools link]).
Yuke Liao1ffc8cb62018-04-06 19:09:07158
159### Step 1 Build
160In Chromium, to compile code with coverage enabled, one needs to add
161`use_clang_coverage=true` and `is_component_build=false` GN flags to the args.gn
162file in the build output directory. Under the hood, they ensure
163`-fprofile-instr-generate` and `-fcoverage-mapping` flags are passed to the
164compiler.
165
166```
167$ gn gen out/coverage \
168 --args='use_clang_coverage=true is_component_build=false'
169$ gclient runhooks
Max Morozf5b31fcd2018-08-10 21:55:48170$ autoninja -C out/coverage crypto_unittests url_unittests
Yuke Liaod3b46272018-03-14 18:25:14171```
172
Yuke Liao1ffc8cb62018-04-06 19:09:07173### Step 2 Create Raw Profiles
Yuke Liaobc35726b2018-10-31 22:16:21174The next step is to run the instrumented binaries. When the program exits, it
Abhishek Aryaaf9811f22018-05-11 22:17:48175writes a raw profile for each process. Because Chromium runs tests in
176multiple processes, the number of processes spawned can be as many as a few
177hundred, resulting in the generation of a few hundred gigabytes raw
178profiles. To limit the number of raw profiles, `%Nm` pattern in
Yuke Liao1ffc8cb62018-04-06 19:09:07179`LLVM_PROFILE_FILE` environment variable is used to run tests in multi-process
180mode, where `N` is the number of raw profiles. With `N = 4`, the total size of
181the raw profiles are limited to a few gigabytes.
182
183```
184$ export LLVM_PROFILE_FILE=”out/report/crypto_unittests.%4m.profraw”
185$ ./out/coverage/crypto_unittests
186$ ls out/report/
187crypto_unittests.3657994905831792357_0.profraw
188...
189crypto_unittests.3657994905831792357_3.profraw
190```
191
192### Step 3 Create Indexed Profile
193Raw profiles must be indexed before generating code coverage reports, and this
194is done using the `merge` command of `llvm-profdata` tool, which merges multiple
Abhishek Aryaaf9811f22018-05-11 22:17:48195raw profiles (.profraw) and indexes them to create a single profile (.profdata).
Yuke Liao1ffc8cb62018-04-06 19:09:07196
197At this point, all the raw profiles can be thrown away because their information
Abhishek Aryaaf9811f22018-05-11 22:17:48198is already contained in the indexed profile.
Yuke Liao1ffc8cb62018-04-06 19:09:07199
200```
201$ llvm-profdata merge -o out/report/coverage.profdata \
202 out/report/crypto_unittests.3657994905831792357_0.profraw
203...
204out/report/crypto_unittests.3657994905831792357_3.profraw
205out/report/url_unittests.714228855822523802_0.profraw
206...
207out/report/url_unittests.714228855822523802_3.profraw
208$ ls out/report/coverage.profdata
209out/report/coverage.profdata
210```
211
212### Step 4 Create Coverage Reports
213Finally, `llvm-cov` is used to render code coverage reports. There are different
Abhishek Aryaaf9811f22018-05-11 22:17:48214report generation modes, and all of them require the following as input:
215- Indexed profile
216- All built target binaries
Roberto Carrillo5221fc12019-01-30 21:34:50217- All exercised source files
Yuke Liao1ffc8cb62018-04-06 19:09:07218
Abhishek Aryaaf9811f22018-05-11 22:17:48219For example, the following command can be used to generate per-file line-by-line
Yuke Liao1ffc8cb62018-04-06 19:09:07220code coverage report:
221
222```
223$ llvm-cov show -output-dir=out/report -format=html \
224 -instr-profile=out/report/coverage.profdata \
225 -object=out/coverage/url_unittests \
226 out/coverage/crypto_unittests
227```
228
229For more information on how to use llvm-cov, please refer to the [guide].
Yuke Liaod3b46272018-03-14 18:25:14230
Max Morozd73e45f2018-04-24 18:32:47231## Contacts
232
233### Reporting problems
Yuke Liaod3b46272018-03-14 18:25:14234For any breakage report and feature requests, please [file a bug].
235
Max Morozd73e45f2018-04-24 18:32:47236### Mailing list
Yuke Liaobc35726b2018-10-31 22:16:21237For questions and general discussions, please join [code-coverage group].
Yuke Liao1ffc8cb62018-04-06 19:09:07238
Max Morozd73e45f2018-04-24 18:32:47239## FAQ
240
241### Can I use `is_component_build=true` for code coverage build?
242
243Yes, code coverage instrumentation works with both component and non-component
244builds. Component build is usually faster to compile, but can be up to several
245times slower to run with code coverage instrumentation. For more information,
Max Morozc5e364a2018-04-25 23:19:49246see [crbug.com/831939].
247
248### I am getting some warnings while using the script, is that fine?
249
Abhishek Aryaaf9811f22018-05-11 22:17:48250Usually this is not a critical issue, but in general we tend not to have any
Max Morozc5e364a2018-04-25 23:19:49251warnings. Please check the list of [known issues], and if there is a similar
252bug, leave a comment with the command you run, the output you get, and Chromium
253revision you use. Otherwise, please [file a new issue] providing the same
254information.
255
256### How do crashes affect code coverage?
257
Max Moroza5a95272018-08-31 16:20:55258If a crash of any type occurs (e.g. Segmentation Fault or ASan error), the
259crashing process might not dump coverage information necessary to generate
Max Morozc5e364a2018-04-25 23:19:49260code coverage report. For single-process applications (e.g. fuzz targets), that
Max Moroza5a95272018-08-31 16:20:55261means no coverage might be reported at all. For multi-process applications, the
262report might be incomplete. It is important to fix the crash first. If this is
Abhishek Aryaaf9811f22018-05-11 22:17:48263happening only in the coverage instrumented build, please [file a bug].
Max Morozd73e45f2018-04-24 18:32:47264
Max Moroza5a95272018-08-31 16:20:55265### How do assertions affect code coverage?
266
267If a crash is caused by CHECK or DCHECK, the coverage dump will still be written
268on the disk ([crrev.com/c/1172932]). However, if a crashing process calls the
269standard [assert] directly or through a custom wrapper, the dump will not be
270written (see [How do crashes affect code coverage?]).
271
Max Moroz63cd04d2018-05-02 16:40:23272### Is it possible to obtain code coverage from a full Chromium build?
273
274Yes, with some important caveats. It is possible to build `chrome` target with
275code coverage instrumentation enabled. However, there are some inconveniences
276involved:
277
278* Linking may take a while
279* The binary is huge (~4GB)
280* The browser "works", but is noticeably slow and laggy
281* The sandbox needs to be disabled (`--no-sandbox`)
Max Moroz63cd04d2018-05-02 16:40:23282
283For more information, please see [crbug.com/834781].
284
Max Moroz3a928902018-05-15 14:39:50285### Why do we see significantly different coverage reported on different revisions?
286
287There can be two possible scenarios:
288
289* It can be a one time flakiness due to a broken build or failing tests.
290* It can be caused by extension of the test suite used for generating code
291coverage reports. When we add new tests to the suite, the aggregate coverage
292reported usually grows after that.
293
294### How can I improve [coverage dashboard]?
295
296Source code of the dashboard is not open sourced at the moment, but if you are a
297Googler, you should have access to the code-coverage repository. There is a
298documentation and scripts for running it locally. To get access and report
Roberto Carrillo5221fc12019-01-30 21:34:50299issues, ping the [code-coverage group].
Max Moroz3a928902018-05-15 14:39:50300
Roberto Carrillo3b567862019-01-30 19:01:25301The code for the service and dashboard currently lives along with findit at
302[this location](https://chromium.googlesource.com/infra/infra/+/master/appengine/findit/)
303because of significant shared logic.
304
305The code used by the bots that generate the coverage data lives (among other
306places) in the
Roberto Carrillo5221fc12019-01-30 21:34:50307[clang coverage recipe module](https://chromium.googlesource.com/chromium/tools/build/+/master/scripts/slave/recipe_modules/clang_coverage/).
Roberto Carrillo3b567862019-01-30 19:01:25308
Max Moroz3a928902018-05-15 14:39:50309### Why is coverage for X not reported or unreasonably low, even though there is a test for X?
310
311There are several reasons why coverage reports can be incomplete or incorrect:
312
313* A particular test is not used for code coverage report generation. Please
Roberto Carrillo5221fc12019-01-30 21:34:50314[file a bug].
Roberto Carrillo3b567862019-01-30 19:01:25315* A test may have a build failure or a runtime crash. Please check the build
316for that particular report (rightmost column on the [coverage dashboard]).
Max Moroz3a928902018-05-15 14:39:50317If there is any failure, please upload a CL with the fix. If you can't fix it,
318feel free to [file a bug].
319* A particular test may not be available on a particular platform. As of now,
320only reports generated on Linux are available on the [coverage dashboard].
321
Max Moroz3a928902018-05-15 14:39:50322### Is coverage reported for the code executed inside the sandbox?
323
324Not at the moment until [crbug.com/842424] is resolved. We do not disable the
325sandbox when running the tests. However, if there are any other non-sandbox'ed
326tests for the same code, the coverage should be reported from those. For more
327information, see [crbug.com/842424].
328
Max Morozd73e45f2018-04-24 18:32:47329
Max Moroza5a95272018-08-31 16:20:55330[assert]: http://man7.org/linux/man-pages/man3/assert.3.html
Yuke Liaobc35726b2018-10-31 22:16:21331[code-coverage group]: https://groups.google.com/a/chromium.org/forum/#!forum/code-coverage
Max Moroz3a928902018-05-15 14:39:50332[code-coverage repository]: https://chrome-internal.googlesource.com/chrome/tools/code-coverage
Roberto Carrillo3b567862019-01-30 19:01:25333[coverage dashboard]: https://analysis.chromium.org/p/chromium/coverage
Yuke Liao1ffc8cb62018-04-06 19:09:07334[coverage script]: https://cs.chromium.org/chromium/src/tools/code_coverage/coverage.py
Roberto Carrillo3b567862019-01-30 19:01:25335[coverage infra diagram]: images/code_coverage_infra_diagram.png
336[coverage dashboard file view]: images/code_coverage_dashboard_file_view.png
337[coverage dashboard component view]: images/code_coverage_dashboard_component_view.png
338[coverage dashboard directory view]: images/code_coverage_dashboard_directory_view.png
339[coverage dashboard main view]: images/code_coverage_dashboard_main_view.png
Max Moroz3a928902018-05-15 14:39:50340[crbug.com/821617]: https://crbug.com/821617
Max Morozc5e364a2018-04-25 23:19:49341[crbug.com/831939]: https://crbug.com/831939
Max Moroz63cd04d2018-05-02 16:40:23342[crbug.com/834781]: https://crbug.com/834781
Max Moroz3a928902018-05-15 14:39:50343[crbug.com/842424]: https://crbug.com/842424
Max Moroza5a95272018-08-31 16:20:55344[crrev.com/c/1172932]: https://crrev.com/c/1172932
Max Moroz3a928902018-05-15 14:39:50345[clang roll]: https://crbug.com/841908
Abhishek Aryab23b1a72018-05-17 20:11:09346[dead code example]: https://chromium.googlesource.com/chromium/src/+/ac6e09311fcc7e734be2ef21a9ccbbe04c4c4706
Max Morozc5e364a2018-04-25 23:19:49347[documentation]: https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
Yuke Liao1ffc8cb62018-04-06 19:09:07348[file a bug]: https://bugs.chromium.org/p/chromium/issues/entry?components=Tools%3ECodeCoverage
Max Morozc5e364a2018-04-25 23:19:49349[file a new issue]: https://bugs.chromium.org/p/chromium/issues/entry?components=Tools%3ECodeCoverage
Roberto Carrillo3b567862019-01-30 19:01:25350[gerrit coverage view]: images/code_coverage_uncovered_lines.png
Max Morozc5e364a2018-04-25 23:19:49351[guide]: http://llvm.org/docs/CommandGuide/llvm-cov.html
Max Moroza5a95272018-08-31 16:20:55352[How do crashes affect code coverage?]: #how-do-crashes-affect-code-coverage
Max Morozc5e364a2018-04-25 23:19:49353[known issues]: https://bugs.chromium.org/p/chromium/issues/list?q=component:Tools%3ECodeCoverage
Roberto Carrillo3b567862019-01-30 19:01:25354[tools link]: https://storage.googleapis.com/chromium-browser-clang-staging/
Max Moroz3a928902018-05-15 14:39:50355[test suite]: https://cs.chromium.org/chromium/src/tools/code_coverage/test_suite.txt