1#!/usr/bin/env python3 2# Copyright (C) 2020 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 16import os 17import subprocess 18import sys 19import time 20from urllib import request, error 21 22from perfetto.trace_processor.platform import PlatformDelegate 23 24# Default port that trace_processor_shell runs on 25TP_PORT = 9001 26 27 28def load_shell(bin_path: str, unique_port: bool, verbose: bool, 29 ingest_ftrace_in_raw: bool, enable_dev_features: bool, 30 platform_delegate: PlatformDelegate): 31 addr, port = platform_delegate.get_bind_addr( 32 port=0 if unique_port else TP_PORT) 33 url = f'{addr}:{str(port)}' 34 35 shell_path = platform_delegate.get_shell_path(bin_path=bin_path) 36 if os.name == 'nt' and not shell_path.endswith('.exe'): 37 tp_exec = [sys.executable, shell_path] 38 else: 39 tp_exec = [shell_path] 40 41 args = ['-D', '--http-port', str(port)] 42 if not ingest_ftrace_in_raw: 43 args.append('--no-ftrace-raw') 44 45 if enable_dev_features: 46 args.append('--dev') 47 48 p = subprocess.Popen( 49 tp_exec + args, 50 stdin=subprocess.DEVNULL, 51 stdout=subprocess.DEVNULL, 52 stderr=None if verbose else subprocess.DEVNULL) 53 54 success = False 55 for _ in range(3): 56 try: 57 if p.poll() is None: 58 _ = request.urlretrieve(f'http://{url}/status') 59 success = True 60 break 61 except error.URLError: 62 time.sleep(1) 63 64 if not success: 65 raise Exception( 66 "Trace processor failed to start. Try rerunning with " 67 "verbose=True in TraceProcessorConfig for more detailed " 68 "information and file a bug at https://goto.google.com/perfetto-bug " 69 "or https://github.com/google/perfetto/issues if necessary.") 70 71 return url, p 72