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_PRODUCER_PRODUCER_IPC_CLIENT_IMPL_H_ 18 #define SRC_TRACING_IPC_PRODUCER_PRODUCER_IPC_CLIENT_IMPL_H_ 19 20 #include <stdint.h> 21 22 #include <set> 23 #include <vector> 24 25 #include "perfetto/ext/base/thread_checker.h" 26 #include "perfetto/ext/base/weak_ptr.h" 27 #include "perfetto/ext/ipc/client.h" 28 #include "perfetto/ext/ipc/service_proxy.h" 29 #include "perfetto/ext/tracing/core/basic_types.h" 30 #include "perfetto/ext/tracing/core/shared_memory.h" 31 #include "perfetto/ext/tracing/core/tracing_service.h" 32 #include "perfetto/ext/tracing/ipc/producer_ipc_client.h" 33 34 #include "protos/perfetto/ipc/producer_port.ipc.h" 35 36 namespace perfetto { 37 38 namespace base { 39 class TaskRunner; 40 } // namespace base 41 42 class Producer; 43 class SharedMemoryArbiter; 44 45 // Exposes a Service endpoint to Producer(s), proxying all requests through a 46 // IPC channel to the remote Service. This class is the glue layer between the 47 // generic Service interface exposed to the clients of the library and the 48 // actual IPC transport. 49 class ProducerIPCClientImpl : public TracingService::ProducerEndpoint, 50 public ipc::ServiceProxy::EventListener { 51 public: 52 ProducerIPCClientImpl(ipc::Client::ConnArgs, 53 Producer*, 54 const std::string& producer_name, 55 base::TaskRunner*, 56 TracingService::ProducerSMBScrapingMode, 57 size_t shared_memory_size_hint_bytes, 58 size_t shared_memory_page_size_hint_bytes, 59 std::unique_ptr<SharedMemory> shm, 60 std::unique_ptr<SharedMemoryArbiter> shm_arbiter); 61 ~ProducerIPCClientImpl() override; 62 63 // TracingService::ProducerEndpoint implementation. 64 // These methods are invoked by the actual Producer(s) code by clients of the 65 // tracing library, which know nothing about the IPC transport. 66 void Disconnect() override; 67 void RegisterDataSource(const DataSourceDescriptor&) override; 68 void UpdateDataSource(const DataSourceDescriptor&) override; 69 void UnregisterDataSource(const std::string& name) override; 70 void RegisterTraceWriter(uint32_t writer_id, uint32_t target_buffer) override; 71 void UnregisterTraceWriter(uint32_t writer_id) override; 72 void CommitData(const CommitDataRequest&, CommitDataCallback) override; 73 void NotifyDataSourceStarted(DataSourceInstanceID) override; 74 void NotifyDataSourceStopped(DataSourceInstanceID) override; 75 void ActivateTriggers(const std::vector<std::string>&) override; 76 void Sync(std::function<void()> callback) override; 77 78 std::unique_ptr<TraceWriter> CreateTraceWriter( 79 BufferID target_buffer, 80 BufferExhaustedPolicy) override; 81 SharedMemoryArbiter* MaybeSharedMemoryArbiter() override; 82 bool IsShmemProvidedByProducer() const override; 83 void NotifyFlushComplete(FlushRequestID) override; 84 SharedMemory* shared_memory() const override; 85 size_t shared_buffer_page_size_kb() const override; 86 87 // ipc::ServiceProxy::EventListener implementation. 88 // These methods are invoked by the IPC layer, which knows nothing about 89 // tracing, producers and consumers. 90 void OnConnect() override; 91 void OnDisconnect() override; 92 GetClientForTesting()93 ipc::Client* GetClientForTesting() { return ipc_channel_.get(); } 94 95 private: 96 // Drops the provider connection if a protocol error was detected while 97 // processing an IPC command. 98 void ScheduleDisconnect(); 99 100 // Invoked soon after having established the connection with the service. 101 void OnConnectionInitialized(bool connection_succeeded, 102 bool using_shmem_provided_by_producer, 103 bool direct_smb_patching_supported); 104 105 // Invoked when the remote Service sends an IPC to tell us to do something 106 // (e.g. start/stop a data source). 107 void OnServiceRequest(const protos::gen::GetAsyncCommandResponse&); 108 109 // TODO think to destruction order, do we rely on any specific dtor sequence? 110 Producer* const producer_; 111 base::TaskRunner* const task_runner_; 112 113 // A callback used to receive the shmem region out of band of the socket. 114 std::function<int(void)> receive_shmem_fd_cb_fuchsia_; 115 116 // The object that owns the client socket and takes care of IPC traffic. 117 std::unique_ptr<ipc::Client> ipc_channel_; 118 119 // The proxy interface for the producer port of the service. It is bound 120 // to |ipc_channel_| and (de)serializes method invocations over the wire. 121 std::unique_ptr<protos::gen::ProducerPortProxy> producer_port_; 122 123 std::unique_ptr<SharedMemory> shared_memory_; 124 std::unique_ptr<SharedMemoryArbiter> shared_memory_arbiter_; 125 size_t shared_buffer_page_size_kb_ = 0; 126 std::set<DataSourceInstanceID> data_sources_setup_; 127 bool connected_ = false; 128 std::string const name_; 129 size_t shared_memory_page_size_hint_bytes_ = 0; 130 size_t shared_memory_size_hint_bytes_ = 0; 131 TracingService::ProducerSMBScrapingMode const smb_scraping_mode_; 132 bool is_shmem_provided_by_producer_ = false; 133 bool direct_smb_patching_supported_ = false; 134 std::vector<std::function<void()>> pending_sync_reqs_; 135 base::WeakPtrFactory<ProducerIPCClientImpl> weak_factory_{this}; 136 PERFETTO_THREAD_CHECKER(thread_checker_) 137 }; 138 139 } // namespace perfetto 140 141 #endif // SRC_TRACING_IPC_PRODUCER_PRODUCER_IPC_CLIENT_IMPL_H_ 142