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 | 22 |
23 _FIELD_IDS_SIZE_RE = re.compile(r'^field_ids_size +: +(\d+)$') | |
23 _METHOD_IDS_SIZE_RE = re.compile(r'^method_ids_size +: +(\d+)$') | 24 _METHOD_IDS_SIZE_RE = re.compile(r'^method_ids_size +: +(\d+)$') |
24 | 25 |
25 def ExtractIfZip(dexfile, tmpdir): | 26 |
27 def _ExtractIfZip(dexfile, tmpdir): | |
26 if not os.path.splitext(dexfile)[1] in ('.zip', '.apk', '.jar'): | 28 if not os.path.splitext(dexfile)[1] in ('.zip', '.apk', '.jar'): |
27 return [dexfile] | 29 return [dexfile] |
28 | 30 |
29 with zipfile.ZipFile(dexfile, 'r') as z: | 31 with zipfile.ZipFile(dexfile, 'r') as z: |
30 dex_files = [n for n in z.namelist() if n.endswith('.dex')] | 32 dex_files = [n for n in z.namelist() if n.endswith('.dex')] |
31 z.extractall(tmpdir, dex_files) | 33 z.extractall(tmpdir, dex_files) |
32 | 34 |
33 return [os.path.join(tmpdir, f) for f in dex_files] | 35 return [os.path.join(tmpdir, f) for f in dex_files] |
34 | 36 |
35 def SingleMethodCount(dexfile): | |
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 | 37 |
42 def MethodCount(dexfile): | 38 def _SingleMethodAndFieldCount(dex_path): |
jbudorick
2016/06/27 09:21:27
I forget what else is in the dexdump -- would we p
| |
39 fields = None | |
40 methods = None | |
41 for line in dexdump.DexDump(dex_path, file_summary=True): | |
42 if fields is None: | |
43 m = _FIELD_IDS_SIZE_RE.match(line) | |
44 if m: | |
45 fields = int(m.group(1)) | |
46 if methods is None: | |
47 m = _METHOD_IDS_SIZE_RE.match(line) | |
48 if m: | |
49 methods = int(m.group(1)) | |
50 if fields is None: | |
51 raise Exception('"fields_ids_size" not found in dex dump of %s' % dex_path) | |
52 if methods is None: | |
53 raise Exception('"method_ids_size" not found in dex dump of %s' % dex_path) | |
54 return fields, methods | |
jbudorick
2016/06/27 09:21:27
nit: If this stays "SingleMethodAndFieldCount", it
| |
55 | |
56 | |
57 def _MethodAndFieldCount(dexfile): | |
43 tmpdir = tempfile.mkdtemp(suffix='_dex_extract') | 58 tmpdir = tempfile.mkdtemp(suffix='_dex_extract') |
44 multidex_file_list = ExtractIfZip(dexfile, tmpdir) | 59 multidex_file_list = _ExtractIfZip(dexfile, tmpdir) |
45 try: | 60 try: |
46 return sum(int(SingleMethodCount(d)) for d in multidex_file_list) | 61 total_fields = 0 |
62 total_methods = 0 | |
63 for path in multidex_file_list: | |
64 f, m = _SingleMethodAndFieldCount(path) | |
65 total_fields += f | |
66 total_methods += m | |
67 return total_fields, total_methods | |
jbudorick
2016/06/27 09:21:26
nit: same
| |
47 finally: | 68 finally: |
48 shutil.rmtree(tmpdir) | 69 shutil.rmtree(tmpdir) |
49 | 70 |
71 | |
50 def main(): | 72 def main(): |
51 parser = argparse.ArgumentParser() | 73 parser = argparse.ArgumentParser() |
52 parser.add_argument( | 74 parser.add_argument( |
53 '--apk-name', help='Name of the APK to which the dexfile corresponds.') | 75 '--apk-name', help='Name of the APK to which the dexfile corresponds.') |
54 parser.add_argument('dexfile') | 76 parser.add_argument('dexfile') |
55 | 77 |
56 args = parser.parse_args() | 78 args = parser.parse_args() |
57 | 79 |
58 devil_chromium.Initialize() | 80 devil_chromium.Initialize() |
59 | 81 |
60 if not args.apk_name: | 82 if not args.apk_name: |
61 dirname, basename = os.path.split(args.dexfile) | 83 dirname, basename = os.path.split(args.dexfile) |
62 while basename: | 84 while basename: |
63 if 'apk' in basename: | 85 if 'apk' in basename: |
64 args.apk_name = basename | 86 args.apk_name = basename |
65 break | 87 break |
66 dirname, basename = os.path.split(dirname) | 88 dirname, basename = os.path.split(dirname) |
67 else: | 89 else: |
68 parser.error( | 90 parser.error( |
69 'Unable to determine apk name from %s, ' | 91 'Unable to determine apk name from %s, ' |
70 'and --apk-name was not provided.' % args.dexfile) | 92 'and --apk-name was not provided.' % args.dexfile) |
71 | 93 |
72 method_count = MethodCount(args.dexfile) | 94 field_count, method_count = _MethodAndFieldCount(args.dexfile) |
95 perf_tests_results_helper.PrintPerfResult( | |
jbudorick
2016/06/27 09:21:26
kinda odd to have method_count.py doing this as we
| |
96 '%s_fields' % args.apk_name, 'total', [field_count], 'fields') | |
73 perf_tests_results_helper.PrintPerfResult( | 97 perf_tests_results_helper.PrintPerfResult( |
74 '%s_methods' % args.apk_name, 'total', [method_count], 'methods') | 98 '%s_methods' % args.apk_name, 'total', [method_count], 'methods') |
75 return 0 | 99 return 0 |
76 | 100 |
77 if __name__ == '__main__': | 101 if __name__ == '__main__': |
78 sys.exit(main()) | 102 sys.exit(main()) |
79 | 103 |
OLD | NEW |