• 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_DATA_SOURCE_H_
18 #define INCLUDE_PERFETTO_TRACING_DATA_SOURCE_H_
19 
20 // This header contains the key class (DataSource) that a producer app should
21 // override in order to create a custom data source that gets tracing Start/Stop
22 // notifications and emits tracing data.
23 
24 #include <assert.h>
25 #include <stddef.h>
26 #include <stdint.h>
27 
28 #include <array>
29 #include <atomic>
30 #include <functional>
31 #include <memory>
32 #include <mutex>
33 
34 #include "perfetto/base/build_config.h"
35 #include "perfetto/base/compiler.h"
36 #include "perfetto/base/export.h"
37 #include "perfetto/protozero/message.h"
38 #include "perfetto/protozero/message_handle.h"
39 #include "perfetto/tracing/buffer_exhausted_policy.h"
40 #include "perfetto/tracing/core/forward_decls.h"
41 #include "perfetto/tracing/internal/basic_types.h"
42 #include "perfetto/tracing/internal/data_source_internal.h"
43 #include "perfetto/tracing/internal/tracing_muxer.h"
44 #include "perfetto/tracing/locked_handle.h"
45 #include "perfetto/tracing/trace_writer_base.h"
46 
47 #include "protos/perfetto/trace/trace_packet.pbzero.h"
48 
49 // PERFETTO_COMPONENT_EXPORT is used to mark symbols in Perfetto's headers
50 // (typically templates) that are defined by the user outside of Perfetto and
51 // should be made visible outside the current module. (e.g., in Chrome's
52 // component build).
53 #if !defined(PERFETTO_COMPONENT_EXPORT)
54 #define PERFETTO_COMPONENT_EXPORT
55 #endif
56 
57 namespace perfetto {
58 namespace internal {
59 class TracingMuxerImpl;
60 class TrackEventCategoryRegistry;
61 template <typename, const internal::TrackEventCategoryRegistry*>
62 class TrackEventDataSource;
63 }  // namespace internal
64 
65 // Base class with the virtual methods to get start/stop notifications.
66 // Embedders are supposed to derive the templated version below, not this one.
67 class PERFETTO_EXPORT DataSourceBase {
68  public:
69   virtual ~DataSourceBase();
70 
71   // TODO(primiano): change the const& args below to be pointers instead. It
72   // makes it more awkward to handle output arguments and require mutable(s).
73   // This requires synchronizing a breaking API change for existing embedders.
74 
75   // OnSetup() is invoked when tracing is configured. In most cases this happens
76   // just before starting the trace. In the case of deferred start (see
77   // deferred_start in trace_config.proto) start might happen later.
78   class SetupArgs {
79    public:
80     // This is valid only within the scope of the OnSetup() call and must not
81     // be retained.
82     const DataSourceConfig* config = nullptr;
83 
84     // The index of this data source instance (0..kMaxDataSourceInstances - 1).
85     uint32_t internal_instance_index = 0;
86   };
87   virtual void OnSetup(const SetupArgs&);
88 
89   class StartArgs {
90    public:
91     // The index of this data source instance (0..kMaxDataSourceInstances - 1).
92     uint32_t internal_instance_index = 0;
93   };
94   virtual void OnStart(const StartArgs&);
95 
96   class StopArgs {
97    public:
98     virtual ~StopArgs();
99 
100     // HandleAsynchronously() can optionally be called to defer the tracing
101     // session stop and write tracing data just before stopping.
102     // This function returns a closure that must be invoked after the last
103     // trace events have been emitted. The returned closure can be called from
104     // any thread. The caller also needs to explicitly call TraceContext.Flush()
105     // from the last Trace() lambda invocation because no other implicit flushes
106     // will happen after the stop signal.
107     // When this function is called, the tracing service will defer the stop of
108     // the tracing session until the returned closure is invoked.
109     // However, the caller cannot hang onto this closure for too long. The
110     // tracing service will forcefully stop the tracing session without waiting
111     // for pending producers after TraceConfig.data_source_stop_timeout_ms
112     // (default: 5s, can be overridden by Consumers when starting a trace).
113     // If the closure is called after this timeout an error will be logged and
114     // the trace data emitted will not be present in the trace. No other
115     // functional side effects (e.g. crashes or corruptions) will happen. In
116     // other words, it is fine to accidentally hold onto this closure for too
117     // long but, if that happens, some tracing data will be lost.
118     virtual std::function<void()> HandleStopAsynchronously() const = 0;
119 
120     // The index of this data source instance (0..kMaxDataSourceInstances - 1).
121     uint32_t internal_instance_index = 0;
122   };
123   virtual void OnStop(const StopArgs&);
124 };
125 
126 struct DefaultDataSourceTraits {
127   // |IncrementalStateType| can optionally be used store custom per-sequence
128   // incremental data (e.g., interning tables). It should have a Clear() method
129   // for when incremental state needs to be cleared. See
130   // TraceContext::GetIncrementalState().
131   using IncrementalStateType = void;
132 
133   // Allows overriding what type of thread-local state configuration the data
134   // source uses. By default every data source gets independent thread-local
135   // state, which means every instance uses separate trace writers and
136   // incremental state even on the same thread. Some data sources (most notably
137   // the track event data source) want to share trace writers and incremental
138   // state on the same thread.
GetDataSourceTLSDefaultDataSourceTraits139   static internal::DataSourceThreadLocalState* GetDataSourceTLS(
140       internal::DataSourceStaticState* static_state,
141       internal::TracingTLS* root_tls) {
142     auto* ds_tls = &root_tls->data_sources_tls[static_state->index];
143     // The per-type TLS is either zero-initialized or must have been initialized
144     // for this specific data source type.
145     assert(!ds_tls->static_state ||
146            ds_tls->static_state->index == static_state->index);
147     return ds_tls;
148   }
149 };
150 
151 // Templated base class meant to be derived by embedders to create a custom data
152 // source. DataSourceType must be the type of the derived class itself, e.g.:
153 // class MyDataSource : public DataSourceBase<MyDataSource> {...}.
154 //
155 // |DataSourceTraits| allows customizing the behavior of the data source. See
156 // |DefaultDataSourceTraits|.
157 template <typename DataSourceType,
158           typename DataSourceTraits = DefaultDataSourceTraits>
159 class DataSource : public DataSourceBase {
160   struct DefaultTracePointTraits;
161 
162  public:
163   // The BufferExhaustedPolicy to use for TraceWriters of this DataSource.
164   // Override this in your DataSource class to change the default, which is to
165   // drop data on shared memory overruns.
166   constexpr static BufferExhaustedPolicy kBufferExhaustedPolicy =
167       BufferExhaustedPolicy::kDrop;
168 
169   // Argument passed to the lambda function passed to Trace() (below).
170   class TraceContext {
171    public:
172     using TracePacketHandle =
173         ::protozero::MessageHandle<::perfetto::protos::pbzero::TracePacket>;
174 
175     TraceContext(TraceContext&&) noexcept = default;
~TraceContext()176     ~TraceContext() {
177       // If the data source is being intercepted, flush the trace writer after
178       // each trace point to make sure the interceptor sees the data right away.
179       if (PERFETTO_UNLIKELY(tls_inst_->is_intercepted))
180         Flush();
181     }
182 
NewTracePacket()183     TracePacketHandle NewTracePacket() {
184       return tls_inst_->trace_writer->NewTracePacket();
185     }
186 
187     // Forces a commit of the thread-local tracing data written so far to the
188     // service. This is almost never required (tracing data is periodically
189     // committed as trace pages are filled up) and has a non-negligible
190     // performance hit (requires an IPC + refresh of the current thread-local
191     // chunk). The only case when this should be used is when handling OnStop()
192     // asynchronously, to ensure sure that the data is committed before the
193     // Stop timeout expires.
194     // The TracePacketHandle obtained by the last NewTracePacket() call must be
195     // finalized before calling Flush() (either implicitly by going out of scope
196     // or by explicitly calling Finalize()).
197     // |cb| is an optional callback. When non-null it will request the
198     // service to ACK the flush and will be invoked on an internal thread after
199     // the service has  acknowledged it. The callback might be NEVER INVOKED if
200     // the service crashes or the IPC connection is dropped.
201     void Flush(std::function<void()> cb = {}) {
202       tls_inst_->trace_writer->Flush(cb);
203     }
204 
205     // Returns the number of bytes written on the current thread by the current
206     // data-source since its creation.
207     // This can be useful for splitting protos that might grow very large.
written()208     uint64_t written() { return tls_inst_->trace_writer->written(); }
209 
210     // Returns a RAII handle to access the data source instance, guaranteeing
211     // that it won't be deleted on another thread (because of trace stopping)
212     // while accessing it from within the Trace() lambda.
213     // The returned handle can be invalid (nullptr) if tracing is stopped
214     // immediately before calling this. The caller is supposed to check for its
215     // validity before using it. After checking, the handle is guaranteed to
216     // remain valid until the handle goes out of scope.
GetDataSourceLocked()217     LockedHandle<DataSourceType> GetDataSourceLocked() {
218       auto* internal_state = static_state_.TryGet(instance_index_);
219       if (!internal_state)
220         return LockedHandle<DataSourceType>();
221       return LockedHandle<DataSourceType>(
222           &internal_state->lock,
223           static_cast<DataSourceType*>(internal_state->data_source.get()));
224     }
225 
GetIncrementalState()226     typename DataSourceTraits::IncrementalStateType* GetIncrementalState() {
227       // Recreate incremental state data if it has been reset by the service.
228       if (tls_inst_->incremental_state_generation !=
229           static_state_.incremental_state_generation.load(
230               std::memory_order_relaxed)) {
231         tls_inst_->incremental_state.reset();
232         CreateIncrementalState(tls_inst_);
233       }
234       return reinterpret_cast<typename DataSourceTraits::IncrementalStateType*>(
235           tls_inst_->incremental_state.get());
236     }
237 
238    private:
239     friend class DataSource;
240     template <typename, const internal::TrackEventCategoryRegistry*>
241     friend class internal::TrackEventDataSource;
TraceContext(internal::DataSourceInstanceThreadLocalState * tls_inst,uint32_t instance_index)242     TraceContext(internal::DataSourceInstanceThreadLocalState* tls_inst,
243                  uint32_t instance_index)
244         : tls_inst_(tls_inst), instance_index_(instance_index) {}
245     TraceContext(const TraceContext&) = delete;
246     TraceContext& operator=(const TraceContext&) = delete;
247 
248     internal::DataSourceInstanceThreadLocalState* const tls_inst_;
249     uint32_t const instance_index_;
250   };
251 
252   // The main tracing method. Tracing code should call this passing a lambda as
253   // argument, with the following signature: void(TraceContext).
254   // The lambda will be called synchronously (i.e., always before Trace()
255   // returns) only if tracing is enabled and the data source has been enabled in
256   // the tracing config.
257   // The lambda can be called more than once per Trace() call, in the case of
258   // concurrent tracing sessions (or even if the data source is instantiated
259   // twice within the same trace config).
260   template <typename Lambda>
Trace(Lambda tracing_fn)261   static void Trace(Lambda tracing_fn) {
262     CallIfEnabled<DefaultTracePointTraits>([&tracing_fn](uint32_t instances) {
263       TraceWithInstances<DefaultTracePointTraits>(instances,
264                                                   std::move(tracing_fn));
265     });
266   }
267 
268   // An efficient trace point guard for checking if this data source is active.
269   // |callback| is a function which will only be called if there are active
270   // instances. It is given an instance state parameter, which should be passed
271   // to TraceWithInstances() to actually record trace data.
272   template <typename Traits = DefaultTracePointTraits, typename Callback>
273   static void CallIfEnabled(Callback callback,
274                             typename Traits::TracePointData trace_point_data =
275                                 {}) PERFETTO_ALWAYS_INLINE {
276     // |instances| is a per-class bitmap that tells:
277     // 1. If the data source is enabled at all.
278     // 2. The index of the slot within |static_state_| that holds the instance
279     //    state. In turn this allows to map the data source to the tracing
280     //    session and buffers.
281     // memory_order_relaxed is okay because:
282     // - |instances| is re-read with an acquire barrier below if this succeeds.
283     // - The code between this point and the acquire-load is based on static
284     //    storage which has indefinite lifetime.
285     uint32_t instances = Traits::GetActiveInstances(trace_point_data)
286                              ->load(std::memory_order_relaxed);
287 
288     // This is the tracing fast-path. Bail out immediately if tracing is not
289     // enabled (or tracing is enabled but not for this data source).
290     if (PERFETTO_LIKELY(!instances))
291       return;
292     callback(instances);
293   }
294 
295   // The "lower half" of a trace point which actually performs tracing after
296   // this data source has been determined to be active.
297   // |instances| must be the instance state value retrieved through
298   // CallIfEnabled().
299   // |tracing_fn| will be called to record trace data as in Trace().
300   //
301   // |trace_point_data| is an optional parameter given to |Traits::
302   // GetActiveInstances| to make it possible to use custom storage for
303   // the data source enabled state. This is, for example, used by TrackEvent to
304   // implement per-tracing category enabled states.
305   //
306   // TODO(primiano): all the stuff below should be outlined from the trace
307   // point. Or at least we should have some compile-time traits like
308   // kOptimizeBinarySize / kOptimizeTracingLatency.
309   template <typename Traits = DefaultTracePointTraits, typename Lambda>
310   static void TraceWithInstances(
311       uint32_t instances,
312       Lambda tracing_fn,
313       typename Traits::TracePointData trace_point_data = {}) {
314     PERFETTO_DCHECK(instances);
315     constexpr auto kMaxDataSourceInstances = internal::kMaxDataSourceInstances;
316 
317     // See tracing_muxer.h for the structure of the TLS.
318     auto* tracing_impl = internal::TracingMuxer::Get();
319     if (PERFETTO_UNLIKELY(!tls_state_))
320       tls_state_ = GetOrCreateDataSourceTLS(&static_state_);
321 
322     // Avoid re-entering the trace point recursively.
323     if (PERFETTO_UNLIKELY(tls_state_->root_tls->is_in_trace_point))
324       return;
325     internal::ScopedReentrancyAnnotator scoped_annotator(*tls_state_->root_tls);
326 
327     // TracingTLS::generation is a global monotonic counter that is incremented
328     // every time a tracing session is stopped. We use that as a signal to force
329     // a slow-path garbage collection of all the trace writers for the current
330     // thread and to destroy the ones that belong to tracing sessions that have
331     // ended. This is to avoid having too many TraceWriter instances alive, each
332     // holding onto one chunk of the shared memory buffer.
333     // Rationale why memory_order_relaxed should be fine:
334     // - The TraceWriter object that we use is always constructed and destructed
335     //   on the current thread. There is no risk of accessing a half-initialized
336     //   TraceWriter (which would be really bad).
337     // - In the worst case, in the case of a race on the generation check, we
338     //   might end up using a TraceWriter for the same data source that belongs
339     //   to a stopped session. This is not really wrong, as we don't give any
340     //   guarantee on the global atomicity of the stop. In the worst case the
341     //   service will reject the data commit if this arrives too late.
342 
343     if (PERFETTO_UNLIKELY(
344             tls_state_->root_tls->generation !=
345             tracing_impl->generation(std::memory_order_relaxed))) {
346       // Will update root_tls->generation.
347       tracing_impl->DestroyStoppedTraceWritersForCurrentThread();
348     }
349 
350     for (uint32_t i = 0; i < kMaxDataSourceInstances; i++) {
351       internal::DataSourceState* instance_state =
352           static_state_.TryGetCached(instances, i);
353       if (!instance_state)
354         continue;
355 
356       // Even if we passed the check above, the DataSourceInstance might be
357       // still destroyed concurrently while this code runs. The code below is
358       // designed to deal with such race, as follows:
359       // - We don't access the user-defined data source instance state. The only
360       //   bits of state we use are |backend_id| and |buffer_id|.
361       // - Beyond those two integers, we access only the TraceWriter here. The
362       //   TraceWriter is always safe because it lives on the TLS.
363       // - |instance_state| is backed by static storage, so the pointer is
364       //   always valid, even after the data source instance is destroyed.
365       // - In the case of a race-on-destruction, we'll still see the latest
366       //   backend_id and buffer_id and in the worst case keep trying writing
367       //   into the tracing shared memory buffer after stopped. But this isn't
368       //   really any worse than the case of the stop IPC being delayed by the
369       //   kernel scheduler. The tracing service is robust against data commit
370       //   attemps made after tracing is stopped.
371       // There is a theoretical race that would case the wrong behavior w.r.t
372       // writing data in the wrong buffer, but it's so rare that we ignore it:
373       // if the data source is stopped and started kMaxDataSourceInstances
374       // times (so that the same id is recycled) while we are in this function,
375       // we might end up reusing the old data source's backend_id and buffer_id
376       // for the new one, because we don't see the generation change past this
377       // point. But stopping and starting tracing (even once) takes so much
378       // handshaking to make this extremely unrealistic.
379 
380       auto& tls_inst = tls_state_->per_instance[i];
381       if (PERFETTO_UNLIKELY(!tls_inst.trace_writer)) {
382         // Here we need an acquire barrier, which matches the release-store made
383         // by TracingMuxerImpl::SetupDataSource(), to ensure that the backend_id
384         // and buffer_id are consistent.
385         instances = Traits::GetActiveInstances(trace_point_data)
386                         ->load(std::memory_order_acquire);
387         instance_state = static_state_.TryGetCached(instances, i);
388         if (!instance_state || !instance_state->trace_lambda_enabled)
389           continue;
390         tls_inst.backend_id = instance_state->backend_id;
391         tls_inst.backend_connection_id = instance_state->backend_connection_id;
392         tls_inst.buffer_id = instance_state->buffer_id;
393         tls_inst.data_source_instance_id =
394             instance_state->data_source_instance_id;
395         tls_inst.is_intercepted = instance_state->interceptor_id != 0;
396         tls_inst.trace_writer = tracing_impl->CreateTraceWriter(
397             &static_state_, i, instance_state,
398             DataSourceType::kBufferExhaustedPolicy);
399         CreateIncrementalState(&tls_inst);
400 
401         // Even in the case of out-of-IDs, SharedMemoryArbiterImpl returns a
402         // NullTraceWriter. The returned pointer should never be null.
403         assert(tls_inst.trace_writer);
404       }
405 
406       tracing_fn(TraceContext(&tls_inst, i));
407     }
408   }
409 
410   // Registers the data source on all tracing backends, including ones that
411   // connect after the registration. Doing so enables the data source to receive
412   // Setup/Start/Stop notifications and makes the Trace() method work when
413   // tracing is enabled and the data source is selected.
414   // This must be called after Tracing::Initialize().
415   // Can return false to signal failure if attemping to register more than
416   // kMaxDataSources (32) data sources types or if tracing hasn't been
417   // initialized.
418   // The optional |constructor_args| will be passed to the data source when it
419   // is constructed.
420   template <class... Args>
Register(const DataSourceDescriptor & descriptor,const Args &...constructor_args)421   static bool Register(const DataSourceDescriptor& descriptor,
422                        const Args&... constructor_args) {
423     // Silences -Wunused-variable warning in case the trace method is not used
424     // by the translation unit that declares the data source.
425     (void)static_state_;
426     (void)tls_state_;
427 
428     auto factory = [constructor_args...]() {
429       return std::unique_ptr<DataSourceBase>(
430           new DataSourceType(constructor_args...));
431     };
432     auto* tracing_impl = internal::TracingMuxer::Get();
433     return tracing_impl->RegisterDataSource(descriptor, factory,
434                                             &static_state_);
435   }
436 
437  private:
438   // Traits for customizing the behavior of a specific trace point.
439   struct DefaultTracePointTraits {
440     // By default, every call to DataSource::Trace() will record trace events
441     // for every active instance of that data source. A single trace point can,
442     // however, use a custom set of enable flags for more fine grained control
443     // of when that trace point is active.
444     //
445     // DANGER: when doing this, the data source must use the appropriate memory
446     // fences when changing the state of the bitmap.
447     //
448     // |TraceWithInstances| may be optionally given an additional parameter for
449     // looking up the enable flags. That parameter is passed as |TracePointData|
450     // to |GetActiveInstances|. This is, for example, used by TrackEvent to
451     // implement per-category enabled states.
452     struct TracePointData {};
GetActiveInstancesDefaultTracePointTraits453     static constexpr std::atomic<uint32_t>* GetActiveInstances(TracePointData) {
454       return &static_state_.valid_instances;
455     }
456   };
457 
458   // Create the user provided incremental state in the given thread-local
459   // storage. Note: The second parameter here is used to specialize the case
460   // where there is no incremental state type.
461   template <typename T>
CreateIncrementalStateImpl(internal::DataSourceInstanceThreadLocalState * tls_inst,const T *)462   static void CreateIncrementalStateImpl(
463       internal::DataSourceInstanceThreadLocalState* tls_inst,
464       const T*) {
465     PERFETTO_DCHECK(!tls_inst->incremental_state);
466     tls_inst->incremental_state_generation =
467         static_state_.incremental_state_generation.load(
468             std::memory_order_relaxed);
469     tls_inst->incremental_state =
470         internal::DataSourceInstanceThreadLocalState::IncrementalStatePointer(
471             reinterpret_cast<void*>(new T()),
472             [](void* p) { delete reinterpret_cast<T*>(p); });
473   }
474 
CreateIncrementalStateImpl(internal::DataSourceInstanceThreadLocalState *,const void *)475   static void CreateIncrementalStateImpl(
476       internal::DataSourceInstanceThreadLocalState*,
477       const void*) {}
478 
CreateIncrementalState(internal::DataSourceInstanceThreadLocalState * tls_inst)479   static void CreateIncrementalState(
480       internal::DataSourceInstanceThreadLocalState* tls_inst) {
481     CreateIncrementalStateImpl(
482         tls_inst,
483         static_cast<typename DataSourceTraits::IncrementalStateType*>(nullptr));
484   }
485 
486   // Note that the returned object is one per-thread per-data-source-type, NOT
487   // per data-source *instance*.
GetOrCreateDataSourceTLS(internal::DataSourceStaticState * static_state)488   static internal::DataSourceThreadLocalState* GetOrCreateDataSourceTLS(
489       internal::DataSourceStaticState* static_state) {
490 #if PERFETTO_BUILDFLAG(PERFETTO_OS_IOS)
491     PERFETTO_FATAL("Data source TLS not supported on iOS, see b/158814068");
492 #endif
493     auto* tracing_impl = internal::TracingMuxer::Get();
494     internal::TracingTLS* root_tls = tracing_impl->GetOrCreateTracingTLS();
495     internal::DataSourceThreadLocalState* ds_tls =
496         DataSourceTraits::GetDataSourceTLS(static_state, root_tls);
497     // We keep re-initializing as the initialization is idempotent and not worth
498     // the code for extra checks.
499     ds_tls->static_state = static_state;
500     assert(!ds_tls->root_tls || ds_tls->root_tls == root_tls);
501     ds_tls->root_tls = root_tls;
502     return ds_tls;
503   }
504 
505   // Static state. Accessed by the static Trace() method fastpaths.
506   static internal::DataSourceStaticState static_state_;
507 
508   // This TLS object is a cached raw pointer and has deliberately no destructor.
509   // The Platform implementation is supposed to create and manage the lifetime
510   // of the Platform::ThreadLocalObject and take care of destroying it.
511   // This is because non-POD thread_local variables have subtleties (global
512   // destructors) that we need to defer to the embedder. In chromium's platform
513   // implementation, for instance, the tls slot is implemented using
514   // chromium's base::ThreadLocalStorage.
515   static PERFETTO_THREAD_LOCAL internal::DataSourceThreadLocalState* tls_state_;
516 };
517 
518 // static
519 template <typename T, typename D>
520 internal::DataSourceStaticState DataSource<T, D>::static_state_;
521 // static
522 template <typename T, typename D>
523 PERFETTO_THREAD_LOCAL internal::DataSourceThreadLocalState*
524     DataSource<T, D>::tls_state_;
525 
526 }  // namespace perfetto
527 
528 // If placed at the end of a macro declaration, eats the semicolon at the end of
529 // the macro invocation (e.g., "MACRO(...);") to avoid warnings about extra
530 // semicolons.
531 #define PERFETTO_INTERNAL_SWALLOW_SEMICOLON() \
532   extern int perfetto_internal_unused
533 
534 // This macro must be used once for each data source next to the data source's
535 // declaration.
536 #define PERFETTO_DECLARE_DATA_SOURCE_STATIC_MEMBERS(...)              \
537   template <>                                                         \
538   PERFETTO_COMPONENT_EXPORT perfetto::internal::DataSourceStaticState \
539       perfetto::DataSource<__VA_ARGS__>::static_state_;               \
540   template <>                                                         \
541   PERFETTO_COMPONENT_EXPORT PERFETTO_THREAD_LOCAL                     \
542       perfetto::internal::DataSourceThreadLocalState*                 \
543           perfetto::DataSource<__VA_ARGS__>::tls_state_
544 
545 // MSVC has a bug where explicit template member specialization declarations
546 // can't have thread_local as the storage class specifier. The generated code
547 // seems correct without the specifier, so drop it until the bug gets fixed.
548 // See https://developercommunity2.visualstudio.com/t/Unable-to-specialize-
549 // static-thread_local/1302689.
550 #if PERFETTO_BUILDFLAG(PERFETTO_COMPILER_MSVC)
551 #define PERFETTO_TEMPLATE_THREAD_LOCAL
552 #else
553 #define PERFETTO_TEMPLATE_THREAD_LOCAL PERFETTO_THREAD_LOCAL
554 #endif
555 
556 // This macro must be used once for each data source in one source file to
557 // allocate static storage for the data source's static state.
558 //
559 // Note: if MSVC fails with a C2086 (redefinition) error here, use the
560 // permissive- flag to enable standards-compliant mode. See
561 // https://developercommunity.visualstudio.com/content/problem/319447/
562 // explicit-specialization-of-static-data-member-inco.html.
563 #define PERFETTO_DEFINE_DATA_SOURCE_STATIC_MEMBERS(...)               \
564   template <>                                                         \
565   PERFETTO_COMPONENT_EXPORT perfetto::internal::DataSourceStaticState \
566       perfetto::DataSource<__VA_ARGS__>::static_state_{};             \
567   template <>                                                         \
568   PERFETTO_COMPONENT_EXPORT PERFETTO_TEMPLATE_THREAD_LOCAL            \
569       perfetto::internal::DataSourceThreadLocalState*                 \
570           perfetto::DataSource<__VA_ARGS__>::tls_state_ = nullptr
571 
572 #endif  // INCLUDE_PERFETTO_TRACING_DATA_SOURCE_H_
573