8 #ifndef PXR_BASE_TRACE_COLLECTOR_H
9 #define PXR_BASE_TRACE_COLLECTOR_H
21 #include "pxr/base/tf/mallocTag.h"
37 #include <tbb/spin_mutex.h>
41 class TraceScopeHolder;
78 return (_isEnabled.load(std::memory_order_acquire) == 1);
90 #ifdef PXR_PYTHON_SUPPORT_ENABLED
92 bool IsPythonTracingEnabled()
const {
93 return _isPythonTracingEnabled.load(std::memory_order_acquire) != 0;
98 #endif // PXR_PYTHON_SUPPORT_ENABLED
118 template <
typename Category = DefaultCategory>
123 return _BeginEvent(key, Category::GetId());
131 template <
typename Category = DefaultCategory>
136 _BeginEventAtTime(key, ms, Category::GetId());
147 template <
typename Category = DefaultCategory>
152 return _EndEvent(key, Category::GetId());
160 template <
typename Category = DefaultCategory>
165 _EndEventAtTime(key, ms, Category::GetId());
173 template <
typename Category = DefaultCategory>
178 return _MarkerEvent(key, Category::GetId());
186 template <
typename Category = DefaultCategory>
191 _MarkerEventAtTime(key, ms, Category::GetId());
199 template <
typename Category = DefaultCategory>
204 _BeginScope(_key, Category::GetId());
212 template <
typename Category,
typename... Args>
215 static_assert(
sizeof...(Args) %2 == 0,
216 "Data arguments must come in pairs");
221 _PerThreadData *threadData = _GetThreadData();
222 threadData->BeginScope(key, Category::GetId());
223 _StoreDataRec(threadData, Category::GetId(), std::forward<Args>(
args)...);
230 template <
typename... Args>
232 static_assert(
sizeof...(Args) %2 == 0,
233 "Data arguments must come in pairs");
237 BeginScope<DefaultCategory>(key,
238 std::forward<Args>(
args)...);
245 template <
typename Category = DefaultCategory>
250 _EndScope(key, Category::GetId());
269 template <
typename Category = DefaultCategory>
273 _PerThreadData *threadData = _GetThreadData();
274 threadData->EmplaceEvent(
281 template <
typename Category,
typename... Args>
283 static_assert(
sizeof...(Args) %2 == 0,
284 "Data arguments must come in pairs");
289 _PerThreadData *threadData = _GetThreadData();
290 _StoreDataRec(threadData, Category::GetId(), std::forward<Args>(
args)...);
299 template <
typename... Args>
301 static_assert(
sizeof...(Args) %2 == 0,
302 "Data arguments must come in pairs");
304 ScopeArgs<DefaultCategory>(std::forward<Args>(
args)...);
314 template <
typename Category = DefaultCategory>
319 _PerThreadData *threadData = _GetThreadData();
320 threadData->EmplaceEvent(
328 template <
typename Category = DefaultCategory,
typename T>
331 _StoreData(_GetThreadData(), key, Category::GetId(), value);
336 template <
typename Category = DefaultCategory>
341 _PerThreadData *threadData = _GetThreadData();
342 threadData->EmplaceEvent(
348 template <
typename Category = DefaultCategory>
351 _PerThreadData *threadData = _GetThreadData();
352 threadData->CounterDelta(key, delta, Category::GetId());
357 template <
typename Category = DefaultCategory>
361 _PerThreadData *threadData = _GetThreadData();
362 threadData->EmplaceEvent(
369 template <
typename Category = DefaultCategory>
373 _PerThreadData *threadData = _GetThreadData();
374 threadData->CounterValue(key, value, Category::GetId());
397 class _PerThreadData;
401 TRACE_API _PerThreadData* _GetThreadData() noexcept;
423 _PerThreadData *threadData = _GetThreadData();
424 threadData->BeginScope(key, cat);
433 #ifdef PXR_PYTHON_SUPPORT_ENABLED
436 #endif // PXR_PYTHON_SUPPORT_ENABLED
439 template <
typename T,
440 typename std::enable_if<
443 void _StoreData(_PerThreadData* threadData,
const TraceKey &key,
445 threadData->StoreData(key, value, cat);
449 template <
typename T,
450 typename std::enable_if<
453 void _StoreData(_PerThreadData* threadData,
const TraceKey &key,
455 threadData->StoreLargeData(key, value, cat);
460 _PerThreadData* threadData,
464 threadData->StoreLargeData(key, value, cat);
469 _PerThreadData* threadData,
472 const std::string& value) {
473 threadData->StoreLargeData(key, value.c_str(), cat);
477 template <
typename K,
typename T,
typename... Args>
480 const T& value, Args&&...
args) {
481 _StoreData(threadData, std::forward<K>(key), cat, value);
482 _StoreDataRec(threadData, cat, std::forward<Args>(
args)...);
491 class _PerThreadData {
512 AtomicRef lock(_writing);
513 _BeginScope(key, cat);
517 AtomicRef lock(_writing);
527 template <
typename T>
530 AtomicRef lock(_writing);
531 _events.load(std::memory_order_acquire)->EmplaceBack(
535 template <
typename T>
538 AtomicRef lock(_writing);
539 EventList* events = _events.load(std::memory_order_acquire);
540 const auto* cached = events->StoreData(data);
544 template <
typename... Args>
545 void EmplaceEvent(Args&&...
args) {
546 AtomicRef lock(_writing);
547 _events.load(std::memory_order_acquire)->EmplaceBack(
548 std::forward<Args>(
args)...);
551 #ifdef PXR_PYTHON_SUPPORT_ENABLED
552 void PushPyScope(
const Key& key,
bool enabled);
554 #endif // PXR_PYTHON_SUPPORT_ENABLED
558 std::unique_ptr<EventList> GetCollectionData();
563 _events.load(std::memory_order_acquire)->EmplaceBack(
570 mutable std::atomic<bool> _writing;
571 std::atomic<EventList*> _events;
575 AtomicRef(std::atomic<bool>&
b) : _bool(b) {
576 _bool.store(
true, std::memory_order_release);
579 _bool.store(
false, std::memory_order_release);
582 std::atomic<bool>& _bool;
595 std::vector<PyScope> _pyScopes;
598 TRACE_API static std::atomic<int> _isEnabled;
610 #ifndef PXR_PYTHON_SUPPORT_ENABLED
614 std::atomic<int> _isPythonTracingEnabled;
616 #ifndef PXR_PYTHON_SUPPORT_ENABLED
625 #endif // PXR_BASE_TRACE_COLLECTOR_H
static TRACE_API TraceCollector & GetInstance()
Returns the singleton instance.
TRACE_API ~TraceCollector()
void RecordCounterValue(const TraceKey &key, double value)
Record a counter value for a name key if Category is enabled.
void RecordCounterValue(const Key &key, double value)
#define ARCH_PRAGMA_UNUSED_PRIVATE_FIELD
GLsizei const GLfloat * value
TF_MALLOC_TAG_NEW("Trace","TraceCollector")
TRACE_API void SetEnabled(bool isEnabled)
Enables or disables collection of events for DefaultCategory.
unsigned int GetThreadId()
GLenum GLenum GLsizei const GLuint GLboolean enabled
void EndEventAtTime(const Key &key, double ms)
TimeStamp MarkerEvent(const Key &key)
void ScopeArgs(Args &&...args)
void Scope(const TraceKey &key, TimeStamp start, TimeStamp stop)
TraceCollectorPtr ThisPtr
void MarkerEventStatic(const TraceKey &key)
void MarkerEventAtTime(const Key &key, double ms)
std::shared_ptr< TfPyTraceFn > TfPyTraceFnId
TRACE_API TimeStamp GetScopeOverhead() const
Return the overhead cost to measure a scope.
GLint GLint GLsizei GLint GLenum GLenum type
static bool IsEnabled()
Returns whether collection of events is enabled for DefaultCategory.
void BeginScope(const TraceKey &key, Args &&...args)
void EndScope(const TraceKey &key)
void ScopeArgs(Args &&...args)
void RecordCounterDelta(const Key &key, double delta)
Record a counter delta for a name key if Category is enabled.
GLboolean GLboolean GLboolean b
void BeginScope(const TraceKey &key, Args &&...args)
TimeStamp EndEvent(const Key &key)
TRACE_API void CreateCollection()
static bool IsEnabled()
Returns the result of TraceCollector::IsEnabled.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
void BeginScope(const TraceKey &_key)
#define PXR_NAMESPACE_CLOSE_SCOPE
void StoreData(const TraceKey &key, const T &value)
TimeStamp BeginEvent(const Key &key)
static TRACE_API void Scope(const TraceKey &key, TimeStamp start, TimeStamp stop) noexcept
TF_DECLARE_WEAK_AND_REF_PTRS(TraceScope)
uint64_t TimeStamp
Time in "ticks".
**If you just want to fire and args
TRACE_API_TEMPLATE_CLASS(TfSingleton< TraceCollector >)
static constexpr TraceCategoryId GetId()
Returns TraceCategory::Default.
TF_DECLARE_WEAK_PTRS(TraceCollector)
void BeginEventAtTime(const Key &key, double ms)
uint32_t TraceCategoryId
Categories that a TraceReporter can use to filter events.
TraceEvent::TimeStamp TimeStamp
void RecordCounterDelta(const TraceKey &key, double delta)
Record a counter delta for a name key if Category is enabled.
const std::string & GetLabel()
Return the label associated with this collector.