• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <functional>
21 #include <memory>
22 #include <vector>
23 
24 #include <stddef.h>
25 #include <stdint.h>
26 
27 #include "perfetto/trace_processor/status.h"
28 
29 namespace perfetto {
30 namespace trace_processor {
31 
32 class TraceProcessor;
33 
34 // This class handles the binary {,un}marshalling for the Trace Processor RPC
35 // API (see protos/perfetto/trace_processor/trace_processor.proto).
36 // This is to deal with cases where the client of the trace processor is not
37 // some in-process C++ code but a remote process:
38 // There are two use cases of this:
39 //   1. The JS<>WASM interop for the web-based UI.
40 //   2. The HTTP RPC mode of trace_processor_shell that allows the UI to talk
41 //      to a native trace processor instead of the bundled WASM one.
42 // This class has (a subset of) the same methods of the public TraceProcessor
43 // interface, but the methods just take and return proto-encoded binary buffers.
44 // This class does NOT define how the transport works (e.g. HTTP vs WASM interop
45 // calls), it just deals with {,un}marshalling.
46 // This class internally creates and owns a TraceProcessor instance, which
47 // lifetime is tied to the lifetime of the Rpc instance.
48 class Rpc {
49  public:
50   // The unique_ptr argument is optional. If non-null it will adopt the passed
51   // instance and allow to directly query that. If null, a new instanace will be
52   // created internally by calling Parse().
53   explicit Rpc(std::unique_ptr<TraceProcessor>);
54   Rpc();
55   ~Rpc();
56 
57   // The methods of this class are mirrors (modulo {un,}marshalling of args) of
58   // the corresponding names in trace_processor.h . See that header for docs.
59 
60   util::Status Parse(const uint8_t* data, size_t len);
61   void NotifyEndOfFile();
62   void RestoreInitialTables();
63   std::string GetCurrentTraceName();
64   std::vector<uint8_t> ComputeMetric(const uint8_t* data, size_t len);
65   std::vector<uint8_t> GetMetricDescriptors(const uint8_t* data, size_t len);
66   void EnableMetatrace();
67   std::vector<uint8_t> DisableAndReadMetatrace();
68 
69   // Runs a query and returns results in batch. Each batch is a proto-encoded
70   // TraceProcessor.QueryResult message and contains a variable number of rows.
71   // The callbacks are called inline, so the whole callstack looks as follows:
72   // Query(..., callback)
73   //   callback(..., has_more=true)
74   //   ...
75   //   callback(..., has_more=false)
76   //   (Query() returns at this point).
77   // TODO(primiano): long-term this API should change and be turned into a
78   // bidirectional streaming api (see go/imperative-metrics). The problem with
79   // the current design is that it holds the callstack until the query is done
80   // and makes nested query hard as they cause re-entrancy. It's okay for now
81   // but will change soon.
82   using QueryResultBatchCallback = std::function<
83       void(const uint8_t* /*buf*/, size_t /*len*/, bool /*has_more*/)>;
84   void Query(const uint8_t* args, size_t len, QueryResultBatchCallback);
85 
86   // DEPRECATED, only for legacy clients. Use |Query()| above.
87   std::vector<uint8_t> RawQuery(const uint8_t* args, size_t len);
88 
89  private:
90   void MaybePrintProgress();
91 
92   std::unique_ptr<TraceProcessor> trace_processor_;
93   bool eof_ = true;  // Reset when calling Parse().
94   int64_t t_parse_started_ = 0;
95   size_t bytes_last_progress_ = 0;
96   size_t bytes_parsed_ = 0;
97 };
98 
99 }  // namespace trace_processor
100 }  // namespace perfetto
101 
102 #endif  // SRC_TRACE_PROCESSOR_RPC_RPC_H_
103