1 /*
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 
17 #ifndef SRC_TRACE_PROCESSOR_RPC_RPC_H_
18 #define SRC_TRACE_PROCESSOR_RPC_RPC_H_
19 
20 #include <memory>
21 #include <vector>
22 
23 #include <stddef.h>
24 #include <stdint.h>
25 
26 #include "perfetto/trace_processor/status.h"
27 
28 namespace perfetto {
29 namespace trace_processor {
30 
31 class TraceProcessor;
32 
33 // This class handles the binary {,un}marshalling for the Trace Processor RPC
34 // API (see protos/perfetto/trace_processor/trace_processor.proto).
35 // This is to deal with cases where the client of the trace processor is not
36 // some in-process C++ code but a remote process:
37 // There are two use cases of this:
38 //   1. The JS<>WASM interop for the web-based UI.
39 //   2. The HTTP RPC mode of trace_processor_shell that allows the UI to talk
40 //      to a native trace processor instead of the bundled WASM one.
41 // This class has (a subset of) the same methods of the public TraceProcessor
42 // interface, but the methods just take and return proto-encoded binary buffers.
43 // This class does NOT define how the transport works (e.g. HTTP vs WASM interop
44 // calls), it just deals with {,un}marshalling.
45 // This class internally creates and owns a TraceProcessor instance, which
46 // lifetime is tied to the lifetime of the Rpc instance.
47 class Rpc {
48  public:
49   // The unique_ptr argument is optional. If non-null it will adopt the passed
50   // instance and allow to directly query that. If null, a new instanace will be
51   // created internally by calling Parse().
52   explicit Rpc(std::unique_ptr<TraceProcessor>);
53   Rpc();
54   ~Rpc();
55 
56   // The methods of this class are mirrors (modulo {un,}marshalling of args) of
57   // the corresponding names in trace_processor.h . See that header for docs.
58 
59   util::Status Parse(const uint8_t* data, size_t len);
60   void NotifyEndOfFile();
61   std::vector<uint8_t> RawQuery(const uint8_t* args, size_t len);
62   void RestoreInitialTables();
63   std::string GetCurrentTraceName();
64 
65  private:
66   void MaybePrintProgress();
67 
68   std::unique_ptr<TraceProcessor> trace_processor_;
69   bool eof_ = true;  // Reset when calling Parse().
70   int64_t t_parse_started_ = 0;
71   size_t bytes_last_progress_ = 0;
72   size_t bytes_parsed_ = 0;
73 };
74 
75 }  // namespace trace_processor
76 }  // namespace perfetto
77 
78 #endif  // SRC_TRACE_PROCESSOR_RPC_RPC_H_
79