Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 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 | |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 24 | #include "perfetto/base/thread_checker.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 25 | #include "perfetto/ipc/service_proxy.h" |
| 26 | #include "perfetto/tracing/core/basic_types.h" |
| 27 | #include "perfetto/tracing/core/service.h" |
Oystein Eftevaag | 6d0bc7f | 2018-01-13 12:21:55 -0800 | [diff] [blame] | 28 | #include "perfetto/tracing/core/shared_memory.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 29 | #include "perfetto/tracing/ipc/producer_ipc_client.h" |
Primiano Tucci | 114b649 | 2017-12-11 23:09:45 +0000 | [diff] [blame] | 30 | |
Primiano Tucci | 20b760c | 2018-01-19 12:36:12 +0000 | [diff] [blame] | 31 | #include "perfetto/ipc/producer_port.ipc.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 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; |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 45 | class SharedMemoryArbiter; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 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 | base::TaskRunner*); |
| 57 | ~ProducerIPCClientImpl() override; |
| 58 | |
| 59 | // Service::ProducerEndpoint implementation. |
| 60 | // These methods are invoked by the actual Producer(s) code by clients of the |
| 61 | // tracing library, which know nothing about the IPC transport. |
| 62 | void RegisterDataSource(const DataSourceDescriptor&, |
| 63 | RegisterDataSourceCallback) override; |
| 64 | void UnregisterDataSource(DataSourceID) override; |
Primiano Tucci | 79f3f91 | 2018-03-02 12:00:31 +0000 | [diff] [blame^] | 65 | void CommitData(const CommitDataRequest&) override; |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 66 | std::unique_ptr<TraceWriter> CreateTraceWriter( |
| 67 | BufferID target_buffer) override; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 68 | SharedMemory* shared_memory() const override; |
| 69 | |
| 70 | // ipc::ServiceProxy::EventListener implementation. |
| 71 | // These methods are invoked by the IPC layer, which knows nothing about |
| 72 | // tracing, producers and consumers. |
| 73 | void OnConnect() override; |
| 74 | void OnDisconnect() override; |
| 75 | |
| 76 | private: |
| 77 | // Invoked soon after having established the connection with the service. |
| 78 | void OnConnectionInitialized(bool connection_succeeded); |
| 79 | |
| 80 | // Invoked when the remote Service sends an IPC to tell us to do something |
| 81 | // (e.g. start/stop a data source). |
Primiano Tucci | 954d8ed | 2018-02-28 23:08:53 +0000 | [diff] [blame] | 82 | void OnServiceRequest(const protos::GetAsyncCommandResponse&); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 83 | |
| 84 | // TODO think to destruction order, do we rely on any specific dtor sequence? |
| 85 | Producer* const producer_; |
| 86 | base::TaskRunner* const task_runner_; |
| 87 | |
| 88 | // The object that owns the client socket and takes care of IPC traffic. |
| 89 | std::unique_ptr<ipc::Client> ipc_channel_; |
| 90 | |
| 91 | // The proxy interface for the producer port of the service. It is bound |
| 92 | // to |ipc_channel_| and (de)serializes method invocations over the wire. |
Primiano Tucci | 954d8ed | 2018-02-28 23:08:53 +0000 | [diff] [blame] | 93 | protos::ProducerPortProxy producer_port_; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 94 | |
| 95 | std::unique_ptr<PosixSharedMemory> shared_memory_; |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 96 | std::unique_ptr<SharedMemoryArbiter> shared_memory_arbiter_; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 97 | bool connected_ = false; |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 98 | PERFETTO_THREAD_CHECKER(thread_checker_) |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | } // namespace perfetto |
| 102 | |
| 103 | #endif // SRC_TRACING_IPC_PRODUCER_PRODUCER_IPC_CLIENT_IMPL_H_ |