1// Copyright (C) 2018 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
15import '../tracks/all_frontend';
16import * as m from 'mithril';
17
18import {Actions} from '../common/actions';
19import {HttpRpcEngine, RPC_URL} from '../common/http_rpc_engine';
20
21import {globals} from './globals';
22import {showModal} from './modal';
23
24const PROMPT = `Trace Processor Native Accelerator detected on ${RPC_URL} with:
25$loadedTraceName
26
27YES, use loaded trace:
28Will load from the current state of Trace Processor. If you did run
29trace_processor_shell --httpd file.pftrace this is likely what you want.
30
31YES, but reset state:
32Use this if you want to open another trace but still use the
33accelerator. This is the equivalent of killing and restarting
34trace_processor_shell --httpd.
35
36NO, Use builtin WASM:
37Will not use the accelerator in this tab.
38
39Using the native accelerator has some minor caveats:
40- Only one tab can be using the accelerator.
41- Sharing, downloading and conversion-to-legacy aren't supported.
42- You may encounter UI errors if the Trace Processor version you are using is
43too old. Get the latest version from get.perfetto.dev/trace_processor.
44`;
45
46// Try to connect to the external Trace Processor HTTP RPC accelerator (if
47// available, often it isn't). If connected it will populate the
48// |httpRpcState| in the frontend local state. In turn that will show the UI
49// chip in the sidebar. trace_controller.ts will repeat this check before
50// trying to load a new trace. We do this ahead of time just to have a
51// consistent UX (i.e. so that the user can tell if the RPC is working without
52// having to open a trace).
53export async function CheckHttpRpcConnection(): Promise<void> {
54  const state = await HttpRpcEngine.checkConnection();
55  globals.frontendLocalState.setHttpRpcState(state);
56
57  // If a trace is already loaded in the trace processor (e.g., the user
58  // launched trace_processor_shell -D trace_file.pftrace), prompt the user to
59  // initialize the UI with the already-loaded trace.
60  if (state.connected && state.loadedTraceName) {
61    showModal({
62      title: 'Use Trace Processor Native Acceleration?',
63      content:
64          m('.modal-pre',
65            PROMPT.replace('$loadedTraceName', state.loadedTraceName)),
66      buttons: [
67        {
68          text: 'YES, use loaded trace',
69          primary: true,
70          id: 'rpc_load',
71          action: () => {
72            globals.dispatch(Actions.openTraceFromHttpRpc({}));
73          }
74        },
75        {
76          text: 'YES, but reset state',
77          primary: false,
78          id: 'rpc_reset',
79          action: () => {}
80        },
81        {
82          text: 'NO, Use builtin WASM',
83          primary: false,
84          id: 'rpc_force_wasm',
85          action: () => {
86            globals.dispatch(
87                Actions.setNewEngineMode({mode: 'FORCE_BUILTIN_WASM'}));
88          }
89        },
90      ],
91    });
92  }
93}
94