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') |
Jinseong Jeon | 158a3f1 | 2019-02-08 01:40:59 -0800 | [diff] [blame] | 41 | result.add_option('--timeout', |
| 42 | type=int, |
| 43 | default=0, |
| 44 | help='Set timeout instead of waiting for OOM.') |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 45 | return result.parse_args(argv) |
| 46 | |
| 47 | |
| 48 | class GitCommit(object): |
Rico Wind | f2017bf | 2019-01-10 09:45:33 +0100 | [diff] [blame] | 49 | def __init__(self, git_hash, destination_dir, destination, timestamp): |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 50 | self.git_hash = git_hash |
Rico Wind | f2017bf | 2019-01-10 09:45:33 +0100 | [diff] [blame] | 51 | self.destination_dir = destination_dir |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 52 | self.destination = destination |
| 53 | self.timestamp = timestamp |
| 54 | |
| 55 | def __str__(self): |
| 56 | return '%s : %s (%s)' % (self.git_hash, self.destination, self.timestamp) |
| 57 | |
| 58 | def __repr__(self): |
| 59 | return self.__str__() |
| 60 | |
| 61 | def git_commit_from_hash(hash): |
| 62 | commit_timestamp = subprocess.check_output(['git', 'show', '--no-patch', |
| 63 | '--no-notes', '--pretty=\'%ct\'', |
| 64 | hash]).strip().strip('\'') |
Rico Wind | f2017bf | 2019-01-10 09:45:33 +0100 | [diff] [blame] | 65 | destination_dir = '%s/%s/' % (MASTER_COMMITS, hash) |
| 66 | destination = '%s%s' % (destination_dir, 'r8.jar') |
| 67 | commit = GitCommit(hash, destination_dir, destination, commit_timestamp) |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 68 | return commit |
| 69 | |
| 70 | def enumerate_git_commits(options): |
| 71 | top = options.top if options.top else utils.get_HEAD_sha1() |
| 72 | # TODO(ricow): if not set, search back 1000 |
| 73 | if not options.bottom: |
| 74 | raise Exception('No bottom specified') |
| 75 | bottom = options.bottom |
| 76 | output = subprocess.check_output(['git', 'rev-list', '--first-parent', top]) |
| 77 | found_bottom = False |
| 78 | commits = [] |
| 79 | for c in output.splitlines(): |
| 80 | commits.append(git_commit_from_hash(c.strip())) |
| 81 | if c.strip() == bottom: |
| 82 | found_bottom = True |
| 83 | break |
| 84 | if not found_bottom: |
| 85 | raise Exception('Bottom not found, did you not use a merge commit') |
| 86 | return commits |
| 87 | |
| 88 | def get_available_commits(commits): |
Rico Wind | f2017bf | 2019-01-10 09:45:33 +0100 | [diff] [blame] | 89 | cloud_commits = subprocess.check_output(['gsutil.py', 'ls', MASTER_COMMITS]).splitlines() |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 90 | available_commits = [] |
| 91 | for commit in commits: |
Rico Wind | f2017bf | 2019-01-10 09:45:33 +0100 | [diff] [blame] | 92 | if commit.destination_dir in cloud_commits: |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 93 | available_commits.append(commit) |
| 94 | return available_commits |
| 95 | |
| 96 | def print_commits(commits): |
| 97 | for commit in commits: |
| 98 | print(commit) |
| 99 | |
| 100 | def permutate_range(start, end): |
| 101 | diff = end - start |
| 102 | assert diff >= 0 |
| 103 | if diff == 1: |
| 104 | return [start, end] |
| 105 | if diff == 0: |
| 106 | return [start] |
| 107 | half = end - (diff / 2) |
| 108 | numbers = [half] |
| 109 | first_half = permutate_range(start, half - 1) |
| 110 | second_half = permutate_range(half + 1, end) |
| 111 | for index in range(len(first_half)): |
| 112 | numbers.append(first_half[index]) |
| 113 | if index < len(second_half): |
| 114 | numbers.append(second_half[index]) |
| 115 | return numbers |
| 116 | |
| 117 | def permutate(number_of_commits): |
| 118 | assert number_of_commits > 0 |
| 119 | numbers = permutate_range(0, number_of_commits - 1) |
| 120 | assert all(n in numbers for n in range(number_of_commits)) |
| 121 | return numbers |
| 122 | |
| 123 | def pull_r8_from_cloud(commit): |
| 124 | utils.download_file_from_cloud_storage(commit.destination, utils.R8_JAR) |
| 125 | |
| 126 | def run_on_app(options, commit): |
| 127 | app = options.app |
| 128 | compiler = options.compiler |
Jinseong Jeon | 158a3f1 | 2019-02-08 01:40:59 -0800 | [diff] [blame] | 129 | cmd = ['tools/run_on_app.py', |
| 130 | '--app', app, |
| 131 | '--compiler', compiler, |
| 132 | '--timeout', str(options.timeout), |
Rico Wind | f2017bf | 2019-01-10 09:45:33 +0100 | [diff] [blame] | 133 | '--no-build', '--find-min-xmx'] |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 134 | stdout = subprocess.check_output(cmd) |
| 135 | output_path = options.output or 'build' |
| 136 | time_commit = '%s_%s' % (commit.timestamp, commit.git_hash) |
| 137 | time_commit_path = os.path.join(output_path, time_commit) |
| 138 | if not os.path.exists(time_commit_path): |
| 139 | os.makedirs(time_commit_path) |
| 140 | stdout_path = os.path.join(time_commit_path, 'stdout') |
| 141 | with open(stdout_path, 'w') as f: |
| 142 | f.write(stdout) |
| 143 | print('Wrote stdout to: %s' % stdout_path) |
| 144 | |
| 145 | |
| 146 | def benchmark(commits, options): |
| 147 | commit_permutations = permutate(len(commits)) |
Rico Wind | f2017bf | 2019-01-10 09:45:33 +0100 | [diff] [blame] | 148 | count = 0 |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 149 | for index in commit_permutations: |
Rico Wind | f2017bf | 2019-01-10 09:45:33 +0100 | [diff] [blame] | 150 | count += 1 |
| 151 | print('Running commit %s out of %s' % (count, len(commits))) |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 152 | commit = commits[index] |
Rico Wind | f2017bf | 2019-01-10 09:45:33 +0100 | [diff] [blame] | 153 | if not utils.cloud_storage_exists(commit.destination): |
| 154 | # We may have a directory, but no r8.jar |
| 155 | continue |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 156 | pull_r8_from_cloud(commit) |
| 157 | print('Running for commit: %s' % commit.git_hash) |
| 158 | run_on_app(options, commit) |
| 159 | |
| 160 | def main(argv): |
| 161 | (options, args) = ParseOptions(argv) |
| 162 | if not options.app: |
| 163 | raise Exception('Please specify an app') |
| 164 | commits = enumerate_git_commits(options) |
| 165 | available_commits = get_available_commits(commits) |
| 166 | print('Running for:') |
| 167 | print_commits(available_commits) |
Rico Wind | f923122 | 2019-01-08 14:43:52 +0100 | [diff] [blame] | 168 | benchmark(available_commits, options) |
| 169 | |
| 170 | if __name__ == '__main__': |
| 171 | sys.exit(main(sys.argv[1:])) |