1#!/usr/bin/env python3 2# Copyright (C) 2017 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 17# DO NOT EDIT. Auto-generated by tools/gen_amalgamated_python_tools 18# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 19 20from __future__ import absolute_import 21from __future__ import division 22from __future__ import print_function 23 24import argparse 25import atexit 26import os 27import shutil 28import signal 29import subprocess 30import sys 31import tempfile 32import time 33import uuid 34 35 36# ----- Amalgamator: begin of python/perfetto/prebuilts/manifests/traceconv.py 37# This file has been generated by: tools/roll-prebuilts v48.1 38TRACECONV_MANIFEST = [{ 39 'arch': 40 'mac-amd64', 41 'file_name': 42 'traceconv', 43 'file_size': 44 9041560, 45 'url': 46 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v48.1/mac-amd64/traceconv', 47 'sha256': 48 'cec2da5cb771a4812d0b2d15604d5023954d28e0af12e87313da2ab70d26b970', 49 'platform': 50 'darwin', 51 'machine': ['x86_64'] 52}, { 53 'arch': 54 'mac-arm64', 55 'file_name': 56 'traceconv', 57 'file_size': 58 8375512, 59 'url': 60 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v48.1/mac-arm64/traceconv', 61 'sha256': 62 '64e200a58ea9c9f366e1071dd274d0023d1fd14043f75dbba3fe0cc138ff5fc7', 63 'platform': 64 'darwin', 65 'machine': ['arm64'] 66}, { 67 'arch': 68 'linux-amd64', 69 'file_name': 70 'traceconv', 71 'file_size': 72 9134136, 73 'url': 74 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v48.1/linux-amd64/traceconv', 75 'sha256': 76 '87b87e1778367c1e3b99fc77439a28b4911125d2751f9909fd1b51f6bd60b6f4', 77 'platform': 78 'linux', 79 'machine': ['x86_64'] 80}, { 81 'arch': 82 'linux-arm', 83 'file_name': 84 'traceconv', 85 'file_size': 86 6753020, 87 'url': 88 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v48.1/linux-arm/traceconv', 89 'sha256': 90 '804c4e13aca5798731056952d9cb0c6ee58795c03477c69514ccd39703060812', 91 'platform': 92 'linux', 93 'machine': ['armv6l', 'armv7l', 'armv8l'] 94}, { 95 'arch': 96 'linux-arm64', 97 'file_name': 98 'traceconv', 99 'file_size': 100 8740064, 101 'url': 102 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v48.1/linux-arm64/traceconv', 103 'sha256': 104 '0d781886531d11e1d573a1ec5e06376ef139bb479eec38c16c8735821c35b895', 105 'platform': 106 'linux', 107 'machine': ['aarch64'] 108}, { 109 'arch': 110 'android-arm', 111 'file_name': 112 'traceconv', 113 'file_size': 114 6792280, 115 'url': 116 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v48.1/android-arm/traceconv', 117 'sha256': 118 '7d91e4133184a3722a25488edd3692c5a195148eba56621014311d3f85d3fc15' 119}, { 120 'arch': 121 'android-arm64', 122 'file_name': 123 'traceconv', 124 'file_size': 125 8677992, 126 'url': 127 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v48.1/android-arm64/traceconv', 128 'sha256': 129 'c03c4a901ed23f1e20a12c98ce4556353a62bddcd260fb4d797cd29ff6c49a05' 130}, { 131 'arch': 132 'android-x86', 133 'file_name': 134 'traceconv', 135 'file_size': 136 9503704, 137 'url': 138 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v48.1/android-x86/traceconv', 139 'sha256': 140 '704e58a7249de56aadec64d4c0d83bab0821d2c4fd77114a9b71705ff4224539' 141}, { 142 'arch': 143 'android-x64', 144 'file_name': 145 'traceconv', 146 'file_size': 147 8964488, 148 'url': 149 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v48.1/android-x64/traceconv', 150 'sha256': 151 'e4f07836fc2a5fb7cd997a9acc4183af7a06997d1e73aac71021af5114b921bc' 152}, { 153 'arch': 154 'windows-amd64', 155 'file_name': 156 'traceconv.exe', 157 'file_size': 158 8763904, 159 'url': 160 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v48.1/windows-amd64/traceconv.exe', 161 'sha256': 162 '084670ac28ed59a9642782a30e051735c1b7474b8cd569b9bc94c305af68290e', 163 'platform': 164 'win32', 165 'machine': ['amd64'] 166}] 167 168# ----- Amalgamator: end of python/perfetto/prebuilts/manifests/traceconv.py 169 170# ----- Amalgamator: begin of python/perfetto/prebuilts/perfetto_prebuilts.py 171# Copyright (C) 2021 The Android Open Source Project 172# 173# Licensed under the Apache License, Version 2.0 (the "License"); 174# you may not use this file except in compliance with the License. 175# You may obtain a copy of the License at 176# 177# http://www.apache.org/licenses/LICENSE-2.0 178# 179# Unless required by applicable law or agreed to in writing, software 180# distributed under the License is distributed on an "AS IS" BASIS, 181# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 182# See the License for the specific language governing permissions and 183# limitations under the License. 184""" 185Functions to fetch pre-pinned Perfetto prebuilts. 186 187This function is used in different places: 188- Into the //tools/{trace_processor, traceconv} scripts, which are just plain 189 wrappers around executables. 190- Into the //tools/{heap_profiler, record_android_trace} scripts, which contain 191 some other hand-written python code. 192 193The manifest argument looks as follows: 194TRACECONV_MANIFEST = [ 195 { 196 'arch': 'mac-amd64', 197 'file_name': 'traceconv', 198 'file_size': 7087080, 199 'url': https://commondatastorage.googleapis.com/.../trace_to_text', 200 'sha256': 7d957c005b0dc130f5bd855d6cec27e060d38841b320d04840afc569f9087490', 201 'platform': 'darwin', 202 'machine': 'x86_64' 203 }, 204 ... 205] 206 207The intended usage is: 208 209 from perfetto.prebuilts.manifests.traceconv import TRACECONV_MANIFEST 210 bin_path = get_perfetto_prebuilt(TRACECONV_MANIFEST) 211 subprocess.call(bin_path, ...) 212""" 213 214import hashlib 215import os 216import platform 217import random 218import subprocess 219import sys 220 221 222def download_or_get_cached(file_name, url, sha256): 223 """ Downloads a prebuilt or returns a cached version 224 225 The first time this is invoked, it downloads the |url| and caches it into 226 ~/.local/share/perfetto/prebuilts/$tool_name. On subsequent invocations it 227 just runs the cached version. 228 """ 229 dir = os.path.join( 230 os.path.expanduser('~'), '.local', 'share', 'perfetto', 'prebuilts') 231 os.makedirs(dir, exist_ok=True) 232 bin_path = os.path.join(dir, file_name) 233 sha256_path = os.path.join(dir, file_name + '.sha256') 234 needs_download = True 235 236 # Avoid recomputing the SHA-256 on each invocation. The SHA-256 of the last 237 # download is cached into file_name.sha256, just check if that matches. 238 if os.path.exists(bin_path) and os.path.exists(sha256_path): 239 with open(sha256_path, 'rb') as f: 240 digest = f.read().decode() 241 if digest == sha256: 242 needs_download = False 243 244 if needs_download: # The file doesn't exist or the SHA256 doesn't match. 245 # Use a unique random file to guard against concurrent executions. 246 # See https://github.com/google/perfetto/issues/786 . 247 tmp_path = '%s.%d.tmp' % (bin_path, random.randint(0, 100000)) 248 print('Downloading ' + url) 249 subprocess.check_call(['curl', '-f', '-L', '-#', '-o', tmp_path, url]) 250 with open(tmp_path, 'rb') as fd: 251 actual_sha256 = hashlib.sha256(fd.read()).hexdigest() 252 if actual_sha256 != sha256: 253 raise Exception('Checksum mismatch for %s (actual: %s, expected: %s)' % 254 (url, actual_sha256, sha256)) 255 os.chmod(tmp_path, 0o755) 256 os.replace(tmp_path, bin_path) 257 with open(tmp_path, 'w') as f: 258 f.write(sha256) 259 os.replace(tmp_path, sha256_path) 260 return bin_path 261 262 263def get_perfetto_prebuilt(manifest, soft_fail=False, arch=None): 264 """ Downloads the prebuilt, if necessary, and returns its path on disk. """ 265 plat = sys.platform.lower() 266 machine = platform.machine().lower() 267 manifest_entry = None 268 for entry in manifest: 269 # If the caller overrides the arch, just match that (for Android prebuilts). 270 if arch: 271 if entry.get('arch') == arch: 272 manifest_entry = entry 273 break 274 continue 275 # Otherwise guess the local machine arch. 276 if entry.get('platform') == plat and machine in entry.get('machine', []): 277 manifest_entry = entry 278 break 279 if manifest_entry is None: 280 if soft_fail: 281 return None 282 raise Exception( 283 ('No prebuilts available for %s-%s\n' % (plat, machine)) + 284 'See https://perfetto.dev/docs/contributing/build-instructions') 285 286 return download_or_get_cached( 287 file_name=manifest_entry['file_name'], 288 url=manifest_entry['url'], 289 sha256=manifest_entry['sha256']) 290 291 292def run_perfetto_prebuilt(manifest): 293 bin_path = get_perfetto_prebuilt(manifest) 294 if sys.platform.lower() == 'win32': 295 sys.exit(subprocess.check_call([bin_path, *sys.argv[1:]])) 296 os.execv(bin_path, [bin_path] + sys.argv[1:]) 297 298# ----- Amalgamator: end of python/perfetto/prebuilts/perfetto_prebuilts.py 299 300NULL = open(os.devnull) 301NOOUT = { 302 'stdout': NULL, 303 'stderr': NULL, 304} 305 306UUID = str(uuid.uuid4())[-6:] 307 308PACKAGES_LIST_CFG = '''data_sources { 309 config { 310 name: "android.packages_list" 311 } 312} 313''' 314 315CFG_INDENT = ' ' 316CFG = '''buffers {{ 317 size_kb: 63488 318}} 319 320data_sources {{ 321 config {{ 322 name: "android.heapprofd" 323 heapprofd_config {{ 324 shmem_size_bytes: {shmem_size} 325 sampling_interval_bytes: {interval} 326{target_cfg} 327 }} 328 }} 329}} 330 331duration_ms: {duration} 332write_into_file: true 333flush_timeout_ms: 30000 334flush_period_ms: 604800000 335''' 336 337# flush_period_ms of 1 week to suppress trace_processor_shell warning. 338 339CONTINUOUS_DUMP = """ 340 continuous_dump_config {{ 341 dump_phase_ms: 0 342 dump_interval_ms: {dump_interval} 343 }} 344""" 345 346PROFILE_LOCAL_PATH = os.path.join(tempfile.gettempdir(), UUID) 347 348IS_INTERRUPTED = False 349 350 351def sigint_handler(sig, frame): 352 global IS_INTERRUPTED 353 IS_INTERRUPTED = True 354 355 356def print_no_profile_error(): 357 print("No profiles generated", file=sys.stderr) 358 print( 359 "If this is unexpected, check " 360 "https://perfetto.dev/docs/data-sources/native-heap-profiler#troubleshooting.", 361 file=sys.stderr) 362 363 364def known_issues_url(number): 365 return ('https://perfetto.dev/docs/data-sources/native-heap-profiler' 366 '#known-issues-android{}'.format(number)) 367 368 369KNOWN_ISSUES = { 370 '10': known_issues_url(10), 371 'Q': known_issues_url(10), 372 '11': known_issues_url(11), 373 'R': known_issues_url(11), 374} 375 376 377def maybe_known_issues(): 378 release_or_codename = subprocess.check_output( 379 ['adb', 'shell', 'getprop', 380 'ro.build.version.release_or_codename']).decode('utf-8').strip() 381 return KNOWN_ISSUES.get(release_or_codename, None) 382 383 384SDK = { 385 'R': 30, 386} 387 388 389def release_or_newer(release): 390 sdk = int( 391 subprocess.check_output( 392 ['adb', 'shell', 'getprop', 393 'ro.system.build.version.sdk']).decode('utf-8').strip()) 394 if sdk >= SDK[release]: 395 return True 396 codename = subprocess.check_output( 397 ['adb', 'shell', 'getprop', 398 'ro.build.version.codename']).decode('utf-8').strip() 399 return codename == release 400 401 402ORDER = ['-n', '-p', '-i', '-o'] 403 404 405def arg_order(action): 406 result = len(ORDER) 407 for opt in action.option_strings: 408 if opt in ORDER: 409 result = min(ORDER.index(opt), result) 410 return result, action.option_strings[0].strip('-') 411 412 413def print_options(parser): 414 for action in sorted(parser._actions, key=arg_order): 415 if action.help is argparse.SUPPRESS: 416 continue 417 opts = ', '.join('`' + x + '`' for x in action.option_strings) 418 metavar = '' if action.metavar is None else ' _' + action.metavar + '_' 419 print('{}{}'.format(opts, metavar)) 420 print(': {}'.format(action.help)) 421 print() 422 423 424def main(argv): 425 parser = argparse.ArgumentParser(description="""Collect a heap profile 426 427 The PERFETTO_PROGUARD_MAP=packagename=map_filename.txt[:packagename=map_filename.txt...] environment variable can be used to pass proguard deobfuscation maps for different packages""", formatter_class=argparse.RawDescriptionHelpFormatter) 428 429 parser.add_argument( 430 "-i", 431 "--interval", 432 help="Sampling interval. " 433 "Default 4096 (4KiB)", 434 type=int, 435 default=4096) 436 parser.add_argument( 437 "-d", 438 "--duration", 439 help="Duration of profile (ms). 0 to run until interrupted. " 440 "Default: until interrupted by user.", 441 type=int, 442 default=0) 443 # This flag is a no-op now. We never start heapprofd explicitly using system 444 # properties. 445 parser.add_argument( 446 "--no-start", help="Do not start heapprofd.", action='store_true') 447 parser.add_argument( 448 "-p", 449 "--pid", 450 help="Comma-separated list of PIDs to " 451 "profile.", 452 metavar="PIDS") 453 parser.add_argument( 454 "-n", 455 "--name", 456 help="Comma-separated list of process " 457 "names to profile.", 458 metavar="NAMES") 459 parser.add_argument( 460 "-c", 461 "--continuous-dump", 462 help="Dump interval in ms. 0 to disable continuous dump.", 463 type=int, 464 default=0) 465 parser.add_argument( 466 "--heaps", 467 help="Comma-separated list of heaps to collect, e.g: malloc,art. " 468 "Requires Android 12.", 469 metavar="HEAPS") 470 parser.add_argument( 471 "--all-heaps", 472 action="store_true", 473 help="Collect allocations from all heaps registered by target.") 474 parser.add_argument( 475 "--no-android-tree-symbolization", 476 action="store_true", 477 help="Do not symbolize using currently lunched target in the " 478 "Android tree.") 479 parser.add_argument( 480 "--disable-selinux", 481 action="store_true", 482 help="Disable SELinux enforcement for duration of " 483 "profile.") 484 parser.add_argument( 485 "--no-versions", 486 action="store_true", 487 help="Do not get version information about APKs.") 488 parser.add_argument( 489 "--no-running", 490 action="store_true", 491 help="Do not target already running processes. Requires Android 11.") 492 parser.add_argument( 493 "--no-startup", 494 action="store_true", 495 help="Do not target processes that start during " 496 "the profile. Requires Android 11.") 497 parser.add_argument( 498 "--shmem-size", 499 help="Size of buffer between client and " 500 "heapprofd. Default 8MiB. Needs to be a power of two " 501 "multiple of 4096, at least 8192.", 502 type=int, 503 default=8 * 1048576) 504 parser.add_argument( 505 "--block-client", 506 help="When buffer is full, block the " 507 "client to wait for buffer space. Use with caution as " 508 "this can significantly slow down the client. " 509 "This is the default", 510 action="store_true") 511 parser.add_argument( 512 "--block-client-timeout", 513 help="If --block-client is given, do not block any allocation for " 514 "longer than this timeout (us).", 515 type=int) 516 parser.add_argument( 517 "--no-block-client", 518 help="When buffer is full, stop the " 519 "profile early.", 520 action="store_true") 521 parser.add_argument( 522 "--idle-allocations", 523 help="Keep track of how many " 524 "bytes were unused since the last dump, per " 525 "callstack", 526 action="store_true") 527 parser.add_argument( 528 "--dump-at-max", 529 help="Dump the maximum memory usage " 530 "rather than at the time of the dump.", 531 action="store_true") 532 parser.add_argument( 533 "--disable-fork-teardown", 534 help="Do not tear down client in forks. This can be useful for programs " 535 "that use vfork. Android 11+ only.", 536 action="store_true") 537 parser.add_argument( 538 "--simpleperf", 539 action="store_true", 540 help="Get simpleperf profile of heapprofd. This is " 541 "only for heapprofd development.") 542 parser.add_argument( 543 "--traceconv-binary", help="Path to local trace to text. For debugging.") 544 parser.add_argument( 545 "--no-annotations", 546 help="Do not suffix the pprof function names with Android ART mode " 547 "annotations such as [jit].", 548 action="store_true") 549 parser.add_argument( 550 "--print-config", 551 action="store_true", 552 help="Print config instead of running. For debugging.") 553 parser.add_argument( 554 "-o", 555 "--output", 556 help="Output directory.", 557 metavar="DIRECTORY", 558 default=None) 559 parser.add_argument( 560 "--print-options", action="store_true", help=argparse.SUPPRESS) 561 562 args = parser.parse_args() 563 if args.print_options: 564 print_options(parser) 565 return 0 566 fail = False 567 if args.block_client and args.no_block_client: 568 print( 569 "FATAL: Both block-client and no-block-client given.", file=sys.stderr) 570 fail = True 571 if args.pid is None and args.name is None: 572 print("FATAL: Neither PID nor NAME given.", file=sys.stderr) 573 fail = True 574 if args.duration is None: 575 print("FATAL: No duration given.", file=sys.stderr) 576 fail = True 577 if args.interval is None: 578 print("FATAL: No interval given.", file=sys.stderr) 579 fail = True 580 if args.shmem_size % 4096: 581 print("FATAL: shmem-size is not a multiple of 4096.", file=sys.stderr) 582 fail = True 583 if args.shmem_size < 8192: 584 print("FATAL: shmem-size is less than 8192.", file=sys.stderr) 585 fail = True 586 if args.shmem_size & (args.shmem_size - 1): 587 print("FATAL: shmem-size is not a power of two.", file=sys.stderr) 588 fail = True 589 590 target_cfg = "" 591 if not args.no_block_client: 592 target_cfg += CFG_INDENT + "block_client: true\n" 593 if args.block_client_timeout: 594 target_cfg += ( 595 CFG_INDENT + 596 "block_client_timeout_us: %s\n" % args.block_client_timeout) 597 if args.no_startup: 598 target_cfg += CFG_INDENT + "no_startup: true\n" 599 if args.no_running: 600 target_cfg += CFG_INDENT + "no_running: true\n" 601 if args.dump_at_max: 602 target_cfg += CFG_INDENT + "dump_at_max: true\n" 603 if args.disable_fork_teardown: 604 target_cfg += CFG_INDENT + "disable_fork_teardown: true\n" 605 if args.all_heaps: 606 target_cfg += CFG_INDENT + "all_heaps: true\n" 607 if args.pid: 608 for pid in args.pid.split(','): 609 try: 610 pid = int(pid) 611 except ValueError: 612 print("FATAL: invalid PID %s" % pid, file=sys.stderr) 613 fail = True 614 target_cfg += CFG_INDENT + 'pid: {}\n'.format(pid) 615 if args.name: 616 for name in args.name.split(','): 617 target_cfg += CFG_INDENT + 'process_cmdline: "{}"\n'.format(name) 618 if args.heaps: 619 for heap in args.heaps.split(','): 620 target_cfg += CFG_INDENT + 'heaps: "{}"\n'.format(heap) 621 622 if fail: 623 parser.print_help() 624 return 1 625 626 traceconv_binary = args.traceconv_binary 627 628 if args.continuous_dump: 629 target_cfg += CONTINUOUS_DUMP.format(dump_interval=args.continuous_dump) 630 cfg = CFG.format( 631 interval=args.interval, 632 duration=args.duration, 633 target_cfg=target_cfg, 634 shmem_size=args.shmem_size) 635 if not args.no_versions: 636 cfg += PACKAGES_LIST_CFG 637 638 if args.print_config: 639 print(cfg) 640 return 0 641 642 # Do this AFTER print_config so we do not download traceconv only to 643 # print out the config. 644 if traceconv_binary is None: 645 traceconv_binary = get_perfetto_prebuilt(TRACECONV_MANIFEST, soft_fail=True) 646 647 known_issues = maybe_known_issues() 648 if known_issues: 649 print('If you are experiencing problems, please see the known issues for ' 650 'your release: {}.'.format(known_issues)) 651 652 # TODO(fmayer): Maybe feature detect whether we can remove traces instead of 653 # this. 654 uuid_trace = release_or_newer('R') 655 if uuid_trace: 656 profile_device_path = '/data/misc/perfetto-traces/profile-' + UUID 657 else: 658 user = subprocess.check_output(['adb', 'shell', 659 'whoami']).decode('utf-8').strip() 660 profile_device_path = '/data/misc/perfetto-traces/profile-' + user 661 662 perfetto_cmd = ('CFG=\'{cfg}\'; echo ${{CFG}} | ' 663 'perfetto --txt -c - -o ' + profile_device_path + ' -d') 664 665 if args.disable_selinux: 666 enforcing = subprocess.check_output(['adb', 'shell', 667 'getenforce']).decode('utf-8').strip() 668 atexit.register( 669 subprocess.check_call, 670 ['adb', 'shell', 'su root setenforce %s' % enforcing]) 671 subprocess.check_call(['adb', 'shell', 'su root setenforce 0']) 672 673 if args.simpleperf: 674 subprocess.check_call([ 675 'adb', 'shell', 'mkdir -p /data/local/tmp/heapprofd_profile && ' 676 'cd /data/local/tmp/heapprofd_profile &&' 677 '(nohup simpleperf record -g -p $(pidof heapprofd) 2>&1 &) ' 678 '> /dev/null' 679 ]) 680 681 profile_target = PROFILE_LOCAL_PATH 682 if args.output is not None: 683 profile_target = args.output 684 else: 685 os.mkdir(profile_target) 686 687 if not os.path.isdir(profile_target): 688 print( 689 "Output directory {} not found".format(profile_target), file=sys.stderr) 690 return 1 691 692 if os.listdir(profile_target): 693 print( 694 "Output directory {} not empty".format(profile_target), file=sys.stderr) 695 return 1 696 697 perfetto_pid = subprocess.check_output( 698 ['adb', 'exec-out', perfetto_cmd.format(cfg=cfg)]).strip() 699 try: 700 perfetto_pid = int(perfetto_pid.strip()) 701 except ValueError: 702 print("Failed to invoke perfetto: {}".format(perfetto_pid), file=sys.stderr) 703 return 1 704 705 old_handler = signal.signal(signal.SIGINT, sigint_handler) 706 print("Profiling active. Press Ctrl+C to terminate.") 707 print("You may disconnect your device.") 708 print() 709 exists = True 710 device_connected = True 711 while not device_connected or (exists and not IS_INTERRUPTED): 712 exists = subprocess.call( 713 ['adb', 'shell', '[ -d /proc/{} ]'.format(perfetto_pid)], **NOOUT) == 0 714 device_connected = subprocess.call(['adb', 'shell', 'true'], **NOOUT) == 0 715 time.sleep(1) 716 print("Waiting for profiler shutdown...") 717 signal.signal(signal.SIGINT, old_handler) 718 if IS_INTERRUPTED: 719 # Not check_call because it could have existed in the meantime. 720 subprocess.call(['adb', 'shell', 'kill', '-INT', str(perfetto_pid)]) 721 if args.simpleperf: 722 subprocess.check_call(['adb', 'shell', 'killall', '-INT', 'simpleperf']) 723 print("Waiting for simpleperf to exit.") 724 while subprocess.call( 725 ['adb', 'shell', '[ -f /proc/$(pidof simpleperf)/exe ]'], **NOOUT) == 0: 726 time.sleep(1) 727 subprocess.check_call( 728 ['adb', 'pull', '/data/local/tmp/heapprofd_profile', profile_target]) 729 print("Pulled simpleperf profile to " + profile_target + 730 "/heapprofd_profile") 731 732 # Wait for perfetto cmd to return. 733 while exists: 734 exists = subprocess.call( 735 ['adb', 'shell', '[ -d /proc/{} ]'.format(perfetto_pid)]) == 0 736 time.sleep(1) 737 738 profile_host_path = os.path.join(profile_target, 'raw-trace') 739 subprocess.check_call(['adb', 'pull', profile_device_path, profile_host_path], 740 stdout=NULL) 741 if uuid_trace: 742 subprocess.check_call(['adb', 'shell', 'rm', profile_device_path], 743 stdout=NULL) 744 745 if traceconv_binary is None: 746 print('Wrote profile to {}'.format(profile_host_path)) 747 print( 748 'This file can be opened using the Perfetto UI, https://ui.perfetto.dev' 749 ) 750 return 0 751 752 binary_path = os.getenv('PERFETTO_BINARY_PATH') 753 if not args.no_android_tree_symbolization: 754 product_out = os.getenv('ANDROID_PRODUCT_OUT') 755 if product_out: 756 product_out_symbols = product_out + '/symbols' 757 else: 758 product_out_symbols = None 759 760 if binary_path is None: 761 binary_path = product_out_symbols 762 elif product_out_symbols is not None: 763 binary_path += os.pathsep + product_out_symbols 764 765 trace_file = os.path.join(profile_target, 'raw-trace') 766 concat_files = [trace_file] 767 768 if binary_path is not None: 769 with open(os.path.join(profile_target, 'symbols'), 'w') as fd: 770 ret = subprocess.call([ 771 traceconv_binary, 'symbolize', 772 os.path.join(profile_target, 'raw-trace') 773 ], 774 env=dict( 775 os.environ, PERFETTO_BINARY_PATH=binary_path), 776 stdout=fd) 777 if ret == 0: 778 concat_files.append(os.path.join(profile_target, 'symbols')) 779 else: 780 print("Failed to symbolize. Continuing without symbols.", file=sys.stderr) 781 782 proguard_map = os.getenv('PERFETTO_PROGUARD_MAP') 783 if proguard_map is not None: 784 with open(os.path.join(profile_target, 'deobfuscation-packets'), 'w') as fd: 785 ret = subprocess.call([ 786 traceconv_binary, 'deobfuscate', 787 os.path.join(profile_target, 'raw-trace') 788 ], 789 env=dict( 790 os.environ, PERFETTO_PROGUARD_MAP=proguard_map), 791 stdout=fd) 792 if ret == 0: 793 concat_files.append(os.path.join(profile_target, 'deobfuscation-packets')) 794 else: 795 print( 796 "Failed to deobfuscate. Continuing without deobfuscated.", 797 file=sys.stderr) 798 799 if len(concat_files) > 1: 800 with open(os.path.join(profile_target, 'symbolized-trace'), 'wb') as out: 801 for fn in concat_files: 802 with open(fn, 'rb') as inp: 803 while True: 804 buf = inp.read(4096) 805 if not buf: 806 break 807 out.write(buf) 808 trace_file = os.path.join(profile_target, 'symbolized-trace') 809 810 conversion_args = [traceconv_binary, 'profile'] + ( 811 ['--no-annotations'] if args.no_annotations else []) + [trace_file] 812 traceconv_output = subprocess.check_output(conversion_args) 813 profile_path = None 814 for word in traceconv_output.decode('utf-8').split(): 815 if 'heap_profile-' in word: 816 profile_path = word 817 if profile_path is None: 818 print_no_profile_error() 819 return 1 820 821 profile_files = os.listdir(profile_path) 822 if not profile_files: 823 print_no_profile_error() 824 return 1 825 826 for profile_file in profile_files: 827 shutil.copy(os.path.join(profile_path, profile_file), profile_target) 828 829 symlink_path = None 830 if not sys.platform.startswith('win'): 831 subprocess.check_call( 832 ['gzip'] + [os.path.join(profile_target, x) for x in profile_files]) 833 if args.output is None: 834 symlink_path = os.path.join( 835 os.path.dirname(profile_target), "heap_profile-latest") 836 if os.path.lexists(symlink_path): 837 os.unlink(symlink_path) 838 os.symlink(profile_target, symlink_path) 839 840 if symlink_path is not None: 841 print("Wrote profiles to {} (symlink {})".format(profile_target, 842 symlink_path)) 843 else: 844 print("Wrote profiles to {}".format(profile_target)) 845 846 print("The raw-trace file can be viewed using https://ui.perfetto.dev.") 847 print("The heap_dump.* files can be viewed using pprof/ (Googlers only) " + 848 "or https://www.speedscope.app/.") 849 print("The two above are equivalent. The raw-trace contains the union of " + 850 "all the heap dumps.") 851 852 853if __name__ == '__main__': 854 sys.exit(main(sys.argv)) 855