blob: 297e5c09a1a3e5b4eb0bcf5e8397254728610feb [file] [log] [blame] [view]
Peter Wen5f35d552023-11-27 18:15:371# Profile-Guided Optimization (PGO)
2
Andrew Grieve8ea61ff2023-12-13 18:15:143## Generating PGO Profiles via Bots
4
5See [go/chrome-pgo-internal] (Googlers only).
6
7[go/chrome-pgo-internal]: https://goto.google.com/chrome-pgo-internal
8
9## Generating PGO Profiles Manually
Arthur Eubanks8a0ecce2020-10-26 20:25:4710
11Normally devs don't need to worry about this and can use the default profile
12for official builds. The default profile can be fetched by adding
13`"checkout_pgo_profiles": True` to `custom_vars` in the gclient config and
14running `gclient runhooks`.
15
16To produce an executable built with a custom PGO profile:
17
18* Produce the instrumented executable using the following gn args:
19
20 ```
21 chrome_pgo_phase = 1
22 enable_resource_allowlist_generation = false
23 is_official_build = true
24 symbol_level = 0
Peter Wen7d7f447e2023-11-17 18:14:0925 use_remoteexec = true
26 ```
27
Egor Pasko255de502024-01-24 14:50:2828 For android you need these in addition:
Peter Wen7d7f447e2023-11-17 18:14:0929 ```
30 target_os = "android"
31 target_cpu = "arm64"
Arthur Eubanks8a0ecce2020-10-26 20:25:4732 ```
33
34* Run representative benchmarks to produce profiles
35
Peter Wen7d7f447e2023-11-17 18:14:0936 `python3 tools/pgo/generate_profile.py -C out/builddir`
37
Peter Wen5f35d552023-11-27 18:15:3738 If collecting profiles on an android device, add a browser name like one of
39 [these][browser_names]:
Peter Wen7d7f447e2023-11-17 18:14:0940
41 ```
42 python3 tools/pgo/generate_profile.py -C out/builddir \
Peter Wen43c566a92024-12-02 21:45:3643 --android-browser android-trichrome-chrome-google-bundle
Peter Wen7d7f447e2023-11-17 18:14:0944 ```
Arthur Eubanks8a0ecce2020-10-26 20:25:4745
Tushar Agarwald7eb537d2024-01-23 18:08:2346 You can find available browsers using:
47
48 ```
49 tools/perf/run_benchmark run --browser=list
50 ```
51
Peter Wen43c566a92024-12-02 21:45:3652 By default, some benchmark replay archives require special access permissions.
53 For more details and to request access, please refer to [Telemetry
54 documentation][telemetry_docs]. You can also choose to run
55 `generate_profile.py` without these benchmarks, using the
56 `--run-public-benchmarks-only` flag. However, note that doing so may produce a
57 profile that isn't fully representative.
Tushar Agarwald7eb537d2024-01-23 18:08:2358
59 ```
60 python3 tools/pgo/generate_profile.py -C out/builddir \
Peter Wen43c566a92024-12-02 21:45:3661 --android-browser android-trichrome-chrome-google-bundle \
Tushar Agarwald7eb537d2024-01-23 18:08:2362 --run-public-benchmarks-only
63 ```
64
Peter Wen43c566a92024-12-02 21:45:3665 If `generate_profile.py` fails with `ServiceException: 401 Anonymous caller
66 does not have storage.objects.get access to the Google Cloud Storage object.`,
67 then run `download_from_google_storage --config` (with your @google address;
68 enter 0 as project-id).
Nico Weber98b74eb2021-12-23 16:14:1269
Peter Wen5f35d552023-11-27 18:15:3770 This will produce `out/builddir/profile.profdata`
Arthur Eubanks8a0ecce2020-10-26 20:25:4771
Peter Wen7d7f447e2023-11-17 18:14:0972* Produce the final PGO'd executable with the following gn args (and additional
73 android args, if any):
Arthur Eubanks8a0ecce2020-10-26 20:25:4774
75 ```
76 enable_resource_allowlist_generation = false
77 is_official_build = true
78 symbol_level = 0
Peter Wen7d7f447e2023-11-17 18:14:0979 use_remoteexec = true
Peter Wen5f35d552023-11-27 18:15:3780 pgo_data_path = "//out/builddir/profile.profdata"
Arthur Eubanks8a0ecce2020-10-26 20:25:4781 ```
Peter Wen7d7f447e2023-11-17 18:14:0982
Peter Wen5f35d552023-11-27 18:15:3783[browser_names]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/catapult/telemetry/telemetry/internal/backends/android_browser_backend_settings.py;l=400;drc=bf85e76dc3467385a623e9bf11ab950cf2889ca5
Tushar Agarwald7eb537d2024-01-23 18:08:2384[telemetry_docs]: https://www.chromium.org/developers/telemetry/upload_to_cloud_storage/#request-access-for-google-partners
Peter Wen5f35d552023-11-27 18:15:3785
86## How It Works
87
88`chrome_pgo_phase` is defined in [`build/config/compiler/pgo/pgo.gni`][pgo_gni].
89This GN variable can be one of 0, 1, or 2, meaning "don't use profile",
90"generating profile", and "use profile" respectively. See [pgo.gni][pgo_gni] for
91details on platform-specific GN variables that determine which phase is used in
92each build.
93
94Which file under `//chrome/build/pgo_profiles/` gets used? It depends on both
95the platform and [`_pgo_target`][pgo_target]. For example, for 64-bit android,
96the file `//chrome/build/android-arm64.pgo.txt` contains the name of the
97`*.profdata` file that is used as the PGO profile by default if no other profile
98is specified via the GN arg `pgo_data_path`.
99
100[pgo_gni]: https://source.chromium.org/chromium/chromium/src/+/main:build/config/compiler/pgo/pgo.gni
101[pgo_target]: https://source.chromium.org/chromium/chromium/src/+/main:build/config/compiler/pgo/BUILD.gn;l=88;drc=3d2e089ad74a30754376571531e00615de96061e
102
103## Background Reading
104
105https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization
106
107https://source.android.com/docs/core/perf/pgo