xref: /aosp_15_r20/external/perfetto/src/tracing/ipc/producer/producer_ipc_client_impl.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 // If create_socket_async is set, it will be called to create and connect to a
50 // socket to the service. If unset, the producer will create and connect itself.
51 class ProducerIPCClientImpl : public TracingService::ProducerEndpoint,
52                               public ipc::ServiceProxy::EventListener {
53  public:
54   ProducerIPCClientImpl(ipc::Client::ConnArgs,
55                         Producer*,
56                         const std::string& producer_name,
57                         base::TaskRunner*,
58                         TracingService::ProducerSMBScrapingMode,
59                         size_t shared_memory_size_hint_bytes,
60                         size_t shared_memory_page_size_hint_bytes,
61                         std::unique_ptr<SharedMemory> shm,
62                         std::unique_ptr<SharedMemoryArbiter> shm_arbiter,
63                         CreateSocketAsync create_socket_async);
64   ~ProducerIPCClientImpl() override;
65 
66   // TracingService::ProducerEndpoint implementation.
67   // These methods are invoked by the actual Producer(s) code by clients of the
68   // tracing library, which know nothing about the IPC transport.
69   void Disconnect() override;
70   void RegisterDataSource(const DataSourceDescriptor&) override;
71   void UpdateDataSource(const DataSourceDescriptor&) override;
72   void UnregisterDataSource(const std::string& name) override;
73   void RegisterTraceWriter(uint32_t writer_id, uint32_t target_buffer) override;
74   void UnregisterTraceWriter(uint32_t writer_id) override;
75   void CommitData(const CommitDataRequest&, CommitDataCallback) override;
76   void NotifyDataSourceStarted(DataSourceInstanceID) override;
77   void NotifyDataSourceStopped(DataSourceInstanceID) override;
78   void ActivateTriggers(const std::vector<std::string>&) override;
79   void Sync(std::function<void()> callback) override;
80 
81   std::unique_ptr<TraceWriter> CreateTraceWriter(
82       BufferID target_buffer,
83       BufferExhaustedPolicy) override;
84   SharedMemoryArbiter* MaybeSharedMemoryArbiter() override;
85   bool IsShmemProvidedByProducer() const override;
86   void NotifyFlushComplete(FlushRequestID) override;
87   SharedMemory* shared_memory() const override;
88   size_t shared_buffer_page_size_kb() const override;
89 
90   // ipc::ServiceProxy::EventListener implementation.
91   // These methods are invoked by the IPC layer, which knows nothing about
92   // tracing, producers and consumers.
93   void OnConnect() override;
94   void OnDisconnect() override;
95 
GetClientForTesting()96   ipc::Client* GetClientForTesting() { return ipc_channel_.get(); }
97 
98  private:
99   // Drops the provider connection if a protocol error was detected while
100   // processing an IPC command.
101   void ScheduleDisconnect();
102 
103   // Invoked soon after having established the connection with the service.
104   void OnConnectionInitialized(bool connection_succeeded,
105                                bool using_shmem_provided_by_producer,
106                                bool direct_smb_patching_supported,
107                                bool use_shmem_emulation);
108 
109   // Invoked when the remote Service sends an IPC to tell us to do something
110   // (e.g. start/stop a data source).
111   void OnServiceRequest(const protos::gen::GetAsyncCommandResponse&);
112 
113   // TODO think to destruction order, do we rely on any specific dtor sequence?
114   Producer* const producer_;
115   base::TaskRunner* const task_runner_;
116 
117   // A callback used to receive the shmem region out of band of the socket.
118   std::function<int(void)> receive_shmem_fd_cb_fuchsia_;
119 
120   // The object that owns the client socket and takes care of IPC traffic.
121   std::unique_ptr<ipc::Client> ipc_channel_;
122 
123   // The proxy interface for the producer port of the service. It is bound
124   // to |ipc_channel_| and (de)serializes method invocations over the wire.
125   std::unique_ptr<protos::gen::ProducerPortProxy> producer_port_;
126 
127   std::unique_ptr<SharedMemory> shared_memory_;
128   std::unique_ptr<SharedMemoryArbiter> shared_memory_arbiter_;
129   size_t shared_buffer_page_size_kb_ = 0;
130   std::set<DataSourceInstanceID> data_sources_setup_;
131   bool connected_ = false;
132   std::string const name_;
133   size_t shared_memory_page_size_hint_bytes_ = 0;
134   size_t shared_memory_size_hint_bytes_ = 0;
135   TracingService::ProducerSMBScrapingMode const smb_scraping_mode_;
136   bool is_shmem_provided_by_producer_ = false;
137   bool direct_smb_patching_supported_ = false;
138   bool use_shmem_emulation_ = false;
139   std::vector<std::function<void()>> pending_sync_reqs_;
140   base::WeakPtrFactory<ProducerIPCClientImpl> weak_factory_{this};
141   PERFETTO_THREAD_CHECKER(thread_checker_)
142 };
143 
144 }  // namespace perfetto
145 
146 #endif  // SRC_TRACING_IPC_PRODUCER_PRODUCER_IPC_CLIENT_IMPL_H_
147