Ian Zerny | dcb172e | 2022-02-22 15:36:45 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 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 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 Zerny | 2989215 | 2024-05-14 14:59:31 +0200 | [diff] [blame] | 11 | import math |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 12 | import optparse |
| 13 | import os |
| 14 | import subprocess |
| 15 | import sys |
| 16 | import time |
| 17 | import utils |
| 18 | |
Rico Wind | 051c1be | 2024-02-19 09:27:11 +0100 | [diff] [blame] | 19 | MASTER_COMMITS = 'gs://r8-releases/raw/main' |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 20 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 21 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 22 | def ParseOptions(argv): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 23 | 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 Adamsen | 9daa13b | 2024-06-12 13:07:58 +0200 | [diff] [blame] | 37 | result.add_option('--timeout', |
| 38 | default=1000, |
| 39 | help='Timeout in seconds (-1 for no timeout)', |
| 40 | type=int) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 41 | return result.parse_args(argv) |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 42 | |
| 43 | |
| 44 | class GitCommit(object): |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 45 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 46 | 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 Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 51 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 52 | 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 Adamsen | 3028371 | 2024-06-12 13:08:14 +0200 | [diff] [blame] | 59 | 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 Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 77 | |
| 78 | def git_commit_from_hash(hash): |
Christoffer Quist Adamsen | 82f0acd | 2024-09-19 16:55:41 +0200 | [diff] [blame] | 79 | # If there is a tag for the given commit then the commit timestamp is on the |
| 80 | # last line. |
Christoffer Adamsen | 4c9f095 | 2024-06-13 13:02:08 +0200 | [diff] [blame] | 81 | commit_timestamp_str = subprocess.check_output( |
Christoffer Adamsen | 3028371 | 2024-06-12 13:08:14 +0200 | [diff] [blame] | 82 | ['git', 'show', '--no-patch', '--no-notes', '--pretty=%ct', |
Christoffer Quist Adamsen | 82f0acd | 2024-09-19 16:55:41 +0200 | [diff] [blame] | 83 | hash]).decode('utf-8').strip().splitlines()[-1] |
Christoffer Adamsen | 4c9f095 | 2024-06-13 13:02:08 +0200 | [diff] [blame] | 84 | commit_timestamp = int(commit_timestamp_str) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 85 | 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 Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 90 | |
| 91 | def enumerate_git_commits(top, bottom): |
Ian Zerny | 2989215 | 2024-05-14 14:59:31 +0200 | [diff] [blame] | 92 | if bottom is None: |
Christoffer Adamsen | 9daa13b | 2024-06-12 13:07:58 +0200 | [diff] [blame] | 93 | output = subprocess.check_output( |
| 94 | ['git', 'rev-list', '--first-parent', '-n', 1000, top]) |
Ian Zerny | 2989215 | 2024-05-14 14:59:31 +0200 | [diff] [blame] | 95 | else: |
Christoffer Adamsen | 9daa13b | 2024-06-12 13:07:58 +0200 | [diff] [blame] | 96 | output = subprocess.check_output( |
| 97 | ['git', 'rev-list', '--first-parent', |
| 98 | '%s^..%s' % (bottom, top)]) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 99 | commits = [] |
Ian Zerny | 2989215 | 2024-05-14 14:59:31 +0200 | [diff] [blame] | 100 | for c in output.decode().splitlines(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 101 | commit_hash = c.strip() |
| 102 | commits.append(git_commit_from_hash(commit_hash)) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 103 | return commits |
| 104 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 105 | |
| 106 | def get_available_commits(commits): |
Christoffer Adamsen | 9daa13b | 2024-06-12 13:07:58 +0200 | [diff] [blame] | 107 | cloud_commits = subprocess.check_output(['gsutil.py', 'ls', MASTER_COMMITS |
| 108 | ]).decode().splitlines() |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 109 | 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 Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 115 | |
| 116 | def print_commits(commits): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 117 | for commit in commits: |
| 118 | print(commit) |
| 119 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 120 | |
| 121 | def permutate_range(start, end): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 122 | diff = end - start |
| 123 | assert diff >= 0 |
| 124 | if diff == 1: |
| 125 | return [start, end] |
| 126 | if diff == 0: |
| 127 | return [start] |
Ian Zerny | 2989215 | 2024-05-14 14:59:31 +0200 | [diff] [blame] | 128 | half = end - math.floor(diff / 2) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 129 | 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 Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 138 | |
| 139 | def permutate(number_of_commits): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 140 | 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 Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 145 | |
| 146 | def pull_r8_from_cloud(commit): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 147 | utils.download_file_from_cloud_storage(commit.destination, utils.R8_JAR) |
| 148 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 149 | |
| 150 | def benchmark(commits, command, dryrun=False): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 151 | commit_permutations = permutate(len(commits)) |
| 152 | count = 0 |
| 153 | for index in commit_permutations: |
| 154 | count += 1 |
Christoffer Adamsen | c748e52 | 2024-06-12 18:48:45 +0200 | [diff] [blame] | 155 | print('\nRunning commit %s out of %s' % (count, len(commits))) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 156 | 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 Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 165 | |
| 166 | def top_or_default(top=None): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 167 | return top if top else utils.get_HEAD_sha1() |
| 168 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 169 | |
| 170 | def bottom_or_default(bottom=None): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 171 | # TODO(ricow): if not set, search back 1000 |
| 172 | if not bottom: |
| 173 | raise Exception('No bottom specified') |
| 174 | return bottom |
| 175 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 176 | |
| 177 | def run(command, top, bottom, dryrun=False): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 178 | 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 Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 184 | |
| 185 | def make_cmd(options): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 186 | return lambda commit: run_cmd(options, commit) |
| 187 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 188 | |
| 189 | def run_cmd(options, commit): |
Christoffer Adamsen | 9daa13b | 2024-06-12 13:07:58 +0200 | [diff] [blame] | 190 | cmd = options.cmd.split(' ') |
| 191 | cmd.append(commit.git_hash) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 192 | 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 Adamsen | c748e52 | 2024-06-12 18:48:45 +0200 | [diff] [blame] | 196 | status = None |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 197 | 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 Adamsen | 9daa13b | 2024-06-12 13:07:58 +0200 | [diff] [blame] | 205 | timeout = options.timeout |
| 206 | while process.poll() is None and timeout != 0: |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 207 | 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 Adamsen | c748e52 | 2024-06-12 18:48:45 +0200 | [diff] [blame] | 213 | 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 Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 219 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 220 | |
| 221 | def main(argv): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 222 | (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 Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 230 | |
| 231 | if __name__ == '__main__': |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 232 | sys.exit(main(sys.argv[1:])) |