blob: 376426fa2e8229596c6264136cb7b4212793bc6a [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')
Jinseong Jeon158a3f12019-02-08 01:40:59 -080041 result.add_option('--timeout',
42 type=int,
43 default=0,
44 help='Set timeout instead of waiting for OOM.')
Rico Windf9231222019-01-08 14:43:52 +010045 return result.parse_args(argv)
46
47
48class GitCommit(object):
Rico Windf2017bf2019-01-10 09:45:33 +010049 def __init__(self, git_hash, destination_dir, destination, timestamp):
Rico Windf9231222019-01-08 14:43:52 +010050 self.git_hash = git_hash
Rico Windf2017bf2019-01-10 09:45:33 +010051 self.destination_dir = destination_dir
Rico Windf9231222019-01-08 14:43:52 +010052 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
61def 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 Windf2017bf2019-01-10 09:45:33 +010065 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 Windf9231222019-01-08 14:43:52 +010068 return commit
69
70def 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
88def get_available_commits(commits):
Rico Windf2017bf2019-01-10 09:45:33 +010089 cloud_commits = subprocess.check_output(['gsutil.py', 'ls', MASTER_COMMITS]).splitlines()
Rico Windf9231222019-01-08 14:43:52 +010090 available_commits = []
91 for commit in commits:
Rico Windf2017bf2019-01-10 09:45:33 +010092 if commit.destination_dir in cloud_commits:
Rico Windf9231222019-01-08 14:43:52 +010093 available_commits.append(commit)
94 return available_commits
95
96def print_commits(commits):
97 for commit in commits:
98 print(commit)
99
100def 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
117def 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
123def pull_r8_from_cloud(commit):
124 utils.download_file_from_cloud_storage(commit.destination, utils.R8_JAR)
125
126def run_on_app(options, commit):
127 app = options.app
128 compiler = options.compiler
Jinseong Jeon158a3f12019-02-08 01:40:59 -0800129 cmd = ['tools/run_on_app.py',
130 '--app', app,
131 '--compiler', compiler,
132 '--timeout', str(options.timeout),
Rico Windf2017bf2019-01-10 09:45:33 +0100133 '--no-build', '--find-min-xmx']
Rico Windf9231222019-01-08 14:43:52 +0100134 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
146def benchmark(commits, options):
147 commit_permutations = permutate(len(commits))
Rico Windf2017bf2019-01-10 09:45:33 +0100148 count = 0
Rico Windf9231222019-01-08 14:43:52 +0100149 for index in commit_permutations:
Rico Windf2017bf2019-01-10 09:45:33 +0100150 count += 1
151 print('Running commit %s out of %s' % (count, len(commits)))
Rico Windf9231222019-01-08 14:43:52 +0100152 commit = commits[index]
Rico Windf2017bf2019-01-10 09:45:33 +0100153 if not utils.cloud_storage_exists(commit.destination):
154 # We may have a directory, but no r8.jar
155 continue
Rico Windf9231222019-01-08 14:43:52 +0100156 pull_r8_from_cloud(commit)
157 print('Running for commit: %s' % commit.git_hash)
158 run_on_app(options, commit)
159
160def 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 Windf9231222019-01-08 14:43:52 +0100168 benchmark(available_commits, options)
169
170if __name__ == '__main__':
171 sys.exit(main(sys.argv[1:]))