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 v48.1 34TRACEBOX_MANIFEST = [{ 35 'arch': 36 'mac-amd64', 37 'file_name': 38 'tracebox', 39 'file_size': 40 1613864, 41 'url': 42 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v48.1/mac-amd64/tracebox', 43 'sha256': 44 'dfb1a3affe905d2e7d1f82bc4dda46b1fda6db054d60ae87c3215dd529b77fee', 45 'platform': 46 'darwin', 47 'machine': ['x86_64'] 48}, { 49 'arch': 50 'mac-arm64', 51 'file_name': 52 'tracebox', 53 'file_size': 54 1492184, 55 'url': 56 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v48.1/mac-arm64/tracebox', 57 'sha256': 58 '4a492a629dd1f13f3146c4b8267c0b163afba8cef1d49e0c00c48bb727496066', 59 'platform': 60 'darwin', 61 'machine': ['arm64'] 62}, { 63 'arch': 64 'linux-amd64', 65 'file_name': 66 'tracebox', 67 'file_size': 68 2380040, 69 'url': 70 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v48.1/linux-amd64/tracebox', 71 'sha256': 72 'd70b284e8c28858fd539ae61ca59764d7f9fd6232073c304926e892fe75e692a', 73 'platform': 74 'linux', 75 'machine': ['x86_64'] 76}, { 77 'arch': 78 'linux-arm', 79 'file_name': 80 'tracebox', 81 'file_size': 82 1450708, 83 'url': 84 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v48.1/linux-arm/tracebox', 85 'sha256': 86 '178fa6a1a9bc80f72d81938d40fe201c25c595ffaff7e030d59c2af09dfcc06c', 87 'platform': 88 'linux', 89 'machine': ['armv6l', 'armv7l', 'armv8l'] 90}, { 91 'arch': 92 'linux-arm64', 93 'file_name': 94 'tracebox', 95 'file_size': 96 2269816, 97 'url': 98 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v48.1/linux-arm64/tracebox', 99 'sha256': 100 '42c64f9807756aaa08a2bfa13e9e4828c193a6b90ba1329408873c3ebf5adf3f', 101 'platform': 102 'linux', 103 'machine': ['aarch64'] 104}, { 105 'arch': 106 'android-arm', 107 'file_name': 108 'tracebox', 109 'file_size': 110 1333336, 111 'url': 112 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v48.1/android-arm/tracebox', 113 'sha256': 114 '93a78d2c42e3c00f117e2f155326383f69c891281ed693a39d87b8cb54ca4e19' 115}, { 116 'arch': 117 'android-arm64', 118 'file_name': 119 'tracebox', 120 'file_size': 121 2115984, 122 'url': 123 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v48.1/android-arm64/tracebox', 124 'sha256': 125 '508248a9e47ab605fd742efb700391d7267b68b586199a93e13e6ca14b72fe3d' 126}, { 127 'arch': 128 'android-x86', 129 'file_name': 130 'tracebox', 131 'file_size': 132 2302960, 133 'url': 134 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v48.1/android-x86/tracebox', 135 'sha256': 136 '63d20a69c4e0c291329d7917e640fa0d4f146c344e79988e87393b1431d594b1' 137}, { 138 'arch': 139 'android-x64', 140 'file_name': 141 'tracebox', 142 'file_size': 143 2147880, 144 'url': 145 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v48.1/android-x64/tracebox', 146 'sha256': 147 'c0ea1d5fd6d020e4c2b45d4d45cdd0c44ae63cd755d69260a3e5d2bacd3cbd6a' 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