• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# Copyright (C) 2019 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# This file should do the same thing when being invoked in any of these ways:
17# ./trace_processor
18# python trace_processor
19# bash trace_processor
20# cat ./trace_processor | bash
21# cat ./trace_processor | python -
22
23BASH_FALLBACK = """ "
24exec python3 - "$@" <<'#'EOF
25#"""
26
27TOOL_NAME = 'trace_processor_shell'
28
29# BEGIN_SECTION_GENERATED_BY(roll-prebuilts)
30# Revision: v25.0
31PERFETTO_PREBUILT_MANIFEST = [{
32    'tool':
33        'trace_processor_shell',
34    'arch':
35        'mac-amd64',
36    'file_name':
37        'trace_processor_shell',
38    'file_size':
39        7302832,
40    'url':
41        'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v25.0/mac-amd64/trace_processor_shell',
42    'sha256':
43        'edc841a7f6769959f3b03f1e93dcd263d30864dc3e1fc4e3fcdbe4e44e19b02a',
44    'platform':
45        'darwin',
46    'machine': ['x86_64']
47}, {
48    'tool':
49        'trace_processor_shell',
50    'arch':
51        'mac-arm64',
52    'file_name':
53        'trace_processor_shell',
54    'file_size':
55        6393768,
56    'url':
57        'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v25.0/mac-arm64/trace_processor_shell',
58    'sha256':
59        '9f768e35d85501d18f09b0313c037a34f3306663d504f31e4874b1b3b20a00d6',
60    'platform':
61        'darwin',
62    'machine': ['arm64']
63}, {
64    'tool':
65        'trace_processor_shell',
66    'arch':
67        'windows-amd64',
68    'file_name':
69        'trace_processor_shell.exe',
70    'file_size':
71        6942720,
72    'url':
73        'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v25.0/windows-amd64/trace_processor_shell.exe',
74    'sha256':
75        'c7118d820f720ca99acc9ef736f553cb0ba01d034e8f61b83f44d857f76058ce',
76    'platform':
77        'win32',
78    'machine': ['amd64']
79}, {
80    'tool':
81        'trace_processor_shell',
82    'arch':
83        'linux-amd64',
84    'file_name':
85        'trace_processor_shell',
86    'file_size':
87        7824920,
88    'url':
89        'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v25.0/linux-amd64/trace_processor_shell',
90    'sha256':
91        '77ec0f9dbec36079d83f2843372446c354d04e7971c1f159c465d4e8e78a3d4d',
92    'platform':
93        'linux',
94    'machine': ['x86_64']
95}, {
96    'tool':
97        'trace_processor_shell',
98    'arch':
99        'linux-arm',
100    'file_name':
101        'trace_processor_shell',
102    'file_size':
103        5076040,
104    'url':
105        'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v25.0/linux-arm/trace_processor_shell',
106    'sha256':
107        '7290ef3bda83736d0076e49bab5524294906bc8d1785ccef876ba8bd490e62b9',
108    'platform':
109        'linux',
110    'machine': ['armv6l', 'armv7l', 'armv8l']
111}, {
112    'tool':
113        'trace_processor_shell',
114    'arch':
115        'linux-arm64',
116    'file_name':
117        'trace_processor_shell',
118    'file_size':
119        7055416,
120    'url':
121        'https://commondatastorage.googleapis.com/perfetto-luci-artifacts/v25.0/linux-arm64/trace_processor_shell',
122    'sha256':
123        'b3d91b69a4152419bc3de2df3aeaef400d829e4a87e5c5cb985af0b56f319279',
124    'platform':
125        'linux',
126    'machine': ['aarch64']
127}]
128
129
130# DO NOT EDIT. If you wish to make edits to this code, you need to change only
131# //tools/get_perfetto_prebuilt.py and run /tools/roll-prebuilts to regenerate
132# all the others scripts this is embedded into.
133def get_perfetto_prebuilt(tool_name, soft_fail=False, arch=None):
134  """ Downloads the prebuilt, if necessary, and returns its path on disk. """
135
136  # The first time this is invoked, it downloads the |url| and caches it into
137  # ~/.perfetto/prebuilts/$tool_name. On subsequent invocations it just runs the
138  # cached version.
139  def download_or_get_cached(file_name, url, sha256):
140    import os, hashlib, subprocess
141    dir = os.path.join(
142        os.path.expanduser('~'), '.local', 'share', 'perfetto', 'prebuilts')
143    os.makedirs(dir, exist_ok=True)
144    bin_path = os.path.join(dir, file_name)
145    sha256_path = os.path.join(dir, file_name + '.sha256')
146    needs_download = True
147
148    # Avoid recomputing the SHA-256 on each invocation. The SHA-256 of the last
149    # download is cached into file_name.sha256, just check if that matches.
150    if os.path.exists(bin_path) and os.path.exists(sha256_path):
151      with open(sha256_path, 'rb') as f:
152        digest = f.read().decode()
153        if digest == sha256:
154          needs_download = False
155
156    if needs_download:
157      # Either the filed doesn't exist or the SHA256 doesn't match.
158      tmp_path = bin_path + '.tmp'
159      print('Downloading ' + url)
160      subprocess.check_call(['curl', '-f', '-L', '-#', '-o', tmp_path, url])
161      with open(tmp_path, 'rb') as fd:
162        actual_sha256 = hashlib.sha256(fd.read()).hexdigest()
163      if actual_sha256 != sha256:
164        raise Exception('Checksum mismatch for %s (actual: %s, expected: %s)' %
165                        (url, actual_sha256, sha256))
166      os.chmod(tmp_path, 0o755)
167      os.rename(tmp_path, bin_path)
168      with open(sha256_path, 'w') as f:
169        f.write(sha256)
170    return bin_path
171    # --- end of download_or_get_cached() ---
172
173  # --- get_perfetto_prebuilt() function starts here. ---
174  import os, platform, sys
175  plat = sys.platform.lower()
176  machine = platform.machine().lower()
177  manifest_entry = None
178  for entry in PERFETTO_PREBUILT_MANIFEST:
179    # If the caller overrides the arch, just match that (for Android prebuilts).
180    if arch and entry.get('arch') == arch:
181      manifest_entry = entry
182      break
183    # Otherwise guess the local machine arch.
184    if entry.get('tool') == tool_name and entry.get(
185        'platform') == plat and machine in entry.get('machine', []):
186      manifest_entry = entry
187      break
188  if manifest_entry is None:
189    if soft_fail:
190      return None
191    raise Exception(
192        ('No prebuilts available for %s-%s\n' % (plat, machine)) +
193        'See https://perfetto.dev/docs/contributing/build-instructions')
194
195  return download_or_get_cached(
196      file_name=manifest_entry['file_name'],
197      url=manifest_entry['url'],
198      sha256=manifest_entry['sha256'])
199
200
201# END_SECTION_GENERATED_BY(roll-prebuilts)
202
203if __name__ == '__main__':
204  import sys, os
205  bin_path = get_perfetto_prebuilt(TOOL_NAME)
206  os.execv(bin_path, [bin_path] + sys.argv[1:])
207
208#EOF
209