Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2018, 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 | # Run all internal tests, archive result to cloud storage. |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 7 | # In the continuous operation flow we have a tester continuously checking |
| 8 | # a specific cloud storage location for a file with a git hash. |
| 9 | # If the file is there, the tester will remove the file, and add another |
| 10 | # file stating that this is now being run. After successfully running, |
| 11 | # the tester will add yet another file, and remove the last one. |
| 12 | # Complete flow with states: |
| 13 | # 1: |
| 14 | # BOT: |
| 15 | # Add file READY_FOR_TESTING (contains git hash) |
| 16 | # Wait until file TESTING_COMPLETE exists (contains git hash) |
| 17 | # Timeout if no progress for RUN_TIMEOUT |
| 18 | # Cleanup READY_FOR_TESTING and TESTING |
| 19 | # 2: |
| 20 | # TESTER: |
| 21 | # Replace file READY_FOR_TESTING by TESTING (contains git hash) |
| 22 | # Run tests for git hash |
| 23 | # Upload commit specific logs if failures |
| 24 | # Upload git specific overall status file (failed or succeeded) |
| 25 | # Replace file TESTING by TESTING_COMPLETE (contains git hash) |
| 26 | # 3: |
| 27 | # BOT: |
| 28 | # Read overall status |
| 29 | # Delete TESTING_COMPLETE |
| 30 | # Exit based on status |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 31 | |
Rico Wind | ba63dc8 | 2019-03-29 14:33:47 +0100 | [diff] [blame] | 32 | import gradle |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 33 | import optparse |
| 34 | import os |
| 35 | import subprocess |
| 36 | import sys |
| 37 | import time |
| 38 | import utils |
Morten Krogh-Jespersen | f241230 | 2019-10-22 10:18:04 +0200 | [diff] [blame] | 39 | import run_on_app |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 40 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 41 | # How often the bot/tester should check state |
| 42 | PULL_DELAY = 30 |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 43 | TEST_RESULT_DIR = 'internal' |
| 44 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 45 | # Magic files |
| 46 | READY_FOR_TESTING = 'READY_FOR_TESTING' |
| 47 | TESTING = 'TESTING' |
| 48 | TESTING_COMPLETE = 'TESTING_COMPLETE' |
| 49 | |
| 50 | ALL_MAGIC = [READY_FOR_TESTING, TESTING, TESTING_COMPLETE] |
| 51 | |
| 52 | # Log file names |
| 53 | STDERR = 'stderr' |
| 54 | STDOUT = 'stdout' |
| 55 | EXITCODE = 'exitcode' |
| 56 | TIMED_OUT = 'timed_out' |
| 57 | |
Morten Krogh-Jespersen | 6cd9f1d | 2019-10-09 14:01:04 +0200 | [diff] [blame] | 58 | BENCHMARK_APPS = [ |
| 59 | { |
| 60 | 'app': 'r8', |
| 61 | 'version': 'cf', |
| 62 | 'find-xmx-min': 128, |
| 63 | 'find-xmx-max': 400, |
| 64 | 'find-xmx-range': 16, |
Morten Krogh-Jespersen | 3793dc9 | 2019-10-09 14:48:48 +0200 | [diff] [blame] | 65 | 'oom-threshold': 247, |
Morten Krogh-Jespersen | 6cd9f1d | 2019-10-09 14:01:04 +0200 | [diff] [blame] | 66 | }, |
| 67 | { |
| 68 | 'app': 'chrome', |
| 69 | 'version': '180917', |
| 70 | 'find-xmx-min': 256, |
| 71 | 'find-xmx-max': 450, |
| 72 | 'find-xmx-range': 16, |
Morten Krogh-Jespersen | 98a1a6e | 2020-04-08 13:51:00 +0200 | [diff] [blame] | 73 | 'oom-threshold': 340, |
Morten Krogh-Jespersen | 6cd9f1d | 2019-10-09 14:01:04 +0200 | [diff] [blame] | 74 | }, |
| 75 | { |
| 76 | 'app': 'youtube', |
| 77 | 'version': '12.22', |
Christoffer Quist Adamsen | edb45c1 | 2020-04-03 07:30:47 +0200 | [diff] [blame] | 78 | 'find-xmx-min': 750, |
| 79 | 'find-xmx-max': 1150, |
Morten Krogh-Jespersen | 6cd9f1d | 2019-10-09 14:01:04 +0200 | [diff] [blame] | 80 | 'find-xmx-range': 32, |
Christoffer Quist Adamsen | edb45c1 | 2020-04-03 07:30:47 +0200 | [diff] [blame] | 81 | 'oom-threshold': 950, |
Morten Krogh-Jespersen | b042c97 | 2019-11-04 08:57:59 +0100 | [diff] [blame] | 82 | # TODO(b/143431825): Youtube can OOM randomly in memory configurations |
| 83 | # that should work. |
| 84 | 'skip-find-xmx-max': True, |
Morten Krogh-Jespersen | 6cd9f1d | 2019-10-09 14:01:04 +0200 | [diff] [blame] | 85 | }, |
Morten Krogh-Jespersen | 6cd9f1d | 2019-10-09 14:01:04 +0200 | [diff] [blame] | 86 | { |
| 87 | 'app': 'iosched', |
| 88 | 'version': '2019', |
| 89 | 'find-xmx-min': 128, |
| 90 | 'find-xmx-max': 1024, |
| 91 | 'find-xmx-range': 16, |
Morten Krogh-Jespersen | 50d4852 | 2019-10-11 21:49:46 +0200 | [diff] [blame] | 92 | 'oom-threshold': 267, |
Morten Krogh-Jespersen | 6cd9f1d | 2019-10-09 14:01:04 +0200 | [diff] [blame] | 93 | }, |
| 94 | ] |
| 95 | |
| 96 | def find_min_xmx_command(record): |
Jinseong Jeon | d28cb58 | 2019-10-15 00:01:40 -0700 | [diff] [blame] | 97 | assert record['find-xmx-min'] < record['find-xmx-max'] |
| 98 | assert record['find-xmx-range'] < record['find-xmx-max'] - record['find-xmx-min'] |
Morten Krogh-Jespersen | 6cd9f1d | 2019-10-09 14:01:04 +0200 | [diff] [blame] | 99 | return [ |
| 100 | 'tools/run_on_app.py', |
| 101 | '--compiler=r8', |
| 102 | '--compiler-build=lib', |
| 103 | '--app=%s' % record['app'], |
| 104 | '--version=%s' % record['version'], |
| 105 | '--no-debug', |
| 106 | '--no-build', |
| 107 | '--find-min-xmx', |
| 108 | '--find-min-xmx-min-memory=%s' % record['find-xmx-min'], |
| 109 | '--find-min-xmx-max-memory=%s' % record['find-xmx-max'], |
| 110 | '--find-min-xmx-range-size=%s' % record['find-xmx-range'], |
| 111 | '--find-min-xmx-archive'] |
| 112 | |
Morten Krogh-Jespersen | 3793dc9 | 2019-10-09 14:48:48 +0200 | [diff] [blame] | 113 | def compile_with_memory_max_command(record): |
Morten Krogh-Jespersen | a0cc49f | 2020-04-17 08:39:59 +0200 | [diff] [blame] | 114 | # TODO(b/152939233): Remove this special handling when fixed. |
| 115 | factor = 1.25 if record['app'] == 'chrome' else 1.15 |
Morten Krogh-Jespersen | b042c97 | 2019-11-04 08:57:59 +0100 | [diff] [blame] | 116 | return [] if 'skip-find-xmx-max' in record else [ |
Morten Krogh-Jespersen | 3793dc9 | 2019-10-09 14:48:48 +0200 | [diff] [blame] | 117 | 'tools/run_on_app.py', |
| 118 | '--compiler=r8', |
| 119 | '--compiler-build=lib', |
| 120 | '--app=%s' % record['app'], |
| 121 | '--version=%s' % record['version'], |
| 122 | '--no-debug', |
| 123 | '--no-build', |
Morten Krogh-Jespersen | a0cc49f | 2020-04-17 08:39:59 +0200 | [diff] [blame] | 124 | '--max-memory=%s' % int(record['oom-threshold'] * factor) |
Morten Krogh-Jespersen | 3793dc9 | 2019-10-09 14:48:48 +0200 | [diff] [blame] | 125 | ] |
| 126 | |
| 127 | def compile_with_memory_min_command(record): |
| 128 | return [ |
| 129 | 'tools/run_on_app.py', |
| 130 | '--compiler=r8', |
| 131 | '--compiler-build=lib', |
| 132 | '--app=%s' % record['app'], |
| 133 | '--version=%s' % record['version'], |
| 134 | '--no-debug', |
| 135 | '--no-build', |
| 136 | '--expect-oom', |
Morten Krogh-Jespersen | 092e971 | 2019-10-24 14:06:32 +0200 | [diff] [blame] | 137 | '--max-memory=%s' % int(record['oom-threshold'] * 0.85) |
Morten Krogh-Jespersen | 3793dc9 | 2019-10-09 14:48:48 +0200 | [diff] [blame] | 138 | ] |
| 139 | |
Rico Wind | 6847d13 | 2018-09-26 08:18:48 +0200 | [diff] [blame] | 140 | TEST_COMMANDS = [ |
Morten Krogh-Jespersen | 2243b16 | 2019-01-14 08:40:53 +0100 | [diff] [blame] | 141 | # Run test.py internal testing. |
Rico Wind | e623bf3 | 2019-08-28 09:28:00 +0200 | [diff] [blame] | 142 | ['tools/test.py', '--only_internal', '--slow_tests', |
Rico Wind | 97b0a99 | 2019-08-30 11:09:15 +0200 | [diff] [blame] | 143 | '--java_max_memory_size=8G'], |
Morten Krogh-Jespersen | 2243b16 | 2019-01-14 08:40:53 +0100 | [diff] [blame] | 144 | # Ensure that all internal apps compile. |
Morten Krogh-Jespersen | 82bde82 | 2019-10-09 14:50:35 +0200 | [diff] [blame] | 145 | ['tools/run_on_app.py', '--run-all', '--out=out'], |
Morten Krogh-Jespersen | 6cd9f1d | 2019-10-09 14:01:04 +0200 | [diff] [blame] | 146 | # Find min xmx for selected benchmark apps |
| 147 | ['tools/gradle.py', 'r8lib'], |
Morten Krogh-Jespersen | 561a2e9 | 2019-10-10 11:11:30 +0200 | [diff] [blame] | 148 | ] + (map(find_min_xmx_command, BENCHMARK_APPS) |
| 149 | + map(compile_with_memory_max_command, BENCHMARK_APPS) |
| 150 | + map(compile_with_memory_min_command, BENCHMARK_APPS)) |
Morten Krogh-Jespersen | 3793dc9 | 2019-10-09 14:48:48 +0200 | [diff] [blame] | 151 | |
Rico Wind | 6847d13 | 2018-09-26 08:18:48 +0200 | [diff] [blame] | 152 | # Command timeout, in seconds. |
Rico Wind | f77c5b9 | 2019-06-11 08:50:41 +0200 | [diff] [blame] | 153 | RUN_TIMEOUT = 3600 * 6 |
Rico Wind | f021d83 | 2018-12-13 11:29:22 +0100 | [diff] [blame] | 154 | BOT_RUN_TIMEOUT = RUN_TIMEOUT * len(TEST_COMMANDS) |
Rico Wind | 6847d13 | 2018-09-26 08:18:48 +0200 | [diff] [blame] | 155 | |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 156 | def log(str): |
Rico Wind | ffccab1 | 2018-09-26 12:39:42 +0200 | [diff] [blame] | 157 | print("%s: %s" % (time.strftime("%c"), str)) |
Rico Wind | 1b09c56 | 2019-01-17 08:53:09 +0100 | [diff] [blame] | 158 | sys.stdout.flush() |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 159 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 160 | def ParseOptions(): |
| 161 | result = optparse.OptionParser() |
| 162 | result.add_option('--continuous', |
| 163 | help='Continuously run internal tests and post results to GCS.', |
| 164 | default=False, action='store_true') |
Rico Wind | 4fd2dda | 2018-09-26 17:41:45 +0200 | [diff] [blame] | 165 | result.add_option('--print_logs', |
| 166 | help='Fetch logs from gcs and print them, takes the commit to print for.', |
| 167 | default=None) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 168 | result.add_option('--bot', |
| 169 | help='Run in bot mode, i.e., scheduling runs.', |
| 170 | default=False, action='store_true') |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 171 | result.add_option('--archive', |
| 172 | help='Post result to GCS, implied by --continuous', |
| 173 | default=False, action='store_true') |
| 174 | return result.parse_args() |
| 175 | |
| 176 | def get_own_file_content(): |
| 177 | with open(sys.argv[0], 'r') as us: |
| 178 | return us.read() |
| 179 | |
| 180 | def restart_if_new_version(original_content): |
| 181 | new_content = get_own_file_content() |
Rico Wind | 1b09c56 | 2019-01-17 08:53:09 +0100 | [diff] [blame] | 182 | log('Lengths %s %s' % (len(original_content), len(new_content))) |
| 183 | log('is master %s ' % utils.is_master()) |
| 184 | # Restart if the script got updated. |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 185 | if new_content != original_content: |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 186 | log('Restarting tools/internal_test.py, content changed') |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 187 | os.execv(sys.argv[0], sys.argv) |
| 188 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 189 | def ensure_git_clean(): |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 190 | # Ensure clean git repo. |
| 191 | diff = subprocess.check_output(['git', 'diff']) |
| 192 | if len(diff) > 0: |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 193 | log('Local modifications to the git repo, exiting') |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 194 | sys.exit(1) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 195 | |
| 196 | def git_pull(): |
| 197 | ensure_git_clean() |
Rico Wind | 2a19d93 | 2018-09-25 16:48:56 +0200 | [diff] [blame] | 198 | subprocess.check_call(['git', 'checkout', 'master']) |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 199 | subprocess.check_call(['git', 'pull']) |
| 200 | return utils.get_HEAD_sha1() |
| 201 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 202 | def git_checkout(git_hash): |
| 203 | ensure_git_clean() |
| 204 | # Ensure that we are up to date to get the commit. |
| 205 | git_pull() |
Rico Wind | d7d9106 | 2019-04-29 09:24:10 +0200 | [diff] [blame] | 206 | exitcode = subprocess.call(['git', 'checkout', git_hash]) |
| 207 | if exitcode != 0: |
| 208 | return None |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 209 | return utils.get_HEAD_sha1() |
| 210 | |
| 211 | def get_test_result_dir(): |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 212 | return os.path.join(utils.R8_TEST_RESULTS_BUCKET, TEST_RESULT_DIR) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 213 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 214 | def get_sha_destination(sha): |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 215 | return os.path.join(get_test_result_dir(), sha) |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 216 | |
| 217 | def archive_status(failed): |
| 218 | gs_destination = 'gs://%s' % get_sha_destination(utils.get_HEAD_sha1()) |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 219 | utils.archive_value('status', gs_destination, failed) |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 220 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 221 | def get_status(sha): |
| 222 | gs_destination = 'gs://%s/status' % get_sha_destination(sha) |
| 223 | return utils.cat_file_on_cloud_storage(gs_destination) |
| 224 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 225 | def archive_log(stdout, stderr, exitcode, timed_out, cmd): |
| 226 | sha = utils.get_HEAD_sha1() |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 227 | cmd_dir = cmd.replace(' ', '_').replace('/', '_') |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 228 | destination = os.path.join(get_sha_destination(sha), cmd_dir) |
| 229 | gs_destination = 'gs://%s' % destination |
| 230 | url = 'https://storage.cloud.google.com/%s' % destination |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 231 | log('Archiving logs to: %s' % gs_destination) |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 232 | utils.archive_value(EXITCODE, gs_destination, exitcode) |
| 233 | utils.archive_value(TIMED_OUT, gs_destination, timed_out) |
| 234 | utils.archive_file(STDOUT, gs_destination, stdout) |
| 235 | utils.archive_file(STDERR, gs_destination, stderr) |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 236 | log('Logs available at: %s' % url) |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 237 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 238 | def get_magic_file_base_path(): |
| 239 | return 'gs://%s/magic' % get_test_result_dir() |
| 240 | |
| 241 | def get_magic_file_gs_path(name): |
| 242 | return '%s/%s' % (get_magic_file_base_path(), name) |
| 243 | |
| 244 | def get_magic_file_exists(name): |
| 245 | return utils.file_exists_on_cloud_storage(get_magic_file_gs_path(name)) |
| 246 | |
| 247 | def delete_magic_file(name): |
| 248 | utils.delete_file_from_cloud_storage(get_magic_file_gs_path(name)) |
| 249 | |
| 250 | def put_magic_file(name, sha): |
Morten Krogh-Jespersen | 0981b72 | 2019-10-09 10:00:33 +0200 | [diff] [blame] | 251 | utils.archive_value(name, get_magic_file_base_path(), sha) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 252 | |
| 253 | def get_magic_file_content(name, ignore_errors=False): |
| 254 | return utils.cat_file_on_cloud_storage(get_magic_file_gs_path(name), |
| 255 | ignore_errors=ignore_errors) |
| 256 | |
| 257 | def print_magic_file_state(): |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 258 | log('Magic file status:') |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 259 | for magic in ALL_MAGIC: |
| 260 | if get_magic_file_exists(magic): |
| 261 | content = get_magic_file_content(magic, ignore_errors=True) |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 262 | log('%s content: %s' % (magic, content)) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 263 | |
Rico Wind | 4fd2dda | 2018-09-26 17:41:45 +0200 | [diff] [blame] | 264 | def fetch_and_print_logs(hash): |
| 265 | gs_base = 'gs://%s' % get_sha_destination(hash) |
| 266 | listing = utils.ls_files_on_cloud_storage(gs_base).strip().split('\n') |
| 267 | for entry in listing: |
| 268 | if not entry.endswith('/status'): # Ignore the overall status file |
| 269 | for to_print in [EXITCODE, TIMED_OUT, STDERR, STDOUT]: |
| 270 | gs_location = '%s%s' % (entry, to_print) |
| 271 | value = utils.cat_file_on_cloud_storage(gs_location) |
| 272 | print('\n\n%s had value:\n%s' % (to_print, value)) |
Morten Krogh-Jespersen | f241230 | 2019-10-22 10:18:04 +0200 | [diff] [blame] | 273 | print("\n\nPrinting find-min-xmx ranges for apps") |
| 274 | run_on_app.print_min_xmx_ranges_for_hash(hash, 'r8', 'lib') |
Rico Wind | 4fd2dda | 2018-09-26 17:41:45 +0200 | [diff] [blame] | 275 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 276 | def run_bot(): |
| 277 | print_magic_file_state() |
| 278 | # Ensure that there is nothing currently scheduled (broken/stopped run) |
| 279 | for magic in ALL_MAGIC: |
| 280 | if get_magic_file_exists(magic): |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 281 | log('ERROR: Synchronizing file %s exists, cleaning up' % magic) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 282 | delete_magic_file(magic) |
| 283 | print_magic_file_state() |
| 284 | assert not get_magic_file_exists(READY_FOR_TESTING) |
| 285 | git_hash = utils.get_HEAD_sha1() |
| 286 | put_magic_file(READY_FOR_TESTING, git_hash) |
| 287 | begin = time.time() |
| 288 | while True: |
| 289 | if time.time() - begin > BOT_RUN_TIMEOUT: |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 290 | log('Timeout exceeded: http://go/internal-r8-doc') |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 291 | raise Exception('Bot timeout') |
| 292 | if get_magic_file_exists(TESTING_COMPLETE): |
| 293 | if get_magic_file_content(TESTING_COMPLETE) == git_hash: |
| 294 | break |
| 295 | else: |
| 296 | raise Exception('Non matching git hashes %s and %s' % ( |
| 297 | get_magic_file_content(TESTING_COMPLETE), git_hash)) |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 298 | log('Still waiting for test result') |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 299 | print_magic_file_state() |
| 300 | time.sleep(PULL_DELAY) |
| 301 | total_time = time.time()-begin |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 302 | log('Done running test for %s in %ss' % (git_hash, total_time)) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 303 | test_status = get_status(git_hash) |
| 304 | delete_magic_file(TESTING_COMPLETE) |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 305 | log('Test status is: %s' % test_status) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 306 | if test_status != '0': |
Rico Wind | 9e9449e | 2019-04-04 14:42:29 +0200 | [diff] [blame] | 307 | print('Tests failed, you can print the logs by running(googlers only):') |
Rico Wind | 1cb65e1 | 2019-04-26 08:54:17 +0200 | [diff] [blame] | 308 | print(' tools/internal_test.py --print_logs %s' % git_hash) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 309 | return 1 |
| 310 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 311 | def run_continuously(): |
| 312 | # If this script changes, we will restart ourselves |
| 313 | own_content = get_own_file_content() |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 314 | while True: |
| 315 | restart_if_new_version(own_content) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 316 | print_magic_file_state() |
| 317 | if get_magic_file_exists(READY_FOR_TESTING): |
| 318 | git_hash = get_magic_file_content(READY_FOR_TESTING) |
| 319 | checked_out = git_checkout(git_hash) |
Rico Wind | d7d9106 | 2019-04-29 09:24:10 +0200 | [diff] [blame] | 320 | if not checked_out: |
| 321 | # Gerrit change, we don't run these on internal. |
| 322 | archive_status(0) |
| 323 | put_magic_file(TESTING_COMPLETE, git_hash) |
| 324 | delete_magic_file(READY_FOR_TESTING) |
| 325 | continue |
Rico Wind | 9519a15 | 2019-01-23 13:34:20 +0100 | [diff] [blame] | 326 | # If the script changed, we need to restart now to get correct commands |
| 327 | # Note that we have not removed the READY_FOR_TESTING yet, so if we |
| 328 | # execv we will pick up the same version. |
| 329 | restart_if_new_version(own_content) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 330 | # Sanity check, if this does not succeed stop. |
| 331 | if checked_out != git_hash: |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 332 | log('Inconsistent state: %s %s' % (git_hash, checked_out)) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 333 | sys.exit(1) |
| 334 | put_magic_file(TESTING, git_hash) |
| 335 | delete_magic_file(READY_FOR_TESTING) |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 336 | log('Running with hash: %s' % git_hash) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 337 | exitcode = run_once(archive=True) |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 338 | log('Running finished with exit code %s' % exitcode) |
Rico Wind | f5175ae | 2020-01-24 08:12:05 +0100 | [diff] [blame] | 339 | # If the bot timed out or something else triggered the bot to fail, don't |
| 340 | # put up the result (it will not be displayed anywhere, and we can't |
| 341 | # remove the magic file if the bot cleaned up). |
| 342 | if get_magic_file_exists(TESTING): |
| 343 | put_magic_file(TESTING_COMPLETE, git_hash) |
| 344 | # There is still a potential race here (we check, bot deletes, we try to |
| 345 | # delete) - this is unlikely and we ignore it (restart if it happens). |
| 346 | delete_magic_file(TESTING) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 347 | time.sleep(PULL_DELAY) |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 348 | |
| 349 | def handle_output(archive, stderr, stdout, exitcode, timed_out, cmd): |
| 350 | if archive: |
| 351 | archive_log(stdout, stderr, exitcode, timed_out, cmd) |
| 352 | else: |
| 353 | print 'Execution of %s resulted in:' % cmd |
| 354 | print 'exit code: %s ' % exitcode |
| 355 | print 'timeout: %s ' % timed_out |
| 356 | with open(stderr, 'r') as f: |
| 357 | print 'stderr: %s' % f.read() |
| 358 | with open(stdout, 'r') as f: |
| 359 | print 'stdout: %s' % f.read() |
| 360 | |
Rico Wind | 6e2205d | 2018-10-25 13:27:13 +0200 | [diff] [blame] | 361 | def execute(cmd, archive, env=None): |
Morten Krogh-Jespersen | b042c97 | 2019-11-04 08:57:59 +0100 | [diff] [blame] | 362 | if cmd == []: |
| 363 | return |
| 364 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 365 | utils.PrintCmd(cmd) |
| 366 | with utils.TempDir() as temp: |
| 367 | try: |
| 368 | stderr_fd = None |
| 369 | stdout_fd = None |
| 370 | exitcode = 0 |
| 371 | stderr = os.path.join(temp, 'stderr') |
| 372 | stderr_fd = open(stderr, 'w') |
| 373 | stdout = os.path.join(temp, 'stdout') |
| 374 | stdout_fd = open(stdout, 'w') |
| 375 | popen = subprocess.Popen(cmd, |
| 376 | bufsize=1024*1024*10, |
| 377 | stdout=stdout_fd, |
Rico Wind | 6e2205d | 2018-10-25 13:27:13 +0200 | [diff] [blame] | 378 | stderr=stderr_fd, |
| 379 | env=env) |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 380 | begin = time.time() |
| 381 | timed_out = False |
| 382 | while popen.poll() == None: |
| 383 | if time.time() - begin > RUN_TIMEOUT: |
| 384 | popen.terminate() |
| 385 | timed_out = True |
| 386 | time.sleep(2) |
| 387 | exitcode = popen.returncode |
| 388 | finally: |
| 389 | if stderr_fd: |
| 390 | stderr_fd.close() |
| 391 | if stdout_fd: |
| 392 | stdout_fd.close() |
Morten Krogh-Jespersen | 4df02ad | 2019-10-08 14:58:39 +0000 | [diff] [blame] | 393 | if exitcode != 0: |
| 394 | handle_output(archive, stderr, stdout, popen.returncode, |
| 395 | timed_out, ' '.join(cmd)) |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 396 | return exitcode |
| 397 | |
| 398 | def run_once(archive): |
| 399 | failed = False |
| 400 | git_hash = utils.get_HEAD_sha1() |
Rico Wind | 1200f51 | 2018-09-26 08:48:37 +0200 | [diff] [blame] | 401 | log('Running once with hash %s' % git_hash) |
Rico Wind | 6e2205d | 2018-10-25 13:27:13 +0200 | [diff] [blame] | 402 | env = os.environ.copy() |
| 403 | # Bot does not have a lot of memory. |
Rico Wind | 3defc8d | 2019-03-27 08:07:31 +0100 | [diff] [blame] | 404 | env['R8_GRADLE_CORES_PER_FORK'] = '16' |
Morten Krogh-Jespersen | 2243b16 | 2019-01-14 08:40:53 +0100 | [diff] [blame] | 405 | failed = any([execute(cmd, archive, env) for cmd in TEST_COMMANDS]) |
Rico Wind | ba63dc8 | 2019-03-29 14:33:47 +0100 | [diff] [blame] | 406 | # Gradle daemon occasionally leaks memory, stop it. |
Rico Wind | 7423300 | 2019-04-04 08:28:58 +0200 | [diff] [blame] | 407 | gradle.RunGradle(['--stop']) |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 408 | archive_status(1 if failed else 0) |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 409 | return failed |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 410 | |
| 411 | def Main(): |
| 412 | (options, args) = ParseOptions() |
| 413 | if options.continuous: |
| 414 | run_continuously() |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 415 | elif options.bot: |
| 416 | return run_bot() |
Rico Wind | 4fd2dda | 2018-09-26 17:41:45 +0200 | [diff] [blame] | 417 | elif options.print_logs: |
| 418 | return fetch_and_print_logs(options.print_logs) |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 419 | else: |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 420 | return run_once(options.archive) |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 421 | |
| 422 | if __name__ == '__main__': |
| 423 | sys.exit(Main()) |