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