xref: /aosp_15_r20/external/perfetto/src/tracing/ipc/consumer/consumer_ipc_client_impl.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2017 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_TRACING_IPC_CONSUMER_CONSUMER_IPC_CLIENT_IMPL_H_
18 #define SRC_TRACING_IPC_CONSUMER_CONSUMER_IPC_CLIENT_IMPL_H_
19 
20 #include <stdint.h>
21 
22 #include <list>
23 #include <vector>
24 
25 #include "perfetto/ext/base/scoped_file.h"
26 #include "perfetto/ext/base/weak_ptr.h"
27 #include "perfetto/ext/ipc/service_proxy.h"
28 #include "perfetto/ext/tracing/core/basic_types.h"
29 #include "perfetto/ext/tracing/core/trace_packet.h"
30 #include "perfetto/ext/tracing/core/tracing_service.h"
31 #include "perfetto/ext/tracing/ipc/consumer_ipc_client.h"
32 #include "perfetto/tracing/core/forward_decls.h"
33 
34 #include "protos/perfetto/ipc/consumer_port.ipc.h"
35 
36 namespace perfetto {
37 
38 namespace base {
39 class TaskRunner;
40 }  // namespace base
41 
42 namespace ipc {
43 class Client;
44 }  // namespace ipc
45 
46 class Consumer;
47 
48 // Exposes a Service endpoint to Consumer(s), proxying all requests through a
49 // IPC channel to the remote Service. This class is the glue layer between the
50 // generic Service interface exposed to the clients of the library and the
51 // actual IPC transport.
52 class ConsumerIPCClientImpl : public TracingService::ConsumerEndpoint,
53                               public ipc::ServiceProxy::EventListener {
54  public:
55   ConsumerIPCClientImpl(const char* service_sock_name,
56                         Consumer*,
57                         base::TaskRunner*);
58   ~ConsumerIPCClientImpl() override;
59 
60   // TracingService::ConsumerEndpoint implementation.
61   // These methods are invoked by the actual Consumer(s) code by clients of the
62   // tracing library, which know nothing about the IPC transport.
63   void EnableTracing(const TraceConfig&, base::ScopedFile) override;
64   void StartTracing() override;
65   void ChangeTraceConfig(const TraceConfig&) override;
66   void DisableTracing() override;
67   void ReadBuffers() override;
68   void FreeBuffers() override;
69   void Flush(uint32_t timeout_ms, FlushCallback, FlushFlags) override;
70   void Detach(const std::string& key) override;
71   void Attach(const std::string& key) override;
72   void GetTraceStats() override;
73   void ObserveEvents(uint32_t enabled_event_types) override;
74   void QueryServiceState(QueryServiceStateArgs,
75                          QueryServiceStateCallback) override;
76   void QueryCapabilities(QueryCapabilitiesCallback) override;
77   void SaveTraceForBugreport(SaveTraceForBugreportCallback) override;
78   void CloneSession(CloneSessionArgs) override;
79 
80   // ipc::ServiceProxy::EventListener implementation.
81   // These methods are invoked by the IPC layer, which knows nothing about
82   // tracing, consumers and consumers.
83   void OnConnect() override;
84   void OnDisconnect() override;
85 
86  private:
87   struct PendingQueryServiceRequest {
88     QueryServiceStateCallback callback;
89 
90     // All the replies will be appended here until |has_more| == false.
91     std::vector<uint8_t> merged_resp;
92   };
93 
94   // List because we need stable iterators.
95   using PendingQueryServiceRequests = std::list<PendingQueryServiceRequest>;
96 
97   void OnReadBuffersResponse(
98       ipc::AsyncResult<protos::gen::ReadBuffersResponse>);
99   void OnEnableTracingResponse(
100       ipc::AsyncResult<protos::gen::EnableTracingResponse>);
101   void OnQueryServiceStateResponse(
102       ipc::AsyncResult<protos::gen::QueryServiceStateResponse>,
103       PendingQueryServiceRequests::iterator);
104 
105   // TODO(primiano): think to dtor order, do we rely on any specific sequence?
106   Consumer* const consumer_;
107 
108   // The object that owns the client socket and takes care of IPC traffic.
109   std::unique_ptr<ipc::Client> ipc_channel_;
110 
111   // The proxy interface for the consumer port of the service. It is bound
112   // to |ipc_channel_| and (de)serializes method invocations over the wire.
113   protos::gen::ConsumerPortProxy consumer_port_;
114 
115   bool connected_ = false;
116 
117   PendingQueryServiceRequests pending_query_svc_reqs_;
118 
119   // When a packet is too big to fit into a ReadBuffersResponse IPC, the service
120   // will chunk it into several IPCs, each containing few slices of the packet
121   // (a packet's slice is always guaranteed to be << kIPCBufferSize). When
122   // chunking happens this field accumulates the slices received until the
123   // one with |last_slice_for_packet| == true is received.
124   TracePacket partial_packet_;
125 
126   // Keep last.
127   base::WeakPtrFactory<ConsumerIPCClientImpl> weak_ptr_factory_;
128 };
129 
130 }  // namespace perfetto
131 
132 #endif  // SRC_TRACING_IPC_CONSUMER_CONSUMER_IPC_CLIENT_IMPL_H_
133