8#ifndef PXR_BASE_TRACE_TRACE_H
9#define PXR_BASE_TRACE_TRACE_H
15#include "pxr/base/trace/api.h"
16#include "pxr/base/trace/collector.h"
18#include "pxr/base/tf/preprocessorUtilsLite.h"
22#if !defined(TRACE_DISABLE)
26#define TRACE_FUNCTION() \
27 _TRACE_FUNCTION_INSTANCE(__LINE__, __ARCH_FUNCTION__, __ARCH_PRETTY_FUNCTION__)
31#define TRACE_SCOPE(name) \
32 _TRACE_SCOPE_INSTANCE(__LINE__, name)
36#define TRACE_FUNCTION_SCOPE(name) \
37 _TRACE_FUNCTION_SCOPE_INSTANCE( \
38 __LINE__, __ARCH_FUNCTION__, __ARCH_PRETTY_FUNCTION__, name)
41#define TRACE_MARKER(name) \
42 _TRACE_MARKER_INSTANCE(__LINE__, name)
45#define TRACE_MARKER_DYNAMIC(name) \
46 _TRACE_MARKER_DYNAMIC_INSTANCE(__LINE__, name)
53#define TRACE_COUNTER_DELTA(name, delta) \
54 _TRACE_COUNTER_INSTANCE(__LINE__, name, delta, true)
60#define TRACE_COUNTER_DELTA_DYNAMIC(name, delta) \
61 TraceCollector::GetInstance().RecordCounterDelta(name, delta);
66#define TRACE_COUNTER_VALUE(name, value) \
67 _TRACE_COUNTER_INSTANCE(__LINE__, name, value, false)
73#define TRACE_COUNTER_VALUE_DYNAMIC(name, value) \
74 TraceCollector::GetInstance().RecordCounterValue(name, value);
94#define TRACE_COUNTER_DELTA_CODE(name, code) \
95 _TRACE_COUNTER_CODE_INSTANCE(__LINE__, name, code, true)
102#define TRACE_FUNCTION_DYNAMIC(name) \
103 _TRACE_FUNCTION_DYNAMIC_INSTANCE(__LINE__, __ARCH_FUNCTION__, __ARCH_PRETTY_FUNCTION__, name)
110#define TRACE_SCOPE_DYNAMIC(name) \
111 _TRACE_SCOPE_DYNAMIC_INSTANCE(__LINE__, name)
120#define _TRACE_FUNCTION_INSTANCE(instance, name, prettyName) \
121constexpr static PXR_NS::TraceStaticKeyData \
122 TF_PP_CAT(TraceKeyData_, instance)(name, prettyName); \
123PXR_NS::TraceScopeAuto TF_PP_CAT(TraceScopeAuto_, instance)(\
124 TF_PP_CAT(TraceKeyData_, instance));
126#define _TRACE_SCOPE_INSTANCE(instance, name) \
127constexpr static PXR_NS::TraceStaticKeyData \
128 TF_PP_CAT(TraceKeyData_, instance)(name); \
129PXR_NS::TraceScopeAuto TF_PP_CAT(TraceScopeAuto_, instance)(\
130 TF_PP_CAT(TraceKeyData_, instance));
132#define _TRACE_FUNCTION_SCOPE_INSTANCE(instance, name, prettyName, scopeName) \
133constexpr static PXR_NS::TraceStaticKeyData \
134 TF_PP_CAT(TraceKeyData_, instance)(name, prettyName, scopeName); \
135PXR_NS::TraceScopeAuto TF_PP_CAT(TraceScopeAuto_, instance)(\
136 TF_PP_CAT(TraceKeyData_, instance));
138#define _TRACE_MARKER_INSTANCE(instance, name) \
139constexpr static PXR_NS::TraceStaticKeyData \
140 TF_PP_CAT(TraceKeyData_, instance)(name); \
141 TraceCollector::GetInstance().MarkerEventStatic(TF_PP_CAT(TraceKeyData_, instance));
143#define _TRACE_COUNTER_INSTANCE(instance, name, value, isDelta) \
144constexpr static PXR_NS::TraceStaticKeyData \
145 TF_PP_CAT(TraceKeyData_, instance)(name); \
146static PXR_NS::TraceCounterHolder \
147 TF_PP_CAT(TraceCounterHolder_, instance) \
148 (TF_PP_CAT(TraceKeyData_, instance)); \
149TF_PP_CAT(TraceCounterHolder_, instance).Record(value, isDelta);
151#define _TRACE_COUNTER_CODE_INSTANCE(instance, name, code, isDelta) \
152static PXR_NS::TraceCounterHolder \
153 TF_PP_CAT(TraceCounterHolder_, instance)(name); \
154if (TF_PP_CAT(TraceCounterHolder_, instance).IsEnabled()) { \
155 double value = 0.0; \
157 TF_PP_CAT(TraceCounterHolder_, instance).RecordDelta(value, isDelta); \
160#define _TRACE_FUNCTION_DYNAMIC_INSTANCE(instance, fnName, fnPrettyName, name) \
161PXR_NS::TraceAuto TF_PP_CAT(TraceAuto_, instance)(fnName, fnPrettyName, name)
163#define _TRACE_SCOPE_DYNAMIC_INSTANCE(instance, str) \
164PXR_NS::TraceAuto TF_PP_CAT(TraceAuto_, instance)(str)
166#define _TRACE_MARKER_DYNAMIC_INSTANCE(instance, name) \
167 TraceCollector::GetInstance().MarkerEvent(name);
171#define TRACE_FUNCTION()
172#define TRACE_FUNCTION_DYNAMIC(name)
173#define TRACE_SCOPE(name)
174#define TRACE_SCOPE_DYNAMIC(name)
175#define TRACE_FUNCTION_SCOPE(name)
176#define TRACE_MARKER(name)
177#define TRACE_MARKER_DYNAMIC(name)
181PXR_NAMESPACE_OPEN_SCOPE
200 template <
typename... Args>
203 , _intervalTimer(false) {
205 _intervalTimer.
Start();
207 ::GetInstance().ScopeArgs(std::forward<Args>(args)...);
215 TraceCollector::TimeStamp stopTicks =
241 TraceAuto(
const char *funcName,
const char *prettyFuncName,
242 const std::string &name)
243 : _key(_CreateKeyString(funcName, prettyFuncName, name)) {
244 std::atomic_thread_fence(std::memory_order_seq_cst);
247 std::atomic_thread_fence(std::memory_order_seq_cst);
254 std::atomic_thread_fence(std::memory_order_seq_cst);
257 std::atomic_thread_fence(std::memory_order_seq_cst);
278 std::atomic_thread_fence(std::memory_order_seq_cst);
280 std::atomic_thread_fence(std::memory_order_seq_cst);
284 static std::string _CreateKeyString(
285 const char *funcName,
286 const char *prettyFuncName,
287 const std::string &name) {
332PXR_NAMESPACE_CLOSE_SCOPE
Token for efficient comparison, assignment, and hashing of known strings.
This is a singleton class that records TraceEvent instances and populates TraceCollection instances.
void RecordCounterDelta(const TraceKey &key, double delta)
Record a counter delta for a name key if Category is enabled.
TimeStamp EndEvent(const Key &key)
Record an end event with key if Category is enabled.
static TRACE_API void Scope(const TraceKey &key, TimeStamp start, TimeStamp stop) noexcept
Record a scope event described by key that started at start for the DefaultCategory.
static bool IsEnabled()
Returns whether collection of events is enabled for DefaultCategory.
void RecordCounterValue(const TraceKey &key, double value)
Record a counter value for a name key if Category is enabled.
static TRACE_API TraceCollector & GetInstance()
Returns the singleton instance.
TimeStamp BeginEvent(const Key &key)
Record a begin event with key if Category is enabled.
Holds on to a counter key, as well as the global collector for fast lookup.
bool IsEnabled() const
Returns whether the TraceCollector is enabled or not.
TraceCounterHolder(const TraceKey &key)
Constructor used by TRACE_COUNTER_* macro.
void Record(double value, bool delta)
Records a counter delta value if the TraceCollector is enabled.
This class stores data used to create dynamic keys which can be referenced in TraceEvent instances.
A wrapper around a TraceStaticKeyData pointer that is stored in TraceEvent instances.
A class which records a timestamp when it is created and a scope event when it is destructed.
TraceScopeAuto(const TraceStaticKeyData &key) noexcept
Constructor for TRACE_FUNCTION macro.
~TraceScopeAuto() noexcept
Destructor.
TraceScopeAuto(const TraceStaticKeyData &key, Args &&... args)
Constructor that also records scope arguments.
This class holds data necessary to create keys for TraceEvent instances.
ARCH_API std::string ArchGetPrettierFunctionName(const std::string &function, const std::string &prettyFunction)
Return well formatted function name.
A simple timer class for measuring an interval of time using the ArchTickTimer facilities.
uint64_t GetCurrentTicks()
Read and return the current time.
void Start()
Start the timer, or reset the start time if it has already been started.
uint64_t GetStartTicks() const
Return this timer's start time, or 0 if it hasn't been started.
bool IsStarted() const
Return true if this timer is started.
A class which records a begin event when it is constructed, and a matching end event when it is destr...
TraceAuto(const TfToken &key)
Constructor taking a TfToken key.
TraceAuto(const char *funcName, const char *prettyFuncName, const std::string &name)
Constructor taking function name, pretty function name and a scope name.
TraceAuto(const std::string &key)
Constructor taking a string key.