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: |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame^] | 49 | using TracingSessionID = uint64_t; |
| 50 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 51 | // The implementation behind the service endpoint exposed to each producer. |
| 52 | class ProducerEndpointImpl : public Service::ProducerEndpoint { |
| 53 | public: |
| 54 | ProducerEndpointImpl(ProducerID, |
| 55 | ServiceImpl*, |
| 56 | base::TaskRunner*, |
| 57 | Producer*, |
| 58 | std::unique_ptr<SharedMemory>); |
| 59 | ~ProducerEndpointImpl() override; |
| 60 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 61 | // Service::ProducerEndpoint implementation. |
| 62 | void RegisterDataSource(const DataSourceDescriptor&, |
| 63 | RegisterDataSourceCallback) override; |
| 64 | void UnregisterDataSource(DataSourceID) override; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 65 | void NotifySharedMemoryUpdate( |
| 66 | const std::vector<uint32_t>& changed_pages) override; |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 67 | std::unique_ptr<TraceWriter> CreateTraceWriter(BufferID) override; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 68 | SharedMemory* shared_memory() const override; |
| 69 | |
| 70 | private: |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame^] | 71 | friend class ServiceImpl; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 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 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 90 | base::WeakPtr<ConsumerEndpointImpl> GetWeakPtr(); |
| 91 | |
| 92 | // Service::ConsumerEndpoint implementation. |
| 93 | void EnableTracing(const TraceConfig&) override; |
| 94 | void DisableTracing() override; |
| 95 | void ReadBuffers() override; |
| 96 | void FreeBuffers() override; |
| 97 | |
| 98 | private: |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame^] | 99 | friend class ServiceImpl; |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 100 | ConsumerEndpointImpl(const ConsumerEndpointImpl&) = delete; |
| 101 | ConsumerEndpointImpl& operator=(const ConsumerEndpointImpl&) = delete; |
| 102 | |
| 103 | ServiceImpl* const service_; |
| 104 | Consumer* const consumer_; |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame^] | 105 | TracingSessionID tracing_session_id_ = 0; |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 106 | base::WeakPtrFactory<ConsumerEndpointImpl> weak_ptr_factory_; |
| 107 | }; |
| 108 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 109 | explicit ServiceImpl(std::unique_ptr<SharedMemory::Factory>, |
| 110 | base::TaskRunner*); |
| 111 | ~ServiceImpl() override; |
| 112 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 113 | // Called by ProducerEndpointImpl. |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 114 | void DisconnectProducer(ProducerID); |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 115 | void RegisterDataSource(ProducerID, |
| 116 | DataSourceID, |
| 117 | const DataSourceDescriptor&); |
| 118 | void CopyProducerPageIntoLogBuffer(ProducerID, |
| 119 | BufferID, |
| 120 | const uint8_t*, |
| 121 | size_t); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 122 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 123 | // Called by ConsumerEndpointImpl. |
| 124 | void DisconnectConsumer(ConsumerEndpointImpl*); |
| 125 | void EnableTracing(ConsumerEndpointImpl*, const TraceConfig&); |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame^] | 126 | void DisableTracing(TracingSessionID); |
| 127 | void ReadBuffers(TracingSessionID, ConsumerEndpointImpl*); |
| 128 | void FreeBuffers(TracingSessionID); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 129 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 130 | // Service implementation. |
| 131 | std::unique_ptr<Service::ProducerEndpoint> ConnectProducer( |
| 132 | Producer*, |
| 133 | size_t shared_buffer_size_hint_bytes = 0) override; |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 134 | |
| 135 | std::unique_ptr<Service::ConsumerEndpoint> ConnectConsumer( |
| 136 | Consumer*) override; |
| 137 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 138 | // Exposed mainly for testing. |
| 139 | size_t num_producers() const { return producers_.size(); } |
| 140 | ProducerEndpointImpl* GetProducer(ProducerID) const; |
| 141 | |
| 142 | private: |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 143 | struct RegisteredDataSource { |
| 144 | ProducerID producer_id; |
| 145 | DataSourceID data_source_id; |
| 146 | DataSourceDescriptor descriptor; |
| 147 | }; |
| 148 | |
| 149 | struct TraceBuffer { |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame^] | 150 | TraceBuffer(); |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 151 | ~TraceBuffer(); |
| 152 | TraceBuffer(TraceBuffer&&) noexcept; |
| 153 | TraceBuffer& operator=(TraceBuffer&&); |
| 154 | |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame^] | 155 | bool Create(size_t size); |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 156 | size_t num_pages() const { return size / kBufferPageSize; } |
| 157 | |
| 158 | uint8_t* get_page(size_t page) { |
| 159 | PERFETTO_DCHECK(page < num_pages()); |
| 160 | return reinterpret_cast<uint8_t*>(data.get()) + page * kBufferPageSize; |
| 161 | } |
| 162 | |
| 163 | uint8_t* get_next_page() { |
| 164 | size_t cur = cur_page; |
| 165 | cur_page = cur_page == num_pages() - 1 ? 0 : cur_page + 1; |
| 166 | return get_page(cur); |
| 167 | } |
| 168 | |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame^] | 169 | size_t size = 0; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 170 | size_t cur_page = 0; // Write pointer in the ring buffer. |
Primiano Tucci | bbaa58c | 2017-12-20 13:48:20 +0100 | [diff] [blame] | 171 | base::PageAllocator::UniquePtr data; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 172 | |
| 173 | // TODO(primiano): The TraceBuffer is not shared and there is no reason to |
| 174 | // use the SharedMemoryABI. This is just a a temporary workaround to reuse |
| 175 | // the convenience of SharedMemoryABI for bookkeeping of the buffer when |
| 176 | // implementing ReadBuffers(). |
| 177 | std::unique_ptr<SharedMemoryABI> abi; |
| 178 | }; |
| 179 | |
| 180 | // Holds the state of a tracing session. A tracing session is uniquely bound |
| 181 | // a specific Consumer. Each Consumer can own one or more sessions. |
| 182 | struct TracingSession { |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame^] | 183 | explicit TracingSession(const TraceConfig&); |
| 184 | |
| 185 | size_t num_buffers() const { return buffers_index.size(); } |
| 186 | |
| 187 | // The original trace config provided by the Consumer when calling |
| 188 | // EnableTracing(). |
| 189 | const TraceConfig config; |
Oystein Eftevaag | 1269b4a | 2018-01-10 16:29:38 -0800 | [diff] [blame] | 190 | |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 191 | // List of data source instances that have been enabled on the various |
| 192 | // producers for this tracing session. |
| 193 | std::multimap<ProducerID, DataSourceInstanceID> data_source_instances; |
| 194 | |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame^] | 195 | // Maps a per-trace-session buffer index into the corresponding global |
| 196 | // BufferID (shared namespace amongst all consumers). This vector has as |
| 197 | // many entries as |config.buffers_size()|. |
| 198 | std::vector<BufferID> buffers_index; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 199 | }; |
| 200 | |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame^] | 201 | ServiceImpl(const ServiceImpl&) = delete; |
| 202 | ServiceImpl& operator=(const ServiceImpl&) = delete; |
| 203 | |
Oystein Eftevaag | 1269b4a | 2018-01-10 16:29:38 -0800 | [diff] [blame] | 204 | void CreateDataSourceInstanceForProducer( |
| 205 | const TraceConfig::DataSource& cfg_data_source, |
| 206 | ProducerEndpointImpl* producer, |
| 207 | TracingSession* tracing_session); |
| 208 | |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame^] | 209 | // Returns a pointer to the |tracing_sessions_| entry or nullptr if the |
| 210 | // session doesn't exists. |
| 211 | TracingSession* GetTracingSession(TracingSessionID); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 212 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 213 | base::TaskRunner* const task_runner_; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 214 | std::unique_ptr<SharedMemory::Factory> shm_factory_; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 215 | ProducerID last_producer_id_ = 0; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 216 | DataSourceInstanceID last_data_source_instance_id_ = 0; |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame^] | 217 | TracingSessionID last_tracing_session_id_ = 0; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 218 | |
| 219 | // Buffer IDs are global across all consumers (because a Producer can produce |
| 220 | // data for more than one trace session, hence more than one consumer). |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame^] | 221 | IdAllocator<BufferID> buffer_ids_; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 222 | |
| 223 | std::multimap<std::string /*name*/, RegisteredDataSource> data_sources_; |
| 224 | |
| 225 | // TODO(primiano): There doesn't seem to be any good reason why |producers_| |
| 226 | // is a map indexed by ID and not just a set<ProducerEndpointImpl*>. |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 227 | std::map<ProducerID, ProducerEndpointImpl*> producers_; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 228 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 229 | std::set<ConsumerEndpointImpl*> consumers_; |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame^] | 230 | std::map<TracingSessionID, TracingSession> tracing_sessions_; |
| 231 | std::map<BufferID, TraceBuffer> buffers_; |
| 232 | |
| 233 | base::WeakPtrFactory<ServiceImpl> weak_ptr_factory_; // Keep at the end. |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | } // namespace perfetto |
| 237 | |
| 238 | #endif // SRC_TRACING_CORE_SERVICE_IMPL_H_ |