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