Loading...
Searching...
No Matches
event.h
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
18
19#include <utility>
20
21PXR_NAMESPACE_OPEN_SCOPE
22
23class TraceEventData;
24
33public:
35 using TimeStamp = uint64_t;
36 using Key = TraceKey;
37
40 enum BeginTag { Begin };
41 enum EndTag { End };
42 enum TimespanTag { Timespan };
43 enum MarkerTag { Marker };
44 enum CounterDeltaTag { CounterDelta };
45 enum CounterValueTag { CounterValue };
46 enum DataTag { Data };
48
50 enum class EventType : uint8_t {
51 Unknown,
52 Begin,
53 End,
54 Timespan,
55 Marker,
56 CounterDelta,
57 CounterValue,
60 };
61
63 enum class DataType : uint8_t {
64 String,
65 Boolean,
66 Int,
67 UInt,
68 Float,
69 Invalid
70 };
71
73 const Key& GetKey() const { return _key; }
74
76 TRACE_API TimeStamp GetTimeStamp() const;
77
79 TRACE_API double GetCounterValue() const;
80
82 TraceCategoryId GetCategory() const { return _category; }
83
85 TRACE_API TimeStamp GetStartTimeStamp() const;
86
88 TRACE_API TimeStamp GetEndTimeStamp() const;
89
92 TRACE_API std::pair<TimeStamp, TimeStamp> GetTimeSpanStamps() const;
93
95 TRACE_API TraceEventData GetData() const;
96
98 TRACE_API EventType GetType() const;
99
102
105 TraceEvent(BeginTag, const Key& key, TraceCategoryId cat) :
106 _key(key),
107 _category(cat),
108 _type(_InternalEventType::Begin),
109 _time(ArchGetTickTime()) {
110 }
111
113 TraceEvent( BeginTag,
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
125 TraceEvent(EndTag, const Key& key, TraceCategoryId cat) :
126 _key(key),
127 _category(cat),
128 _type(_InternalEventType::End),
129 _time(ArchGetTickTime()) {
130 }
131
133 TraceEvent( EndTag,
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
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
157 TraceEvent(MarkerTag, const Key& key, TraceCategoryId cat) :
158 _key(key),
159 _category(cat),
160 _type(_InternalEventType::Marker),
161 _time(ArchGetTickTime()) {
162 }
163
165 TraceEvent( MarkerTag,
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
176 TraceEvent( CounterDeltaTag,
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
188 TraceEvent( CounterValueTag,
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
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 }
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
264
266 void SetTimeStamp(TimeStamp time) { _time = time; }
267private:
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,
278 ScopeDataLarge,
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
291PXR_NAMESPACE_CLOSE_SCOPE
292
293#endif // PXR_BASE_TRACE_EVENT_H
uint32_t TraceCategoryId
Categories that a TraceReporter can use to filter events.
Definition: category.h:27
This class holds data that can be stored in TraceEvents.
Definition: eventData.h:28
This represents an event recorded by a TraceCollector.
Definition: event.h:32
TRACE_API TraceEventData GetData() const
Returns the data stored in a data event.
uint64_t TimeStamp
Time in "ticks".
Definition: event.h:35
TraceEvent(MarkerTag, const Key &key, TraceCategoryId cat)
Constructor for Marker events that will automatically set the timestamp from the current time.
Definition: event.h:157
TRACE_API double GetCounterValue() const
Return the counter value 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
const Key & GetKey() const
Return this event's key.
Definition: event.h:73
TraceEvent(EndTag, const Key &key, TimeStamp ts, TraceCategoryId cat)
Constructor for End events that takes a specific TimeStamp ts.
Definition: event.h:133
DataType
The different types of data that can be stored in a TraceEvent instance.
Definition: event.h:63
@ UInt
The event is storing an unsigned integer.
@ Int
The event is storing an integer.
@ Float
The event is storing an double.
@ String
The event is storing a string.
@ Boolean
The event is storing a bool.
@ Invalid
The event is not storing any data.
TRACE_API std::pair< TimeStamp, TimeStamp > GetTimeSpanStamps() const
Return both the start and end time of a timespan event by GetStartTimeStamp() and GetEndTimeStamp().
TraceEvent(BeginTag, const Key &key, TraceCategoryId cat)
Constructor for Begin events that will automatically set the timestamp from the current time.
Definition: event.h:105
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
TRACE_API EventType GetType() const
Returns the type of the event.
TraceEvent(CounterValueTag, const Key &key, double value, TraceCategoryId cat)
Constructor for Counter value events.
Definition: event.h:188
TraceEvent(BeginTag, const Key &key, TimeStamp ts, TraceCategoryId cat)
Constructor for Begin events that takes a specific TimeStamp ts.
Definition: event.h:113
TraceCategoryId GetCategory() const
Returns the event's category id.
Definition: event.h:82
TraceEvent(CounterDeltaTag, const Key &key, double value, TraceCategoryId cat)
Constructor for Counter delta events.
Definition: event.h:176
TRACE_API TimeStamp GetEndTimeStamp() const
Returns the end time of a timespan event.
TraceEvent(EndTag, const Key &key, TraceCategoryId cat)
Constructor for End events that will automatically set the timestamp from the current time.
Definition: event.h:125
TRACE_API TimeStamp GetTimeStamp() const
Return the time stamp associated with this event.
void SetTimeStamp(TimeStamp time)
Sets the events timestamp to time.
Definition: event.h:266
TRACE_API TimeStamp GetStartTimeStamp() const
Returns the start time of a timespan event.
EventType
Valid event types.
Definition: event.h:50
@ ScopeData
The event stores data that is associated with its enclosing scope.
@ Unknown
The event is an unknown type.
A wrapper around a TraceStaticKeyData pointer that is stored in TraceEvent instances.
Definition: key.h:23
uint64_t ArchGetTickTime()
Return the current time in system-dependent units.
Definition: timing.h:67
High-resolution, low-cost timing routines.