| OLD | NEW |
| 1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
| 2 # Copyright 2015 The Chromium Authors. All rights reserved. | 2 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import argparse | 6 import argparse |
| 7 import collections |
| 7 import os | 8 import os |
| 8 import re | 9 import re |
| 9 import shutil | 10 import shutil |
| 10 import sys | 11 import sys |
| 11 import tempfile | 12 import tempfile |
| 12 import zipfile | 13 import zipfile |
| 13 | 14 |
| 14 import devil_chromium | 15 import devil_chromium |
| 15 from devil.android.sdk import dexdump | 16 from devil.android.sdk import dexdump |
| 16 from pylib.constants import host_paths | 17 from pylib.constants import host_paths |
| 17 | 18 |
| 18 sys.path.append(os.path.join(host_paths.DIR_SOURCE_ROOT, 'build', 'util', 'lib', | 19 sys.path.append(os.path.join(host_paths.DIR_SOURCE_ROOT, 'build', 'util', 'lib', |
| 19 'common')) | 20 'common')) |
| 20 import perf_tests_results_helper # pylint: disable=import-error | 21 import perf_tests_results_helper # pylint: disable=import-error |
| 21 | 22 |
| 23 # Example dexdump output: |
| 24 # DEX file header: |
| 25 # magic : 'dex\n035\0' |
| 26 # checksum : b664fc68 |
| 27 # signature : ae73...87f1 |
| 28 # file_size : 4579656 |
| 29 # header_size : 112 |
| 30 # link_size : 0 |
| 31 # link_off : 0 (0x000000) |
| 32 # string_ids_size : 46148 |
| 33 # string_ids_off : 112 (0x000070) |
| 34 # type_ids_size : 5730 |
| 35 # type_ids_off : 184704 (0x02d180) |
| 36 # proto_ids_size : 8289 |
| 37 # proto_ids_off : 207624 (0x032b08) |
| 38 # field_ids_size : 17854 |
| 39 # field_ids_off : 307092 (0x04af94) |
| 40 # method_ids_size : 33699 |
| 41 # method_ids_off : 449924 (0x06dd84) |
| 42 # class_defs_size : 2616 |
| 43 # class_defs_off : 719516 (0x0afa9c) |
| 44 # data_size : 3776428 |
| 45 # data_off : 803228 (0x0c419c) |
| 22 | 46 |
| 23 _METHOD_IDS_SIZE_RE = re.compile(r'^method_ids_size +: +(\d+)$') | 47 # For what these mean, refer to: |
| 48 # https://source.android.com/devices/tech/dalvik/dex-format.html |
| 24 | 49 |
| 25 def ExtractIfZip(dexfile, tmpdir): | |
| 26 if not os.path.splitext(dexfile)[1] in ('.zip', '.apk', '.jar'): | |
| 27 return [dexfile] | |
| 28 | 50 |
| 29 with zipfile.ZipFile(dexfile, 'r') as z: | 51 def _ExtractSizesFromDexFile(dex_path): |
| 30 dex_files = [n for n in z.namelist() if n.endswith('.dex')] | 52 counts = {} |
| 31 z.extractall(tmpdir, dex_files) | 53 for line in dexdump.DexDump(dex_path, file_summary=True): |
| 54 if not line.strip(): |
| 55 return counts |
| 56 m = re.match(r'([a-z_]+_size) *: (\d+)', line) |
| 57 if m: |
| 58 counts[m.group(1)] = int(m.group(2)) |
| 59 raise Exception('Unexpected end of output.') |
| 32 | 60 |
| 33 return [os.path.join(tmpdir, f) for f in dex_files] | |
| 34 | 61 |
| 35 def SingleMethodCount(dexfile): | 62 def _ExtractSizesFromZip(path): |
| 36 for line in dexdump.DexDump(dexfile, file_summary=True): | |
| 37 m = _METHOD_IDS_SIZE_RE.match(line) | |
| 38 if m: | |
| 39 return m.group(1) | |
| 40 raise Exception('"method_ids_size" not found in dex dump of %s' % dexfile) | |
| 41 | |
| 42 def MethodCount(dexfile): | |
| 43 tmpdir = tempfile.mkdtemp(suffix='_dex_extract') | 63 tmpdir = tempfile.mkdtemp(suffix='_dex_extract') |
| 44 multidex_file_list = ExtractIfZip(dexfile, tmpdir) | |
| 45 try: | 64 try: |
| 46 return sum(int(SingleMethodCount(d)) for d in multidex_file_list) | 65 counts = collections.defaultdict(int) |
| 66 with zipfile.ZipFile(path, 'r') as z: |
| 67 for subpath in z.namelist(): |
| 68 if not subpath.endswith('.dex'): |
| 69 continue |
| 70 extracted_path = z.extract(subpath, tmpdir) |
| 71 cur_counts = _ExtractSizesFromDexFile(extracted_path) |
| 72 for k in cur_counts: |
| 73 counts[k] += cur_counts[k] |
| 74 return dict(counts) |
| 47 finally: | 75 finally: |
| 48 shutil.rmtree(tmpdir) | 76 shutil.rmtree(tmpdir) |
| 49 | 77 |
| 78 |
| 50 def main(): | 79 def main(): |
| 51 parser = argparse.ArgumentParser() | 80 parser = argparse.ArgumentParser() |
| 52 parser.add_argument( | 81 parser.add_argument( |
| 53 '--apk-name', help='Name of the APK to which the dexfile corresponds.') | 82 '--apk-name', help='Name of the APK to which the dexfile corresponds.') |
| 54 parser.add_argument('dexfile') | 83 parser.add_argument('dexfile') |
| 55 | 84 |
| 56 args = parser.parse_args() | 85 args = parser.parse_args() |
| 57 | 86 |
| 58 devil_chromium.Initialize() | 87 devil_chromium.Initialize() |
| 59 | 88 |
| 60 if not args.apk_name: | 89 if not args.apk_name: |
| 61 dirname, basename = os.path.split(args.dexfile) | 90 dirname, basename = os.path.split(args.dexfile) |
| 62 while basename: | 91 while basename: |
| 63 if 'apk' in basename: | 92 if 'apk' in basename: |
| 64 args.apk_name = basename | 93 args.apk_name = basename |
| 65 break | 94 break |
| 66 dirname, basename = os.path.split(dirname) | 95 dirname, basename = os.path.split(dirname) |
| 67 else: | 96 else: |
| 68 parser.error( | 97 parser.error( |
| 69 'Unable to determine apk name from %s, ' | 98 'Unable to determine apk name from %s, ' |
| 70 'and --apk-name was not provided.' % args.dexfile) | 99 'and --apk-name was not provided.' % args.dexfile) |
| 71 | 100 |
| 72 method_count = MethodCount(args.dexfile) | 101 if os.path.splitext(args.dexfile)[1] in ('.zip', '.apk', '.jar'): |
| 73 perf_tests_results_helper.PrintPerfResult( | 102 sizes = _ExtractSizesFromZip(args.dexfile) |
| 74 '%s_methods' % args.apk_name, 'total', [method_count], 'methods') | 103 else: |
| 104 sizes = _ExtractSizesFromDexFile(args.dexfile) |
| 105 |
| 106 def print_result(name, value_key): |
| 107 perf_tests_results_helper.PrintPerfResult( |
| 108 '%s_%s' % (args.apk_name, name), 'total', [sizes[value_key]], name) |
| 109 |
| 110 print_result('classes', 'class_defs_size') |
| 111 print_result('fields', 'field_ids_size') |
| 112 print_result('methods', 'method_ids_size') |
| 113 print_result('strings', 'string_ids_size') |
| 75 return 0 | 114 return 0 |
| 76 | 115 |
| 77 if __name__ == '__main__': | 116 if __name__ == '__main__': |
| 78 sys.exit(main()) | 117 sys.exit(main()) |
| 79 | 118 |
| OLD | NEW |