blob: 783e695d85d85338c552e7faecbbc679f2c7d3b3 [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
Søren Gjesse832a6622023-10-12 13:51:24 +020016GOLEM_BUILD_TARGETS_TESTS = [
17 utils.GRADLE_TASK_ALL_TESTS_WITH_APPLY_MAPPING_JAR,
Rico Wind9365e942024-03-27 12:58:05 +010018 utils.GRADLE_TASK_TESTBASE_WITH_APPLY_MAPPING_JAR,
Søren Gjesse832a6622023-10-12 13:51:24 +020019 utils.GRADLE_TASK_TEST_DEPS_JAR
20]
Christoffer Adamsen1659b422024-08-16 13:53:34 +020021GOLEM_BUILD_TARGETS = [
22 utils.GRADLE_TASK_R8LIB, utils.GRADLE_TASK_KEEP_ANNO_JAR
23] + GOLEM_BUILD_TARGETS_TESTS
Søren Gjesse832a6622023-10-12 13:51:24 +020024
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020025
Ian Zernyd5061b32022-02-02 13:24:30 +010026def get_golem_resource_path(benchmark):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020027 return os.path.join('benchmarks', benchmark)
28
Ian Zernyd5061b32022-02-02 13:24:30 +010029
Ian Zerny161ff742022-01-20 12:39:40 +010030def get_jdk_home(options, benchmark):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020031 if options.golem:
32 return os.path.join(get_golem_resource_path(benchmark), 'linux')
33 return None
34
Ian Zerny161ff742022-01-20 12:39:40 +010035
36def parse_options(argv):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020037 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 Adamsen1659b422024-08-16 13:53:34 +020051 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 Adamsen2434a4d2023-10-16 11:29:03 +020058 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 Adamsenbc1d4ef2024-06-04 14:51:02 +020075 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 Adamsen2434a4d2023-10-16 11:29:03 +020081 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 Adamsenc748e522024-06-12 18:48:45 +020090 result.add_argument(
91 '--version-jar',
92 help='The r8.jar corresponding to the version given at --version.',
93 default=None)
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +020094 result.add_argument('--temp',
95 help='A directory to use for temporaries and outputs.',
96 default=None)
Christoffer Adamsen8c13d042024-06-12 13:08:06 +020097 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 Adamsend064d302024-06-12 13:08:30 +0200103 # 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 Adamsen8c13d042024-06-12 13:08:06 +0200107 return options, args
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200108
Ian Zerny161ff742022-01-20 12:39:40 +0100109
Ian Zerny171c6a62022-04-04 14:25:30 +0200110def main(argv, temp):
Christoffer Adamsen8c13d042024-06-12 13:08:06 +0200111 options, args = parse_options(argv)
Ian Zerny161ff742022-01-20 12:39:40 +0100112
Christoffer Adamsenbc1d4ef2024-06-04 14:51:02 +0200113 if options.output:
114 options.output = os.path.abspath(options.output)
115
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200116 if options.temp:
117 temp = options.temp
Christoffer Adamsenbc1d4ef2024-06-04 14:51:02 +0200118 os.makedirs(temp, exist_ok=True)
Ian Zerny171c6a62022-04-04 14:25:30 +0200119
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200120 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 Zerny161ff742022-01-20 12:39:40 +0100126 if options.nolib:
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200127 testBuildTargets = [
Christoffer Adamsen9daa13b2024-06-12 13:07:58 +0200128 utils.GRADLE_TASK_TEST_JAR, utils.GRADLE_TASK_TEST_DEPS_JAR,
Christoffer Adamsend58f0472024-06-04 14:28:12 +0200129 utils.GRADLE_TASK_TEST_UNZIP_TESTBASE
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200130 ]
Christoffer Adamsen1659b422024-08-16 13:53:34 +0200131 buildTargets = [utils.GRADLE_TASK_R8, utils.GRADLE_TASK_KEEP_ANNO_JAR
132 ] + testBuildTargets
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200133 r8jar = utils.R8_JAR
Christoffer Adamsen9daa13b2024-06-12 13:07:58 +0200134 testjars = [
135 utils.R8_TESTS_JAR, utils.R8_TESTS_DEPS_JAR, utils.R8_TESTBASE_JAR
136 ]
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200137 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 Wind9365e942024-03-27 12:58:05 +0100143 os.path.join(utils.R8LIB_TESTS_DEPS_JAR),
144 os.path.join(utils.R8LIB_TESTBASE_JAR)
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200145 ]
Ian Zerny161ff742022-01-20 12:39:40 +0100146
Christoffer Adamsenc748e522024-06-12 18:48:45 +0200147 if options.version or options.version_jar:
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200148 # r8 is downloaded so only test jar needs to be built.
149 buildTargets = testBuildTargets
Christoffer Adamsenc748e522024-06-12 18:48:45 +0200150 r8jar = options.version_jar or compiledump.download_distribution(
151 options.version, options, temp)
Ian Zerny161ff742022-01-20 12:39:40 +0100152
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200153 if not options.no_build:
154 gradle.RunGradle(buildTargets + ['-Pno_internal'])
Ian Zerny171c6a62022-04-04 14:25:30 +0200155
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200156 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 Zerny161ff742022-01-20 12:39:40 +0100163
Ian Zerny161ff742022-01-20 12:39:40 +0100164
165def run(options, r8jar, testjars):
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200166 jdkhome = get_jdk_home(options, options.benchmark)
Christoffer Adamsen8c13d042024-06-12 13:08:06 +0200167 cmd = [
168 jdk.GetJavaExecutable(jdkhome), '-Xms8g', '-Xmx8g',
Christoffer Adamsen39c00c22024-06-12 18:48:27 +0200169 '-XX:+TieredCompilation', '-XX:TieredStopAtLevel=4',
Christoffer Adamsen1659b422024-08-16 13:53:34 +0200170 '-DBENCHMARK_IGNORE_CODE_SIZE_DIFFERENCES',
Christoffer Adamsene1fe9b62024-08-29 16:00:36 +0200171 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 Adamsen8c13d042024-06-12 13:08:06 +0200174 ]
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200175 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 Zerny1dd09cb2024-04-22 07:38:58 +0200181 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 Adamsen2434a4d2023-10-16 11:29:03 +0200183 ])
Christoffer Adamsen8c13d042024-06-12 13:08:06 +0200184 if options.iterations is not None:
185 if options.iterations == 0:
186 return
Christoffer Adamsenbc1d4ef2024-06-04 14:51:02 +0200187 cmd.append(f'-DBENCHMARK_ITERATIONS={options.iterations}')
188 if options.output:
189 cmd.append(f'-DBENCHMARK_OUTPUT={options.output}')
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200190 cmd.extend(['-cp', ':'.join([r8jar] + testjars)])
Christoffer Adamsen1659b422024-08-16 13:53:34 +0200191 if options.debug_agent:
192 cmd.append(
193 '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005'
194 )
Søren Gjessed54faae2023-10-12 09:07:54 +0200195 cmd.extend([
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200196 '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 Zerny161ff742022-01-20 12:39:40 +0100202 ])
Christoffer Adamsen8c13d042024-06-12 13:08:06 +0200203 utils.PrintCmd(cmd, quiet=options.quiet)
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200204 return subprocess.check_call(cmd)
205
Ian Zerny161ff742022-01-20 12:39:40 +0100206
207if __name__ == '__main__':
Christoffer Quist Adamsen2434a4d2023-10-16 11:29:03 +0200208 with utils.TempDir() as temp:
209 sys.exit(main(sys.argv[1:], temp))