HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
executionStats.h
Go to the documentation of this file.
1 //
2 // Copyright 2025 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_EXEC_VDF_EXECUTION_STATS_H
8 #define PXR_EXEC_VDF_EXECUTION_STATS_H
9 
10 /// \file
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/exec/vdf/api.h"
15 #include "pxr/exec/vdf/types.h"
16 #include "pxr/exec/vdf/node.h"
17 
18 #include "pxr/base/arch/timing.h"
19 
20 #include <tbb/concurrent_queue.h>
21 #include <tbb/enumerable_thread_specific.h>
22 
23 #include <deque>
24 #include <optional>
25 #include <thread>
26 
28 
29 class VdfNetwork;
30 
31 /// Execution stats profiling event logger.
32 ///
33 /// Clients must use a VdfExecutionStatsProcessor to interact with the results
34 /// logged in the stats object.
35 ///
37 {
38  // Flag for a begin event.
39  //
40  constexpr static uint8_t _StartFlag = 0x80;
41 
42  // Flag for a corresponding end event.
43  //
44  constexpr static uint8_t _EndFlag = 0xC0;
45 
46 public:
47 
48  /// The upper 2 bits are reserved as a flag for the event type:
49  /// Highest bit : time event flag
50  /// 2nd high bit : time end event flag
51  ///
52  /// The lower 6 bits are the base type of the event. Scoped events are
53  /// automatically tagged as begin and end events.
54  ///
55  enum EventType : uint8_t {
56  // Base enum of timed events
61 
62  // Single events
67 
68  // NOTE : All event types must be less than or equal to MaxEvent
69  MaxEvent = 0x3F
70  };
71 
72  typedef uint64_t EventData;
73 
74  /// Execution Stats event. Event struct that is pushed onto event vector.
75  /// Should never be constructed outside of this class.
76  ///
77  struct Event {
81 
84  VdfId nodeId,
85  EventData data) :
86  nodeId(nodeId), data(data), event(event) {}
87  };
88 
89  /// Scoped event that automatically logs when created and destroyed.
90  /// Should be used in the event of logging time intervals over logging
91  /// single begin and end events via LogTimestamp.
92  ///
93  struct ScopedEvent {
94  VDF_API
96  VdfExecutionStats* stats,
97  const VdfNode& node,
98  EventType eventType);
99 
100  VDF_API
101  ~ScopedEvent();
102 
103  protected:
105 
106  private:
107  const VdfNode* _node;
108  EventType _event;
109  };
110 
111  /// Scoped event that automatically pushes and pops malloc tags for the
112  /// given VdfNode.
113  ///
114  struct ScopedMallocEvent : public ScopedEvent {
115  VDF_API
117  VdfExecutionStats* stats,
118  const VdfNode& node,
119  EventType eventType);
120 
121  VDF_API
123 
124  private:
125  std::string _tagName;
126  };
127 
128  /// Constructor for parent execution stats that have no invoking node.
129  ///
130  VDF_API
131  explicit VdfExecutionStats(const VdfNetwork* network);
132 
133  /// Destructor
134  ///
135  VDF_API
137 
138  /// Log event API. Used to log a single event.
139  ///
140  void LogData(EventType event, const VdfNode& node, EventData data) {
141  _Log(event, node.GetId(), data);
142  }
143 
144  /// Log timestamp API. Used to log a single timestamp.
145  ///
146  void LogTimestamp(EventType event, const VdfNode& node) {
147  _LogTime(event, node);
148  }
149 
150  /// Logs timestamped begin event. Automatically flags the event.
151  /// NOTE : To get automatic begin and end logging on scope, use ScopedEvent.
152  ///
154  _LogTime(_TagBegin(event), node);
155  }
156 
157  /// Logs timestamped end event. Automatically flags the event.
158  /// NOTE : To get automatic begina dn end logging on scope, use ScopedEvent.
159  ///
160  void LogEndTimestamp(EventType event, const VdfNode& node) {
161  _LogTime(_TagEnd(event), node);
162  }
163 
164  /// Push execution stats onto the hierarchy queue.
165  ///
166  VDF_API
168  const VdfNetwork* network,
169  const VdfNode* invokingNode);
170 
171  /// Returns the invoking node, if any.
172  ///
173  const std::optional<VdfId>& GetInvokingNodeId() const {
174  return _invokingNodeId;
175  }
176 
177  /// Returns a unique name for the given node.
178  ///
179  VDF_API
180  static std::string GetMallocTagName(
181  const VdfId *invokingNodeId,
182  const VdfNode &node);
183 
184  /// Returns the base event (e.g. event type specified by the lower 6 bits).
185  ///
187  return (EventType)(static_cast<uint8_t>(event) & 0x3F);
188  }
189 
190  /// Returns true if the event is an end event (e.g. if the second highest
191  /// bit is set).
192  ///
193  static bool IsEndEvent(EventType event) {
194  return 0x40 & static_cast<uint8_t>(event);
195  }
196 
197  /// Returns true if the event is a begin event.
198  ///
199  static bool IsBeginEvent(EventType event) {
200  return !IsEndEvent(event);
201  }
202 
203 protected:
204  /// Sub stat constructor. Only called from _AddSubStat.
205  ///
206  VDF_API
207  VdfExecutionStats(const VdfNetwork* network, VdfId nodeId);
208 
209  /// Logs data.
210  ///
212  {
213  _events.local().events.push_back(Event(event, nodeId, data));
214  }
215 
216  /// Logs timestamp.
217  ///
218  void _LogTime(EventType event, const VdfNode& node)
219  {
220  _Log(event, node.GetId(), ArchGetTickTime());
221  }
222 
223  /// Adds sub stat.
224  ///
225  VDF_API
227  const VdfNetwork* network,
228  VdfId invokingNodeId);
229 
230  /// Tags the begin flag.
231  ///
233  return (EventType)(static_cast<uint8_t>(event) | _StartFlag);
234  }
235 
236  /// Tags the end flag.
237  ///
239  return (EventType)(static_cast<uint8_t>(event) | _EndFlag);
240  }
241 
242 private:
244 
245  // Pointer to the VdfNetwork for whose nodes this execution stats tracks.
246  //
247  const VdfNetwork* _network;
248 
249  // Index of the VdfNode that calls into this network.
250  //
251  std::optional<VdfId> _invokingNodeId;
252 
253  // A structure of events recorded per thread.
254  //
255  struct _PerThreadEvents {
256  _PerThreadEvents() : threadId(std::this_thread::get_id()) {}
257 
258  // The thread id
259  std::thread::id threadId;
260 
261  // The vector of events recorded
262  typedef std::deque<Event> EventVector;
263  EventVector events;
264  };
265 
266  // The per-thread event vectors.
267  //
268  tbb::enumerable_thread_specific<_PerThreadEvents> _events;
269 
270  // Concurrent vector of VdfExecutionStats to keep track of execution stats
271  // from networks (i.e. sharing network) that are invoked during computation
272  // while profiling.
273  //
274  tbb::concurrent_queue<VdfExecutionStats*> _subStats;
275 };
276 
277 ////////////////////////////////////////////////////////////////////////////////
278 
280 
281 #endif
GLboolean * data
Definition: glcorearb.h:131
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
void LogEndTimestamp(EventType event, const VdfNode &node)
uint64_t ArchGetTickTime()
Definition: timing.h:67
Definition: node.h:52
void _Log(EventType event, VdfId nodeId, EventData data)
void LogBeginTimestamp(EventType event, const VdfNode &node)
#define VDF_API
Definition: api.h:25
void _LogTime(EventType event, const VdfNode &node)
VDF_API ScopedEvent(VdfExecutionStats *stats, const VdfNode &node, EventType eventType)
struct _cl_event * event
Definition: glcorearb.h:2961
static bool IsEndEvent(EventType event)
VDF_API VdfExecutionStats * _AddSubStat(const VdfNetwork *network, VdfId invokingNodeId)
VdfId GetId() const
Definition: node.h:116
void LogTimestamp(EventType event, const VdfNode &node)
VDF_API ScopedMallocEvent(VdfExecutionStats *stats, const VdfNode &node, EventType eventType)
static EventType GetBaseEvent(EventType event)
GLuint id
Definition: glcorearb.h:655
Event(EventType event, VdfId nodeId, EventData data)
EventType _TagEnd(EventType event)
VDF_API ~VdfExecutionStats()
void LogData(EventType event, const VdfNode &node, EventData data)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
EventType _TagBegin(EventType event)
VDF_API VdfExecutionStats(const VdfNetwork *network)
VDF_API VdfExecutionStats * AddSubStat(const VdfNetwork *network, const VdfNode *invokingNode)
const std::optional< VdfId > & GetInvokingNodeId() const
Definition: format.h:1821
static VDF_API std::string GetMallocTagName(const VdfId *invokingNodeId, const VdfNode &node)
static bool IsBeginEvent(EventType event)
uint64_t VdfId
The unique identifier type for Vdf objects.
Definition: types.h:107