HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
eventNode.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_NODE_H
9 #define PXR_BASE_TRACE_EVENT_NODE_H
10 
11 #include "pxr/pxr.h"
12 
13 #include "pxr/base/trace/api.h"
14 #include "pxr/base/trace/event.h"
16 
19 #include "pxr/base/tf/refBase.h"
20 #include "pxr/base/tf/refPtr.h"
22 #include "pxr/base/tf/span.h"
23 #include "pxr/base/tf/token.h"
24 
26 
28 
29 ////////////////////////////////////////////////////////////////////////////////
30 /// \class TraceEventNode
31 ///
32 /// TraceEventNode is used to represents call tree of a trace. Each node
33 /// represents a Begin-End trace event pair, or a single Timespan event. This is
34 /// useful for timeline views of a trace.
35 ///
36 
38 public:
39 
42  using AttributeMap = std::multimap<TfToken, AttributeData>;
43 
44  /// Creates a new root node.
45  ///
46  TRACE_API static TraceEventNodeRefPtr New();
47 
48  /// Creates a new node with \p key, \p category, \p beginTime and
49  /// \p endTime.
50  static TraceEventNodeRefPtr New(const TfToken &key,
51  const TraceCategoryId category,
52  const TimeStamp beginTime,
53  const TimeStamp endTime,
54  const bool separateEvents) {
55  return TfCreateRefPtr(
56  new TraceEventNode(
57  key, category, beginTime, endTime, separateEvents)
58  );
59  }
60 
61  /// Appends \p node as a child node.
62  TRACE_API void Append(TraceEventNodeRefPtr &&node);
63 
64  /// Appends \p node as a child node.
65  void Append(const TraceEventNodeRefPtr &node) {
66  Append(TraceEventNodeRefPtr { node });
67  }
68 
69  /// Returns the name of this node.
70  const TfToken &GetKey() const { return _key; }
71 
72  /// Returns the category of this node.
73  TraceCategoryId GetCategory() const { return _category; }
74 
75  /// Sets this node's begin and end time to the time extents of its direct
76  /// children.
78 
79  /// \name Profile Data Accessors
80  /// @{
81 
82  /// Returns the time that this scope started.
83  TimeStamp GetBeginTime() const { return _beginTime; }
84 
85  /// Returns the time that this scope ended.
86  TimeStamp GetEndTime() const { return _endTime; }
87 
88  /// Returns the time duration of this scope as GetEndTime() - GetBeginTime()
89  TimeStamp GetTimeDuration() const { return _endTime - _beginTime; }
90 
91  /// @}
92 
93  /// \name Children Accessors
94  /// @{
95 
96  /// Returns a TfSpan of references to the children of this node.
98  return _children;
99  }
100 
101  /// @}
102 
103  /// Return the data associated with this node.
104  TRACE_API const AttributeMap& GetAttributes() const;
105 
106  /// Add data to this node. If attribute data already exists for `key`,
107  /// `attr` is prepended to the values associated with `key`.
108  TRACE_API void AddAttribute(const TfToken& key, AttributeData&& attr);
109 
110  /// Returns whether this node was created from a Begin-End pair or a single
111  /// Timespan event.
112  bool IsFromSeparateEvents() const {
113  return _attributesAndSeparateEvents.BitsAs<bool>();
114  }
115 
116  ~TraceEventNode() override;
117 
118 private:
120 
121  void _SetIsSeparateEvents(bool isSeparate) {
122  _attributesAndSeparateEvents.SetBits(isSeparate);
123  }
124 
125  TraceEventNode(const TfToken &key,
126  TraceCategoryId category,
127  TimeStamp beginTime,
128  TimeStamp endTime,
129  bool separateEvents)
130  : _category(category)
131  , _key(key)
132  , _beginTime(beginTime)
133  , _endTime(endTime)
134  , _attributesAndSeparateEvents(nullptr, separateEvents)
135  {
136  }
137 
138  // _category (4 bytes) is first so it packs with TfRefBase's 4-byte count.
139  const TraceCategoryId _category;
140  const TfToken _key;
141  TimeStamp _beginTime;
142  TimeStamp _endTime;
143  // Empirical results show ~85% of nodes have < 2 children.
145  TfPointerAndBits<AttributeMap> _attributesAndSeparateEvents;
146 };
147 
149 
150 #endif // PXR_BASE_TRACE_EVENT_NODE_H
TfRefPtr< T > TfCreateRefPtr(T *ptr)
Definition: refPtr.h:1190
TRACE_API const AttributeMap & GetAttributes() const
Return the data associated with this node.
TimeStamp GetTimeDuration() const
Returns the time duration of this scope as GetEndTime() - GetBeginTime()
Definition: eventNode.h:89
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
void SetBits(Integral val) noexcept
Set the stored bits. No static range checking is performed.
static TraceEventNodeRefPtr New(const TfToken &key, const TraceCategoryId category, const TimeStamp beginTime, const TimeStamp endTime, const bool separateEvents)
Definition: eventNode.h:50
bool IsFromSeparateEvents() const
Definition: eventNode.h:112
TRACE_API void Append(TraceEventNodeRefPtr &&node)
Appends node as a child node.
Definition: token.h:70
void Append(const TraceEventNodeRefPtr &node)
Appends node as a child node.
Definition: eventNode.h:65
Definition: span.h:70
TraceCategoryId GetCategory() const
Returns the category of this node.
Definition: eventNode.h:73
PXR_NAMESPACE_OPEN_SCOPE TF_DECLARE_REF_PTRS(TraceEventNode)
TfSpan< const TraceEventNodeRefPtr > GetChildrenRef() const
Returns a TfSpan of references to the children of this node.
Definition: eventNode.h:97
constexpr Integral BitsAs() const noexcept
Retrieve the stored bits as the integral type Integral.
TraceEvent::TimeStamp TimeStamp
Definition: eventNode.h:40
friend class Trace_EventTreeBuilder
Definition: eventNode.h:119
TRACE_API void AddAttribute(const TfToken &key, AttributeData &&attr)
TRACE_API void SetBeginAndEndTimesFromChildren()
TimeStamp GetEndTime() const
Returns the time that this scope ended.
Definition: eventNode.h:86
~TraceEventNode() override
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
uint64_t TimeStamp
Time in "ticks".
Definition: event.h:35
std::multimap< TfToken, AttributeData > AttributeMap
Definition: eventNode.h:42
const TfToken & GetKey() const
Returns the name of this node.
Definition: eventNode.h:70
TraceEventData AttributeData
Definition: eventNode.h:41
static TRACE_API TraceEventNodeRefPtr New()
uint32_t TraceCategoryId
Categories that a TraceReporter can use to filter events.
Definition: category.h:27
TimeStamp GetBeginTime() const
Returns the time that this scope started.
Definition: eventNode.h:83
#define TRACE_API
Definition: api.h:23