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