1#!/usr/bin/env python3 2# Copyright (C) 2021 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 17# DO NOT EDIT. Auto-generated by tools/gen_amalgamated_python_tools 18# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 19 20# This file should do the same thing when being invoked in any of these ways: 21# ./trace_processor 22# python trace_processor 23# bash trace_processor 24# cat ./trace_processor | bash 25# cat ./trace_processor | python - 26 27BASH_FALLBACK=""" " 28exec python3 - "$@" <<'#'EOF 29#""" # yapf: disable 30 31 32# ----- Amalgamator: begin of python/perfetto/prebuilts/manifests/trace_processor_shell.py 33# This file has been generated by: /Users/hjd/src/perfetto/tools/roll-prebuilts v34.0 34TRACE_PROCESSOR_SHELL_MANIFEST = [{ 35 'arch': 36 'mac-amd64', 37 'file_name': 38 'trace_processor_shell', 39 'file_size': 40 8714576, 41 'url': 42 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/mac-amd64/trace_processor_shell', 43 'sha256': 44 '9bdb89493f0f00db5d3a73166450ac2f6ee830de16415e79c5a0234990caa644', 45 'platform': 46 'darwin', 47 'machine': ['x86_64'] 48}, { 49 'arch': 50 'mac-arm64', 51 'file_name': 52 'trace_processor_shell', 53 'file_size': 54 7286968, 55 'url': 56 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/mac-arm64/trace_processor_shell', 57 'sha256': 58 '948536035fbe680b47b94a99d320ff459450738e4aeeb16cef18364f0023622b', 59 'platform': 60 'darwin', 61 'machine': ['arm64'] 62}, { 63 'arch': 64 'linux-amd64', 65 'file_name': 66 'trace_processor_shell', 67 'file_size': 68 8576688, 69 'url': 70 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/linux-amd64/trace_processor_shell', 71 'sha256': 72 '493698c81fffcabc340c72831b175962dba5a31dfe8572a6d5af083a116af4f8', 73 'platform': 74 'linux', 75 'machine': ['x86_64'] 76}, { 77 'arch': 78 'linux-arm', 79 'file_name': 80 'trace_processor_shell', 81 'file_size': 82 6125384, 83 'url': 84 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/linux-arm/trace_processor_shell', 85 'sha256': 86 '53f1e27603695cf92d22519993b6eafa9c60957d9cb33bd0b300df8573b87ebb', 87 'platform': 88 'linux', 89 'machine': ['armv6l', 'armv7l', 'armv8l'] 90}, { 91 'arch': 92 'linux-arm64', 93 'file_name': 94 'trace_processor_shell', 95 'file_size': 96 8036288, 97 'url': 98 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/linux-arm64/trace_processor_shell', 99 'sha256': 100 '2a2cda222c9d5e18b638057688babb00a3a975ccd4b7dd65f26211c2cb7767f9', 101 'platform': 102 'linux', 103 'machine': ['aarch64'] 104}, { 105 'arch': 106 'android-arm', 107 'file_name': 108 'trace_processor_shell', 109 'file_size': 110 5813384, 111 'url': 112 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/android-arm/trace_processor_shell', 113 'sha256': 114 'f3ec4c194d0b06af5b296c1c479e6b29090e6b7cc7e58fbd55ca2919a126f0ee' 115}, { 116 'arch': 117 'android-arm64', 118 'file_name': 119 'trace_processor_shell', 120 'file_size': 121 7294768, 122 'url': 123 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/android-arm64/trace_processor_shell', 124 'sha256': 125 'f44f47d4b873ec68b6fa4f4c69a3e5a13d58b4d9cb2ec591fa687d4480c1950b' 126}, { 127 'arch': 128 'android-x86', 129 'file_name': 130 'trace_processor_shell', 131 'file_size': 132 8090716, 133 'url': 134 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/android-x86/trace_processor_shell', 135 'sha256': 136 '5636d8251747376787640bc3a4894ecf3091e4bf3d38b007003e1992fc5792df' 137}, { 138 'arch': 139 'android-x64', 140 'file_name': 141 'trace_processor_shell', 142 'file_size': 143 8359784, 144 'url': 145 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/android-x64/trace_processor_shell', 146 'sha256': 147 '50440fa055ab998f6cf24f9a9a7388520cc854708735521505e10291bc52f3d0' 148}, { 149 'arch': 150 'windows-amd64', 151 'file_name': 152 'trace_processor_shell.exe', 153 'file_size': 154 8130560, 155 'url': 156 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v34.0/windows-amd64/trace_processor_shell.exe', 157 'sha256': 158 '5cbcf98e29a2d989523235e11e4e0dade692a295ebf47a6c93a09a050ce9bc91', 159 'platform': 160 'win32', 161 'machine': ['amd64'] 162}] 163 164# ----- Amalgamator: end of python/perfetto/prebuilts/manifests/trace_processor_shell.py 165 166# ----- Amalgamator: begin of python/perfetto/prebuilts/perfetto_prebuilts.py 167# Copyright (C) 2021 The Android Open Source Project 168# 169# Licensed under the Apache License, Version 2.0 (the "License"); 170# you may not use this file except in compliance with the License. 171# You may obtain a copy of the License at 172# 173# http://www.apache.org/licenses/LICENSE-2.0 174# 175# Unless required by applicable law or agreed to in writing, software 176# distributed under the License is distributed on an "AS IS" BASIS, 177# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 178# See the License for the specific language governing permissions and 179# limitations under the License. 180""" 181Functions to fetch pre-pinned Perfetto prebuilts. 182 183This function is used in different places: 184- Into the //tools/{trace_processor, traceconv} scripts, which are just plain 185 wrappers around executables. 186- Into the //tools/{heap_profiler, record_android_trace} scripts, which contain 187 some other hand-written python code. 188 189The manifest argument looks as follows: 190TRACECONV_MANIFEST = [ 191 { 192 'arch': 'mac-amd64', 193 'file_name': 'traceconv', 194 'file_size': 7087080, 195 'url': https://commondatastorage.googleapis.com/.../trace_to_text', 196 'sha256': 7d957c005b0dc130f5bd855d6cec27e060d38841b320d04840afc569f9087490', 197 'platform': 'darwin', 198 'machine': 'x86_64' 199 }, 200 ... 201] 202 203The intended usage is: 204 205 from perfetto.prebuilts.manifests.traceconv import TRACECONV_MANIFEST 206 bin_path = get_perfetto_prebuilt(TRACECONV_MANIFEST) 207 subprocess.call(bin_path, ...) 208""" 209 210import hashlib 211import os 212import platform 213import subprocess 214import sys 215 216 217def download_or_get_cached(file_name, url, sha256): 218 """ Downloads a prebuilt or returns a cached version 219 220 The first time this is invoked, it downloads the |url| and caches it into 221 ~/.local/share/perfetto/prebuilts/$tool_name. On subsequent invocations it 222 just runs the cached version. 223 """ 224 dir = os.path.join( 225 os.path.expanduser('~'), '.local', 'share', 'perfetto', 'prebuilts') 226 os.makedirs(dir, exist_ok=True) 227 bin_path = os.path.join(dir, file_name) 228 sha256_path = os.path.join(dir, file_name + '.sha256') 229 needs_download = True 230 231 # Avoid recomputing the SHA-256 on each invocation. The SHA-256 of the last 232 # download is cached into file_name.sha256, just check if that matches. 233 if os.path.exists(bin_path) and os.path.exists(sha256_path): 234 with open(sha256_path, 'rb') as f: 235 digest = f.read().decode() 236 if digest == sha256: 237 needs_download = False 238 239 if needs_download: 240 # Either the filed doesn't exist or the SHA256 doesn't match. 241 tmp_path = bin_path + '.tmp' 242 print('Downloading ' + url) 243 subprocess.check_call(['curl', '-f', '-L', '-#', '-o', tmp_path, url]) 244 with open(tmp_path, 'rb') as fd: 245 actual_sha256 = hashlib.sha256(fd.read()).hexdigest() 246 if actual_sha256 != sha256: 247 raise Exception('Checksum mismatch for %s (actual: %s, expected: %s)' % 248 (url, actual_sha256, sha256)) 249 os.chmod(tmp_path, 0o755) 250 os.replace(tmp_path, bin_path) 251 with open(sha256_path, 'w') as f: 252 f.write(sha256) 253 return bin_path 254 255 256def get_perfetto_prebuilt(manifest, soft_fail=False, arch=None): 257 """ Downloads the prebuilt, if necessary, and returns its path on disk. """ 258 plat = sys.platform.lower() 259 machine = platform.machine().lower() 260 manifest_entry = None 261 for entry in manifest: 262 # If the caller overrides the arch, just match that (for Android prebuilts). 263 if arch: 264 if entry.get('arch') == arch: 265 manifest_entry = entry 266 break 267 continue 268 # Otherwise guess the local machine arch. 269 if entry.get('platform') == plat and machine in entry.get('machine', []): 270 manifest_entry = entry 271 break 272 if manifest_entry is None: 273 if soft_fail: 274 return None 275 raise Exception( 276 ('No prebuilts available for %s-%s\n' % (plat, machine)) + 277 'See https://perfetto.dev/docs/contributing/build-instructions') 278 279 return download_or_get_cached( 280 file_name=manifest_entry['file_name'], 281 url=manifest_entry['url'], 282 sha256=manifest_entry['sha256']) 283 284 285def run_perfetto_prebuilt(manifest): 286 bin_path = get_perfetto_prebuilt(manifest) 287 if sys.platform.lower() == 'win32': 288 sys.exit(subprocess.check_call([bin_path, *sys.argv[1:]])) 289 os.execv(bin_path, [bin_path] + sys.argv[1:]) 290 291# ----- Amalgamator: end of python/perfetto/prebuilts/perfetto_prebuilts.py 292 293if __name__ == '__main__': 294 run_perfetto_prebuilt(TRACE_PROCESSOR_SHELL_MANIFEST) 295 296#EOF 297