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 |
| 25 | import signal |
| 26 | import subprocess |
| 27 | import sys |
| 28 | import tempfile |
| 29 | import time |
| 30 | import urllib |
| 31 | |
| 32 | TRACE_TO_TEXT_SHAS = { |
Florian Mayer | 887948c | 2019-08-07 11:20:30 +0100 | [diff] [blame] | 33 | 'linux': '74cb40d9413d7ad756e327e09e46a8adde90db92', |
| 34 | 'mac': '47045bc6abd4f9113969211f37fdd2259b0c1cc3', |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 35 | } |
| 36 | TRACE_TO_TEXT_PATH = tempfile.gettempdir() |
| 37 | TRACE_TO_TEXT_BASE_URL = ( |
| 38 | 'https://storage.googleapis.com/perfetto/') |
| 39 | |
Florian Mayer | bd0a62a | 2019-04-10 11:09:21 +0100 | [diff] [blame] | 40 | NULL = open(os.devnull) |
| 41 | NOOUT = { |
| 42 | 'stdout': NULL, |
| 43 | 'stderr': NULL, |
| 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 | |
Florian Mayer | c8b2869 | 2019-05-16 17:03:21 +0100 | [diff] [blame] | 73 | PACKAGES_LIST_CFG='''data_sources { |
| 74 | config { |
Florian Mayer | fe4361d | 2019-05-14 11:54:00 +0100 | [diff] [blame] | 75 | name: "android.packages_list" |
Florian Mayer | c8b2869 | 2019-05-16 17:03:21 +0100 | [diff] [blame] | 76 | } |
| 77 | } |
Florian Mayer | fe4361d | 2019-05-14 11:54:00 +0100 | [diff] [blame] | 78 | ''' |
| 79 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 80 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 81 | CFG_IDENT = ' ' |
| 82 | CFG='''buffers {{ |
| 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 | |
Florian Mayer | bb3b682 | 2019-03-08 17:08:59 +0000 | [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 |
| 115 | def sigint_handler(sig, frame): |
| 116 | global IS_INTERRUPTED |
| 117 | IS_INTERRUPTED = True |
| 118 | |
| 119 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 120 | def main(argv): |
| 121 | parser = argparse.ArgumentParser() |
| 122 | parser.add_argument("-i", "--interval", help="Sampling interval. " |
Florian Mayer | 607c1bc | 2019-02-01 11:04:58 +0000 | [diff] [blame] | 123 | "Default 4096 (4KiB)", type=int, default=4096) |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 124 | parser.add_argument("-d", "--duration", help="Duration of profile (ms). " |
Florian Mayer | 0eee91b | 2019-05-10 10:36:16 +0100 | [diff] [blame] | 125 | "Default 7 days.", type=int, default=604800000) |
| 126 | parser.add_argument("--no-start", help="Do not start heapprofd.", |
Florian Mayer | 33ceb8d | 2019-01-11 14:51:28 +0000 | [diff] [blame] | 127 | action='store_true') |
Florian Mayer | 0eee91b | 2019-05-10 10:36:16 +0100 | [diff] [blame] | 128 | parser.add_argument("-p", "--pid", help="Comma-separated list of PIDs to " |
| 129 | "profile.", metavar="PIDS") |
| 130 | parser.add_argument("-n", "--name", help="Comma-separated list of process " |
| 131 | "names to profile.", metavar="NAMES") |
Florian Mayer | a8312c7 | 2019-01-31 13:50:22 +0000 | [diff] [blame] | 132 | parser.add_argument("-c", "--continuous-dump", |
| 133 | help="Dump interval in ms. 0 to disable continuous dump.", |
| 134 | type=int, default=0) |
Florian Mayer | 6ae9526 | 2018-12-06 16:10:29 +0000 | [diff] [blame] | 135 | parser.add_argument("--disable-selinux", action="store_true", |
| 136 | help="Disable SELinux enforcement for duration of " |
Florian Mayer | 0eee91b | 2019-05-10 10:36:16 +0100 | [diff] [blame] | 137 | "profile.") |
Florian Mayer | fe4361d | 2019-05-14 11:54:00 +0100 | [diff] [blame] | 138 | parser.add_argument("--no-versions", action="store_true", |
| 139 | help="Do not get version information about APKs.") |
Florian Mayer | 400e443 | 2019-05-29 11:53:20 +0100 | [diff] [blame] | 140 | parser.add_argument("--no-running", action="store_true", |
| 141 | help="Do not target already running processes.") |
| 142 | parser.add_argument("--no-startup", action="store_true", |
| 143 | help="Do not target processes that start during " |
| 144 | "the profile.") |
Florian Mayer | 91b3c6d | 2019-04-10 13:44:37 -0700 | [diff] [blame] | 145 | parser.add_argument("--shmem-size", help="Size of buffer between client and " |
| 146 | "heapprofd. Default 8MiB. Needs to be a power of two " |
| 147 | "multiple of 4096, at least 8192.", type=int, |
| 148 | default=8 * 1048576) |
Florian Mayer | d6bdb6f | 2019-05-03 17:53:58 +0100 | [diff] [blame] | 149 | parser.add_argument("--block-client", help="When buffer is full, block the " |
Florian Mayer | 1a17b68 | 2019-05-09 12:02:45 +0100 | [diff] [blame] | 150 | "client to wait for buffer space. Use with caution as " |
Florian Mayer | f40dedd | 2019-07-19 13:08:48 +0100 | [diff] [blame] | 151 | "this can significantly slow down the client. " |
| 152 | "This is the default", action="store_true") |
| 153 | parser.add_argument("--no-block-client", help="When buffer is full, stop the " |
| 154 | "profile early.", action="store_true") |
Florian Mayer | 7142c7c | 2019-05-20 18:11:41 +0100 | [diff] [blame] | 155 | parser.add_argument("--idle-allocations", help="Keep track of how many " |
Florian Mayer | 4c19b69 | 2019-07-15 16:58:38 +0100 | [diff] [blame] | 156 | "bytes were unused since the last dump, per " |
Florian Mayer | 7142c7c | 2019-05-20 18:11:41 +0100 | [diff] [blame] | 157 | "callstack", action="store_true") |
Florian Mayer | 8707d4d | 2019-07-16 11:17:46 +0100 | [diff] [blame] | 158 | parser.add_argument("--dump-at-max", help="Dump the maximum memory usage" |
| 159 | "rather than at the time of the dump.", |
| 160 | action="store_true") |
Florian Mayer | 2b8a3b2 | 2019-05-02 18:35:38 +0100 | [diff] [blame] | 161 | parser.add_argument("--simpleperf", action="store_true", |
| 162 | help="Get simpleperf profile of heapprofd. This is " |
| 163 | "only for heapprofd development.") |
Florian Mayer | 0eee91b | 2019-05-10 10:36:16 +0100 | [diff] [blame] | 164 | parser.add_argument("--trace-to-text-binary", |
| 165 | help="Path to local trace to text. For debugging.") |
Florian Mayer | fe4361d | 2019-05-14 11:54:00 +0100 | [diff] [blame] | 166 | parser.add_argument("--print-config", action="store_true", |
| 167 | help="Print config instead of running. For debugging.") |
Florian Mayer | 0eee91b | 2019-05-10 10:36:16 +0100 | [diff] [blame] | 168 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 169 | args = parser.parse_args() |
| 170 | |
| 171 | fail = False |
Florian Mayer | f40dedd | 2019-07-19 13:08:48 +0100 | [diff] [blame] | 172 | if args.block_client and args.no_block_client: |
| 173 | print("FATAL: Both block-client and no-block-client given.", |
| 174 | file=sys.stderr) |
| 175 | fail = True |
Florian Mayer | a774cb7 | 2019-04-29 14:20:43 +0100 | [diff] [blame] | 176 | if args.pid is None and args.name is None: |
| 177 | print("FATAL: Neither PID nor NAME given.", file=sys.stderr) |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 178 | fail = True |
| 179 | if args.duration is None: |
| 180 | print("FATAL: No duration given.", file=sys.stderr) |
| 181 | fail = True |
| 182 | if args.interval is None: |
| 183 | print("FATAL: No interval given.", file=sys.stderr) |
| 184 | fail = True |
Florian Mayer | 91b3c6d | 2019-04-10 13:44:37 -0700 | [diff] [blame] | 185 | if args.shmem_size % 4096: |
| 186 | print("FATAL: shmem-size is not a multiple of 4096.", file=sys.stderr) |
| 187 | fail = True |
| 188 | if args.shmem_size < 8192: |
| 189 | print("FATAL: shmem-size is less than 8192.", file=sys.stderr) |
| 190 | fail = True |
| 191 | if args.shmem_size & (args.shmem_size - 1): |
| 192 | print("FATAL: shmem-size is not a power of two.", file=sys.stderr) |
| 193 | fail = True |
Florian Mayer | 0eee91b | 2019-05-10 10:36:16 +0100 | [diff] [blame] | 194 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 195 | target_cfg = "" |
Florian Mayer | f40dedd | 2019-07-19 13:08:48 +0100 | [diff] [blame] | 196 | if not args.no_block_client: |
Florian Mayer | d6bdb6f | 2019-05-03 17:53:58 +0100 | [diff] [blame] | 197 | target_cfg += "block_client: true\n" |
Florian Mayer | 7142c7c | 2019-05-20 18:11:41 +0100 | [diff] [blame] | 198 | if args.idle_allocations: |
| 199 | target_cfg += "idle_allocations: true\n" |
Florian Mayer | 400e443 | 2019-05-29 11:53:20 +0100 | [diff] [blame] | 200 | if args.no_startup: |
| 201 | target_cfg += "no_startup: true\n" |
| 202 | if args.no_running: |
| 203 | target_cfg += "no_running: true\n" |
Florian Mayer | 8707d4d | 2019-07-16 11:17:46 +0100 | [diff] [blame] | 204 | if args.dump_at_max: |
| 205 | target_cfg += "dump_at_max: true\n" |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 206 | if args.pid: |
Florian Mayer | 0eee91b | 2019-05-10 10:36:16 +0100 | [diff] [blame] | 207 | for pid in args.pid.split(','): |
| 208 | try: |
| 209 | pid = int(pid) |
| 210 | except ValueError: |
| 211 | print("FATAL: invalid PID %s" % pid, file=sys.stderr) |
| 212 | fail = True |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 213 | target_cfg += '{}pid: {}\n'.format(CFG_IDENT, pid) |
| 214 | if args.name: |
Florian Mayer | 0eee91b | 2019-05-10 10:36:16 +0100 | [diff] [blame] | 215 | for name in args.name.split(','): |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 216 | target_cfg += '{}process_cmdline: "{}"\n'.format(CFG_IDENT, name) |
| 217 | |
Florian Mayer | 0eee91b | 2019-05-10 10:36:16 +0100 | [diff] [blame] | 218 | if fail: |
| 219 | parser.print_help() |
| 220 | return 1 |
| 221 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 222 | trace_to_text_binary = args.trace_to_text_binary |
| 223 | if trace_to_text_binary is None: |
| 224 | platform = None |
| 225 | if sys.platform.startswith('linux'): |
| 226 | platform = 'linux' |
| 227 | elif sys.platform.startswith('darwin'): |
| 228 | platform = 'mac' |
| 229 | else: |
| 230 | print("Invalid platform: {}".format(sys.platform), file=sys.stderr) |
Florian Mayer | b627963 | 2018-11-29 13:31:49 +0000 | [diff] [blame] | 231 | return 1 |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 232 | |
| 233 | trace_to_text_binary = load_trace_to_text(platform) |
| 234 | |
Florian Mayer | a8312c7 | 2019-01-31 13:50:22 +0000 | [diff] [blame] | 235 | continuous_dump_cfg = "" |
| 236 | if args.continuous_dump: |
| 237 | continuous_dump_cfg = CONTINUOUS_DUMP.format( |
| 238 | dump_interval=args.continuous_dump) |
Florian Mayer | a774cb7 | 2019-04-29 14:20:43 +0100 | [diff] [blame] | 239 | cfg = CFG.format(interval=args.interval, |
Florian Mayer | a8312c7 | 2019-01-31 13:50:22 +0000 | [diff] [blame] | 240 | duration=args.duration, target_cfg=target_cfg, |
Florian Mayer | 91b3c6d | 2019-04-10 13:44:37 -0700 | [diff] [blame] | 241 | continuous_dump_cfg=continuous_dump_cfg, |
| 242 | shmem_size=args.shmem_size) |
Florian Mayer | fe4361d | 2019-05-14 11:54:00 +0100 | [diff] [blame] | 243 | if not args.no_versions: |
| 244 | cfg += PACKAGES_LIST_CFG |
| 245 | |
| 246 | if args.print_config: |
| 247 | print(cfg) |
| 248 | return 0 |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 249 | |
Florian Mayer | 6ae9526 | 2018-12-06 16:10:29 +0000 | [diff] [blame] | 250 | if args.disable_selinux: |
| 251 | enforcing = subprocess.check_output(['adb', 'shell', 'getenforce']) |
| 252 | atexit.register(subprocess.check_call, |
| 253 | ['adb', 'shell', 'su root setenforce %s' % enforcing]) |
| 254 | subprocess.check_call(['adb', 'shell', 'su root setenforce 0']) |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 255 | |
Florian Mayer | 33ceb8d | 2019-01-11 14:51:28 +0000 | [diff] [blame] | 256 | if not args.no_start: |
Florian Mayer | 75266d5 | 2019-02-01 18:09:43 +0000 | [diff] [blame] | 257 | heapprofd_prop = subprocess.check_output( |
| 258 | ['adb', 'shell', 'getprop persist.heapprofd.enable']) |
| 259 | if heapprofd_prop.strip() != '1': |
| 260 | subprocess.check_call( |
| 261 | ['adb', 'shell', 'setprop persist.heapprofd.enable 1']) |
| 262 | atexit.register(subprocess.check_call, |
| 263 | ['adb', 'shell', 'setprop persist.heapprofd.enable 0']) |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 264 | |
Florian Mayer | bb3b682 | 2019-03-08 17:08:59 +0000 | [diff] [blame] | 265 | user = subprocess.check_output(['adb', 'shell', 'whoami']).strip() |
Florian Mayer | 2b8a3b2 | 2019-05-02 18:35:38 +0100 | [diff] [blame] | 266 | |
| 267 | if args.simpleperf: |
| 268 | subprocess.check_call( |
| 269 | ['adb', 'shell', |
| 270 | 'mkdir -p /data/local/tmp/heapprofd_profile && ' |
| 271 | 'cd /data/local/tmp/heapprofd_profile &&' |
Florian Mayer | 37c0a5e | 2019-08-01 10:52:32 +0100 | [diff] [blame] | 272 | '(nohup simpleperf record -g -p $(pidof heapprofd) 2>&1 &) ' |
Florian Mayer | 2b8a3b2 | 2019-05-02 18:35:38 +0100 | [diff] [blame] | 273 | '> /dev/null']) |
| 274 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 275 | perfetto_pid = subprocess.check_output( |
Florian Mayer | bb3b682 | 2019-03-08 17:08:59 +0000 | [diff] [blame] | 276 | ['adb', 'exec-out', PERFETTO_CMD.format(cfg=cfg, user=user)]).strip() |
Florian Mayer | 3564742 | 2019-03-07 16:28:10 +0000 | [diff] [blame] | 277 | try: |
| 278 | int(perfetto_pid.strip()) |
| 279 | except ValueError: |
| 280 | print("Failed to invoke perfetto: {}".format(perfetto_pid), |
| 281 | file=sys.stderr) |
| 282 | return 1 |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 283 | |
| 284 | old_handler = signal.signal(signal.SIGINT, sigint_handler) |
| 285 | print("Profiling active. Press Ctrl+C to terminate.") |
Florian Mayer | bd0a62a | 2019-04-10 11:09:21 +0100 | [diff] [blame] | 286 | print("You may disconnect your device.") |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 287 | exists = True |
Florian Mayer | bd0a62a | 2019-04-10 11:09:21 +0100 | [diff] [blame] | 288 | device_connected = True |
| 289 | while not device_connected or (exists and not IS_INTERRUPTED): |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 290 | exists = subprocess.call( |
Florian Mayer | bd0a62a | 2019-04-10 11:09:21 +0100 | [diff] [blame] | 291 | ['adb', 'shell', '[ -d /proc/{} ]'.format(perfetto_pid)], |
| 292 | **NOOUT) == 0 |
| 293 | device_connected = subprocess.call(['adb', 'shell', 'true'], **NOOUT) == 0 |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 294 | time.sleep(1) |
| 295 | signal.signal(signal.SIGINT, old_handler) |
| 296 | if IS_INTERRUPTED: |
| 297 | # Not check_call because it could have existed in the meantime. |
Florian Mayer | 85969b9 | 2019-01-23 17:23:16 +0000 | [diff] [blame] | 298 | subprocess.call(['adb', 'shell', 'kill', '-INT', perfetto_pid]) |
Florian Mayer | 2b8a3b2 | 2019-05-02 18:35:38 +0100 | [diff] [blame] | 299 | if args.simpleperf: |
| 300 | subprocess.check_call(['adb', 'shell', 'killall', '-INT', 'simpleperf']) |
| 301 | print("Waiting for simpleperf to exit.") |
| 302 | while subprocess.call( |
Florian Mayer | 37c0a5e | 2019-08-01 10:52:32 +0100 | [diff] [blame] | 303 | ['adb', 'shell', '[ -f /proc/$(pidof simpleperf)/exe ]'], |
Florian Mayer | 2b8a3b2 | 2019-05-02 18:35:38 +0100 | [diff] [blame] | 304 | **NOOUT) == 0: |
| 305 | time.sleep(1) |
| 306 | subprocess.check_call(['adb', 'pull', '/data/local/tmp/heapprofd_profile', |
| 307 | '/tmp']) |
| 308 | print("Pulled simpleperf profile to /tmp/heapprofd_profile") |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 309 | |
Florian Mayer | ddbe31e | 2018-11-30 14:49:30 +0000 | [diff] [blame] | 310 | # Wait for perfetto cmd to return. |
| 311 | while exists: |
| 312 | exists = subprocess.call( |
| 313 | ['adb', 'shell', '[ -d /proc/{} ]'.format(perfetto_pid)]) == 0 |
| 314 | time.sleep(1) |
| 315 | |
Florian Mayer | bb3b682 | 2019-03-08 17:08:59 +0000 | [diff] [blame] | 316 | subprocess.check_call(['adb', 'pull', |
| 317 | '/data/misc/perfetto-traces/profile-{}'.format(user), |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 318 | '/tmp/profile'], stdout=NULL) |
| 319 | trace_to_text_output = subprocess.check_output( |
Florian Mayer | 6fc484e | 2019-04-10 16:07:54 +0100 | [diff] [blame] | 320 | [trace_to_text_binary, 'profile', '/tmp/profile']) |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 321 | profile_path = None |
| 322 | for word in trace_to_text_output.split(): |
| 323 | if 'heap_profile-' in word: |
| 324 | profile_path = word |
| 325 | if profile_path is None: |
| 326 | print("Could not find trace_to_text output path.", file=sys.stderr) |
| 327 | return 1 |
| 328 | |
| 329 | profile_files = os.listdir(profile_path) |
| 330 | if not profile_files: |
| 331 | print("No profiles generated", file=sys.stderr) |
Florian Mayer | 4e6e590 | 2019-06-07 18:36:17 +0100 | [diff] [blame] | 332 | print("If this is unexpected, check " |
| 333 | "https://docs.perfetto.dev/#/heapprofd?id=troubleshooting.", |
| 334 | file=sys.stderr) |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 335 | return 1 |
| 336 | |
| 337 | subprocess.check_call(['gzip'] + [os.path.join(profile_path, x) for x in |
| 338 | os.listdir(profile_path)]) |
Florian Mayer | 82f43d1 | 2019-01-17 14:37:45 +0000 | [diff] [blame] | 339 | |
| 340 | symlink_path = os.path.join(os.path.dirname(profile_path), |
| 341 | "heap_profile-latest") |
Florian Mayer | 91967ee | 2019-05-10 16:43:50 +0100 | [diff] [blame] | 342 | if os.path.lexists(symlink_path): |
Florian Mayer | 3130551 | 2019-01-21 17:37:02 +0000 | [diff] [blame] | 343 | os.unlink(symlink_path) |
Florian Mayer | 82f43d1 | 2019-01-17 14:37:45 +0000 | [diff] [blame] | 344 | os.symlink(profile_path, symlink_path) |
| 345 | |
| 346 | print("Wrote profiles to {} (symlink {})".format(profile_path, symlink_path)) |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 347 | print("These can be viewed using pprof. Googlers: head to pprof/ and " |
| 348 | "upload them.") |
| 349 | |
| 350 | |
| 351 | if __name__ == '__main__': |
| 352 | sys.exit(main(sys.argv)) |