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

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: .hash -> symbols 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', '.hash'],
110 'bss': ['.bss'],
111 'other': ['.init_array', '.fini_array', '.comment', '.note.gnu.gold-version',
112 '.ARM.attributes', '.note.gnu.build-id', '.gnu.version',
113 '.gnu.version_d', '.gnu.version_r', '.interp', '.gcc_except_table']
114 }
115
116
117 def _ExtractMainLibSectionSizesFromApk(apk_path, main_lib_path):
118 tmpdir = tempfile.mkdtemp(suffix='_apk_extract')
119 grouped_section_sizes = collections.defaultdict(int)
120 try:
121 with zipfile.ZipFile(apk_path, 'r') as z:
122 extracted_lib_path = z.extract(main_lib_path, tmpdir)
123 section_sizes = _CreateSectionNameSizeMap(extracted_lib_path)
124
125 for group_name, section_names in _READELF_SIZES_METRICS.iteritems():
126 for section_name in section_names:
127 if section_name in section_sizes:
128 grouped_section_sizes[group_name] += section_sizes.pop(section_name)
129
130 # Group any unknown section headers into the "other" group.
131 for section_header, section_size in section_sizes.iteritems():
132 print "Unknown elf section header:", section_header
133 grouped_section_sizes['other'] += section_size
134
135 return grouped_section_sizes
136 finally:
137 shutil.rmtree(tmpdir)
138
139
140 def _CreateSectionNameSizeMap(so_path):
141 stdout = cmd_helper.GetCmdOutput(['readelf', '-S', '--wide', so_path])
142 section_sizes = {}
143 # Matches [ 2] .hash HASH 00000000006681f0 0001f0 003154 04 A 3 0 8
144 for match in re.finditer(r'\[[\s\d]+\] (\..*)$', stdout, re.MULTILINE):
145 items = match.group(1).split()
146 section_sizes[items[0]] = int(items[4], 16)
147
148 return section_sizes
102 149
103 150
104 def CountStaticInitializers(so_path): 151 def CountStaticInitializers(so_path):
105 # Static initializers expected in official builds. Note that this list is 152 # 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 153 # built using 'nm' on libchrome.so which results from a GCC official build
107 # (i.e. Clang is not supported currently). 154 # (i.e. Clang is not supported currently).
108 def get_elf_section_size(readelf_stdout, section_name): 155 def get_elf_section_size(readelf_stdout, section_name):
109 # Matches: .ctors PROGBITS 000000000516add0 5169dd0 000010 00 WA 0 0 8 156 # Matches: .ctors PROGBITS 000000000516add0 5169dd0 000010 00 WA 0 0 8
110 match = re.search(r'\.%s.*$' % re.escape(section_name), 157 match = re.search(r'\.%s.*$' % re.escape(section_name),
111 readelf_stdout, re.MULTILINE) 158 readelf_stdout, re.MULTILINE)
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 # Size of main .so vs remaining. 405 # Size of main .so vs remaining.
359 main_lib_info = native_code.FindLargest() 406 main_lib_info = native_code.FindLargest()
360 if main_lib_info: 407 if main_lib_info:
361 main_lib_size = main_lib_info.file_size 408 main_lib_size = main_lib_info.file_size
362 ReportPerfResult(chartjson, apk_basename + '_Specifics', 409 ReportPerfResult(chartjson, apk_basename + '_Specifics',
363 'main lib size', main_lib_size, 'bytes') 410 'main lib size', main_lib_size, 'bytes')
364 secondary_size = native_code.ComputeUncompressedSize() - main_lib_size 411 secondary_size = native_code.ComputeUncompressedSize() - main_lib_size
365 ReportPerfResult(chartjson, apk_basename + '_Specifics', 412 ReportPerfResult(chartjson, apk_basename + '_Specifics',
366 'other lib size', secondary_size, 'bytes') 413 'other lib size', secondary_size, 'bytes')
367 414
415 main_lib_section_sizes = _ExtractMainLibSectionSizesFromApk(
416 apk_filename, main_lib_info.filename)
417 for metric_name, size in main_lib_section_sizes.iteritems():
418 ReportPerfResult(chartjson, apk_basename + '_MainLibInfo',
419 metric_name, size, 'bytes')
420
368 # Main metric that we want to monitor for jumps. 421 # Main metric that we want to monitor for jumps.
369 normalized_apk_size = total_apk_size 422 normalized_apk_size = total_apk_size
370 # Always look at uncompressed .dex & .so. 423 # Always look at uncompressed .dex & .so.
371 normalized_apk_size -= java_code.ComputeZippedSize() 424 normalized_apk_size -= java_code.ComputeZippedSize()
372 normalized_apk_size += java_code.ComputeUncompressedSize() 425 normalized_apk_size += java_code.ComputeUncompressedSize()
373 normalized_apk_size -= native_code.ComputeZippedSize() 426 normalized_apk_size -= native_code.ComputeZippedSize()
374 normalized_apk_size += native_code.ComputeUncompressedSize() 427 normalized_apk_size += native_code.ComputeUncompressedSize()
375 # Avoid noise caused when strings change and translations haven't yet been 428 # Avoid noise caused when strings change and translations haven't yet been
376 # updated. 429 # updated.
377 english_pak = translations.FindByPattern(r'.*/en[-_][Uu][Ss]\.l?pak') 430 english_pak = translations.FindByPattern(r'.*/en[-_][Uu][Ss]\.l?pak')
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
657 710
658 if chartjson: 711 if chartjson:
659 results_path = os.path.join(options.output_dir, 'results-chart.json') 712 results_path = os.path.join(options.output_dir, 'results-chart.json')
660 logging.critical('Dumping json to %s', results_path) 713 logging.critical('Dumping json to %s', results_path)
661 with open(results_path, 'w') as json_file: 714 with open(results_path, 'w') as json_file:
662 json.dump(chartjson, json_file) 715 json.dump(chartjson, json_file)
663 716
664 717
665 if __name__ == '__main__': 718 if __name__ == '__main__':
666 sys.exit(main(sys.argv)) 719 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