blob: 37c3dae1fb3c58c189a925fb91ba3ce91a403ee2 [file] [log] [blame]
Ian Zernydcb172e2022-02-22 15:36:45 +01001#!/usr/bin/env python3
Ian Zerny02a4b5b2019-10-21 13:32:41 +02002# 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 a command over builds back in time. This
7# utilizes the prebuilt full r8 jars on cloud storage. The script find all
8# commits that exists on cloud storage in the given range. It will then run the
9# oldest and newest such commit, and gradually fill in the commits in between.
10
Ian Zerny29892152024-05-14 14:59:31 +020011import math
Ian Zerny02a4b5b2019-10-21 13:32:41 +020012import optparse
13import os
14import subprocess
15import sys
16import time
17import utils
18
Rico Wind051c1be2024-02-19 09:27:11 +010019MASTER_COMMITS = 'gs://r8-releases/raw/main'
Ian Zerny02a4b5b2019-10-21 13:32:41 +020020
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020021
Ian Zerny02a4b5b2019-10-21 13:32:41 +020022def ParseOptions(argv):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020023 result = optparse.OptionParser()
24 result.add_option('--cmd', help='Command to run')
25 result.add_option('--top',
26 default=top_or_default(),
27 help='The most recent commit to test')
28 result.add_option('--bottom', help='The oldest commit to test')
29 result.add_option(
30 '--dry-run',
31 help='Do not download or run the command, but print the actions',
32 default=False,
33 action='store_true')
34 result.add_option('--output',
35 default='build',
36 help='Directory where to output results')
37 return result.parse_args(argv)
Ian Zerny02a4b5b2019-10-21 13:32:41 +020038
39
40class GitCommit(object):
Ian Zerny02a4b5b2019-10-21 13:32:41 +020041
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020042 def __init__(self, git_hash, destination_dir, destination, timestamp):
43 self.git_hash = git_hash
44 self.destination_dir = destination_dir
45 self.destination = destination
46 self.timestamp = timestamp
Ian Zerny02a4b5b2019-10-21 13:32:41 +020047
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020048 def __str__(self):
49 return '%s : %s (%s)' % (self.git_hash, self.destination,
50 self.timestamp)
51
52 def __repr__(self):
53 return self.__str__()
54
Ian Zerny02a4b5b2019-10-21 13:32:41 +020055
56def git_commit_from_hash(hash):
Ian Zerny29892152024-05-14 14:59:31 +020057 commit_timestamp = subprocess.check_output([
58 'git',
59 'show',
60 '--no-patch',
61 '--no-notes',
62 '--pretty=\'%ct\'',
63 hash
64 ]).decode().strip().strip('\'')
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020065 destination_dir = '%s/%s/' % (MASTER_COMMITS, hash)
66 destination = '%s%s' % (destination_dir, 'r8.jar')
67 commit = GitCommit(hash, destination_dir, destination, commit_timestamp)
68 return commit
69
Ian Zerny02a4b5b2019-10-21 13:32:41 +020070
71def enumerate_git_commits(top, bottom):
Ian Zerny29892152024-05-14 14:59:31 +020072 if bottom is None:
73 output = subprocess.check_output(['git', 'rev-list', '--first-parent', '-n', 1000, top])
74 else:
75 output = subprocess.check_output(['git', 'rev-list', '--first-parent', '%s^..%s' % (bottom, top)])
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020076 commits = []
Ian Zerny29892152024-05-14 14:59:31 +020077 for c in output.decode().splitlines():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020078 commit_hash = c.strip()
79 commits.append(git_commit_from_hash(commit_hash))
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020080 return commits
81
Ian Zerny02a4b5b2019-10-21 13:32:41 +020082
83def get_available_commits(commits):
Ian Zerny29892152024-05-14 14:59:31 +020084 cloud_commits = subprocess.check_output(
85 ['gsutil.py', 'ls', MASTER_COMMITS]).decode().splitlines()
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020086 available_commits = []
87 for commit in commits:
88 if commit.destination_dir in cloud_commits:
89 available_commits.append(commit)
90 return available_commits
91
Ian Zerny02a4b5b2019-10-21 13:32:41 +020092
93def print_commits(commits):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020094 for commit in commits:
95 print(commit)
96
Ian Zerny02a4b5b2019-10-21 13:32:41 +020097
98def permutate_range(start, end):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020099 diff = end - start
100 assert diff >= 0
101 if diff == 1:
102 return [start, end]
103 if diff == 0:
104 return [start]
Ian Zerny29892152024-05-14 14:59:31 +0200105 half = end - math.floor(diff / 2)
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200106 numbers = [half]
107 first_half = permutate_range(start, half - 1)
108 second_half = permutate_range(half + 1, end)
109 for index in range(len(first_half)):
110 numbers.append(first_half[index])
111 if index < len(second_half):
112 numbers.append(second_half[index])
113 return numbers
114
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200115
116def permutate(number_of_commits):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200117 assert number_of_commits > 0
118 numbers = permutate_range(0, number_of_commits - 1)
119 assert all(n in numbers for n in range(number_of_commits))
120 return numbers
121
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200122
123def pull_r8_from_cloud(commit):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200124 utils.download_file_from_cloud_storage(commit.destination, utils.R8_JAR)
125
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200126
127def benchmark(commits, command, dryrun=False):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200128 commit_permutations = permutate(len(commits))
129 count = 0
130 for index in commit_permutations:
131 count += 1
132 print('Running commit %s out of %s' % (count, len(commits)))
133 commit = commits[index]
134 if not utils.cloud_storage_exists(commit.destination):
135 # We may have a directory, but no r8.jar
136 continue
137 if not dryrun:
138 pull_r8_from_cloud(commit)
139 print('Running for commit: %s' % commit.git_hash)
140 command(commit)
141
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200142
143def top_or_default(top=None):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200144 return top if top else utils.get_HEAD_sha1()
145
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200146
147def bottom_or_default(bottom=None):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200148 # TODO(ricow): if not set, search back 1000
149 if not bottom:
150 raise Exception('No bottom specified')
151 return bottom
152
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200153
154def run(command, top, bottom, dryrun=False):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200155 commits = enumerate_git_commits(top, bottom)
156 available_commits = get_available_commits(commits)
157 print('Running for:')
158 print_commits(available_commits)
159 benchmark(available_commits, command, dryrun=dryrun)
160
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200161
162def make_cmd(options):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200163 return lambda commit: run_cmd(options, commit)
164
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200165
166def run_cmd(options, commit):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200167 cmd = [options.cmd, commit.git_hash]
168 output_path = options.output or 'build'
169 time_commit = '%s_%s' % (commit.timestamp, commit.git_hash)
170 time_commit_path = os.path.join(output_path, time_commit)
171 print(' '.join(cmd))
172 if not options.dry_run:
173 if not os.path.exists(time_commit_path):
174 os.makedirs(time_commit_path)
175 stdout_path = os.path.join(time_commit_path, 'stdout')
176 stderr_path = os.path.join(time_commit_path, 'stderr')
177 with open(stdout_path, 'w') as stdout:
178 with open(stderr_path, 'w') as stderr:
179 process = subprocess.Popen(cmd, stdout=stdout, stderr=stderr)
180 timeout = 1000
181 while process.poll() is None and timeout > 0:
182 time.sleep(1)
183 timeout -= 1
184 if process.poll() is None:
185 process.kill()
186 print("Task timed out")
187 stderr.write("timeout\n")
188 print('Wrote outputs to: %s' % time_commit_path)
189
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200190
191def main(argv):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200192 (options, args) = ParseOptions(argv)
193 if not options.cmd:
194 raise Exception('Please specify a command')
195 top = top_or_default(options.top)
196 bottom = bottom_or_default(options.bottom)
197 command = make_cmd(options)
198 run(command, top, bottom, dryrun=options.dry_run)
199
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200200
201if __name__ == '__main__':
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200202 sys.exit(main(sys.argv[1:]))