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 v49.0 34TRACEBOX_MANIFEST = [{ 35 'arch': 36 'mac-amd64', 37 'file_name': 38 'tracebox', 39 'file_size': 40 1646808, 41 'url': 42 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v49.0/mac-amd64/tracebox', 43 'sha256': 44 '85b3060ed4d49e2c8d69dbb4d6ff26ab662f9b28c0032791674c90683dd33d39', 45 'platform': 46 'darwin', 47 'machine': ['x86_64'] 48}, { 49 'arch': 50 'mac-arm64', 51 'file_name': 52 'tracebox', 53 'file_size': 54 1508856, 55 'url': 56 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v49.0/mac-arm64/tracebox', 57 'sha256': 58 'ea2cce845daf0eba469ff356b3bcefc8e9a384084569271a470b58a9dcbf8def', 59 'platform': 60 'darwin', 61 'machine': ['arm64'] 62}, { 63 'arch': 64 'linux-amd64', 65 'file_name': 66 'tracebox', 67 'file_size': 68 2415168, 69 'url': 70 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v49.0/linux-amd64/tracebox', 71 'sha256': 72 '5361676fb3c2490ae2136ab7a37dcd9e4ee5a2a6c0ba722facf3215a23a8c633', 73 'platform': 74 'linux', 75 'machine': ['x86_64'] 76}, { 77 'arch': 78 'linux-arm', 79 'file_name': 80 'tracebox', 81 'file_size': 82 1478024, 83 'url': 84 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v49.0/linux-arm/tracebox', 85 'sha256': 86 '18db321576be555d8c9281df9fc03aa6b3b4358ae2424ffbd65fc120cd650b8b', 87 'platform': 88 'linux', 89 'machine': ['armv6l', 'armv7l', 'armv8l'] 90}, { 91 'arch': 92 'linux-arm64', 93 'file_name': 94 'tracebox', 95 'file_size': 96 2304384, 97 'url': 98 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v49.0/linux-arm64/tracebox', 99 'sha256': 100 '70c9e2b63eb92a82db65916c346b09867bfedc0c4593974c019102f485c0dc9d', 101 'platform': 102 'linux', 103 'machine': ['aarch64'] 104}, { 105 'arch': 106 'android-arm', 107 'file_name': 108 'tracebox', 109 'file_size': 110 1354916, 111 'url': 112 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v49.0/android-arm/tracebox', 113 'sha256': 114 '724a1cb4774bdf8a64beb37194f7394df5a052c36369ea52f64fe519fcb40117' 115}, { 116 'arch': 117 'android-arm64', 118 'file_name': 119 'tracebox', 120 'file_size': 121 2142008, 122 'url': 123 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v49.0/android-arm64/tracebox', 124 'sha256': 125 '7616bfc3be1269c3ac1eec5a1f868fb65c2830ed001b5fbcc3800c909c676848' 126}, { 127 'arch': 128 'android-x86', 129 'file_name': 130 'tracebox', 131 'file_size': 132 2341884, 133 'url': 134 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v49.0/android-x86/tracebox', 135 'sha256': 136 '29124bee9bf4e2e296b0c96071b8c9706b57d963cbf0359d6afd95a9049b2b82' 137}, { 138 'arch': 139 'android-x64', 140 'file_name': 141 'tracebox', 142 'file_size': 143 2178416, 144 'url': 145 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v49.0/android-x64/tracebox', 146 'sha256': 147 '826fffce1e138c1d5ac107492ee696c09ad83f9ae9aa647c810d71084f797509' 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