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