blob: 41da4515867228f551b8fe6336519e05127adda6 [file] [log] [blame]
Ian Zerny161ff742022-01-20 12:39:40 +01001#!/usr/bin/env python3
2# Copyright (c) 2022, 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
6import argparse
7import os
8import subprocess
9import sys
10
Ian Zernybf22d4e2022-04-07 12:31:21 +020011import compiledump
Ian Zerny161ff742022-01-20 12:39:40 +010012import gradle
13import jdk
14import utils
15
16NONLIB_BUILD_TARGET = 'R8WithRelocatedDeps'
17NONLIB_TEST_BUILD_TARGETS = [utils.R8_TESTS_TARGET, utils.R8_TESTS_DEPS_TARGET]
18
19R8LIB_BUILD_TARGET = utils.R8LIB
20R8LIB_TEST_BUILD_TARGETS = [utils.R8LIB_TESTS_TARGET, utils.R8LIB_TESTS_DEPS_TARGET]
21
22# The r8lib target is always the golem target.
23GOLEM_BUILD_TARGETS = [R8LIB_BUILD_TARGET] + R8LIB_TEST_BUILD_TARGETS
24
Ian Zernyd5061b32022-02-02 13:24:30 +010025def get_golem_resource_path(benchmark):
Ian Zernyfa776422022-03-09 07:15:29 +010026 return os.path.join('benchmarks', benchmark)
Ian Zernyd5061b32022-02-02 13:24:30 +010027
Ian Zerny161ff742022-01-20 12:39:40 +010028def get_jdk_home(options, benchmark):
29 if options.golem:
Ian Zernyd5061b32022-02-02 13:24:30 +010030 return os.path.join(get_golem_resource_path(benchmark), 'linux')
Ian Zerny161ff742022-01-20 12:39:40 +010031 return None
32
33def parse_options(argv):
34 result = argparse.ArgumentParser(description = 'Run test-based benchmarks.')
35 result.add_argument('--golem',
36 help='Indicate this as a run on golem',
37 default=False,
38 action='store_true')
39 result.add_argument('--benchmark',
40 help='The test benchmark to run',
41 required=True)
42 result.add_argument('--target',
43 help='The test target to run',
44 required=True,
Ian Zerny221a99b2022-03-09 15:08:26 +010045 # These should 1:1 with benchmarks/BenchmarkTarget.java
46 choices=['d8', 'r8-full', 'r8-force', 'r8-compat'])
Ian Zerny161ff742022-01-20 12:39:40 +010047 result.add_argument('--nolib', '--no-lib', '--no-r8lib',
48 help='Run the non-lib R8 build (default false)',
49 default=False,
50 action='store_true')
51 result.add_argument('--no-build', '--no_build',
52 help='Run without building first (default false)',
53 default=False,
54 action='store_true')
55 result.add_argument('--enable-assertions', '--enable_assertions', '-ea',
56 help='Enable assertions when running',
57 default=False,
58 action='store_true')
59 result.add_argument('--print-times',
60 help='Print timing information from r8',
61 default=False,
62 action='store_true')
Ian Zerny171c6a62022-04-04 14:25:30 +020063 result.add_argument('--version', '-v',
64 help='Use R8 version/hash for the run (default local build)',
65 default=None)
Ian Zerny161ff742022-01-20 12:39:40 +010066 result.add_argument('--temp',
67 help='A directory to use for temporaries and outputs.',
68 default=None)
69 return result.parse_known_args(argv)
70
Ian Zerny171c6a62022-04-04 14:25:30 +020071def main(argv, temp):
Ian Zerny161ff742022-01-20 12:39:40 +010072 (options, args) = parse_options(argv)
73
Ian Zerny171c6a62022-04-04 14:25:30 +020074 if options.temp:
75 temp = options.temp
76
Ian Zerny161ff742022-01-20 12:39:40 +010077 if options.golem:
78 options.no_build = True
79 if options.nolib:
80 print("Error: golem should always run r8lib")
81 return 1
82
83 if options.nolib:
Ian Zerny171c6a62022-04-04 14:25:30 +020084 testBuildTargets = NONLIB_TEST_BUILD_TARGETS
Ian Zerny161ff742022-01-20 12:39:40 +010085 buildTargets = [NONLIB_BUILD_TARGET] + NONLIB_TEST_BUILD_TARGETS
86 r8jar = utils.R8_WITH_RELOCATED_DEPS_JAR
87 testjars = [utils.R8_TESTS_DEPS_JAR, utils.R8_TESTS_JAR]
88 else:
Ian Zerny171c6a62022-04-04 14:25:30 +020089 testBuildTargets = R8LIB_TEST_BUILD_TARGETS
Ian Zerny161ff742022-01-20 12:39:40 +010090 buildTargets = GOLEM_BUILD_TARGETS
91 r8jar = utils.R8LIB_JAR
92 testjars = [utils.R8LIB_TESTS_DEPS_JAR, utils.R8LIB_TESTS_JAR]
93
Ian Zerny171c6a62022-04-04 14:25:30 +020094 if options.version:
95 # r8 is downloaded so only test jar needs to be built.
96 buildTargets = testBuildTargets
97 r8jar = compiledump.download_distribution(options.version, options.nolib, temp)
98
Ian Zerny161ff742022-01-20 12:39:40 +010099 if not options.no_build:
100 gradle.RunGradle(buildTargets + ['-Pno_internal'])
101
Ian Zernybf22d4e2022-04-07 12:31:21 +0200102 if not options.golem:
103 # When running locally, change the working directory to be in 'temp'.
104 # This is hard to do properly within the JVM so we do it here.
105 with utils.ChangedWorkingDirectory(temp):
106 return run(options, r8jar, testjars)
107 else:
108 return run(options, r8jar, testjars)
Ian Zerny161ff742022-01-20 12:39:40 +0100109
110def run(options, r8jar, testjars):
111 jdkhome = get_jdk_home(options, options.benchmark)
112 cmd = [jdk.GetJavaExecutable(jdkhome)]
113 if options.enable_assertions:
114 cmd.append('-ea')
115 if options.print_times:
116 cmd.append('-Dcom.android.tools.r8.printtimes=1')
117 cmd.extend(['-cp', ':'.join([r8jar] + testjars)])
118 cmd.extend([
119 'com.android.tools.r8.benchmarks.BenchmarkMainEntryRunner',
120 options.benchmark,
121 options.target,
Ian Zernybf22d4e2022-04-07 12:31:21 +0200122 # When running locally the working directory is moved and we pass the
123 # repository root as an argument. The runner can then setup dependencies.
124 'golem' if options.golem else utils.REPO_ROOT,
Ian Zerny161ff742022-01-20 12:39:40 +0100125 ])
126 return subprocess.check_call(cmd)
127
128if __name__ == '__main__':
Ian Zerny171c6a62022-04-04 14:25:30 +0200129 with utils.TempDir() as temp:
130 sys.exit(main(sys.argv[1:], temp))