Florian Mayer | c8aa81c | 2021-04-19 15:16:15 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Florian Mayer | a8ff903 | 2020-03-04 11:31:48 -0800 | [diff] [blame] | 2 | |
| 3 | # Copyright (C) 2020 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | from __future__ import absolute_import |
| 18 | from __future__ import division |
| 19 | from __future__ import print_function |
| 20 | |
| 21 | import argparse |
| 22 | import os |
| 23 | import subprocess |
| 24 | import sys |
| 25 | import tempfile |
| 26 | import time |
Florian Mayer | 0ac0e74 | 2021-05-19 15:46:59 +0100 | [diff] [blame] | 27 | import uuid |
Florian Mayer | a8ff903 | 2020-03-04 11:31:48 -0800 | [diff] [blame] | 28 | |
| 29 | NULL = open(os.devnull) |
| 30 | |
| 31 | PACKAGES_LIST_CFG = '''data_sources { |
| 32 | config { |
| 33 | name: "android.packages_list" |
| 34 | } |
| 35 | } |
| 36 | ''' |
| 37 | |
| 38 | CFG_IDENT = ' ' |
| 39 | CFG = '''buffers {{ |
| 40 | size_kb: 100024 |
| 41 | fill_policy: RING_BUFFER |
| 42 | }} |
| 43 | |
| 44 | data_sources {{ |
| 45 | config {{ |
| 46 | name: "android.java_hprof" |
| 47 | java_hprof_config {{ |
| 48 | {target_cfg} |
| 49 | {continuous_dump_config} |
| 50 | }} |
| 51 | }} |
| 52 | }} |
| 53 | |
Florian Mayer | bab9365 | 2020-09-25 13:58:31 +0100 | [diff] [blame] | 54 | data_source_stop_timeout_ms: {data_source_stop_timeout_ms} |
| 55 | duration_ms: {duration_ms} |
Florian Mayer | a8ff903 | 2020-03-04 11:31:48 -0800 | [diff] [blame] | 56 | ''' |
| 57 | |
| 58 | CONTINUOUS_DUMP = """ |
| 59 | continuous_dump_config {{ |
| 60 | dump_phase_ms: 0 |
| 61 | dump_interval_ms: {dump_interval} |
| 62 | }} |
| 63 | """ |
| 64 | |
Florian Mayer | 0ac0e74 | 2021-05-19 15:46:59 +0100 | [diff] [blame] | 65 | UUID = str(uuid.uuid4())[-6:] |
| 66 | PROFILE_PATH = '/data/misc/perfetto-traces/java-profile-' + UUID |
| 67 | |
Florian Mayer | a8ff903 | 2020-03-04 11:31:48 -0800 | [diff] [blame] | 68 | PERFETTO_CMD = ('CFG=\'{cfg}\'; echo ${{CFG}} | ' |
Florian Mayer | 0ac0e74 | 2021-05-19 15:46:59 +0100 | [diff] [blame] | 69 | 'perfetto --txt -c - -o ' + PROFILE_PATH + ' -d') |
Florian Mayer | a8ff903 | 2020-03-04 11:31:48 -0800 | [diff] [blame] | 70 | |
Florian Mayer | 9a1689e | 2021-05-19 15:47:25 +0100 | [diff] [blame] | 71 | SDK = { |
| 72 | 'S': 31, |
| 73 | } |
| 74 | |
| 75 | def release_or_newer(release): |
| 76 | sdk = int(subprocess.check_output( |
| 77 | ['adb', 'shell', 'getprop', 'ro.system.build.version.sdk'] |
| 78 | ).decode('utf-8').strip()) |
| 79 | if sdk >= SDK[release]: |
| 80 | return True |
| 81 | codename = subprocess.check_output( |
| 82 | ['adb', 'shell', 'getprop', 'ro.build.version.codename'] |
| 83 | ).decode('utf-8').strip() |
| 84 | return codename == release |
Florian Mayer | bab9365 | 2020-09-25 13:58:31 +0100 | [diff] [blame] | 85 | |
Florian Mayer | a8ff903 | 2020-03-04 11:31:48 -0800 | [diff] [blame] | 86 | def main(argv): |
| 87 | parser = argparse.ArgumentParser() |
| 88 | parser.add_argument( |
| 89 | "-o", |
| 90 | "--output", |
| 91 | help="Filename to save profile to.", |
| 92 | metavar="FILE", |
| 93 | default=None) |
| 94 | parser.add_argument( |
| 95 | "-p", |
| 96 | "--pid", |
| 97 | help="Comma-separated list of PIDs to " |
| 98 | "profile.", |
| 99 | metavar="PIDS") |
| 100 | parser.add_argument( |
| 101 | "-n", |
| 102 | "--name", |
| 103 | help="Comma-separated list of process " |
| 104 | "names to profile.", |
| 105 | metavar="NAMES") |
| 106 | parser.add_argument( |
| 107 | "-c", |
| 108 | "--continuous-dump", |
| 109 | help="Dump interval in ms. 0 to disable continuous dump.", |
| 110 | type=int, |
| 111 | default=0) |
| 112 | parser.add_argument( |
| 113 | "--no-versions", |
| 114 | action="store_true", |
| 115 | help="Do not get version information about APKs.") |
| 116 | parser.add_argument( |
Florian Mayer | e3eed3a | 2020-04-04 11:57:30 +0200 | [diff] [blame] | 117 | "--dump-smaps", |
| 118 | action="store_true", |
| 119 | help="Get information about /proc/$PID/smaps of target.") |
| 120 | parser.add_argument( |
Florian Mayer | a8ff903 | 2020-03-04 11:31:48 -0800 | [diff] [blame] | 121 | "--print-config", |
| 122 | action="store_true", |
| 123 | help="Print config instead of running. For debugging.") |
Florian Mayer | bab9365 | 2020-09-25 13:58:31 +0100 | [diff] [blame] | 124 | parser.add_argument( |
Florian Mayer | 70f1e98 | 2020-11-11 15:54:44 +0000 | [diff] [blame] | 125 | "--stop-when-done", |
Florian Mayer | bab9365 | 2020-09-25 13:58:31 +0100 | [diff] [blame] | 126 | action="store_true", |
Florian Mayer | 9a1689e | 2021-05-19 15:47:25 +0100 | [diff] [blame] | 127 | default=None, |
| 128 | help="Use a new method to stop the profile when the dump is done. " |
| 129 | "Previously, we would hardcode a duration. Available and default on S.") |
| 130 | parser.add_argument( |
| 131 | "--no-stop-when-done", |
| 132 | action="store_false", |
| 133 | dest='stop_when_done', |
| 134 | help="Do not use a new method to stop the profile when the dump is done.") |
Florian Mayer | 70f1e98 | 2020-11-11 15:54:44 +0000 | [diff] [blame] | 135 | |
Florian Mayer | a8ff903 | 2020-03-04 11:31:48 -0800 | [diff] [blame] | 136 | args = parser.parse_args() |
| 137 | |
Florian Mayer | 9a1689e | 2021-05-19 15:47:25 +0100 | [diff] [blame] | 138 | if args.stop_when_done is None: |
| 139 | args.stop_when_done = release_or_newer('S') |
| 140 | |
Florian Mayer | a8ff903 | 2020-03-04 11:31:48 -0800 | [diff] [blame] | 141 | fail = False |
| 142 | if args.pid is None and args.name is None: |
| 143 | print("FATAL: Neither PID nor NAME given.", file=sys.stderr) |
| 144 | fail = True |
| 145 | |
| 146 | target_cfg = "" |
| 147 | if args.pid: |
| 148 | for pid in args.pid.split(','): |
| 149 | try: |
| 150 | pid = int(pid) |
| 151 | except ValueError: |
| 152 | print("FATAL: invalid PID %s" % pid, file=sys.stderr) |
| 153 | fail = True |
| 154 | target_cfg += '{}pid: {}\n'.format(CFG_IDENT, pid) |
| 155 | if args.name: |
| 156 | for name in args.name.split(','): |
| 157 | target_cfg += '{}process_cmdline: "{}"\n'.format(CFG_IDENT, name) |
Florian Mayer | e3eed3a | 2020-04-04 11:57:30 +0200 | [diff] [blame] | 158 | if args.dump_smaps: |
| 159 | target_cfg += '{}dump_smaps: true\n'.format(CFG_IDENT) |
Florian Mayer | a8ff903 | 2020-03-04 11:31:48 -0800 | [diff] [blame] | 160 | |
| 161 | if fail: |
| 162 | parser.print_help() |
| 163 | return 1 |
| 164 | |
| 165 | output_file = args.output |
| 166 | if output_file is None: |
| 167 | fd, name = tempfile.mkstemp('profile') |
| 168 | os.close(fd) |
| 169 | output_file = name |
| 170 | |
| 171 | continuous_dump_cfg = "" |
| 172 | if args.continuous_dump: |
| 173 | continuous_dump_cfg = CONTINUOUS_DUMP.format( |
| 174 | dump_interval=args.continuous_dump) |
Florian Mayer | bab9365 | 2020-09-25 13:58:31 +0100 | [diff] [blame] | 175 | |
| 176 | # TODO(fmayer): Once the changes have been in S for long enough, make this |
| 177 | # the default for S+. |
| 178 | if args.stop_when_done: |
| 179 | duration_ms = 1000 |
| 180 | data_source_stop_timeout_ms = 100000 |
| 181 | else: |
| 182 | duration_ms = 20000 |
| 183 | data_source_stop_timeout_ms = 0 |
| 184 | |
Florian Mayer | a8ff903 | 2020-03-04 11:31:48 -0800 | [diff] [blame] | 185 | cfg = CFG.format( |
| 186 | target_cfg=target_cfg, |
Florian Mayer | bab9365 | 2020-09-25 13:58:31 +0100 | [diff] [blame] | 187 | continuous_dump_config=continuous_dump_cfg, |
| 188 | duration_ms=duration_ms, |
| 189 | data_source_stop_timeout_ms=data_source_stop_timeout_ms) |
Florian Mayer | a8ff903 | 2020-03-04 11:31:48 -0800 | [diff] [blame] | 190 | if not args.no_versions: |
| 191 | cfg += PACKAGES_LIST_CFG |
| 192 | |
| 193 | if args.print_config: |
| 194 | print(cfg) |
| 195 | return 0 |
| 196 | |
Florian Mayer | c8aa81c | 2021-04-19 15:16:15 +0100 | [diff] [blame] | 197 | user = subprocess.check_output( |
| 198 | ['adb', 'shell', 'whoami']).strip().decode('utf8') |
Florian Mayer | a8ff903 | 2020-03-04 11:31:48 -0800 | [diff] [blame] | 199 | perfetto_pid = subprocess.check_output( |
| 200 | ['adb', 'exec-out', |
Florian Mayer | c8aa81c | 2021-04-19 15:16:15 +0100 | [diff] [blame] | 201 | PERFETTO_CMD.format(cfg=cfg, user=user)]).strip().decode('utf8') |
Florian Mayer | a8ff903 | 2020-03-04 11:31:48 -0800 | [diff] [blame] | 202 | try: |
| 203 | int(perfetto_pid.strip()) |
| 204 | except ValueError: |
| 205 | print("Failed to invoke perfetto: {}".format(perfetto_pid), file=sys.stderr) |
| 206 | return 1 |
| 207 | |
| 208 | print("Dumping Java Heap.") |
| 209 | exists = True |
| 210 | |
| 211 | # Wait for perfetto cmd to return. |
| 212 | while exists: |
| 213 | exists = subprocess.call( |
| 214 | ['adb', 'shell', '[ -d /proc/{} ]'.format(perfetto_pid)]) == 0 |
| 215 | time.sleep(1) |
| 216 | |
| 217 | subprocess.check_call( |
Florian Mayer | 0ac0e74 | 2021-05-19 15:46:59 +0100 | [diff] [blame] | 218 | ['adb', 'pull', PROFILE_PATH, output_file], stdout=NULL) |
| 219 | |
| 220 | subprocess.check_call( |
| 221 | ['adb', 'shell', 'rm', PROFILE_PATH], stdout=NULL) |
Florian Mayer | a8ff903 | 2020-03-04 11:31:48 -0800 | [diff] [blame] | 222 | |
| 223 | print("Wrote profile to {}".format(output_file)) |
| 224 | print("This can be viewed using https://ui.perfetto.dev.") |
| 225 | |
| 226 | |
| 227 | if __name__ == '__main__': |
| 228 | sys.exit(main(sys.argv)) |