blob: 47acca5b010b0602fcd7454b1e9d3624c1a93e6d [file] [log] [blame]
Mads Ager418d1ca2017-05-22 09:35:49 +02001# 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
7import hashlib
8import os
Tamas Kenez82efeb52017-06-12 13:56:22 +02009import re
Mads Ager418d1ca2017-05-22 09:35:49 +020010import shutil
11import subprocess
12import sys
13import tempfile
14
15TOOLS_DIR = os.path.abspath(os.path.normpath(os.path.join(__file__, '..')))
16REPO_ROOT = os.path.realpath(os.path.join(TOOLS_DIR, '..'))
Tamas Kenezfc34cd82017-07-13 12:43:57 +020017MEMORY_USE_TMP_FILE = 'memory_use.tmp'
Tamas Kenez02bff032017-07-18 12:13:58 +020018DEX_SEGMENTS_JAR = os.path.join(REPO_ROOT, 'build', 'libs',
19 'dexsegments.jar')
20DEX_SEGMENTS_RESULT_PATTERN = re.compile('- ([^:]+): ([0-9]+)')
Mads Ager12a56bc2017-11-27 11:51:25 +010021BUILD = os.path.join(REPO_ROOT, 'build')
22LIBS = os.path.join(BUILD, 'libs')
23GENERATED_LICENSE_DIR = os.path.join(BUILD, 'generatedLicense')
Mads Agera4911eb2017-11-22 13:19:36 +010024SRC_ROOT = os.path.join(REPO_ROOT, 'src', 'main', 'java')
Søren Gjessedc9d8a22017-10-12 12:40:59 +020025
26D8 = 'd8'
27R8 = 'r8'
Mads Agerb10c07f2017-11-27 13:25:52 +010028R8_SRC = 'sourceJar'
Søren Gjessedc9d8a22017-10-12 12:40:59 +020029COMPATDX = 'compatdx'
30COMPATPROGUARD = 'compatproguard'
31
Rico Wind74fab302017-10-02 07:25:33 +020032D8_JAR = os.path.join(LIBS, 'd8.jar')
33R8_JAR = os.path.join(LIBS, 'r8.jar')
Mads Agerb10c07f2017-11-27 13:25:52 +010034R8_SRC_JAR = os.path.join(LIBS, 'r8-src.jar')
Mads Ager0bd1ebd2017-11-22 13:40:21 +010035R8_EXCLUDE_DEPS_JAR = os.path.join(LIBS, 'r8-exclude-deps.jar')
Rico Wind74fab302017-10-02 07:25:33 +020036COMPATDX_JAR = os.path.join(LIBS, 'compatdx.jar')
Søren Gjessedc9d8a22017-10-12 12:40:59 +020037COMPATPROGUARD_JAR = os.path.join(LIBS, 'compatproguard.jar')
Mads Ager12a56bc2017-11-27 11:51:25 +010038MAVEN_ZIP = os.path.join(LIBS, 'r8.zip')
39GENERATED_LICENSE = os.path.join(GENERATED_LICENSE_DIR, 'LICENSE')
Mads Ager418d1ca2017-05-22 09:35:49 +020040
41def 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 Windf80f5a22017-06-16 09:15:57 +020048def IsWindows():
49 return os.name == 'nt'
50
Mads Ager418d1ca2017-05-22 09:35:49 +020051def DownloadFromGoogleCloudStorage(sha1_file, bucket='r8-deps'):
Rico Windf80f5a22017-06-16 09:15:57 +020052 suffix = '.bat' if IsWindows() else ''
53 download_script = 'download_from_google_storage%s' % suffix
54 cmd = [download_script, '-n', '-b', bucket, '-u', '-s',
Mads Ager418d1ca2017-05-22 09:35:49 +020055 sha1_file]
56 PrintCmd(cmd)
57 subprocess.check_call(cmd)
58
59def 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 Kenez971eec62017-05-24 11:08:40 +020069def makedirs_if_needed(path):
70 try:
71 os.makedirs(path)
72 except OSError:
73 if not os.path.isdir(path):
74 raise
75
Rico Windb4621c12017-08-28 12:48:53 +020076def upload_dir_to_cloud_storage(directory, destination):
Rico Winda94f01c2017-06-27 10:32:34 +020077 # Upload and make the content encoding right for viewing directly
Rico Winddce9c872017-08-14 14:49:04 +020078 cmd = ['gsutil.py', 'cp', '-z', 'html', '-a',
Rico Winda94f01c2017-06-27 10:32:34 +020079 'public-read', '-R', directory, destination]
80 PrintCmd(cmd)
81 subprocess.check_call(cmd)
82
Rico Windb4621c12017-08-28 12:48:53 +020083def 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 Wind1a29c4f2018-01-25 08:43:08 +010088# 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.
91def 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 Ager418d1ca2017-05-22 09:35:49 +020097class 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
109class 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 Windf80f5a22017-06-16 09:15:57 +0200115 print 'Enter directory = ', self._working_directory
Mads Ager418d1ca2017-05-22 09:35:49 +0200116 os.chdir(self._working_directory)
117
118 def __exit__(self, *_):
Rico Windf80f5a22017-06-16 09:15:57 +0200119 print 'Enter directory = ', self._old_cwd
Mads Ager418d1ca2017-05-22 09:35:49 +0200120 os.chdir(self._old_cwd)
Tamas Kenez82efeb52017-06-12 13:56:22 +0200121
122# Reading Android CTS test_result.xml
123
124class CtsModule(object):
125 def __init__(self, module_name):
126 self.name = module_name
127
128class CtsTestCase(object):
129 def __init__(self, test_case_name):
130 self.name = test_case_name
131
132class 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.
139def 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 Windf80f5a22017-06-16 09:15:57 +0200156 assert outcome in ['fail', 'pass']
Tamas Kenez82efeb52017-06-12 13:56:22 +0200157 yield CtsTest(m.groups()[1], outcome == 'pass')
Tamas Kenezfc34cd82017-07-13 12:43:57 +0200158
159def 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 Kenez02bff032017-07-18 12:13:58 +0200179 return result
180
181# Return a dictionary: {segment_name -> segments_size}
182def 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
202def 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 Kenez2cf47cf2017-07-25 10:22:52 +0200206
Mads Agerbc7b2ce2018-02-05 11:28:47 +0100207# Ensure that we are not benchmarking with a google jvm.
Tamas Kenez2cf47cf2017-07-25 10:22:52 +0200208def 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 Agerbc7b2ce2018-02-05 11:28:47 +0100216 m = re.search('google', version)
217 if m is not None:
218 raise Exception("Do not use google JVM for benchmarking: " + version)
Tamas Kenez0cad51c2017-08-21 14:42:01 +0200219
220def 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 Hertzae3a1212017-09-08 10:01:17 +0200242 '--compiler-filter=quicken'
Tamas Kenez0cad51c2017-08-21 14:42:01 +0200243 ]
244
245 PrintCmd(cmd)
246 subprocess.check_call(cmd,
247 env = {"LD_LIBRARY_PATH":
248 os.path.join(TOOLS_DIR, 'linux', 'art', 'lib')}
249 )