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 |
| 13 | import tempfile |
| 14 | |
| 15 | TOOLS_DIR = os.path.abspath(os.path.normpath(os.path.join(__file__, '..'))) |
| 16 | REPO_ROOT = os.path.realpath(os.path.join(TOOLS_DIR, '..')) |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 17 | MEMORY_USE_TMP_FILE = 'memory_use.tmp' |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 18 | DEX_SEGMENTS_JAR = os.path.join(REPO_ROOT, 'build', 'libs', |
| 19 | 'dexsegments.jar') |
| 20 | DEX_SEGMENTS_RESULT_PATTERN = re.compile('- ([^:]+): ([0-9]+)') |
Mads Ager | 12a56bc | 2017-11-27 11:51:25 +0100 | [diff] [blame] | 21 | BUILD = os.path.join(REPO_ROOT, 'build') |
| 22 | LIBS = os.path.join(BUILD, 'libs') |
| 23 | GENERATED_LICENSE_DIR = os.path.join(BUILD, 'generatedLicense') |
Mads Ager | a4911eb | 2017-11-22 13:19:36 +0100 | [diff] [blame] | 24 | SRC_ROOT = os.path.join(REPO_ROOT, 'src', 'main', 'java') |
Søren Gjesse | dc9d8a2 | 2017-10-12 12:40:59 +0200 | [diff] [blame] | 25 | |
| 26 | D8 = 'd8' |
| 27 | R8 = 'r8' |
Mads Ager | b10c07f | 2017-11-27 13:25:52 +0100 | [diff] [blame] | 28 | R8_SRC = 'sourceJar' |
Søren Gjesse | dc9d8a2 | 2017-10-12 12:40:59 +0200 | [diff] [blame] | 29 | COMPATDX = 'compatdx' |
| 30 | COMPATPROGUARD = 'compatproguard' |
| 31 | |
Rico Wind | 74fab30 | 2017-10-02 07:25:33 +0200 | [diff] [blame] | 32 | D8_JAR = os.path.join(LIBS, 'd8.jar') |
| 33 | R8_JAR = os.path.join(LIBS, 'r8.jar') |
Mads Ager | b10c07f | 2017-11-27 13:25:52 +0100 | [diff] [blame] | 34 | R8_SRC_JAR = os.path.join(LIBS, 'r8-src.jar') |
Mads Ager | 0bd1ebd | 2017-11-22 13:40:21 +0100 | [diff] [blame] | 35 | R8_EXCLUDE_DEPS_JAR = os.path.join(LIBS, 'r8-exclude-deps.jar') |
Rico Wind | 74fab30 | 2017-10-02 07:25:33 +0200 | [diff] [blame] | 36 | COMPATDX_JAR = os.path.join(LIBS, 'compatdx.jar') |
Søren Gjesse | dc9d8a2 | 2017-10-12 12:40:59 +0200 | [diff] [blame] | 37 | COMPATPROGUARD_JAR = os.path.join(LIBS, 'compatproguard.jar') |
Mads Ager | 12a56bc | 2017-11-27 11:51:25 +0100 | [diff] [blame] | 38 | MAVEN_ZIP = os.path.join(LIBS, 'r8.zip') |
| 39 | GENERATED_LICENSE = os.path.join(GENERATED_LICENSE_DIR, 'LICENSE') |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 40 | |
| 41 | def PrintCmd(s): |
| 42 | if type(s) is list: |
| 43 | s = ' '.join(s) |
| 44 | print 'Running: %s' % s |
| 45 | # I know this will hit os on windows eventually if we don't do this. |
| 46 | sys.stdout.flush() |
| 47 | |
Rico Wind | f80f5a2 | 2017-06-16 09:15:57 +0200 | [diff] [blame] | 48 | def IsWindows(): |
| 49 | return os.name == 'nt' |
| 50 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 51 | def DownloadFromGoogleCloudStorage(sha1_file, bucket='r8-deps'): |
Rico Wind | f80f5a2 | 2017-06-16 09:15:57 +0200 | [diff] [blame] | 52 | suffix = '.bat' if IsWindows() else '' |
| 53 | download_script = 'download_from_google_storage%s' % suffix |
| 54 | cmd = [download_script, '-n', '-b', bucket, '-u', '-s', |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 55 | sha1_file] |
| 56 | PrintCmd(cmd) |
| 57 | subprocess.check_call(cmd) |
| 58 | |
| 59 | def get_sha1(filename): |
| 60 | sha1 = hashlib.sha1() |
| 61 | with open(filename, 'rb') as f: |
| 62 | while True: |
| 63 | chunk = f.read(1024*1024) |
| 64 | if not chunk: |
| 65 | break |
| 66 | sha1.update(chunk) |
| 67 | return sha1.hexdigest() |
| 68 | |
Tamas Kenez | 971eec6 | 2017-05-24 11:08:40 +0200 | [diff] [blame] | 69 | def makedirs_if_needed(path): |
| 70 | try: |
| 71 | os.makedirs(path) |
| 72 | except OSError: |
| 73 | if not os.path.isdir(path): |
| 74 | raise |
| 75 | |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 76 | def upload_dir_to_cloud_storage(directory, destination): |
Rico Wind | a94f01c | 2017-06-27 10:32:34 +0200 | [diff] [blame] | 77 | # Upload and make the content encoding right for viewing directly |
Rico Wind | dce9c87 | 2017-08-14 14:49:04 +0200 | [diff] [blame] | 78 | cmd = ['gsutil.py', 'cp', '-z', 'html', '-a', |
Rico Wind | a94f01c | 2017-06-27 10:32:34 +0200 | [diff] [blame] | 79 | 'public-read', '-R', directory, destination] |
| 80 | PrintCmd(cmd) |
| 81 | subprocess.check_call(cmd) |
| 82 | |
Rico Wind | b4621c1 | 2017-08-28 12:48:53 +0200 | [diff] [blame] | 83 | def upload_file_to_cloud_storage(source, destination): |
| 84 | cmd = ['gsutil.py', 'cp', '-a', 'public-read', source, destination] |
| 85 | PrintCmd(cmd) |
| 86 | subprocess.check_call(cmd) |
| 87 | |
Rico Wind | 1a29c4f | 2018-01-25 08:43:08 +0100 | [diff] [blame] | 88 | # Note that gcs is eventually consistent with regards to list operations. |
| 89 | # This is not a problem in our case, but don't ever use this method |
| 90 | # for synchronization. |
| 91 | def cloud_storage_exists(destination): |
| 92 | cmd = ['gsutil.py', 'ls', destination] |
| 93 | PrintCmd(cmd) |
| 94 | exit_code = subprocess.call(cmd) |
| 95 | return exit_code == 0 |
| 96 | |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 97 | class TempDir(object): |
| 98 | def __init__(self, prefix=''): |
| 99 | self._temp_dir = None |
| 100 | self._prefix = prefix |
| 101 | |
| 102 | def __enter__(self): |
| 103 | self._temp_dir = tempfile.mkdtemp(self._prefix) |
| 104 | return self._temp_dir |
| 105 | |
| 106 | def __exit__(self, *_): |
| 107 | shutil.rmtree(self._temp_dir, ignore_errors=True) |
| 108 | |
| 109 | class ChangedWorkingDirectory(object): |
| 110 | def __init__(self, working_directory): |
| 111 | self._working_directory = working_directory |
| 112 | |
| 113 | def __enter__(self): |
| 114 | self._old_cwd = os.getcwd() |
Rico Wind | f80f5a2 | 2017-06-16 09:15:57 +0200 | [diff] [blame] | 115 | print 'Enter directory = ', self._working_directory |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 116 | os.chdir(self._working_directory) |
| 117 | |
| 118 | def __exit__(self, *_): |
Rico Wind | f80f5a2 | 2017-06-16 09:15:57 +0200 | [diff] [blame] | 119 | print 'Enter directory = ', self._old_cwd |
Mads Ager | 418d1ca | 2017-05-22 09:35:49 +0200 | [diff] [blame] | 120 | os.chdir(self._old_cwd) |
Tamas Kenez | 82efeb5 | 2017-06-12 13:56:22 +0200 | [diff] [blame] | 121 | |
| 122 | # Reading Android CTS test_result.xml |
| 123 | |
| 124 | class CtsModule(object): |
| 125 | def __init__(self, module_name): |
| 126 | self.name = module_name |
| 127 | |
| 128 | class CtsTestCase(object): |
| 129 | def __init__(self, test_case_name): |
| 130 | self.name = test_case_name |
| 131 | |
| 132 | class CtsTest(object): |
| 133 | def __init__(self, test_name, outcome): |
| 134 | self.name = test_name |
| 135 | self.outcome = outcome |
| 136 | |
| 137 | # Generator yielding CtsModule, CtsTestCase or CtsTest from |
| 138 | # reading through a CTS test_result.xml file. |
| 139 | def read_cts_test_result(file_xml): |
| 140 | re_module = re.compile('<Module name="([^"]*)"') |
| 141 | re_test_case = re.compile('<TestCase name="([^"]*)"') |
| 142 | re_test = re.compile('<Test result="(pass|fail)" name="([^"]*)"') |
| 143 | with open(file_xml) as f: |
| 144 | for line in f: |
| 145 | m = re_module.search(line) |
| 146 | if m: |
| 147 | yield CtsModule(m.groups()[0]) |
| 148 | continue |
| 149 | m = re_test_case.search(line) |
| 150 | if m: |
| 151 | yield CtsTestCase(m.groups()[0]) |
| 152 | continue |
| 153 | m = re_test.search(line) |
| 154 | if m: |
| 155 | outcome = m.groups()[0] |
Rico Wind | f80f5a2 | 2017-06-16 09:15:57 +0200 | [diff] [blame] | 156 | assert outcome in ['fail', 'pass'] |
Tamas Kenez | 82efeb5 | 2017-06-12 13:56:22 +0200 | [diff] [blame] | 157 | yield CtsTest(m.groups()[1], outcome == 'pass') |
Tamas Kenez | fc34cd8 | 2017-07-13 12:43:57 +0200 | [diff] [blame] | 158 | |
| 159 | def grep_memoryuse(logfile): |
| 160 | re_vmhwm = re.compile('^VmHWM:[ \t]*([0-9]+)[ \t]*([a-zA-Z]*)') |
| 161 | result = None |
| 162 | with open(logfile) as f: |
| 163 | for line in f: |
| 164 | m = re_vmhwm.search(line) |
| 165 | if m: |
| 166 | groups = m.groups() |
| 167 | s = len(groups) |
| 168 | if s >= 1: |
| 169 | result = int(groups[0]) |
| 170 | if s >= 2: |
| 171 | unit = groups[1] |
| 172 | if unit == 'kB': |
| 173 | result *= 1024 |
| 174 | elif unit != '': |
| 175 | raise Exception('Unrecognized unit in memory usage log: {}' |
| 176 | .format(unit)) |
| 177 | if result is None: |
| 178 | raise Exception('No memory usage found in log: {}'.format(logfile)) |
Tamas Kenez | 02bff03 | 2017-07-18 12:13:58 +0200 | [diff] [blame] | 179 | return result |
| 180 | |
| 181 | # Return a dictionary: {segment_name -> segments_size} |
| 182 | def getDexSegmentSizes(dex_files): |
| 183 | assert len(dex_files) > 0 |
| 184 | cmd = ['java', '-jar', DEX_SEGMENTS_JAR] |
| 185 | cmd.extend(dex_files) |
| 186 | PrintCmd(cmd) |
| 187 | output = subprocess.check_output(cmd) |
| 188 | |
| 189 | matches = DEX_SEGMENTS_RESULT_PATTERN.findall(output) |
| 190 | |
| 191 | if matches is None or len(matches) == 0: |
| 192 | raise Exception('DexSegments failed to return any output for' \ |
| 193 | ' these files: {}'.format(dex_files)) |
| 194 | |
| 195 | result = {} |
| 196 | |
| 197 | for match in matches: |
| 198 | result[match[0]] = int(match[1]) |
| 199 | |
| 200 | return result |
| 201 | |
| 202 | def print_dexsegments(prefix, dex_files): |
| 203 | for segment_name, size in getDexSegmentSizes(dex_files).items(): |
| 204 | print('{}-{}(CodeSize): {}' |
| 205 | .format(prefix, segment_name, size)) |
Tamas Kenez | 2cf47cf | 2017-07-25 10:22:52 +0200 | [diff] [blame] | 206 | |
Mads Ager | bc7b2ce | 2018-02-05 11:28:47 +0100 | [diff] [blame^] | 207 | # Ensure that we are not benchmarking with a google jvm. |
Tamas Kenez | 2cf47cf | 2017-07-25 10:22:52 +0200 | [diff] [blame] | 208 | def check_java_version(): |
| 209 | cmd= ['java', '-version'] |
| 210 | output = subprocess.check_output(cmd, stderr = subprocess.STDOUT) |
| 211 | m = re.search('openjdk version "([^"]*)"', output) |
| 212 | if m is None: |
| 213 | raise Exception("Can't check java version: no version string in output" |
| 214 | " of 'java -version': '{}'".format(output)) |
| 215 | version = m.groups(0)[0] |
Mads Ager | bc7b2ce | 2018-02-05 11:28:47 +0100 | [diff] [blame^] | 216 | m = re.search('google', version) |
| 217 | if m is not None: |
| 218 | raise Exception("Do not use google JVM for benchmarking: " + version) |
Tamas Kenez | 0cad51c | 2017-08-21 14:42:01 +0200 | [diff] [blame] | 219 | |
| 220 | def verify_with_dex2oat(dex_file): |
| 221 | |
| 222 | # dex2oat accepts non-existent dex files, check here instead |
| 223 | if not os.path.exists(dex_file): |
| 224 | raise Exception('Dex file not found: "{}"'.format(dex_file)) |
| 225 | |
| 226 | android_root_dir = os.path.join(TOOLS_DIR, 'linux', 'art', 'product', |
| 227 | 'angler') |
| 228 | boot_art = os.path.join(android_root_dir, 'system', 'framework', 'boot.art') |
| 229 | dex2oat = os.path.join(TOOLS_DIR, 'linux', 'art', 'bin', 'dex2oat') |
| 230 | |
| 231 | with TempDir() as temp: |
| 232 | oat_file = os.path.join(temp, 'all.oat') |
| 233 | |
| 234 | cmd = [ |
| 235 | dex2oat, |
| 236 | '--android-root=' + android_root_dir, |
| 237 | '--runtime-arg', '-Xnorelocate', |
| 238 | '--boot-image=' + boot_art, |
| 239 | '--dex-file=' + dex_file, |
| 240 | '--oat-file=' + oat_file, |
| 241 | '--instruction-set=arm64', |
Sebastien Hertz | ae3a121 | 2017-09-08 10:01:17 +0200 | [diff] [blame] | 242 | '--compiler-filter=quicken' |
Tamas Kenez | 0cad51c | 2017-08-21 14:42:01 +0200 | [diff] [blame] | 243 | ] |
| 244 | |
| 245 | PrintCmd(cmd) |
| 246 | subprocess.check_call(cmd, |
| 247 | env = {"LD_LIBRARY_PATH": |
| 248 | os.path.join(TOOLS_DIR, 'linux', 'art', 'lib')} |
| 249 | ) |