Ian Zerny | 161ff74 | 2022-01-20 12:39:40 +0100 | [diff] [blame] | 1 | #!/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 | |
| 6 | import argparse |
| 7 | import os |
| 8 | import subprocess |
| 9 | import sys |
| 10 | |
Ian Zerny | bf22d4e | 2022-04-07 12:31:21 +0200 | [diff] [blame] | 11 | import compiledump |
Ian Zerny | 161ff74 | 2022-01-20 12:39:40 +0100 | [diff] [blame] | 12 | import gradle |
| 13 | import jdk |
| 14 | import utils |
| 15 | |
Søren Gjesse | 832a662 | 2023-10-12 13:51:24 +0200 | [diff] [blame] | 16 | GOLEM_BUILD_TARGETS_TESTS = [ |
| 17 | utils.GRADLE_TASK_ALL_TESTS_WITH_APPLY_MAPPING_JAR, |
Rico Wind | 9365e94 | 2024-03-27 12:58:05 +0100 | [diff] [blame] | 18 | utils.GRADLE_TASK_TESTBASE_WITH_APPLY_MAPPING_JAR, |
Søren Gjesse | 832a662 | 2023-10-12 13:51:24 +0200 | [diff] [blame] | 19 | utils.GRADLE_TASK_TEST_DEPS_JAR |
| 20 | ] |
Christoffer Adamsen | 1659b42 | 2024-08-16 13:53:34 +0200 | [diff] [blame] | 21 | GOLEM_BUILD_TARGETS = [ |
| 22 | utils.GRADLE_TASK_R8LIB, utils.GRADLE_TASK_KEEP_ANNO_JAR |
| 23 | ] + GOLEM_BUILD_TARGETS_TESTS |
Søren Gjesse | 832a662 | 2023-10-12 13:51:24 +0200 | [diff] [blame] | 24 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 25 | |
Ian Zerny | d5061b3 | 2022-02-02 13:24:30 +0100 | [diff] [blame] | 26 | def get_golem_resource_path(benchmark): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 27 | return os.path.join('benchmarks', benchmark) |
| 28 | |
Ian Zerny | d5061b3 | 2022-02-02 13:24:30 +0100 | [diff] [blame] | 29 | |
Ian Zerny | 161ff74 | 2022-01-20 12:39:40 +0100 | [diff] [blame] | 30 | def get_jdk_home(options, benchmark): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 31 | if options.golem: |
| 32 | return os.path.join(get_golem_resource_path(benchmark), 'linux') |
| 33 | return None |
| 34 | |
Ian Zerny | 161ff74 | 2022-01-20 12:39:40 +0100 | [diff] [blame] | 35 | |
| 36 | def parse_options(argv): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 37 | result = argparse.ArgumentParser(description='Run test-based benchmarks.') |
| 38 | result.add_argument('--golem', |
| 39 | help='Indicate this as a run on golem', |
| 40 | default=False, |
| 41 | action='store_true') |
| 42 | result.add_argument('--benchmark', |
| 43 | help='The test benchmark to run', |
| 44 | required=True) |
| 45 | result.add_argument( |
| 46 | '--target', |
| 47 | help='The test target to run', |
| 48 | required=True, |
| 49 | # These should 1:1 with benchmarks/BenchmarkTarget.java |
| 50 | choices=['d8', 'r8-full', 'r8-force', 'r8-compat']) |
Christoffer Adamsen | 1659b42 | 2024-08-16 13:53:34 +0200 | [diff] [blame] | 51 | result.add_argument( |
| 52 | '--debug-agent', |
| 53 | '--debug_agent', |
| 54 | help= |
| 55 | 'Enable Java debug agent and suspend compilation (default disabled)', |
| 56 | default=False, |
| 57 | action='store_true') |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 58 | result.add_argument('--nolib', |
| 59 | '--no-lib', |
| 60 | '--no-r8lib', |
| 61 | help='Run the non-lib R8 build (default false)', |
| 62 | default=False, |
| 63 | action='store_true') |
| 64 | result.add_argument('--no-build', |
| 65 | '--no_build', |
| 66 | help='Run without building first (default false)', |
| 67 | default=False, |
| 68 | action='store_true') |
| 69 | result.add_argument('--enable-assertions', |
| 70 | '--enable_assertions', |
| 71 | '-ea', |
| 72 | help='Enable assertions when running', |
| 73 | default=False, |
| 74 | action='store_true') |
Christoffer Adamsen | bc1d4ef | 2024-06-04 14:51:02 +0200 | [diff] [blame] | 75 | result.add_argument('--iterations', |
| 76 | '-i', |
| 77 | help='Number of iterations to run', |
| 78 | type=int) |
| 79 | result.add_argument('--output', |
| 80 | help='Output path where to write the result') |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 81 | result.add_argument('--print-times', |
| 82 | help='Print timing information from r8', |
| 83 | default=False, |
| 84 | action='store_true') |
| 85 | result.add_argument( |
| 86 | '--version', |
| 87 | '-v', |
| 88 | help='Use R8 version/hash for the run (default local build)', |
| 89 | default=None) |
Christoffer Adamsen | c748e52 | 2024-06-12 18:48:45 +0200 | [diff] [blame] | 90 | result.add_argument( |
| 91 | '--version-jar', |
| 92 | help='The r8.jar corresponding to the version given at --version.', |
| 93 | default=None) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 94 | result.add_argument('--temp', |
| 95 | help='A directory to use for temporaries and outputs.', |
| 96 | default=None) |
Christoffer Adamsen | 8c13d04 | 2024-06-12 13:08:06 +0200 | [diff] [blame] | 97 | result.add_argument('--verbose', |
| 98 | help='To enable verbose logging.', |
| 99 | action='store_true', |
| 100 | default=False) |
| 101 | options, args = result.parse_known_args(argv) |
| 102 | options.quiet = not options.verbose |
Christoffer Adamsen | d064d30 | 2024-06-12 13:08:30 +0200 | [diff] [blame] | 103 | # We must download the non-lib distribution when running with a specific |
| 104 | # version, since BenchmarkMainEntryRunner is using R8 internals. |
| 105 | # TODO(b/346477461): Look into removing this limitation. |
| 106 | assert options.version is None or options.nolib |
Christoffer Adamsen | 8c13d04 | 2024-06-12 13:08:06 +0200 | [diff] [blame] | 107 | return options, args |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 108 | |
Ian Zerny | 161ff74 | 2022-01-20 12:39:40 +0100 | [diff] [blame] | 109 | |
Ian Zerny | 171c6a6 | 2022-04-04 14:25:30 +0200 | [diff] [blame] | 110 | def main(argv, temp): |
Christoffer Adamsen | 8c13d04 | 2024-06-12 13:08:06 +0200 | [diff] [blame] | 111 | options, args = parse_options(argv) |
Ian Zerny | 161ff74 | 2022-01-20 12:39:40 +0100 | [diff] [blame] | 112 | |
Christoffer Adamsen | bc1d4ef | 2024-06-04 14:51:02 +0200 | [diff] [blame] | 113 | if options.output: |
| 114 | options.output = os.path.abspath(options.output) |
| 115 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 116 | if options.temp: |
| 117 | temp = options.temp |
Christoffer Adamsen | bc1d4ef | 2024-06-04 14:51:02 +0200 | [diff] [blame] | 118 | os.makedirs(temp, exist_ok=True) |
Ian Zerny | 171c6a6 | 2022-04-04 14:25:30 +0200 | [diff] [blame] | 119 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 120 | if options.golem: |
| 121 | options.no_build = True |
| 122 | if options.nolib: |
| 123 | print("Error: golem should always run r8lib") |
| 124 | return 1 |
| 125 | |
Ian Zerny | 161ff74 | 2022-01-20 12:39:40 +0100 | [diff] [blame] | 126 | if options.nolib: |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 127 | testBuildTargets = [ |
Christoffer Adamsen | 9daa13b | 2024-06-12 13:07:58 +0200 | [diff] [blame] | 128 | utils.GRADLE_TASK_TEST_JAR, utils.GRADLE_TASK_TEST_DEPS_JAR, |
Christoffer Adamsen | d58f047 | 2024-06-04 14:28:12 +0200 | [diff] [blame] | 129 | utils.GRADLE_TASK_TEST_UNZIP_TESTBASE |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 130 | ] |
Christoffer Adamsen | 1659b42 | 2024-08-16 13:53:34 +0200 | [diff] [blame] | 131 | buildTargets = [utils.GRADLE_TASK_R8, utils.GRADLE_TASK_KEEP_ANNO_JAR |
| 132 | ] + testBuildTargets |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 133 | r8jar = utils.R8_JAR |
Christoffer Adamsen | 9daa13b | 2024-06-12 13:07:58 +0200 | [diff] [blame] | 134 | testjars = [ |
| 135 | utils.R8_TESTS_JAR, utils.R8_TESTS_DEPS_JAR, utils.R8_TESTBASE_JAR |
| 136 | ] |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 137 | else: |
| 138 | testBuildTargets = GOLEM_BUILD_TARGETS_TESTS |
| 139 | buildTargets = GOLEM_BUILD_TARGETS |
| 140 | r8jar = utils.R8LIB_JAR |
| 141 | testjars = [ |
| 142 | os.path.join(utils.R8LIB_TESTS_JAR), |
Rico Wind | 9365e94 | 2024-03-27 12:58:05 +0100 | [diff] [blame] | 143 | os.path.join(utils.R8LIB_TESTS_DEPS_JAR), |
| 144 | os.path.join(utils.R8LIB_TESTBASE_JAR) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 145 | ] |
Ian Zerny | 161ff74 | 2022-01-20 12:39:40 +0100 | [diff] [blame] | 146 | |
Christoffer Adamsen | c748e52 | 2024-06-12 18:48:45 +0200 | [diff] [blame] | 147 | if options.version or options.version_jar: |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 148 | # r8 is downloaded so only test jar needs to be built. |
| 149 | buildTargets = testBuildTargets |
Christoffer Adamsen | c748e52 | 2024-06-12 18:48:45 +0200 | [diff] [blame] | 150 | r8jar = options.version_jar or compiledump.download_distribution( |
| 151 | options.version, options, temp) |
Ian Zerny | 161ff74 | 2022-01-20 12:39:40 +0100 | [diff] [blame] | 152 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 153 | if not options.no_build: |
| 154 | gradle.RunGradle(buildTargets + ['-Pno_internal']) |
Ian Zerny | 171c6a6 | 2022-04-04 14:25:30 +0200 | [diff] [blame] | 155 | |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 156 | if not options.golem: |
| 157 | # When running locally, change the working directory to be in 'temp'. |
| 158 | # This is hard to do properly within the JVM so we do it here. |
| 159 | with utils.ChangedWorkingDirectory(temp): |
| 160 | return run(options, r8jar, testjars) |
| 161 | else: |
| 162 | return run(options, r8jar, testjars) |
Ian Zerny | 161ff74 | 2022-01-20 12:39:40 +0100 | [diff] [blame] | 163 | |
Ian Zerny | 161ff74 | 2022-01-20 12:39:40 +0100 | [diff] [blame] | 164 | |
| 165 | def run(options, r8jar, testjars): |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 166 | jdkhome = get_jdk_home(options, options.benchmark) |
Christoffer Adamsen | 8c13d04 | 2024-06-12 13:08:06 +0200 | [diff] [blame] | 167 | cmd = [ |
| 168 | jdk.GetJavaExecutable(jdkhome), '-Xms8g', '-Xmx8g', |
Christoffer Adamsen | 39c00c2 | 2024-06-12 18:48:27 +0200 | [diff] [blame] | 169 | '-XX:+TieredCompilation', '-XX:TieredStopAtLevel=4', |
Christoffer Adamsen | 1659b42 | 2024-08-16 13:53:34 +0200 | [diff] [blame] | 170 | '-DBENCHMARK_IGNORE_CODE_SIZE_DIFFERENCES', |
Christoffer Adamsen | e1fe9b6 | 2024-08-29 16:00:36 +0200 | [diff] [blame^] | 171 | f'-DBUILD_PROP_KEEPANNO_RUNTIME_PATH={utils.REPO_ROOT}/d8_r8/keepanno/build/classes/java/main', |
| 172 | # Since we change the working directory to a temp folder. |
| 173 | f'-DREPO_ROOT={utils.REPO_ROOT}' |
Christoffer Adamsen | 8c13d04 | 2024-06-12 13:08:06 +0200 | [diff] [blame] | 174 | ] |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 175 | if options.enable_assertions: |
| 176 | cmd.append('-ea') |
| 177 | if options.print_times: |
| 178 | cmd.append('-Dcom.android.tools.r8.printtimes=1') |
| 179 | if not options.golem: |
| 180 | cmd.extend([ |
Ian Zerny | 1dd09cb | 2024-04-22 07:38:58 +0200 | [diff] [blame] | 181 | f'-DTEST_DATA_LOCATION={utils.REPO_ROOT}/d8_r8/test_modules/tests_java_8/build/classes/java/test', |
| 182 | f'-DTESTBASE_DATA_LOCATION={utils.REPO_ROOT}/d8_r8/test_modules/testbase/build/classes/java/main', |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 183 | ]) |
Christoffer Adamsen | 8c13d04 | 2024-06-12 13:08:06 +0200 | [diff] [blame] | 184 | if options.iterations is not None: |
| 185 | if options.iterations == 0: |
| 186 | return |
Christoffer Adamsen | bc1d4ef | 2024-06-04 14:51:02 +0200 | [diff] [blame] | 187 | cmd.append(f'-DBENCHMARK_ITERATIONS={options.iterations}') |
| 188 | if options.output: |
| 189 | cmd.append(f'-DBENCHMARK_OUTPUT={options.output}') |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 190 | cmd.extend(['-cp', ':'.join([r8jar] + testjars)]) |
Christoffer Adamsen | 1659b42 | 2024-08-16 13:53:34 +0200 | [diff] [blame] | 191 | if options.debug_agent: |
| 192 | cmd.append( |
| 193 | '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005' |
| 194 | ) |
Søren Gjesse | d54faae | 2023-10-12 09:07:54 +0200 | [diff] [blame] | 195 | cmd.extend([ |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 196 | 'com.android.tools.r8.benchmarks.BenchmarkMainEntryRunner', |
| 197 | options.benchmark, |
| 198 | options.target, |
| 199 | # When running locally the working directory is moved and we pass the |
| 200 | # repository root as an argument. The runner can then setup dependencies. |
| 201 | 'golem' if options.golem else utils.REPO_ROOT, |
Ian Zerny | 161ff74 | 2022-01-20 12:39:40 +0100 | [diff] [blame] | 202 | ]) |
Christoffer Adamsen | 8c13d04 | 2024-06-12 13:08:06 +0200 | [diff] [blame] | 203 | utils.PrintCmd(cmd, quiet=options.quiet) |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 204 | return subprocess.check_call(cmd) |
| 205 | |
Ian Zerny | 161ff74 | 2022-01-20 12:39:40 +0100 | [diff] [blame] | 206 | |
| 207 | if __name__ == '__main__': |
Christoffer Quist Adamsen | 2434a4d | 2023-10-16 11:29:03 +0200 | [diff] [blame] | 208 | with utils.TempDir() as temp: |
| 209 | sys.exit(main(sys.argv[1:], temp)) |