Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
Morten Krogh-Jespersen | cd55f81 | 2020-11-04 09:13:31 +0100 | [diff] [blame] | 2 | # Copyright (c) 2020, 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. |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 5 | |
Morten Krogh-Jespersen | cd55f81 | 2020-11-04 09:13:31 +0100 | [diff] [blame] | 6 | import adb |
| 7 | import apk_masseur |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 8 | import compiledump |
| 9 | import gradle |
| 10 | import jdk |
| 11 | import optparse |
| 12 | import os |
| 13 | import shutil |
| 14 | import sys |
| 15 | import time |
| 16 | import utils |
| 17 | import zipfile |
| 18 | |
| 19 | from datetime import datetime |
| 20 | |
| 21 | SHRINKERS = ['r8', 'r8-full', 'r8-nolib', 'r8-nolib-full'] |
| 22 | |
| 23 | class AttrDict(dict): |
| 24 | def __getattr__(self, name): |
| 25 | return self.get(name, None) |
| 26 | |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 27 | # To generate the files for a new app, navigate to the app source folder and |
| 28 | # run: |
| 29 | # ./gradlew clean :app:assembleRelease -Dcom.android.tools.r8.dumpinputtodirectory=<path> |
| 30 | # and store the dump and the apk. |
| 31 | # If the app has instrumented tests, adding `testBuildType "release"` and |
| 32 | # running: |
| 33 | # ./gradlew assembleAndroidTest -Dcom.android.tools.r8.dumpinputtodirectory=<path> |
| 34 | # will also generate dumps and apk for tests. |
| 35 | |
| 36 | class App(object): |
| 37 | def __init__(self, fields): |
| 38 | defaults = { |
| 39 | 'id': None, |
| 40 | 'name': None, |
| 41 | 'dump_app': None, |
Morten Krogh-Jespersen | cd55f81 | 2020-11-04 09:13:31 +0100 | [diff] [blame] | 42 | 'apk_app': None, |
Morten Krogh-Jespersen | 51a1635 | 2020-11-04 09:31:15 +0100 | [diff] [blame] | 43 | 'apk_test': None, |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 44 | 'skip': False, |
| 45 | 'url': None, # url is not used but nice to have for updating apps |
| 46 | 'revision': None, |
| 47 | 'folder': None, |
| 48 | 'skip_recompilation': False, |
| 49 | } |
| 50 | # This below does not work in python3 |
| 51 | defaults.update(fields.items()) |
| 52 | self.__dict__ = defaults |
| 53 | |
| 54 | |
| 55 | APPS = [ |
| 56 | App({ |
Morten Krogh-Jespersen | 0f85fa5 | 2020-11-04 13:40:43 +0100 | [diff] [blame] | 57 | 'id': 'com.numix.calculator', |
| 58 | 'name': 'Calculator', |
| 59 | 'dump_app': 'dump_app.zip', |
| 60 | 'apk_app': 'app-release.apk', |
| 61 | # Compiling tests fail: Library class android.content.res.XmlResourceParser |
| 62 | # implements program class org.xmlpull.v1.XmlPullParser. Nothing to really |
| 63 | # do about that. |
| 64 | 'id_test': 'com.numix.calculator.test', |
| 65 | 'dump_test': 'dump_test.zip', |
| 66 | 'apk_test': 'app-release-androidTest.apk', |
| 67 | 'url': 'https://github.com/numixproject/android-suite/tree/master/Calculator', |
| 68 | 'revision': 'f58e1b53f7278c9b675d5855842c6d8a44cccb1f', |
| 69 | 'folder': 'android-suite-calculator', |
| 70 | }), |
| 71 | App({ |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 72 | 'id': 'com.example.applymapping', |
| 73 | 'name': 'applymapping', |
| 74 | 'dump_app': 'dump_app.zip', |
Morten Krogh-Jespersen | cd55f81 | 2020-11-04 09:13:31 +0100 | [diff] [blame] | 75 | 'apk_app': 'app-release.apk', |
Morten Krogh-Jespersen | 51a1635 | 2020-11-04 09:31:15 +0100 | [diff] [blame] | 76 | 'id_test': 'com.example.applymapping.test', |
| 77 | 'dump_test': 'dump_test.zip', |
| 78 | 'apk_test': 'app-release-androidTest.apk', |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 79 | 'url': 'https://github.com/mkj-gram/applymapping', |
| 80 | 'revision': 'e3ae14b8c16fa4718e5dea8f7ad00937701b3c48', |
| 81 | 'folder': 'applymapping', |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 82 | }) |
| 83 | ] |
| 84 | |
| 85 | |
Morten Krogh-Jespersen | dfeb0e3 | 2020-11-04 14:55:55 +0100 | [diff] [blame^] | 86 | def remove_print_lines(file): |
| 87 | with open(file) as f: |
| 88 | lines = f.readlines() |
| 89 | with open(file, 'w') as f: |
| 90 | for line in lines: |
| 91 | if '-printconfiguration' not in line: |
| 92 | f.write(line) |
| 93 | |
| 94 | |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 95 | def download_app(app_sha): |
| 96 | utils.DownloadFromGoogleCloudStorage(app_sha) |
| 97 | |
| 98 | |
Morten Krogh-Jespersen | cd55f81 | 2020-11-04 09:13:31 +0100 | [diff] [blame] | 99 | def is_logging_enabled_for(app, options): |
| 100 | if options.no_logging: |
| 101 | return False |
| 102 | if options.app_logging_filter and app.name not in options.app_logging_filter: |
| 103 | return False |
| 104 | return True |
| 105 | |
| 106 | |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 107 | def is_minified_r8(shrinker): |
| 108 | return '-nolib' not in shrinker |
| 109 | |
| 110 | |
| 111 | def is_full_r8(shrinker): |
| 112 | return '-full' not in shrinker |
| 113 | |
| 114 | |
| 115 | def compute_size_of_dex_files_in_package(path): |
| 116 | dex_size = 0 |
| 117 | z = zipfile.ZipFile(path, 'r') |
| 118 | for filename in z.namelist(): |
| 119 | if filename.endswith('.dex'): |
| 120 | dex_size += z.getinfo(filename).file_size |
| 121 | return dex_size |
| 122 | |
| 123 | |
| 124 | def dump_for_app(app_dir, app): |
| 125 | return os.path.join(app_dir, app.dump_app) |
| 126 | |
| 127 | |
Morten Krogh-Jespersen | 51a1635 | 2020-11-04 09:31:15 +0100 | [diff] [blame] | 128 | def dump_test_for_app(app_dir, app): |
| 129 | return os.path.join(app_dir, app.dump_test) |
| 130 | |
| 131 | |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 132 | def get_results_for_app(app, options, temp_dir): |
| 133 | app_folder = app.folder if app.folder else app.name + "_" + app.revision |
| 134 | app_dir = os.path.join(utils.OPENSOURCE_DUMPS_DIR, app_folder) |
| 135 | |
| 136 | if not os.path.exists(app_dir) and not options.golem: |
| 137 | # Download the app from google storage. |
| 138 | download_app(app_dir + ".tar.gz.sha1") |
| 139 | |
| 140 | # Ensure that the dumps are in place |
| 141 | assert os.path.isfile(dump_for_app(app_dir, app)), "Could not find dump " \ |
| 142 | "for app " + app.name |
| 143 | |
| 144 | result = {} |
| 145 | result['status'] = 'success' |
| 146 | result_per_shrinker = build_app_with_shrinkers( |
| 147 | app, options, temp_dir, app_dir) |
| 148 | for shrinker, shrinker_result in result_per_shrinker.iteritems(): |
| 149 | result[shrinker] = shrinker_result |
| 150 | return result |
| 151 | |
| 152 | |
| 153 | def build_app_with_shrinkers(app, options, temp_dir, app_dir): |
| 154 | result_per_shrinker = {} |
| 155 | for shrinker in options.shrinker: |
| 156 | results = [] |
| 157 | build_app_and_run_with_shrinker( |
| 158 | app, options, temp_dir, app_dir, shrinker, results) |
| 159 | result_per_shrinker[shrinker] = results |
| 160 | if len(options.apps) > 1: |
| 161 | print('') |
| 162 | log_results_for_app(app, result_per_shrinker, options) |
| 163 | print('') |
| 164 | |
| 165 | return result_per_shrinker |
| 166 | |
| 167 | |
| 168 | def is_last_build(index, compilation_steps): |
| 169 | return index == compilation_steps - 1 |
| 170 | |
| 171 | |
| 172 | def build_app_and_run_with_shrinker(app, options, temp_dir, app_dir, shrinker, |
| 173 | results): |
| 174 | print('[{}] Building {} with {}'.format( |
| 175 | datetime.now().strftime("%H:%M:%S"), |
| 176 | app.name, |
| 177 | shrinker)) |
| 178 | print('To compile locally: ' |
| 179 | 'tools/run_on_as_app.py --shrinker {} --r8-compilation-steps {} ' |
| 180 | '--app {}'.format( |
| 181 | shrinker, |
| 182 | options.r8_compilation_steps, |
| 183 | app.name)) |
| 184 | print('HINT: use --shrinker r8-nolib --no-build if you have a local R8.jar') |
| 185 | recomp_jar = None |
| 186 | status = 'success' |
| 187 | compilation_steps = 1 if app.skip_recompilation else options.r8_compilation_steps; |
| 188 | for compilation_step in range(0, compilation_steps): |
| 189 | if status != 'success': |
| 190 | break |
Morten Krogh-Jespersen | 51a1635 | 2020-11-04 09:31:15 +0100 | [diff] [blame] | 191 | print('Compiling {} of {}'.format(compilation_step + 1, compilation_steps)) |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 192 | result = {} |
| 193 | try: |
| 194 | start = time.time() |
Morten Krogh-Jespersen | 51a1635 | 2020-11-04 09:31:15 +0100 | [diff] [blame] | 195 | (app_jar, mapping, new_recomp_jar) = \ |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 196 | build_app_with_shrinker( |
| 197 | app, options, temp_dir, app_dir, shrinker, compilation_step, |
| 198 | compilation_steps, recomp_jar) |
| 199 | end = time.time() |
| 200 | dex_size = compute_size_of_dex_files_in_package(app_jar) |
| 201 | result['build_status'] = 'success' |
| 202 | result['recompilation_status'] = 'success' |
| 203 | result['output_jar'] = app_jar |
Morten Krogh-Jespersen | 51a1635 | 2020-11-04 09:31:15 +0100 | [diff] [blame] | 204 | result['output_mapping'] = mapping |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 205 | result['dex_size'] = dex_size |
| 206 | result['duration'] = int((end - start) * 1000) # Wall time |
| 207 | if (new_recomp_jar is None |
| 208 | and not is_last_build(compilation_step, compilation_steps)): |
| 209 | result['recompilation_status'] = 'failed' |
| 210 | warn('Failed to build {} with {}'.format(app.name, shrinker)) |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 211 | break |
| 212 | recomp_jar = new_recomp_jar |
| 213 | except Exception as e: |
| 214 | warn('Failed to build {} with {}'.format(app.name, shrinker)) |
| 215 | if e: |
| 216 | print('Error: ' + str(e)) |
| 217 | result['build_status'] = 'failed' |
| 218 | status = 'failed' |
| 219 | |
Morten Krogh-Jespersen | 51a1635 | 2020-11-04 09:31:15 +0100 | [diff] [blame] | 220 | original_app_apk = os.path.join(app_dir, app.apk_app) |
| 221 | app_apk_destination = os.path.join( |
| 222 | temp_dir,"{}_{}.apk".format(app.id, compilation_step)) |
| 223 | |
Morten Krogh-Jespersen | cd55f81 | 2020-11-04 09:13:31 +0100 | [diff] [blame] | 224 | if result.get('build_status') == 'success' and options.monkey: |
| 225 | # Make a copy of the given APK, move the newly generated dex files into the |
| 226 | # copied APK, and then sign the APK. |
Morten Krogh-Jespersen | cd55f81 | 2020-11-04 09:13:31 +0100 | [diff] [blame] | 227 | apk_masseur.masseur( |
| 228 | original_app_apk, dex=app_jar, resources='META-INF/services/*', |
Morten Krogh-Jespersen | 51a1635 | 2020-11-04 09:31:15 +0100 | [diff] [blame] | 229 | out=app_apk_destination, |
Morten Krogh-Jespersen | cd55f81 | 2020-11-04 09:13:31 +0100 | [diff] [blame] | 230 | quiet=options.quiet, logging=is_logging_enabled_for(app, options), |
| 231 | keystore=options.keystore) |
| 232 | |
| 233 | result['monkey_status'] = 'success' if adb.run_monkey( |
Morten Krogh-Jespersen | 51a1635 | 2020-11-04 09:31:15 +0100 | [diff] [blame] | 234 | app.id, options.emulator_id, app_apk_destination, options.monkey_events, |
Morten Krogh-Jespersen | cd55f81 | 2020-11-04 09:13:31 +0100 | [diff] [blame] | 235 | options.quiet, is_logging_enabled_for(app, options)) else 'failed' |
| 236 | |
Morten Krogh-Jespersen | 51a1635 | 2020-11-04 09:31:15 +0100 | [diff] [blame] | 237 | if result.get('build_status') == 'success' and options.run_tests: |
| 238 | if not os.path.isfile(app_apk_destination): |
| 239 | apk_masseur.masseur( |
| 240 | original_app_apk, dex=app_jar, resources='META-INF/services/*', |
| 241 | out=app_apk_destination, |
| 242 | quiet=options.quiet, logging=is_logging_enabled_for(app, options), |
| 243 | keystore=options.keystore) |
| 244 | |
| 245 | # Compile the tests with the mapping file. |
| 246 | test_jar = build_test_with_shrinker( |
| 247 | app, options, temp_dir, app_dir,shrinker, compilation_step, |
| 248 | result['output_mapping']) |
| 249 | original_test_apk = os.path.join(app_dir, app.apk_test) |
| 250 | test_apk_destination = os.path.join( |
| 251 | temp_dir,"{}_{}.test.apk".format(app.id_test, compilation_step)) |
| 252 | apk_masseur.masseur( |
| 253 | original_test_apk, dex=test_jar, resources='META-INF/services/*', |
| 254 | out=test_apk_destination, |
| 255 | quiet=options.quiet, logging=is_logging_enabled_for(app, options), |
| 256 | keystore=options.keystore) |
| 257 | result['instrumentation_test_status'] = 'success' if adb.run_instrumented( |
| 258 | app.id, app.id_test, options.emulator_id, app_apk_destination, |
| 259 | test_apk_destination, options.quiet, |
| 260 | is_logging_enabled_for(app, options)) else 'failed' |
| 261 | |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 262 | results.append(result) |
Morten Krogh-Jespersen | cd55f81 | 2020-11-04 09:13:31 +0100 | [diff] [blame] | 263 | if result.get('recompilation_status') != 'success': |
| 264 | break |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 265 | |
| 266 | |
| 267 | def build_app_with_shrinker(app, options, temp_dir, app_dir, shrinker, |
| 268 | compilation_step_index, compilation_steps, |
| 269 | prev_recomp_jar): |
| 270 | r8jar = os.path.join( |
| 271 | temp_dir, 'r8lib.jar' if is_minified_r8(shrinker) else 'r8.jar') |
| 272 | |
| 273 | args = AttrDict({ |
| 274 | 'dump': dump_for_app(app_dir, app), |
| 275 | 'r8_jar': r8jar, |
| 276 | 'ea': False if options.disable_assertions else True, |
| 277 | 'version': 'master', |
| 278 | 'compiler': 'r8full' if is_full_r8(shrinker) else 'r8', |
| 279 | 'debug_agent': options.debug_agent, |
| 280 | 'program_jar': prev_recomp_jar, |
Morten Krogh-Jespersen | dfeb0e3 | 2020-11-04 14:55:55 +0100 | [diff] [blame^] | 281 | 'nolib': not is_minified_r8(shrinker), |
| 282 | 'config_file_consumer': remove_print_lines, |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 283 | }) |
| 284 | |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 285 | compile_result = compiledump.run1(temp_dir, args, []) |
Morten Krogh-Jespersen | 51a1635 | 2020-11-04 09:31:15 +0100 | [diff] [blame] | 286 | |
| 287 | out_jar = os.path.join(temp_dir, "out.jar") |
| 288 | out_mapping = os.path.join(temp_dir, "out.jar.map") |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 289 | app_jar = os.path.join( |
| 290 | temp_dir, '{}_{}_{}_dex_out.jar'.format( |
| 291 | app.name, shrinker, compilation_step_index)) |
Morten Krogh-Jespersen | 51a1635 | 2020-11-04 09:31:15 +0100 | [diff] [blame] | 292 | app_mapping = os.path.join( |
| 293 | temp_dir, '{}_{}_{}_dex_out.jar.map'.format( |
| 294 | app.name, shrinker, compilation_step_index)) |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 295 | |
| 296 | if compile_result != 0 or not os.path.isfile(out_jar): |
| 297 | assert False, "Compilation of app_jar failed" |
| 298 | shutil.move(out_jar, app_jar) |
Morten Krogh-Jespersen | 51a1635 | 2020-11-04 09:31:15 +0100 | [diff] [blame] | 299 | shutil.move(out_mapping, app_mapping) |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 300 | |
| 301 | recomp_jar = None |
| 302 | if compilation_step_index < compilation_steps - 1: |
| 303 | args['classfile'] = True |
| 304 | args['min_api'] = "10000" |
| 305 | compile_result = compiledump.run1(temp_dir, args, []) |
| 306 | if compile_result == 0: |
| 307 | recomp_jar = os.path.join( |
| 308 | temp_dir, '{}_{}_{}_cf_out.jar'.format( |
| 309 | app.name, shrinker, compilation_step_index)) |
| 310 | shutil.move(out_jar, recomp_jar) |
| 311 | |
Morten Krogh-Jespersen | 51a1635 | 2020-11-04 09:31:15 +0100 | [diff] [blame] | 312 | return (app_jar, app_mapping, recomp_jar) |
| 313 | |
Morten Krogh-Jespersen | 51a1635 | 2020-11-04 09:31:15 +0100 | [diff] [blame] | 314 | def build_test_with_shrinker(app, options, temp_dir, app_dir, shrinker, |
| 315 | compilation_step_index, mapping): |
| 316 | r8jar = os.path.join( |
| 317 | temp_dir, 'r8lib.jar' if is_minified_r8(shrinker) else 'r8.jar') |
| 318 | |
| 319 | def rewrite_file(file): |
Morten Krogh-Jespersen | dfeb0e3 | 2020-11-04 14:55:55 +0100 | [diff] [blame^] | 320 | remove_print_lines(file) |
Morten Krogh-Jespersen | 51a1635 | 2020-11-04 09:31:15 +0100 | [diff] [blame] | 321 | with open(file) as f: |
| 322 | lines = f.readlines() |
| 323 | with open(file, 'w') as f: |
| 324 | for line in lines: |
| 325 | if '-applymapping' not in line: |
| 326 | f.write(line + '\n') |
| 327 | f.write("-applymapping " + mapping + '\n') |
| 328 | |
| 329 | args = AttrDict({ |
| 330 | 'dump': dump_test_for_app(app_dir, app), |
| 331 | 'r8_jar': r8jar, |
| 332 | 'ea': False if options.disable_assertions else True, |
| 333 | 'version': 'master', |
| 334 | 'compiler': 'r8full' if is_full_r8(shrinker) else 'r8', |
| 335 | 'debug_agent': options.debug_agent, |
| 336 | 'nolib': not is_minified_r8(shrinker), |
| 337 | # The config file will have an -applymapping reference to an old map. |
| 338 | # Update it to point to mapping file build in the compilation of the app. |
| 339 | 'config_file_consumer': rewrite_file |
| 340 | }) |
| 341 | |
| 342 | compile_result = compiledump.run1(temp_dir, args, []) |
| 343 | |
| 344 | out_jar = os.path.join(temp_dir, "out.jar") |
| 345 | test_jar = os.path.join( |
| 346 | temp_dir, '{}_{}_{}_test_out.jar'.format( |
| 347 | app.name, shrinker, compilation_step_index)) |
| 348 | |
| 349 | if compile_result != 0 or not os.path.isfile(out_jar): |
| 350 | assert False, "Compilation of test_jar failed" |
| 351 | shutil.move(out_jar, test_jar) |
| 352 | |
| 353 | return test_jar |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 354 | |
| 355 | |
| 356 | def log_results_for_apps(result_per_shrinker_per_app, options): |
| 357 | print('') |
| 358 | app_errors = 0 |
| 359 | for (app, result_per_shrinker) in result_per_shrinker_per_app: |
| 360 | app_errors += (1 if log_results_for_app(app, result_per_shrinker, options) |
| 361 | else 0) |
| 362 | return app_errors |
| 363 | |
| 364 | |
| 365 | def log_results_for_app(app, result_per_shrinker, options): |
| 366 | if options.print_dexsegments: |
| 367 | log_segments_for_app(app, result_per_shrinker, options) |
| 368 | return False |
| 369 | else: |
| 370 | return log_comparison_results_for_app(app, result_per_shrinker, options) |
| 371 | |
| 372 | |
| 373 | def log_segments_for_app(app, result_per_shrinker, options): |
| 374 | for shrinker in SHRINKERS: |
| 375 | if shrinker not in result_per_shrinker: |
| 376 | continue |
| 377 | for result in result_per_shrinker.get(shrinker): |
| 378 | benchmark_name = '{}-{}'.format(options.print_dexsegments, app.name) |
| 379 | utils.print_dexsegments(benchmark_name, [result.get('output_jar')]) |
| 380 | duration = result.get('duration') |
| 381 | print('%s-Total(RunTimeRaw): %s ms' % (benchmark_name, duration)) |
| 382 | print('%s-Total(CodeSize): %s' % (benchmark_name, result.get('dex_size'))) |
| 383 | |
| 384 | |
| 385 | def percentage_diff_as_string(before, after): |
| 386 | if after < before: |
| 387 | return '-' + str(round((1.0 - after / before) * 100)) + '%' |
| 388 | else: |
| 389 | return '+' + str(round((after - before) / before * 100)) + '%' |
| 390 | |
| 391 | |
| 392 | def log_comparison_results_for_app(app, result_per_shrinker, options): |
| 393 | print(app.name + ':') |
| 394 | app_error = False |
| 395 | if result_per_shrinker.get('status', 'success') != 'success': |
| 396 | error_message = result_per_shrinker.get('error_message') |
| 397 | print(' skipped ({})'.format(error_message)) |
| 398 | return |
| 399 | |
| 400 | proguard_result = result_per_shrinker.get('pg', {}) |
| 401 | proguard_dex_size = float(proguard_result.get('dex_size', -1)) |
| 402 | |
| 403 | for shrinker in SHRINKERS: |
| 404 | if shrinker not in result_per_shrinker: |
| 405 | continue |
| 406 | compilation_index = 1 |
| 407 | for result in result_per_shrinker.get(shrinker): |
| 408 | build_status = result.get('build_status') |
| 409 | if build_status != 'success' and build_status is not None: |
| 410 | app_error = True |
| 411 | warn(' {}-#{}: {}'.format(shrinker, compilation_index, build_status)) |
| 412 | continue |
| 413 | |
| 414 | print(' {}-#{}:'.format(shrinker, compilation_index)) |
| 415 | dex_size = result.get('dex_size') |
| 416 | msg = ' dex size: {}'.format(dex_size) |
| 417 | if dex_size != proguard_dex_size and proguard_dex_size >= 0: |
| 418 | msg = '{} ({}, {})'.format( |
| 419 | msg, dex_size - proguard_dex_size, |
| 420 | percentage_diff_as_string(proguard_dex_size, dex_size)) |
| 421 | success(msg) if dex_size < proguard_dex_size else warn(msg) |
| 422 | else: |
| 423 | print(msg) |
| 424 | |
Morten Krogh-Jespersen | cd55f81 | 2020-11-04 09:13:31 +0100 | [diff] [blame] | 425 | if options.monkey: |
| 426 | monkey_status = result.get('monkey_status') |
| 427 | if monkey_status != 'success': |
| 428 | app_error = True |
| 429 | warn(' monkey: {}'.format(monkey_status)) |
| 430 | else: |
| 431 | success(' monkey: {}'.format(monkey_status)) |
| 432 | |
Morten Krogh-Jespersen | 51a1635 | 2020-11-04 09:31:15 +0100 | [diff] [blame] | 433 | if options.run_tests and 'instrumentation_test_status' in result: |
| 434 | test_status = result.get('instrumentation_test_status') |
| 435 | if test_status != 'success': |
| 436 | warn(' instrumentation_tests: {}'.format(test_status)) |
| 437 | else: |
| 438 | success(' instrumentation_tests: {}'.format(test_status)) |
| 439 | |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 440 | recompilation_status = result.get('recompilation_status', '') |
| 441 | if recompilation_status == 'failed': |
| 442 | app_error = True |
| 443 | warn(' recompilation {}-#{}: failed'.format(shrinker, |
| 444 | compilation_index)) |
| 445 | continue |
| 446 | |
| 447 | compilation_index += 1 |
| 448 | |
| 449 | return app_error |
| 450 | |
| 451 | |
| 452 | def parse_options(argv): |
| 453 | result = optparse.OptionParser() |
| 454 | result.add_option('--app', |
| 455 | help='What app to run on', |
| 456 | choices=[app.name for app in APPS], |
| 457 | action='append') |
| 458 | result.add_option('--bot', |
| 459 | help='Running on bot, use third_party dependency.', |
| 460 | default=False, |
| 461 | action='store_true') |
| 462 | result.add_option('--debug-agent', |
| 463 | help='Enable Java debug agent and suspend compilation ' |
| 464 | '(default disabled)', |
| 465 | default=False, |
| 466 | action='store_true') |
| 467 | result.add_option('--disable-assertions', '--disable_assertions', |
| 468 | help='Disable assertions when compiling', |
| 469 | default=False, |
| 470 | action='store_true') |
Morten Krogh-Jespersen | cd55f81 | 2020-11-04 09:13:31 +0100 | [diff] [blame] | 471 | result.add_option('--emulator-id', '--emulator_id', |
| 472 | help='Id of the emulator to use', |
| 473 | default='emulator-5554') |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 474 | result.add_option('--golem', |
| 475 | help='Running on golem, do not download', |
| 476 | default=False, |
| 477 | action='store_true') |
| 478 | result.add_option('--hash', |
| 479 | help='The commit of R8 to use') |
| 480 | result.add_option('--keystore', |
| 481 | help='Path to app.keystore', |
| 482 | default=os.path.join(utils.TOOLS_DIR, 'debug.keystore')) |
| 483 | result.add_option('--keystore-password', '--keystore_password', |
| 484 | help='Password for app.keystore', |
| 485 | default='android') |
| 486 | result.add_option('--app-logging-filter', '--app_logging_filter', |
| 487 | help='The apps for which to turn on logging', |
| 488 | action='append') |
| 489 | result.add_option('--monkey', |
| 490 | help='Whether to install and run app(s) with monkey', |
| 491 | default=False, |
| 492 | action='store_true') |
| 493 | result.add_option('--monkey-events', '--monkey_events', |
| 494 | help='Number of events that the monkey should trigger', |
| 495 | default=250, |
| 496 | type=int) |
| 497 | result.add_option('--no-build', '--no_build', |
| 498 | help='Run without building ToT first (only when using ToT)', |
| 499 | default=False, |
| 500 | action='store_true') |
| 501 | result.add_option('--no-logging', '--no_logging', |
| 502 | help='Disable logging except for errors', |
| 503 | default=False, |
| 504 | action='store_true') |
| 505 | result.add_option('--print-dexsegments', |
| 506 | metavar='BENCHMARKNAME', |
| 507 | help='Print the sizes of individual dex segments as ' + |
| 508 | '\'<BENCHMARKNAME>-<APP>-<segment>(CodeSize): ' |
| 509 | '<bytes>\'') |
| 510 | result.add_option('--quiet', |
| 511 | help='Disable verbose logging', |
| 512 | default=False, |
| 513 | action='store_true') |
| 514 | result.add_option('--r8-compilation-steps', '--r8_compilation_steps', |
| 515 | help='Number of times R8 should be run on each app', |
| 516 | default=2, |
| 517 | type=int) |
| 518 | result.add_option('--run-tests', '--run_tests', |
| 519 | help='Whether to run instrumentation tests', |
| 520 | default=False, |
| 521 | action='store_true') |
| 522 | result.add_option('--sign-apks', '--sign_apks', |
| 523 | help='Whether the APKs should be signed', |
| 524 | default=False, |
| 525 | action='store_true') |
| 526 | result.add_option('--shrinker', |
| 527 | help='The shrinkers to use (by default, all are run)', |
| 528 | action='append') |
| 529 | result.add_option('--version', |
| 530 | help='The version of R8 to use (e.g., 1.4.51)') |
| 531 | (options, args) = result.parse_args(argv) |
| 532 | if options.app: |
| 533 | options.apps = [app for app in APPS if app.name in options.app] |
| 534 | del options.app |
| 535 | else: |
| 536 | options.apps = APPS |
| 537 | if options.app_logging_filter: |
| 538 | for app_name in options.app_logging_filter: |
| 539 | assert any(app.name == app_name for app in options.apps) |
| 540 | if options.shrinker: |
| 541 | for shrinker in options.shrinker: |
| 542 | assert shrinker in SHRINKERS |
| 543 | else: |
| 544 | options.shrinker = [shrinker for shrinker in SHRINKERS] |
| 545 | |
| 546 | if options.hash or options.version: |
| 547 | # No need to build R8 if a specific version should be used. |
| 548 | options.no_build = True |
| 549 | if 'r8-nolib' in options.shrinker: |
| 550 | warn('Skipping shrinker r8-nolib because a specific version ' |
| 551 | + 'of r8 was specified') |
| 552 | options.shrinker.remove('r8-nolib') |
| 553 | if 'r8-nolib-full' in options.shrinker: |
| 554 | warn('Skipping shrinker r8-nolib-full because a specific version ' |
| 555 | + 'of r8 was specified') |
| 556 | options.shrinker.remove('r8-nolib-full') |
| 557 | return (options, args) |
| 558 | |
| 559 | |
| 560 | def main(argv): |
| 561 | (options, args) = parse_options(argv) |
| 562 | |
| 563 | if options.bot: |
| 564 | options.no_logging = True |
| 565 | options.shrinker = ['r8', 'r8-full'] |
| 566 | print(options.shrinker) |
| 567 | |
| 568 | if options.golem: |
| 569 | golem.link_third_party() |
| 570 | options.disable_assertions = True |
| 571 | options.no_build = True |
| 572 | options.r8_compilation_steps = 1 |
| 573 | options.quiet = True |
| 574 | options.no_logging = True |
| 575 | |
Morten Krogh-Jespersen | 45d7a7b | 2020-11-02 08:31:09 +0100 | [diff] [blame] | 576 | with utils.TempDir() as temp_dir: |
| 577 | if options.hash: |
| 578 | # Download r8-<hash>.jar from |
| 579 | # https://storage.googleapis.com/r8-releases/raw/. |
| 580 | target = 'r8-{}.jar'.format(options.hash) |
| 581 | update_prebuilds_in_android.download_hash( |
| 582 | temp_dir, 'com/android/tools/r8/' + options.hash, target) |
| 583 | as_utils.MoveFile( |
| 584 | os.path.join(temp_dir, target), os.path.join(temp_dir, 'r8lib.jar'), |
| 585 | quiet=options.quiet) |
| 586 | elif options.version: |
| 587 | # Download r8-<version>.jar from |
| 588 | # https://storage.googleapis.com/r8-releases/raw/. |
| 589 | target = 'r8-{}.jar'.format(options.version) |
| 590 | update_prebuilds_in_android.download_version( |
| 591 | temp_dir, 'com/android/tools/r8/' + options.version, target) |
| 592 | as_utils.MoveFile( |
| 593 | os.path.join(temp_dir, target), os.path.join(temp_dir, 'r8lib.jar'), |
| 594 | quiet=options.quiet) |
| 595 | else: |
| 596 | if not (options.no_build or options.golem): |
| 597 | gradle.RunGradle(['r8', '-Pno_internal']) |
| 598 | build_r8lib = False |
| 599 | for shrinker in options.shrinker: |
| 600 | if is_minified_r8(shrinker): |
| 601 | build_r8lib = True |
| 602 | if build_r8lib: |
| 603 | gradle.RunGradle(['r8lib', '-Pno_internal']) |
| 604 | # Make a copy of r8.jar and r8lib.jar such that they stay the same for |
| 605 | # the entire execution of this script. |
| 606 | if 'r8-nolib' in options.shrinker or 'r8-nolib-full' in options.shrinker: |
| 607 | assert os.path.isfile(utils.R8_JAR), 'Cannot build without r8.jar' |
| 608 | shutil.copyfile(utils.R8_JAR, os.path.join(temp_dir, 'r8.jar')) |
| 609 | if 'r8' in options.shrinker or 'r8-full' in options.shrinker: |
| 610 | assert os.path.isfile(utils.R8LIB_JAR), 'Cannot build without r8lib.jar' |
| 611 | shutil.copyfile(utils.R8LIB_JAR, os.path.join(temp_dir, 'r8lib.jar')) |
| 612 | |
| 613 | result_per_shrinker_per_app = [] |
| 614 | for app in options.apps: |
| 615 | if app.skip: |
| 616 | continue |
| 617 | result_per_shrinker_per_app.append( |
| 618 | (app, get_results_for_app(app, options, temp_dir))) |
| 619 | return log_results_for_apps(result_per_shrinker_per_app, options) |
| 620 | |
| 621 | |
| 622 | def success(message): |
| 623 | CGREEN = '\033[32m' |
| 624 | CEND = '\033[0m' |
| 625 | print(CGREEN + message + CEND) |
| 626 | |
| 627 | |
| 628 | def warn(message): |
| 629 | CRED = '\033[91m' |
| 630 | CEND = '\033[0m' |
| 631 | print(CRED + message + CEND) |
| 632 | |
| 633 | |
| 634 | if __name__ == '__main__': |
| 635 | sys.exit(main(sys.argv[1:])) |