HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
event.h
Go to the documentation of this file.
1 //
2 // Copyright 2018 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 
8 #ifndef PXR_BASE_TRACE_EVENT_H
9 #define PXR_BASE_TRACE_EVENT_H
10 
11 #include "pxr/pxr.h"
12 
13 #include "pxr/base/trace/api.h"
15 #include "pxr/base/trace/key.h"
16 
17 #include "pxr/base/arch/timing.h"
18 
19 #include <utility>
20 
22 
23 class TraceEventData;
24 
25 ////////////////////////////////////////////////////////////////////////////////
26 /// \class TraceEvent
27 ///
28 /// This represents an event recorded by a TraceCollector.
29 /// It contains a key (name), categoryId, timestamp, type, and optional
30 /// metadata.
31 ///
32 class TraceEvent {
33 public:
34  /// Time in "ticks".
35  using TimeStamp = uint64_t;
36  using Key = TraceKey;
37 
38  /// \name Tag enums to select constructors
39  /// @{
40  enum BeginTag { Begin };
41  enum EndTag { End };
43  enum MarkerTag { Marker };
46  enum DataTag { Data };
47  /// @}
48 
49  /// Valid event types
50  enum class EventType : uint8_t {
51  Unknown, ///< The event is an unknown type.
52  Begin, ///< The event represents the beginning timestamp of a scope.
53  End, ///< The event represents the ending timestamp of a scope.
54  Timespan, ///< The event represents begin and end timestamp of a scope.
55  Marker, ///< The event represents an marker without a duration.
56  CounterDelta, ///< The event represents a change in a counter.
57  CounterValue, ///< The event represents the value of a counter.
58  ScopeData,
59  ///< The event stores data that is associated with its enclosing scope.
60  };
61 
62  /// The different types of data that can be stored in a TraceEvent instance.
63  enum class DataType : uint8_t {
64  String, ///< The event is storing a string.
65  Boolean, ///< The event is storing a bool.
66  Int, ///< The event is storing an integer.
67  UInt, ///< The event is storing an unsigned integer.
68  Float, ///< The event is storing an double.
69  Invalid ///< The event is not storing any data.
70  };
71 
72  /// Return this event's key.
73  const Key& GetKey() const { return _key; }
74 
75  /// Return the time stamp associated with this event.
77 
78  /// Return the counter value associated with this event.
79  TRACE_API double GetCounterValue() const;
80 
81  /// Returns the event's category id.
82  TraceCategoryId GetCategory() const { return _category; }
83 
84  /// Returns the start time of a timespan event.
86 
87  /// Returns the end time of a timespan event.
89 
90  /// Return both the start and end time of a timespan event by
91  /// GetStartTimeStamp() and GetEndTimeStamp().
92  TRACE_API std::pair<TimeStamp, TimeStamp> GetTimeSpanStamps() const;
93 
94  /// Returns the data stored in a data event.
96 
97  /// Returns the type of the event.
98  TRACE_API EventType GetType() const;
99 
100  /// \name Constructors
101  /// @{
102 
103  /// Constructor for Begin events that will automatically set the
104  /// timestamp from the current time.
106  _key(key),
107  _category(cat),
108  _type(_InternalEventType::Begin),
109  _time(ArchGetTickTime()) {
110  }
111 
112  /// Constructor for Begin events that takes a specific TimeStamp \a ts.
114  const Key& key,
115  TimeStamp ts,
116  TraceCategoryId cat) :
117  _key(key),
118  _category(cat),
119  _type(_InternalEventType::Begin),
120  _time(ts) {
121  }
122 
123  /// Constructor for End events that will automatically set the
124  /// timestamp from the current time.
125  TraceEvent(EndTag, const Key& key, TraceCategoryId cat) :
126  _key(key),
127  _category(cat),
128  _type(_InternalEventType::End),
129  _time(ArchGetTickTime()) {
130  }
131 
132  /// Constructor for End events that takes a specific TimeStamp \a ts.
134  const Key& key,
135  TimeStamp ts,
136  TraceCategoryId cat) :
137  _key(key),
138  _category(cat),
139  _type(_InternalEventType::End),
140  _time(ts) {
141  }
142 
143  /// Constructor for Timespan events that takes the start time and end time.
145  TimespanTag, const Key& key,
146  TimeStamp startTime, TimeStamp endTime,
147  TraceCategoryId cat) :
148  _key(key),
149  _category(cat),
150  _type(_InternalEventType::Timespan),
151  _time(endTime) {
152  new (&_payload) TimeStamp(startTime);
153  }
154 
155  /// Constructor for Marker events that will automatically set the
156  /// timestamp from the current time.
158  _key(key),
159  _category(cat),
160  _type(_InternalEventType::Marker),
161  _time(ArchGetTickTime()) {
162  }
163 
164  /// Constructor for Mark events that takes a specific TimeStamp \a ts.
166  const Key& key,
167  TimeStamp ts,
168  TraceCategoryId cat) :
169  _key(key),
170  _category(cat),
171  _type(_InternalEventType::Marker),
172  _time(ts) {
173  }
174 
175  /// Constructor for Counter delta events.
177  const Key& key,
178  double value,
179  TraceCategoryId cat) :
180  _key(key),
181  _category(cat),
182  _type(_InternalEventType::CounterDelta),
183  _time(ArchGetTickTime()) {
184  new (&_payload) double(value);
185  }
186 
187  /// Constructor for Counter value events.
189  const Key& key,
190  double value,
191  TraceCategoryId cat) :
192  _key(key),
193  _category(cat),
194  _type(_InternalEventType::CounterValue),
195  _time(ArchGetTickTime()) {
196  new (&_payload) double(value);
197  }
198 
199  /// \name Constructors for data events
200  /// @{
201  TraceEvent(DataTag, const Key& key, bool data, TraceCategoryId cat) :
202  _key(key),
203  _category(cat),
204  _dataType(DataType::Boolean),
205  _type(_InternalEventType::ScopeData),
206  _time(ArchGetTickTime()) {
207  new (&_payload) bool(data);
208  }
209 
210  TraceEvent(DataTag, const Key& key, int data, TraceCategoryId cat) :
211  _key(key),
212  _category(cat),
213  _dataType(DataType::Int),
214  _type(_InternalEventType::ScopeData),
215  _time(ArchGetTickTime()) {
216  new (&_payload) int64_t(data);
217  }
218 
219  TraceEvent(DataTag, const Key& key, int64_t data, TraceCategoryId cat) :
220  _key(key),
221  _category(cat),
222  _dataType(DataType::Int),
223  _type(_InternalEventType::ScopeData),
224  _time(ArchGetTickTime()) {
225  new (&_payload) int64_t(data);
226  }
227 
228  TraceEvent(DataTag, const Key& key, uint64_t data, TraceCategoryId cat) :
229  _key(key),
230  _category(cat),
231  _dataType(DataType::UInt),
232  _type(_InternalEventType::ScopeData),
233  _time(ArchGetTickTime()) {
234  new (&_payload) uint64_t(data);
235  }
236 
237  TraceEvent(DataTag, const Key& key, double data, TraceCategoryId cat) :
238  _key(key),
239  _category(cat),
240  _dataType(DataType::Float),
241  _type(_InternalEventType::ScopeData),
242  _time(ArchGetTickTime()) {
243  new (&_payload) double(data);
244  }
245 
246  TraceEvent(DataTag, const Key& key, const char* data, TraceCategoryId cat) :
247  _key(key),
248  _category(cat),
249  _dataType(DataType::String),
250  _type(_InternalEventType::ScopeDataLarge),
251  _time(ArchGetTickTime()) {
252  new (&_payload) const char*(data);
253  }
254  /// @}
255 
256  // Can move this, but not copy it
257  TraceEvent(const TraceEvent&) = delete;
258  TraceEvent& operator= (const TraceEvent&) = delete;
259 
260  TraceEvent(TraceEvent&&) = default;
261  TraceEvent& operator= (TraceEvent&&) = default;
262 
263  /// @}
264 
265  /// Sets the events timestamp to \p time.
266  void SetTimeStamp(TimeStamp time) { _time = time; }
267 private:
268  // Valid event types. This type has more detail that the public facing
269  // EventType enum.
270  enum class _InternalEventType : uint8_t {
271  Begin,
272  End,
273  Timespan,
274  Marker,
275  CounterDelta,
276  CounterValue,
277  ScopeData,
279  };
280 
281  using PayloadStorage = std::aligned_storage<8, 8>::type;
282 
283  Key _key;
284  TraceCategoryId _category;
285  DataType _dataType;
286  _InternalEventType _type;
287  TimeStamp _time;
288  PayloadStorage _payload;
289 };
290 
292 
293 #endif // PXR_BASE_TRACE_EVENT_H
The event represents an marker without a duration.
type
Definition: core.h:556
TraceEvent(EndTag, const Key &key, TimeStamp ts, TraceCategoryId cat)
Constructor for End events that takes a specific TimeStamp ts.
Definition: event.h:133
TraceEvent(DataTag, const Key &key, int64_t data, TraceCategoryId cat)
Definition: event.h:219
GT_API const UT_StringHolder time
The event is storing an double.
TraceEvent(MarkerTag, const Key &key, TraceCategoryId cat)
Definition: event.h:157
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLfloat * value
Definition: glcorearb.h:824
uint64_t ArchGetTickTime()
Definition: timing.h:67
TraceEvent(DataTag, const Key &key, bool data, TraceCategoryId cat)
Definition: event.h:201
TRACE_API TraceEventData GetData() const
Returns the data stored in a data event.
CounterValueTag
Definition: event.h:45
The event is storing an unsigned integer.
The event is storing a bool.
TraceKey Key
Definition: event.h:36
TRACE_API EventType GetType() const
Returns the type of the event.
TraceEvent(EndTag, const Key &key, TraceCategoryId cat)
Definition: event.h:125
TRACE_API double GetCounterValue() const
Return the counter value associated with this event.
MarkerTag
Definition: event.h:43
The event represents the ending timestamp of a scope.
TraceEvent(CounterValueTag, const Key &key, double value, TraceCategoryId cat)
Constructor for Counter value events.
Definition: event.h:188
TRACE_API TimeStamp GetStartTimeStamp() const
Returns the start time of a timespan event.
TraceEvent(DataTag, const Key &key, int data, TraceCategoryId cat)
Definition: event.h:210
The event is storing a string.
TRACE_API std::pair< TimeStamp, TimeStamp > GetTimeSpanStamps() const
TraceEvent(TimespanTag, const Key &key, TimeStamp startTime, TimeStamp endTime, TraceCategoryId cat)
Constructor for Timespan events that takes the start time and end time.
Definition: event.h:144
TraceEvent(BeginTag, const Key &key, TraceCategoryId cat)
Definition: event.h:105
TRACE_API TimeStamp GetEndTimeStamp() const
Returns the end time of a timespan event.
TimespanTag
Definition: event.h:42
TraceEvent & operator=(const TraceEvent &)=delete
The event is storing an integer.
TraceEvent(CounterDeltaTag, const Key &key, double value, TraceCategoryId cat)
Constructor for Counter delta events.
Definition: event.h:176
The event is not storing any data.
The event represents the beginning timestamp of a scope.
TraceEvent(DataTag, const Key &key, double data, TraceCategoryId cat)
Definition: event.h:237
DataType
The different types of data that can be stored in a TraceEvent instance.
Definition: event.h:63
BeginTag
Definition: event.h:40
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
uint64_t TimeStamp
Time in "ticks".
Definition: event.h:35
The event is an unknown type.
const Key & GetKey() const
Return this event's key.
Definition: event.h:73
EventType
Valid event types.
Definition: event.h:50
The event stores data that is associated with its enclosing scope.
TRACE_API TimeStamp GetTimeStamp() const
Return the time stamp associated with this event.
TraceEvent(MarkerTag, const Key &key, TimeStamp ts, TraceCategoryId cat)
Constructor for Mark events that takes a specific TimeStamp ts.
Definition: event.h:165
TraceEvent(BeginTag, const Key &key, TimeStamp ts, TraceCategoryId cat)
Constructor for Begin events that takes a specific TimeStamp ts.
Definition: event.h:113
The event represents a change in a counter.
CounterDeltaTag
Definition: event.h:44
uint32_t TraceCategoryId
Categories that a TraceReporter can use to filter events.
Definition: category.h:27
The event represents begin and end timestamp of a scope.
void SetTimeStamp(TimeStamp time)
Sets the events timestamp to time.
Definition: event.h:266
Definition: format.h:1821
TraceEvent(DataTag, const Key &key, const char *data, TraceCategoryId cat)
Definition: event.h:246
The event represents the value of a counter.
#define TRACE_API
Definition: api.h:23
Definition: key.h:23
TraceCategoryId GetCategory() const
Returns the event's category id.
Definition: event.h:82
TraceEvent(DataTag, const Key &key, uint64_t data, TraceCategoryId cat)
Definition: event.h:228