• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                                   bool no_flush,
72                                   DataSourceStaticState*) = 0;
73 
74   // Updates the DataSourceDescriptor for the DataSource.
75   virtual void UpdateDataSourceDescriptor(const DataSourceDescriptor&,
76                                           const DataSourceStaticState*) = 0;
77 
78   // It identifies the right backend and forwards the call to it.
79   // The returned TraceWriter must be used within the same sequence (for most
80   // projects this means "same thread"). Alternatively the client needs to take
81   // care of using synchronization primitives to prevent concurrent accesses.
82   virtual std::unique_ptr<TraceWriterBase> CreateTraceWriter(
83       DataSourceStaticState*,
84       uint32_t data_source_instance_index,
85       DataSourceState*,
86       BufferExhaustedPolicy buffer_exhausted_policy) = 0;
87 
88   virtual void DestroyStoppedTraceWritersForCurrentThread() = 0;
89 
generation(std::memory_order ord)90   uint32_t generation(std::memory_order ord) { return generation_.load(ord); }
91 
92   using InterceptorFactory = std::function<std::unique_ptr<InterceptorBase>()>;
93   virtual void RegisterInterceptor(const InterceptorDescriptor&,
94                                    InterceptorFactory,
95                                    InterceptorBase::TLSFactory,
96                                    InterceptorBase::TracePacketCallback) = 0;
97 
98   // Informs the tracing services to activate any of these triggers if any
99   // tracing session was waiting for them.
100   //
101   // Sends the trigger signal to all the initialized backends that are currently
102   // connected and that connect in the next `ttl_ms` milliseconds (but returns
103   // immediately anyway).
104   virtual void ActivateTriggers(const std::vector<std::string>&,
105                                 uint32_t ttl_ms) = 0;
106 
GetCurrentThreadId()107   base::PlatformThreadId GetCurrentThreadId() {
108     return platform_->GetCurrentThreadId();
109   }
110 
111  protected:
TracingMuxer(Platform * platform)112   explicit TracingMuxer(Platform* platform) : platform_(platform) {}
113 
114   static TracingMuxer* instance_;
115   Platform* const platform_ = nullptr;
116 
117   // Incremented every time a data source is destroyed. See tracing_tls.h.
118   std::atomic<uint32_t> generation_{};
119 };
120 
121 }  // namespace internal
122 }  // namespace perfetto
123 
124 #endif  // INCLUDE_PERFETTO_TRACING_INTERNAL_TRACING_MUXER_H_
125