blob: 1124f956173a6c5e20b8f78d09ed5d013df0ee62 [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')
Christoffer Adamsen9daa13b2024-06-12 13:07:58 +020037 result.add_option('--timeout',
38 default=1000,
39 help='Timeout in seconds (-1 for no timeout)',
40 type=int)
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020041 return result.parse_args(argv)
Ian Zerny02a4b5b2019-10-21 13:32:41 +020042
43
44class GitCommit(object):
Ian Zerny02a4b5b2019-10-21 13:32:41 +020045
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020046 def __init__(self, git_hash, destination_dir, destination, timestamp):
47 self.git_hash = git_hash
48 self.destination_dir = destination_dir
49 self.destination = destination
50 self.timestamp = timestamp
Ian Zerny02a4b5b2019-10-21 13:32:41 +020051
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020052 def __str__(self):
53 return '%s : %s (%s)' % (self.git_hash, self.destination,
54 self.timestamp)
55
56 def __repr__(self):
57 return self.__str__()
58
Christoffer Adamsen30283712024-06-12 13:08:14 +020059 def hash(self):
60 return self.git_hash
61
62 def title(self):
63 result = subprocess.check_output(
64 ['git', 'show-branch', '--no-name', self.git_hash]).decode('utf-8')
65 return result.strip()
66
67 def author_name(self):
68 result = subprocess.check_output([
69 'git', 'show', '--no-notes', '--no-patch', '--pretty=%an',
70 self.git_hash
71 ]).decode('utf-8')
72 return result.strip()
73
74 def committer_timestamp(self):
75 return self.timestamp
76
Ian Zerny02a4b5b2019-10-21 13:32:41 +020077
78def git_commit_from_hash(hash):
Christoffer Quist Adamsen82f0acd2024-09-19 16:55:41 +020079 # If there is a tag for the given commit then the commit timestamp is on the
80 # last line.
Christoffer Adamsen4c9f0952024-06-13 13:02:08 +020081 commit_timestamp_str = subprocess.check_output(
Christoffer Adamsen30283712024-06-12 13:08:14 +020082 ['git', 'show', '--no-patch', '--no-notes', '--pretty=%ct',
Christoffer Quist Adamsen82f0acd2024-09-19 16:55:41 +020083 hash]).decode('utf-8').strip().splitlines()[-1]
Christoffer Adamsen4c9f0952024-06-13 13:02:08 +020084 commit_timestamp = int(commit_timestamp_str)
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020085 destination_dir = '%s/%s/' % (MASTER_COMMITS, hash)
86 destination = '%s%s' % (destination_dir, 'r8.jar')
87 commit = GitCommit(hash, destination_dir, destination, commit_timestamp)
88 return commit
89
Ian Zerny02a4b5b2019-10-21 13:32:41 +020090
91def enumerate_git_commits(top, bottom):
Ian Zerny29892152024-05-14 14:59:31 +020092 if bottom is None:
Christoffer Adamsen9daa13b2024-06-12 13:07:58 +020093 output = subprocess.check_output(
94 ['git', 'rev-list', '--first-parent', '-n', 1000, top])
Ian Zerny29892152024-05-14 14:59:31 +020095 else:
Christoffer Adamsen9daa13b2024-06-12 13:07:58 +020096 output = subprocess.check_output(
97 ['git', 'rev-list', '--first-parent',
98 '%s^..%s' % (bottom, top)])
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020099 commits = []
Ian Zerny29892152024-05-14 14:59:31 +0200100 for c in output.decode().splitlines():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200101 commit_hash = c.strip()
102 commits.append(git_commit_from_hash(commit_hash))
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200103 return commits
104
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200105
106def get_available_commits(commits):
Christoffer Adamsen9daa13b2024-06-12 13:07:58 +0200107 cloud_commits = subprocess.check_output(['gsutil.py', 'ls', MASTER_COMMITS
108 ]).decode().splitlines()
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200109 available_commits = []
110 for commit in commits:
111 if commit.destination_dir in cloud_commits:
112 available_commits.append(commit)
113 return available_commits
114
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200115
116def print_commits(commits):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200117 for commit in commits:
118 print(commit)
119
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200120
121def permutate_range(start, end):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200122 diff = end - start
123 assert diff >= 0
124 if diff == 1:
125 return [start, end]
126 if diff == 0:
127 return [start]
Ian Zerny29892152024-05-14 14:59:31 +0200128 half = end - math.floor(diff / 2)
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200129 numbers = [half]
130 first_half = permutate_range(start, half - 1)
131 second_half = permutate_range(half + 1, end)
132 for index in range(len(first_half)):
133 numbers.append(first_half[index])
134 if index < len(second_half):
135 numbers.append(second_half[index])
136 return numbers
137
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200138
139def permutate(number_of_commits):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200140 assert number_of_commits > 0
141 numbers = permutate_range(0, number_of_commits - 1)
142 assert all(n in numbers for n in range(number_of_commits))
143 return numbers
144
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200145
146def pull_r8_from_cloud(commit):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200147 utils.download_file_from_cloud_storage(commit.destination, utils.R8_JAR)
148
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200149
150def benchmark(commits, command, dryrun=False):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200151 commit_permutations = permutate(len(commits))
152 count = 0
153 for index in commit_permutations:
154 count += 1
Christoffer Adamsenc748e522024-06-12 18:48:45 +0200155 print('\nRunning commit %s out of %s' % (count, len(commits)))
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200156 commit = commits[index]
157 if not utils.cloud_storage_exists(commit.destination):
158 # We may have a directory, but no r8.jar
159 continue
160 if not dryrun:
161 pull_r8_from_cloud(commit)
162 print('Running for commit: %s' % commit.git_hash)
163 command(commit)
164
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200165
166def top_or_default(top=None):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200167 return top if top else utils.get_HEAD_sha1()
168
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200169
170def bottom_or_default(bottom=None):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200171 # TODO(ricow): if not set, search back 1000
172 if not bottom:
173 raise Exception('No bottom specified')
174 return bottom
175
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200176
177def run(command, top, bottom, dryrun=False):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200178 commits = enumerate_git_commits(top, bottom)
179 available_commits = get_available_commits(commits)
180 print('Running for:')
181 print_commits(available_commits)
182 benchmark(available_commits, command, dryrun=dryrun)
183
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200184
185def make_cmd(options):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200186 return lambda commit: run_cmd(options, commit)
187
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200188
189def run_cmd(options, commit):
Christoffer Adamsen9daa13b2024-06-12 13:07:58 +0200190 cmd = options.cmd.split(' ')
191 cmd.append(commit.git_hash)
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200192 output_path = options.output or 'build'
193 time_commit = '%s_%s' % (commit.timestamp, commit.git_hash)
194 time_commit_path = os.path.join(output_path, time_commit)
195 print(' '.join(cmd))
Christoffer Adamsenc748e522024-06-12 18:48:45 +0200196 status = None
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200197 if not options.dry_run:
198 if not os.path.exists(time_commit_path):
199 os.makedirs(time_commit_path)
200 stdout_path = os.path.join(time_commit_path, 'stdout')
201 stderr_path = os.path.join(time_commit_path, 'stderr')
202 with open(stdout_path, 'w') as stdout:
203 with open(stderr_path, 'w') as stderr:
204 process = subprocess.Popen(cmd, stdout=stdout, stderr=stderr)
Christoffer Adamsen9daa13b2024-06-12 13:07:58 +0200205 timeout = options.timeout
206 while process.poll() is None and timeout != 0:
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200207 time.sleep(1)
208 timeout -= 1
209 if process.poll() is None:
210 process.kill()
211 print("Task timed out")
212 stderr.write("timeout\n")
Christoffer Adamsenc748e522024-06-12 18:48:45 +0200213 status = 'TIMED OUT'
214 else:
215 returncode = process.returncode
216 status = 'SUCCESS' if returncode == 0 else f'FAILED ({returncode})'
217 print(f'Wrote outputs to: {time_commit_path}')
218 print(status)
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200219
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200220
221def main(argv):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200222 (options, args) = ParseOptions(argv)
223 if not options.cmd:
224 raise Exception('Please specify a command')
225 top = top_or_default(options.top)
226 bottom = bottom_or_default(options.bottom)
227 command = make_cmd(options)
228 run(command, top, bottom, dryrun=options.dry_run)
229
Ian Zerny02a4b5b2019-10-21 13:32:41 +0200230
231if __name__ == '__main__':
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200232 sys.exit(main(sys.argv[1:]))