• 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_PRODUCER_IPC_SERVICE_H_
18 #define SRC_TRACING_IPC_SERVICE_PRODUCER_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/producer.h"
28 #include "perfetto/ext/tracing/core/tracing_service.h"
29 
30 #include "protos/perfetto/ipc/producer_port.ipc.h"
31 
32 namespace perfetto {
33 
34 namespace ipc {
35 class Host;
36 }  // namespace ipc
37 
38 // Implements the Producer port of the IPC service. This class proxies requests
39 // and responses between the core service logic (|svc_|) and remote Producer(s)
40 // on the IPC socket, through the methods overriddden from ProducerPort.
41 class ProducerIPCService : public protos::gen::ProducerPort {
42  public:
43   explicit ProducerIPCService(TracingService* core_service);
44   ~ProducerIPCService() override;
45 
46   // ProducerPort implementation (from .proto IPC definition).
47   void InitializeConnection(const protos::gen::InitializeConnectionRequest&,
48                             DeferredInitializeConnectionResponse) override;
49   void RegisterDataSource(const protos::gen::RegisterDataSourceRequest&,
50                           DeferredRegisterDataSourceResponse) override;
51   void UnregisterDataSource(const protos::gen::UnregisterDataSourceRequest&,
52                             DeferredUnregisterDataSourceResponse) override;
53   void RegisterTraceWriter(const protos::gen::RegisterTraceWriterRequest&,
54                            DeferredRegisterTraceWriterResponse) override;
55   void UnregisterTraceWriter(const protos::gen::UnregisterTraceWriterRequest&,
56                              DeferredUnregisterTraceWriterResponse) override;
57   void CommitData(const protos::gen::CommitDataRequest&,
58                   DeferredCommitDataResponse) override;
59   void NotifyDataSourceStarted(
60       const protos::gen::NotifyDataSourceStartedRequest&,
61       DeferredNotifyDataSourceStartedResponse) override;
62   void NotifyDataSourceStopped(
63       const protos::gen::NotifyDataSourceStoppedRequest&,
64       DeferredNotifyDataSourceStoppedResponse) override;
65   void ActivateTriggers(const protos::gen::ActivateTriggersRequest&,
66                         DeferredActivateTriggersResponse) override;
67 
68   void GetAsyncCommand(const protos::gen::GetAsyncCommandRequest&,
69                        DeferredGetAsyncCommandResponse) override;
70   void Sync(const protos::gen::SyncRequest&, DeferredSyncResponse) override;
71   void OnClientDisconnected() override;
72 
73  private:
74   // Acts like a Producer with the core Service business logic (which doesn't
75   // know anything about the remote transport), but all it does is proxying
76   // methods to the remote Producer on the other side of the IPC channel.
77   class RemoteProducer : public Producer {
78    public:
79     RemoteProducer();
80     ~RemoteProducer() override;
81 
82     // These methods are called by the |core_service_| business logic. There is
83     // no connection here, these methods are posted straight away.
84     void OnConnect() override;
85     void OnDisconnect() override;
86     void SetupDataSource(DataSourceInstanceID,
87                          const DataSourceConfig&) override;
88     void StartDataSource(DataSourceInstanceID,
89                          const DataSourceConfig&) override;
90     void StopDataSource(DataSourceInstanceID) override;
91     void OnTracingSetup() override;
92     void Flush(FlushRequestID,
93                const DataSourceInstanceID* data_source_ids,
94                size_t num_data_sources) override;
95 
96     void ClearIncrementalState(const DataSourceInstanceID* data_source_ids,
97                                size_t num_data_sources) override;
98 
99     void SendSetupTracing();
100 
101     // The interface obtained from the core service business logic through
102     // Service::ConnectProducer(this). This allows to invoke methods for a
103     // specific Producer on the Service business logic.
104     std::unique_ptr<TracingService::ProducerEndpoint> service_endpoint;
105 
106     // The back-channel (based on a never ending stream request) that allows us
107     // to send asynchronous commands to the remote Producer (e.g. start/stop a
108     // data source).
109     DeferredGetAsyncCommandResponse async_producer_commands;
110 
111     // Set if the service calls OnTracingSetup() before the
112     // |async_producer_commands| was bound by the service. In this case, we
113     // forward the SetupTracing command when it is bound later.
114     bool send_setup_tracing_on_async_commands_bound = false;
115   };
116 
117   ProducerIPCService(const ProducerIPCService&) = delete;
118   ProducerIPCService& operator=(const ProducerIPCService&) = delete;
119 
120   // Returns the ProducerEndpoint in the core business logic that corresponds to
121   // the current IPC request.
122   RemoteProducer* GetProducerForCurrentRequest();
123 
124   TracingService* const core_service_;
125 
126   // Maps IPC clients to ProducerEndpoint instances registered on the
127   // |core_service_| business logic.
128   std::map<ipc::ClientID, std::unique_ptr<RemoteProducer>> producers_;
129 
130   // List because pointers need to be stable.
131   std::list<DeferredSyncResponse> pending_syncs_;
132 
133   base::WeakPtrFactory<ProducerIPCService> weak_ptr_factory_;  // Keep last.
134 };
135 
136 }  // namespace perfetto
137 
138 #endif  // SRC_TRACING_IPC_SERVICE_PRODUCER_IPC_SERVICE_H_
139