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/ext/base/weak_ptr.h" 25 #include "perfetto/ext/ipc/basic_types.h" 26 #include "perfetto/ext/tracing/core/producer.h" 27 #include "perfetto/ext/tracing/core/tracing_service.h" 28 29 #include "protos/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::gen::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::gen::InitializeConnectionRequest&, 47 DeferredInitializeConnectionResponse) override; 48 void RegisterDataSource(const protos::gen::RegisterDataSourceRequest&, 49 DeferredRegisterDataSourceResponse) override; 50 void UnregisterDataSource(const protos::gen::UnregisterDataSourceRequest&, 51 DeferredUnregisterDataSourceResponse) override; 52 void RegisterTraceWriter(const protos::gen::RegisterTraceWriterRequest&, 53 DeferredRegisterTraceWriterResponse) override; 54 void UnregisterTraceWriter(const protos::gen::UnregisterTraceWriterRequest&, 55 DeferredUnregisterTraceWriterResponse) override; 56 void CommitData(const protos::gen::CommitDataRequest&, 57 DeferredCommitDataResponse) override; 58 void NotifyDataSourceStarted( 59 const protos::gen::NotifyDataSourceStartedRequest&, 60 DeferredNotifyDataSourceStartedResponse) override; 61 void NotifyDataSourceStopped( 62 const protos::gen::NotifyDataSourceStoppedRequest&, 63 DeferredNotifyDataSourceStoppedResponse) override; 64 65 void ActivateTriggers(const protos::gen::ActivateTriggersRequest&, 66 DeferredActivateTriggersResponse) override; 67 68 void GetAsyncCommand(const protos::gen::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 void SendSetupTracing(); 99 100 // The interface obtained from the core service business logic through 101 // Service::ConnectProducer(this). This allows to invoke methods for a 102 // specific Producer on the Service business logic. 103 std::unique_ptr<TracingService::ProducerEndpoint> service_endpoint; 104 105 // The back-channel (based on a never ending stream request) that allows us 106 // to send asynchronous commands to the remote Producer (e.g. start/stop a 107 // data source). 108 DeferredGetAsyncCommandResponse async_producer_commands; 109 110 // Set if the service calls OnTracingSetup() before the 111 // |async_producer_commands| was bound by the service. In this case, we 112 // forward the SetupTracing command when it is bound later. 113 bool send_setup_tracing_on_async_commands_bound = false; 114 }; 115 116 ProducerIPCService(const ProducerIPCService&) = delete; 117 ProducerIPCService& operator=(const ProducerIPCService&) = delete; 118 119 // Returns the ProducerEndpoint in the core business logic that corresponds to 120 // the current IPC request. 121 RemoteProducer* GetProducerForCurrentRequest(); 122 123 TracingService* const core_service_; 124 125 // Maps IPC clients to ProducerEndpoint instances registered on the 126 // |core_service_| business logic. 127 std::map<ipc::ClientID, std::unique_ptr<RemoteProducer>> producers_; 128 129 base::WeakPtrFactory<ProducerIPCService> weak_ptr_factory_; // Keep last. 130 }; 131 132 } // namespace perfetto 133 134 #endif // SRC_TRACING_IPC_SERVICE_PRODUCER_IPC_SERVICE_H_ 135