1*6777b538SAndroid Build Coastguard Worker#! /usr/bin/env python3 2*6777b538SAndroid Build Coastguard Worker# Copyright 2015 The Chromium Authors 3*6777b538SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be 4*6777b538SAndroid Build Coastguard Worker# found in the LICENSE file. 5*6777b538SAndroid Build Coastguard Worker 6*6777b538SAndroid Build Coastguard Worker 7*6777b538SAndroid Build Coastguard Workerimport argparse 8*6777b538SAndroid Build Coastguard Workerimport os 9*6777b538SAndroid Build Coastguard Workerimport re 10*6777b538SAndroid Build Coastguard Workerimport zipfile 11*6777b538SAndroid Build Coastguard Worker 12*6777b538SAndroid Build Coastguard Workerfrom pylib.dex import dex_parser 13*6777b538SAndroid Build Coastguard Worker 14*6777b538SAndroid Build Coastguard Worker 15*6777b538SAndroid Build Coastguard Workerclass DexStatsCollector: 16*6777b538SAndroid Build Coastguard Worker """Tracks count of method/field/string/type as well as unique methods.""" 17*6777b538SAndroid Build Coastguard Worker 18*6777b538SAndroid Build Coastguard Worker def __init__(self): 19*6777b538SAndroid Build Coastguard Worker # Signatures of all methods from all seen dex files. 20*6777b538SAndroid Build Coastguard Worker self._unique_methods = set() 21*6777b538SAndroid Build Coastguard Worker # Map of label -> { metric -> count }. 22*6777b538SAndroid Build Coastguard Worker self._counts_by_label = {} 23*6777b538SAndroid Build Coastguard Worker 24*6777b538SAndroid Build Coastguard Worker def _CollectFromDexfile(self, label, dexfile): 25*6777b538SAndroid Build Coastguard Worker assert label not in self._counts_by_label, 'exists: ' + label 26*6777b538SAndroid Build Coastguard Worker self._counts_by_label[label] = { 27*6777b538SAndroid Build Coastguard Worker 'fields': dexfile.header.field_ids_size, 28*6777b538SAndroid Build Coastguard Worker 'methods': dexfile.header.method_ids_size, 29*6777b538SAndroid Build Coastguard Worker 'strings': dexfile.header.string_ids_size, 30*6777b538SAndroid Build Coastguard Worker 'types': dexfile.header.type_ids_size, 31*6777b538SAndroid Build Coastguard Worker } 32*6777b538SAndroid Build Coastguard Worker self._unique_methods.update(dexfile.IterMethodSignatureParts()) 33*6777b538SAndroid Build Coastguard Worker 34*6777b538SAndroid Build Coastguard Worker def CollectFromZip(self, label, path): 35*6777b538SAndroid Build Coastguard Worker """Add dex stats from an .apk/.jar/.aab/.zip.""" 36*6777b538SAndroid Build Coastguard Worker with zipfile.ZipFile(path, 'r') as z: 37*6777b538SAndroid Build Coastguard Worker for subpath in z.namelist(): 38*6777b538SAndroid Build Coastguard Worker if not re.match(r'.*classes\d*\.dex$', subpath): 39*6777b538SAndroid Build Coastguard Worker continue 40*6777b538SAndroid Build Coastguard Worker dexfile = dex_parser.DexFile(bytearray(z.read(subpath))) 41*6777b538SAndroid Build Coastguard Worker self._CollectFromDexfile('{}!{}'.format(label, subpath), dexfile) 42*6777b538SAndroid Build Coastguard Worker 43*6777b538SAndroid Build Coastguard Worker def CollectFromDex(self, label, path): 44*6777b538SAndroid Build Coastguard Worker """Add dex stats from a .dex file.""" 45*6777b538SAndroid Build Coastguard Worker with open(path, 'rb') as f: 46*6777b538SAndroid Build Coastguard Worker dexfile = dex_parser.DexFile(bytearray(f.read())) 47*6777b538SAndroid Build Coastguard Worker self._CollectFromDexfile(label, dexfile) 48*6777b538SAndroid Build Coastguard Worker 49*6777b538SAndroid Build Coastguard Worker def MergeFrom(self, parent_label, other): 50*6777b538SAndroid Build Coastguard Worker """Add dex stats from another DexStatsCollector.""" 51*6777b538SAndroid Build Coastguard Worker # pylint: disable=protected-access 52*6777b538SAndroid Build Coastguard Worker for label, other_counts in other._counts_by_label.items(): 53*6777b538SAndroid Build Coastguard Worker new_label = '{}-{}'.format(parent_label, label) 54*6777b538SAndroid Build Coastguard Worker self._counts_by_label[new_label] = other_counts.copy() 55*6777b538SAndroid Build Coastguard Worker self._unique_methods.update(other._unique_methods) 56*6777b538SAndroid Build Coastguard Worker # pylint: enable=protected-access 57*6777b538SAndroid Build Coastguard Worker 58*6777b538SAndroid Build Coastguard Worker def GetUniqueMethodCount(self): 59*6777b538SAndroid Build Coastguard Worker """Returns total number of unique methods across encountered dex files.""" 60*6777b538SAndroid Build Coastguard Worker return len(self._unique_methods) 61*6777b538SAndroid Build Coastguard Worker 62*6777b538SAndroid Build Coastguard Worker def GetCountsByLabel(self): 63*6777b538SAndroid Build Coastguard Worker """Returns dict of label -> {metric -> count}.""" 64*6777b538SAndroid Build Coastguard Worker return self._counts_by_label 65*6777b538SAndroid Build Coastguard Worker 66*6777b538SAndroid Build Coastguard Worker def GetTotalCounts(self): 67*6777b538SAndroid Build Coastguard Worker """Returns dict of {metric -> count}, where |count| is sum(metric).""" 68*6777b538SAndroid Build Coastguard Worker ret = {} 69*6777b538SAndroid Build Coastguard Worker for metric in ('fields', 'methods', 'strings', 'types'): 70*6777b538SAndroid Build Coastguard Worker ret[metric] = sum(x[metric] for x in self._counts_by_label.values()) 71*6777b538SAndroid Build Coastguard Worker return ret 72*6777b538SAndroid Build Coastguard Worker 73*6777b538SAndroid Build Coastguard Worker def GetDexCacheSize(self, pre_oreo): 74*6777b538SAndroid Build Coastguard Worker """Returns number of bytes of dirty RAM is consumed from all dex files.""" 75*6777b538SAndroid Build Coastguard Worker # Dex Cache was optimized in Android Oreo: 76*6777b538SAndroid Build Coastguard Worker # https://source.android.com/devices/tech/dalvik/improvements#dex-cache-removal 77*6777b538SAndroid Build Coastguard Worker if pre_oreo: 78*6777b538SAndroid Build Coastguard Worker total = sum(self.GetTotalCounts().values()) 79*6777b538SAndroid Build Coastguard Worker else: 80*6777b538SAndroid Build Coastguard Worker total = sum(c['methods'] for c in self._counts_by_label.values()) 81*6777b538SAndroid Build Coastguard Worker return total * 4 # 4 bytes per entry. 82*6777b538SAndroid Build Coastguard Worker 83*6777b538SAndroid Build Coastguard Worker 84*6777b538SAndroid Build Coastguard Workerdef main(): 85*6777b538SAndroid Build Coastguard Worker parser = argparse.ArgumentParser() 86*6777b538SAndroid Build Coastguard Worker parser.add_argument('paths', nargs='+') 87*6777b538SAndroid Build Coastguard Worker args = parser.parse_args() 88*6777b538SAndroid Build Coastguard Worker 89*6777b538SAndroid Build Coastguard Worker collector = DexStatsCollector() 90*6777b538SAndroid Build Coastguard Worker for path in args.paths: 91*6777b538SAndroid Build Coastguard Worker if os.path.splitext(path)[1] in ('.zip', '.apk', '.jar', '.aab'): 92*6777b538SAndroid Build Coastguard Worker collector.CollectFromZip(path, path) 93*6777b538SAndroid Build Coastguard Worker else: 94*6777b538SAndroid Build Coastguard Worker collector.CollectFromDex(path, path) 95*6777b538SAndroid Build Coastguard Worker 96*6777b538SAndroid Build Coastguard Worker counts_by_label = collector.GetCountsByLabel() 97*6777b538SAndroid Build Coastguard Worker for label, counts in sorted(counts_by_label.items()): 98*6777b538SAndroid Build Coastguard Worker print('{}:'.format(label)) 99*6777b538SAndroid Build Coastguard Worker for metric, count in sorted(counts.items()): 100*6777b538SAndroid Build Coastguard Worker print(' {}:'.format(metric), count) 101*6777b538SAndroid Build Coastguard Worker print() 102*6777b538SAndroid Build Coastguard Worker 103*6777b538SAndroid Build Coastguard Worker if len(counts_by_label) > 1: 104*6777b538SAndroid Build Coastguard Worker print('Totals:') 105*6777b538SAndroid Build Coastguard Worker for metric, count in sorted(collector.GetTotalCounts().items()): 106*6777b538SAndroid Build Coastguard Worker print(' {}:'.format(metric), count) 107*6777b538SAndroid Build Coastguard Worker print() 108*6777b538SAndroid Build Coastguard Worker 109*6777b538SAndroid Build Coastguard Worker print('Unique Methods:', collector.GetUniqueMethodCount()) 110*6777b538SAndroid Build Coastguard Worker print('DexCache (Pre-Oreo):', collector.GetDexCacheSize(pre_oreo=True), 111*6777b538SAndroid Build Coastguard Worker 'bytes of dirty memory') 112*6777b538SAndroid Build Coastguard Worker print('DexCache (Oreo+):', collector.GetDexCacheSize(pre_oreo=False), 113*6777b538SAndroid Build Coastguard Worker 'bytes of dirty memory') 114*6777b538SAndroid Build Coastguard Worker 115*6777b538SAndroid Build Coastguard Worker 116*6777b538SAndroid Build Coastguard Workerif __name__ == '__main__': 117*6777b538SAndroid Build Coastguard Worker main() 118