Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 1 | # Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file |
| 2 | # for details. All rights reserved. Use of this source code is governed by a |
| 3 | # BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | # Different utility functions used accross scripts |
| 6 | |
| 7 | import hashlib |
| 8 | import os |
Tamas Kenez | 82efeb5 | 2017-06-12 13:56:22 +0200 | [diff] [blame] | 9 | import re |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 10 | import shutil |
| 11 | import subprocess |
| 12 | import sys |
Jean-Marie Henaff | 7a64eec | 2018-05-31 15:30:35 +0200 | [diff] [blame] | 13 | import tarfile |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 14 | import tempfile |
| 15 | |
Rico Wind | 9d70f61 | 2018-08-31 09:17:43 +0200 | [diff] [blame] | 16 | ANDROID_JAR = 'third_party/android_jar/lib-v{api}/android.jar' |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 17 | TOOLS_DIR = os.path.abspath(os.path.normpath(os.path.join(__file__, '..'))) |
| 18 | REPO_ROOT = os.path.realpath(os.path.join(TOOLS_DIR, '..')) |
Rico Wind | 6238f22 | 2018-10-03 10:36:10 +0200 | [diff] [blame] | 19 | THIRD_PARTY = os.path.join(REPO_ROOT, 'third_party') |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 20 | MEMORY_USE_TMP_FILE = 'memory_use.tmp' |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 21 | DEX_SEGMENTS_RESULT_PATTERN = re.compile('- ([^:]+): ([0-9]+)') |
Mads Ager | 12a56bc | 2017-11-27 11:51:25 +0100 | [diff] [blame] | 22 | BUILD = os.path.join(REPO_ROOT, 'build') |
Ian Zerny | 0f5fc73 | 2018-11-15 14:34:41 +0100 | [diff] [blame] | 23 | BUILD_DEPS_DIR = os.path.join(BUILD, 'deps') |
| 24 | BUILD_MAIN_DIR = os.path.join(BUILD, 'classes', 'main') |
| 25 | BUILD_TEST_DIR = os.path.join(BUILD, 'classes', 'test') |
Mads Ager | 12a56bc | 2017-11-27 11:51:25 +0100 | [diff] [blame] | 26 | LIBS = os.path.join(BUILD, 'libs') |
| 27 | GENERATED_LICENSE_DIR = os.path.join(BUILD, 'generatedLicense') |
Mads Ager | a4911eb | 2017-11-22 13:19:36 +0100 | [diff] [blame] | 28 | SRC_ROOT = os.path.join(REPO_ROOT, 'src', 'main', 'java') |
Søren Gjesse | dc9d8a2 | 2017-10-12 12:40:59 +0200 | [diff] [blame] | 29 | |
| 30 | D8 = 'd8' |
| 31 | R8 = 'r8' |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 32 | R8LIB = 'r8lib' |
Morten Krogh-Jespersen | e28db46 | 2019-01-09 13:32:15 +0100 | [diff] [blame^] | 33 | R8LIB_NO_DEPS = 'r8LibNoDeps' |
Mads Ager | b10c07f | 2017-11-27 13:25:52 +0100 | [diff] [blame] | 34 | R8_SRC = 'sourceJar' |
Søren Gjesse | dc9d8a2 | 2017-10-12 12:40:59 +0200 | [diff] [blame] | 35 | COMPATDX = 'compatdx' |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 36 | COMPATDXLIB = 'compatdxlib' |
Søren Gjesse | dc9d8a2 | 2017-10-12 12:40:59 +0200 | [diff] [blame] | 37 | COMPATPROGUARD = 'compatproguard' |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 38 | COMPATPROGUARDLIB = 'compatproguardlib' |
Søren Gjesse | dc9d8a2 | 2017-10-12 12:40:59 +0200 | [diff] [blame] | 39 | |
Rico Wind | 74fab30 | 2017-10-02 07:25:33 +0200 | [diff] [blame] | 40 | D8_JAR = os.path.join(LIBS, 'd8.jar') |
| 41 | R8_JAR = os.path.join(LIBS, 'r8.jar') |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 42 | R8LIB_JAR = os.path.join(LIBS, 'r8lib.jar') |
Mads Ager | b10c07f | 2017-11-27 13:25:52 +0100 | [diff] [blame] | 43 | R8_SRC_JAR = os.path.join(LIBS, 'r8-src.jar') |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 44 | R8LIB_EXCLUDE_DEPS_JAR = os.path.join(LIBS, 'r8lib-exclude-deps.jar') |
Tamas Kenez | 180be09 | 2018-12-05 15:23:06 +0100 | [diff] [blame] | 45 | R8_FULL_EXCLUDE_DEPS_JAR = os.path.join(LIBS, 'r8-full-exclude-deps.jar') |
Rico Wind | 74fab30 | 2017-10-02 07:25:33 +0200 | [diff] [blame] | 46 | COMPATDX_JAR = os.path.join(LIBS, 'compatdx.jar') |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 47 | COMPATDXLIB_JAR = os.path.join(LIBS, 'compatdxlib.jar') |
Søren Gjesse | dc9d8a2 | 2017-10-12 12:40:59 +0200 | [diff] [blame] | 48 | COMPATPROGUARD_JAR = os.path.join(LIBS, 'compatproguard.jar') |
Tamas Kenez | 03ab76f | 2018-12-07 14:33:25 +0100 | [diff] [blame] | 49 | COMPATPROGUARDLIB_JAR = os.path.join(LIBS, 'compatproguardlib.jar') |
Mads Ager | 12a56bc | 2017-11-27 11:51:25 +0100 | [diff] [blame] | 50 | MAVEN_ZIP = os.path.join(LIBS, 'r8.zip') |
| 51 | GENERATED_LICENSE = os.path.join(GENERATED_LICENSE_DIR, 'LICENSE') |
Mathias Rav | 3fb4a3a | 2018-05-29 15:41:36 +0200 | [diff] [blame] | 52 | RT_JAR = os.path.join(REPO_ROOT, 'third_party/openjdk/openjdk-rt-1.8/rt.jar') |
Mathias Rav | b46dc00 | 2018-06-06 09:37:11 +0200 | [diff] [blame] | 53 | R8LIB_KEEP_RULES = os.path.join(REPO_ROOT, 'src/main/keep.txt') |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 54 | |
| 55 | def PrintCmd(s): |
| 56 | if type(s) is list: |
| 57 | s = ' '.join(s) |
| 58 | print 'Running: %s' % s |
| 59 | # I know this will hit os on windows eventually if we don't do this. |
| 60 | sys.stdout.flush() |
| 61 | |
Rico Wind | f80f5a2 | 2017-06-16 09:15:57 +0200 | [diff] [blame] | 62 | def IsWindows(): |
| 63 | return os.name == 'nt' |
| 64 | |
Jean-Marie Henaff | e4e36d1 | 2018-04-05 10:33:50 +0200 | [diff] [blame] | 65 | def DownloadFromX20(sha1_file): |
| 66 | download_script = os.path.join(REPO_ROOT, 'tools', 'download_from_x20.py') |
| 67 | cmd = [download_script, sha1_file] |
| 68 | PrintCmd(cmd) |
| 69 | subprocess.check_call(cmd) |
| 70 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 71 | def DownloadFromGoogleCloudStorage(sha1_file, bucket='r8-deps'): |
Rico Wind | f80f5a2 | 2017-06-16 09:15:57 +0200 | [diff] [blame] | 72 | suffix = '.bat' if IsWindows() else '' |
| 73 | download_script = 'download_from_google_storage%s' % suffix |
| 74 | cmd = [download_script, '-n', '-b', bucket, '-u', '-s', |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 75 | sha1_file] |
| 76 | PrintCmd(cmd) |
| 77 | subprocess.check_call(cmd) |
| 78 | |
| 79 | def get_sha1(filename): |
| 80 | sha1 = hashlib.sha1() |
| 81 | with open(filename, 'rb') as f: |
| 82 | while True: |
| 83 | chunk = f.read(1024*1024) |
| 84 | if not chunk: |
| 85 | break |
| 86 | sha1.update(chunk) |
| 87 | return sha1.hexdigest() |
| 88 | |
Jean-Marie Henaff | 7a64eec | 2018-05-31 15:30:35 +0200 | [diff] [blame] | 89 | def get_HEAD_sha1(): |
| 90 | cmd = ['git', 'rev-parse', 'HEAD'] |
| 91 | PrintCmd(cmd) |
| 92 | with ChangedWorkingDirectory(REPO_ROOT): |
| 93 | return subprocess.check_output(cmd).strip() |
| 94 | |
Tamas Kenez | 971eec6 | 2017-05-24 11:08:40 +0200 | [diff] [blame] | 95 | def makedirs_if_needed(path): |
| 96 | try: |
| 97 | os.makedirs(path) |
| 98 | except OSError: |
| 99 | if not os.path.isdir(path): |
| 100 | raise |
| 101 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 102 | def upload_dir_to_cloud_storage(directory, destination, is_html=False, public_read=True): |
Rico Wind | a94f01c | 2017-06-27 10:32:34 +0200 | [diff] [blame] | 103 | # Upload and make the content encoding right for viewing directly |
Rico Wind | d0d88cf | 2018-02-09 09:46:11 +0100 | [diff] [blame] | 104 | cmd = ['gsutil.py', 'cp'] |
| 105 | if is_html: |
| 106 | cmd += ['-z', 'html'] |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 107 | if public_read: |
| 108 | cmd += ['-a', 'public-read'] |
| 109 | cmd += ['-R', directory, destination] |
Rico Wind | a94f01c | 2017-06-27 10:32:34 +0200 | [diff] [blame] | 110 | PrintCmd(cmd) |
| 111 | subprocess.check_call(cmd) |
| 112 | |
Rico Wind | 800fd71 | 2018-09-24 11:29:33 +0200 | [diff] [blame] | 113 | def upload_file_to_cloud_storage(source, destination, public_read=True): |
| 114 | cmd = ['gsutil.py', 'cp'] |
| 115 | if public_read: |
| 116 | cmd += ['-a', 'public-read'] |
| 117 | cmd += [source, destination] |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 118 | PrintCmd(cmd) |
| 119 | subprocess.check_call(cmd) |
| 120 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 121 | def delete_file_from_cloud_storage(destination): |
| 122 | cmd = ['gsutil.py', 'rm', destination] |
| 123 | PrintCmd(cmd) |
| 124 | subprocess.check_call(cmd) |
| 125 | |
Rico Wind | 4fd2dda | 2018-09-26 17:41:45 +0200 | [diff] [blame] | 126 | def ls_files_on_cloud_storage(destination): |
| 127 | cmd = ['gsutil.py', 'ls', destination] |
| 128 | PrintCmd(cmd) |
| 129 | return subprocess.check_output(cmd) |
| 130 | |
Rico Wind | 139eece | 2018-09-25 09:42:09 +0200 | [diff] [blame] | 131 | def cat_file_on_cloud_storage(destination, ignore_errors=False): |
| 132 | cmd = ['gsutil.py', 'cat', destination] |
| 133 | PrintCmd(cmd) |
| 134 | try: |
| 135 | return subprocess.check_output(cmd) |
| 136 | except subprocess.CalledProcessError as e: |
| 137 | if ignore_errors: |
| 138 | return '' |
| 139 | else: |
| 140 | raise e |
| 141 | |
| 142 | def file_exists_on_cloud_storage(destination): |
| 143 | cmd = ['gsutil.py', 'ls', destination] |
| 144 | PrintCmd(cmd) |
| 145 | return subprocess.call(cmd) == 0 |
| 146 | |
Jean-Marie Henaff | 7a64eec | 2018-05-31 15:30:35 +0200 | [diff] [blame] | 147 | def download_file_from_cloud_storage(source, destination): |
| 148 | cmd = ['gsutil.py', 'cp', source, destination] |
| 149 | PrintCmd(cmd) |
| 150 | subprocess.check_call(cmd) |
| 151 | |
| 152 | def create_archive(name): |
| 153 | tarname = '%s.tar.gz' % name |
| 154 | with tarfile.open(tarname, 'w:gz') as tar: |
| 155 | tar.add(name) |
| 156 | return tarname |
| 157 | |
| 158 | def extract_dir(filename): |
| 159 | return filename[0:len(filename) - len('.tar.gz')] |
| 160 | |
| 161 | def unpack_archive(filename): |
| 162 | dest_dir = extract_dir(filename) |
| 163 | if os.path.exists(dest_dir): |
| 164 | print 'Deleting existing dir %s' % dest_dir |
| 165 | shutil.rmtree(dest_dir) |
| 166 | dirname = os.path.dirname(os.path.abspath(filename)) |
| 167 | with tarfile.open(filename, 'r:gz') as tar: |
| 168 | tar.extractall(path=dirname) |
| 169 | |
Rico Wind | 1a29c4f | 2018-01-25 08:43:08 +0100 | [diff] [blame] | 170 | # Note that gcs is eventually consistent with regards to list operations. |
| 171 | # This is not a problem in our case, but don't ever use this method |
| 172 | # for synchronization. |
| 173 | def cloud_storage_exists(destination): |
| 174 | cmd = ['gsutil.py', 'ls', destination] |
| 175 | PrintCmd(cmd) |
| 176 | exit_code = subprocess.call(cmd) |
| 177 | return exit_code == 0 |
| 178 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 179 | class TempDir(object): |
| 180 | def __init__(self, prefix=''): |
| 181 | self._temp_dir = None |
| 182 | self._prefix = prefix |
| 183 | |
| 184 | def __enter__(self): |
| 185 | self._temp_dir = tempfile.mkdtemp(self._prefix) |
| 186 | return self._temp_dir |
| 187 | |
| 188 | def __exit__(self, *_): |
| 189 | shutil.rmtree(self._temp_dir, ignore_errors=True) |
| 190 | |
| 191 | class ChangedWorkingDirectory(object): |
| 192 | def __init__(self, working_directory): |
| 193 | self._working_directory = working_directory |
| 194 | |
| 195 | def __enter__(self): |
| 196 | self._old_cwd = os.getcwd() |
Rico Wind | f80f5a2 | 2017-06-16 09:15:57 +0200 | [diff] [blame] | 197 | print 'Enter directory = ', self._working_directory |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 198 | os.chdir(self._working_directory) |
| 199 | |
| 200 | def __exit__(self, *_): |
Rico Wind | f80f5a2 | 2017-06-16 09:15:57 +0200 | [diff] [blame] | 201 | print 'Enter directory = ', self._old_cwd |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 202 | os.chdir(self._old_cwd) |
Tamas Kenez | 82efeb5 | 2017-06-12 13:56:22 +0200 | [diff] [blame] | 203 | |
| 204 | # Reading Android CTS test_result.xml |
| 205 | |
| 206 | class CtsModule(object): |
| 207 | def __init__(self, module_name): |
| 208 | self.name = module_name |
| 209 | |
| 210 | class CtsTestCase(object): |
| 211 | def __init__(self, test_case_name): |
| 212 | self.name = test_case_name |
| 213 | |
| 214 | class CtsTest(object): |
| 215 | def __init__(self, test_name, outcome): |
| 216 | self.name = test_name |
| 217 | self.outcome = outcome |
| 218 | |
| 219 | # Generator yielding CtsModule, CtsTestCase or CtsTest from |
| 220 | # reading through a CTS test_result.xml file. |
| 221 | def read_cts_test_result(file_xml): |
| 222 | re_module = re.compile('<Module name="([^"]*)"') |
| 223 | re_test_case = re.compile('<TestCase name="([^"]*)"') |
| 224 | re_test = re.compile('<Test result="(pass|fail)" name="([^"]*)"') |
| 225 | with open(file_xml) as f: |
| 226 | for line in f: |
| 227 | m = re_module.search(line) |
| 228 | if m: |
| 229 | yield CtsModule(m.groups()[0]) |
| 230 | continue |
| 231 | m = re_test_case.search(line) |
| 232 | if m: |
| 233 | yield CtsTestCase(m.groups()[0]) |
| 234 | continue |
| 235 | m = re_test.search(line) |
| 236 | if m: |
| 237 | outcome = m.groups()[0] |
Rico Wind | f80f5a2 | 2017-06-16 09:15:57 +0200 | [diff] [blame] | 238 | assert outcome in ['fail', 'pass'] |
Tamas Kenez | 82efeb5 | 2017-06-12 13:56:22 +0200 | [diff] [blame] | 239 | yield CtsTest(m.groups()[1], outcome == 'pass') |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 240 | |
| 241 | def grep_memoryuse(logfile): |
| 242 | re_vmhwm = re.compile('^VmHWM:[ \t]*([0-9]+)[ \t]*([a-zA-Z]*)') |
| 243 | result = None |
| 244 | with open(logfile) as f: |
| 245 | for line in f: |
| 246 | m = re_vmhwm.search(line) |
| 247 | if m: |
| 248 | groups = m.groups() |
| 249 | s = len(groups) |
| 250 | if s >= 1: |
| 251 | result = int(groups[0]) |
| 252 | if s >= 2: |
| 253 | unit = groups[1] |
| 254 | if unit == 'kB': |
| 255 | result *= 1024 |
| 256 | elif unit != '': |
| 257 | raise Exception('Unrecognized unit in memory usage log: {}' |
| 258 | .format(unit)) |
| 259 | if result is None: |
| 260 | raise Exception('No memory usage found in log: {}'.format(logfile)) |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 261 | return result |
| 262 | |
| 263 | # Return a dictionary: {segment_name -> segments_size} |
| 264 | def getDexSegmentSizes(dex_files): |
| 265 | assert len(dex_files) > 0 |
Mathias Rav | 9cf6a74 | 2018-05-22 09:34:45 +0200 | [diff] [blame] | 266 | cmd = ['java', '-jar', R8_JAR, 'dexsegments'] |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 267 | cmd.extend(dex_files) |
| 268 | PrintCmd(cmd) |
| 269 | output = subprocess.check_output(cmd) |
| 270 | |
| 271 | matches = DEX_SEGMENTS_RESULT_PATTERN.findall(output) |
| 272 | |
| 273 | if matches is None or len(matches) == 0: |
| 274 | raise Exception('DexSegments failed to return any output for' \ |
| 275 | ' these files: {}'.format(dex_files)) |
| 276 | |
| 277 | result = {} |
| 278 | |
| 279 | for match in matches: |
| 280 | result[match[0]] = int(match[1]) |
| 281 | |
| 282 | return result |
| 283 | |
Rico Wind | c0b1638 | 2018-05-17 13:23:43 +0200 | [diff] [blame] | 284 | def get_maven_path(version): |
| 285 | return os.path.join('com', 'android', 'tools', 'r8', version) |
| 286 | |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 287 | def print_dexsegments(prefix, dex_files): |
| 288 | for segment_name, size in getDexSegmentSizes(dex_files).items(): |
| 289 | print('{}-{}(CodeSize): {}' |
| 290 | .format(prefix, segment_name, size)) |
Tamas Kenez | 2cf47cf | 2017-07-25 10:22:52 +0200 | [diff] [blame] | 291 | |
Mads Ager | bc7b2ce | 2018-02-05 11:28:47 +0100 | [diff] [blame] | 292 | # Ensure that we are not benchmarking with a google jvm. |
Tamas Kenez | 2cf47cf | 2017-07-25 10:22:52 +0200 | [diff] [blame] | 293 | def check_java_version(): |
| 294 | cmd= ['java', '-version'] |
| 295 | output = subprocess.check_output(cmd, stderr = subprocess.STDOUT) |
| 296 | m = re.search('openjdk version "([^"]*)"', output) |
| 297 | if m is None: |
| 298 | raise Exception("Can't check java version: no version string in output" |
| 299 | " of 'java -version': '{}'".format(output)) |
| 300 | version = m.groups(0)[0] |
Mads Ager | bc7b2ce | 2018-02-05 11:28:47 +0100 | [diff] [blame] | 301 | m = re.search('google', version) |
| 302 | if m is not None: |
| 303 | raise Exception("Do not use google JVM for benchmarking: " + version) |
Tamas Kenez | 0cad51c | 2017-08-21 14:42:01 +0200 | [diff] [blame] | 304 | |
Rico Wind | 9d70f61 | 2018-08-31 09:17:43 +0200 | [diff] [blame] | 305 | def get_android_jar(api): |
| 306 | return os.path.join(REPO_ROOT, ANDROID_JAR.format(api=api)) |
Rico Wind | da6836e | 2018-12-07 12:32:03 +0100 | [diff] [blame] | 307 | |
| 308 | def is_bot(): |
| 309 | return 'BUILDBOT_BUILDERNAME' in os.environ |