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 INCLUDE_PERFETTO_TRACING_INTERNAL_TRACING_MUXER_H_ 18 #define INCLUDE_PERFETTO_TRACING_INTERNAL_TRACING_MUXER_H_ 19 20 #include <atomic> 21 #include <memory> 22 23 #include "perfetto/base/export.h" 24 #include "perfetto/tracing/buffer_exhausted_policy.h" 25 #include "perfetto/tracing/core/forward_decls.h" 26 #include "perfetto/tracing/interceptor.h" 27 #include "perfetto/tracing/internal/basic_types.h" 28 #include "perfetto/tracing/internal/tracing_tls.h" 29 #include "perfetto/tracing/platform.h" 30 31 namespace perfetto { 32 33 class DataSourceBase; 34 class TraceWriterBase; 35 struct TracingInitArgs; 36 class TracingSession; 37 38 namespace internal { 39 40 struct DataSourceParams { 41 bool supports_multiple_instances; 42 bool requires_callbacks_under_lock; 43 }; 44 45 struct DataSourceStaticState; 46 47 // This class acts as a bridge between the public API methods and the 48 // TracingBackend(s). It exposes a simplified view of the world to the API 49 // methods, so that they don't have to care about the multiplicity of backends. 50 // It handles all the bookkeeping to map data source instances and trace writers 51 // to the various backends. 52 // See tracing_muxer_impl.h for the full picture. This class contains only the 53 // fewer fields and methods that need to be exposed to public/ headers. Fields 54 // and methods that are required to implement them should go into 55 // src/tracing/internal/tracing_muxer_impl.h instead: that one can pull in 56 // perfetto headers outside of public, this one cannot. 57 class PERFETTO_EXPORT_COMPONENT TracingMuxer { 58 public: Get()59 static TracingMuxer* Get() { return instance_; } 60 61 virtual ~TracingMuxer(); 62 GetOrCreateTracingTLS()63 TracingTLS* GetOrCreateTracingTLS() { 64 return static_cast<TracingTLS*>(platform_->GetOrCreateThreadLocalObject()); 65 } 66 67 // This method can fail and return false if trying to register more than 68 // kMaxDataSources types. 69 using DataSourceFactory = std::function<std::unique_ptr<DataSourceBase>()>; 70 virtual bool RegisterDataSource(const DataSourceDescriptor&, 71 DataSourceFactory, 72 DataSourceParams, 73 bool no_flush, 74 DataSourceStaticState*) = 0; 75 76 // Updates the DataSourceDescriptor for the DataSource. 77 virtual void UpdateDataSourceDescriptor(const DataSourceDescriptor&, 78 const DataSourceStaticState*) = 0; 79 80 // It identifies the right backend and forwards the call to it. 81 // The returned TraceWriter must be used within the same sequence (for most 82 // projects this means "same thread"). Alternatively the client needs to take 83 // care of using synchronization primitives to prevent concurrent accesses. 84 virtual std::unique_ptr<TraceWriterBase> CreateTraceWriter( 85 DataSourceStaticState*, 86 uint32_t data_source_instance_index, 87 DataSourceState*, 88 BufferExhaustedPolicy buffer_exhausted_policy) = 0; 89 90 virtual void DestroyStoppedTraceWritersForCurrentThread() = 0; 91 generation(std::memory_order ord)92 uint32_t generation(std::memory_order ord) { return generation_.load(ord); } 93 94 using InterceptorFactory = std::function<std::unique_ptr<InterceptorBase>()>; 95 virtual void RegisterInterceptor(const InterceptorDescriptor&, 96 InterceptorFactory, 97 InterceptorBase::TLSFactory, 98 InterceptorBase::TracePacketCallback) = 0; 99 100 // Informs the tracing services to activate any of these triggers if any 101 // tracing session was waiting for them. 102 // 103 // Sends the trigger signal to all the initialized backends that are currently 104 // connected and that connect in the next `ttl_ms` milliseconds (but returns 105 // immediately anyway). 106 virtual void ActivateTriggers(const std::vector<std::string>&, 107 uint32_t ttl_ms) = 0; 108 GetCurrentThreadId()109 base::PlatformThreadId GetCurrentThreadId() { 110 return platform_->GetCurrentThreadId(); 111 } 112 113 protected: TracingMuxer(Platform * platform)114 explicit TracingMuxer(Platform* platform) : platform_(platform) {} 115 116 static TracingMuxer* instance_; 117 Platform* const platform_ = nullptr; 118 119 // Incremented every time a data source is destroyed. See tracing_tls.h. 120 std::atomic<uint32_t> generation_{}; 121 }; 122 123 } // namespace internal 124 } // namespace perfetto 125 126 #endif // INCLUDE_PERFETTO_TRACING_INTERNAL_TRACING_MUXER_H_ 127