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 Adamsen | 9daa13b | 2024-06-12 13:07:58 +0200 | [diff] [blame] | 79 | commit_timestamp = subprocess.check_output( |
Christoffer Adamsen | 3028371 | 2024-06-12 13:08:14 +0200 | [diff] [blame] | 80 | ['git', 'show', '--no-patch', '--no-notes', '--pretty=%ct', |
| 81 | hash]).decode('utf-8').strip() |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 82 | destination_dir = '%s/%s/' % (MASTER_COMMITS, hash) |
| 83 | destination = '%s%s' % (destination_dir, 'r8.jar') |
| 84 | commit = GitCommit(hash, destination_dir, destination, commit_timestamp) |
| 85 | return commit |
| 86 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 87 | |
| 88 | def enumerate_git_commits(top, bottom): |
Ian Zerny | 2989215 | 2024-05-14 14:59:31 +0200 | [diff] [blame] | 89 | if bottom is None: |
Christoffer Adamsen | 9daa13b | 2024-06-12 13:07:58 +0200 | [diff] [blame] | 90 | output = subprocess.check_output( |
| 91 | ['git', 'rev-list', '--first-parent', '-n', 1000, top]) |
Ian Zerny | 2989215 | 2024-05-14 14:59:31 +0200 | [diff] [blame] | 92 | else: |
Christoffer Adamsen | 9daa13b | 2024-06-12 13:07:58 +0200 | [diff] [blame] | 93 | output = subprocess.check_output( |
| 94 | ['git', 'rev-list', '--first-parent', |
| 95 | '%s^..%s' % (bottom, top)]) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 96 | commits = [] |
Ian Zerny | 2989215 | 2024-05-14 14:59:31 +0200 | [diff] [blame] | 97 | for c in output.decode().splitlines(): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 98 | commit_hash = c.strip() |
| 99 | commits.append(git_commit_from_hash(commit_hash)) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 100 | return commits |
| 101 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 102 | |
| 103 | def get_available_commits(commits): |
Christoffer Adamsen | 9daa13b | 2024-06-12 13:07:58 +0200 | [diff] [blame] | 104 | cloud_commits = subprocess.check_output(['gsutil.py', 'ls', MASTER_COMMITS |
| 105 | ]).decode().splitlines() |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 106 | available_commits = [] |
| 107 | for commit in commits: |
| 108 | if commit.destination_dir in cloud_commits: |
| 109 | available_commits.append(commit) |
| 110 | return available_commits |
| 111 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 112 | |
| 113 | def print_commits(commits): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 114 | for commit in commits: |
| 115 | print(commit) |
| 116 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 117 | |
| 118 | def permutate_range(start, end): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 119 | diff = end - start |
| 120 | assert diff >= 0 |
| 121 | if diff == 1: |
| 122 | return [start, end] |
| 123 | if diff == 0: |
| 124 | return [start] |
Ian Zerny | 2989215 | 2024-05-14 14:59:31 +0200 | [diff] [blame] | 125 | half = end - math.floor(diff / 2) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 126 | numbers = [half] |
| 127 | first_half = permutate_range(start, half - 1) |
| 128 | second_half = permutate_range(half + 1, end) |
| 129 | for index in range(len(first_half)): |
| 130 | numbers.append(first_half[index]) |
| 131 | if index < len(second_half): |
| 132 | numbers.append(second_half[index]) |
| 133 | return numbers |
| 134 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 135 | |
| 136 | def permutate(number_of_commits): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 137 | assert number_of_commits > 0 |
| 138 | numbers = permutate_range(0, number_of_commits - 1) |
| 139 | assert all(n in numbers for n in range(number_of_commits)) |
| 140 | return numbers |
| 141 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 142 | |
| 143 | def pull_r8_from_cloud(commit): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 144 | utils.download_file_from_cloud_storage(commit.destination, utils.R8_JAR) |
| 145 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 146 | |
| 147 | def benchmark(commits, command, dryrun=False): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 148 | commit_permutations = permutate(len(commits)) |
| 149 | count = 0 |
| 150 | for index in commit_permutations: |
| 151 | count += 1 |
| 152 | print('Running commit %s out of %s' % (count, len(commits))) |
| 153 | commit = commits[index] |
| 154 | if not utils.cloud_storage_exists(commit.destination): |
| 155 | # We may have a directory, but no r8.jar |
| 156 | continue |
| 157 | if not dryrun: |
| 158 | pull_r8_from_cloud(commit) |
| 159 | print('Running for commit: %s' % commit.git_hash) |
| 160 | command(commit) |
| 161 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 162 | |
| 163 | def top_or_default(top=None): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 164 | return top if top else utils.get_HEAD_sha1() |
| 165 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 166 | |
| 167 | def bottom_or_default(bottom=None): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 168 | # TODO(ricow): if not set, search back 1000 |
| 169 | if not bottom: |
| 170 | raise Exception('No bottom specified') |
| 171 | return bottom |
| 172 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 173 | |
| 174 | def run(command, top, bottom, dryrun=False): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 175 | commits = enumerate_git_commits(top, bottom) |
| 176 | available_commits = get_available_commits(commits) |
| 177 | print('Running for:') |
| 178 | print_commits(available_commits) |
| 179 | benchmark(available_commits, command, dryrun=dryrun) |
| 180 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 181 | |
| 182 | def make_cmd(options): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 183 | return lambda commit: run_cmd(options, commit) |
| 184 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 185 | |
| 186 | def run_cmd(options, commit): |
Christoffer Adamsen | 9daa13b | 2024-06-12 13:07:58 +0200 | [diff] [blame] | 187 | cmd = options.cmd.split(' ') |
| 188 | cmd.append(commit.git_hash) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 189 | output_path = options.output or 'build' |
| 190 | time_commit = '%s_%s' % (commit.timestamp, commit.git_hash) |
| 191 | time_commit_path = os.path.join(output_path, time_commit) |
| 192 | print(' '.join(cmd)) |
| 193 | if not options.dry_run: |
| 194 | if not os.path.exists(time_commit_path): |
| 195 | os.makedirs(time_commit_path) |
| 196 | stdout_path = os.path.join(time_commit_path, 'stdout') |
| 197 | stderr_path = os.path.join(time_commit_path, 'stderr') |
| 198 | with open(stdout_path, 'w') as stdout: |
| 199 | with open(stderr_path, 'w') as stderr: |
| 200 | process = subprocess.Popen(cmd, stdout=stdout, stderr=stderr) |
Christoffer Adamsen | 9daa13b | 2024-06-12 13:07:58 +0200 | [diff] [blame] | 201 | timeout = options.timeout |
| 202 | while process.poll() is None and timeout != 0: |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 203 | time.sleep(1) |
| 204 | timeout -= 1 |
| 205 | if process.poll() is None: |
| 206 | process.kill() |
| 207 | print("Task timed out") |
| 208 | stderr.write("timeout\n") |
| 209 | print('Wrote outputs to: %s' % time_commit_path) |
| 210 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 211 | |
| 212 | def main(argv): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 213 | (options, args) = ParseOptions(argv) |
| 214 | if not options.cmd: |
| 215 | raise Exception('Please specify a command') |
| 216 | top = top_or_default(options.top) |
| 217 | bottom = bottom_or_default(options.bottom) |
| 218 | command = make_cmd(options) |
| 219 | run(command, top, bottom, dryrun=options.dry_run) |
| 220 | |
Ian Zerny | 02a4b5b | 2019-10-21 13:32:41 +0200 | [diff] [blame] | 221 | |
| 222 | if __name__ == '__main__': |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 223 | sys.exit(main(sys.argv[1:])) |