Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 """ |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 # Matches [ 2] .hash HASH 00000000006681f0 0001f0 003154 04 A 3 0 8 | 153 # Matches [ 2] .hash HASH 00000000006681f0 0001f0 003154 04 A 3 0 8 |
| 154 for match in re.finditer(r'\[[\s\d]+\] (\..*)$', stdout, re.MULTILINE): | 154 for match in re.finditer(r'\[[\s\d]+\] (\..*)$', stdout, re.MULTILINE): |
| 155 items = match.group(1).split() | 155 items = match.group(1).split() |
| 156 section_sizes[items[0]] = int(items[4], 16) | 156 section_sizes[items[0]] = int(items[4], 16) |
| 157 | 157 |
| 158 return section_sizes | 158 return section_sizes |
| 159 | 159 |
| 160 | 160 |
| 161 def _ParseLibBuildId(so_path, tools_prefix): | 161 def _ParseLibBuildId(so_path, tools_prefix): |
| 162 """Returns the Build ID of the given native library.""" | 162 """Returns the Build ID of the given native library.""" |
| 163 stdout = _RunReadelf(so_path, ['n'], tools_prefix) | 163 stdout = _RunReadelf(so_path, ['-n'], tools_prefix) |
| 164 match = re.search(r'Build ID: (\w+)', stdout) | 164 match = re.search(r'Build ID: (\w+)', stdout) |
| 165 return match.group(1) if match else None | 165 return match.group(1) if match else None |
| 166 | 166 |
| 167 | 167 |
| 168 def CountStaticInitializers(so_path, tools_prefix): | 168 def CountStaticInitializers(so_path, tools_prefix): |
| 169 # Static initializers expected in official builds. Note that this list is | 169 # Static initializers expected in official builds. Note that this list is |
| 170 # built using 'nm' on libchrome.so which results from a GCC official build | 170 # built using 'nm' on libchrome.so which results from a GCC official build |
| 171 # (i.e. Clang is not supported currently). | 171 # (i.e. Clang is not supported currently). |
| 172 def get_elf_section_size(readelf_stdout, section_name): | 172 def get_elf_section_size(readelf_stdout, section_name): |
| 173 # Matches: .ctors PROGBITS 000000000516add0 5169dd0 000010 00 WA 0 0 8 | 173 # Matches: .ctors PROGBITS 000000000516add0 5169dd0 000010 00 WA 0 0 8 |
| (...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 587 if i in id_name_map and name != id_name_map[i]: | 587 if i in id_name_map and name != id_name_map[i]: |
| 588 print 'WARNING: Resource ID conflict %s (%s vs %s)' % ( | 588 print 'WARNING: Resource ID conflict %s (%s vs %s)' % ( |
| 589 i, id_name_map[i], name) | 589 i, id_name_map[i], name) |
| 590 id_name_map[i] = name | 590 id_name_map[i] = name |
| 591 id_header_map[i] = os.path.relpath(header, out_dir) | 591 id_header_map[i] = os.path.relpath(header, out_dir) |
| 592 return id_name_map, id_header_map | 592 return id_name_map, id_header_map |
| 593 | 593 |
| 594 | 594 |
| 595 def _PrintStaticInitializersCountFromApk(apk_filename, tools_prefix, | 595 def _PrintStaticInitializersCountFromApk(apk_filename, tools_prefix, |
| 596 chartjson=None): | 596 chartjson=None): |
| 597 print 'Finding static initializers (can take a minute)' | |
| 598 with zipfile.ZipFile(apk_filename) as z: | 597 with zipfile.ZipFile(apk_filename) as z: |
| 599 infolist = z.infolist() | 598 so_files = [f for f in z.infolist() |
| 599 if f.filename.endswith('.so') and f.file_size > 0] | |
| 600 has_64 = any('64' in f.filename for f in so_files) | |
|
agrieve
2017/04/07 19:46:40
would be good to add in some comments saying why w
| |
| 601 files_to_check = [f for f in so_files if not has_64 or '64' in f.filename] | |
| 600 out_dir = constants.GetOutDirectory() | 602 out_dir = constants.GetOutDirectory() |
| 601 si_count = 0 | 603 si_count = 0 |
| 602 for zip_info in infolist: | 604 for so_info in files_to_check: |
| 603 # Check file size to account for placeholder libraries. | 605 lib_name = os.path.basename(so_info.filename).replace('crazy.', '') |
| 604 if zip_info.filename.endswith('.so') and zip_info.file_size > 0: | 606 unstripped_path = os.path.join(out_dir, 'lib.unstripped', lib_name) |
| 605 lib_name = os.path.basename(zip_info.filename).replace('crazy.', '') | 607 if os.path.exists(unstripped_path): |
| 606 unstripped_path = os.path.join(out_dir, 'lib.unstripped', lib_name) | 608 si_count += _PrintStaticInitializersCount( |
| 607 if os.path.exists(unstripped_path): | 609 apk_filename, so_info.filename, unstripped_path, tools_prefix) |
| 608 si_count += _PrintStaticInitializersCount( | 610 else: |
| 609 apk_filename, zip_info.filename, unstripped_path, tools_prefix) | 611 raise Exception('Unstripped .so not found. Looked here: %s', |
| 610 else: | 612 unstripped_path) |
| 611 raise Exception('Unstripped .so not found. Looked here: %s', | |
| 612 unstripped_path) | |
| 613 ReportPerfResult(chartjson, 'StaticInitializersCount', 'count', si_count, | 613 ReportPerfResult(chartjson, 'StaticInitializersCount', 'count', si_count, |
| 614 'count') | 614 'count') |
| 615 | 615 |
| 616 | 616 |
| 617 def _PrintStaticInitializersCount(apk_path, apk_so_name, so_with_symbols_path, | 617 def _PrintStaticInitializersCount(apk_path, apk_so_name, so_with_symbols_path, |
| 618 tools_prefix): | 618 tools_prefix): |
| 619 """Counts the number of static initializers in the given shared library. | 619 """Counts the number of static initializers in the given shared library. |
| 620 Additionally, files for which static initializers were found are printed | 620 Additionally, files for which static initializers were found are printed |
| 621 on the standard output. | 621 on the standard output. |
| 622 | 622 |
| 623 Args: | 623 Args: |
| 624 apk_path: Path to the apk. | 624 apk_path: Path to the apk. |
| 625 apk_so_name: Name of the so. | 625 apk_so_name: Name of the so. |
| 626 so_with_symbols_path: Path to the unstripped libchrome.so file. | 626 so_with_symbols_path: Path to the unstripped libchrome.so file. |
| 627 tools_prefix: Prefix for arch-specific version of binary utility tools. | 627 tools_prefix: Prefix for arch-specific version of binary utility tools. |
| 628 Returns: | 628 Returns: |
| 629 The number of static initializers found. | 629 The number of static initializers found. |
| 630 """ | 630 """ |
| 631 # GetStaticInitializers uses get-static-initializers.py to get a list of all | 631 # This mostly copies infra/scripts/legacy/scripts/slave/chromium/sizes.py. |
| 632 # static initializers. This does not work on all archs (particularly arm). | 632 print 'Finding static initializers in %s (can take a minute)' % apk_so_name |
| 633 # TODO(rnephew): Get rid of warning when crbug.com/585588 is fixed. | |
| 634 with Unzip(apk_path, filename=apk_so_name) as unzipped_so: | 633 with Unzip(apk_path, filename=apk_so_name) as unzipped_so: |
| 635 _VerifyLibBuildIdsMatch(tools_prefix, unzipped_so, so_with_symbols_path) | 634 _VerifyLibBuildIdsMatch(tools_prefix, unzipped_so, so_with_symbols_path) |
| 636 readelf_si_count = CountStaticInitializers(unzipped_so, tools_prefix) | 635 readelf_si_count = CountStaticInitializers(unzipped_so, tools_prefix) |
| 637 sis, dump_si_count = GetStaticInitializers( | 636 sis, dump_si_count = GetStaticInitializers(so_with_symbols_path, tools_prefix) |
| 638 so_with_symbols_path, tools_prefix) | 637 print ('Found %s files with static initializers using readelf\n' |
| 639 if readelf_si_count != dump_si_count: | 638 'Found %s files with static initializers using ' |
| 640 print ('There are %d files with static initializers, but ' | 639 'dump-static-initializers') % (readelf_si_count, dump_si_count) |
| 641 'dump-static-initializers found %d: files' % | |
| 642 (readelf_si_count, dump_si_count)) | |
| 643 else: | |
| 644 print '%s - Found %d files with static initializers:' % ( | |
| 645 os.path.basename(so_with_symbols_path), dump_si_count) | |
| 646 print '\n'.join(sis) | 640 print '\n'.join(sis) |
| 647 | 641 |
| 648 return readelf_si_count | 642 return readelf_si_count |
| 649 | 643 |
| 650 def _FormatBytes(byts): | 644 def _FormatBytes(byts): |
| 651 """Pretty-print a number of bytes.""" | 645 """Pretty-print a number of bytes.""" |
| 652 if byts > 2**20.0: | 646 if byts > 2**20.0: |
| 653 byts /= 2**20.0 | 647 byts /= 2**20.0 |
| 654 return '%.2fm' % byts | 648 return '%.2fm' % byts |
| 655 if byts > 2**10.0: | 649 if byts > 2**10.0: |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 782 args.apk, tools_prefix, chartjson=chartjson) | 776 args.apk, tools_prefix, chartjson=chartjson) |
| 783 if chartjson: | 777 if chartjson: |
| 784 results_path = os.path.join(args.output_dir, 'results-chart.json') | 778 results_path = os.path.join(args.output_dir, 'results-chart.json') |
| 785 logging.critical('Dumping json to %s', results_path) | 779 logging.critical('Dumping json to %s', results_path) |
| 786 with open(results_path, 'w') as json_file: | 780 with open(results_path, 'w') as json_file: |
| 787 json.dump(chartjson, json_file) | 781 json.dump(chartjson, json_file) |
| 788 | 782 |
| 789 | 783 |
| 790 if __name__ == '__main__': | 784 if __name__ == '__main__': |
| 791 sys.exit(main()) | 785 sys.exit(main()) |
| OLD | NEW |