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