• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <vector>
23 
24 #include "perfetto/base/thread_checker.h"
25 #include "perfetto/ipc/service_proxy.h"
26 #include "perfetto/tracing/core/basic_types.h"
27 #include "perfetto/tracing/core/service.h"
28 #include "perfetto/tracing/core/shared_memory.h"
29 #include "perfetto/tracing/ipc/producer_ipc_client.h"
30 
31 #include "perfetto/ipc/producer_port.ipc.h"
32 
33 namespace perfetto {
34 
35 namespace base {
36 class TaskRunner;
37 }  // namespace base
38 
39 namespace ipc {
40 class Client;
41 }  // namespace ipc
42 
43 class Producer;
44 class PosixSharedMemory;
45 class SharedMemoryArbiter;
46 
47 // Exposes a Service endpoint to Producer(s), proxying all requests through a
48 // IPC channel to the remote Service. This class is the glue layer between the
49 // generic Service interface exposed to the clients of the library and the
50 // actual IPC transport.
51 class ProducerIPCClientImpl : public Service::ProducerEndpoint,
52                               public ipc::ServiceProxy::EventListener {
53  public:
54   ProducerIPCClientImpl(const char* service_sock_name,
55                         Producer*,
56                         const std::string& producer_name,
57                         base::TaskRunner*);
58   ~ProducerIPCClientImpl() override;
59 
60   // Service::ProducerEndpoint implementation.
61   // These methods are invoked by the actual Producer(s) code by clients of the
62   // tracing library, which know nothing about the IPC transport.
63   void RegisterDataSource(const DataSourceDescriptor&) override;
64   void UnregisterDataSource(const std::string& name) override;
65   void CommitData(const CommitDataRequest&, CommitDataCallback) override;
66   std::unique_ptr<TraceWriter> CreateTraceWriter(
67       BufferID target_buffer) override;
68   void NotifyFlushComplete(FlushRequestID) override;
69   SharedMemory* shared_memory() const override;
70   size_t shared_buffer_page_size_kb() const override;
71 
72   // ipc::ServiceProxy::EventListener implementation.
73   // These methods are invoked by the IPC layer, which knows nothing about
74   // tracing, producers and consumers.
75   void OnConnect() override;
76   void OnDisconnect() override;
77 
78  private:
79   // Invoked soon after having established the connection with the service.
80   void OnConnectionInitialized(bool connection_succeeded);
81 
82   // Invoked when the remote Service sends an IPC to tell us to do something
83   // (e.g. start/stop a data source).
84   void OnServiceRequest(const protos::GetAsyncCommandResponse&);
85 
86   // TODO think to destruction order, do we rely on any specific dtor sequence?
87   Producer* const producer_;
88   base::TaskRunner* const task_runner_;
89 
90   // The object that owns the client socket and takes care of IPC traffic.
91   std::unique_ptr<ipc::Client> ipc_channel_;
92 
93   // The proxy interface for the producer port of the service. It is bound
94   // to |ipc_channel_| and (de)serializes method invocations over the wire.
95   protos::ProducerPortProxy producer_port_;
96 
97   std::unique_ptr<PosixSharedMemory> shared_memory_;
98   std::unique_ptr<SharedMemoryArbiter> shared_memory_arbiter_;
99   size_t shared_buffer_page_size_kb_ = 0;
100   bool connected_ = false;
101   std::string const name_;
102   PERFETTO_THREAD_CHECKER(thread_checker_)
103 };
104 
105 }  // namespace perfetto
106 
107 #endif  // SRC_TRACING_IPC_PRODUCER_PRODUCER_IPC_CLIENT_IMPL_H_
108