1# Copyright (C) 2021 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14""" 15Functions to fetch pre-pinned Perfetto prebuilts. 16 17This function is used in different places: 18- Into the //tools/{trace_processor, traceconv} scripts, which are just plain 19 wrappers around executables. 20- Into the //tools/{heap_profiler, record_android_trace} scripts, which contain 21 some other hand-written python code. 22 23The manifest argument looks as follows: 24TRACECONV_MANIFEST = [ 25 { 26 'arch': 'mac-amd64', 27 'file_name': 'traceconv', 28 'file_size': 7087080, 29 'url': https://commondatastorage.googleapis.com/.../trace_to_text', 30 'sha256': 7d957c005b0dc130f5bd855d6cec27e060d38841b320d04840afc569f9087490', 31 'platform': 'darwin', 32 'machine': 'x86_64' 33 }, 34 ... 35] 36 37The intended usage is: 38 39 from perfetto.prebuilts.manifests.traceconv import TRACECONV_MANIFEST 40 bin_path = get_perfetto_prebuilt(TRACECONV_MANIFEST) 41 subprocess.call(bin_path, ...) 42""" 43 44import hashlib 45import os 46import platform 47import random 48import subprocess 49import sys 50 51 52def download_or_get_cached(file_name, url, sha256): 53 """ Downloads a prebuilt or returns a cached version 54 55 The first time this is invoked, it downloads the |url| and caches it into 56 ~/.local/share/perfetto/prebuilts/$tool_name. On subsequent invocations it 57 just runs the cached version. 58 """ 59 dir = os.path.join( 60 os.path.expanduser('~'), '.local', 'share', 'perfetto', 'prebuilts') 61 os.makedirs(dir, exist_ok=True) 62 bin_path = os.path.join(dir, file_name) 63 sha256_path = os.path.join(dir, file_name + '.sha256') 64 needs_download = True 65 66 # Avoid recomputing the SHA-256 on each invocation. The SHA-256 of the last 67 # download is cached into file_name.sha256, just check if that matches. 68 if os.path.exists(bin_path) and os.path.exists(sha256_path): 69 with open(sha256_path, 'rb') as f: 70 digest = f.read().decode() 71 if digest == sha256: 72 needs_download = False 73 74 if needs_download: # The file doesn't exist or the SHA256 doesn't match. 75 # Use a unique random file to guard against concurrent executions. 76 # See https://github.com/google/perfetto/issues/786 . 77 tmp_path = '%s.%d.tmp' % (bin_path, random.randint(0, 100000)) 78 print('Downloading ' + url) 79 subprocess.check_call(['curl', '-f', '-L', '-#', '-o', tmp_path, url]) 80 with open(tmp_path, 'rb') as fd: 81 actual_sha256 = hashlib.sha256(fd.read()).hexdigest() 82 if actual_sha256 != sha256: 83 raise Exception('Checksum mismatch for %s (actual: %s, expected: %s)' % 84 (url, actual_sha256, sha256)) 85 os.chmod(tmp_path, 0o755) 86 os.replace(tmp_path, bin_path) 87 with open(tmp_path, 'w') as f: 88 f.write(sha256) 89 os.replace(tmp_path, sha256_path) 90 return bin_path 91 92 93def get_perfetto_prebuilt(manifest, soft_fail=False, arch=None): 94 """ Downloads the prebuilt, if necessary, and returns its path on disk. """ 95 plat = sys.platform.lower() 96 machine = platform.machine().lower() 97 manifest_entry = None 98 for entry in manifest: 99 # If the caller overrides the arch, just match that (for Android prebuilts). 100 if arch: 101 if entry.get('arch') == arch: 102 manifest_entry = entry 103 break 104 continue 105 # Otherwise guess the local machine arch. 106 if entry.get('platform') == plat and machine in entry.get('machine', []): 107 manifest_entry = entry 108 break 109 if manifest_entry is None: 110 if soft_fail: 111 return None 112 raise Exception( 113 ('No prebuilts available for %s-%s\n' % (plat, machine)) + 114 'See https://perfetto.dev/docs/contributing/build-instructions') 115 116 return download_or_get_cached( 117 file_name=manifest_entry['file_name'], 118 url=manifest_entry['url'], 119 sha256=manifest_entry['sha256']) 120 121 122def run_perfetto_prebuilt(manifest): 123 bin_path = get_perfetto_prebuilt(manifest) 124 if sys.platform.lower() == 'win32': 125 sys.exit(subprocess.check_call([bin_path, *sys.argv[1:]])) 126 os.execv(bin_path, [bin_path] + sys.argv[1:]) 127