All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
trace.h
Go to the documentation of this file.
1 //
2 // Copyright 2018 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 
25 #ifndef TRACE_H
26 #define TRACE_H
27 
29 
30 #include "pxr/pxr.h"
31 
32 #include "pxr/base/trace/api.h"
33 #include "pxr/base/trace/collector.h"
34 
35 #include "pxr/base/tf/preprocessorUtilsLite.h"
36 
37 #include <atomic>
38 
39 #if !defined(TRACE_DISABLE)
40 
43 #define TRACE_FUNCTION() \
44  _TRACE_FUNCTION_INSTANCE(__LINE__, __ARCH_FUNCTION__, __ARCH_PRETTY_FUNCTION__)
45 
48 #define TRACE_SCOPE(name) \
49  _TRACE_SCOPE_INSTANCE(__LINE__, name)
50 
53 #define TRACE_FUNCTION_SCOPE(name) \
54  _TRACE_FUNCTION_SCOPE_INSTANCE( \
55  __LINE__, __ARCH_FUNCTION__, __ARCH_PRETTY_FUNCTION__, name)
56 
58 #define TRACE_MARKER(name) \
59  _TRACE_MARKER_INSTANCE(__LINE__, name)
60 
62 #define TRACE_MARKER_DYNAMIC(name) \
63  _TRACE_MARKER_DYNAMIC_INSTANCE(__LINE__, name)
64 
70 #define TRACE_COUNTER_DELTA(name, delta) \
71  _TRACE_COUNTER_INSTANCE(__LINE__, name, delta, /* isDelta */ true)
72 
77 #define TRACE_COUNTER_DELTA_DYNAMIC(name, delta) \
78  TraceCollector::GetInstance().RecordCounterDelta(name, delta);
79 
83 #define TRACE_COUNTER_VALUE(name, value) \
84  _TRACE_COUNTER_INSTANCE(__LINE__, name, value, /* isDelta */ false)
85 
90 #define TRACE_COUNTER_VALUE_DYNAMIC(name, value) \
91  TraceCollector::GetInstance().RecordCounterValue(name, value);
92 
111 #define TRACE_COUNTER_DELTA_CODE(name, code) \
112  _TRACE_COUNTER_CODE_INSTANCE(__LINE__, name, code, true)
113 
119 #define TRACE_FUNCTION_DYNAMIC(name) \
120  _TRACE_FUNCTION_DYNAMIC_INSTANCE(__LINE__, __ARCH_FUNCTION__, __ARCH_PRETTY_FUNCTION__, name)
121 
127 #define TRACE_SCOPE_DYNAMIC(name) \
128  _TRACE_SCOPE_DYNAMIC_INSTANCE(__LINE__, name)
129 
130 
135 
136 
137 #define _TRACE_FUNCTION_INSTANCE(instance, name, prettyName) \
138 constexpr static PXR_NS::TraceStaticKeyData \
139  TF_PP_CAT(TraceKeyData_, instance)(name, prettyName); \
140 PXR_NS::TraceScopeAuto TF_PP_CAT(TraceScopeAuto_, instance)(\
141  TF_PP_CAT(TraceKeyData_, instance));
142 
143 #define _TRACE_SCOPE_INSTANCE(instance, name) \
144 constexpr static PXR_NS::TraceStaticKeyData \
145  TF_PP_CAT(TraceKeyData_, instance)(name); \
146 PXR_NS::TraceScopeAuto TF_PP_CAT(TraceScopeAuto_, instance)(\
147  TF_PP_CAT(TraceKeyData_, instance));
148 
149 #define _TRACE_FUNCTION_SCOPE_INSTANCE(instance, name, prettyName, scopeName) \
150 constexpr static PXR_NS::TraceStaticKeyData \
151  TF_PP_CAT(TraceKeyData_, instance)(name, prettyName, scopeName); \
152 PXR_NS::TraceScopeAuto TF_PP_CAT(TraceScopeAuto_, instance)(\
153  TF_PP_CAT(TraceKeyData_, instance));
154 
155 #define _TRACE_MARKER_INSTANCE(instance, name) \
156 constexpr static PXR_NS::TraceStaticKeyData \
157  TF_PP_CAT(TraceKeyData_, instance)(name); \
158  TraceCollector::GetInstance().MarkerEventStatic(TF_PP_CAT(TraceKeyData_, instance));
159 
160 #define _TRACE_COUNTER_INSTANCE(instance, name, value, isDelta) \
161 constexpr static PXR_NS::TraceStaticKeyData \
162  TF_PP_CAT(TraceKeyData_, instance)(name); \
163 static PXR_NS::TraceCounterHolder \
164  TF_PP_CAT(TraceCounterHolder_, instance) \
165  (TF_PP_CAT(TraceKeyData_, instance)); \
166 TF_PP_CAT(TraceCounterHolder_, instance).Record(value, isDelta);
167 
168 #define _TRACE_COUNTER_CODE_INSTANCE(instance, name, code, isDelta) \
169 static PXR_NS::TraceCounterHolder \
170  TF_PP_CAT(TraceCounterHolder_, instance)(name); \
171 if (TF_PP_CAT(TraceCounterHolder_, instance).IsEnabled()) { \
172  double value = 0.0; \
173  code \
174  TF_PP_CAT(TraceCounterHolder_, instance).RecordDelta(value, isDelta); \
175 }
176 
177 #define _TRACE_FUNCTION_DYNAMIC_INSTANCE(instance, fnName, fnPrettyName, name) \
178 PXR_NS::TraceAuto TF_PP_CAT(TraceAuto_, instance)(fnName, fnPrettyName, name)
179 
180 #define _TRACE_SCOPE_DYNAMIC_INSTANCE(instance, str) \
181 PXR_NS::TraceAuto TF_PP_CAT(TraceAuto_, instance)(str)
182 
183 #define _TRACE_MARKER_DYNAMIC_INSTANCE(instance, name) \
184  TraceCollector::GetInstance().MarkerEvent(name);
185 
186 #else // TRACE_DISABLE
187 
188 #define TRACE_FUNCTION()
189 #define TRACE_FUNCTION_DYNAMIC(name)
190 #define TRACE_SCOPE(name)
191 #define TRACE_SCOPE_DYNAMIC(name)
192 #define TRACE_FUNCTION_SCOPE(name)
193 #define TRACE_MARKER(name)
194 #define TRACE_MARKER_DYNAMIC(name)
195 
196 #endif // TRACE_DISABLE
197 
198 PXR_NAMESPACE_OPEN_SCOPE
199 
207 public:
210  explicit TraceScopeAuto(const TraceStaticKeyData& key)
211  : _key(nullptr)
212  , _start(0) {
213 
214  if (ARCH_UNLIKELY(TraceCollector::IsEnabled())) {
215  // Init the key if needed.
216  _key = &key;
217  _start = ArchGetTickTime();
218  }
219 
220  }
221 
224  template < typename... Args>
225  TraceScopeAuto(const TraceStaticKeyData& key, Args&&... args)
226  : TraceScopeAuto(key) {
227  if (ARCH_UNLIKELY(_key)) {
228  TraceCollector::GetInstance().ScopeArgs(std::forward<Args>(args)...);
229  }
230  }
231 
235  if (ARCH_UNLIKELY(_key)) {
236  TraceCollector::GetInstance().Scope(*_key, _start);
237  }
238  }
239 
240 private:
241  const TraceStaticKeyData* _key;
242  TraceEvent::TimeStamp _start;
243 };
244 
256 struct TraceAuto {
259  TraceAuto(const char *funcName, const char *prettyFuncName,
260  const std::string &name)
261  : _key(_CreateKeyString(funcName, prettyFuncName, name)) {
262  std::atomic_thread_fence(std::memory_order_seq_cst);
263  _collector = &TraceCollector::GetInstance();
264  _collector->BeginEvent(_key);
265  std::atomic_thread_fence(std::memory_order_seq_cst);
266  }
267 
270  explicit TraceAuto(const TfToken& key)
271  : _key(key) {
272  std::atomic_thread_fence(std::memory_order_seq_cst);
273  _collector = &TraceCollector::GetInstance();
274  _collector->BeginEvent(_key);
275  std::atomic_thread_fence(std::memory_order_seq_cst);
276  }
277 
280  explicit TraceAuto(const std::string& key)
281  : TraceAuto(TfToken(key)) {}
282 
283  // Non-copyable
284  //
285  TraceAuto(const TraceAuto &) = delete;
286  TraceAuto& operator=(const TraceAuto &) = delete;
287 
288  // Non-movable
289  //
290  TraceAuto(TraceAuto &&) = delete;
291  TraceAuto& operator=(TraceAuto &&) = delete;
292 
296  std::atomic_thread_fence(std::memory_order_seq_cst);
297  _collector->EndEvent(_key);
298  std::atomic_thread_fence(std::memory_order_seq_cst);
299  }
300 
301 private:
302  static std::string _CreateKeyString(
303  const char *funcName,
304  const char *prettyFuncName,
305  const std::string &name) {
306  std::string key = ArchGetPrettierFunctionName(funcName, prettyFuncName);
307  key += " [";
308  key += name;
309  key += "]";
310  return key;
311  }
312 
313  TraceCollector* _collector;
314  TraceDynamicKey _key;
315 };
316 
324 public:
327  explicit TraceCounterHolder(const TraceKey& key)
328  : _key(key) {}
329 
332  bool IsEnabled() const {
333  return TraceCollector::IsEnabled();
334  }
335 
338  void Record(double value, bool delta) {
339  if (delta) {
341  } else {
343  }
344  }
345 
346 private:
347  TraceKey _key;
348 };
349 
350 PXR_NAMESPACE_CLOSE_SCOPE
351 
352 #endif // TRACE_H
bool IsEnabled() const
Returns whether the TraceCollector is enabled or not.
Definition: trace.h:332
Holds on to a counter key, as well as the global collector for fast lookup.
Definition: trace.h:323
TraceScopeAuto(const TraceStaticKeyData &key, Args &&...args)
Constructor that also records scope arguments.
Definition: trace.h:225
TraceScopeAuto(const TraceStaticKeyData &key)
Constructor for TRACE_FUNCTION macro.
Definition: trace.h:210
TraceCounterHolder(const TraceKey &key)
Constructor used by TRACE_COUNTER_* macro.
Definition: trace.h:327
uint64_t ArchGetTickTime()
Return the current time in system-dependent units.
Definition: timing.h:64
TimeStamp EndEvent(const Key &key)
Record an end event with key if Category is enabled.
Definition: collector.h:162
TraceAuto(const char *funcName, const char *prettyFuncName, const std::string &name)
Constructor taking function name, pretty function name and a scope name.
Definition: trace.h:259
uint64_t TimeStamp
Time in &quot;ticks&quot;.
Definition: event.h:50
void RecordCounterValue(const TraceKey &key, double value)
Record a counter value for a name key if Category is enabled.
Definition: collector.h:363
A class which records a begin event when it is constructed, and a matching end event when it is destr...
Definition: trace.h:256
This class stores data used to create dynamic keys which can be referenced in TraceEvent instances...
Definition: dynamicKey.h:42
TimeStamp BeginEvent(const Key &key)
Record a begin event with key if Category is enabled.
Definition: collector.h:133
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
void ScopeArgs(Args &&...args)
Record multiple data events with category cat if Category is enabled.
Definition: collector.h:287
static TRACE_API TraceCollector & GetInstance()
Returns the singleton instance.
Definition: collector.h:84
A class which records a timestamp when it is created and a scope event when it is destructed...
Definition: trace.h:206
This is a singleton class that records TraceEvent instances and populates TraceCollection instances...
Definition: collector.h:71
void Scope(const TraceKey &key, TimeStamp start)
Record a scope event described by key that started at start if Category is enabled.
Definition: collector.h:274
~TraceScopeAuto()
Destructor.
Definition: trace.h:234
~TraceAuto()
Destructor.
Definition: trace.h:295
TraceAuto(const TfToken &key)
Constructor taking a TfToken key.
Definition: trace.h:270
static bool IsEnabled()
Returns whether collection of events is enabled for DefaultCategory.
Definition: collector.h:94
void Record(double value, bool delta)
Records a counter delta value if the TraceCollector is enabled.
Definition: trace.h:338
TraceAuto(const std::string &key)
Constructor taking a string key.
Definition: trace.h:280
void RecordCounterDelta(const TraceKey &key, double delta)
Record a counter delta for a name key if Category is enabled.
Definition: collector.h:342
ARCH_API std::string ArchGetPrettierFunctionName(const std::string &function, const std::string &prettyFunction)
Return well formatted function name.
This class holds data necessary to create keys for TraceEvent instances.
Definition: staticKeyData.h:42
A wrapper around a TraceStaticKeyData pointer that is stored in TraceEvent instances.
Definition: key.h:40