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