Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file |
| 3 | # for details. All rights reserved. Use of this source code is governed by a |
| 4 | # BSD-style license that can be found in the LICENSE file. |
| 5 | |
| 6 | # Convenience script for running run_on_app.py finding minimum memory need for |
| 7 | # compiling a given app back in time. This utilizes the prebuilt r8 jars on |
| 8 | # cloud storage. |
| 9 | # The script find all commits that exists on cloud storage in the given range. |
| 10 | # It will then run the oldest and newest such commit, and gradually fill in |
| 11 | # the commits in between. |
| 12 | |
| 13 | import optparse |
| 14 | import os |
| 15 | import subprocess |
| 16 | import sys |
| 17 | import utils |
| 18 | |
Rico Wind | f2017bf | 2019-01-10 09:45:33 +0100 | [diff] [blame] | 19 | MASTER_COMMITS = 'gs://r8-releases/raw/master' |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 20 | APPS = ['gmscore', 'nest', 'youtube', 'gmail', 'chrome'] |
| 21 | COMPILERS = ['d8', 'r8'] |
| 22 | |
| 23 | def ParseOptions(argv): |
| 24 | result = optparse.OptionParser() |
| 25 | result.add_option('--compiler', |
| 26 | help='The compiler to use', |
| 27 | default='d8', |
| 28 | choices=COMPILERS) |
| 29 | result.add_option('--app', |
| 30 | help='What app to run on', |
| 31 | default='gmail', |
| 32 | choices=APPS) |
| 33 | result.add_option('--top', |
| 34 | default=utils.get_HEAD_sha1(), |
| 35 | help='The most recent commit to test') |
| 36 | result.add_option('--bottom', |
| 37 | help='The oldest commit to test') |
| 38 | result.add_option('--output', |
| 39 | default='build', |
| 40 | help='Directory where to output results') |
| 41 | return result.parse_args(argv) |
| 42 | |
| 43 | |
| 44 | class GitCommit(object): |
Rico Wind | f2017bf | 2019-01-10 09:45:33 +0100 | [diff] [blame] | 45 | def __init__(self, git_hash, destination_dir, destination, timestamp): |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 46 | self.git_hash = git_hash |
Rico Wind | f2017bf | 2019-01-10 09:45:33 +0100 | [diff] [blame] | 47 | self.destination_dir = destination_dir |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 48 | self.destination = destination |
| 49 | self.timestamp = timestamp |
| 50 | |
| 51 | def __str__(self): |
| 52 | return '%s : %s (%s)' % (self.git_hash, self.destination, self.timestamp) |
| 53 | |
| 54 | def __repr__(self): |
| 55 | return self.__str__() |
| 56 | |
| 57 | def git_commit_from_hash(hash): |
| 58 | commit_timestamp = subprocess.check_output(['git', 'show', '--no-patch', |
| 59 | '--no-notes', '--pretty=\'%ct\'', |
| 60 | hash]).strip().strip('\'') |
Rico Wind | f2017bf | 2019-01-10 09:45:33 +0100 | [diff] [blame] | 61 | destination_dir = '%s/%s/' % (MASTER_COMMITS, hash) |
| 62 | destination = '%s%s' % (destination_dir, 'r8.jar') |
| 63 | commit = GitCommit(hash, destination_dir, destination, commit_timestamp) |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 64 | return commit |
| 65 | |
| 66 | def enumerate_git_commits(options): |
| 67 | top = options.top if options.top else utils.get_HEAD_sha1() |
| 68 | # TODO(ricow): if not set, search back 1000 |
| 69 | if not options.bottom: |
| 70 | raise Exception('No bottom specified') |
| 71 | bottom = options.bottom |
| 72 | output = subprocess.check_output(['git', 'rev-list', '--first-parent', top]) |
| 73 | found_bottom = False |
| 74 | commits = [] |
| 75 | for c in output.splitlines(): |
| 76 | commits.append(git_commit_from_hash(c.strip())) |
| 77 | if c.strip() == bottom: |
| 78 | found_bottom = True |
| 79 | break |
| 80 | if not found_bottom: |
| 81 | raise Exception('Bottom not found, did you not use a merge commit') |
| 82 | return commits |
| 83 | |
| 84 | def get_available_commits(commits): |
Rico Wind | f2017bf | 2019-01-10 09:45:33 +0100 | [diff] [blame] | 85 | cloud_commits = subprocess.check_output(['gsutil.py', 'ls', MASTER_COMMITS]).splitlines() |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 86 | available_commits = [] |
| 87 | for commit in commits: |
Rico Wind | f2017bf | 2019-01-10 09:45:33 +0100 | [diff] [blame] | 88 | if commit.destination_dir in cloud_commits: |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 89 | available_commits.append(commit) |
| 90 | return available_commits |
| 91 | |
| 92 | def print_commits(commits): |
| 93 | for commit in commits: |
| 94 | print(commit) |
| 95 | |
| 96 | def permutate_range(start, end): |
| 97 | diff = end - start |
| 98 | assert diff >= 0 |
| 99 | if diff == 1: |
| 100 | return [start, end] |
| 101 | if diff == 0: |
| 102 | return [start] |
| 103 | half = end - (diff / 2) |
| 104 | numbers = [half] |
| 105 | first_half = permutate_range(start, half - 1) |
| 106 | second_half = permutate_range(half + 1, end) |
| 107 | for index in range(len(first_half)): |
| 108 | numbers.append(first_half[index]) |
| 109 | if index < len(second_half): |
| 110 | numbers.append(second_half[index]) |
| 111 | return numbers |
| 112 | |
| 113 | def permutate(number_of_commits): |
| 114 | assert number_of_commits > 0 |
| 115 | numbers = permutate_range(0, number_of_commits - 1) |
| 116 | assert all(n in numbers for n in range(number_of_commits)) |
| 117 | return numbers |
| 118 | |
| 119 | def pull_r8_from_cloud(commit): |
| 120 | utils.download_file_from_cloud_storage(commit.destination, utils.R8_JAR) |
| 121 | |
| 122 | def run_on_app(options, commit): |
| 123 | app = options.app |
| 124 | compiler = options.compiler |
| 125 | cmd = ['tools/run_on_app.py', '--app', app, '--compiler', compiler, |
Rico Wind | f2017bf | 2019-01-10 09:45:33 +0100 | [diff] [blame] | 126 | '--no-build', '--find-min-xmx'] |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 127 | stdout = subprocess.check_output(cmd) |
| 128 | output_path = options.output or 'build' |
| 129 | time_commit = '%s_%s' % (commit.timestamp, commit.git_hash) |
| 130 | time_commit_path = os.path.join(output_path, time_commit) |
| 131 | if not os.path.exists(time_commit_path): |
| 132 | os.makedirs(time_commit_path) |
| 133 | stdout_path = os.path.join(time_commit_path, 'stdout') |
| 134 | with open(stdout_path, 'w') as f: |
| 135 | f.write(stdout) |
| 136 | print('Wrote stdout to: %s' % stdout_path) |
| 137 | |
| 138 | |
| 139 | def benchmark(commits, options): |
| 140 | commit_permutations = permutate(len(commits)) |
Rico Wind | f2017bf | 2019-01-10 09:45:33 +0100 | [diff] [blame] | 141 | count = 0 |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 142 | for index in commit_permutations: |
Rico Wind | f2017bf | 2019-01-10 09:45:33 +0100 | [diff] [blame] | 143 | count += 1 |
| 144 | print('Running commit %s out of %s' % (count, len(commits))) |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 145 | commit = commits[index] |
Rico Wind | f2017bf | 2019-01-10 09:45:33 +0100 | [diff] [blame] | 146 | if not utils.cloud_storage_exists(commit.destination): |
| 147 | # We may have a directory, but no r8.jar |
| 148 | continue |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 149 | pull_r8_from_cloud(commit) |
| 150 | print('Running for commit: %s' % commit.git_hash) |
| 151 | run_on_app(options, commit) |
| 152 | |
| 153 | def main(argv): |
| 154 | (options, args) = ParseOptions(argv) |
| 155 | if not options.app: |
| 156 | raise Exception('Please specify an app') |
| 157 | commits = enumerate_git_commits(options) |
| 158 | available_commits = get_available_commits(commits) |
| 159 | print('Running for:') |
| 160 | print_commits(available_commits) |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 161 | benchmark(available_commits, options) |
| 162 | |
| 163 | if __name__ == '__main__': |
| 164 | sys.exit(main(sys.argv[1:])) |