Commit 876e2e65 authored by agrieve's avatar agrieve Committed by Commit bot

Add more metrics to method_count.py: fields, classes, strings

These might be useful to track as well.

BUG=none

Review-Url: https://codereview.chromium.org/2100703002
Cr-Commit-Position: refs/heads/master@{#402675}
parent b7d6c2f3
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
# found in the LICENSE file. # found in the LICENSE file.
import argparse import argparse
import collections
import os import os
import re import re
import shutil import shutil
...@@ -19,34 +20,62 @@ sys.path.append(os.path.join(host_paths.DIR_SOURCE_ROOT, 'build', 'util', 'lib', ...@@ -19,34 +20,62 @@ sys.path.append(os.path.join(host_paths.DIR_SOURCE_ROOT, 'build', 'util', 'lib',
'common')) 'common'))
import perf_tests_results_helper # pylint: disable=import-error import perf_tests_results_helper # pylint: disable=import-error
# Example dexdump output:
_METHOD_IDS_SIZE_RE = re.compile(r'^method_ids_size +: +(\d+)$') # DEX file header:
# magic : 'dex\n035\0'
def ExtractIfZip(dexfile, tmpdir): # checksum : b664fc68
if not os.path.splitext(dexfile)[1] in ('.zip', '.apk', '.jar'): # signature : ae73...87f1
return [dexfile] # file_size : 4579656
# header_size : 112
with zipfile.ZipFile(dexfile, 'r') as z: # link_size : 0
dex_files = [n for n in z.namelist() if n.endswith('.dex')] # link_off : 0 (0x000000)
z.extractall(tmpdir, dex_files) # string_ids_size : 46148
# string_ids_off : 112 (0x000070)
return [os.path.join(tmpdir, f) for f in dex_files] # type_ids_size : 5730
# type_ids_off : 184704 (0x02d180)
def SingleMethodCount(dexfile): # proto_ids_size : 8289
for line in dexdump.DexDump(dexfile, file_summary=True): # proto_ids_off : 207624 (0x032b08)
m = _METHOD_IDS_SIZE_RE.match(line) # field_ids_size : 17854
# field_ids_off : 307092 (0x04af94)
# method_ids_size : 33699
# method_ids_off : 449924 (0x06dd84)
# class_defs_size : 2616
# class_defs_off : 719516 (0x0afa9c)
# data_size : 3776428
# data_off : 803228 (0x0c419c)
# For what these mean, refer to:
# https://source.android.com/devices/tech/dalvik/dex-format.html
def _ExtractSizesFromDexFile(dex_path):
counts = {}
for line in dexdump.DexDump(dex_path, file_summary=True):
if not line.strip():
return counts
m = re.match(r'([a-z_]+_size) *: (\d+)', line)
if m: if m:
return m.group(1) counts[m.group(1)] = int(m.group(2))
raise Exception('"method_ids_size" not found in dex dump of %s' % dexfile) raise Exception('Unexpected end of output.')
def MethodCount(dexfile):
def _ExtractSizesFromZip(path):
tmpdir = tempfile.mkdtemp(suffix='_dex_extract') tmpdir = tempfile.mkdtemp(suffix='_dex_extract')
multidex_file_list = ExtractIfZip(dexfile, tmpdir)
try: try:
return sum(int(SingleMethodCount(d)) for d in multidex_file_list) counts = collections.defaultdict(int)
with zipfile.ZipFile(path, 'r') as z:
for subpath in z.namelist():
if not subpath.endswith('.dex'):
continue
extracted_path = z.extract(subpath, tmpdir)
cur_counts = _ExtractSizesFromDexFile(extracted_path)
for k in cur_counts:
counts[k] += cur_counts[k]
return dict(counts)
finally: finally:
shutil.rmtree(tmpdir) shutil.rmtree(tmpdir)
def main(): def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument( parser.add_argument(
...@@ -69,9 +98,19 @@ def main(): ...@@ -69,9 +98,19 @@ def main():
'Unable to determine apk name from %s, ' 'Unable to determine apk name from %s, '
'and --apk-name was not provided.' % args.dexfile) 'and --apk-name was not provided.' % args.dexfile)
method_count = MethodCount(args.dexfile) if os.path.splitext(args.dexfile)[1] in ('.zip', '.apk', '.jar'):
perf_tests_results_helper.PrintPerfResult( sizes = _ExtractSizesFromZip(args.dexfile)
'%s_methods' % args.apk_name, 'total', [method_count], 'methods') else:
sizes = _ExtractSizesFromDexFile(args.dexfile)
def print_result(name, value_key):
perf_tests_results_helper.PrintPerfResult(
'%s_%s' % (args.apk_name, name), 'total', [sizes[value_key]], name)
print_result('classes', 'class_defs_size')
print_result('fields', 'field_ids_size')
print_result('methods', 'method_ids_size')
print_result('strings', 'string_ids_size')
return 0 return 0
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment