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