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 | bbaa58c | 2017-12-20 13:48:20 +0100 | [diff] [blame] | 25 | #include "perfetto/base/page_allocator.h" |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 26 | #include "perfetto/base/weak_ptr.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 27 | #include "perfetto/tracing/core/basic_types.h" |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 28 | #include "perfetto/tracing/core/data_source_descriptor.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 29 | #include "perfetto/tracing/core/service.h" |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 30 | #include "perfetto/tracing/core/shared_memory_abi.h" |
Oystein Eftevaag | 1269b4a | 2018-01-10 16:29:38 -0800 | [diff] [blame^] | 31 | #include "perfetto/tracing/core/trace_config.h" |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 32 | #include "src/tracing/core/id_allocator.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 33 | |
| 34 | namespace perfetto { |
| 35 | |
| 36 | namespace base { |
| 37 | class TaskRunner; |
| 38 | } // namespace base |
| 39 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 40 | class Consumer; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 41 | class DataSourceConfig; |
| 42 | class Producer; |
| 43 | class SharedMemory; |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 44 | class TraceConfig; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 45 | |
| 46 | // The tracing service business logic. |
| 47 | class ServiceImpl : public Service { |
| 48 | public: |
| 49 | // The implementation behind the service endpoint exposed to each producer. |
| 50 | class ProducerEndpointImpl : public Service::ProducerEndpoint { |
| 51 | public: |
| 52 | ProducerEndpointImpl(ProducerID, |
| 53 | ServiceImpl*, |
| 54 | base::TaskRunner*, |
| 55 | Producer*, |
| 56 | std::unique_ptr<SharedMemory>); |
| 57 | ~ProducerEndpointImpl() override; |
| 58 | |
| 59 | Producer* producer() const { return producer_; } |
Oystein Eftevaag | 1269b4a | 2018-01-10 16:29:38 -0800 | [diff] [blame^] | 60 | ProducerID id() const { return id_; } |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 61 | |
| 62 | // Service::ProducerEndpoint implementation. |
| 63 | void RegisterDataSource(const DataSourceDescriptor&, |
| 64 | RegisterDataSourceCallback) override; |
| 65 | void UnregisterDataSource(DataSourceID) override; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 66 | void NotifySharedMemoryUpdate( |
| 67 | const std::vector<uint32_t>& changed_pages) override; |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 68 | std::unique_ptr<TraceWriter> CreateTraceWriter(BufferID) override; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 69 | SharedMemory* shared_memory() const override; |
| 70 | |
| 71 | private: |
| 72 | ProducerEndpointImpl(const ProducerEndpointImpl&) = delete; |
| 73 | ProducerEndpointImpl& operator=(const ProducerEndpointImpl&) = delete; |
| 74 | |
| 75 | ProducerID const id_; |
| 76 | ServiceImpl* const service_; |
| 77 | base::TaskRunner* const task_runner_; |
| 78 | Producer* producer_; |
| 79 | std::unique_ptr<SharedMemory> shared_memory_; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 80 | SharedMemoryABI shmem_abi_; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 81 | DataSourceID last_data_source_id_ = 0; |
| 82 | }; |
| 83 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 84 | // The implementation behind the service endpoint exposed to each consumer. |
| 85 | class ConsumerEndpointImpl : public Service::ConsumerEndpoint { |
| 86 | public: |
| 87 | ConsumerEndpointImpl(ServiceImpl*, base::TaskRunner*, Consumer*); |
| 88 | ~ConsumerEndpointImpl() override; |
| 89 | |
| 90 | Consumer* consumer() const { return consumer_; } |
| 91 | base::WeakPtr<ConsumerEndpointImpl> GetWeakPtr(); |
| 92 | |
| 93 | // Service::ConsumerEndpoint implementation. |
| 94 | void EnableTracing(const TraceConfig&) override; |
| 95 | void DisableTracing() override; |
| 96 | void ReadBuffers() override; |
| 97 | void FreeBuffers() override; |
| 98 | |
| 99 | private: |
| 100 | ConsumerEndpointImpl(const ConsumerEndpointImpl&) = delete; |
| 101 | ConsumerEndpointImpl& operator=(const ConsumerEndpointImpl&) = delete; |
| 102 | |
| 103 | ServiceImpl* const service_; |
| 104 | Consumer* const consumer_; |
| 105 | base::WeakPtrFactory<ConsumerEndpointImpl> weak_ptr_factory_; |
| 106 | }; |
| 107 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 108 | explicit ServiceImpl(std::unique_ptr<SharedMemory::Factory>, |
| 109 | base::TaskRunner*); |
| 110 | ~ServiceImpl() override; |
| 111 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 112 | // Called by ProducerEndpointImpl. |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 113 | void DisconnectProducer(ProducerID); |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 114 | void RegisterDataSource(ProducerID, |
| 115 | DataSourceID, |
| 116 | const DataSourceDescriptor&); |
| 117 | void CopyProducerPageIntoLogBuffer(ProducerID, |
| 118 | BufferID, |
| 119 | const uint8_t*, |
| 120 | size_t); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 121 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 122 | // Called by ConsumerEndpointImpl. |
| 123 | void DisconnectConsumer(ConsumerEndpointImpl*); |
| 124 | void EnableTracing(ConsumerEndpointImpl*, const TraceConfig&); |
| 125 | void DisableTracing(ConsumerEndpointImpl*); |
| 126 | void ReadBuffers(ConsumerEndpointImpl*); |
| 127 | void FreeBuffers(ConsumerEndpointImpl*); |
| 128 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 129 | // Service implementation. |
| 130 | std::unique_ptr<Service::ProducerEndpoint> ConnectProducer( |
| 131 | Producer*, |
| 132 | size_t shared_buffer_size_hint_bytes = 0) override; |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 133 | |
| 134 | std::unique_ptr<Service::ConsumerEndpoint> ConnectConsumer( |
| 135 | Consumer*) override; |
| 136 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 137 | // Exposed mainly for testing. |
| 138 | size_t num_producers() const { return producers_.size(); } |
| 139 | ProducerEndpointImpl* GetProducer(ProducerID) const; |
| 140 | |
| 141 | private: |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 142 | struct RegisteredDataSource { |
| 143 | ProducerID producer_id; |
| 144 | DataSourceID data_source_id; |
| 145 | DataSourceDescriptor descriptor; |
| 146 | }; |
| 147 | |
| 148 | struct TraceBuffer { |
| 149 | // TODO(primiano): make this configurable. |
| 150 | static constexpr size_t kBufferPageSize = 4096; |
| 151 | explicit TraceBuffer(size_t size); |
| 152 | ~TraceBuffer(); |
| 153 | TraceBuffer(TraceBuffer&&) noexcept; |
| 154 | TraceBuffer& operator=(TraceBuffer&&); |
| 155 | |
Primiano Tucci | bbaa58c | 2017-12-20 13:48:20 +0100 | [diff] [blame] | 156 | bool is_valid() const { return !!data; } |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 157 | size_t num_pages() const { return size / kBufferPageSize; } |
| 158 | |
| 159 | uint8_t* get_page(size_t page) { |
| 160 | PERFETTO_DCHECK(page < num_pages()); |
| 161 | return reinterpret_cast<uint8_t*>(data.get()) + page * kBufferPageSize; |
| 162 | } |
| 163 | |
| 164 | uint8_t* get_next_page() { |
| 165 | size_t cur = cur_page; |
| 166 | cur_page = cur_page == num_pages() - 1 ? 0 : cur_page + 1; |
| 167 | return get_page(cur); |
| 168 | } |
| 169 | |
| 170 | size_t size; |
| 171 | size_t cur_page = 0; // Write pointer in the ring buffer. |
Primiano Tucci | bbaa58c | 2017-12-20 13:48:20 +0100 | [diff] [blame] | 172 | base::PageAllocator::UniquePtr data; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 173 | |
| 174 | // TODO(primiano): The TraceBuffer is not shared and there is no reason to |
| 175 | // use the SharedMemoryABI. This is just a a temporary workaround to reuse |
| 176 | // the convenience of SharedMemoryABI for bookkeeping of the buffer when |
| 177 | // implementing ReadBuffers(). |
| 178 | std::unique_ptr<SharedMemoryABI> abi; |
| 179 | }; |
| 180 | |
| 181 | // Holds the state of a tracing session. A tracing session is uniquely bound |
| 182 | // a specific Consumer. Each Consumer can own one or more sessions. |
| 183 | struct TracingSession { |
Oystein Eftevaag | 1269b4a | 2018-01-10 16:29:38 -0800 | [diff] [blame^] | 184 | TracingSession(const TraceConfig& new_config) : config(new_config) {} |
| 185 | |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 186 | // List of data source instances that have been enabled on the various |
| 187 | // producers for this tracing session. |
| 188 | std::multimap<ProducerID, DataSourceInstanceID> data_source_instances; |
| 189 | |
| 190 | // The key of this map matches the |target_buffer| in the |
| 191 | // SharedMemoryABI::ChunkHeader. |
| 192 | std::map<BufferID, TraceBuffer> trace_buffers; |
Oystein Eftevaag | 1269b4a | 2018-01-10 16:29:38 -0800 | [diff] [blame^] | 193 | |
| 194 | TraceConfig config; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 195 | }; |
| 196 | |
Oystein Eftevaag | 1269b4a | 2018-01-10 16:29:38 -0800 | [diff] [blame^] | 197 | void CreateDataSourceInstanceForProducer( |
| 198 | const TraceConfig::DataSource& cfg_data_source, |
| 199 | ProducerEndpointImpl* producer, |
| 200 | TracingSession* tracing_session); |
| 201 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 202 | ServiceImpl(const ServiceImpl&) = delete; |
| 203 | ServiceImpl& operator=(const ServiceImpl&) = delete; |
| 204 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 205 | base::TaskRunner* const task_runner_; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 206 | std::unique_ptr<SharedMemory::Factory> shm_factory_; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 207 | ProducerID last_producer_id_ = 0; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 208 | DataSourceInstanceID last_data_source_instance_id_ = 0; |
| 209 | |
| 210 | // Buffer IDs are global across all consumers (because a Producer can produce |
| 211 | // data for more than one trace session, hence more than one consumer). |
| 212 | IdAllocator buffer_ids_; |
| 213 | |
| 214 | std::multimap<std::string /*name*/, RegisteredDataSource> data_sources_; |
| 215 | |
| 216 | // TODO(primiano): There doesn't seem to be any good reason why |producers_| |
| 217 | // is a map indexed by ID and not just a set<ProducerEndpointImpl*>. |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 218 | std::map<ProducerID, ProducerEndpointImpl*> producers_; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 219 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 220 | std::set<ConsumerEndpointImpl*> consumers_; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 221 | std::map<ConsumerEndpointImpl*, TracingSession> tracing_sessions_; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | } // namespace perfetto |
| 225 | |
| 226 | #endif // SRC_TRACING_CORE_SERVICE_IMPL_H_ |