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 | |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 25 | #include "gtest/gtest_prod.h" |
Primiano Tucci | bbaa58c | 2017-12-20 13:48:20 +0100 | [diff] [blame] | 26 | #include "perfetto/base/page_allocator.h" |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 27 | #include "perfetto/base/weak_ptr.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 28 | #include "perfetto/tracing/core/basic_types.h" |
Primiano Tucci | ecf9e4a | 2018-03-14 14:51:58 +0000 | [diff] [blame^] | 29 | #include "perfetto/tracing/core/commit_data_request.h" |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 30 | #include "perfetto/tracing/core/data_source_descriptor.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 31 | #include "perfetto/tracing/core/service.h" |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 32 | #include "perfetto/tracing/core/shared_memory_abi.h" |
Oystein Eftevaag | 1269b4a | 2018-01-10 16:29:38 -0800 | [diff] [blame] | 33 | #include "perfetto/tracing/core/trace_config.h" |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 34 | #include "src/tracing/core/id_allocator.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 35 | |
| 36 | namespace perfetto { |
| 37 | |
| 38 | namespace base { |
| 39 | class TaskRunner; |
| 40 | } // namespace base |
| 41 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 42 | class Consumer; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 43 | class DataSourceConfig; |
| 44 | class Producer; |
| 45 | class SharedMemory; |
Primiano Tucci | ecf9e4a | 2018-03-14 14:51:58 +0000 | [diff] [blame^] | 46 | class TraceBuffez; |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 47 | class TraceConfig; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 48 | |
| 49 | // The tracing service business logic. |
| 50 | class ServiceImpl : public Service { |
| 51 | public: |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 52 | using TracingSessionID = uint64_t; |
| 53 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 54 | // The implementation behind the service endpoint exposed to each producer. |
| 55 | class ProducerEndpointImpl : public Service::ProducerEndpoint { |
| 56 | public: |
| 57 | ProducerEndpointImpl(ProducerID, |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 58 | uid_t uid, |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 59 | ServiceImpl*, |
| 60 | base::TaskRunner*, |
| 61 | Producer*, |
| 62 | std::unique_ptr<SharedMemory>); |
| 63 | ~ProducerEndpointImpl() override; |
| 64 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 65 | // Service::ProducerEndpoint implementation. |
| 66 | void RegisterDataSource(const DataSourceDescriptor&, |
| 67 | RegisterDataSourceCallback) override; |
| 68 | void UnregisterDataSource(DataSourceID) override; |
Primiano Tucci | 79f3f91 | 2018-03-02 12:00:31 +0000 | [diff] [blame] | 69 | void CommitData(const CommitDataRequest&) override; |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 70 | std::unique_ptr<TraceWriter> CreateTraceWriter(BufferID) override; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 71 | SharedMemory* shared_memory() const override; |
| 72 | |
| 73 | private: |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 74 | friend class ServiceImpl; |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 75 | FRIEND_TEST(ServiceImplTest, RegisterAndUnregister); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 76 | ProducerEndpointImpl(const ProducerEndpointImpl&) = delete; |
| 77 | ProducerEndpointImpl& operator=(const ProducerEndpointImpl&) = delete; |
| 78 | |
| 79 | ProducerID const id_; |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 80 | const uid_t uid_; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 81 | ServiceImpl* const service_; |
| 82 | base::TaskRunner* const task_runner_; |
| 83 | Producer* producer_; |
| 84 | std::unique_ptr<SharedMemory> shared_memory_; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 85 | SharedMemoryABI shmem_abi_; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 86 | DataSourceID last_data_source_id_ = 0; |
Florian Mayer | cd08ec6 | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 87 | PERFETTO_THREAD_CHECKER(thread_checker_) |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 90 | // The implementation behind the service endpoint exposed to each consumer. |
| 91 | class ConsumerEndpointImpl : public Service::ConsumerEndpoint { |
| 92 | public: |
| 93 | ConsumerEndpointImpl(ServiceImpl*, base::TaskRunner*, Consumer*); |
| 94 | ~ConsumerEndpointImpl() override; |
| 95 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 96 | base::WeakPtr<ConsumerEndpointImpl> GetWeakPtr(); |
| 97 | |
| 98 | // Service::ConsumerEndpoint implementation. |
| 99 | void EnableTracing(const TraceConfig&) override; |
| 100 | void DisableTracing() override; |
| 101 | void ReadBuffers() override; |
| 102 | void FreeBuffers() override; |
| 103 | |
| 104 | private: |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 105 | friend class ServiceImpl; |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 106 | ConsumerEndpointImpl(const ConsumerEndpointImpl&) = delete; |
| 107 | ConsumerEndpointImpl& operator=(const ConsumerEndpointImpl&) = delete; |
| 108 | |
| 109 | ServiceImpl* const service_; |
| 110 | Consumer* const consumer_; |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 111 | TracingSessionID tracing_session_id_ = 0; |
Florian Mayer | cd08ec6 | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 112 | |
| 113 | PERFETTO_THREAD_CHECKER(thread_checker_) |
| 114 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 115 | base::WeakPtrFactory<ConsumerEndpointImpl> weak_ptr_factory_; |
| 116 | }; |
| 117 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 118 | explicit ServiceImpl(std::unique_ptr<SharedMemory::Factory>, |
| 119 | base::TaskRunner*); |
| 120 | ~ServiceImpl() override; |
| 121 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 122 | // Called by ProducerEndpointImpl. |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 123 | void DisconnectProducer(ProducerID); |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 124 | void RegisterDataSource(ProducerID, |
| 125 | DataSourceID, |
| 126 | const DataSourceDescriptor&); |
Sami Kyostila | 06487a2 | 2018-02-27 13:48:38 +0000 | [diff] [blame] | 127 | void UnregisterDataSource(ProducerID, DataSourceID); |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 128 | void CopyProducerPageIntoLogBuffer(ProducerID, |
Primiano Tucci | ecf9e4a | 2018-03-14 14:51:58 +0000 | [diff] [blame^] | 129 | uid_t, |
| 130 | WriterID, |
| 131 | ChunkID, |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 132 | BufferID, |
Primiano Tucci | ecf9e4a | 2018-03-14 14:51:58 +0000 | [diff] [blame^] | 133 | uint16_t num_fragments, |
| 134 | uint8_t chunk_flags, |
| 135 | const uint8_t* src, |
| 136 | size_t size); |
| 137 | void ApplyChunkPatches(ProducerID, |
| 138 | const std::vector<CommitDataRequest::ChunkToPatch>&); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 139 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 140 | // Called by ConsumerEndpointImpl. |
| 141 | void DisconnectConsumer(ConsumerEndpointImpl*); |
| 142 | void EnableTracing(ConsumerEndpointImpl*, const TraceConfig&); |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 143 | void DisableTracing(TracingSessionID); |
| 144 | void ReadBuffers(TracingSessionID, ConsumerEndpointImpl*); |
| 145 | void FreeBuffers(TracingSessionID); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 146 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 147 | // Service implementation. |
| 148 | std::unique_ptr<Service::ProducerEndpoint> ConnectProducer( |
| 149 | Producer*, |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 150 | uid_t uid, |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 151 | size_t shared_buffer_size_hint_bytes = 0) override; |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 152 | |
| 153 | std::unique_ptr<Service::ConsumerEndpoint> ConnectConsumer( |
| 154 | Consumer*) override; |
| 155 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 156 | // Exposed mainly for testing. |
| 157 | size_t num_producers() const { return producers_.size(); } |
| 158 | ProducerEndpointImpl* GetProducer(ProducerID) const; |
| 159 | |
| 160 | private: |
Primiano Tucci | 081d46a | 2018-02-28 11:09:43 +0000 | [diff] [blame] | 161 | FRIEND_TEST(ServiceImplTest, ProducerIDWrapping); |
| 162 | |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 163 | struct RegisteredDataSource { |
| 164 | ProducerID producer_id; |
| 165 | DataSourceID data_source_id; |
| 166 | DataSourceDescriptor descriptor; |
| 167 | }; |
| 168 | |
Sami Kyostila | 06487a2 | 2018-02-27 13:48:38 +0000 | [diff] [blame] | 169 | // Represents an active data source for a tracing session. |
| 170 | struct DataSourceInstance { |
| 171 | DataSourceInstanceID instance_id; |
| 172 | DataSourceID data_source_id; |
| 173 | }; |
| 174 | |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 175 | // Holds the state of a tracing session. A tracing session is uniquely bound |
| 176 | // a specific Consumer. Each Consumer can own one or more sessions. |
| 177 | struct TracingSession { |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 178 | explicit TracingSession(const TraceConfig&); |
| 179 | |
| 180 | size_t num_buffers() const { return buffers_index.size(); } |
| 181 | |
| 182 | // The original trace config provided by the Consumer when calling |
| 183 | // EnableTracing(). |
| 184 | const TraceConfig config; |
Oystein Eftevaag | 1269b4a | 2018-01-10 16:29:38 -0800 | [diff] [blame] | 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. |
Sami Kyostila | 06487a2 | 2018-02-27 13:48:38 +0000 | [diff] [blame] | 188 | std::multimap<ProducerID, DataSourceInstance> data_source_instances; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 189 | |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 190 | // Maps a per-trace-session buffer index into the corresponding global |
| 191 | // BufferID (shared namespace amongst all consumers). This vector has as |
| 192 | // many entries as |config.buffers_size()|. |
| 193 | std::vector<BufferID> buffers_index; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 194 | }; |
| 195 | |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 196 | ServiceImpl(const ServiceImpl&) = delete; |
| 197 | ServiceImpl& operator=(const ServiceImpl&) = delete; |
| 198 | |
Sami Kyostila | 06487a2 | 2018-02-27 13:48:38 +0000 | [diff] [blame] | 199 | void CreateDataSourceInstance(const TraceConfig::DataSource&, |
| 200 | const RegisteredDataSource&, |
| 201 | TracingSession*); |
Oystein Eftevaag | 1269b4a | 2018-01-10 16:29:38 -0800 | [diff] [blame] | 202 | |
Primiano Tucci | 081d46a | 2018-02-28 11:09:43 +0000 | [diff] [blame] | 203 | // Returns the next available ProducerID that is not in |producers_|. |
| 204 | ProducerID GetNextProducerID(); |
| 205 | |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 206 | // Returns a pointer to the |tracing_sessions_| entry or nullptr if the |
| 207 | // session doesn't exists. |
| 208 | TracingSession* GetTracingSession(TracingSessionID); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 209 | |
Lalit Maganti | 485faff | 2018-03-06 11:51:35 +0000 | [diff] [blame] | 210 | // Update the memory guard rail by using the latest information from the |
| 211 | // shared memory and trace buffers. |
| 212 | void UpdateMemoryGuardrail(); |
| 213 | |
Primiano Tucci | ecf9e4a | 2018-03-14 14:51:58 +0000 | [diff] [blame^] | 214 | TraceBuffez* GetBufferByID(BufferID); |
| 215 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 216 | base::TaskRunner* const task_runner_; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 217 | std::unique_ptr<SharedMemory::Factory> shm_factory_; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 218 | ProducerID last_producer_id_ = 0; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 219 | DataSourceInstanceID last_data_source_instance_id_ = 0; |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 220 | TracingSessionID last_tracing_session_id_ = 0; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 221 | |
| 222 | // Buffer IDs are global across all consumers (because a Producer can produce |
| 223 | // data for more than one trace session, hence more than one consumer). |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 224 | IdAllocator<BufferID> buffer_ids_; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 225 | |
| 226 | std::multimap<std::string /*name*/, RegisteredDataSource> data_sources_; |
| 227 | |
| 228 | // TODO(primiano): There doesn't seem to be any good reason why |producers_| |
| 229 | // is a map indexed by ID and not just a set<ProducerEndpointImpl*>. |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 230 | std::map<ProducerID, ProducerEndpointImpl*> producers_; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 231 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 232 | std::set<ConsumerEndpointImpl*> consumers_; |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 233 | std::map<TracingSessionID, TracingSession> tracing_sessions_; |
Primiano Tucci | ecf9e4a | 2018-03-14 14:51:58 +0000 | [diff] [blame^] | 234 | std::map<BufferID, std::unique_ptr<TraceBuffez>> buffers_; |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 235 | |
Florian Mayer | 61c5548 | 2018-03-06 14:43:54 +0000 | [diff] [blame] | 236 | bool lockdown_mode_ = false; |
| 237 | |
Florian Mayer | cd08ec6 | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 238 | PERFETTO_THREAD_CHECKER(thread_checker_) |
| 239 | |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 240 | base::WeakPtrFactory<ServiceImpl> weak_ptr_factory_; // Keep at the end. |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | } // namespace perfetto |
| 244 | |
| 245 | #endif // SRC_TRACING_CORE_SERVICE_IMPL_H_ |