xref: /aosp_15_r20/external/perfetto/src/tracing/ipc/service/consumer_ipc_service.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_SERVICE_CONSUMER_IPC_SERVICE_H_
18 #define SRC_TRACING_IPC_SERVICE_CONSUMER_IPC_SERVICE_H_
19 
20 #include <list>
21 #include <map>
22 #include <memory>
23 #include <string>
24 
25 #include "perfetto/ext/base/weak_ptr.h"
26 #include "perfetto/ext/ipc/basic_types.h"
27 #include "perfetto/ext/tracing/core/consumer.h"
28 #include "perfetto/ext/tracing/core/tracing_service.h"
29 #include "perfetto/tracing/core/forward_decls.h"
30 #include "protos/perfetto/ipc/consumer_port.ipc.h"
31 
32 namespace perfetto {
33 
34 namespace ipc {
35 class Host;
36 }  // namespace ipc
37 
38 // Implements the Consumer port of the IPC service. This class proxies requests
39 // and responses between the core service logic (|svc_|) and remote Consumer(s)
40 // on the IPC socket, through the methods overriddden from ConsumerPort.
41 class ConsumerIPCService : public protos::gen::ConsumerPort {
42  public:
43   explicit ConsumerIPCService(TracingService* core_service);
44   ~ConsumerIPCService() override;
45 
46   // ConsumerPort implementation (from .proto IPC definition).
47   void EnableTracing(const protos::gen::EnableTracingRequest&,
48                      DeferredEnableTracingResponse) override;
49   void StartTracing(const protos::gen::StartTracingRequest&,
50                     DeferredStartTracingResponse) override;
51   void ChangeTraceConfig(const protos::gen::ChangeTraceConfigRequest&,
52                          DeferredChangeTraceConfigResponse) override;
53   void DisableTracing(const protos::gen::DisableTracingRequest&,
54                       DeferredDisableTracingResponse) override;
55   void ReadBuffers(const protos::gen::ReadBuffersRequest&,
56                    DeferredReadBuffersResponse) override;
57   void FreeBuffers(const protos::gen::FreeBuffersRequest&,
58                    DeferredFreeBuffersResponse) override;
59   void Flush(const protos::gen::FlushRequest&, DeferredFlushResponse) override;
60   void Detach(const protos::gen::DetachRequest&,
61               DeferredDetachResponse) override;
62   void Attach(const protos::gen::AttachRequest&,
63               DeferredAttachResponse) override;
64   void GetTraceStats(const protos::gen::GetTraceStatsRequest&,
65                      DeferredGetTraceStatsResponse) override;
66   void ObserveEvents(const protos::gen::ObserveEventsRequest&,
67                      DeferredObserveEventsResponse) override;
68   void QueryServiceState(const protos::gen::QueryServiceStateRequest&,
69                          DeferredQueryServiceStateResponse) override;
70   void QueryCapabilities(const protos::gen::QueryCapabilitiesRequest&,
71                          DeferredQueryCapabilitiesResponse) override;
72   void SaveTraceForBugreport(const protos::gen::SaveTraceForBugreportRequest&,
73                              DeferredSaveTraceForBugreportResponse) override;
74   void CloneSession(const protos::gen::CloneSessionRequest&,
75                     DeferredCloneSessionResponse) override;
76   void OnClientDisconnected() override;
77 
78  private:
79   // Acts like a Consumer with the core Service business logic (which doesn't
80   // know anything about the remote transport), but all it does is proxying
81   // methods to the remote Consumer on the other side of the IPC channel.
82   class RemoteConsumer : public Consumer {
83    public:
84     RemoteConsumer();
85     ~RemoteConsumer() override;
86 
87     // These methods are called by the |core_service_| business logic. There is
88     // no connection here, these methods are posted straight away.
89     void OnConnect() override;
90     void OnDisconnect() override;
91     void OnTracingDisabled(const std::string& error) override;
92     void OnTraceData(std::vector<TracePacket>, bool has_more) override;
93     void OnDetach(bool) override;
94     void OnAttach(bool, const TraceConfig&) override;
95     void OnTraceStats(bool, const TraceStats&) override;
96     void OnObservableEvents(const ObservableEvents&) override;
97     void OnSessionCloned(const OnSessionClonedArgs&) override;
98 
99     void CloseObserveEventsResponseStream();
100 
101     // The interface obtained from the core service business logic through
102     // TracingService::ConnectConsumer(this). This allows to invoke methods for
103     // a specific Consumer on the Service business logic.
104     std::unique_ptr<TracingService::ConsumerEndpoint> service_endpoint;
105 
106     // After ReadBuffers() is invoked, this binds the async callback that
107     // allows to stream trace packets back to the client.
108     DeferredReadBuffersResponse read_buffers_response;
109 
110     // After EnableTracing() is invoked, this binds the async callback that
111     // allows to send the OnTracingDisabled notification.
112     DeferredEnableTracingResponse enable_tracing_response;
113 
114     // After Detach() is invoked, this binds the async callback that allows to
115     // send the session id to the consumer.
116     DeferredDetachResponse detach_response;
117 
118     // As above, but for the Attach() case.
119     DeferredAttachResponse attach_response;
120 
121     // As above, but for GetTraceStats().
122     DeferredGetTraceStatsResponse get_trace_stats_response;
123 
124     // As above, but for CloneSession().
125     DeferredCloneSessionResponse clone_session_response;
126 
127     // After ObserveEvents() is invoked, this binds the async callback that
128     // allows to stream ObservableEvents back to the client.
129     DeferredObserveEventsResponse observe_events_response;
130   };
131 
132   // This has to be a container that doesn't invalidate iterators.
133   using PendingFlushResponses = std::list<DeferredFlushResponse>;
134   using PendingQuerySvcResponses = std::list<DeferredQueryServiceStateResponse>;
135   using PendingQueryCapabilitiesResponses =
136       std::list<DeferredQueryCapabilitiesResponse>;
137   using PendingSaveTraceForBugreportResponses =
138       std::list<DeferredSaveTraceForBugreportResponse>;
139 
140   ConsumerIPCService(const ConsumerIPCService&) = delete;
141   ConsumerIPCService& operator=(const ConsumerIPCService&) = delete;
142 
143   // Returns the ConsumerEndpoint in the core business logic that corresponds to
144   // the current IPC request.
145   RemoteConsumer* GetConsumerForCurrentRequest();
146 
147   void OnFlushCallback(bool success, PendingFlushResponses::iterator);
148   void OnQueryServiceCallback(bool success,
149                               const TracingServiceState&,
150                               PendingQuerySvcResponses::iterator);
151   void OnQueryCapabilitiesCallback(const TracingServiceCapabilities&,
152                                    PendingQueryCapabilitiesResponses::iterator);
153   void OnSaveTraceForBugreportCallback(
154       bool success,
155       const std::string& msg,
156       PendingSaveTraceForBugreportResponses::iterator);
157 
158   TracingService* const core_service_;
159 
160   // Maps IPC clients to ConsumerEndpoint instances registered on the
161   // |core_service_| business logic.
162   std::map<ipc::ClientID, std::unique_ptr<RemoteConsumer>> consumers_;
163 
164   PendingFlushResponses pending_flush_responses_;
165   PendingQuerySvcResponses pending_query_service_responses_;
166   PendingQueryCapabilitiesResponses pending_query_capabilities_responses_;
167   PendingSaveTraceForBugreportResponses pending_bugreport_responses_;
168 
169   base::WeakPtrFactory<ConsumerIPCService> weak_ptr_factory_;  // Keep last.
170 };
171 
172 }  // namespace perfetto
173 
174 #endif  // SRC_TRACING_IPC_SERVICE_CONSUMER_IPC_SERVICE_H_
175