1 /* 2 * Copyright (C) 2019 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_INTERNAL_TRACING_MUXER_IMPL_H_ 18 #define SRC_TRACING_INTERNAL_TRACING_MUXER_IMPL_H_ 19 20 #include <stddef.h> 21 #include <stdint.h> 22 23 #include <array> 24 #include <atomic> 25 #include <bitset> 26 #include <list> 27 #include <map> 28 #include <memory> 29 #include <vector> 30 31 #include "perfetto/ext/base/scoped_file.h" 32 #include "perfetto/ext/base/thread_checker.h" 33 #include "perfetto/ext/tracing/core/basic_types.h" 34 #include "perfetto/ext/tracing/core/consumer.h" 35 #include "perfetto/ext/tracing/core/producer.h" 36 #include "perfetto/tracing/core/data_source_descriptor.h" 37 #include "perfetto/tracing/core/forward_decls.h" 38 #include "perfetto/tracing/core/trace_config.h" 39 #include "perfetto/tracing/internal/basic_types.h" 40 #include "perfetto/tracing/internal/tracing_muxer.h" 41 #include "perfetto/tracing/tracing.h" 42 43 #include "protos/perfetto/common/interceptor_descriptor.gen.h" 44 45 namespace perfetto { 46 47 class ConsumerEndpoint; 48 class DataSourceBase; 49 class ProducerEndpoint; 50 class TraceWriterBase; 51 class TracingBackend; 52 class TracingSession; 53 struct TracingInitArgs; 54 55 namespace base { 56 class TaskRunner; 57 } 58 59 namespace internal { 60 61 struct DataSourceStaticState; 62 63 // This class acts as a bridge between the public API and the TracingBackend(s). 64 // It exposes a simplified view of the world to the API methods handling all the 65 // bookkeeping to map data source instances and trace writers to the various 66 // backends. It deals with N data sources, M backends (1 backend == 1 tracing 67 // service == 1 producer connection) and T concurrent tracing sessions. 68 // 69 // Handing data source registration and start/stop flows [producer side]: 70 // ---------------------------------------------------------------------- 71 // 1. The API client subclasses perfetto::DataSource and calls 72 // DataSource::Register<MyDataSource>(). In turn this calls into the 73 // TracingMuxer. 74 // 2. The tracing muxer iterates through all the backends (1 backend == 1 75 // service == 1 producer connection) and registers the data source on each 76 // backend. 77 // 3. When any (services behind a) backend starts tracing and requests to start 78 // that specific data source, the TracingMuxerImpl constructs a new instance 79 // of MyDataSource and calls the OnStart() method. 80 // 81 // Controlling trace and retrieving trace data [consumer side]: 82 // ------------------------------------------------------------ 83 // 1. The API client calls Tracing::NewTrace(), returns a RAII TracingSession 84 // object. 85 // 2. NewTrace() calls into internal::TracingMuxer(Impl). TracingMuxer 86 // subclasses the TracingSession object (TracingSessionImpl) and returns it. 87 // 3. The tracing muxer identifies the backend (according to the args passed to 88 // NewTrace), creates a new Consumer and connects to it. 89 // 4. When the API client calls Start()/Stop()/ReadTrace() methods, the 90 // TracingMuxer forwards them to the consumer associated to the 91 // TracingSession. Likewise for callbacks coming from the consumer-side of 92 // the service. 93 class TracingMuxerImpl : public TracingMuxer { 94 public: 95 // This is different than TracingSessionID because it's global across all 96 // backends. TracingSessionID is global only within the scope of one service. 97 using TracingSessionGlobalID = uint64_t; 98 99 static void InitializeInstance(const TracingInitArgs&); 100 101 // TracingMuxer implementation. 102 bool RegisterDataSource(const DataSourceDescriptor&, 103 DataSourceFactory, 104 DataSourceStaticState*) override; 105 std::unique_ptr<TraceWriterBase> CreateTraceWriter( 106 DataSourceStaticState*, 107 uint32_t data_source_instance_index, 108 DataSourceState*, 109 BufferExhaustedPolicy buffer_exhausted_policy) override; 110 void DestroyStoppedTraceWritersForCurrentThread() override; 111 void RegisterInterceptor(const InterceptorDescriptor&, 112 InterceptorFactory, 113 InterceptorBase::TLSFactory, 114 InterceptorBase::TracePacketCallback) override; 115 116 std::unique_ptr<TracingSession> CreateTracingSession(BackendType); 117 118 // Producer-side bookkeeping methods. 119 void UpdateDataSourcesOnAllBackends(); 120 void SetupDataSource(TracingBackendId, 121 uint32_t backend_connection_id, 122 DataSourceInstanceID, 123 const DataSourceConfig&); 124 void StartDataSource(TracingBackendId, DataSourceInstanceID); 125 void StopDataSource_AsyncBegin(TracingBackendId, DataSourceInstanceID); 126 void StopDataSource_AsyncEnd(TracingBackendId, DataSourceInstanceID); 127 void ClearDataSourceIncrementalState(TracingBackendId, DataSourceInstanceID); 128 void SyncProducersForTesting(); 129 130 // Consumer-side bookkeeping methods. 131 void SetupTracingSession(TracingSessionGlobalID, 132 const std::shared_ptr<TraceConfig>&, 133 base::ScopedFile trace_fd = base::ScopedFile()); 134 void StartTracingSession(TracingSessionGlobalID); 135 void ChangeTracingSessionConfig(TracingSessionGlobalID, const TraceConfig&); 136 void StopTracingSession(TracingSessionGlobalID); 137 void DestroyTracingSession(TracingSessionGlobalID); 138 void FlushTracingSession(TracingSessionGlobalID, 139 uint32_t, 140 std::function<void(bool)>); 141 void ReadTracingSessionData( 142 TracingSessionGlobalID, 143 std::function<void(TracingSession::ReadTraceCallbackArgs)>); 144 void GetTraceStats(TracingSessionGlobalID, 145 TracingSession::GetTraceStatsCallback); 146 void QueryServiceState(TracingSessionGlobalID, 147 TracingSession::QueryServiceStateCallback); 148 149 // Sets the batching period to |batch_commits_duration_ms| on the backends 150 // with type |backend_type|. 151 void SetBatchCommitsDurationForTesting(uint32_t batch_commits_duration_ms, 152 BackendType backend_type); 153 154 // Enables direct SMB patching on the backends with type |backend_type| (see 155 // SharedMemoryArbiter::EnableDirectSMBPatching). Returns true if the 156 // operation succeeded for all backends with type |backend_type|, false 157 // otherwise. 158 bool EnableDirectSMBPatchingForTesting(BackendType backend_type); 159 160 void SetMaxProducerReconnectionsForTesting(uint32_t count); 161 162 private: 163 // For each TracingBackend we create and register one ProducerImpl instance. 164 // This talks to the producer-side of the service, gets start/stop requests 165 // from it and routes them to the registered data sources. 166 // One ProducerImpl == one backend == one tracing service. 167 // This class is needed to disambiguate callbacks coming from different 168 // services. TracingMuxerImpl can't directly implement the Producer interface 169 // because the Producer virtual methods don't allow to identify the service. 170 class ProducerImpl : public Producer { 171 public: 172 ProducerImpl(TracingMuxerImpl*, 173 TracingBackendId, 174 uint32_t shmem_batch_commits_duration_ms); 175 ~ProducerImpl() override; 176 177 void Initialize(std::unique_ptr<ProducerEndpoint> endpoint); 178 void RegisterDataSource(const DataSourceDescriptor&, 179 DataSourceFactory, 180 DataSourceStaticState*); 181 182 // perfetto::Producer implementation. 183 void OnConnect() override; 184 void OnDisconnect() override; 185 void OnTracingSetup() override; 186 void SetupDataSource(DataSourceInstanceID, 187 const DataSourceConfig&) override; 188 void StartDataSource(DataSourceInstanceID, 189 const DataSourceConfig&) override; 190 void StopDataSource(DataSourceInstanceID) override; 191 void Flush(FlushRequestID, const DataSourceInstanceID*, size_t) override; 192 void ClearIncrementalState(const DataSourceInstanceID*, size_t) override; 193 194 void SweepDeadServices(); 195 196 PERFETTO_THREAD_CHECKER(thread_checker_) 197 TracingMuxerImpl* const muxer_; 198 TracingBackendId const backend_id_; 199 bool connected_ = false; 200 uint32_t connection_id_ = 0; 201 202 const uint32_t shmem_batch_commits_duration_ms_ = 0; 203 204 // Set of data sources that have been actually registered on this producer. 205 // This can be a subset of the global |data_sources_|, because data sources 206 // can register before the producer is fully connected. 207 std::bitset<kMaxDataSources> registered_data_sources_{}; 208 209 // A collection of disconnected service endpoints. Since trace writers on 210 // arbitrary threads might continue writing data to disconnected services, 211 // we keep the old services around and periodically try to clean up ones 212 // that no longer have any writers (see SweepDeadServices). 213 std::list<std::shared_ptr<ProducerEndpoint>> dead_services_; 214 215 // The currently active service endpoint is maintained as an atomic shared 216 // pointer so it won't get deleted from underneath threads that are creating 217 // trace writers. At any given time one endpoint can be shared (and thus 218 // kept alive) by the |service_| pointer, an entry in |dead_services_| and 219 // as a pointer on the stack in CreateTraceWriter() (on an arbitrary 220 // thread). The endpoint is never shared outside ProducerImpl itself. 221 // 222 // WARNING: Any *write* access to this variable or any *read* access from a 223 // non-muxer thread must be done through std::atomic_{load,store} to avoid 224 // data races. 225 std::shared_ptr<ProducerEndpoint> service_; // Keep last. 226 }; 227 228 // For each TracingSession created by the API client (Tracing::NewTrace() we 229 // create and register one ConsumerImpl instance. 230 // This talks to the consumer-side of the service, gets end-of-trace and 231 // on-trace-data callbacks and routes them to the API client callbacks. 232 // This class is needed to disambiguate callbacks coming from different 233 // tracing sessions. 234 class ConsumerImpl : public Consumer { 235 public: 236 ConsumerImpl(TracingMuxerImpl*, 237 BackendType, 238 TracingBackendId, 239 TracingSessionGlobalID); 240 ~ConsumerImpl() override; 241 242 void Initialize(std::unique_ptr<ConsumerEndpoint> endpoint); 243 244 // perfetto::Consumer implementation. 245 void OnConnect() override; 246 void OnDisconnect() override; 247 void OnTracingDisabled(const std::string& error) override; 248 void OnTraceData(std::vector<TracePacket>, bool has_more) override; 249 void OnDetach(bool success) override; 250 void OnAttach(bool success, const TraceConfig&) override; 251 void OnTraceStats(bool success, const TraceStats&) override; 252 void OnObservableEvents(const ObservableEvents&) override; 253 254 void NotifyStartComplete(); 255 void NotifyError(const TracingError&); 256 void NotifyStopComplete(); 257 258 // Will eventually inform the |muxer_| when it is safe to remove |this|. 259 void Disconnect(); 260 261 TracingMuxerImpl* const muxer_; 262 BackendType const backend_type_; 263 TracingBackendId const backend_id_; 264 TracingSessionGlobalID const session_id_; 265 bool connected_ = false; 266 267 // This is to handle the case where the Setup call from the API client 268 // arrives before the consumer has connected. In this case we keep around 269 // the config and check if we have it after connection. 270 bool start_pending_ = false; 271 272 // Similarly if the session is stopped before the consumer was connected, we 273 // need to wait until the session has started before stopping it. 274 bool stop_pending_ = false; 275 276 // Similarly we need to buffer a call to get trace statistics if the 277 // consumer wasn't connected yet. 278 bool get_trace_stats_pending_ = false; 279 280 // Whether this session was already stopped. This will happen in response to 281 // Stop{,Blocking}, but also if the service stops the session for us 282 // automatically (e.g., when there are no data sources). 283 bool stopped_ = false; 284 285 // shared_ptr because it's posted across threads. This is to avoid copying 286 // it more than once. 287 std::shared_ptr<TraceConfig> trace_config_; 288 base::ScopedFile trace_fd_; 289 290 // If the API client passes a callback to start, we should invoke this when 291 // NotifyStartComplete() is invoked. 292 std::function<void()> start_complete_callback_; 293 294 // An internal callback used to implement StartBlocking(). 295 std::function<void()> blocking_start_complete_callback_; 296 297 // If the API client passes a callback to get notification about the 298 // errors, we should invoke this when NotifyError() is invoked. 299 std::function<void(TracingError)> error_callback_; 300 301 // If the API client passes a callback to stop, we should invoke this when 302 // OnTracingDisabled() is invoked. 303 std::function<void()> stop_complete_callback_; 304 305 // An internal callback used to implement StopBlocking(). 306 std::function<void()> blocking_stop_complete_callback_; 307 308 // Callback passed to ReadTrace(). 309 std::function<void(TracingSession::ReadTraceCallbackArgs)> 310 read_trace_callback_; 311 312 // Callback passed to GetTraceStats(). 313 TracingSession::GetTraceStatsCallback get_trace_stats_callback_; 314 315 // Callback for a pending call to QueryServiceState(). 316 TracingSession::QueryServiceStateCallback query_service_state_callback_; 317 318 // The states of all data sources in this tracing session. |true| means the 319 // data source has started tracing. 320 using DataSourceHandle = std::pair<std::string, std::string>; 321 std::map<DataSourceHandle, bool> data_source_states_; 322 323 std::unique_ptr<ConsumerEndpoint> service_; // Keep before last. 324 PERFETTO_THREAD_CHECKER(thread_checker_) // Keep last. 325 }; 326 327 // This object is returned to API clients when they call 328 // Tracing::CreateTracingSession(). 329 class TracingSessionImpl : public TracingSession { 330 public: 331 TracingSessionImpl(TracingMuxerImpl*, TracingSessionGlobalID, BackendType); 332 ~TracingSessionImpl() override; 333 void Setup(const TraceConfig&, int fd) override; 334 void Start() override; 335 void StartBlocking() override; 336 void SetOnStartCallback(std::function<void()>) override; 337 void SetOnErrorCallback(std::function<void(TracingError)>) override; 338 void Stop() override; 339 void StopBlocking() override; 340 void Flush(std::function<void(bool)>, uint32_t timeout_ms) override; 341 void ReadTrace(ReadTraceCallback) override; 342 void SetOnStopCallback(std::function<void()>) override; 343 void GetTraceStats(GetTraceStatsCallback) override; 344 void QueryServiceState(QueryServiceStateCallback) override; 345 void ChangeTraceConfig(const TraceConfig&) override; 346 347 private: 348 TracingMuxerImpl* const muxer_; 349 TracingSessionGlobalID const session_id_; 350 BackendType const backend_type_; 351 }; 352 353 struct RegisteredDataSource { 354 DataSourceDescriptor descriptor; 355 DataSourceFactory factory{}; 356 DataSourceStaticState* static_state = nullptr; 357 }; 358 359 struct RegisteredInterceptor { 360 protos::gen::InterceptorDescriptor descriptor; 361 InterceptorFactory factory{}; 362 InterceptorBase::TLSFactory tls_factory{}; 363 InterceptorBase::TracePacketCallback packet_callback{}; 364 }; 365 366 struct RegisteredBackend { 367 // Backends are supposed to have static lifetime. 368 TracingBackend* backend = nullptr; 369 TracingBackendId id = 0; 370 BackendType type{}; 371 372 TracingBackend::ConnectProducerArgs producer_conn_args; 373 std::unique_ptr<ProducerImpl> producer; 374 375 // The calling code can request more than one concurrently active tracing 376 // session for the same backend. We need to create one consumer per session. 377 std::vector<std::unique_ptr<ConsumerImpl>> consumers; 378 }; 379 380 explicit TracingMuxerImpl(const TracingInitArgs&); 381 void Initialize(const TracingInitArgs& args); 382 ConsumerImpl* FindConsumer(TracingSessionGlobalID session_id); 383 void InitializeConsumer(TracingSessionGlobalID session_id); 384 void OnConsumerDisconnected(ConsumerImpl* consumer); 385 void OnProducerDisconnected(ProducerImpl* producer); 386 387 struct FindDataSourceRes { 388 FindDataSourceRes() = default; FindDataSourceResFindDataSourceRes389 FindDataSourceRes(DataSourceStaticState* a, DataSourceState* b, uint32_t c) 390 : static_state(a), internal_state(b), instance_idx(c) {} 391 explicit operator bool() const { return !!internal_state; } 392 393 DataSourceStaticState* static_state = nullptr; 394 DataSourceState* internal_state = nullptr; 395 uint32_t instance_idx = 0; 396 }; 397 FindDataSourceRes FindDataSource(TracingBackendId, DataSourceInstanceID); 398 399 std::unique_ptr<base::TaskRunner> task_runner_; 400 std::vector<RegisteredDataSource> data_sources_; 401 std::vector<RegisteredBackend> backends_; 402 std::vector<RegisteredInterceptor> interceptors_; 403 TracingPolicy* policy_ = nullptr; 404 405 std::atomic<TracingSessionGlobalID> next_tracing_session_id_{}; 406 407 // Maximum number of times we will try to reconnect producer backend. 408 // Should only be modified for testing purposes. 409 std::atomic<uint32_t> max_producer_reconnections_{100u}; 410 411 PERFETTO_THREAD_CHECKER(thread_checker_) 412 }; 413 414 } // namespace internal 415 } // namespace perfetto 416 417 #endif // SRC_TRACING_INTERNAL_TRACING_MUXER_IMPL_H_ 418