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 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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)' | 597 print 'Finding static initializers (can take a minute)' |
598 with zipfile.ZipFile(apk_filename) as z: | 598 with zipfile.ZipFile(apk_filename) as z: |
599 infolist = z.infolist() | 599 infolist = sorted(z.infolist(), key=lambda z: z.file_size, reverse=True) |
600 out_dir = constants.GetOutDirectory() | 600 out_dir = constants.GetOutDirectory() |
601 si_count = 0 | 601 si_count = 0 |
602 saw_first_so = False | |
602 for zip_info in infolist: | 603 for zip_info in infolist: |
603 # Check file size to account for placeholder libraries. | 604 # Check file size to account for placeholder libraries. |
604 if zip_info.filename.endswith('.so') and zip_info.file_size > 0: | 605 if zip_info.filename.endswith('.so') and zip_info.file_size > 0: |
605 lib_name = os.path.basename(zip_info.filename).replace('crazy.', '') | 606 lib_name = os.path.basename(zip_info.filename).replace('crazy.', '') |
606 unstripped_path = os.path.join(out_dir, 'lib.unstripped', lib_name) | 607 unstripped_path = os.path.join(out_dir, 'lib.unstripped', lib_name) |
607 if os.path.exists(unstripped_path): | 608 if os.path.exists(unstripped_path): |
609 # Some 64 bit APKS (MonochromePublic.apk, SystemWebView.apk) include | |
610 # 32 bit .so files. Since some bots already perform | |
611 # dump-static-initializers.py checks on the 32 bit unstripped so files, | |
612 # we ignore these when 64 bit .so files are present by only checking the | |
613 # largest .so file (this also avoids the complexity of finding .so files | |
614 # generated with a secondary toolchain). See http://crbug.com/708942. | |
615 check_unstripped, saw_first_so = not saw_first_so, True | |
agrieve
2017/04/06 19:26:46
This will not check the SIs in the crazy linker, w
| |
608 si_count += _PrintStaticInitializersCount( | 616 si_count += _PrintStaticInitializersCount( |
609 apk_filename, zip_info.filename, unstripped_path, tools_prefix) | 617 apk_filename, zip_info.filename, unstripped_path, tools_prefix, |
618 check_unstripped) | |
610 else: | 619 else: |
611 raise Exception('Unstripped .so not found. Looked here: %s', | 620 raise Exception('Unstripped .so not found. Looked here: %s', |
612 unstripped_path) | 621 unstripped_path) |
613 ReportPerfResult(chartjson, 'StaticInitializersCount', 'count', si_count, | 622 ReportPerfResult(chartjson, 'StaticInitializersCount', 'count', si_count, |
614 'count') | 623 'count') |
615 | 624 |
616 | 625 |
617 def _PrintStaticInitializersCount(apk_path, apk_so_name, so_with_symbols_path, | 626 def _PrintStaticInitializersCount(apk_path, apk_so_name, so_with_symbols_path, |
618 tools_prefix): | 627 tools_prefix, check_unstripped): |
619 """Counts the number of static initializers in the given shared library. | 628 """Counts the number of static initializers in the given shared library. |
620 Additionally, files for which static initializers were found are printed | 629 Additionally, files for which static initializers were found are printed |
621 on the standard output. | 630 on the standard output. |
622 | 631 |
623 Args: | 632 Args: |
624 apk_path: Path to the apk. | 633 apk_path: Path to the apk. |
625 apk_so_name: Name of the so. | 634 apk_so_name: Name of the so. |
626 so_with_symbols_path: Path to the unstripped libchrome.so file. | 635 so_with_symbols_path: Path to the unstripped libchrome.so file. |
627 tools_prefix: Prefix for arch-specific version of binary utility tools. | 636 tools_prefix: Prefix for arch-specific version of binary utility tools. |
628 Returns: | 637 Returns: |
629 The number of static initializers found. | 638 The number of static initializers found. |
630 """ | 639 """ |
631 # GetStaticInitializers uses get-static-initializers.py to get a list of all | 640 # GetStaticInitializers uses get-static-initializers.py to get a list of all |
632 # static initializers. This does not work on all archs (particularly arm). | 641 # static initializers. This does not work on all archs (particularly arm). |
633 # TODO(rnephew): Get rid of warning when crbug.com/585588 is fixed. | 642 # TODO(rnephew): Get rid of warning when crbug.com/585588 is fixed. |
634 with Unzip(apk_path, filename=apk_so_name) as unzipped_so: | 643 with Unzip(apk_path, filename=apk_so_name) as unzipped_so: |
635 _VerifyLibBuildIdsMatch(tools_prefix, unzipped_so, so_with_symbols_path) | 644 if check_unstripped: |
645 _VerifyLibBuildIdsMatch(tools_prefix, unzipped_so, so_with_symbols_path) | |
636 readelf_si_count = CountStaticInitializers(unzipped_so, tools_prefix) | 646 readelf_si_count = CountStaticInitializers(unzipped_so, tools_prefix) |
637 sis, dump_si_count = GetStaticInitializers( | 647 |
638 so_with_symbols_path, tools_prefix) | 648 if check_unstripped: |
639 if readelf_si_count != dump_si_count: | 649 sis, dump_si_count = GetStaticInitializers( |
640 print ('There are %d files with static initializers, but ' | 650 so_with_symbols_path, tools_prefix) |
641 'dump-static-initializers found %d: files' % | 651 if readelf_si_count != dump_si_count: |
642 (readelf_si_count, dump_si_count)) | 652 print ('There are %d files with static initializers, but ' |
643 else: | 653 'dump-static-initializers found %d files' % |
644 print '%s - Found %d files with static initializers:' % ( | 654 (readelf_si_count, dump_si_count)) |
645 os.path.basename(so_with_symbols_path), dump_si_count) | 655 else: |
646 print '\n'.join(sis) | 656 print '%s - Found %d files with static initializers:' % ( |
657 os.path.basename(so_with_symbols_path), dump_si_count) | |
658 print '\n'.join(sis) | |
647 | 659 |
648 return readelf_si_count | 660 return readelf_si_count |
649 | 661 |
650 def _FormatBytes(byts): | 662 def _FormatBytes(byts): |
651 """Pretty-print a number of bytes.""" | 663 """Pretty-print a number of bytes.""" |
652 if byts > 2**20.0: | 664 if byts > 2**20.0: |
653 byts /= 2**20.0 | 665 byts /= 2**20.0 |
654 return '%.2fm' % byts | 666 return '%.2fm' % byts |
655 if byts > 2**10.0: | 667 if byts > 2**10.0: |
656 byts /= 2**10.0 | 668 byts /= 2**10.0 |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
782 args.apk, tools_prefix, chartjson=chartjson) | 794 args.apk, tools_prefix, chartjson=chartjson) |
783 if chartjson: | 795 if chartjson: |
784 results_path = os.path.join(args.output_dir, 'results-chart.json') | 796 results_path = os.path.join(args.output_dir, 'results-chart.json') |
785 logging.critical('Dumping json to %s', results_path) | 797 logging.critical('Dumping json to %s', results_path) |
786 with open(results_path, 'w') as json_file: | 798 with open(results_path, 'w') as json_file: |
787 json.dump(chartjson, json_file) | 799 json.dump(chartjson, json_file) |
788 | 800 |
789 | 801 |
790 if __name__ == '__main__': | 802 if __name__ == '__main__': |
791 sys.exit(main()) | 803 sys.exit(main()) |
OLD | NEW |