Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: build/android/resource_sizes.py

Issue 2673023003: Reland of Add main lib section sizes to resource_sizes.py. (Closed)
Patch Set: Change sections + print unknown sections Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2011 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 """Prints the size of each given file and optionally computes the size of 6 """Prints the size of each given file and optionally computes the size of
7 libchrome.so without the dependencies added for building with android NDK. 7 libchrome.so without the dependencies added for building with android NDK.
8 Also breaks down the contents of the APK to determine the installed size 8 Also breaks down the contents of the APK to determine the installed size
9 and assign size contributions to different classes of file. 9 and assign size contributions to different classes of file.
10 """ 10 """
11 11
12 import collections 12 import collections
13 import json 13 import json
14 import logging 14 import logging
15 import operator 15 import operator
16 import optparse 16 import optparse
17 import os 17 import os
18 import re 18 import re
19 import shutil
19 import struct 20 import struct
20 import sys 21 import sys
21 import tempfile 22 import tempfile
22 import zipfile 23 import zipfile
23 import zlib 24 import zlib
24 25
25 import devil_chromium 26 import devil_chromium
26 from devil.android.sdk import build_tools 27 from devil.android.sdk import build_tools
27 from devil.utils import cmd_helper 28 from devil.utils import cmd_helper
28 from devil.utils import lazy 29 from devil.utils import lazy
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 'benchmark_name': 'resource_sizes', 93 'benchmark_name': 'resource_sizes',
93 'benchmark_description': 'APK resource size information.', 94 'benchmark_description': 'APK resource size information.',
94 'trace_rerun_options': [], 95 'trace_rerun_options': [],
95 'charts': {} 96 'charts': {}
96 } 97 }
97 _DUMP_STATIC_INITIALIZERS_PATH = os.path.join( 98 _DUMP_STATIC_INITIALIZERS_PATH = os.path.join(
98 host_paths.DIR_SOURCE_ROOT, 'tools', 'linux', 'dump-static-initializers.py') 99 host_paths.DIR_SOURCE_ROOT, 'tools', 'linux', 'dump-static-initializers.py')
99 # Pragma exists when enable_resource_whitelist_generation=true. 100 # Pragma exists when enable_resource_whitelist_generation=true.
100 _RC_HEADER_RE = re.compile( 101 _RC_HEADER_RE = re.compile(
101 r'^#define (?P<name>\w+) (?:_Pragma\(.*?\) )?(?P<id>\d+)$') 102 r'^#define (?P<name>\w+) (?:_Pragma\(.*?\) )?(?P<id>\d+)$')
103 _READELF_SIZES_METRICS = {
104 'text': ['.text'],
105 'data': ['.data', '.rodata', '.data.rel.ro', '.data.rel.ro.local'],
106 'relocations': ['.rel.dyn', '.rel.plt', '.rela.dyn', '.rela.plt'],
107 'unwind': ['.ARM.extab', '.ARM.exidx', '.eh_frame', '.eh_frame_hdr',],
108 'symbols': ['.dynsym', '.dynstr', '.dynamic', '.shstrtab', '.got', '.plt',
109 '.got.plt'],
110 'bss': ['.bss'],
111 'other': ['.hash', '.init_array', '.fini_array', '.comment',
estevenson 2017/02/03 20:00:49 If .hash is a symbol hash table, should it be in s
agrieve 2017/02/03 20:12:47 Yes! Great point!
112 '.note.gnu.gold-version', '.ARM.attributes', '.note.gnu.build-id',
113 '.gnu.version', '.gnu.version_d', '.gnu.version_r', '.interp',
114 '.gcc_except_table']
115 }
116
117
118 def _ExtractMainLibSectionSizesFromApk(apk_path, main_lib_path):
119 tmpdir = tempfile.mkdtemp(suffix='_apk_extract')
120 grouped_section_sizes = collections.defaultdict(int)
121 try:
122 with zipfile.ZipFile(apk_path, 'r') as z:
123 extracted_lib_path = z.extract(main_lib_path, tmpdir)
124 section_sizes = _CreateSectionNameSizeMap(extracted_lib_path)
125
126 for group_name, section_names in _READELF_SIZES_METRICS.iteritems():
127 for section_name in section_names:
128 if section_name in section_sizes:
129 grouped_section_sizes[group_name] += section_sizes.pop(section_name)
130
131 # Group any unknown section headers into the "other" group.
132 for section_header, section_size in section_sizes.iteritems():
133 print "Unknown elf section header:", section_header
agrieve 2017/02/03 20:12:47 Great idea!
134 grouped_section_sizes['other'] += section_size
135
136 return grouped_section_sizes
137 finally:
138 shutil.rmtree(tmpdir)
139
140
141 def _CreateSectionNameSizeMap(so_path):
142 stdout = cmd_helper.GetCmdOutput(['readelf', '-S', '--wide', so_path])
143 section_sizes = {}
144 # Matches [ 2] .hash HASH 00000000006681f0 0001f0 003154 04 A 3 0 8
145 for match in re.finditer(r'\[[\s\d]+\] (\..*)$', stdout, re.MULTILINE):
146 items = match.group(1).split()
147 section_sizes[items[0]] = int(items[4], 16)
148
149 return section_sizes
102 150
103 151
104 def CountStaticInitializers(so_path): 152 def CountStaticInitializers(so_path):
105 # Static initializers expected in official builds. Note that this list is 153 # Static initializers expected in official builds. Note that this list is
106 # built using 'nm' on libchrome.so which results from a GCC official build 154 # built using 'nm' on libchrome.so which results from a GCC official build
107 # (i.e. Clang is not supported currently). 155 # (i.e. Clang is not supported currently).
108 def get_elf_section_size(readelf_stdout, section_name): 156 def get_elf_section_size(readelf_stdout, section_name):
109 # Matches: .ctors PROGBITS 000000000516add0 5169dd0 000010 00 WA 0 0 8 157 # Matches: .ctors PROGBITS 000000000516add0 5169dd0 000010 00 WA 0 0 8
110 match = re.search(r'\.%s.*$' % re.escape(section_name), 158 match = re.search(r'\.%s.*$' % re.escape(section_name),
111 readelf_stdout, re.MULTILINE) 159 readelf_stdout, re.MULTILINE)
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 # Size of main .so vs remaining. 406 # Size of main .so vs remaining.
359 main_lib_info = native_code.FindLargest() 407 main_lib_info = native_code.FindLargest()
360 if main_lib_info: 408 if main_lib_info:
361 main_lib_size = main_lib_info.file_size 409 main_lib_size = main_lib_info.file_size
362 ReportPerfResult(chartjson, apk_basename + '_Specifics', 410 ReportPerfResult(chartjson, apk_basename + '_Specifics',
363 'main lib size', main_lib_size, 'bytes') 411 'main lib size', main_lib_size, 'bytes')
364 secondary_size = native_code.ComputeUncompressedSize() - main_lib_size 412 secondary_size = native_code.ComputeUncompressedSize() - main_lib_size
365 ReportPerfResult(chartjson, apk_basename + '_Specifics', 413 ReportPerfResult(chartjson, apk_basename + '_Specifics',
366 'other lib size', secondary_size, 'bytes') 414 'other lib size', secondary_size, 'bytes')
367 415
416 main_lib_section_sizes = _ExtractMainLibSectionSizesFromApk(
417 apk_filename, main_lib_info.filename)
418 for metric_name, size in main_lib_section_sizes.iteritems():
419 ReportPerfResult(chartjson, apk_basename + '_MainLibInfo',
420 metric_name, size, 'bytes')
421
368 # Main metric that we want to monitor for jumps. 422 # Main metric that we want to monitor for jumps.
369 normalized_apk_size = total_apk_size 423 normalized_apk_size = total_apk_size
370 # Always look at uncompressed .dex & .so. 424 # Always look at uncompressed .dex & .so.
371 normalized_apk_size -= java_code.ComputeZippedSize() 425 normalized_apk_size -= java_code.ComputeZippedSize()
372 normalized_apk_size += java_code.ComputeUncompressedSize() 426 normalized_apk_size += java_code.ComputeUncompressedSize()
373 normalized_apk_size -= native_code.ComputeZippedSize() 427 normalized_apk_size -= native_code.ComputeZippedSize()
374 normalized_apk_size += native_code.ComputeUncompressedSize() 428 normalized_apk_size += native_code.ComputeUncompressedSize()
375 # Avoid noise caused when strings change and translations haven't yet been 429 # Avoid noise caused when strings change and translations haven't yet been
376 # updated. 430 # updated.
377 english_pak = translations.FindByPattern(r'.*/en[-_][Uu][Ss]\.l?pak') 431 english_pak = translations.FindByPattern(r'.*/en[-_][Uu][Ss]\.l?pak')
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
657 711
658 if chartjson: 712 if chartjson:
659 results_path = os.path.join(options.output_dir, 'results-chart.json') 713 results_path = os.path.join(options.output_dir, 'results-chart.json')
660 logging.critical('Dumping json to %s', results_path) 714 logging.critical('Dumping json to %s', results_path)
661 with open(results_path, 'w') as json_file: 715 with open(results_path, 'w') as json_file:
662 json.dump(chartjson, json_file) 716 json.dump(chartjson, json_file)
663 717
664 718
665 if __name__ == '__main__': 719 if __name__ == '__main__':
666 sys.exit(main(sys.argv)) 720 sys.exit(main(sys.argv))
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698