Florian Mayer | c8aa81c | 2021-04-19 15:16:15 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 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 | |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 16 | # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 17 | # DO NOT EDIT. Auto-generated by tools/gen_amalgamated_python_tools |
| 18 | # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 19 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 20 | from __future__ import absolute_import |
| 21 | from __future__ import division |
| 22 | from __future__ import print_function |
| 23 | |
| 24 | import argparse |
| 25 | import atexit |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 26 | import os |
Florian Mayer | 92c80d8 | 2019-09-25 14:00:01 +0100 | [diff] [blame] | 27 | import shutil |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 28 | import signal |
| 29 | import subprocess |
| 30 | import sys |
| 31 | import tempfile |
| 32 | import time |
Florian Mayer | 1586639 | 2020-04-02 11:52:16 +0200 | [diff] [blame] | 33 | import uuid |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 34 | |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 35 | |
| 36 | # ----- Amalgamator: begin of python/perfetto/prebuilts/manifests/traceconv.py |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 37 | # This file has been generated by: /Users/hjd/src/perfetto/tools/roll-prebuilts v34.0 |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 38 | TRACECONV_MANIFEST = [{ |
| 39 | 'arch': |
| 40 | 'mac-amd64', |
| 41 | 'file_name': |
| 42 | 'traceconv', |
| 43 | 'file_size': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 44 | 7904536, |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 45 | 'url': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 46 | 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/mac-amd64/traceconv', |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 47 | 'sha256': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 48 | '037f84ac943f3f4d75447c668cc49c966fe3d85eca3a455c958b24fc6a9e314a', |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 49 | 'platform': |
| 50 | 'darwin', |
| 51 | 'machine': ['x86_64'] |
| 52 | }, { |
| 53 | 'arch': |
| 54 | 'mac-arm64', |
| 55 | 'file_name': |
| 56 | 'traceconv', |
| 57 | 'file_size': |
Lalit Maganti | b0bb364 | 2023-04-27 13:40:05 +0100 | [diff] [blame] | 58 | 6554600, |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 59 | 'url': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 60 | 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/mac-arm64/traceconv', |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 61 | 'sha256': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 62 | 'eda545ef4fa37fdfa1b47ced7cbbe0aa3c0df9bd161cacd7c78e6c55aef98d20', |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 63 | 'platform': |
| 64 | 'darwin', |
| 65 | 'machine': ['arm64'] |
| 66 | }, { |
| 67 | 'arch': |
| 68 | 'linux-amd64', |
| 69 | 'file_name': |
| 70 | 'traceconv', |
| 71 | 'file_size': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 72 | 7664384, |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 73 | 'url': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 74 | 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/linux-amd64/traceconv', |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 75 | 'sha256': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 76 | '24285e6e0e873d393fa5a993bac18ec8e1ab5fae6f4e3453214e095ef36e4c45', |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 77 | 'platform': |
| 78 | 'linux', |
| 79 | 'machine': ['x86_64'] |
| 80 | }, { |
| 81 | 'arch': |
| 82 | 'linux-arm', |
| 83 | 'file_name': |
| 84 | 'traceconv', |
| 85 | 'file_size': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 86 | 5657944, |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 87 | 'url': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 88 | 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/linux-arm/traceconv', |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 89 | 'sha256': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 90 | 'c9af3d976f849fc75e96c2c552cb14fcc9eacce6fe7c45c4a8289080b0f66706', |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 91 | 'platform': |
| 92 | 'linux', |
| 93 | 'machine': ['armv6l', 'armv7l', 'armv8l'] |
| 94 | }, { |
| 95 | 'arch': |
| 96 | 'linux-arm64', |
| 97 | 'file_name': |
| 98 | 'traceconv', |
| 99 | 'file_size': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 100 | 7184224, |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 101 | 'url': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 102 | 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/linux-arm64/traceconv', |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 103 | 'sha256': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 104 | 'c6dc936492d58a40cd8e0b58abc46bd479e0c1c387cd1ba29198a6c9b2000d7a', |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 105 | 'platform': |
| 106 | 'linux', |
| 107 | 'machine': ['aarch64'] |
| 108 | }, { |
| 109 | 'arch': |
| 110 | 'android-arm', |
| 111 | 'file_name': |
| 112 | 'traceconv', |
| 113 | 'file_size': |
Lalit Maganti | b0bb364 | 2023-04-27 13:40:05 +0100 | [diff] [blame] | 114 | 5325260, |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 115 | 'url': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 116 | 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/android-arm/traceconv', |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 117 | 'sha256': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 118 | '963267dcb58cdde9f61a952e5cb7f3557833209d3251e7fdcefc3b52db54f77b' |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 119 | }, { |
| 120 | 'arch': |
| 121 | 'android-arm64', |
| 122 | 'file_name': |
| 123 | 'traceconv', |
| 124 | 'file_size': |
Lalit Maganti | b0bb364 | 2023-04-27 13:40:05 +0100 | [diff] [blame] | 125 | 6572688, |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 126 | 'url': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 127 | 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/android-arm64/traceconv', |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 128 | 'sha256': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 129 | '87373c351fe5e947826cd957438cab8a37a352bf83b1cbbb15fe276eee9d873a' |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 130 | }, { |
| 131 | 'arch': |
| 132 | 'android-x86', |
| 133 | 'file_name': |
| 134 | 'traceconv', |
| 135 | 'file_size': |
Lalit Maganti | b0bb364 | 2023-04-27 13:40:05 +0100 | [diff] [blame] | 136 | 7303588, |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 137 | 'url': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 138 | 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/android-x86/traceconv', |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 139 | 'sha256': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 140 | 'dfc4e714963b5ed662d29d6028ffa69e67f8cd2f9a28223f715437a260fd456f' |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 141 | }, { |
| 142 | 'arch': |
| 143 | 'android-x64', |
| 144 | 'file_name': |
| 145 | 'traceconv', |
| 146 | 'file_size': |
Lalit Maganti | b0bb364 | 2023-04-27 13:40:05 +0100 | [diff] [blame] | 147 | 7482056, |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 148 | 'url': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 149 | 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/android-x64/traceconv', |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 150 | 'sha256': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 151 | '79c666c629fcffd810635270b45e58b40ed253d22650f41550057e5d8f8c49a7' |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 152 | }, { |
| 153 | 'arch': |
| 154 | 'windows-amd64', |
| 155 | 'file_name': |
| 156 | 'traceconv.exe', |
| 157 | 'file_size': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 158 | 7072768, |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 159 | 'url': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 160 | 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/windows-amd64/traceconv.exe', |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 161 | 'sha256': |
Hector Dearman | e812c09 | 2023-05-02 17:45:42 +0100 | [diff] [blame] | 162 | '40fac80fdeae443a924e160650c94629e6463c1fb5a4f04f4ef6e9e5e72a3965', |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 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 | """ |
| 185 | Functions to fetch pre-pinned Perfetto prebuilts. |
| 186 | |
| 187 | This 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 | |
| 193 | The manifest argument looks as follows: |
| 194 | TRACECONV_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 | |
| 207 | The 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 | |
| 214 | import hashlib |
| 215 | import os |
| 216 | import platform |
| 217 | import subprocess |
| 218 | import sys |
| 219 | |
| 220 | |
| 221 | def 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 |
Joseph Koshy | 5efb8a1 | 2022-10-19 12:39:29 +0100 | [diff] [blame] | 225 | ~/.local/share/perfetto/prebuilts/$tool_name. On subsequent invocations it |
| 226 | just runs the cached version. |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 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) |
Lalit Maganti | 1480b86 | 2022-10-27 15:02:54 +0100 | [diff] [blame] | 254 | os.replace(tmp_path, bin_path) |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 255 | with open(sha256_path, 'w') as f: |
| 256 | f.write(sha256) |
| 257 | return bin_path |
| 258 | |
| 259 | |
| 260 | def 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 | |
| 289 | def 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 | |
Florian Mayer | bd0a62a | 2019-04-10 11:09:21 +0100 | [diff] [blame] | 297 | NULL = open(os.devnull) |
| 298 | NOOUT = { |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 299 | 'stdout': NULL, |
| 300 | 'stderr': NULL, |
Florian Mayer | bd0a62a | 2019-04-10 11:09:21 +0100 | [diff] [blame] | 301 | } |
| 302 | |
Florian Mayer | 2007d5c | 2020-09-08 15:45:44 +0100 | [diff] [blame] | 303 | UUID = str(uuid.uuid4())[-6:] |
Florian Mayer | bd0a62a | 2019-04-10 11:09:21 +0100 | [diff] [blame] | 304 | |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 305 | PACKAGES_LIST_CFG = '''data_sources { |
Florian Mayer | c8b2869 | 2019-05-16 17:03:21 +0100 | [diff] [blame] | 306 | config { |
Florian Mayer | fe4361d | 2019-05-14 11:54:00 +0100 | [diff] [blame] | 307 | name: "android.packages_list" |
Florian Mayer | c8b2869 | 2019-05-16 17:03:21 +0100 | [diff] [blame] | 308 | } |
| 309 | } |
Florian Mayer | fe4361d | 2019-05-14 11:54:00 +0100 | [diff] [blame] | 310 | ''' |
| 311 | |
Florian Mayer | a8ff903 | 2020-03-04 11:31:48 -0800 | [diff] [blame] | 312 | CFG_INDENT = ' ' |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 313 | CFG = '''buffers {{ |
Florian Mayer | 2214c2e | 2021-02-03 16:54:00 +0000 | [diff] [blame] | 314 | size_kb: 63488 |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 315 | }} |
| 316 | |
| 317 | data_sources {{ |
| 318 | config {{ |
| 319 | name: "android.heapprofd" |
| 320 | heapprofd_config {{ |
Florian Mayer | 91b3c6d | 2019-04-10 13:44:37 -0700 | [diff] [blame] | 321 | shmem_size_bytes: {shmem_size} |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 322 | sampling_interval_bytes: {interval} |
| 323 | {target_cfg} |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 324 | }} |
| 325 | }} |
| 326 | }} |
| 327 | |
| 328 | duration_ms: {duration} |
Florian Mayer | 2aab316 | 2019-05-03 16:02:30 +0100 | [diff] [blame] | 329 | write_into_file: true |
Florian Mayer | 8261046 | 2019-04-16 10:26:07 +0100 | [diff] [blame] | 330 | flush_timeout_ms: 30000 |
Florian Mayer | 96f607b | 2020-01-14 12:57:41 +0000 | [diff] [blame] | 331 | flush_period_ms: 604800000 |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 332 | ''' |
| 333 | |
Florian Mayer | 96f607b | 2020-01-14 12:57:41 +0000 | [diff] [blame] | 334 | # flush_period_ms of 1 week to suppress trace_processor_shell warning. |
| 335 | |
Florian Mayer | a8312c7 | 2019-01-31 13:50:22 +0000 | [diff] [blame] | 336 | CONTINUOUS_DUMP = """ |
| 337 | continuous_dump_config {{ |
| 338 | dump_phase_ms: 0 |
| 339 | dump_interval_ms: {dump_interval} |
| 340 | }} |
| 341 | """ |
| 342 | |
Florian Mayer | 2007d5c | 2020-09-08 15:45:44 +0100 | [diff] [blame] | 343 | PROFILE_LOCAL_PATH = os.path.join(tempfile.gettempdir(), UUID) |
Florian Mayer | 1586639 | 2020-04-02 11:52:16 +0200 | [diff] [blame] | 344 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 345 | IS_INTERRUPTED = False |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 346 | |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 347 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 348 | def sigint_handler(sig, frame): |
| 349 | global IS_INTERRUPTED |
| 350 | IS_INTERRUPTED = True |
| 351 | |
| 352 | |
Florian Mayer | 96f607b | 2020-01-14 12:57:41 +0000 | [diff] [blame] | 353 | def print_no_profile_error(): |
| 354 | print("No profiles generated", file=sys.stderr) |
| 355 | print( |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 356 | "If this is unexpected, check " |
| 357 | "https://perfetto.dev/docs/data-sources/native-heap-profiler#troubleshooting.", |
| 358 | file=sys.stderr) |
| 359 | |
Florian Mayer | 96f607b | 2020-01-14 12:57:41 +0000 | [diff] [blame] | 360 | |
Florian Mayer | c4de391 | 2020-11-23 14:11:43 +0000 | [diff] [blame] | 361 | def known_issues_url(number): |
| 362 | return ('https://perfetto.dev/docs/data-sources/native-heap-profiler' |
| 363 | '#known-issues-android{}'.format(number)) |
| 364 | |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 365 | |
Florian Mayer | c4de391 | 2020-11-23 14:11:43 +0000 | [diff] [blame] | 366 | KNOWN_ISSUES = { |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 367 | '10': known_issues_url(10), |
| 368 | 'Q': known_issues_url(10), |
| 369 | '11': known_issues_url(11), |
| 370 | 'R': known_issues_url(11), |
Florian Mayer | c4de391 | 2020-11-23 14:11:43 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 373 | |
Florian Mayer | c4de391 | 2020-11-23 14:11:43 +0000 | [diff] [blame] | 374 | def maybe_known_issues(): |
| 375 | release_or_codename = subprocess.check_output( |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 376 | ['adb', 'shell', 'getprop', |
| 377 | 'ro.build.version.release_or_codename']).decode('utf-8').strip() |
Florian Mayer | c4de391 | 2020-11-23 14:11:43 +0000 | [diff] [blame] | 378 | return KNOWN_ISSUES.get(release_or_codename, None) |
| 379 | |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 380 | |
Florian Mayer | 9a90474 | 2020-04-28 18:40:52 +0200 | [diff] [blame] | 381 | SDK = { |
| 382 | 'R': 30, |
| 383 | } |
| 384 | |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 385 | |
Florian Mayer | 9a90474 | 2020-04-28 18:40:52 +0200 | [diff] [blame] | 386 | def release_or_newer(release): |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 387 | sdk = int( |
| 388 | subprocess.check_output( |
| 389 | ['adb', 'shell', 'getprop', |
| 390 | 'ro.system.build.version.sdk']).decode('utf-8').strip()) |
Florian Mayer | 9a90474 | 2020-04-28 18:40:52 +0200 | [diff] [blame] | 391 | if sdk >= SDK[release]: |
| 392 | return True |
| 393 | codename = subprocess.check_output( |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 394 | ['adb', 'shell', 'getprop', |
| 395 | 'ro.build.version.codename']).decode('utf-8').strip() |
Florian Mayer | 9a90474 | 2020-04-28 18:40:52 +0200 | [diff] [blame] | 396 | return codename == release |
| 397 | |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 398 | |
Florian Mayer | be1e425 | 2021-05-26 15:49:21 +0100 | [diff] [blame] | 399 | ORDER = ['-n', '-p', '-i', '-o'] |
| 400 | |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 401 | |
Florian Mayer | be1e425 | 2021-05-26 15:49:21 +0100 | [diff] [blame] | 402 | def 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 | |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 409 | |
Florian Mayer | be1e425 | 2021-05-26 15:49:21 +0100 | [diff] [blame] | 410 | def 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) |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 415 | metavar = '' if action.metavar is None else ' _' + action.metavar + '_' |
Florian Mayer | be1e425 | 2021-05-26 15:49:21 +0100 | [diff] [blame] | 416 | print('{}{}'.format(opts, metavar)) |
| 417 | print(': {}'.format(action.help)) |
| 418 | print() |
| 419 | |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 420 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 421 | def main(argv): |
| 422 | parser = argparse.ArgumentParser() |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 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", |
Florian Mayer | 2ad3861 | 2020-08-06 10:35:31 +0100 | [diff] [blame] | 433 | help="Duration of profile (ms). 0 to run until interrupted. " |
| 434 | "Default: until interrupted by user.", |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 435 | type=int, |
Florian Mayer | 2ad3861 | 2020-08-06 10:35:31 +0100 | [diff] [blame] | 436 | default=0) |
Florian Mayer | 19ec17e | 2020-06-10 16:09:12 +0200 | [diff] [blame] | 437 | # This flag is a no-op now. We never start heapprofd explicitly using system |
| 438 | # properties. |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 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( |
Florian Mayer | 39ddaad | 2020-06-25 19:58:00 +0200 | [diff] [blame] | 460 | "--heaps", |
| 461 | help="Comma-separated list of heaps to collect, e.g: malloc,art. " |
| 462 | "Requires Android 12.", |
| 463 | metavar="HEAPS") |
Hector Dearman | 091a54e | 2020-08-03 11:17:13 +0100 | [diff] [blame] | 464 | parser.add_argument( |
Florian Mayer | ed4b1c0 | 2020-07-28 16:56:30 +0100 | [diff] [blame] | 465 | "--all-heaps", |
| 466 | action="store_true", |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 467 | help="Collect allocations from all heaps registered by target.") |
Florian Mayer | 39ddaad | 2020-06-25 19:58:00 +0200 | [diff] [blame] | 468 | parser.add_argument( |
Florian Mayer | cad84b7 | 2020-11-24 14:26:32 +0000 | [diff] [blame] | 469 | "--no-android-tree-symbolization", |
| 470 | action="store_true", |
| 471 | help="Do not symbolize using currently lunched target in the " |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 472 | "Android tree.") |
Florian Mayer | cad84b7 | 2020-11-24 14:26:32 +0000 | [diff] [blame] | 473 | parser.add_argument( |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 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", |
Florian Mayer | 615fbc9 | 2019-12-18 16:29:07 +0000 | [diff] [blame] | 485 | help="Do not target already running processes. Requires Android 11.") |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 486 | parser.add_argument( |
| 487 | "--no-startup", |
| 488 | action="store_true", |
| 489 | help="Do not target processes that start during " |
Florian Mayer | 615fbc9 | 2019-12-18 16:29:07 +0000 | [diff] [blame] | 490 | "the profile. Requires Android 11.") |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 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( |
Florian Mayer | e17af21 | 2019-11-13 10:04:03 +0000 | [diff] [blame] | 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( |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 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", |
Florian Mayer | 2fcb7dd | 2020-04-27 15:26:47 +0200 | [diff] [blame] | 523 | help="Dump the maximum memory usage " |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 524 | "rather than at the time of the dump.", |
| 525 | action="store_true") |
| 526 | parser.add_argument( |
Florian Mayer | f9cd853 | 2020-04-21 12:18:10 +0200 | [diff] [blame] | 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( |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 532 | "--simpleperf", |
| 533 | action="store_true", |
| 534 | help="Get simpleperf profile of heapprofd. This is " |
| 535 | "only for heapprofd development.") |
| 536 | parser.add_argument( |
Lalit Maganti | 9d03502 | 2022-06-13 17:40:31 +0100 | [diff] [blame] | 537 | "--traceconv-binary", help="Path to local trace to text. For debugging.") |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 538 | parser.add_argument( |
Ryan Savitski | 4bb2b22 | 2022-11-17 18:08:03 +0000 | [diff] [blame] | 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( |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 544 | "--print-config", |
| 545 | action="store_true", |
| 546 | help="Print config instead of running. For debugging.") |
Florian Mayer | e9a55c6 | 2020-04-14 16:39:34 +0200 | [diff] [blame] | 547 | parser.add_argument( |
| 548 | "-o", |
| 549 | "--output", |
| 550 | help="Output directory.", |
| 551 | metavar="DIRECTORY", |
| 552 | default=None) |
Florian Mayer | be1e425 | 2021-05-26 15:49:21 +0100 | [diff] [blame] | 553 | parser.add_argument( |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 554 | "--print-options", action="store_true", help=argparse.SUPPRESS) |
Florian Mayer | 0eee91b | 2019-05-10 10:36:16 +0100 | [diff] [blame] | 555 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 556 | args = parser.parse_args() |
Florian Mayer | be1e425 | 2021-05-26 15:49:21 +0100 | [diff] [blame] | 557 | if args.print_options: |
| 558 | print_options(parser) |
| 559 | return 0 |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 560 | fail = False |
Florian Mayer | f40dedd | 2019-07-19 13:08:48 +0100 | [diff] [blame] | 561 | if args.block_client and args.no_block_client: |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 562 | print( |
| 563 | "FATAL: Both block-client and no-block-client given.", file=sys.stderr) |
Florian Mayer | f40dedd | 2019-07-19 13:08:48 +0100 | [diff] [blame] | 564 | fail = True |
Florian Mayer | a774cb7 | 2019-04-29 14:20:43 +0100 | [diff] [blame] | 565 | if args.pid is None and args.name is None: |
| 566 | print("FATAL: Neither PID nor NAME given.", file=sys.stderr) |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 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 |
Florian Mayer | 91b3c6d | 2019-04-10 13:44:37 -0700 | [diff] [blame] | 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 |
Florian Mayer | 0eee91b | 2019-05-10 10:36:16 +0100 | [diff] [blame] | 583 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 584 | target_cfg = "" |
Florian Mayer | f40dedd | 2019-07-19 13:08:48 +0100 | [diff] [blame] | 585 | if not args.no_block_client: |
Florian Mayer | 2ad3861 | 2020-08-06 10:35:31 +0100 | [diff] [blame] | 586 | target_cfg += CFG_INDENT + "block_client: true\n" |
Florian Mayer | e17af21 | 2019-11-13 10:04:03 +0000 | [diff] [blame] | 587 | if args.block_client_timeout: |
Florian Mayer | 2ad3861 | 2020-08-06 10:35:31 +0100 | [diff] [blame] | 588 | target_cfg += ( |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 589 | CFG_INDENT + |
| 590 | "block_client_timeout_us: %s\n" % args.block_client_timeout) |
Florian Mayer | 400e443 | 2019-05-29 11:53:20 +0100 | [diff] [blame] | 591 | if args.no_startup: |
Florian Mayer | 2ad3861 | 2020-08-06 10:35:31 +0100 | [diff] [blame] | 592 | target_cfg += CFG_INDENT + "no_startup: true\n" |
Florian Mayer | 400e443 | 2019-05-29 11:53:20 +0100 | [diff] [blame] | 593 | if args.no_running: |
Florian Mayer | 2ad3861 | 2020-08-06 10:35:31 +0100 | [diff] [blame] | 594 | target_cfg += CFG_INDENT + "no_running: true\n" |
Florian Mayer | 8707d4d | 2019-07-16 11:17:46 +0100 | [diff] [blame] | 595 | if args.dump_at_max: |
Florian Mayer | 2ad3861 | 2020-08-06 10:35:31 +0100 | [diff] [blame] | 596 | target_cfg += CFG_INDENT + "dump_at_max: true\n" |
Florian Mayer | f9cd853 | 2020-04-21 12:18:10 +0200 | [diff] [blame] | 597 | if args.disable_fork_teardown: |
Florian Mayer | 2ad3861 | 2020-08-06 10:35:31 +0100 | [diff] [blame] | 598 | target_cfg += CFG_INDENT + "disable_fork_teardown: true\n" |
Florian Mayer | ed4b1c0 | 2020-07-28 16:56:30 +0100 | [diff] [blame] | 599 | if args.all_heaps: |
Florian Mayer | 2ad3861 | 2020-08-06 10:35:31 +0100 | [diff] [blame] | 600 | target_cfg += CFG_INDENT + "all_heaps: true\n" |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 601 | if args.pid: |
Florian Mayer | 0eee91b | 2019-05-10 10:36:16 +0100 | [diff] [blame] | 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 |
Florian Mayer | 2ad3861 | 2020-08-06 10:35:31 +0100 | [diff] [blame] | 608 | target_cfg += CFG_INDENT + 'pid: {}\n'.format(pid) |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 609 | if args.name: |
Florian Mayer | 0eee91b | 2019-05-10 10:36:16 +0100 | [diff] [blame] | 610 | for name in args.name.split(','): |
Florian Mayer | 2ad3861 | 2020-08-06 10:35:31 +0100 | [diff] [blame] | 611 | target_cfg += CFG_INDENT + 'process_cmdline: "{}"\n'.format(name) |
Florian Mayer | 39ddaad | 2020-06-25 19:58:00 +0200 | [diff] [blame] | 612 | if args.heaps: |
| 613 | for heap in args.heaps.split(','): |
Florian Mayer | 2ad3861 | 2020-08-06 10:35:31 +0100 | [diff] [blame] | 614 | target_cfg += CFG_INDENT + 'heaps: "{}"\n'.format(heap) |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 615 | |
Florian Mayer | 0eee91b | 2019-05-10 10:36:16 +0100 | [diff] [blame] | 616 | if fail: |
| 617 | parser.print_help() |
| 618 | return 1 |
| 619 | |
Hector Dearman | a9545e5 | 2022-05-17 12:23:25 +0100 | [diff] [blame] | 620 | traceconv_binary = args.traceconv_binary |
Florian Mayer | 2ad3861 | 2020-08-06 10:35:31 +0100 | [diff] [blame] | 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 | |
Hector Dearman | a9545e5 | 2022-05-17 12:23:25 +0100 | [diff] [blame] | 636 | # Do this AFTER print_config so we do not download traceconv only to |
Florian Mayer | 2ad3861 | 2020-08-06 10:35:31 +0100 | [diff] [blame] | 637 | # print out the config. |
Hector Dearman | a9545e5 | 2022-05-17 12:23:25 +0100 | [diff] [blame] | 638 | if traceconv_binary is None: |
Primiano Tucci | 11d94e1 | 2022-08-02 17:44:33 +0100 | [diff] [blame] | 639 | traceconv_binary = get_perfetto_prebuilt(TRACECONV_MANIFEST, soft_fail=True) |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 640 | |
Florian Mayer | c4de391 | 2020-11-23 14:11:43 +0000 | [diff] [blame] | 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 | |
Florian Mayer | 230b955 | 2020-07-10 22:11:24 +0100 | [diff] [blame] | 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: |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 652 | user = subprocess.check_output(['adb', 'shell', |
| 653 | 'whoami']).decode('utf-8').strip() |
Florian Mayer | 230b955 | 2020-07-10 22:11:24 +0100 | [diff] [blame] | 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 | |
Florian Mayer | 6ae9526 | 2018-12-06 16:10:29 +0000 | [diff] [blame] | 659 | if args.disable_selinux: |
Christopher Phlipot | c7d0d3a | 2022-09-15 11:34:24 -0700 | [diff] [blame] | 660 | enforcing = subprocess.check_output(['adb', 'shell', |
| 661 | 'getenforce']).decode('utf-8').strip() |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 662 | atexit.register( |
| 663 | subprocess.check_call, |
Florian Mayer | 6ae9526 | 2018-12-06 16:10:29 +0000 | [diff] [blame] | 664 | ['adb', 'shell', 'su root setenforce %s' % enforcing]) |
| 665 | subprocess.check_call(['adb', 'shell', 'su root setenforce 0']) |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 666 | |
Florian Mayer | 2b8a3b2 | 2019-05-02 18:35:38 +0100 | [diff] [blame] | 667 | if args.simpleperf: |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 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 | ]) |
Florian Mayer | 2b8a3b2 | 2019-05-02 18:35:38 +0100 | [diff] [blame] | 674 | |
Florian Mayer | e9a55c6 | 2020-04-14 16:39:34 +0200 | [diff] [blame] | 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): |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 682 | print( |
| 683 | "Output directory {} not found".format(profile_target), file=sys.stderr) |
Florian Mayer | e9a55c6 | 2020-04-14 16:39:34 +0200 | [diff] [blame] | 684 | return 1 |
| 685 | |
| 686 | if os.listdir(profile_target): |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 687 | print( |
| 688 | "Output directory {} not empty".format(profile_target), file=sys.stderr) |
Florian Mayer | e9a55c6 | 2020-04-14 16:39:34 +0200 | [diff] [blame] | 689 | return 1 |
| 690 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 691 | perfetto_pid = subprocess.check_output( |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 692 | ['adb', 'exec-out', perfetto_cmd.format(cfg=cfg)]).strip() |
Florian Mayer | 3564742 | 2019-03-07 16:28:10 +0000 | [diff] [blame] | 693 | try: |
Florian Mayer | 0bc3252 | 2020-04-28 16:35:55 +0200 | [diff] [blame] | 694 | perfetto_pid = int(perfetto_pid.strip()) |
Florian Mayer | 3564742 | 2019-03-07 16:28:10 +0000 | [diff] [blame] | 695 | except ValueError: |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 696 | print("Failed to invoke perfetto: {}".format(perfetto_pid), file=sys.stderr) |
Florian Mayer | 3564742 | 2019-03-07 16:28:10 +0000 | [diff] [blame] | 697 | return 1 |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 698 | |
| 699 | old_handler = signal.signal(signal.SIGINT, sigint_handler) |
| 700 | print("Profiling active. Press Ctrl+C to terminate.") |
Florian Mayer | bd0a62a | 2019-04-10 11:09:21 +0100 | [diff] [blame] | 701 | print("You may disconnect your device.") |
Florian Mayer | 96f607b | 2020-01-14 12:57:41 +0000 | [diff] [blame] | 702 | print() |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 703 | exists = True |
Florian Mayer | bd0a62a | 2019-04-10 11:09:21 +0100 | [diff] [blame] | 704 | device_connected = True |
| 705 | while not device_connected or (exists and not IS_INTERRUPTED): |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 706 | exists = subprocess.call( |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 707 | ['adb', 'shell', '[ -d /proc/{} ]'.format(perfetto_pid)], **NOOUT) == 0 |
Florian Mayer | bd0a62a | 2019-04-10 11:09:21 +0100 | [diff] [blame] | 708 | device_connected = subprocess.call(['adb', 'shell', 'true'], **NOOUT) == 0 |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 709 | time.sleep(1) |
Florian Mayer | 2007d5c | 2020-09-08 15:45:44 +0100 | [diff] [blame] | 710 | print("Waiting for profiler shutdown...") |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 711 | signal.signal(signal.SIGINT, old_handler) |
| 712 | if IS_INTERRUPTED: |
| 713 | # Not check_call because it could have existed in the meantime. |
Florian Mayer | 0bc3252 | 2020-04-28 16:35:55 +0200 | [diff] [blame] | 714 | subprocess.call(['adb', 'shell', 'kill', '-INT', str(perfetto_pid)]) |
Florian Mayer | 2b8a3b2 | 2019-05-02 18:35:38 +0100 | [diff] [blame] | 715 | if args.simpleperf: |
| 716 | subprocess.check_call(['adb', 'shell', 'killall', '-INT', 'simpleperf']) |
| 717 | print("Waiting for simpleperf to exit.") |
| 718 | while subprocess.call( |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 719 | ['adb', 'shell', '[ -f /proc/$(pidof simpleperf)/exe ]'], **NOOUT) == 0: |
Florian Mayer | 2b8a3b2 | 2019-05-02 18:35:38 +0100 | [diff] [blame] | 720 | time.sleep(1) |
Primiano Tucci | 834fdc7 | 2019-10-04 11:33:44 +0100 | [diff] [blame] | 721 | subprocess.check_call( |
Florian Mayer | 3c57f3f | 2020-10-01 14:58:08 +0100 | [diff] [blame] | 722 | ['adb', 'pull', '/data/local/tmp/heapprofd_profile', profile_target]) |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 723 | print("Pulled simpleperf profile to " + profile_target + |
| 724 | "/heapprofd_profile") |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 725 | |
Florian Mayer | ddbe31e | 2018-11-30 14:49:30 +0000 | [diff] [blame] | 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 | |
Florian Mayer | 2007d5c | 2020-09-08 15:45:44 +0100 | [diff] [blame] | 732 | profile_host_path = os.path.join(profile_target, 'raw-trace') |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 733 | subprocess.check_call(['adb', 'pull', profile_device_path, profile_host_path], |
| 734 | stdout=NULL) |
Florian Mayer | 4fc5993 | 2020-04-28 17:19:55 +0200 | [diff] [blame] | 735 | if uuid_trace: |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 736 | subprocess.check_call(['adb', 'shell', 'rm', profile_device_path], |
| 737 | stdout=NULL) |
Florian Mayer | 213c8d4 | 2019-12-18 17:10:34 +0000 | [diff] [blame] | 738 | |
Hector Dearman | a9545e5 | 2022-05-17 12:23:25 +0100 | [diff] [blame] | 739 | if traceconv_binary is None: |
Florian Mayer | 2007d5c | 2020-09-08 15:45:44 +0100 | [diff] [blame] | 740 | print('Wrote profile to {}'.format(profile_host_path)) |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 741 | print( |
| 742 | 'This file can be opened using the Perfetto UI, https://ui.perfetto.dev' |
| 743 | ) |
Florian Mayer | 2007d5c | 2020-09-08 15:45:44 +0100 | [diff] [blame] | 744 | return 0 |
| 745 | |
Florian Mayer | cad84b7 | 2020-11-24 14:26:32 +0000 | [diff] [blame] | 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') |
Florian Mayer | edc4810 | 2020-12-15 19:38:44 +0000 | [diff] [blame] | 760 | concat_files = [trace_file] |
Florian Mayer | cad84b7 | 2020-11-24 14:26:32 +0000 | [diff] [blame] | 761 | |
| 762 | if binary_path is not None: |
| 763 | with open(os.path.join(profile_target, 'symbols'), 'w') as fd: |
| 764 | ret = subprocess.call([ |
Hector Dearman | a9545e5 | 2022-05-17 12:23:25 +0100 | [diff] [blame] | 765 | traceconv_binary, 'symbolize', |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 766 | os.path.join(profile_target, 'raw-trace') |
| 767 | ], |
| 768 | env=dict( |
| 769 | os.environ, PERFETTO_BINARY_PATH=binary_path), |
| 770 | stdout=fd) |
Florian Mayer | cad84b7 | 2020-11-24 14:26:32 +0000 | [diff] [blame] | 771 | if ret == 0: |
Florian Mayer | edc4810 | 2020-12-15 19:38:44 +0000 | [diff] [blame] | 772 | concat_files.append(os.path.join(profile_target, 'symbols')) |
Florian Mayer | cad84b7 | 2020-11-24 14:26:32 +0000 | [diff] [blame] | 773 | else: |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 774 | print("Failed to symbolize. Continuing without symbols.", file=sys.stderr) |
Florian Mayer | cad84b7 | 2020-11-24 14:26:32 +0000 | [diff] [blame] | 775 | |
Florian Mayer | edc4810 | 2020-12-15 19:38:44 +0000 | [diff] [blame] | 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([ |
Hector Dearman | a9545e5 | 2022-05-17 12:23:25 +0100 | [diff] [blame] | 780 | traceconv_binary, 'deobfuscate', |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 781 | os.path.join(profile_target, 'raw-trace') |
| 782 | ], |
| 783 | env=dict( |
| 784 | os.environ, PERFETTO_PROGUARD_MAP=proguard_map), |
| 785 | stdout=fd) |
Florian Mayer | edc4810 | 2020-12-15 19:38:44 +0000 | [diff] [blame] | 786 | if ret == 0: |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 787 | concat_files.append(os.path.join(profile_target, 'deobfuscation-packets')) |
Florian Mayer | edc4810 | 2020-12-15 19:38:44 +0000 | [diff] [blame] | 788 | else: |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 789 | print( |
| 790 | "Failed to deobfuscate. Continuing without deobfuscated.", |
| 791 | file=sys.stderr) |
Florian Mayer | edc4810 | 2020-12-15 19:38:44 +0000 | [diff] [blame] | 792 | |
| 793 | if len(concat_files) > 1: |
Isaac Nickaein | 1ca5728 | 2021-03-15 16:10:59 +0000 | [diff] [blame] | 794 | with open(os.path.join(profile_target, 'symbolized-trace'), 'wb') as out: |
Florian Mayer | edc4810 | 2020-12-15 19:38:44 +0000 | [diff] [blame] | 795 | for fn in concat_files: |
Isaac Nickaein | 1ca5728 | 2021-03-15 16:10:59 +0000 | [diff] [blame] | 796 | with open(fn, 'rb') as inp: |
Florian Mayer | edc4810 | 2020-12-15 19:38:44 +0000 | [diff] [blame] | 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 | |
Ryan Savitski | 4bb2b22 | 2022-11-17 18:08:03 +0000 | [diff] [blame] | 804 | conversion_args = [traceconv_binary, 'profile'] + ( |
| 805 | ['--no-annotations'] if args.no_annotations else []) + [trace_file] |
| 806 | traceconv_output = subprocess.check_output(conversion_args) |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 807 | profile_path = None |
Hector Dearman | a9545e5 | 2022-05-17 12:23:25 +0100 | [diff] [blame] | 808 | for word in traceconv_output.decode('utf-8').split(): |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 809 | if 'heap_profile-' in word: |
| 810 | profile_path = word |
| 811 | if profile_path is None: |
Florian Mayer | 88bca76 | 2020-07-28 14:42:15 +0100 | [diff] [blame] | 812 | print_no_profile_error() |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 813 | return 1 |
| 814 | |
| 815 | profile_files = os.listdir(profile_path) |
| 816 | if not profile_files: |
Florian Mayer | 88bca76 | 2020-07-28 14:42:15 +0100 | [diff] [blame] | 817 | print_no_profile_error() |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 818 | return 1 |
| 819 | |
Florian Mayer | e9a55c6 | 2020-04-14 16:39:34 +0200 | [diff] [blame] | 820 | for profile_file in profile_files: |
| 821 | shutil.copy(os.path.join(profile_path, profile_file), profile_target) |
| 822 | |
Florian Mayer | e9a55c6 | 2020-04-14 16:39:34 +0200 | [diff] [blame] | 823 | symlink_path = None |
Primiano Tucci | f94e497 | 2021-08-03 11:35:45 +0100 | [diff] [blame] | 824 | if not sys.platform.startswith('win'): |
| 825 | subprocess.check_call( |
Lalit Maganti | 79c8019 | 2021-08-04 15:35:33 +0100 | [diff] [blame] | 826 | ['gzip'] + [os.path.join(profile_target, x) for x in profile_files]) |
Primiano Tucci | f94e497 | 2021-08-03 11:35:45 +0100 | [diff] [blame] | 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) |
Florian Mayer | 82f43d1 | 2019-01-17 14:37:45 +0000 | [diff] [blame] | 833 | |
Florian Mayer | e9a55c6 | 2020-04-14 16:39:34 +0200 | [diff] [blame] | 834 | if symlink_path is not None: |
Primiano Tucci | d3e40bd | 2021-08-03 10:52:39 +0100 | [diff] [blame] | 835 | print("Wrote profiles to {} (symlink {})".format(profile_target, |
| 836 | symlink_path)) |
Florian Mayer | e9a55c6 | 2020-04-14 16:39:34 +0200 | [diff] [blame] | 837 | else: |
| 838 | print("Wrote profiles to {}".format(profile_target)) |
| 839 | |
Primiano Tucci | f94e497 | 2021-08-03 11:35:45 +0100 | [diff] [blame] | 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.") |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 845 | |
| 846 | |
Florian Mayer | 801349e | 2018-11-29 10:15:25 +0000 | [diff] [blame] | 847 | if __name__ == '__main__': |
| 848 | sys.exit(main(sys.argv)) |