Yuke Liao | ea6e337 | 2020-04-29 01:43:59 | [diff] [blame] | 1 | #!/usr/bin/env python |
Avi Drissman | 7601e4b | 2022-09-15 20:11:09 | [diff] [blame] | 2 | # Copyright 2020 The Chromium Authors |
Yuke Liao | ea6e337 | 2020-04-29 01:43:59 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | """Downloads pgo profiles for optimizing official Chrome. |
| 6 | |
| 7 | This script has the following responsibilities: |
| 8 | 1. Download a requested profile if necessary. |
| 9 | 2. Return a path to the current profile to feed to the build system. |
| 10 | 3. Removed stale profiles (2 days) to save disk spaces because profiles are |
| 11 | large (~1GB) and updated frequently (~4 times a day). |
| 12 | """ |
| 13 | |
| 14 | from __future__ import print_function |
| 15 | |
| 16 | import argparse |
| 17 | import os |
| 18 | import sys |
| 19 | import time |
| 20 | |
| 21 | _SRC_ROOT = os.path.abspath( |
| 22 | os.path.join(os.path.dirname(__file__), os.path.pardir)) |
| 23 | sys.path.append(os.path.join(_SRC_ROOT, 'third_party', 'depot_tools')) |
| 24 | import download_from_google_storage |
| 25 | |
| 26 | sys.path.append(os.path.join(_SRC_ROOT, 'build')) |
| 27 | import gn_helpers |
| 28 | |
| 29 | # Absolute path to the directory that stores pgo related state files, which |
| 30 | # specifcies which profile to update and use. |
| 31 | _PGO_DIR = os.path.join(_SRC_ROOT, 'chrome', 'build') |
| 32 | |
| 33 | # Absolute path to the directory that stores pgo profiles. |
| 34 | _PGO_PROFILE_DIR = os.path.join(_PGO_DIR, 'pgo_profiles') |
| 35 | |
| 36 | |
| 37 | def _read_profile_name(target): |
| 38 | """Read profile name given a target. |
| 39 | |
| 40 | Args: |
| 41 | target(str): The target name, such as win32, mac. |
| 42 | |
| 43 | Returns: |
| 44 | Name of the profile to update and use, such as: |
| 45 | chrome-win32-master-67ad3c89d2017131cc9ce664a1580315517550d1.profdata. |
| 46 | """ |
| 47 | state_file = os.path.join(_PGO_DIR, '%s.pgo.txt' % target) |
| 48 | with open(state_file, 'r') as f: |
| 49 | profile_name = f.read().strip() |
| 50 | |
Yuke Liao | 153db57 | 2020-05-03 23:43:29 | [diff] [blame] | 51 | return profile_name |
Yuke Liao | ea6e337 | 2020-04-29 01:43:59 | [diff] [blame] | 52 | |
| 53 | |
| 54 | def _remove_unused_profiles(current_profile_name): |
| 55 | """Removes unused profiles, except the current one, to save disk space.""" |
| 56 | days = 2 |
| 57 | expiration_duration = 60 * 60 * 24 * days |
| 58 | for f in os.listdir(_PGO_PROFILE_DIR): |
| 59 | if f == current_profile_name: |
| 60 | continue |
| 61 | |
| 62 | p = os.path.join(_PGO_PROFILE_DIR, f) |
| 63 | age = time.time() - os.path.getmtime(p) |
| 64 | if age > expiration_duration: |
| 65 | print('Removing profile %s as it hasn\'t been used in the past %d days' % |
| 66 | (p, days)) |
| 67 | os.remove(p) |
| 68 | |
| 69 | |
| 70 | def _update(args): |
| 71 | """Update profile if necessary according to the state file. |
| 72 | |
| 73 | Args: |
| 74 | args(dict): A dict of cmd arguments, such as target and gs_url_base. |
| 75 | |
| 76 | Raises: |
| 77 | RuntimeError: If failed to download profiles from gcs. |
| 78 | """ |
Peter Wen | aedb8e4 | 2025-05-06 21:19:30 | [diff] [blame] | 79 | profile_name = args.override_filename or _read_profile_name(args.target) |
Yuke Liao | ea6e337 | 2020-04-29 01:43:59 | [diff] [blame] | 80 | profile_path = os.path.join(_PGO_PROFILE_DIR, profile_name) |
| 81 | if os.path.isfile(profile_path): |
| 82 | os.utime(profile_path, None) |
| 83 | return |
| 84 | |
| 85 | gsutil = download_from_google_storage.Gsutil( |
| 86 | download_from_google_storage.GSUTIL_DEFAULT_PATH) |
| 87 | gs_path = 'gs://' + args.gs_url_base.strip('/') + '/' + profile_name |
| 88 | code = gsutil.call('cp', gs_path, profile_path) |
| 89 | if code != 0: |
| 90 | raise RuntimeError('gsutil failed to download "%s"' % gs_path) |
| 91 | |
| 92 | _remove_unused_profiles(profile_name) |
| 93 | |
| 94 | |
| 95 | def _get_profile_path(args): |
| 96 | """Returns an absolute path to the current profile. |
| 97 | |
| 98 | Args: |
| 99 | args(dict): A dict of cmd arguments, such as target and gs_url_base. |
| 100 | |
| 101 | Raises: |
| 102 | RuntimeError: If the current profile is missing. |
| 103 | """ |
Peter Wen | aedb8e4 | 2025-05-06 21:19:30 | [diff] [blame] | 104 | profile_name = args.override_filename or _read_profile_name(args.target) |
| 105 | profile_path = os.path.join(_PGO_PROFILE_DIR, profile_name) |
Yuke Liao | ea6e337 | 2020-04-29 01:43:59 | [diff] [blame] | 106 | if not os.path.isfile(profile_path): |
Yuke Liao | 871169a | 2020-05-01 20:07:18 | [diff] [blame] | 107 | raise RuntimeError( |
| 108 | 'requested profile "%s" doesn\'t exist, please make sure ' |
Sebastien Marchand | 9b4b654 | 2020-06-19 22:41:47 | [diff] [blame] | 109 | '"checkout_pgo_profiles" is set to True in the "custom_vars" section ' |
| 110 | 'of your .gclient file, e.g.: \n' |
| 111 | 'solutions = [ \n' |
| 112 | ' { \n' |
| 113 | ' "name": "src", \n' |
| 114 | ' # ... \n' |
| 115 | ' "custom_vars": { \n' |
| 116 | ' "checkout_pgo_profiles": True, \n' |
| 117 | ' }, \n' |
| 118 | ' }, \n' |
| 119 | '], \n' |
| 120 | 'and then run "gclient runhooks" to download it. You can also simply ' |
| 121 | 'disable the PGO optimizations by setting |chrome_pgo_phase = 0| in ' |
| 122 | 'your GN arguments.'% |
Yuke Liao | 871169a | 2020-05-01 20:07:18 | [diff] [blame] | 123 | profile_path) |
Yuke Liao | ea6e337 | 2020-04-29 01:43:59 | [diff] [blame] | 124 | |
| 125 | os.utime(profile_path, None) |
| 126 | profile_path.rstrip(os.sep) |
| 127 | print(gn_helpers.ToGNString(profile_path)) |
| 128 | |
| 129 | |
| 130 | def main(): |
| 131 | parser = argparse.ArgumentParser( |
| 132 | description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) |
| 133 | parser.add_argument( |
| 134 | '--target', |
| 135 | required=True, |
Jeff Yoon | c5cb00d | 2023-04-03 22:11:49 | [diff] [blame] | 136 | choices=[ |
Kuan Huang | 6978626 | 2023-08-17 23:18:42 | [diff] [blame] | 137 | 'win-arm64', |
Jeff Yoon | a2739b2 | 2023-04-18 20:37:29 | [diff] [blame] | 138 | 'win32', |
| 139 | 'win64', |
| 140 | 'mac', |
| 141 | 'mac-arm', |
| 142 | 'linux', |
Samuel Huang | 7b2de39 | 2023-07-31 14:54:15 | [diff] [blame] | 143 | 'android-arm32', |
| 144 | 'android-arm64', |
Darren Wu | c8c6f93 | 2025-05-02 23:50:12 | [diff] [blame] | 145 | 'android-desktop-x64', |
Jeff Yoon | c5cb00d | 2023-04-03 22:11:49 | [diff] [blame] | 146 | ], |
Yuke Liao | ea6e337 | 2020-04-29 01:43:59 | [diff] [blame] | 147 | help='Identifier of a specific target platform + architecture.') |
Peter Wen | aedb8e4 | 2025-05-06 21:19:30 | [diff] [blame] | 148 | parser.add_argument('--override-filename', |
| 149 | help='The filename to prefer instead of the sha1 file.') |
Yuke Liao | ea6e337 | 2020-04-29 01:43:59 | [diff] [blame] | 150 | subparsers = parser.add_subparsers() |
| 151 | |
| 152 | parser_update = subparsers.add_parser('update') |
| 153 | parser_update.add_argument( |
| 154 | '--gs-url-base', |
| 155 | required=True, |
| 156 | help='The base GS URL to search for the profile.') |
| 157 | parser_update.set_defaults(func=_update) |
| 158 | |
| 159 | parser_get_profile_path = subparsers.add_parser('get_profile_path') |
| 160 | parser_get_profile_path.set_defaults(func=_get_profile_path) |
| 161 | |
| 162 | args = parser.parse_args() |
| 163 | return args.func(args) |
| 164 | |
| 165 | |
| 166 | if __name__ == '__main__': |
| 167 | sys.exit(main()) |