• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/base/weak_ptr.h"
26 #include "perfetto/ipc/basic_types.h"
27 #include "perfetto/tracing/core/consumer.h"
28 #include "perfetto/tracing/core/tracing_service.h"
29 
30 #include "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::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::EnableTracingRequest&,
48                      DeferredEnableTracingResponse) override;
49   void StartTracing(const protos::StartTracingRequest&,
50                     DeferredStartTracingResponse) override;
51   void ChangeTraceConfig(const protos::ChangeTraceConfigRequest&,
52                          DeferredChangeTraceConfigResponse) override;
53   void DisableTracing(const protos::DisableTracingRequest&,
54                       DeferredDisableTracingResponse) override;
55   void ReadBuffers(const protos::ReadBuffersRequest&,
56                    DeferredReadBuffersResponse) override;
57   void FreeBuffers(const protos::FreeBuffersRequest&,
58                    DeferredFreeBuffersResponse) override;
59   void Flush(const protos::FlushRequest&, DeferredFlushResponse) override;
60   void Detach(const protos::DetachRequest&, DeferredDetachResponse) override;
61   void Attach(const protos::AttachRequest&, DeferredAttachResponse) override;
62   void GetTraceStats(const protos::GetTraceStatsRequest&,
63                      DeferredGetTraceStatsResponse) override;
64   void ObserveEvents(const protos::ObserveEventsRequest&,
65                      DeferredObserveEventsResponse) override;
66   void OnClientDisconnected() override;
67 
68  private:
69   // Acts like a Consumer with the core Service business logic (which doesn't
70   // know anything about the remote transport), but all it does is proxying
71   // methods to the remote Consumer on the other side of the IPC channel.
72   class RemoteConsumer : public Consumer {
73    public:
74     RemoteConsumer();
75     ~RemoteConsumer() override;
76 
77     // These methods are called by the |core_service_| business logic. There is
78     // no connection here, these methods are posted straight away.
79     void OnConnect() override;
80     void OnDisconnect() override;
81     void OnTracingDisabled() override;
82     void OnTraceData(std::vector<TracePacket>, bool has_more) override;
83     void OnDetach(bool) override;
84     void OnAttach(bool, const TraceConfig&) override;
85     void OnTraceStats(bool, const TraceStats&) override;
86     void OnObservableEvents(const ObservableEvents&) override;
87 
88     void CloseObserveEventsResponseStream();
89 
90     // The interface obtained from the core service business logic through
91     // TracingService::ConnectConsumer(this). This allows to invoke methods for
92     // a specific Consumer on the Service business logic.
93     std::unique_ptr<TracingService::ConsumerEndpoint> service_endpoint;
94 
95     // After ReadBuffers() is invoked, this binds the async callback that
96     // allows to stream trace packets back to the client.
97     DeferredReadBuffersResponse read_buffers_response;
98 
99     // After EnableTracing() is invoked, this binds the async callback that
100     // allows to send the OnTracingDisabled notification.
101     DeferredEnableTracingResponse enable_tracing_response;
102 
103     // After Detach() is invoked, this binds the async callback that allows to
104     // send the session id to the consumer.
105     DeferredDetachResponse detach_response;
106 
107     // As above, but for the Attach() case.
108     DeferredAttachResponse attach_response;
109 
110     // As above, but for GetTraceStats().
111     DeferredGetTraceStatsResponse get_trace_stats_response;
112 
113     // After ObserveEvents() is invoked, this binds the async callback that
114     // allows to stream ObservableEvents back to the client.
115     DeferredObserveEventsResponse observe_events_response;
116   };
117 
118   // This has to be a container that doesn't invalidate iterators.
119   using PendingFlushResponses = std::list<DeferredFlushResponse>;
120 
121   ConsumerIPCService(const ConsumerIPCService&) = delete;
122   ConsumerIPCService& operator=(const ConsumerIPCService&) = delete;
123 
124   // Returns the ConsumerEndpoint in the core business logic that corresponds to
125   // the current IPC request.
126   RemoteConsumer* GetConsumerForCurrentRequest();
127 
128   void OnFlushCallback(bool success, PendingFlushResponses::iterator);
129 
130   TracingService* const core_service_;
131 
132   // Maps IPC clients to ConsumerEndpoint instances registered on the
133   // |core_service_| business logic.
134   std::map<ipc::ClientID, std::unique_ptr<RemoteConsumer>> consumers_;
135 
136   PendingFlushResponses pending_flush_responses_;
137 
138   base::WeakPtrFactory<ConsumerIPCService> weak_ptr_factory_;  // Keep last.
139 };
140 
141 }  // namespace perfetto
142 
143 #endif  // SRC_TRACING_IPC_SERVICE_CONSUMER_IPC_SERVICE_H_
144