blob: fa43821968be4b5d6e28e04e25a0818c43992d9b [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 Clang’s 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
Yuke Liao43bbbcd52019-06-21 19:34:5050The code coverage CI Builders periodically build all the test targets and fuzzer
Roberto Carrillo3b567862019-01-30 19:01:2551targets 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.
Yuke Liao43bbbcd52019-06-21 19:34:5057More 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
Yuke Liao43bbbcd52019-06-21 19:34:5078Refer to the following screenshots:
Roberto Carrillo3b567862019-01-30 19:01:2579
80##### Directory View
81
Yuke Liao43bbbcd52019-06-21 19:34:5082See coverage breakdown by directories (default landing page).
83
Roberto Carrillo3b567862019-01-30 19:01:2584![coverage dashboard directory view]
85
86##### Component View
87
Yuke Liao43bbbcd52019-06-21 19:34:5088Use the view dropdown menu to switch between directory and component.
89
Roberto Carrillo3b567862019-01-30 19:01:2590![coverage dashboard component view]
91
92##### Source View
93
Yuke Liao43bbbcd52019-06-21 19:34:5094Click on a particular source file in one of the views above to see line-by-line
95coverage breakdown, and it's useful to identify:
96- Uncovered lines and code blocks that lack test coverage.
Roberto Carrillo3b567862019-01-30 19:01:2597- Potentially dead code. See [dead code example].
Yuke Liao43bbbcd52019-06-21 19:34:5098- Hot spots in your code.
Roberto Carrillo3b567862019-01-30 19:01:2599
100![coverage dashboard file view]
101
Yuke Liao43bbbcd52019-06-21 19:34:50102##### Project View
103
104Click on "Previous Reports" to check out the coverage history of the project.
105
106![coverage dashboard link to previous reports]
107
108List of historical coverage reports are in reverse chronological order.
109
110![coverage dashboard previous reports]
111
Roberto Carrillo3b567862019-01-30 19:01:25112#### Gerrit Coverage View
113
114The other client supported at the moment is the gerrit plugin for code coverage.
115
116![gerrit coverage view]
117
118See [this doc](code_coverage_in_gerrit.md) for information about the feature
119that allows gerrit to display code coverage information generated for a given CL
120by CQ bot. Or see this
Roberto Carrillo5221fc12019-01-30 21:34:50121[15-second video tutorial](https://www.youtube.com/watch?v=cxXlYcSgIPE).
Yuke Liaod3b46272018-03-14 18:25:14122
Roberto Carrillobc1560e2019-01-30 20:08:58123## Local Coverage Script
Yuke Liao1ffc8cb62018-04-06 19:09:07124The [coverage script] automates the process described below and provides a
Roberto Carrillo3b567862019-01-30 19:01:25125one-stop service to generate code coverage reports locally in just one command.
Yuke Liaod3b46272018-03-14 18:25:14126
Ben Joyce88282362021-01-29 23:53:31127This script is currently supported on Android, Linux, Mac, iOS and ChromeOS
128platforms.
Yuke Liao1ffc8cb62018-04-06 19:09:07129
130Here is an example usage:
131
Yuke Liaod3b46272018-03-14 18:25:14132```
Yuke Liao1ffc8cb62018-04-06 19:09:07133$ gn gen out/coverage \
Bryce Thomasc9137cf2020-03-31 19:47:17134 --args="use_clang_coverage=true is_component_build=false
135 dcheck_always_on=true is_debug=false"
Yuke Liao1ffc8cb62018-04-06 19:09:07136$ python tools/code_coverage/coverage.py \
137 crypto_unittests url_unittests \
138 -b out/coverage -o out/report \
139 -c 'out/coverage/crypto_unittests' \
140 -c 'out/coverage/url_unittests --gtest_filter=URLParser.PathURL' \
141 -f url/ -f crypto/
142```
143The command above builds `crypto_unittests` and `url_unittests` targets and then
Max Moroza5a95272018-08-31 16:20:55144runs them individually with their commands and arguments specified by the `-c` flag.
Abhishek Aryaaf9811f22018-05-11 22:17:48145For `url_unittests`, it only runs the test `URLParser.PathURL`. The coverage report
Yuke Liao1ffc8cb62018-04-06 19:09:07146is filtered to include only files and sub-directories under `url/` and `crypto/`
147directories.
148
Abhishek Aryaaf9811f22018-05-11 22:17:48149Aside from automating the process, this script provides visualization features to
Roberto Carrillo3b567862019-01-30 19:01:25150view code coverage breakdown by directories and by components, similar to the
151views in the [coverage dashboard](#coverage-dashboard) above.
Abhishek Aryaaf9811f22018-05-11 22:17:48152
Yuke Liao1ffc8cb62018-04-06 19:09:07153## Workflow
154This section presents the workflow of generating code coverage reports using two
155unit test targets in Chromium repo as an example: `crypto_unittests` and
156`url_unittests`, and the following diagram shows a step-by-step overview of the
157process.
158
159![code coverage generation workflow](images/code_coverage_workflow.png)
160
161### Step 0 Download Tooling
162Generating code coverage reports requires llvm-profdata and llvm-cov tools.
Zequan Wu6d00cccd2021-03-15 22:40:44163You can get them by adding `"checkout_clang_coverage_tools": True,` to
164`custom_vars` in the `.gclient` config and run `gclient runhooks`. You can also
165download the tools manually ([tools link])
Yuke Liao1ffc8cb62018-04-06 19:09:07166
167### Step 1 Build
168In Chromium, to compile code with coverage enabled, one needs to add
Yuke Liao8c0868fe62019-10-22 21:02:33169`use_clang_coverage=true`, `is_component_build=false` and `is_debug=false` GN
170flags to the args.gn file in the build output directory. Under the hood, they
171ensure `-fprofile-instr-generate` and `-fcoverage-mapping` flags are passed to
172the compiler.
Yuke Liao1ffc8cb62018-04-06 19:09:07173
174```
175$ gn gen out/coverage \
Yuke Liao8c0868fe62019-10-22 21:02:33176 --args='use_clang_coverage=true is_component_build=false is_debug=false'
Yuke Liao1ffc8cb62018-04-06 19:09:07177$ gclient runhooks
Max Morozf5b31fcd2018-08-10 21:55:48178$ autoninja -C out/coverage crypto_unittests url_unittests
Yuke Liaod3b46272018-03-14 18:25:14179```
180
Yuke Liao1ffc8cb62018-04-06 19:09:07181### Step 2 Create Raw Profiles
Yuke Liaobc35726b2018-10-31 22:16:21182The next step is to run the instrumented binaries. When the program exits, it
Abhishek Aryaaf9811f22018-05-11 22:17:48183writes a raw profile for each process. Because Chromium runs tests in
184multiple processes, the number of processes spawned can be as many as a few
185hundred, resulting in the generation of a few hundred gigabytes’ raw
186profiles. To limit the number of raw profiles, `%Nm` pattern in
Yuke Liao1ffc8cb62018-04-06 19:09:07187`LLVM_PROFILE_FILE` environment variable is used to run tests in multi-process
188mode, where `N` is the number of raw profiles. With `N = 4`, the total size of
Ben Joyce88282362021-01-29 23:53:31189the raw profiles are limited to a few gigabytes. (If working on Android, the
190.profraw files will be located in ./out/coverage/coverage by default.)
Yuke Liao1ffc8cb62018-04-06 19:09:07191
192```
Ben Joyce739fdb202021-01-13 19:22:22193$ export LLVM_PROFILE_FILE="out/report/crypto_unittests.%4m.profraw"
Yuke Liao1ffc8cb62018-04-06 19:09:07194$ ./out/coverage/crypto_unittests
195$ ls out/report/
196crypto_unittests.3657994905831792357_0.profraw
197...
198crypto_unittests.3657994905831792357_3.profraw
199```
200
201### Step 3 Create Indexed Profile
202Raw profiles must be indexed before generating code coverage reports, and this
203is done using the `merge` command of `llvm-profdata` tool, which merges multiple
Abhishek Aryaaf9811f22018-05-11 22:17:48204raw profiles (.profraw) and indexes them to create a single profile (.profdata).
Yuke Liao1ffc8cb62018-04-06 19:09:07205
206At this point, all the raw profiles can be thrown away because their information
Abhishek Aryaaf9811f22018-05-11 22:17:48207is already contained in the indexed profile.
Yuke Liao1ffc8cb62018-04-06 19:09:07208
209```
210$ llvm-profdata merge -o out/report/coverage.profdata \
211 out/report/crypto_unittests.3657994905831792357_0.profraw
212...
213out/report/crypto_unittests.3657994905831792357_3.profraw
214out/report/url_unittests.714228855822523802_0.profraw
215...
216out/report/url_unittests.714228855822523802_3.profraw
217$ ls out/report/coverage.profdata
218out/report/coverage.profdata
219```
220
221### Step 4 Create Coverage Reports
222Finally, `llvm-cov` is used to render code coverage reports. There are different
Abhishek Aryaaf9811f22018-05-11 22:17:48223report generation modes, and all of them require the following as input:
224- Indexed profile
225- All built target binaries
Roberto Carrillo5221fc12019-01-30 21:34:50226- All exercised source files
Yuke Liao1ffc8cb62018-04-06 19:09:07227
Abhishek Aryaaf9811f22018-05-11 22:17:48228For example, the following command can be used to generate per-file line-by-line
Yuke Liao1ffc8cb62018-04-06 19:09:07229code coverage report:
230
231```
232$ llvm-cov show -output-dir=out/report -format=html \
233 -instr-profile=out/report/coverage.profdata \
234 -object=out/coverage/url_unittests \
235 out/coverage/crypto_unittests
236```
237
Ben Joyce88282362021-01-29 23:53:31238If creating a report for Android, the -object arg would be the lib.unstripped
239file, ie out/coverage/lib.unstripped/libcrypto_unittests__library.so
240
Yuke Liao1ffc8cb62018-04-06 19:09:07241For more information on how to use llvm-cov, please refer to the [guide].
Yuke Liaod3b46272018-03-14 18:25:14242
Max Morozd73e45f2018-04-24 18:32:47243## Contacts
244
245### Reporting problems
Yuke Liaod3b46272018-03-14 18:25:14246For any breakage report and feature requests, please [file a bug].
247
Max Morozd73e45f2018-04-24 18:32:47248### Mailing list
Yuke Liaobc35726b2018-10-31 22:16:21249For questions and general discussions, please join [code-coverage group].
Yuke Liao1ffc8cb62018-04-06 19:09:07250
Max Morozd73e45f2018-04-24 18:32:47251## FAQ
252
253### Can I use `is_component_build=true` for code coverage build?
254
255Yes, code coverage instrumentation works with both component and non-component
256builds. Component build is usually faster to compile, but can be up to several
257times slower to run with code coverage instrumentation. For more information,
Max Morozc5e364a2018-04-25 23:19:49258see [crbug.com/831939].
259
260### I am getting some warnings while using the script, is that fine?
261
Abhishek Aryaaf9811f22018-05-11 22:17:48262Usually this is not a critical issue, but in general we tend not to have any
Max Morozc5e364a2018-04-25 23:19:49263warnings. Please check the list of [known issues], and if there is a similar
264bug, leave a comment with the command you run, the output you get, and Chromium
Yuke Liao03c644072019-07-30 18:33:40265revision you use. Otherwise, please [file a bug] providing the same information.
Max Morozc5e364a2018-04-25 23:19:49266
267### How do crashes affect code coverage?
268
Max Moroza5a95272018-08-31 16:20:55269If a crash of any type occurs (e.g. Segmentation Fault or ASan error), the
270crashing process might not dump coverage information necessary to generate
Max Morozc5e364a2018-04-25 23:19:49271code coverage report. For single-process applications (e.g. fuzz targets), that
Max Moroza5a95272018-08-31 16:20:55272means no coverage might be reported at all. For multi-process applications, the
273report might be incomplete. It is important to fix the crash first. If this is
Abhishek Aryaaf9811f22018-05-11 22:17:48274happening only in the coverage instrumented build, please [file a bug].
Max Morozd73e45f2018-04-24 18:32:47275
Max Moroza5a95272018-08-31 16:20:55276### How do assertions affect code coverage?
277
278If a crash is caused by CHECK or DCHECK, the coverage dump will still be written
279on the disk ([crrev.com/c/1172932]). However, if a crashing process calls the
280standard [assert] directly or through a custom wrapper, the dump will not be
281written (see [How do crashes affect code coverage?]).
282
Max Moroz63cd04d2018-05-02 16:40:23283### Is it possible to obtain code coverage from a full Chromium build?
284
285Yes, with some important caveats. It is possible to build `chrome` target with
286code coverage instrumentation enabled. However, there are some inconveniences
287involved:
288
289* Linking may take a while
290* The binary is huge (~4GB)
291* The browser "works", but is noticeably slow and laggy
292* The sandbox needs to be disabled (`--no-sandbox`)
Max Moroz63cd04d2018-05-02 16:40:23293
294For more information, please see [crbug.com/834781].
295
Max Moroz3a928902018-05-15 14:39:50296### Why do we see significantly different coverage reported on different revisions?
297
298There can be two possible scenarios:
299
300* It can be a one time flakiness due to a broken build or failing tests.
301* It can be caused by extension of the test suite used for generating code
302coverage reports. When we add new tests to the suite, the aggregate coverage
303reported usually grows after that.
304
305### How can I improve [coverage dashboard]?
306
Roberto Carrillo3b567862019-01-30 19:01:25307The code for the service and dashboard currently lives along with findit at
308[this location](https://chromium.googlesource.com/infra/infra/+/master/appengine/findit/)
309because of significant shared logic.
310
311The code used by the bots that generate the coverage data lives (among other
312places) in the
Yuke Liao04efa972019-06-26 21:14:07313[code coverage recipe module](https://chromium.googlesource.com/chromium/tools/build/+/master/scripts/slave/recipe_modules/code_coverage/).
Roberto Carrillo3b567862019-01-30 19:01:25314
Max Moroz3a928902018-05-15 14:39:50315### Why is coverage for X not reported or unreasonably low, even though there is a test for X?
316
317There are several reasons why coverage reports can be incomplete or incorrect:
318
319* A particular test is not used for code coverage report generation. Please
Roberto Carrillo5221fc12019-01-30 21:34:50320[file a bug].
Roberto Carrillo3b567862019-01-30 19:01:25321* A test may have a build failure or a runtime crash. Please check the build
322for that particular report (rightmost column on the [coverage dashboard]).
Max Moroz3a928902018-05-15 14:39:50323If there is any failure, please upload a CL with the fix. If you can't fix it,
324feel free to [file a bug].
325* A particular test may not be available on a particular platform. As of now,
Yuke Liao43bbbcd52019-06-21 19:34:50326only reports generated on Linux and CrOS are available on the
327[coverage dashboard].
Max Moroz3a928902018-05-15 14:39:50328
Max Moroz3a928902018-05-15 14:39:50329### Is coverage reported for the code executed inside the sandbox?
330
Yuke Liao8c0868fe62019-10-22 21:02:33331Yes!
Max Moroz3a928902018-05-15 14:39:50332
Max Morozd73e45f2018-04-24 18:32:47333
Max Moroza5a95272018-08-31 16:20:55334[assert]: http://man7.org/linux/man-pages/man3/assert.3.html
Yuke Liaobc35726b2018-10-31 22:16:21335[code-coverage group]: https://groups.google.com/a/chromium.org/forum/#!forum/code-coverage
Max Moroz3a928902018-05-15 14:39:50336[code-coverage repository]: https://chrome-internal.googlesource.com/chrome/tools/code-coverage
Roberto Carrillo3b567862019-01-30 19:01:25337[coverage dashboard]: https://analysis.chromium.org/p/chromium/coverage
Yuke Liao1ffc8cb62018-04-06 19:09:07338[coverage script]: https://cs.chromium.org/chromium/src/tools/code_coverage/coverage.py
Roberto Carrillo3b567862019-01-30 19:01:25339[coverage infra diagram]: images/code_coverage_infra_diagram.png
340[coverage dashboard file view]: images/code_coverage_dashboard_file_view.png
341[coverage dashboard component view]: images/code_coverage_dashboard_component_view.png
342[coverage dashboard directory view]: images/code_coverage_dashboard_directory_view.png
Yuke Liao43bbbcd52019-06-21 19:34:50343[coverage dashboard link to previous reports]: images/code_coverage_dashboard_link_to_previous_reports.png
344[coverage dashboard previous reports]: images/code_coverage_dashboard_previous_reports.png
Max Moroz3a928902018-05-15 14:39:50345[crbug.com/821617]: https://crbug.com/821617
Max Morozc5e364a2018-04-25 23:19:49346[crbug.com/831939]: https://crbug.com/831939
Max Moroz63cd04d2018-05-02 16:40:23347[crbug.com/834781]: https://crbug.com/834781
Max Moroza5a95272018-08-31 16:20:55348[crrev.com/c/1172932]: https://crrev.com/c/1172932
Max Moroz3a928902018-05-15 14:39:50349[clang roll]: https://crbug.com/841908
Abhishek Aryab23b1a72018-05-17 20:11:09350[dead code example]: https://chromium.googlesource.com/chromium/src/+/ac6e09311fcc7e734be2ef21a9ccbbe04c4c4706
Max Morozc5e364a2018-04-25 23:19:49351[documentation]: https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
Yuke Liao03c644072019-07-30 18:33:40352[file a bug]: https://bugs.chromium.org/p/chromium/issues/entry?components=Infra%3ETest%3ECodeCoverage
Yuke Liao43bbbcd52019-06-21 19:34:50353[gerrit coverage view]: images/code_coverage_annotations.png
Max Morozc5e364a2018-04-25 23:19:49354[guide]: http://llvm.org/docs/CommandGuide/llvm-cov.html
Max Moroza5a95272018-08-31 16:20:55355[How do crashes affect code coverage?]: #how-do-crashes-affect-code-coverage
Yuke Liao03c644072019-07-30 18:33:40356[known issues]: https://bugs.chromium.org/p/chromium/issues/list?q=component:Infra%3ETest%3ECodeCoverage
Roberto Carrillo3b567862019-01-30 19:01:25357[tools link]: https://storage.googleapis.com/chromium-browser-clang-staging/