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