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 v46.0 38TRACECONV_MANIFEST = [{ 39 'arch': 40 'mac-amd64', 41 'file_name': 42 'traceconv', 43 'file_size': 44 8743272, 45 'url': 46 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v46.0/mac-amd64/traceconv', 47 'sha256': 48 'e0abc72fc69d3be68f16b198253ea9a802c266f7c2609debe561976b627d5d67', 49 'platform': 50 'darwin', 51 'machine': ['x86_64'] 52}, { 53 'arch': 54 'mac-arm64', 55 'file_name': 56 'traceconv', 57 'file_size': 58 8158456, 59 'url': 60 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v46.0/mac-arm64/traceconv', 61 'sha256': 62 '9cd1635898536bcdcff8b944b05bf2ffc84fc0c55ba270746203a4fa0bae9f01', 63 'platform': 64 'darwin', 65 'machine': ['arm64'] 66}, { 67 'arch': 68 'linux-amd64', 69 'file_name': 70 'traceconv', 71 'file_size': 72 8809008, 73 'url': 74 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v46.0/linux-amd64/traceconv', 75 'sha256': 76 '63e8599c19125db25bb1d1d39b0bba6b30ea30760ebfce87d0d9508458cad8ba', 77 'platform': 78 'linux', 79 'machine': ['x86_64'] 80}, { 81 'arch': 82 'linux-arm', 83 'file_name': 84 'traceconv', 85 'file_size': 86 6665536, 87 'url': 88 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v46.0/linux-arm/traceconv', 89 'sha256': 90 'c60d071015bed4da8b9411727e5c35c8429dd4698c92533bea82bf927f1f6966', 91 'platform': 92 'linux', 93 'machine': ['armv6l', 'armv7l', 'armv8l'] 94}, { 95 'arch': 96 'linux-arm64', 97 'file_name': 98 'traceconv', 99 'file_size': 100 8477496, 101 'url': 102 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v46.0/linux-arm64/traceconv', 103 'sha256': 104 'd675922ce9900d4f50105ea8bfcd1edd7fc92ffc56c3e0e7f86bf2af41c5b3db', 105 'platform': 106 'linux', 107 'machine': ['aarch64'] 108}, { 109 'arch': 110 'android-arm', 111 'file_name': 112 'traceconv', 113 'file_size': 114 6680736, 115 'url': 116 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v46.0/android-arm/traceconv', 117 'sha256': 118 '7148b59e5328ab3d6200d52ceb7dd97fe4d82e873dfabd2da3f2f5be54e8e726' 119}, { 120 'arch': 121 'android-arm64', 122 'file_name': 123 'traceconv', 124 'file_size': 125 8424248, 126 'url': 127 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v46.0/android-arm64/traceconv', 128 'sha256': 129 'fc7830c47a662a38cee0a11b3c0e9959cffcd4b9b6260e38ef45284371f6e4df' 130}, { 131 'arch': 132 'android-x86', 133 'file_name': 134 'traceconv', 135 'file_size': 136 9115416, 137 'url': 138 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v46.0/android-x86/traceconv', 139 'sha256': 140 '4f7829506c6bcb249615b87aa40440e45824d4ce378c5f086c317e55241b9b7c' 141}, { 142 'arch': 143 'android-x64', 144 'file_name': 145 'traceconv', 146 'file_size': 147 8657048, 148 'url': 149 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v46.0/android-x64/traceconv', 150 'sha256': 151 '1799a71ad7a743989456ad50b25fdfc01324d4a8f3d9eb97e0799494a3d7ed1c' 152}, { 153 'arch': 154 'windows-amd64', 155 'file_name': 156 'traceconv.exe', 157 'file_size': 158 8527872, 159 'url': 160 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v46.0/windows-amd64/traceconv.exe', 161 'sha256': 162 '9e7afe063caefe8ae5164014d0180933c66688f0af6618ed3ac1179454d2c23c', 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() 426 parser.add_argument( 427 "-i", 428 "--interval", 429 help="Sampling interval. " 430 "Default 4096 (4KiB)", 431 type=int, 432 default=4096) 433 parser.add_argument( 434 "-d", 435 "--duration", 436 help="Duration of profile (ms). 0 to run until interrupted. " 437 "Default: until interrupted by user.", 438 type=int, 439 default=0) 440 # This flag is a no-op now. We never start heapprofd explicitly using system 441 # properties. 442 parser.add_argument( 443 "--no-start", help="Do not start heapprofd.", action='store_true') 444 parser.add_argument( 445 "-p", 446 "--pid", 447 help="Comma-separated list of PIDs to " 448 "profile.", 449 metavar="PIDS") 450 parser.add_argument( 451 "-n", 452 "--name", 453 help="Comma-separated list of process " 454 "names to profile.", 455 metavar="NAMES") 456 parser.add_argument( 457 "-c", 458 "--continuous-dump", 459 help="Dump interval in ms. 0 to disable continuous dump.", 460 type=int, 461 default=0) 462 parser.add_argument( 463 "--heaps", 464 help="Comma-separated list of heaps to collect, e.g: malloc,art. " 465 "Requires Android 12.", 466 metavar="HEAPS") 467 parser.add_argument( 468 "--all-heaps", 469 action="store_true", 470 help="Collect allocations from all heaps registered by target.") 471 parser.add_argument( 472 "--no-android-tree-symbolization", 473 action="store_true", 474 help="Do not symbolize using currently lunched target in the " 475 "Android tree.") 476 parser.add_argument( 477 "--disable-selinux", 478 action="store_true", 479 help="Disable SELinux enforcement for duration of " 480 "profile.") 481 parser.add_argument( 482 "--no-versions", 483 action="store_true", 484 help="Do not get version information about APKs.") 485 parser.add_argument( 486 "--no-running", 487 action="store_true", 488 help="Do not target already running processes. Requires Android 11.") 489 parser.add_argument( 490 "--no-startup", 491 action="store_true", 492 help="Do not target processes that start during " 493 "the profile. Requires Android 11.") 494 parser.add_argument( 495 "--shmem-size", 496 help="Size of buffer between client and " 497 "heapprofd. Default 8MiB. Needs to be a power of two " 498 "multiple of 4096, at least 8192.", 499 type=int, 500 default=8 * 1048576) 501 parser.add_argument( 502 "--block-client", 503 help="When buffer is full, block the " 504 "client to wait for buffer space. Use with caution as " 505 "this can significantly slow down the client. " 506 "This is the default", 507 action="store_true") 508 parser.add_argument( 509 "--block-client-timeout", 510 help="If --block-client is given, do not block any allocation for " 511 "longer than this timeout (us).", 512 type=int) 513 parser.add_argument( 514 "--no-block-client", 515 help="When buffer is full, stop the " 516 "profile early.", 517 action="store_true") 518 parser.add_argument( 519 "--idle-allocations", 520 help="Keep track of how many " 521 "bytes were unused since the last dump, per " 522 "callstack", 523 action="store_true") 524 parser.add_argument( 525 "--dump-at-max", 526 help="Dump the maximum memory usage " 527 "rather than at the time of the dump.", 528 action="store_true") 529 parser.add_argument( 530 "--disable-fork-teardown", 531 help="Do not tear down client in forks. This can be useful for programs " 532 "that use vfork. Android 11+ only.", 533 action="store_true") 534 parser.add_argument( 535 "--simpleperf", 536 action="store_true", 537 help="Get simpleperf profile of heapprofd. This is " 538 "only for heapprofd development.") 539 parser.add_argument( 540 "--traceconv-binary", help="Path to local trace to text. For debugging.") 541 parser.add_argument( 542 "--no-annotations", 543 help="Do not suffix the pprof function names with Android ART mode " 544 "annotations such as [jit].", 545 action="store_true") 546 parser.add_argument( 547 "--print-config", 548 action="store_true", 549 help="Print config instead of running. For debugging.") 550 parser.add_argument( 551 "-o", 552 "--output", 553 help="Output directory.", 554 metavar="DIRECTORY", 555 default=None) 556 parser.add_argument( 557 "--print-options", action="store_true", help=argparse.SUPPRESS) 558 559 args = parser.parse_args() 560 if args.print_options: 561 print_options(parser) 562 return 0 563 fail = False 564 if args.block_client and args.no_block_client: 565 print( 566 "FATAL: Both block-client and no-block-client given.", file=sys.stderr) 567 fail = True 568 if args.pid is None and args.name is None: 569 print("FATAL: Neither PID nor NAME given.", file=sys.stderr) 570 fail = True 571 if args.duration is None: 572 print("FATAL: No duration given.", file=sys.stderr) 573 fail = True 574 if args.interval is None: 575 print("FATAL: No interval given.", file=sys.stderr) 576 fail = True 577 if args.shmem_size % 4096: 578 print("FATAL: shmem-size is not a multiple of 4096.", file=sys.stderr) 579 fail = True 580 if args.shmem_size < 8192: 581 print("FATAL: shmem-size is less than 8192.", file=sys.stderr) 582 fail = True 583 if args.shmem_size & (args.shmem_size - 1): 584 print("FATAL: shmem-size is not a power of two.", file=sys.stderr) 585 fail = True 586 587 target_cfg = "" 588 if not args.no_block_client: 589 target_cfg += CFG_INDENT + "block_client: true\n" 590 if args.block_client_timeout: 591 target_cfg += ( 592 CFG_INDENT + 593 "block_client_timeout_us: %s\n" % args.block_client_timeout) 594 if args.no_startup: 595 target_cfg += CFG_INDENT + "no_startup: true\n" 596 if args.no_running: 597 target_cfg += CFG_INDENT + "no_running: true\n" 598 if args.dump_at_max: 599 target_cfg += CFG_INDENT + "dump_at_max: true\n" 600 if args.disable_fork_teardown: 601 target_cfg += CFG_INDENT + "disable_fork_teardown: true\n" 602 if args.all_heaps: 603 target_cfg += CFG_INDENT + "all_heaps: true\n" 604 if args.pid: 605 for pid in args.pid.split(','): 606 try: 607 pid = int(pid) 608 except ValueError: 609 print("FATAL: invalid PID %s" % pid, file=sys.stderr) 610 fail = True 611 target_cfg += CFG_INDENT + 'pid: {}\n'.format(pid) 612 if args.name: 613 for name in args.name.split(','): 614 target_cfg += CFG_INDENT + 'process_cmdline: "{}"\n'.format(name) 615 if args.heaps: 616 for heap in args.heaps.split(','): 617 target_cfg += CFG_INDENT + 'heaps: "{}"\n'.format(heap) 618 619 if fail: 620 parser.print_help() 621 return 1 622 623 traceconv_binary = args.traceconv_binary 624 625 if args.continuous_dump: 626 target_cfg += CONTINUOUS_DUMP.format(dump_interval=args.continuous_dump) 627 cfg = CFG.format( 628 interval=args.interval, 629 duration=args.duration, 630 target_cfg=target_cfg, 631 shmem_size=args.shmem_size) 632 if not args.no_versions: 633 cfg += PACKAGES_LIST_CFG 634 635 if args.print_config: 636 print(cfg) 637 return 0 638 639 # Do this AFTER print_config so we do not download traceconv only to 640 # print out the config. 641 if traceconv_binary is None: 642 traceconv_binary = get_perfetto_prebuilt(TRACECONV_MANIFEST, soft_fail=True) 643 644 known_issues = maybe_known_issues() 645 if known_issues: 646 print('If you are experiencing problems, please see the known issues for ' 647 'your release: {}.'.format(known_issues)) 648 649 # TODO(fmayer): Maybe feature detect whether we can remove traces instead of 650 # this. 651 uuid_trace = release_or_newer('R') 652 if uuid_trace: 653 profile_device_path = '/data/misc/perfetto-traces/profile-' + UUID 654 else: 655 user = subprocess.check_output(['adb', 'shell', 656 'whoami']).decode('utf-8').strip() 657 profile_device_path = '/data/misc/perfetto-traces/profile-' + user 658 659 perfetto_cmd = ('CFG=\'{cfg}\'; echo ${{CFG}} | ' 660 'perfetto --txt -c - -o ' + profile_device_path + ' -d') 661 662 if args.disable_selinux: 663 enforcing = subprocess.check_output(['adb', 'shell', 664 'getenforce']).decode('utf-8').strip() 665 atexit.register( 666 subprocess.check_call, 667 ['adb', 'shell', 'su root setenforce %s' % enforcing]) 668 subprocess.check_call(['adb', 'shell', 'su root setenforce 0']) 669 670 if args.simpleperf: 671 subprocess.check_call([ 672 'adb', 'shell', 'mkdir -p /data/local/tmp/heapprofd_profile && ' 673 'cd /data/local/tmp/heapprofd_profile &&' 674 '(nohup simpleperf record -g -p $(pidof heapprofd) 2>&1 &) ' 675 '> /dev/null' 676 ]) 677 678 profile_target = PROFILE_LOCAL_PATH 679 if args.output is not None: 680 profile_target = args.output 681 else: 682 os.mkdir(profile_target) 683 684 if not os.path.isdir(profile_target): 685 print( 686 "Output directory {} not found".format(profile_target), file=sys.stderr) 687 return 1 688 689 if os.listdir(profile_target): 690 print( 691 "Output directory {} not empty".format(profile_target), file=sys.stderr) 692 return 1 693 694 perfetto_pid = subprocess.check_output( 695 ['adb', 'exec-out', perfetto_cmd.format(cfg=cfg)]).strip() 696 try: 697 perfetto_pid = int(perfetto_pid.strip()) 698 except ValueError: 699 print("Failed to invoke perfetto: {}".format(perfetto_pid), file=sys.stderr) 700 return 1 701 702 old_handler = signal.signal(signal.SIGINT, sigint_handler) 703 print("Profiling active. Press Ctrl+C to terminate.") 704 print("You may disconnect your device.") 705 print() 706 exists = True 707 device_connected = True 708 while not device_connected or (exists and not IS_INTERRUPTED): 709 exists = subprocess.call( 710 ['adb', 'shell', '[ -d /proc/{} ]'.format(perfetto_pid)], **NOOUT) == 0 711 device_connected = subprocess.call(['adb', 'shell', 'true'], **NOOUT) == 0 712 time.sleep(1) 713 print("Waiting for profiler shutdown...") 714 signal.signal(signal.SIGINT, old_handler) 715 if IS_INTERRUPTED: 716 # Not check_call because it could have existed in the meantime. 717 subprocess.call(['adb', 'shell', 'kill', '-INT', str(perfetto_pid)]) 718 if args.simpleperf: 719 subprocess.check_call(['adb', 'shell', 'killall', '-INT', 'simpleperf']) 720 print("Waiting for simpleperf to exit.") 721 while subprocess.call( 722 ['adb', 'shell', '[ -f /proc/$(pidof simpleperf)/exe ]'], **NOOUT) == 0: 723 time.sleep(1) 724 subprocess.check_call( 725 ['adb', 'pull', '/data/local/tmp/heapprofd_profile', profile_target]) 726 print("Pulled simpleperf profile to " + profile_target + 727 "/heapprofd_profile") 728 729 # Wait for perfetto cmd to return. 730 while exists: 731 exists = subprocess.call( 732 ['adb', 'shell', '[ -d /proc/{} ]'.format(perfetto_pid)]) == 0 733 time.sleep(1) 734 735 profile_host_path = os.path.join(profile_target, 'raw-trace') 736 subprocess.check_call(['adb', 'pull', profile_device_path, profile_host_path], 737 stdout=NULL) 738 if uuid_trace: 739 subprocess.check_call(['adb', 'shell', 'rm', profile_device_path], 740 stdout=NULL) 741 742 if traceconv_binary is None: 743 print('Wrote profile to {}'.format(profile_host_path)) 744 print( 745 'This file can be opened using the Perfetto UI, https://ui.perfetto.dev' 746 ) 747 return 0 748 749 binary_path = os.getenv('PERFETTO_BINARY_PATH') 750 if not args.no_android_tree_symbolization: 751 product_out = os.getenv('ANDROID_PRODUCT_OUT') 752 if product_out: 753 product_out_symbols = product_out + '/symbols' 754 else: 755 product_out_symbols = None 756 757 if binary_path is None: 758 binary_path = product_out_symbols 759 elif product_out_symbols is not None: 760 binary_path += os.pathsep + product_out_symbols 761 762 trace_file = os.path.join(profile_target, 'raw-trace') 763 concat_files = [trace_file] 764 765 if binary_path is not None: 766 with open(os.path.join(profile_target, 'symbols'), 'w') as fd: 767 ret = subprocess.call([ 768 traceconv_binary, 'symbolize', 769 os.path.join(profile_target, 'raw-trace') 770 ], 771 env=dict( 772 os.environ, PERFETTO_BINARY_PATH=binary_path), 773 stdout=fd) 774 if ret == 0: 775 concat_files.append(os.path.join(profile_target, 'symbols')) 776 else: 777 print("Failed to symbolize. Continuing without symbols.", file=sys.stderr) 778 779 proguard_map = os.getenv('PERFETTO_PROGUARD_MAP') 780 if proguard_map is not None: 781 with open(os.path.join(profile_target, 'deobfuscation-packets'), 'w') as fd: 782 ret = subprocess.call([ 783 traceconv_binary, 'deobfuscate', 784 os.path.join(profile_target, 'raw-trace') 785 ], 786 env=dict( 787 os.environ, PERFETTO_PROGUARD_MAP=proguard_map), 788 stdout=fd) 789 if ret == 0: 790 concat_files.append(os.path.join(profile_target, 'deobfuscation-packets')) 791 else: 792 print( 793 "Failed to deobfuscate. Continuing without deobfuscated.", 794 file=sys.stderr) 795 796 if len(concat_files) > 1: 797 with open(os.path.join(profile_target, 'symbolized-trace'), 'wb') as out: 798 for fn in concat_files: 799 with open(fn, 'rb') as inp: 800 while True: 801 buf = inp.read(4096) 802 if not buf: 803 break 804 out.write(buf) 805 trace_file = os.path.join(profile_target, 'symbolized-trace') 806 807 conversion_args = [traceconv_binary, 'profile'] + ( 808 ['--no-annotations'] if args.no_annotations else []) + [trace_file] 809 traceconv_output = subprocess.check_output(conversion_args) 810 profile_path = None 811 for word in traceconv_output.decode('utf-8').split(): 812 if 'heap_profile-' in word: 813 profile_path = word 814 if profile_path is None: 815 print_no_profile_error() 816 return 1 817 818 profile_files = os.listdir(profile_path) 819 if not profile_files: 820 print_no_profile_error() 821 return 1 822 823 for profile_file in profile_files: 824 shutil.copy(os.path.join(profile_path, profile_file), profile_target) 825 826 symlink_path = None 827 if not sys.platform.startswith('win'): 828 subprocess.check_call( 829 ['gzip'] + [os.path.join(profile_target, x) for x in profile_files]) 830 if args.output is None: 831 symlink_path = os.path.join( 832 os.path.dirname(profile_target), "heap_profile-latest") 833 if os.path.lexists(symlink_path): 834 os.unlink(symlink_path) 835 os.symlink(profile_target, symlink_path) 836 837 if symlink_path is not None: 838 print("Wrote profiles to {} (symlink {})".format(profile_target, 839 symlink_path)) 840 else: 841 print("Wrote profiles to {}".format(profile_target)) 842 843 print("The raw-trace file can be viewed using https://ui.perfetto.dev.") 844 print("The heap_dump.* files can be viewed using pprof/ (Googlers only) " + 845 "or https://www.speedscope.app/.") 846 print("The two above are equivalent. The raw-trace contains the union of " + 847 "all the heap dumps.") 848 849 850if __name__ == '__main__': 851 sys.exit(main(sys.argv)) 852