Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright (C) 2017 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 atexit |
| 23 | import hashlib |
| 24 | import os |
Florian Mayer | 92c80d8 | 2019-09-25 14:00:01 +0100 | [diff] [blame] | 25 | import shutil |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 26 | import signal |
| 27 | import subprocess |
| 28 | import sys |
| 29 | import tempfile |
| 30 | import time |
| 31 | import urllib |
| 32 | |
| 33 | TRACE_TO_TEXT_SHAS = { |
Florian Mayer | 213c8d4 | 2019-12-18 17:10:34 +0000 | [diff] [blame^] | 34 | 'linux': 'cd6d89e0ada48c0d7850206d80c324b912a3db42', |
| 35 | 'mac': 'aadf14ce5988e5d5e283fb816f72bb2bba8cc9e4', |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 36 | } |
| 37 | TRACE_TO_TEXT_PATH = tempfile.gettempdir() |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 38 | TRACE_TO_TEXT_BASE_URL = ('https://storage.googleapis.com/perfetto/') |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 39 | |
Florian Mayer | bd0a62a | 2019-04-10 11:09:21 +0100 | [diff] [blame] | 40 | NULL = open(os.devnull) |
| 41 | NOOUT = { |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 42 | 'stdout': NULL, |
| 43 | 'stderr': NULL, |
Florian Mayer | bd0a62a | 2019-04-10 11:09:21 +0100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 47 | def check_hash(file_name, sha_value): |
| 48 | with open(file_name, 'rb') as fd: |
| 49 | # TODO(fmayer): Chunking. |
| 50 | file_hash = hashlib.sha1(fd.read()).hexdigest() |
| 51 | return file_hash == sha_value |
| 52 | |
| 53 | |
| 54 | def load_trace_to_text(platform): |
| 55 | sha_value = TRACE_TO_TEXT_SHAS[platform] |
| 56 | file_name = 'trace_to_text-' + platform + '-' + sha_value |
| 57 | local_file = os.path.join(TRACE_TO_TEXT_PATH, file_name) |
| 58 | |
| 59 | if os.path.exists(local_file): |
| 60 | if not check_hash(local_file, sha_value): |
| 61 | os.remove(local_file) |
| 62 | else: |
| 63 | return local_file |
| 64 | |
| 65 | url = TRACE_TO_TEXT_BASE_URL + file_name |
| 66 | urllib.urlretrieve(url, local_file) |
| 67 | if not check_hash(local_file, sha_value): |
| 68 | os.remove(local_file) |
| 69 | raise ValueError("Invalid signature.") |
| 70 | os.chmod(local_file, 0o755) |
| 71 | return local_file |
| 72 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 73 | |
| 74 | PACKAGES_LIST_CFG = '''data_sources { |
Florian Mayer | c8b2869 | 2019-05-16 17:03:21 +0100 | [diff] [blame] | 75 | config { |
Florian Mayer | fe4361d | 2019-05-14 11:54:00 +0100 | [diff] [blame] | 76 | name: "android.packages_list" |
Florian Mayer | c8b2869 | 2019-05-16 17:03:21 +0100 | [diff] [blame] | 77 | } |
| 78 | } |
Florian Mayer | fe4361d | 2019-05-14 11:54:00 +0100 | [diff] [blame] | 79 | ''' |
| 80 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 81 | CFG_IDENT = ' ' |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 82 | CFG = '''buffers {{ |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 83 | size_kb: 32768 |
| 84 | }} |
| 85 | |
| 86 | data_sources {{ |
| 87 | config {{ |
| 88 | name: "android.heapprofd" |
| 89 | heapprofd_config {{ |
| 90 | |
Florian Mayer | 91b3c6d | 2019-04-10 13:44:37 -0700 | [diff] [blame] | 91 | shmem_size_bytes: {shmem_size} |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 92 | sampling_interval_bytes: {interval} |
| 93 | {target_cfg} |
Florian Mayer | a8312c7 | 2019-01-31 13:50:22 +0000 | [diff] [blame] | 94 | {continuous_dump_cfg} |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 95 | }} |
| 96 | }} |
| 97 | }} |
| 98 | |
| 99 | duration_ms: {duration} |
Florian Mayer | 2aab316 | 2019-05-03 16:02:30 +0100 | [diff] [blame] | 100 | write_into_file: true |
Florian Mayer | 8261046 | 2019-04-16 10:26:07 +0100 | [diff] [blame] | 101 | flush_timeout_ms: 30000 |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 102 | ''' |
| 103 | |
Florian Mayer | a8312c7 | 2019-01-31 13:50:22 +0000 | [diff] [blame] | 104 | CONTINUOUS_DUMP = """ |
| 105 | continuous_dump_config {{ |
| 106 | dump_phase_ms: 0 |
| 107 | dump_interval_ms: {dump_interval} |
| 108 | }} |
| 109 | """ |
| 110 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 111 | PERFETTO_CMD = ('CFG=\'{cfg}\'; echo ${{CFG}} | ' |
| 112 | 'perfetto --txt -c - -o ' |
| 113 | '/data/misc/perfetto-traces/profile-{user} -d') |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 114 | IS_INTERRUPTED = False |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 115 | |
| 116 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 117 | def sigint_handler(sig, frame): |
| 118 | global IS_INTERRUPTED |
| 119 | IS_INTERRUPTED = True |
| 120 | |
| 121 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 122 | def main(argv): |
| 123 | parser = argparse.ArgumentParser() |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 124 | parser.add_argument( |
| 125 | "-i", |
| 126 | "--interval", |
| 127 | help="Sampling interval. " |
| 128 | "Default 4096 (4KiB)", |
| 129 | type=int, |
| 130 | default=4096) |
| 131 | parser.add_argument( |
| 132 | "-d", |
| 133 | "--duration", |
| 134 | help="Duration of profile (ms). " |
| 135 | "Default 7 days.", |
| 136 | type=int, |
| 137 | default=604800000) |
| 138 | parser.add_argument( |
| 139 | "--no-start", help="Do not start heapprofd.", action='store_true') |
| 140 | parser.add_argument( |
| 141 | "-p", |
| 142 | "--pid", |
| 143 | help="Comma-separated list of PIDs to " |
| 144 | "profile.", |
| 145 | metavar="PIDS") |
| 146 | parser.add_argument( |
| 147 | "-n", |
| 148 | "--name", |
| 149 | help="Comma-separated list of process " |
| 150 | "names to profile.", |
| 151 | metavar="NAMES") |
| 152 | parser.add_argument( |
| 153 | "-c", |
| 154 | "--continuous-dump", |
| 155 | help="Dump interval in ms. 0 to disable continuous dump.", |
| 156 | type=int, |
| 157 | default=0) |
| 158 | parser.add_argument( |
| 159 | "--disable-selinux", |
| 160 | action="store_true", |
| 161 | help="Disable SELinux enforcement for duration of " |
| 162 | "profile.") |
| 163 | parser.add_argument( |
| 164 | "--no-versions", |
| 165 | action="store_true", |
| 166 | help="Do not get version information about APKs.") |
| 167 | parser.add_argument( |
| 168 | "--no-running", |
| 169 | action="store_true", |
| 170 | help="Do not target already running processes.") |
| 171 | parser.add_argument( |
| 172 | "--no-startup", |
| 173 | action="store_true", |
| 174 | help="Do not target processes that start during " |
| 175 | "the profile.") |
| 176 | parser.add_argument( |
| 177 | "--shmem-size", |
| 178 | help="Size of buffer between client and " |
| 179 | "heapprofd. Default 8MiB. Needs to be a power of two " |
| 180 | "multiple of 4096, at least 8192.", |
| 181 | type=int, |
| 182 | default=8 * 1048576) |
| 183 | parser.add_argument( |
| 184 | "--block-client", |
| 185 | help="When buffer is full, block the " |
| 186 | "client to wait for buffer space. Use with caution as " |
| 187 | "this can significantly slow down the client. " |
| 188 | "This is the default", |
| 189 | action="store_true") |
| 190 | parser.add_argument( |
Florian Mayer | e17af21 | 2019-11-13 10:04:03 +0000 | [diff] [blame] | 191 | "--block-client-timeout", |
| 192 | help="If --block-client is given, do not block any allocation for " |
| 193 | "longer than this timeout (us).", |
| 194 | type=int) |
| 195 | parser.add_argument( |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 196 | "--no-block-client", |
| 197 | help="When buffer is full, stop the " |
| 198 | "profile early.", |
| 199 | action="store_true") |
| 200 | parser.add_argument( |
| 201 | "--idle-allocations", |
| 202 | help="Keep track of how many " |
| 203 | "bytes were unused since the last dump, per " |
| 204 | "callstack", |
| 205 | action="store_true") |
| 206 | parser.add_argument( |
| 207 | "--dump-at-max", |
| 208 | help="Dump the maximum memory usage" |
| 209 | "rather than at the time of the dump.", |
| 210 | action="store_true") |
| 211 | parser.add_argument( |
| 212 | "--simpleperf", |
| 213 | action="store_true", |
| 214 | help="Get simpleperf profile of heapprofd. This is " |
| 215 | "only for heapprofd development.") |
| 216 | parser.add_argument( |
| 217 | "--trace-to-text-binary", |
| 218 | help="Path to local trace to text. For debugging.") |
| 219 | parser.add_argument( |
| 220 | "--print-config", |
| 221 | action="store_true", |
| 222 | help="Print config instead of running. For debugging.") |
Florian Mayer | 0eee91b | 2019-05-10 10:36:16 +0100 | [diff] [blame] | 223 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 224 | args = parser.parse_args() |
| 225 | |
| 226 | fail = False |
Florian Mayer | f40dedd | 2019-07-19 13:08:48 +0100 | [diff] [blame] | 227 | if args.block_client and args.no_block_client: |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 228 | print( |
| 229 | "FATAL: Both block-client and no-block-client given.", file=sys.stderr) |
Florian Mayer | f40dedd | 2019-07-19 13:08:48 +0100 | [diff] [blame] | 230 | fail = True |
Florian Mayer | a774cb7 | 2019-04-29 14:20:43 +0100 | [diff] [blame] | 231 | if args.pid is None and args.name is None: |
| 232 | print("FATAL: Neither PID nor NAME given.", file=sys.stderr) |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 233 | fail = True |
| 234 | if args.duration is None: |
| 235 | print("FATAL: No duration given.", file=sys.stderr) |
| 236 | fail = True |
| 237 | if args.interval is None: |
| 238 | print("FATAL: No interval given.", file=sys.stderr) |
| 239 | fail = True |
Florian Mayer | 91b3c6d | 2019-04-10 13:44:37 -0700 | [diff] [blame] | 240 | if args.shmem_size % 4096: |
| 241 | print("FATAL: shmem-size is not a multiple of 4096.", file=sys.stderr) |
| 242 | fail = True |
| 243 | if args.shmem_size < 8192: |
| 244 | print("FATAL: shmem-size is less than 8192.", file=sys.stderr) |
| 245 | fail = True |
| 246 | if args.shmem_size & (args.shmem_size - 1): |
| 247 | print("FATAL: shmem-size is not a power of two.", file=sys.stderr) |
| 248 | fail = True |
Florian Mayer | 0eee91b | 2019-05-10 10:36:16 +0100 | [diff] [blame] | 249 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 250 | target_cfg = "" |
Florian Mayer | f40dedd | 2019-07-19 13:08:48 +0100 | [diff] [blame] | 251 | if not args.no_block_client: |
Florian Mayer | d6bdb6f | 2019-05-03 17:53:58 +0100 | [diff] [blame] | 252 | target_cfg += "block_client: true\n" |
Florian Mayer | e17af21 | 2019-11-13 10:04:03 +0000 | [diff] [blame] | 253 | if args.block_client_timeout: |
| 254 | target_cfg += "block_client_timeout_us: %s\n" % args.block_client_timeout |
Florian Mayer | 7142c7c | 2019-05-20 18:11:41 +0100 | [diff] [blame] | 255 | if args.idle_allocations: |
| 256 | target_cfg += "idle_allocations: true\n" |
Florian Mayer | 400e443 | 2019-05-29 11:53:20 +0100 | [diff] [blame] | 257 | if args.no_startup: |
| 258 | target_cfg += "no_startup: true\n" |
| 259 | if args.no_running: |
| 260 | target_cfg += "no_running: true\n" |
Florian Mayer | 8707d4d | 2019-07-16 11:17:46 +0100 | [diff] [blame] | 261 | if args.dump_at_max: |
| 262 | target_cfg += "dump_at_max: true\n" |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 263 | if args.pid: |
Florian Mayer | 0eee91b | 2019-05-10 10:36:16 +0100 | [diff] [blame] | 264 | for pid in args.pid.split(','): |
| 265 | try: |
| 266 | pid = int(pid) |
| 267 | except ValueError: |
| 268 | print("FATAL: invalid PID %s" % pid, file=sys.stderr) |
| 269 | fail = True |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 270 | target_cfg += '{}pid: {}\n'.format(CFG_IDENT, pid) |
| 271 | if args.name: |
Florian Mayer | 0eee91b | 2019-05-10 10:36:16 +0100 | [diff] [blame] | 272 | for name in args.name.split(','): |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 273 | target_cfg += '{}process_cmdline: "{}"\n'.format(CFG_IDENT, name) |
| 274 | |
Florian Mayer | 0eee91b | 2019-05-10 10:36:16 +0100 | [diff] [blame] | 275 | if fail: |
| 276 | parser.print_help() |
| 277 | return 1 |
| 278 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 279 | trace_to_text_binary = args.trace_to_text_binary |
| 280 | if trace_to_text_binary is None: |
| 281 | platform = None |
| 282 | if sys.platform.startswith('linux'): |
| 283 | platform = 'linux' |
| 284 | elif sys.platform.startswith('darwin'): |
| 285 | platform = 'mac' |
| 286 | else: |
| 287 | print("Invalid platform: {}".format(sys.platform), file=sys.stderr) |
Florian Mayer | b627963 | 2018-11-29 13:31:49 +0000 | [diff] [blame] | 288 | return 1 |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 289 | |
| 290 | trace_to_text_binary = load_trace_to_text(platform) |
| 291 | |
Florian Mayer | a8312c7 | 2019-01-31 13:50:22 +0000 | [diff] [blame] | 292 | continuous_dump_cfg = "" |
| 293 | if args.continuous_dump: |
| 294 | continuous_dump_cfg = CONTINUOUS_DUMP.format( |
| 295 | dump_interval=args.continuous_dump) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 296 | cfg = CFG.format( |
| 297 | interval=args.interval, |
| 298 | duration=args.duration, |
| 299 | target_cfg=target_cfg, |
| 300 | continuous_dump_cfg=continuous_dump_cfg, |
| 301 | shmem_size=args.shmem_size) |
Florian Mayer | fe4361d | 2019-05-14 11:54:00 +0100 | [diff] [blame] | 302 | if not args.no_versions: |
| 303 | cfg += PACKAGES_LIST_CFG |
| 304 | |
| 305 | if args.print_config: |
| 306 | print(cfg) |
| 307 | return 0 |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 308 | |
Florian Mayer | 6ae9526 | 2018-12-06 16:10:29 +0000 | [diff] [blame] | 309 | if args.disable_selinux: |
| 310 | enforcing = subprocess.check_output(['adb', 'shell', 'getenforce']) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 311 | atexit.register( |
| 312 | subprocess.check_call, |
Florian Mayer | 6ae9526 | 2018-12-06 16:10:29 +0000 | [diff] [blame] | 313 | ['adb', 'shell', 'su root setenforce %s' % enforcing]) |
| 314 | subprocess.check_call(['adb', 'shell', 'su root setenforce 0']) |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 315 | |
Florian Mayer | 33ceb8d | 2019-01-11 14:51:28 +0000 | [diff] [blame] | 316 | if not args.no_start: |
Florian Mayer | 75266d5 | 2019-02-01 18:09:43 +0000 | [diff] [blame] | 317 | heapprofd_prop = subprocess.check_output( |
| 318 | ['adb', 'shell', 'getprop persist.heapprofd.enable']) |
| 319 | if heapprofd_prop.strip() != '1': |
| 320 | subprocess.check_call( |
| 321 | ['adb', 'shell', 'setprop persist.heapprofd.enable 1']) |
| 322 | atexit.register(subprocess.check_call, |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 323 | ['adb', 'shell', 'setprop persist.heapprofd.enable 0']) |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 324 | |
Florian Mayer | bb3b682 | 2019-03-08 17:08:59 +0000 | [diff] [blame] | 325 | user = subprocess.check_output(['adb', 'shell', 'whoami']).strip() |
Florian Mayer | 2b8a3b2 | 2019-05-02 18:35:38 +0100 | [diff] [blame] | 326 | |
| 327 | if args.simpleperf: |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 328 | subprocess.check_call([ |
| 329 | 'adb', 'shell', 'mkdir -p /data/local/tmp/heapprofd_profile && ' |
| 330 | 'cd /data/local/tmp/heapprofd_profile &&' |
| 331 | '(nohup simpleperf record -g -p $(pidof heapprofd) 2>&1 &) ' |
| 332 | '> /dev/null' |
| 333 | ]) |
Florian Mayer | 2b8a3b2 | 2019-05-02 18:35:38 +0100 | [diff] [blame] | 334 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 335 | perfetto_pid = subprocess.check_output( |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 336 | ['adb', 'exec-out', |
| 337 | PERFETTO_CMD.format(cfg=cfg, user=user)]).strip() |
Florian Mayer | 3564742 | 2019-03-07 16:28:10 +0000 | [diff] [blame] | 338 | try: |
| 339 | int(perfetto_pid.strip()) |
| 340 | except ValueError: |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 341 | print("Failed to invoke perfetto: {}".format(perfetto_pid), file=sys.stderr) |
Florian Mayer | 3564742 | 2019-03-07 16:28:10 +0000 | [diff] [blame] | 342 | return 1 |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 343 | |
| 344 | old_handler = signal.signal(signal.SIGINT, sigint_handler) |
| 345 | print("Profiling active. Press Ctrl+C to terminate.") |
Florian Mayer | bd0a62a | 2019-04-10 11:09:21 +0100 | [diff] [blame] | 346 | print("You may disconnect your device.") |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 347 | exists = True |
Florian Mayer | bd0a62a | 2019-04-10 11:09:21 +0100 | [diff] [blame] | 348 | device_connected = True |
| 349 | while not device_connected or (exists and not IS_INTERRUPTED): |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 350 | exists = subprocess.call( |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 351 | ['adb', 'shell', '[ -d /proc/{} ]'.format(perfetto_pid)], **NOOUT) == 0 |
Florian Mayer | bd0a62a | 2019-04-10 11:09:21 +0100 | [diff] [blame] | 352 | device_connected = subprocess.call(['adb', 'shell', 'true'], **NOOUT) == 0 |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 353 | time.sleep(1) |
| 354 | signal.signal(signal.SIGINT, old_handler) |
| 355 | if IS_INTERRUPTED: |
| 356 | # Not check_call because it could have existed in the meantime. |
Florian Mayer | 85969b9 | 2019-01-23 17:23:16 +0000 | [diff] [blame] | 357 | subprocess.call(['adb', 'shell', 'kill', '-INT', perfetto_pid]) |
Florian Mayer | 2b8a3b2 | 2019-05-02 18:35:38 +0100 | [diff] [blame] | 358 | if args.simpleperf: |
| 359 | subprocess.check_call(['adb', 'shell', 'killall', '-INT', 'simpleperf']) |
| 360 | print("Waiting for simpleperf to exit.") |
| 361 | while subprocess.call( |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 362 | ['adb', 'shell', '[ -f /proc/$(pidof simpleperf)/exe ]'], **NOOUT) == 0: |
Florian Mayer | 2b8a3b2 | 2019-05-02 18:35:38 +0100 | [diff] [blame] | 363 | time.sleep(1) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 364 | subprocess.check_call( |
| 365 | ['adb', 'pull', '/data/local/tmp/heapprofd_profile', '/tmp']) |
Florian Mayer | 2b8a3b2 | 2019-05-02 18:35:38 +0100 | [diff] [blame] | 366 | print("Pulled simpleperf profile to /tmp/heapprofd_profile") |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 367 | |
Florian Mayer | ddbe31e | 2018-11-30 14:49:30 +0000 | [diff] [blame] | 368 | # Wait for perfetto cmd to return. |
| 369 | while exists: |
| 370 | exists = subprocess.call( |
| 371 | ['adb', 'shell', '[ -d /proc/{} ]'.format(perfetto_pid)]) == 0 |
| 372 | time.sleep(1) |
| 373 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 374 | subprocess.check_call([ |
| 375 | 'adb', 'pull', '/data/misc/perfetto-traces/profile-{}'.format(user), |
| 376 | '/tmp/profile' |
| 377 | ], |
| 378 | stdout=NULL) |
Florian Mayer | 213c8d4 | 2019-12-18 17:10:34 +0000 | [diff] [blame^] | 379 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 380 | trace_to_text_output = subprocess.check_output( |
Florian Mayer | 213c8d4 | 2019-12-18 17:10:34 +0000 | [diff] [blame^] | 381 | [trace_to_text_binary, 'profile', '/tmp/profile'], |
| 382 | env=os.environ) |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 383 | profile_path = None |
| 384 | for word in trace_to_text_output.split(): |
| 385 | if 'heap_profile-' in word: |
| 386 | profile_path = word |
| 387 | if profile_path is None: |
| 388 | print("Could not find trace_to_text output path.", file=sys.stderr) |
| 389 | return 1 |
| 390 | |
| 391 | profile_files = os.listdir(profile_path) |
| 392 | if not profile_files: |
| 393 | print("No profiles generated", file=sys.stderr) |
Matthew Clarkson | 63028d6 | 2019-10-10 15:48:23 +0100 | [diff] [blame] | 394 | print( |
| 395 | "If this is unexpected, check " |
| 396 | "https://docs.perfetto.dev/#/heapprofd?id=troubleshooting.", |
| 397 | file=sys.stderr) |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 398 | return 1 |
| 399 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 400 | subprocess.check_call( |
| 401 | ['gzip'] + |
| 402 | [os.path.join(profile_path, x) for x in os.listdir(profile_path)]) |
Florian Mayer | 82f43d1 | 2019-01-17 14:37:45 +0000 | [diff] [blame] | 403 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 404 | symlink_path = os.path.join( |
| 405 | os.path.dirname(profile_path), "heap_profile-latest") |
Florian Mayer | 91967ee | 2019-05-10 16:43:50 +0100 | [diff] [blame] | 406 | if os.path.lexists(symlink_path): |
Florian Mayer | 3130551 | 2019-01-21 17:37:02 +0000 | [diff] [blame] | 407 | os.unlink(symlink_path) |
Florian Mayer | 82f43d1 | 2019-01-17 14:37:45 +0000 | [diff] [blame] | 408 | os.symlink(profile_path, symlink_path) |
Florian Mayer | 92c80d8 | 2019-09-25 14:00:01 +0100 | [diff] [blame] | 409 | shutil.copyfile('/tmp/profile', os.path.join(profile_path, 'raw-trace')) |
Florian Mayer | 82f43d1 | 2019-01-17 14:37:45 +0000 | [diff] [blame] | 410 | |
Florian Mayer | 213c8d4 | 2019-12-18 17:10:34 +0000 | [diff] [blame^] | 411 | binary_path = os.getenv('PERFETTO_BINARY_PATH') |
| 412 | if binary_path is not None: |
| 413 | with open(os.path.join(profile_path, 'symbols'), 'w') as fd: |
| 414 | ret = subprocess.call([ |
| 415 | trace_to_text_binary, 'symbolize', |
| 416 | os.path.join(profile_path, 'raw-trace')], |
| 417 | env=os.environ, |
| 418 | stdout=fd) |
| 419 | if ret != 0: |
| 420 | print("Failed to symbolize.", file=sys.stderr) |
| 421 | |
Florian Mayer | 82f43d1 | 2019-01-17 14:37:45 +0000 | [diff] [blame] | 422 | print("Wrote profiles to {} (symlink {})".format(profile_path, symlink_path)) |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 423 | print("These can be viewed using pprof. Googlers: head to pprof/ and " |
| 424 | "upload them.") |
| 425 | |
| 426 | |
| 427 | if __name__ == '__main__': |
| 428 | sys.exit(main(sys.argv)) |