Egor Pasko | 0462e852d | 2018-03-29 15:52:09 | [diff] [blame] | 1 | #!/usr/bin/env vpython |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 2 | # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """ A utility to generate an up-to-date orderfile. |
| 7 | |
| 8 | The orderfile is used by the linker to order text sections such that the |
| 9 | sections are placed consecutively in the order specified. This allows us |
| 10 | to page in less code during start-up. |
| 11 | |
| 12 | Example usage: |
Egor Pasko | 93e514e | 2017-10-31 13:32:36 | [diff] [blame] | 13 | tools/cygprofile/orderfile_generator_backend.py -l 20 -j 1000 --use-goma \ |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 14 | --target-arch=arm |
| 15 | """ |
| 16 | |
Benoit Lize | a1b64f8 | 2017-12-07 10:12:50 | [diff] [blame] | 17 | import argparse |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 18 | import hashlib |
| 19 | import json |
Matthew Cary | 69e9e42 | 2018-08-10 18:30:06 | [diff] [blame] | 20 | import glob |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 21 | import logging |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 22 | import os |
| 23 | import re |
| 24 | import shutil |
| 25 | import subprocess |
| 26 | import sys |
Benoit Lize | a87e5bc | 2017-11-07 15:12:57 | [diff] [blame] | 27 | import tempfile |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 28 | import time |
| 29 | |
Matthew Cary | 91df979 | 2018-11-30 14:35:15 | [diff] [blame] | 30 | import cluster |
Matthew Cary | e840064 | 2018-06-14 15:43:02 | [diff] [blame] | 31 | import cyglog_to_orderfile |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 32 | import cygprofile_utils |
Benoit Lize | fefbb27c | 2018-01-17 13:54:18 | [diff] [blame] | 33 | import patch_orderfile |
Benoit Lize | a87e5bc | 2017-11-07 15:12:57 | [diff] [blame] | 34 | import process_profiles |
Egor Pasko | 3bc0b93 | 2018-04-03 10:08:27 | [diff] [blame] | 35 | import profile_android_startup |
Benoit Lize | fefbb27c | 2018-01-17 13:54:18 | [diff] [blame] | 36 | import symbol_extractor |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 37 | |
Monica Salama | eea2d94 | 2019-03-11 12:36:18 | [diff] [blame^] | 38 | _SRC_PATH = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir) |
| 39 | sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'catapult', 'devil')) |
| 40 | from devil.android import device_utils |
| 41 | from devil.android.sdk import version_codes |
| 42 | |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 43 | |
| 44 | _SRC_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), |
| 45 | os.pardir, os.pardir) |
| 46 | sys.path.append(os.path.join(_SRC_PATH, 'build', 'android')) |
| 47 | import devil_chromium |
| 48 | from pylib import constants |
| 49 | |
| 50 | |
| 51 | # Needs to happen early for GetBuildType()/GetOutDirectory() to work correctly |
| 52 | constants.SetBuildType('Release') |
| 53 | |
| 54 | |
Matthew Cary | 53f74ba | 2019-01-24 10:07:18 | [diff] [blame] | 55 | # Architecture specific GN args. Trying to build an orderfile for an |
| 56 | # architecture not listed here will eventually throw. |
| 57 | _ARCH_GN_ARGS = { |
| 58 | 'arm': [ 'target_cpu = "arm"' ], |
| 59 | 'arm64': [ 'target_cpu = "arm64"', |
| 60 | 'android_64bit_browser = true'], |
| 61 | } |
| 62 | |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 63 | class CommandError(Exception): |
| 64 | """Indicates that a dispatched shell command exited with a non-zero status.""" |
| 65 | |
| 66 | def __init__(self, value): |
| 67 | super(CommandError, self).__init__() |
| 68 | self.value = value |
| 69 | |
| 70 | def __str__(self): |
| 71 | return repr(self.value) |
| 72 | |
| 73 | |
| 74 | def _GenerateHash(file_path): |
| 75 | """Calculates and returns the hash of the file at file_path.""" |
| 76 | sha1 = hashlib.sha1() |
| 77 | with open(file_path, 'rb') as f: |
| 78 | while True: |
| 79 | # Read in 1mb chunks, so it doesn't all have to be loaded into memory. |
| 80 | chunk = f.read(1024 * 1024) |
| 81 | if not chunk: |
| 82 | break |
| 83 | sha1.update(chunk) |
| 84 | return sha1.hexdigest() |
| 85 | |
| 86 | |
| 87 | def _GetFileExtension(file_name): |
| 88 | """Calculates the file extension from a file name. |
| 89 | |
| 90 | Args: |
| 91 | file_name: The source file name. |
| 92 | Returns: |
| 93 | The part of file_name after the dot (.) or None if the file has no |
| 94 | extension. |
| 95 | Examples: /home/user/foo.bar -> bar |
| 96 | /home/user.name/foo -> None |
| 97 | /home/user/.foo -> None |
| 98 | /home/user/foo.bar.baz -> baz |
| 99 | """ |
| 100 | file_name_parts = os.path.basename(file_name).split('.') |
| 101 | if len(file_name_parts) > 1: |
| 102 | return file_name_parts[-1] |
| 103 | else: |
| 104 | return None |
| 105 | |
| 106 | |
| 107 | def _StashOutputDirectory(buildpath): |
| 108 | """Takes the output directory and stashes it in the default output directory. |
| 109 | |
| 110 | This allows it to be used for incremental builds next time (after unstashing) |
| 111 | by keeping it in a place that isn't deleted normally, while also ensuring |
| 112 | that it is properly clobbered when appropriate. |
| 113 | |
| 114 | This is a dirty hack to deal with the needs of clobbering while also handling |
| 115 | incremental builds and the hardcoded relative paths used in some of the |
| 116 | project files. |
| 117 | |
| 118 | Args: |
| 119 | buildpath: The path where the building happens. If this corresponds to the |
| 120 | default output directory, no action is taken. |
| 121 | """ |
| 122 | if os.path.abspath(buildpath) == os.path.abspath(os.path.dirname( |
| 123 | constants.GetOutDirectory())): |
| 124 | return |
| 125 | name = os.path.basename(buildpath) |
| 126 | stashpath = os.path.join(constants.GetOutDirectory(), name) |
| 127 | if not os.path.exists(buildpath): |
| 128 | return |
| 129 | if os.path.exists(stashpath): |
| 130 | shutil.rmtree(stashpath, ignore_errors=True) |
| 131 | shutil.move(buildpath, stashpath) |
| 132 | |
| 133 | |
| 134 | def _UnstashOutputDirectory(buildpath): |
| 135 | """Inverse of _StashOutputDirectory. |
| 136 | |
| 137 | Moves the output directory stashed within the default output directory |
| 138 | (out/Release) to the position where the builds can actually happen. |
| 139 | |
| 140 | This is a dirty hack to deal with the needs of clobbering while also handling |
| 141 | incremental builds and the hardcoded relative paths used in some of the |
| 142 | project files. |
| 143 | |
| 144 | Args: |
| 145 | buildpath: The path where the building happens. If this corresponds to the |
| 146 | default output directory, no action is taken. |
| 147 | """ |
| 148 | if os.path.abspath(buildpath) == os.path.abspath(os.path.dirname( |
| 149 | constants.GetOutDirectory())): |
| 150 | return |
| 151 | name = os.path.basename(buildpath) |
| 152 | stashpath = os.path.join(constants.GetOutDirectory(), name) |
| 153 | if not os.path.exists(stashpath): |
| 154 | return |
| 155 | if os.path.exists(buildpath): |
| 156 | shutil.rmtree(buildpath, ignore_errors=True) |
| 157 | shutil.move(stashpath, buildpath) |
| 158 | |
| 159 | |
| 160 | class StepRecorder(object): |
| 161 | """Records steps and timings.""" |
| 162 | |
| 163 | def __init__(self, buildbot): |
| 164 | self.timings = [] |
| 165 | self._previous_step = ('', 0.0) |
| 166 | self._buildbot = buildbot |
| 167 | self._error_recorded = False |
| 168 | |
| 169 | def BeginStep(self, name): |
| 170 | """Marks a beginning of the next step in the script. |
| 171 | |
| 172 | On buildbot, this prints a specially formatted name that will show up |
| 173 | in the waterfall. Otherwise, just prints the step name. |
| 174 | |
| 175 | Args: |
| 176 | name: The name of the step. |
| 177 | """ |
| 178 | self.EndStep() |
| 179 | self._previous_step = (name, time.time()) |
| 180 | print 'Running step: ', name |
| 181 | |
| 182 | def EndStep(self): |
| 183 | """Records successful completion of the current step. |
| 184 | |
| 185 | This is optional if the step is immediately followed by another BeginStep. |
| 186 | """ |
| 187 | if self._previous_step[0]: |
| 188 | elapsed = time.time() - self._previous_step[1] |
| 189 | print 'Step %s took %f seconds' % (self._previous_step[0], elapsed) |
| 190 | self.timings.append((self._previous_step[0], elapsed)) |
| 191 | |
| 192 | self._previous_step = ('', 0.0) |
| 193 | |
| 194 | def FailStep(self, message=None): |
| 195 | """Marks that a particular step has failed. |
| 196 | |
| 197 | On buildbot, this will mark the current step as failed on the waterfall. |
| 198 | Otherwise we will just print an optional failure message. |
| 199 | |
| 200 | Args: |
| 201 | message: An optional explanation as to why the step failed. |
| 202 | """ |
| 203 | print 'STEP FAILED!!' |
| 204 | if message: |
| 205 | print message |
| 206 | self._error_recorded = True |
| 207 | self.EndStep() |
| 208 | |
| 209 | def ErrorRecorded(self): |
| 210 | """True if FailStep has been called.""" |
| 211 | return self._error_recorded |
| 212 | |
| 213 | def RunCommand(self, cmd, cwd=constants.DIR_SOURCE_ROOT, raise_on_error=True, |
| 214 | stdout=None): |
| 215 | """Execute a shell command. |
| 216 | |
| 217 | Args: |
| 218 | cmd: A list of command strings. |
Matthew Cary | 78aae16 | 2018-08-10 17:16:30 | [diff] [blame] | 219 | cwd: Directory in which the command should be executed, defaults to build |
| 220 | root of script's location if not specified. |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 221 | raise_on_error: If true will raise a CommandError if the call doesn't |
| 222 | succeed and mark the step as failed. |
| 223 | stdout: A file to redirect stdout for the command to. |
| 224 | |
| 225 | Returns: |
| 226 | The process's return code. |
| 227 | |
| 228 | Raises: |
| 229 | CommandError: An error executing the specified command. |
| 230 | """ |
| 231 | print 'Executing %s in %s' % (' '.join(cmd), cwd) |
| 232 | process = subprocess.Popen(cmd, stdout=stdout, cwd=cwd, env=os.environ) |
| 233 | process.wait() |
| 234 | if raise_on_error and process.returncode != 0: |
| 235 | self.FailStep() |
| 236 | raise CommandError('Exception executing command %s' % ' '.join(cmd)) |
| 237 | return process.returncode |
| 238 | |
| 239 | |
| 240 | class ClankCompiler(object): |
| 241 | """Handles compilation of clank.""" |
| 242 | |
| 243 | def __init__(self, out_dir, step_recorder, arch, jobs, max_load, use_goma, |
Matthew Cary | d6bfcb7 | 2018-09-14 11:27:46 | [diff] [blame] | 244 | goma_dir, system_health_profiling, monochrome): |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 245 | self._out_dir = out_dir |
| 246 | self._step_recorder = step_recorder |
Benoit Lize | a87e5bc | 2017-11-07 15:12:57 | [diff] [blame] | 247 | self._arch = arch |
| 248 | self._jobs = jobs |
| 249 | self._max_load = max_load |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 250 | self._use_goma = use_goma |
Benoit Lize | a87e5bc | 2017-11-07 15:12:57 | [diff] [blame] | 251 | self._goma_dir = goma_dir |
Matthew Cary | 78aae16 | 2018-08-10 17:16:30 | [diff] [blame] | 252 | self._system_health_profiling = system_health_profiling |
Matthew Cary | d6bfcb7 | 2018-09-14 11:27:46 | [diff] [blame] | 253 | if monochrome: |
| 254 | self._apk = 'Monochrome.apk' |
Matthew Cary | e1b0006 | 2018-09-19 14:27:44 | [diff] [blame] | 255 | self._apk_target = 'monochrome_apk' |
Matthew Cary | d6bfcb7 | 2018-09-14 11:27:46 | [diff] [blame] | 256 | self._libname = 'libmonochrome' |
| 257 | self._libchrome_target = 'monochrome' |
| 258 | else: |
| 259 | self._apk = 'Chrome.apk' |
Matthew Cary | e1b0006 | 2018-09-19 14:27:44 | [diff] [blame] | 260 | self._apk_target = 'chrome_apk' |
Matthew Cary | d6bfcb7 | 2018-09-14 11:27:46 | [diff] [blame] | 261 | self._libname = 'libchrome' |
| 262 | self._libchrome_target = 'libchrome' |
Matthew Cary | 78aae16 | 2018-08-10 17:16:30 | [diff] [blame] | 263 | |
| 264 | self.obj_dir = os.path.join(self._out_dir, 'Release', 'obj') |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 265 | self.lib_chrome_so = os.path.join( |
Matthew Cary | d6bfcb7 | 2018-09-14 11:27:46 | [diff] [blame] | 266 | self._out_dir, 'Release', 'lib.unstripped', |
| 267 | '{}.so'.format(self._libname)) |
| 268 | self.chrome_apk = os.path.join(self._out_dir, 'Release', 'apks', self._apk) |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 269 | |
| 270 | def Build(self, instrumented, target): |
| 271 | """Builds the provided ninja target with or without order_profiling on. |
| 272 | |
| 273 | Args: |
Benoit Lize | a87e5bc | 2017-11-07 15:12:57 | [diff] [blame] | 274 | instrumented: (bool) Whether we want to build an instrumented binary. |
| 275 | target: (str) The name of the ninja target to build. |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 276 | """ |
| 277 | self._step_recorder.BeginStep('Compile %s' % target) |
| 278 | |
| 279 | # Set the "Release Official" flavor, the parts affecting performance. |
| 280 | args = [ |
Peter Collingbourne | 1c85d5d2 | 2018-08-22 18:06:35 | [diff] [blame] | 281 | 'enable_resource_whitelist_generation=false', |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 282 | 'is_chrome_branded=true', |
| 283 | 'is_debug=false', |
| 284 | 'is_official_build=true', |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 285 | 'target_os="android"', |
| 286 | 'use_goma=' + str(self._use_goma).lower(), |
| 287 | 'use_order_profiling=' + str(instrumented).lower(), |
| 288 | ] |
Matthew Cary | 53f74ba | 2019-01-24 10:07:18 | [diff] [blame] | 289 | args += _ARCH_GN_ARGS[self._arch] |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 290 | if self._goma_dir: |
| 291 | args += ['goma_dir="%s"' % self._goma_dir] |
Matthew Cary | 78aae16 | 2018-08-10 17:16:30 | [diff] [blame] | 292 | if self._system_health_profiling: |
| 293 | args += ['devtools_instrumentation_dumping = ' + |
| 294 | str(instrumented).lower()] |
Benoit Lize | a87e5bc | 2017-11-07 15:12:57 | [diff] [blame] | 295 | |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 296 | self._step_recorder.RunCommand( |
| 297 | ['gn', 'gen', os.path.join(self._out_dir, 'Release'), |
| 298 | '--args=' + ' '.join(args)]) |
| 299 | |
| 300 | self._step_recorder.RunCommand( |
| 301 | ['ninja', '-C', os.path.join(self._out_dir, 'Release'), |
| 302 | '-j' + str(self._jobs), '-l' + str(self._max_load), target]) |
| 303 | |
| 304 | def CompileChromeApk(self, instrumented, force_relink=False): |
| 305 | """Builds a Chrome.apk either with or without order_profiling on. |
| 306 | |
| 307 | Args: |
Benoit Lize | a87e5bc | 2017-11-07 15:12:57 | [diff] [blame] | 308 | instrumented: (bool) Whether to build an instrumented apk. |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 309 | force_relink: Whether libchromeview.so should be re-created. |
| 310 | """ |
| 311 | if force_relink: |
| 312 | self._step_recorder.RunCommand(['rm', '-rf', self.lib_chrome_so]) |
Matthew Cary | e1b0006 | 2018-09-19 14:27:44 | [diff] [blame] | 313 | self.Build(instrumented, self._apk_target) |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 314 | |
| 315 | def CompileLibchrome(self, instrumented, force_relink=False): |
| 316 | """Builds a libchrome.so either with or without order_profiling on. |
| 317 | |
| 318 | Args: |
Benoit Lize | a87e5bc | 2017-11-07 15:12:57 | [diff] [blame] | 319 | instrumented: (bool) Whether to build an instrumented apk. |
| 320 | force_relink: (bool) Whether libchrome.so should be re-created. |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 321 | """ |
| 322 | if force_relink: |
| 323 | self._step_recorder.RunCommand(['rm', '-rf', self.lib_chrome_so]) |
Matthew Cary | d6bfcb7 | 2018-09-14 11:27:46 | [diff] [blame] | 324 | self.Build(instrumented, self._libchrome_target) |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 325 | |
| 326 | |
| 327 | class OrderfileUpdater(object): |
| 328 | """Handles uploading and committing a new orderfile in the repository. |
| 329 | |
| 330 | Only used for testing or on a bot. |
| 331 | """ |
| 332 | |
| 333 | _CLOUD_STORAGE_BUCKET_FOR_DEBUG = None |
| 334 | _CLOUD_STORAGE_BUCKET = None |
| 335 | _UPLOAD_TO_CLOUD_COMMAND = 'upload_to_google_storage.py' |
| 336 | |
| 337 | def __init__(self, repository_root, step_recorder, branch, netrc): |
| 338 | """Constructor. |
| 339 | |
| 340 | Args: |
| 341 | repository_root: (str) Root of the target repository. |
| 342 | step_recorder: (StepRecorder) Step recorder, for logging. |
| 343 | branch: (str) Branch to commit to. |
| 344 | netrc: (str) Path to the .netrc file to use. |
| 345 | """ |
| 346 | self._repository_root = repository_root |
| 347 | self._step_recorder = step_recorder |
| 348 | self._branch = branch |
| 349 | self._netrc = netrc |
| 350 | |
| 351 | def CommitFileHashes(self, unpatched_orderfile_filename, orderfile_filename): |
| 352 | """Commits unpatched and patched orderfiles hashes, if provided. |
| 353 | |
| 354 | Files must have been successfilly uploaded to cloud storage first. |
| 355 | |
| 356 | Args: |
| 357 | unpatched_orderfile_filename: (str or None) Unpatched orderfile path. |
| 358 | orderfile_filename: (str or None) Orderfile path. |
| 359 | |
| 360 | Raises: |
| 361 | NotImplementedError when the commit logic hasn't been overriden. |
| 362 | """ |
| 363 | files_to_commit = [] |
| 364 | commit_message_lines = ['Update Orderfile.'] |
| 365 | for filename in [unpatched_orderfile_filename, orderfile_filename]: |
| 366 | if not filename: |
| 367 | continue |
| 368 | (relative_path, sha1) = self._GetHashFilePathAndContents(filename) |
| 369 | commit_message_lines.append('Profile: %s: %s' % ( |
| 370 | os.path.basename(relative_path), sha1)) |
| 371 | files_to_commit.append(relative_path) |
| 372 | if files_to_commit: |
| 373 | self._CommitFiles(files_to_commit, commit_message_lines) |
| 374 | |
| 375 | def UploadToCloudStorage(self, filename, use_debug_location): |
| 376 | """Uploads a file to cloud storage. |
| 377 | |
| 378 | Args: |
| 379 | filename: (str) File to upload. |
| 380 | use_debug_location: (bool) Whether to use the debug location. |
| 381 | """ |
| 382 | bucket = (self._CLOUD_STORAGE_BUCKET_FOR_DEBUG if use_debug_location |
| 383 | else self._CLOUD_STORAGE_BUCKET) |
| 384 | extension = _GetFileExtension(filename) |
| 385 | cmd = [self._UPLOAD_TO_CLOUD_COMMAND, '--bucket', bucket] |
| 386 | if extension: |
| 387 | cmd.extend(['-z', extension]) |
| 388 | cmd.append(filename) |
| 389 | self._step_recorder.RunCommand(cmd) |
| 390 | print 'Download: https://sandbox.google.com/storage/%s/%s' % ( |
| 391 | bucket, _GenerateHash(filename)) |
| 392 | |
| 393 | def _GetHashFilePathAndContents(self, filename): |
| 394 | """Gets the name and content of the hash file created from uploading the |
| 395 | given file. |
| 396 | |
| 397 | Args: |
| 398 | filename: (str) The file that was uploaded to cloud storage. |
| 399 | |
| 400 | Returns: |
| 401 | A tuple of the hash file name, relative to the reository root, and the |
| 402 | content, which should be the sha1 hash of the file |
| 403 | ('base_file.sha1', hash) |
| 404 | """ |
| 405 | abs_hash_filename = filename + '.sha1' |
| 406 | rel_hash_filename = os.path.relpath( |
| 407 | abs_hash_filename, self._repository_root) |
| 408 | with open(abs_hash_filename, 'r') as f: |
| 409 | return (rel_hash_filename, f.read()) |
| 410 | |
| 411 | def _CommitFiles(self, files_to_commit, commit_message_lines): |
| 412 | """Commits a list of files, with a given message.""" |
| 413 | raise NotImplementedError |
| 414 | |
| 415 | |
| 416 | class OrderfileGenerator(object): |
| 417 | """A utility for generating a new orderfile for Clank. |
| 418 | |
| 419 | Builds an instrumented binary, profiles a run of the application, and |
| 420 | generates an updated orderfile. |
| 421 | """ |
| 422 | _CLANK_REPO = os.path.join(constants.DIR_SOURCE_ROOT, 'clank') |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 423 | _CHECK_ORDERFILE_SCRIPT = os.path.join( |
| 424 | constants.DIR_SOURCE_ROOT, 'tools', 'cygprofile', 'check_orderfile.py') |
| 425 | _BUILD_ROOT = os.path.abspath(os.path.dirname(os.path.dirname( |
| 426 | constants.GetOutDirectory()))) # Normally /path/to/src |
| 427 | |
| 428 | _UNPATCHED_ORDERFILE_FILENAME = os.path.join( |
| 429 | _CLANK_REPO, 'orderfiles', 'unpatched_orderfile.%s') |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 430 | |
| 431 | _PATH_TO_ORDERFILE = os.path.join(_CLANK_REPO, 'orderfiles', |
| 432 | 'orderfile.%s.out') |
| 433 | |
| 434 | # Previous orderfile_generator debug files would be overwritten. |
| 435 | _DIRECTORY_FOR_DEBUG_FILES = '/tmp/orderfile_generator_debug_files' |
| 436 | |
| 437 | def _GetPathToOrderfile(self): |
| 438 | """Gets the path to the architecture-specific orderfile.""" |
| 439 | return self._PATH_TO_ORDERFILE % self._options.arch |
| 440 | |
| 441 | def _GetUnpatchedOrderfileFilename(self): |
| 442 | """Gets the path to the architecture-specific unpatched orderfile.""" |
| 443 | return self._UNPATCHED_ORDERFILE_FILENAME % self._options.arch |
| 444 | |
Monica Salama | eea2d94 | 2019-03-11 12:36:18 | [diff] [blame^] | 445 | def _SetDevice(self): |
| 446 | """ Selects the device to be used by the script. |
| 447 | |
| 448 | Returns: |
| 449 | (Device with given serial ID) : if the --device flag is set. |
| 450 | (Device running Android[K,L]) : if --use-legacy-chrome-apk flag is set or |
| 451 | no device running Android N+ was found. |
| 452 | (Device running Android N+) : Otherwise. |
| 453 | |
| 454 | Raises Error: |
| 455 | If no device meeting the requirements has been found. |
| 456 | """ |
| 457 | devices = None |
| 458 | if self._options.device: |
| 459 | devices = [device_utils.DeviceUtils(self._options.device)] |
| 460 | else: |
| 461 | devices = device_utils.DeviceUtils.HealthyDevices() |
| 462 | |
| 463 | assert devices, 'Expected at least one connected device' |
| 464 | |
| 465 | if self._options.use_legacy_chrome_apk: |
| 466 | self._monochrome = False |
| 467 | for device in devices: |
| 468 | device_version = device.build_version_sdk |
| 469 | if (device_version >= version_codes.KITKAT |
| 470 | and device_version <= version_codes.LOLLIPOP_MR1): |
| 471 | return device |
| 472 | |
| 473 | assert not self._options.use_legacy_chrome_apk, \ |
| 474 | 'No device found running suitable android version for Chrome.apk.' |
| 475 | |
| 476 | preferred_device = None |
| 477 | for device in devices: |
| 478 | if device.build_version_sdk >= version_codes.NOUGAT: |
| 479 | preferred_device = device |
| 480 | break |
| 481 | |
| 482 | self._monochrome = preferred_device is not None |
| 483 | |
| 484 | return preferred_device if preferred_device else devices[0] |
| 485 | |
| 486 | |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 487 | def __init__(self, options, orderfile_updater_class): |
| 488 | self._options = options |
| 489 | |
| 490 | self._instrumented_out_dir = os.path.join( |
| 491 | self._BUILD_ROOT, self._options.arch + '_instrumented_out') |
| 492 | self._uninstrumented_out_dir = os.path.join( |
| 493 | self._BUILD_ROOT, self._options.arch + '_uninstrumented_out') |
| 494 | |
| 495 | if options.profile: |
Benoit Lize | a1b64f8 | 2017-12-07 10:12:50 | [diff] [blame] | 496 | output_directory = os.path.join(self._instrumented_out_dir, 'Release') |
Matthew Cary | b8daed94 | 2018-06-11 10:58:08 | [diff] [blame] | 497 | host_profile_dir = os.path.join(output_directory, 'profile_data') |
Benoit Lize | a1b64f8 | 2017-12-07 10:12:50 | [diff] [blame] | 498 | urls = [profile_android_startup.AndroidProfileTool.TEST_URL] |
| 499 | use_wpr = True |
| 500 | simulate_user = False |
Benoit L | 9646681 | 2018-03-06 15:58:37 | [diff] [blame] | 501 | urls = options.urls |
| 502 | use_wpr = not options.no_wpr |
| 503 | simulate_user = options.simulate_user |
Monica Salama | eea2d94 | 2019-03-11 12:36:18 | [diff] [blame^] | 504 | device = self._SetDevice() |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 505 | self._profiler = profile_android_startup.AndroidProfileTool( |
Matthew Cary | 453ff145 | 2018-07-18 12:19:35 | [diff] [blame] | 506 | output_directory, host_profile_dir, use_wpr, urls, simulate_user, |
Monica Salama | eea2d94 | 2019-03-11 12:36:18 | [diff] [blame^] | 507 | device=device) |
Matthew Cary | 69e9e42 | 2018-08-10 18:30:06 | [diff] [blame] | 508 | if options.pregenerated_profiles: |
| 509 | self._profiler.SetPregeneratedProfiles( |
| 510 | glob.glob(options.pregenerated_profiles)) |
| 511 | else: |
| 512 | assert not options.pregenerated_profiles, ( |
| 513 | '--pregenerated-profiles cannot be used with --skip-profile') |
| 514 | assert not options.profile_save_dir, ( |
| 515 | '--profile-save-dir cannot be used with --skip-profile') |
Monica Salama | eea2d94 | 2019-03-11 12:36:18 | [diff] [blame^] | 516 | self._monochrome = not self._options.use_legacy_chrome_apk |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 517 | |
Matthew Cary | f949bba | 2019-02-04 13:39:23 | [diff] [blame] | 518 | # Outlined function handling enabled by default for all architectures. |
| 519 | self._order_outlined_functions = not options.noorder_outlined_functions |
| 520 | |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 521 | self._output_data = {} |
| 522 | self._step_recorder = StepRecorder(options.buildbot) |
| 523 | self._compiler = None |
| 524 | assert issubclass(orderfile_updater_class, OrderfileUpdater) |
| 525 | self._orderfile_updater = orderfile_updater_class(self._CLANK_REPO, |
| 526 | self._step_recorder, |
| 527 | options.branch, |
| 528 | options.netrc) |
| 529 | assert os.path.isdir(constants.DIR_SOURCE_ROOT), 'No src directory found' |
Benoit Lize | fefbb27c | 2018-01-17 13:54:18 | [diff] [blame] | 530 | symbol_extractor.SetArchitecture(options.arch) |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 531 | |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 532 | @staticmethod |
| 533 | def _RemoveBlanks(src_file, dest_file): |
| 534 | """A utility to remove blank lines from a file. |
| 535 | |
| 536 | Args: |
| 537 | src_file: The name of the file to remove the blanks from. |
| 538 | dest_file: The name of the file to write the output without blanks. |
| 539 | """ |
| 540 | assert src_file != dest_file, 'Source and destination need to be distinct' |
| 541 | |
| 542 | try: |
| 543 | src = open(src_file, 'r') |
| 544 | dest = open(dest_file, 'w') |
| 545 | for line in src: |
| 546 | if line and not line.isspace(): |
| 547 | dest.write(line) |
| 548 | finally: |
| 549 | src.close() |
| 550 | dest.close() |
| 551 | |
| 552 | def _GenerateAndProcessProfile(self): |
Matthew Cary | 78aae16 | 2018-08-10 17:16:30 | [diff] [blame] | 553 | """Invokes a script to merge the per-thread traces into one file. |
| 554 | |
| 555 | The produced list of offsets is saved in |
| 556 | self._GetUnpatchedOrderfileFilename(). |
| 557 | """ |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 558 | self._step_recorder.BeginStep('Generate Profile Data') |
| 559 | files = [] |
Matthew Cary | 78aae16 | 2018-08-10 17:16:30 | [diff] [blame] | 560 | logging.getLogger().setLevel(logging.DEBUG) |
| 561 | if self._options.system_health_orderfile: |
| 562 | files = self._profiler.CollectSystemHealthProfile( |
| 563 | self._compiler.chrome_apk) |
Matthew Cary | 69e9e42 | 2018-08-10 18:30:06 | [diff] [blame] | 564 | self._MaybeSaveProfile(files) |
Matthew Cary | 78aae16 | 2018-08-10 17:16:30 | [diff] [blame] | 565 | try: |
| 566 | self._ProcessPhasedOrderfile(files) |
| 567 | except Exception: |
| 568 | for f in files: |
| 569 | self._SaveForDebugging(f) |
Matthew Cary | 8b141623 | 2018-08-10 19:12:22 | [diff] [blame] | 570 | self._SaveForDebugging(self._compiler.lib_chrome_so) |
Matthew Cary | 78aae16 | 2018-08-10 17:16:30 | [diff] [blame] | 571 | raise |
| 572 | finally: |
| 573 | self._profiler.Cleanup() |
| 574 | else: |
| 575 | self._CollectLegacyProfile() |
| 576 | logging.getLogger().setLevel(logging.INFO) |
| 577 | |
| 578 | def _ProcessPhasedOrderfile(self, files): |
| 579 | """Process the phased orderfiles produced by system health benchmarks. |
| 580 | |
| 581 | The offsets will be placed in _GetUnpatchedOrderfileFilename(). |
| 582 | |
| 583 | Args: |
| 584 | file: Profile files pulled locally. |
| 585 | """ |
| 586 | self._step_recorder.BeginStep('Process Phased Orderfile') |
| 587 | profiles = process_profiles.ProfileManager(files) |
| 588 | processor = process_profiles.SymbolOffsetProcessor( |
| 589 | self._compiler.lib_chrome_so) |
Matthew Cary | 91df979 | 2018-11-30 14:35:15 | [diff] [blame] | 590 | ordered_symbols= cluster.ClusterOffsets(profiles, processor) |
Matthew Cary | 8b141623 | 2018-08-10 19:12:22 | [diff] [blame] | 591 | if not ordered_symbols: |
| 592 | raise Exception('Failed to get ordered symbols') |
Matthew Cary | 91df979 | 2018-11-30 14:35:15 | [diff] [blame] | 593 | self._output_data['offsets_kib'] = processor.SymbolsSize( |
| 594 | ordered_symbols) / 1024 |
Matthew Cary | 78aae16 | 2018-08-10 17:16:30 | [diff] [blame] | 595 | with open(self._GetUnpatchedOrderfileFilename(), 'w') as orderfile: |
Matthew Cary | 8b141623 | 2018-08-10 19:12:22 | [diff] [blame] | 596 | orderfile.write('\n'.join(ordered_symbols)) |
Matthew Cary | 78aae16 | 2018-08-10 17:16:30 | [diff] [blame] | 597 | |
| 598 | def _CollectLegacyProfile(self): |
Matthew Cary | 2df8d45 | 2018-09-19 07:50:20 | [diff] [blame] | 599 | files = [] |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 600 | try: |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 601 | files = self._profiler.CollectProfile( |
| 602 | self._compiler.chrome_apk, |
| 603 | constants.PACKAGE_INFO['chrome']) |
Matthew Cary | 69e9e42 | 2018-08-10 18:30:06 | [diff] [blame] | 604 | self._MaybeSaveProfile(files) |
Matthew Cary | b8daed94 | 2018-06-11 10:58:08 | [diff] [blame] | 605 | self._step_recorder.BeginStep('Process profile') |
Benoit L | 9646681 | 2018-03-06 15:58:37 | [diff] [blame] | 606 | assert os.path.exists(self._compiler.lib_chrome_so) |
| 607 | offsets = process_profiles.GetReachedOffsetsFromDumpFiles( |
| 608 | files, self._compiler.lib_chrome_so) |
| 609 | if not offsets: |
| 610 | raise Exception('No profiler offsets found in {}'.format( |
| 611 | '\n'.join(files))) |
Matthew Cary | 8b141623 | 2018-08-10 19:12:22 | [diff] [blame] | 612 | processor = process_profiles.SymbolOffsetProcessor( |
| 613 | self._compiler.lib_chrome_so) |
| 614 | ordered_symbols = processor.GetOrderedSymbols(offsets) |
| 615 | if not ordered_symbols: |
| 616 | raise Exception('No symbol names from offsets found in {}'.format( |
| 617 | '\n'.join(files))) |
| 618 | with open(self._GetUnpatchedOrderfileFilename(), 'w') as orderfile: |
| 619 | orderfile.write('\n'.join(ordered_symbols)) |
Matthew Cary | 0f1f681a | 2018-01-22 10:40:51 | [diff] [blame] | 620 | except Exception: |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 621 | for f in files: |
| 622 | self._SaveForDebugging(f) |
| 623 | raise |
| 624 | finally: |
| 625 | self._profiler.Cleanup() |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 626 | |
Matthew Cary | 69e9e42 | 2018-08-10 18:30:06 | [diff] [blame] | 627 | def _MaybeSaveProfile(self, files): |
| 628 | if self._options.profile_save_dir: |
| 629 | logging.info('Saving profiles to %s', self._options.profile_save_dir) |
| 630 | for f in files: |
| 631 | shutil.copy(f, self._options.profile_save_dir) |
| 632 | logging.info('Saved profile %s', f) |
| 633 | |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 634 | def _PatchOrderfile(self): |
| 635 | """Patches the orderfile using clean version of libchrome.so.""" |
| 636 | self._step_recorder.BeginStep('Patch Orderfile') |
Benoit Lize | fefbb27c | 2018-01-17 13:54:18 | [diff] [blame] | 637 | patch_orderfile.GeneratePatchedOrderfile( |
| 638 | self._GetUnpatchedOrderfileFilename(), self._compiler.lib_chrome_so, |
Matthew Cary | f949bba | 2019-02-04 13:39:23 | [diff] [blame] | 639 | self._GetPathToOrderfile(), self._order_outlined_functions) |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 640 | |
| 641 | def _VerifySymbolOrder(self): |
| 642 | self._step_recorder.BeginStep('Verify Symbol Order') |
| 643 | return_code = self._step_recorder.RunCommand( |
| 644 | [self._CHECK_ORDERFILE_SCRIPT, self._compiler.lib_chrome_so, |
| 645 | self._GetPathToOrderfile(), |
| 646 | '--target-arch=' + self._options.arch], |
| 647 | constants.DIR_SOURCE_ROOT, |
| 648 | raise_on_error=False) |
| 649 | if return_code: |
| 650 | self._step_recorder.FailStep('Orderfile check returned %d.' % return_code) |
| 651 | |
| 652 | def _RecordHash(self, file_name): |
| 653 | """Records the hash of the file into the output_data dictionary.""" |
| 654 | self._output_data[os.path.basename(file_name) + '.sha1'] = _GenerateHash( |
| 655 | file_name) |
| 656 | |
| 657 | def _SaveFileLocally(self, file_name, file_sha1): |
| 658 | """Saves the file to a temporary location and prints the sha1sum.""" |
| 659 | if not os.path.exists(self._DIRECTORY_FOR_DEBUG_FILES): |
| 660 | os.makedirs(self._DIRECTORY_FOR_DEBUG_FILES) |
| 661 | shutil.copy(file_name, self._DIRECTORY_FOR_DEBUG_FILES) |
| 662 | print 'File: %s, saved in: %s, sha1sum: %s' % ( |
| 663 | file_name, self._DIRECTORY_FOR_DEBUG_FILES, file_sha1) |
| 664 | |
| 665 | def _SaveForDebugging(self, filename): |
| 666 | """Uploads the file to cloud storage or saves to a temporary location.""" |
| 667 | file_sha1 = _GenerateHash(filename) |
| 668 | if not self._options.buildbot: |
| 669 | self._SaveFileLocally(filename, file_sha1) |
| 670 | else: |
| 671 | print 'Uploading file for debugging: ' + filename |
| 672 | self._orderfile_updater.UploadToCloudStorage( |
| 673 | filename, use_debug_location=True) |
| 674 | |
| 675 | def _SaveForDebuggingWithOverwrite(self, file_name): |
| 676 | """Uploads and overwrites the file in cloud storage or copies locally. |
| 677 | |
| 678 | Should be used for large binaries like lib_chrome_so. |
| 679 | |
| 680 | Args: |
| 681 | file_name: (str) File to upload. |
| 682 | """ |
| 683 | file_sha1 = _GenerateHash(file_name) |
| 684 | if not self._options.buildbot: |
| 685 | self._SaveFileLocally(file_name, file_sha1) |
| 686 | else: |
| 687 | print 'Uploading file for debugging: %s, sha1sum: %s' % ( |
| 688 | file_name, file_sha1) |
| 689 | upload_location = '%s/%s' % ( |
| 690 | self._CLOUD_STORAGE_BUCKET_FOR_DEBUG, os.path.basename(file_name)) |
| 691 | self._step_recorder.RunCommand([ |
| 692 | 'gsutil.py', 'cp', file_name, 'gs://' + upload_location]) |
| 693 | print ('Uploaded to: https://sandbox.google.com/storage/' + |
| 694 | upload_location) |
| 695 | |
| 696 | def _MaybeArchiveOrderfile(self, filename): |
| 697 | """In buildbot configuration, uploads the generated orderfile to |
| 698 | Google Cloud Storage. |
| 699 | |
| 700 | Args: |
| 701 | filename: (str) Orderfile to upload. |
| 702 | """ |
Matthew Cary | 91df979 | 2018-11-30 14:35:15 | [diff] [blame] | 703 | # First compute hashes so that we can download them later if we need to. |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 704 | self._step_recorder.BeginStep('Compute hash for ' + filename) |
| 705 | self._RecordHash(filename) |
| 706 | if self._options.buildbot: |
| 707 | self._step_recorder.BeginStep('Archive ' + filename) |
| 708 | self._orderfile_updater.UploadToCloudStorage( |
| 709 | filename, use_debug_location=False) |
| 710 | |
Egor Pasko | bce64d01 | 2018-11-20 12:02:37 | [diff] [blame] | 711 | def UploadReadyOrderfiles(self): |
| 712 | self._step_recorder.BeginStep('Upload Ready Orderfiles') |
| 713 | for file_name in [self._GetUnpatchedOrderfileFilename(), |
| 714 | self._GetPathToOrderfile()]: |
| 715 | self._orderfile_updater.UploadToCloudStorage( |
| 716 | file_name, use_debug_location=False) |
| 717 | |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 718 | def _GetHashFilePathAndContents(self, base_file): |
| 719 | """Gets the name and content of the hash file created from uploading the |
| 720 | given file. |
| 721 | |
| 722 | Args: |
| 723 | base_file: The file that was uploaded to cloud storage. |
| 724 | |
| 725 | Returns: |
| 726 | A tuple of the hash file name, relative to the clank repo path, and the |
| 727 | content, which should be the sha1 hash of the file |
| 728 | ('base_file.sha1', hash) |
| 729 | """ |
| 730 | abs_file_name = base_file + '.sha1' |
| 731 | rel_file_name = os.path.relpath(abs_file_name, self._CLANK_REPO) |
| 732 | with open(abs_file_name, 'r') as f: |
| 733 | return (rel_file_name, f.read()) |
| 734 | |
| 735 | def Generate(self): |
| 736 | """Generates and maybe upload an order.""" |
| 737 | profile_uploaded = False |
| 738 | orderfile_uploaded = False |
| 739 | |
Matthew Cary | e840064 | 2018-06-14 15:43:02 | [diff] [blame] | 740 | assert (bool(self._options.profile) ^ |
| 741 | bool(self._options.manual_symbol_offsets)) |
Matthew Cary | 78aae16 | 2018-08-10 17:16:30 | [diff] [blame] | 742 | if self._options.system_health_orderfile and not self._options.profile: |
| 743 | raise AssertionError('--system_health_orderfile must be not be used ' |
| 744 | 'with --skip-profile') |
| 745 | if (self._options.manual_symbol_offsets and |
| 746 | not self._options.system_health_orderfile): |
| 747 | raise AssertionError('--manual-symbol-offsets must be used with ' |
| 748 | '--system_health_orderfile.') |
Matthew Cary | e840064 | 2018-06-14 15:43:02 | [diff] [blame] | 749 | |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 750 | if self._options.profile: |
| 751 | try: |
| 752 | _UnstashOutputDirectory(self._instrumented_out_dir) |
| 753 | self._compiler = ClankCompiler( |
| 754 | self._instrumented_out_dir, |
| 755 | self._step_recorder, self._options.arch, self._options.jobs, |
| 756 | self._options.max_load, self._options.use_goma, |
Matthew Cary | d6bfcb7 | 2018-09-14 11:27:46 | [diff] [blame] | 757 | self._options.goma_dir, self._options.system_health_orderfile, |
Monica Salama | eea2d94 | 2019-03-11 12:36:18 | [diff] [blame^] | 758 | self._monochrome) |
Matthew Cary | a2bea45 | 2018-11-13 10:21:07 | [diff] [blame] | 759 | if not self._options.pregenerated_profiles: |
| 760 | # If there are pregenerated profiles, the instrumented build should |
| 761 | # not be changed to avoid invalidating the pregenerated profile |
| 762 | # offsets. |
| 763 | self._compiler.CompileChromeApk(True) |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 764 | self._GenerateAndProcessProfile() |
| 765 | self._MaybeArchiveOrderfile(self._GetUnpatchedOrderfileFilename()) |
| 766 | profile_uploaded = True |
| 767 | finally: |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 768 | _StashOutputDirectory(self._instrumented_out_dir) |
Matthew Cary | e840064 | 2018-06-14 15:43:02 | [diff] [blame] | 769 | elif self._options.manual_symbol_offsets: |
| 770 | assert self._options.manual_libname |
| 771 | assert self._options.manual_objdir |
| 772 | with file(self._options.manual_symbol_offsets) as f: |
| 773 | symbol_offsets = [int(x) for x in f.xreadlines()] |
| 774 | processor = process_profiles.SymbolOffsetProcessor( |
Matthew Cary | b46ad28 | 2018-11-23 14:43:57 | [diff] [blame] | 775 | self._compiler.manual_libname) |
Matthew Cary | e840064 | 2018-06-14 15:43:02 | [diff] [blame] | 776 | generator = cyglog_to_orderfile.OffsetOrderfileGenerator( |
| 777 | processor, cyglog_to_orderfile.ObjectFileProcessor( |
| 778 | self._options.manual_objdir)) |
| 779 | ordered_sections = generator.GetOrderedSections(symbol_offsets) |
| 780 | if not ordered_sections: # Either None or empty is a problem. |
| 781 | raise Exception('Failed to get ordered sections') |
| 782 | with open(self._GetUnpatchedOrderfileFilename(), 'w') as orderfile: |
| 783 | orderfile.write('\n'.join(ordered_sections)) |
| 784 | |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 785 | if self._options.patch: |
| 786 | if self._options.profile: |
| 787 | self._RemoveBlanks(self._GetUnpatchedOrderfileFilename(), |
| 788 | self._GetPathToOrderfile()) |
| 789 | try: |
| 790 | _UnstashOutputDirectory(self._uninstrumented_out_dir) |
| 791 | self._compiler = ClankCompiler( |
| 792 | self._uninstrumented_out_dir, self._step_recorder, |
| 793 | self._options.arch, self._options.jobs, self._options.max_load, |
Matthew Cary | 78aae16 | 2018-08-10 17:16:30 | [diff] [blame] | 794 | self._options.use_goma, self._options.goma_dir, |
Monica Salama | eea2d94 | 2019-03-11 12:36:18 | [diff] [blame^] | 795 | self._options.system_health_orderfile, self._monochrome) |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 796 | self._compiler.CompileLibchrome(False) |
| 797 | self._PatchOrderfile() |
| 798 | # Because identical code folding is a bit different with and without |
| 799 | # the orderfile build, we need to re-patch the orderfile with code |
| 800 | # folding as close to the final version as possible. |
| 801 | self._compiler.CompileLibchrome(False, force_relink=True) |
| 802 | self._PatchOrderfile() |
| 803 | self._compiler.CompileLibchrome(False, force_relink=True) |
| 804 | self._VerifySymbolOrder() |
| 805 | self._MaybeArchiveOrderfile(self._GetPathToOrderfile()) |
| 806 | finally: |
| 807 | _StashOutputDirectory(self._uninstrumented_out_dir) |
| 808 | orderfile_uploaded = True |
| 809 | |
| 810 | if (self._options.buildbot and self._options.netrc |
| 811 | and not self._step_recorder.ErrorRecorded()): |
| 812 | unpatched_orderfile_filename = ( |
| 813 | self._GetUnpatchedOrderfileFilename() if profile_uploaded else None) |
| 814 | orderfile_filename = ( |
Benoit Lize | d913be8 | 2017-10-24 13:38:27 | [diff] [blame] | 815 | self._GetPathToOrderfile() if orderfile_uploaded else None) |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 816 | self._orderfile_updater.CommitFileHashes( |
| 817 | unpatched_orderfile_filename, orderfile_filename) |
| 818 | |
| 819 | self._step_recorder.EndStep() |
| 820 | return not self._step_recorder.ErrorRecorded() |
| 821 | |
| 822 | def GetReportingData(self): |
| 823 | """Get a dictionary of reporting data (timings, output hashes)""" |
| 824 | self._output_data['timings'] = self._step_recorder.timings |
| 825 | return self._output_data |
| 826 | |
| 827 | |
Benoit Lize | a1b64f8 | 2017-12-07 10:12:50 | [diff] [blame] | 828 | def CreateArgumentParser(): |
| 829 | """Creates and returns the argument parser.""" |
| 830 | parser = argparse.ArgumentParser() |
| 831 | parser.add_argument( |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 832 | '--buildbot', action='store_true', |
| 833 | help='If true, the script expects to be run on a buildbot') |
Benoit Lize | a1b64f8 | 2017-12-07 10:12:50 | [diff] [blame] | 834 | parser.add_argument( |
Matthew Cary | 453ff145 | 2018-07-18 12:19:35 | [diff] [blame] | 835 | '--device', default=None, type=str, |
| 836 | help='Device serial number on which to run profiling.') |
| 837 | parser.add_argument( |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 838 | '--verify', action='store_true', |
| 839 | help='If true, the script only verifies the current orderfile') |
Benoit Lize | a1b64f8 | 2017-12-07 10:12:50 | [diff] [blame] | 840 | parser.add_argument('--target-arch', action='store', dest='arch', |
Stephen Martinis | 71b391b5 | 2018-12-04 15:08:04 | [diff] [blame] | 841 | default='arm', |
Matthew Cary | 53f74ba | 2019-01-24 10:07:18 | [diff] [blame] | 842 | choices=['arm', 'arm64'], |
| 843 | help='The target architecture for which to build.') |
Benoit Lize | a1b64f8 | 2017-12-07 10:12:50 | [diff] [blame] | 844 | parser.add_argument('--output-json', action='store', dest='json_file', |
| 845 | help='Location to save stats in json format') |
| 846 | parser.add_argument( |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 847 | '--skip-profile', action='store_false', dest='profile', default=True, |
| 848 | help='Don\'t generate a profile on the device. Only patch from the ' |
| 849 | 'existing profile.') |
Benoit Lize | a1b64f8 | 2017-12-07 10:12:50 | [diff] [blame] | 850 | parser.add_argument( |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 851 | '--skip-patch', action='store_false', dest='patch', default=True, |
| 852 | help='Only generate the raw (unpatched) orderfile, don\'t patch it.') |
Benoit Lize | a1b64f8 | 2017-12-07 10:12:50 | [diff] [blame] | 853 | parser.add_argument( |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 854 | '--netrc', action='store', |
| 855 | help='A custom .netrc file to use for git checkin. Only used on bots.') |
Benoit Lize | a1b64f8 | 2017-12-07 10:12:50 | [diff] [blame] | 856 | parser.add_argument( |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 857 | '--branch', action='store', default='master', |
| 858 | help='When running on buildbot with a netrc, the branch orderfile ' |
| 859 | 'hashes get checked into.') |
| 860 | # Note: -j50 was causing issues on the bot. |
Benoit Lize | a1b64f8 | 2017-12-07 10:12:50 | [diff] [blame] | 861 | parser.add_argument( |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 862 | '-j', '--jobs', action='store', default=20, |
| 863 | help='Number of jobs to use for compilation.') |
Benoit Lize | a1b64f8 | 2017-12-07 10:12:50 | [diff] [blame] | 864 | parser.add_argument( |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 865 | '-l', '--max-load', action='store', default=4, help='Max cpu load.') |
Benoit Lize | a1b64f8 | 2017-12-07 10:12:50 | [diff] [blame] | 866 | parser.add_argument('--goma-dir', help='GOMA directory.') |
| 867 | parser.add_argument( |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 868 | '--use-goma', action='store_true', help='Enable GOMA.', default=False) |
Benoit Lize | a1b64f8 | 2017-12-07 10:12:50 | [diff] [blame] | 869 | parser.add_argument('--adb-path', help='Path to the adb binary.') |
Matthew Cary | e840064 | 2018-06-14 15:43:02 | [diff] [blame] | 870 | |
Matthew Cary | 04f4103 | 2018-12-10 15:55:27 | [diff] [blame] | 871 | parser.add_argument('--nosystem-health-orderfile', action='store_false', |
| 872 | dest='system_health_orderfile', default=True, |
Matthew Cary | c262f5d | 2018-09-19 14:36:28 | [diff] [blame] | 873 | help=('Create an orderfile based on an about:blank ' |
| 874 | 'startup benchmark instead of system health ' |
| 875 | 'benchmarks.')) |
Monica Salama | eea2d94 | 2019-03-11 12:36:18 | [diff] [blame^] | 876 | parser.add_argument( |
| 877 | '--use-legacy-chrome-apk', action='store_true', default=False, |
| 878 | help=('Compile and instrument chrome for [L, K] devices.')) |
Matthew Cary | 78aae16 | 2018-08-10 17:16:30 | [diff] [blame] | 879 | |
Matthew Cary | e840064 | 2018-06-14 15:43:02 | [diff] [blame] | 880 | parser.add_argument('--manual-symbol-offsets', default=None, type=str, |
| 881 | help=('File of list of ordered symbol offsets generated ' |
| 882 | 'by manual profiling. Must set other --manual* ' |
| 883 | 'flags if this is used, and must --skip-profile.')) |
| 884 | parser.add_argument('--manual-libname', default=None, type=str, |
| 885 | help=('Library filename corresponding to ' |
| 886 | '--manual-symbol-offsets.')) |
| 887 | parser.add_argument('--manual-objdir', default=None, type=str, |
| 888 | help=('Root of object file directory corresponding to ' |
| 889 | '--manual-symbol-offsets.')) |
Matthew Cary | f949bba | 2019-02-04 13:39:23 | [diff] [blame] | 890 | parser.add_argument('--noorder-outlined-functions', action='store_true', |
| 891 | help='Disable outlined functions in the orderfile.') |
Matthew Cary | 69e9e42 | 2018-08-10 18:30:06 | [diff] [blame] | 892 | parser.add_argument('--pregenerated-profiles', default=None, type=str, |
| 893 | help=('Pregenerated profiles to use instead of running ' |
| 894 | 'profile step. Cannot be used with ' |
| 895 | '--skip-profiles.')) |
| 896 | parser.add_argument('--profile-save-dir', default=None, type=str, |
| 897 | help=('Directory to save any profiles created. These can ' |
| 898 | 'be used with --pregenerated-profiles. Cannot be ' |
| 899 | 'used with --skip-profiles.')) |
Egor Pasko | bce64d01 | 2018-11-20 12:02:37 | [diff] [blame] | 900 | parser.add_argument('--upload-ready-orderfiles', action='store_true', |
| 901 | help=('Skip orderfile generation and manually upload ' |
| 902 | 'orderfiles (both patched and unpatched) from ' |
| 903 | 'their normal location in the tree to the cloud ' |
| 904 | 'storage. DANGEROUS! USE WITH CARE!')) |
Matthew Cary | e840064 | 2018-06-14 15:43:02 | [diff] [blame] | 905 | |
Benoit Lize | a1b64f8 | 2017-12-07 10:12:50 | [diff] [blame] | 906 | profile_android_startup.AddProfileCollectionArguments(parser) |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 907 | return parser |
| 908 | |
| 909 | |
| 910 | def CreateOrderfile(options, orderfile_updater_class): |
| 911 | """Creates an oderfile. |
| 912 | |
| 913 | Args: |
| 914 | options: As returned from optparse.OptionParser.parse_args() |
| 915 | orderfile_updater_class: (OrderfileUpdater) subclass of OrderfileUpdater. |
| 916 | |
| 917 | Returns: |
| 918 | True iff success. |
| 919 | """ |
| 920 | logging.basicConfig(level=logging.INFO) |
| 921 | devil_chromium.Initialize(adb_path=options.adb_path) |
| 922 | |
Egor Pasko | 93e514e | 2017-10-31 13:32:36 | [diff] [blame] | 923 | generator = OrderfileGenerator(options, orderfile_updater_class) |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 924 | try: |
| 925 | if options.verify: |
Egor Pasko | 93e514e | 2017-10-31 13:32:36 | [diff] [blame] | 926 | generator._VerifySymbolOrder() |
Egor Pasko | bce64d01 | 2018-11-20 12:02:37 | [diff] [blame] | 927 | elif options.upload_ready_orderfiles: |
| 928 | return generator.UploadReadyOrderfiles() |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 929 | else: |
Egor Pasko | 93e514e | 2017-10-31 13:32:36 | [diff] [blame] | 930 | return generator.Generate() |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 931 | finally: |
Egor Pasko | 93e514e | 2017-10-31 13:32:36 | [diff] [blame] | 932 | json_output = json.dumps(generator.GetReportingData(), |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 933 | indent=2) + '\n' |
| 934 | if options.json_file: |
| 935 | with open(options.json_file, 'w') as f: |
| 936 | f.write(json_output) |
| 937 | print json_output |
| 938 | return False |
| 939 | |
| 940 | |
Benoit Lize | a1b64f8 | 2017-12-07 10:12:50 | [diff] [blame] | 941 | def main(): |
| 942 | parser = CreateArgumentParser() |
| 943 | options = parser.parse_args() |
Benoit Lize | a3fe293 | 2017-10-20 10:24:52 | [diff] [blame] | 944 | return 0 if CreateOrderfile(options, OrderfileUpdater) else 1 |
| 945 | |
| 946 | |
| 947 | if __name__ == '__main__': |
Benoit Lize | a1b64f8 | 2017-12-07 10:12:50 | [diff] [blame] | 948 | sys.exit(main()) |