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_CORE_SERVICE_IMPL_H_ |
| 18 | #define SRC_TRACING_CORE_SERVICE_IMPL_H_ |
| 19 | |
| 20 | #include <functional> |
| 21 | #include <map> |
| 22 | #include <memory> |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 23 | #include <set> |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 24 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 25 | #include "perfetto/base/weak_ptr.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 26 | #include "perfetto/tracing/core/basic_types.h" |
| 27 | #include "perfetto/tracing/core/service.h" |
| 28 | |
| 29 | namespace perfetto { |
| 30 | |
| 31 | namespace base { |
| 32 | class TaskRunner; |
| 33 | } // namespace base |
| 34 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 35 | class Consumer; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 36 | class DataSourceConfig; |
| 37 | class Producer; |
| 38 | class SharedMemory; |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 39 | class TraceConfig; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 40 | |
| 41 | // The tracing service business logic. |
| 42 | class ServiceImpl : public Service { |
| 43 | public: |
| 44 | // The implementation behind the service endpoint exposed to each producer. |
| 45 | class ProducerEndpointImpl : public Service::ProducerEndpoint { |
| 46 | public: |
| 47 | ProducerEndpointImpl(ProducerID, |
| 48 | ServiceImpl*, |
| 49 | base::TaskRunner*, |
| 50 | Producer*, |
| 51 | std::unique_ptr<SharedMemory>); |
| 52 | ~ProducerEndpointImpl() override; |
| 53 | |
| 54 | Producer* producer() const { return producer_; } |
| 55 | |
| 56 | // Service::ProducerEndpoint implementation. |
| 57 | void RegisterDataSource(const DataSourceDescriptor&, |
| 58 | RegisterDataSourceCallback) override; |
| 59 | void UnregisterDataSource(DataSourceID) override; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 60 | void NotifySharedMemoryUpdate( |
| 61 | const std::vector<uint32_t>& changed_pages) override; |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame^] | 62 | std::unique_ptr<TraceWriter> CreateTraceWriter(BufferID) override; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 63 | SharedMemory* shared_memory() const override; |
| 64 | |
| 65 | private: |
| 66 | ProducerEndpointImpl(const ProducerEndpointImpl&) = delete; |
| 67 | ProducerEndpointImpl& operator=(const ProducerEndpointImpl&) = delete; |
| 68 | |
| 69 | ProducerID const id_; |
| 70 | ServiceImpl* const service_; |
| 71 | base::TaskRunner* const task_runner_; |
| 72 | Producer* producer_; |
| 73 | std::unique_ptr<SharedMemory> shared_memory_; |
| 74 | DataSourceID last_data_source_id_ = 0; |
| 75 | }; |
| 76 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 77 | // The implementation behind the service endpoint exposed to each consumer. |
| 78 | class ConsumerEndpointImpl : public Service::ConsumerEndpoint { |
| 79 | public: |
| 80 | ConsumerEndpointImpl(ServiceImpl*, base::TaskRunner*, Consumer*); |
| 81 | ~ConsumerEndpointImpl() override; |
| 82 | |
| 83 | Consumer* consumer() const { return consumer_; } |
| 84 | base::WeakPtr<ConsumerEndpointImpl> GetWeakPtr(); |
| 85 | |
| 86 | // Service::ConsumerEndpoint implementation. |
| 87 | void EnableTracing(const TraceConfig&) override; |
| 88 | void DisableTracing() override; |
| 89 | void ReadBuffers() override; |
| 90 | void FreeBuffers() override; |
| 91 | |
| 92 | private: |
| 93 | ConsumerEndpointImpl(const ConsumerEndpointImpl&) = delete; |
| 94 | ConsumerEndpointImpl& operator=(const ConsumerEndpointImpl&) = delete; |
| 95 | |
| 96 | ServiceImpl* const service_; |
| 97 | Consumer* const consumer_; |
| 98 | base::WeakPtrFactory<ConsumerEndpointImpl> weak_ptr_factory_; |
| 99 | }; |
| 100 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 101 | explicit ServiceImpl(std::unique_ptr<SharedMemory::Factory>, |
| 102 | base::TaskRunner*); |
| 103 | ~ServiceImpl() override; |
| 104 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 105 | // Called by ProducerEndpointImpl. |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 106 | void DisconnectProducer(ProducerID); |
| 107 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 108 | // Called by ConsumerEndpointImpl. |
| 109 | void DisconnectConsumer(ConsumerEndpointImpl*); |
| 110 | void EnableTracing(ConsumerEndpointImpl*, const TraceConfig&); |
| 111 | void DisableTracing(ConsumerEndpointImpl*); |
| 112 | void ReadBuffers(ConsumerEndpointImpl*); |
| 113 | void FreeBuffers(ConsumerEndpointImpl*); |
| 114 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 115 | // Service implementation. |
| 116 | std::unique_ptr<Service::ProducerEndpoint> ConnectProducer( |
| 117 | Producer*, |
| 118 | size_t shared_buffer_size_hint_bytes = 0) override; |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 119 | |
| 120 | std::unique_ptr<Service::ConsumerEndpoint> ConnectConsumer( |
| 121 | Consumer*) override; |
| 122 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 123 | // Exposed mainly for testing. |
| 124 | size_t num_producers() const { return producers_.size(); } |
| 125 | ProducerEndpointImpl* GetProducer(ProducerID) const; |
| 126 | |
| 127 | private: |
| 128 | ServiceImpl(const ServiceImpl&) = delete; |
| 129 | ServiceImpl& operator=(const ServiceImpl&) = delete; |
| 130 | |
| 131 | std::unique_ptr<SharedMemory::Factory> shm_factory_; |
| 132 | base::TaskRunner* const task_runner_; |
| 133 | ProducerID last_producer_id_ = 0; |
| 134 | std::map<ProducerID, ProducerEndpointImpl*> producers_; |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 135 | std::set<ConsumerEndpointImpl*> consumers_; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | } // namespace perfetto |
| 139 | |
| 140 | #endif // SRC_TRACING_CORE_SERVICE_IMPL_H_ |