blob: f0bdf4b94ea7723350f1fb28629bfe280f5b6b43 [file] [log] [blame]
Ian Zernydcb172e2022-02-22 15:36:45 +01001#!/usr/bin/env python3
Rico Wind800fd712018-09-24 11:29:33 +02002# 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 Wind139eece2018-09-25 09:42:09 +02007# 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 Wind800fd712018-09-24 11:29:33 +020031
Rico Windba63dc82019-03-29 14:33:47 +010032import gradle
Rico Wind800fd712018-09-24 11:29:33 +020033import optparse
34import os
35import subprocess
36import sys
37import time
38import utils
Morten Krogh-Jespersenf2412302019-10-22 10:18:04 +020039import run_on_app
Rico Wind800fd712018-09-24 11:29:33 +020040
Christoffer Quist Adamsen81d41502021-06-25 09:33:43 +020041import chrome_data
Christoffer Quist Adamsen4380de32021-04-23 06:24:31 +020042import youtube_data
43
Rico Wind139eece2018-09-25 09:42:09 +020044# How often the bot/tester should check state
45PULL_DELAY = 30
Rico Wind800fd712018-09-24 11:29:33 +020046TEST_RESULT_DIR = 'internal'
47
Rico Wind139eece2018-09-25 09:42:09 +020048# Magic files
49READY_FOR_TESTING = 'READY_FOR_TESTING'
50TESTING = 'TESTING'
51TESTING_COMPLETE = 'TESTING_COMPLETE'
52
53ALL_MAGIC = [READY_FOR_TESTING, TESTING, TESTING_COMPLETE]
54
55# Log file names
56STDERR = 'stderr'
57STDOUT = 'stdout'
58EXITCODE = 'exitcode'
59TIMED_OUT = 'timed_out'
60
Christoffer Quist Adamsenb8ab39d2023-08-23 12:22:10 +020061BENCHMARK_APPS = [chrome_data, youtube_data]
Morten Krogh-Jespersen6cd9f1d2019-10-09 14:01:04 +020062
Rico Winda7b8fff2023-07-03 07:49:17 +020063DEPENDENT_PYTHON_FILES = [gradle, utils, run_on_app]
64
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020065
Christoffer Quist Adamsen81d41502021-06-25 09:33:43 +020066def find_min_xmx_command(app_data):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020067 record = app_data.GetMemoryData(app_data.GetLatestVersion())
68 assert record['find-xmx-min'] < record['find-xmx-max']
69 assert record[
70 'find-xmx-range'] < record['find-xmx-max'] - record['find-xmx-min']
71 return [
72 'tools/run_on_app.py', '--compiler=r8', '--compiler-build=lib',
73 '--app=%s' % app_data.GetName(),
74 '--version=%s' % app_data.GetLatestVersion(), '--no-debug',
75 '--no-build', '--find-min-xmx',
76 '--find-min-xmx-min-memory=%s' % record['find-xmx-min'],
77 '--find-min-xmx-max-memory=%s' % record['find-xmx-max'],
78 '--find-min-xmx-range-size=%s' % record['find-xmx-range'],
79 '--find-min-xmx-archive'
80 ]
81
Morten Krogh-Jespersen6cd9f1d2019-10-09 14:01:04 +020082
Christoffer Quist Adamsen81d41502021-06-25 09:33:43 +020083def compile_with_memory_max_command(app_data):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020084 # TODO(b/152939233): Remove this special handling when fixed.
85 factor = 1.25 if app_data.GetName() == 'chrome' else 1.15
86 record = app_data.GetMemoryData(app_data.GetLatestVersion())
87 return [] if 'skip-find-xmx-max' in record else [
88 'tools/run_on_app.py', '--compiler=r8', '--compiler-build=lib',
89 '--app=%s' % app_data.GetName(),
90 '--version=%s' %
91 app_data.GetLatestVersion(), '--no-debug', '--no-build',
92 '--max-memory=%s' % int(record['oom-threshold'] * factor)
93 ]
94
Morten Krogh-Jespersen3793dc92019-10-09 14:48:48 +020095
Christoffer Quist Adamsen81d41502021-06-25 09:33:43 +020096def compile_with_memory_min_command(app_data):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020097 record = app_data.GetMemoryData(app_data.GetLatestVersion())
98 return [
99 'tools/run_on_app.py', '--compiler=r8', '--compiler-build=lib',
100 '--app=%s' % app_data.GetName(),
101 '--version=%s' % app_data.GetLatestVersion(), '--no-debug',
102 '--no-build', '--expect-oom',
103 '--max-memory=%s' % int(record['oom-threshold'] * 0.85)
104 ]
105
Morten Krogh-Jespersen3793dc92019-10-09 14:48:48 +0200106
Ian Zernydf1deca2023-10-12 10:38:46 +0200107CLEAN_COMMANDS = [
Rico Wind5a360e92022-03-31 08:49:25 +0200108 # Make sure we have a clean build to not be polluted by old test files
Rico Winde0c7cdf2023-10-31 08:16:03 +0100109 ['tools/gradle.py', 'clean'],
Ian Zernydf1deca2023-10-12 10:38:46 +0200110]
111
112# TODO(b/210982978): Enable testing of min xmx again
113TEST_COMMANDS = [
Morten Krogh-Jespersen2243b162019-01-14 08:40:53 +0100114 # Run test.py internal testing.
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200115 [
116 'tools/test.py', '--only_internal', '--slow_tests',
117 '--java_max_memory_size=8G'
118 ],
Morten Krogh-Jespersen2243b162019-01-14 08:40:53 +0100119 # Ensure that all internal apps compile.
Rico Windbeeaded2024-04-11 09:49:37 +0200120 ['tools/run_on_app.py', '--run-all', '--out=out', '--workers', '3'],
Morten Krogh-Jespersenfcb06452021-12-16 15:19:49 +0100121]
Morten Krogh-Jespersen3793dc92019-10-09 14:48:48 +0200122
Rico Wind6847d132018-09-26 08:18:48 +0200123# Command timeout, in seconds.
Rico Wind39c73eb2023-08-17 08:59:01 +0200124RUN_TIMEOUT = 3600 * 7
Rico Windf021d832018-12-13 11:29:22 +0100125BOT_RUN_TIMEOUT = RUN_TIMEOUT * len(TEST_COMMANDS)
Rico Wind6847d132018-09-26 08:18:48 +0200126
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200127
Rico Wind1200f512018-09-26 08:48:37 +0200128def log(str):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200129 print("%s: %s" % (time.strftime("%c"), str))
130 sys.stdout.flush()
131
Rico Wind1200f512018-09-26 08:48:37 +0200132
Rico Wind800fd712018-09-24 11:29:33 +0200133def ParseOptions():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200134 result = optparse.OptionParser()
135 result.add_option(
136 '--continuous',
137 help='Continuously run internal tests and post results to GCS.',
138 default=False,
139 action='store_true')
140 result.add_option(
141 '--print_logs',
142 help=
143 'Fetch logs from gcs and print them, takes the commit to print for.',
144 default=None)
145 result.add_option('--bot',
146 help='Run in bot mode, i.e., scheduling runs.',
147 default=False,
148 action='store_true')
149 result.add_option('--archive',
150 help='Post result to GCS, implied by --continuous',
151 default=False,
152 action='store_true')
153 return result.parse_args()
154
Rico Wind800fd712018-09-24 11:29:33 +0200155
Rico Wind139eece2018-09-25 09:42:09 +0200156def ensure_git_clean():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200157 # Ensure clean git repo.
158 diff = subprocess.check_output(['git', 'diff']).decode('utf-8')
159 if len(diff) > 0:
160 log('Local modifications to the git repo, exiting')
161 sys.exit(1)
162
Rico Wind139eece2018-09-25 09:42:09 +0200163
164def git_pull():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200165 ensure_git_clean()
166 subprocess.check_call(['git', 'checkout', 'main'])
167 subprocess.check_call(['git', 'pull'])
168 return utils.get_HEAD_sha1()
169
Rico Wind800fd712018-09-24 11:29:33 +0200170
Rico Wind139eece2018-09-25 09:42:09 +0200171def git_checkout(git_hash):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200172 ensure_git_clean()
173 # Ensure that we are up to date to get the commit.
174 git_pull()
175 exitcode = subprocess.call(['git', 'checkout', git_hash])
176 if exitcode != 0:
177 return None
178 return utils.get_HEAD_sha1()
179
Rico Wind139eece2018-09-25 09:42:09 +0200180
181def get_test_result_dir():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200182 return os.path.join(utils.R8_INTERNAL_TEST_RESULTS_BUCKET, TEST_RESULT_DIR)
183
Rico Wind139eece2018-09-25 09:42:09 +0200184
Rico Wind800fd712018-09-24 11:29:33 +0200185def get_sha_destination(sha):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200186 return os.path.join(get_test_result_dir(), sha)
187
Rico Wind800fd712018-09-24 11:29:33 +0200188
189def archive_status(failed):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200190 gs_destination = 'gs://%s' % get_sha_destination(utils.get_HEAD_sha1())
191 utils.archive_value('status', gs_destination, failed)
192
Rico Wind800fd712018-09-24 11:29:33 +0200193
Rico Wind139eece2018-09-25 09:42:09 +0200194def get_status(sha):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200195 gs_destination = 'gs://%s/status' % get_sha_destination(sha)
196 return utils.cat_file_on_cloud_storage(gs_destination)
197
Rico Wind139eece2018-09-25 09:42:09 +0200198
Rico Wind800fd712018-09-24 11:29:33 +0200199def archive_log(stdout, stderr, exitcode, timed_out, cmd):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200200 sha = utils.get_HEAD_sha1()
201 cmd_dir = cmd.replace(' ', '_').replace('/', '_')
202 destination = os.path.join(get_sha_destination(sha), cmd_dir)
203 gs_destination = 'gs://%s' % destination
204 url = 'https://storage.cloud.google.com/%s' % destination
205 log('Archiving logs to: %s' % gs_destination)
206 utils.archive_value(EXITCODE, gs_destination, exitcode)
207 utils.archive_value(TIMED_OUT, gs_destination, timed_out)
208 utils.archive_file(STDOUT, gs_destination, stdout)
209 utils.archive_file(STDERR, gs_destination, stderr)
210 log('Logs available at: %s' % url)
211
Rico Wind800fd712018-09-24 11:29:33 +0200212
Rico Wind139eece2018-09-25 09:42:09 +0200213def get_magic_file_base_path():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200214 return 'gs://%s/magic' % get_test_result_dir()
215
Rico Wind139eece2018-09-25 09:42:09 +0200216
217def get_magic_file_gs_path(name):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200218 return '%s/%s' % (get_magic_file_base_path(), name)
219
Rico Wind139eece2018-09-25 09:42:09 +0200220
221def get_magic_file_exists(name):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200222 return utils.file_exists_on_cloud_storage(get_magic_file_gs_path(name))
223
Rico Wind139eece2018-09-25 09:42:09 +0200224
225def delete_magic_file(name):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200226 utils.delete_file_from_cloud_storage(get_magic_file_gs_path(name))
227
Rico Wind139eece2018-09-25 09:42:09 +0200228
229def put_magic_file(name, sha):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200230 utils.archive_value(name, get_magic_file_base_path(), sha)
231
Rico Wind139eece2018-09-25 09:42:09 +0200232
233def get_magic_file_content(name, ignore_errors=False):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200234 return utils.cat_file_on_cloud_storage(get_magic_file_gs_path(name),
235 ignore_errors=ignore_errors)
236
Rico Wind139eece2018-09-25 09:42:09 +0200237
238def print_magic_file_state():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200239 log('Magic file status:')
240 for magic in ALL_MAGIC:
241 if get_magic_file_exists(magic):
242 content = get_magic_file_content(magic, ignore_errors=True)
243 log('%s content: %s' % (magic, content))
244
Rico Wind139eece2018-09-25 09:42:09 +0200245
Rico Wind4fd2dda2018-09-26 17:41:45 +0200246def fetch_and_print_logs(hash):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200247 gs_base = 'gs://%s' % get_sha_destination(hash)
248 listing = utils.ls_files_on_cloud_storage(gs_base).strip().split('\n')
249 for entry in listing:
250 if not entry.endswith('/status'): # Ignore the overall status file
251 for to_print in [EXITCODE, TIMED_OUT, STDERR, STDOUT]:
252 gs_location = '%s%s' % (entry, to_print)
253 value = utils.cat_file_on_cloud_storage(gs_location)
254 print('\n\n%s had value:\n%s' % (to_print, value))
255 print("\n\nPrinting find-min-xmx ranges for apps")
256 run_on_app.print_min_xmx_ranges_for_hash(hash, 'r8', 'lib')
257
Rico Wind4fd2dda2018-09-26 17:41:45 +0200258
Rico Wind139eece2018-09-25 09:42:09 +0200259def run_bot():
Rico Wind139eece2018-09-25 09:42:09 +0200260 print_magic_file_state()
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200261 # Ensure that there is nothing currently scheduled (broken/stopped run)
262 for magic in ALL_MAGIC:
263 if get_magic_file_exists(magic):
264 log('ERROR: Synchronizing file %s exists, cleaning up' % magic)
265 delete_magic_file(magic)
266 print_magic_file_state()
267 assert not get_magic_file_exists(READY_FOR_TESTING)
268 git_hash = utils.get_HEAD_sha1()
269 put_magic_file(READY_FOR_TESTING, git_hash)
270 begin = time.time()
271 while True:
272 if time.time() - begin > BOT_RUN_TIMEOUT:
273 log('Timeout exceeded: http://go/internal-r8-doc')
274 raise Exception('Bot timeout')
275 if get_magic_file_exists(TESTING_COMPLETE):
276 if get_magic_file_content(TESTING_COMPLETE) == git_hash:
277 break
278 else:
279 raise Exception(
280 'Non matching git hashes %s and %s' %
281 (get_magic_file_content(TESTING_COMPLETE), git_hash))
282 log('Still waiting for test result')
283 print_magic_file_state()
284 time.sleep(PULL_DELAY)
285 total_time = time.time() - begin
286 log('Done running test for %s in %ss' % (git_hash, total_time))
287 test_status = get_status(git_hash)
288 delete_magic_file(TESTING_COMPLETE)
289 log('Test status is: %s' % test_status)
290 if test_status != '0':
291 print('Tests failed, you can print the logs by running(googlers only):')
292 print(' tools/internal_test.py --print_logs %s' % git_hash)
293 return 1
294
Rico Wind139eece2018-09-25 09:42:09 +0200295
Rico Wind800fd712018-09-24 11:29:33 +0200296def run_continuously():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200297 while True:
298 print_magic_file_state()
299 if get_magic_file_exists(READY_FOR_TESTING):
300 git_hash = get_magic_file_content(READY_FOR_TESTING)
301 checked_out = git_checkout(git_hash)
302 if not checked_out:
303 # Gerrit change, we don't run these on internal.
304 archive_status(0)
305 put_magic_file(TESTING_COMPLETE, git_hash)
306 delete_magic_file(READY_FOR_TESTING)
307 continue
308 # Sanity check, if this does not succeed stop.
309 if checked_out != git_hash:
310 log('Inconsistent state: %s %s' % (git_hash, checked_out))
311 sys.exit(1)
312 put_magic_file(TESTING, git_hash)
313 delete_magic_file(READY_FOR_TESTING)
314 log('Running with hash: %s' % git_hash)
315 exitcode = run_external()
316 log('Running finished with exit code %s' % exitcode)
317 # If the bot timed out or something else triggered the bot to fail, don't
318 # put up the result (it will not be displayed anywhere, and we can't
319 # remove the magic file if the bot cleaned up).
320 if get_magic_file_exists(TESTING):
321 put_magic_file(TESTING_COMPLETE, git_hash)
322 # There is still a potential race here (we check, bot deletes, we try to
323 # delete) - this is unlikely and we ignore it (restart if it happens).
324 delete_magic_file(TESTING)
325 time.sleep(PULL_DELAY)
326
Rico Wind800fd712018-09-24 11:29:33 +0200327
Rico Wind62ed83c2023-08-25 11:01:53 +0200328def run_external():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200329 return subprocess.call(
330 [sys.executable, "tools/internal_test.py", "--archive"])
331
Rico Wind62ed83c2023-08-25 11:01:53 +0200332
Rico Wind800fd712018-09-24 11:29:33 +0200333def handle_output(archive, stderr, stdout, exitcode, timed_out, cmd):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200334 if archive:
335 archive_log(stdout, stderr, exitcode, timed_out, cmd)
336 else:
337 print('Execution of %s resulted in:' % cmd)
338 print('exit code: %s ' % exitcode)
339 print('timeout: %s ' % timed_out)
340 with open(stderr, 'r') as f:
341 print('stderr: %s' % f.read())
342 with open(stdout, 'r') as f:
343 print('stdout: %s' % f.read())
344
Rico Wind800fd712018-09-24 11:29:33 +0200345
Rico Wind6e2205d2018-10-25 13:27:13 +0200346def execute(cmd, archive, env=None):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200347 if cmd == []:
348 return
Christoffer Quist Adamsen280fae02021-11-05 13:06:17 +0100349
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200350 assert (cmd[0].endswith('.py'))
351 cmd = [sys.executable] + cmd
Christoffer Quist Adamsene2788d22021-11-05 12:37:39 +0100352
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200353 utils.PrintCmd(cmd)
354 with utils.TempDir() as temp:
355 try:
356 stderr_fd = None
357 stdout_fd = None
358 exitcode = 0
359 stderr = os.path.join(temp, 'stderr')
360 stderr_fd = open(stderr, 'w')
361 stdout = os.path.join(temp, 'stdout')
362 stdout_fd = open(stdout, 'w')
363 popen = subprocess.Popen(cmd,
364 bufsize=1024 * 1024 * 10,
365 stdout=stdout_fd,
366 stderr=stderr_fd,
367 env=env)
368 begin = time.time()
369 timed_out = False
370 while popen.poll() == None:
371 if time.time() - begin > RUN_TIMEOUT:
372 popen.terminate()
373 timed_out = True
374 time.sleep(2)
375 exitcode = popen.returncode
376 finally:
377 if stderr_fd:
378 stderr_fd.close()
379 if stdout_fd:
380 stdout_fd.close()
381 if exitcode != 0:
382 handle_output(archive, stderr, stdout, popen.returncode,
383 timed_out, ' '.join(cmd))
384 return exitcode
Morten Krogh-Jespersenb042c972019-11-04 08:57:59 +0100385
Rico Wind800fd712018-09-24 11:29:33 +0200386
387def run_once(archive):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200388 git_hash = utils.get_HEAD_sha1()
389 log('Running once with hash %s' % git_hash)
390 env = os.environ.copy()
391 # Bot does not have a lot of memory.
392 env['R8_GRADLE_CORES_PER_FORK'] = '5'
393 if archive:
394 [execute(cmd, archive, env) for cmd in CLEAN_COMMANDS]
395 failed = any([execute(cmd, archive, env) for cmd in TEST_COMMANDS])
396 # Gradle daemon occasionally leaks memory, stop it.
397 gradle.RunGradle(['--stop'])
398 archive_status(1 if failed else 0)
399 return failed
400
Rico Wind800fd712018-09-24 11:29:33 +0200401
402def Main():
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200403 (options, args) = ParseOptions()
404 if options.continuous:
405 run_continuously()
406 elif options.bot:
407 return run_bot()
408 elif options.print_logs:
409 return fetch_and_print_logs(options.print_logs)
410 else:
411 return run_once(options.archive)
412
Rico Wind800fd712018-09-24 11:29:33 +0200413
414if __name__ == '__main__':
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200415 sys.exit(Main())