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 DataSourceStaticState; 39 40 // This class acts as a bridge between the public API methods and the 41 // TracingBackend(s). It exposes a simplified view of the world to the API 42 // methods, so that they don't have to care about the multiplicity of backends. 43 // It handles all the bookkeeping to map data source instances and trace writers 44 // to the various backends. 45 // See tracing_muxer_impl.h for the full picture. This class contains only the 46 // fewer fields and methods that need to be exposed to public/ headers. Fields 47 // and methods that are required to implement them should go into 48 // src/tracing/internal/tracing_muxer_impl.h instead: that one can pull in 49 // perfetto headers outside of public, this one cannot. 50 class PERFETTO_EXPORT TracingMuxer { 51 public: Get()52 static TracingMuxer* Get() { return instance_; } 53 54 virtual ~TracingMuxer(); 55 GetOrCreateTracingTLS()56 TracingTLS* GetOrCreateTracingTLS() { 57 return static_cast<TracingTLS*>(platform_->GetOrCreateThreadLocalObject()); 58 } 59 60 // This method can fail and return false if trying to register more than 61 // kMaxDataSources types. 62 using DataSourceFactory = std::function<std::unique_ptr<DataSourceBase>()>; 63 virtual bool RegisterDataSource(const DataSourceDescriptor&, 64 DataSourceFactory, 65 DataSourceStaticState*) = 0; 66 67 // It identifies the right backend and forwards the call to it. 68 // The returned TraceWriter must be used within the same sequence (for most 69 // projects this means "same thread"). Alternatively the client needs to take 70 // care of using synchronization primitives to prevent concurrent accesses. 71 virtual std::unique_ptr<TraceWriterBase> CreateTraceWriter( 72 DataSourceStaticState*, 73 uint32_t data_source_instance_index, 74 DataSourceState*, 75 BufferExhaustedPolicy buffer_exhausted_policy) = 0; 76 77 virtual void DestroyStoppedTraceWritersForCurrentThread() = 0; 78 generation(std::memory_order ord)79 uint32_t generation(std::memory_order ord) { return generation_.load(ord); } 80 81 using InterceptorFactory = std::function<std::unique_ptr<InterceptorBase>()>; 82 virtual void RegisterInterceptor(const InterceptorDescriptor&, 83 InterceptorFactory, 84 InterceptorBase::TLSFactory, 85 InterceptorBase::TracePacketCallback) = 0; 86 87 protected: TracingMuxer(Platform * platform)88 explicit TracingMuxer(Platform* platform) : platform_(platform) {} 89 90 static TracingMuxer* instance_; 91 Platform* const platform_ = nullptr; 92 93 // Incremented every time a data source is destroyed. See tracing_tls.h. 94 std::atomic<uint32_t> generation_{}; 95 }; 96 97 } // namespace internal 98 } // namespace perfetto 99 100 #endif // INCLUDE_PERFETTO_TRACING_INTERNAL_TRACING_MUXER_H_ 101