Peter Wen | 5f35d55 | 2023-11-27 18:15:37 | [diff] [blame] | 1 | # Profile-Guided Optimization (PGO) |
| 2 | |
Andrew Grieve | 8ea61ff | 2023-12-13 18:15:14 | [diff] [blame] | 3 | ## Generating PGO Profiles via Bots |
| 4 | |
| 5 | See [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 Eubanks | 8a0ecce | 2020-10-26 20:25:47 | [diff] [blame] | 10 | |
| 11 | Normally devs don't need to worry about this and can use the default profile |
| 12 | for official builds. The default profile can be fetched by adding |
| 13 | `"checkout_pgo_profiles": True` to `custom_vars` in the gclient config and |
| 14 | running `gclient runhooks`. |
| 15 | |
| 16 | To 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 Wen | 7d7f447e | 2023-11-17 18:14:09 | [diff] [blame] | 25 | use_remoteexec = true |
| 26 | ``` |
| 27 | |
Egor Pasko | 255de50 | 2024-01-24 14:50:28 | [diff] [blame] | 28 | For android you need these in addition: |
Peter Wen | 7d7f447e | 2023-11-17 18:14:09 | [diff] [blame] | 29 | ``` |
| 30 | target_os = "android" |
| 31 | target_cpu = "arm64" |
Arthur Eubanks | 8a0ecce | 2020-10-26 20:25:47 | [diff] [blame] | 32 | ``` |
| 33 | |
| 34 | * Run representative benchmarks to produce profiles |
| 35 | |
Peter Wen | 7d7f447e | 2023-11-17 18:14:09 | [diff] [blame] | 36 | `python3 tools/pgo/generate_profile.py -C out/builddir` |
| 37 | |
Peter Wen | 5f35d55 | 2023-11-27 18:15:37 | [diff] [blame] | 38 | If collecting profiles on an android device, add a browser name like one of |
| 39 | [these][browser_names]: |
Peter Wen | 7d7f447e | 2023-11-17 18:14:09 | [diff] [blame] | 40 | |
| 41 | ``` |
| 42 | python3 tools/pgo/generate_profile.py -C out/builddir \ |
Peter Wen | 43c566a9 | 2024-12-02 21:45:36 | [diff] [blame] | 43 | --android-browser android-trichrome-chrome-google-bundle |
Peter Wen | 7d7f447e | 2023-11-17 18:14:09 | [diff] [blame] | 44 | ``` |
Arthur Eubanks | 8a0ecce | 2020-10-26 20:25:47 | [diff] [blame] | 45 | |
Tushar Agarwal | d7eb537d | 2024-01-23 18:08:23 | [diff] [blame] | 46 | You can find available browsers using: |
| 47 | |
| 48 | ``` |
| 49 | tools/perf/run_benchmark run --browser=list |
| 50 | ``` |
| 51 | |
Peter Wen | 43c566a9 | 2024-12-02 21:45:36 | [diff] [blame] | 52 | 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 Agarwal | d7eb537d | 2024-01-23 18:08:23 | [diff] [blame] | 58 | |
| 59 | ``` |
| 60 | python3 tools/pgo/generate_profile.py -C out/builddir \ |
Peter Wen | 43c566a9 | 2024-12-02 21:45:36 | [diff] [blame] | 61 | --android-browser android-trichrome-chrome-google-bundle \ |
Tushar Agarwal | d7eb537d | 2024-01-23 18:08:23 | [diff] [blame] | 62 | --run-public-benchmarks-only |
| 63 | ``` |
| 64 | |
Peter Wen | 43c566a9 | 2024-12-02 21:45:36 | [diff] [blame] | 65 | 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 Weber | 98b74eb | 2021-12-23 16:14:12 | [diff] [blame] | 69 | |
Peter Wen | 5f35d55 | 2023-11-27 18:15:37 | [diff] [blame] | 70 | This will produce `out/builddir/profile.profdata` |
Arthur Eubanks | 8a0ecce | 2020-10-26 20:25:47 | [diff] [blame] | 71 | |
Peter Wen | 7d7f447e | 2023-11-17 18:14:09 | [diff] [blame] | 72 | * Produce the final PGO'd executable with the following gn args (and additional |
| 73 | android args, if any): |
Arthur Eubanks | 8a0ecce | 2020-10-26 20:25:47 | [diff] [blame] | 74 | |
| 75 | ``` |
| 76 | enable_resource_allowlist_generation = false |
| 77 | is_official_build = true |
| 78 | symbol_level = 0 |
Peter Wen | 7d7f447e | 2023-11-17 18:14:09 | [diff] [blame] | 79 | use_remoteexec = true |
Peter Wen | 5f35d55 | 2023-11-27 18:15:37 | [diff] [blame] | 80 | pgo_data_path = "//out/builddir/profile.profdata" |
Arthur Eubanks | 8a0ecce | 2020-10-26 20:25:47 | [diff] [blame] | 81 | ``` |
Peter Wen | 7d7f447e | 2023-11-17 18:14:09 | [diff] [blame] | 82 | |
Peter Wen | 5f35d55 | 2023-11-27 18:15:37 | [diff] [blame] | 83 | [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 Agarwal | d7eb537d | 2024-01-23 18:08:23 | [diff] [blame] | 84 | [telemetry_docs]: https://www.chromium.org/developers/telemetry/upload_to_cloud_storage/#request-access-for-google-partners |
Peter Wen | 5f35d55 | 2023-11-27 18:15:37 | [diff] [blame] | 85 | |
| 86 | ## How It Works |
| 87 | |
| 88 | `chrome_pgo_phase` is defined in [`build/config/compiler/pgo/pgo.gni`][pgo_gni]. |
| 89 | This 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 |
| 91 | details on platform-specific GN variables that determine which phase is used in |
| 92 | each build. |
| 93 | |
| 94 | Which file under `//chrome/build/pgo_profiles/` gets used? It depends on both |
| 95 | the platform and [`_pgo_target`][pgo_target]. For example, for 64-bit android, |
| 96 | the 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 |
| 98 | is 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 | |
| 105 | https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization |
| 106 | |
| 107 | https://source.android.com/docs/core/perf/pgo |