1 /*
2 * Copyright (C) 2018 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
17 #include <emscripten/emscripten.h>
18
19 #include "perfetto/base/logging.h"
20 #include "perfetto/trace_processor/trace_processor.h"
21 #include "src/trace_processor/rpc/rpc.h"
22
23 namespace perfetto {
24 namespace trace_processor {
25
26 namespace {
27 Rpc* g_trace_processor_rpc;
28
29 // The buffer used to pass the request arguments. The caller (JS) decides how
30 // big this buffer should be in the Initialize() call.
31 uint8_t* g_req_buf;
32 } // namespace
33
34 // +---------------------------------------------------------------------------+
35 // | Exported functions called by the JS/TS running in the worker. |
36 // +---------------------------------------------------------------------------+
37 extern "C" {
38
39 // Returns the address of the allocated request buffer.
40 uint8_t* EMSCRIPTEN_KEEPALIVE trace_processor_rpc_init(Rpc::RpcResponseFunction,
41 uint32_t);
trace_processor_rpc_init(Rpc::RpcResponseFunction resp_function,uint32_t req_buffer_size)42 uint8_t* trace_processor_rpc_init(Rpc::RpcResponseFunction resp_function,
43 uint32_t req_buffer_size) {
44 g_trace_processor_rpc = new Rpc();
45
46 // |resp_function| is a JS-bound function passed by wasm_bridge.ts. It will
47 // call back into JavaScript. There the JS code will copy the passed
48 // buffer with the response (a proto-encoded TraceProcessorRpc message) and
49 // postMessage() it to the controller. See the comment in wasm_bridge.ts for
50 // an overview of the JS<>Wasm callstack.
51 g_trace_processor_rpc->SetRpcResponseFunction(resp_function);
52
53 g_req_buf = new uint8_t[req_buffer_size];
54 return g_req_buf;
55 }
56
57 void EMSCRIPTEN_KEEPALIVE trace_processor_on_rpc_request(uint32_t);
trace_processor_on_rpc_request(uint32_t size)58 void trace_processor_on_rpc_request(uint32_t size) {
59 g_trace_processor_rpc->OnRpcRequest(g_req_buf, size);
60 }
61
62 } // extern "C"
63 } // namespace trace_processor
64 } // namespace perfetto
65
main(int,char **)66 int main(int, char**) {
67 // This is unused but is needed for the following reasons:
68 // - We need the callMain() Emscripten JS helper function for traceconv (but
69 // not for trace_processor).
70 // - Newer versions of emscripten require that callMain is explicitly exported
71 // via EXTRA_EXPORTED_RUNTIME_METHODS = ['callMain'].
72 // - We have one set of EXTRA_EXPORTED_RUNTIME_METHODS for both
73 // trace_processor.wasm (which does not need a main()) and traceconv (which
74 // does).
75 // - Without this main(), the Wasm bootstrap code will cause a JS error at
76 // runtime when trying to load trace_processor.js.
77 return 0;
78 }
79