-
Notifications
You must be signed in to change notification settings - Fork 0
/
perfetto
executable file
·84 lines (68 loc) · 2.66 KB
/
perfetto
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
#
# Copyright (C) 2021 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Based on https://cs.android.com/android/platform/superproject/+/main:external/perfetto/tools/record_android_trace
import argcomplete
import argparse
import http.server
import os
import socketserver
import sys
import webbrowser
# HTTP Server used to open the trace in the browser.
class HttpHandler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header('Access-Control-Allow-Origin', self.server.allow_origin)
self.send_header('Cache-Control', 'no-cache')
super().end_headers()
def do_GET(self):
if self.path != '/' + self.server.expected_fname:
self.send_error(404, "File not found")
return
self.server.fname_get_completed = True
super().do_GET()
def do_POST(self):
self.send_error(404, "File not found")
def open_trace_in_browser(path, origin):
# We reuse the HTTP+RPC port because it's the only one allowed by the CSP.
PORT = 9001
path = os.path.abspath(path)
os.chdir(os.path.dirname(path))
fname = os.path.basename(path)
socketserver.TCPServer.allow_reuse_address = True
with socketserver.TCPServer(('127.0.0.1', PORT), HttpHandler) as httpd:
address = f'{origin}/#!/?url=http://127.0.0.1:{PORT}/{fname}'
webbrowser.open_new_tab(address)
httpd.expected_fname = fname
httpd.fname_get_completed = None
httpd.allow_origin = origin
while httpd.fname_get_completed is None:
httpd.handle_request()
def main():
parser = argparse.ArgumentParser()
help = 'The web address used to open trace files'
parser.add_argument('--origin', default='https://ui.perfetto.dev', help=help)
help = 'The trace file to open'
completer = argcomplete.completers.FilesCompleter(['*.trace', '*.ftrace', '*.pftrace', '*.perfetto-trace'])
parser.add_argument('trace_file', nargs='?', help=help).completer = completer
argcomplete.autocomplete(parser)
args = parser.parse_args()
if args.trace_file:
open_trace_in_browser(args.trace_file, args.origin)
else:
webbrowser.open_new_tab(args.origin)
if __name__ == '__main__':
sys.exit(main())