blob: f23faaae59fbec25087a59dfd08e48832def5365 [file] [log] [blame]
Rico Windf9231222019-01-08 14:43:52 +01001#!/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
13import optparse
14import os
15import subprocess
16import sys
17import utils
18
Rico Windf2017bf2019-01-10 09:45:33 +010019MASTER_COMMITS = 'gs://r8-releases/raw/master'
Rico Windf9231222019-01-08 14:43:52 +010020APPS = ['gmscore', 'nest', 'youtube', 'gmail', 'chrome']
21COMPILERS = ['d8', 'r8']
22
23def 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
44class GitCommit(object):
Rico Windf2017bf2019-01-10 09:45:33 +010045 def __init__(self, git_hash, destination_dir, destination, timestamp):
Rico Windf9231222019-01-08 14:43:52 +010046 self.git_hash = git_hash
Rico Windf2017bf2019-01-10 09:45:33 +010047 self.destination_dir = destination_dir
Rico Windf9231222019-01-08 14:43:52 +010048 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
57def 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 Windf2017bf2019-01-10 09:45:33 +010061 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 Windf9231222019-01-08 14:43:52 +010064 return commit
65
66def 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
84def get_available_commits(commits):
Rico Windf2017bf2019-01-10 09:45:33 +010085 cloud_commits = subprocess.check_output(['gsutil.py', 'ls', MASTER_COMMITS]).splitlines()
Rico Windf9231222019-01-08 14:43:52 +010086 available_commits = []
87 for commit in commits:
Rico Windf2017bf2019-01-10 09:45:33 +010088 if commit.destination_dir in cloud_commits:
Rico Windf9231222019-01-08 14:43:52 +010089 available_commits.append(commit)
90 return available_commits
91
92def print_commits(commits):
93 for commit in commits:
94 print(commit)
95
96def 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
113def 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
119def pull_r8_from_cloud(commit):
120 utils.download_file_from_cloud_storage(commit.destination, utils.R8_JAR)
121
122def 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 Windf2017bf2019-01-10 09:45:33 +0100126 '--no-build', '--find-min-xmx']
Rico Windf9231222019-01-08 14:43:52 +0100127 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
139def benchmark(commits, options):
140 commit_permutations = permutate(len(commits))
Rico Windf2017bf2019-01-10 09:45:33 +0100141 count = 0
Rico Windf9231222019-01-08 14:43:52 +0100142 for index in commit_permutations:
Rico Windf2017bf2019-01-10 09:45:33 +0100143 count += 1
144 print('Running commit %s out of %s' % (count, len(commits)))
Rico Windf9231222019-01-08 14:43:52 +0100145 commit = commits[index]
Rico Windf2017bf2019-01-10 09:45:33 +0100146 if not utils.cloud_storage_exists(commit.destination):
147 # We may have a directory, but no r8.jar
148 continue
Rico Windf9231222019-01-08 14:43:52 +0100149 pull_r8_from_cloud(commit)
150 print('Running for commit: %s' % commit.git_hash)
151 run_on_app(options, commit)
152
153def 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 Windf9231222019-01-08 14:43:52 +0100161 benchmark(available_commits, options)
162
163if __name__ == '__main__':
164 sys.exit(main(sys.argv[1:]))