HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
aggregateNode.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_AGGREGATE_NODE_H
9 #define PXR_BASE_TRACE_AGGREGATE_NODE_H
10 
11 #include "pxr/pxr.h"
12 
13 #include "pxr/base/trace/api.h"
14 #include "pxr/base/trace/event.h"
15 #include "pxr/base/trace/threads.h"
16 
17 #include "pxr/base/tf/refBase.h"
18 #include "pxr/base/tf/refPtr.h"
19 #include "pxr/base/tf/token.h"
20 #include "pxr/base/tf/weakBase.h"
21 #include "pxr/base/tf/weakPtr.h"
23 #include "pxr/base/arch/timing.h"
24 
25 #include <vector>
27 
29 
31 
32 ////////////////////////////////////////////////////////////////////////////////
33 /// \class TraceAggregateNode
34 ///
35 /// A representation of a call tree. Each node represents one or more calls that
36 /// occurred in the trace. Multiple calls to a child node are aggregated into one
37 /// node.
38 ///
39 
40 class TraceAggregateNode : public TfRefBase, public TfWeakBase {
41 public:
42 
44  using ThisPtr = TraceAggregateNodePtr;
45  using ThisRefPtr = TraceAggregateNodeRefPtr;
46 
48 
49  static ThisRefPtr New() {
50  return This::New(TfToken("root"), 0, 0);
51  }
52 
53  static ThisRefPtr New(const TfToken &key,
54  const TimeStamp ts,
55  const int count = 1,
56  const int exclusiveCount = 1) {
57  return TfCreateRefPtr(new This(key, ts, count, exclusiveCount));
58  }
59 
60  TRACE_API TraceAggregateNodeRefPtr
61  Append(const TfToken &key, TimeStamp ts,int c = 1, int xc = 1);
62 
63  // Same as Append() but return nothing.
64  TRACE_API void
65  AppendBlind(const TfToken &key, TimeStamp ts, int c = 1, int xc = 1);
66 
67  TRACE_API void Append(TraceAggregateNodeRefPtr child);
68 
69  /// Returns the node's key.
70  const TfToken& GetKey() { return _key;}
71 
72  /// \name Profile Data Accessors
73  /// @{
74 
75  /// Returns the total time of this node ands its children.
76  TimeStamp GetInclusiveTime() { return _ts; }
77 
78  /// Returns the time spent in this node but not its children.
79  TRACE_API TimeStamp GetExclusiveTime(bool recursive = false);
80 
81  /// Returns the call count of this node. \p recursive determines if
82  /// recursive calls are counted.
83  int GetCount(bool recursive = false) const {
84  return recursive ? _recursiveCount : _count;
85  }
86 
87  /// Returns the exclusive count.
88  int GetExclusiveCount() const { return _exclusiveCount; }
89 
90  /// @}
91 
92 
93  /// \name Counter Value Accessors
94  /// @{
95 
97 
98  TRACE_API double GetInclusiveCounterValue(int index) const;
99 
101 
102  TRACE_API double GetExclusiveCounterValue(int index) const;
103 
104  /// @}
105 
106  /// Recursively calculates the inclusive counter values from the inclusive
107  /// and exclusive counts of child nodes.
109 
110 
111  /// \name Children Accessors
112  /// @{
113  const TraceAggregateNodePtrVector GetChildren() {
114  // convert to a vector of weak ptrs
115  return TraceAggregateNodePtrVector( _children.begin(),_children.end() );
116  }
117 
118  const TraceAggregateNodeRefPtrVector &GetChildrenRef() {
119  return _children;
120  }
121 
122  TRACE_API TraceAggregateNodeRefPtr GetChild(const TfToken &key);
123  TraceAggregateNodeRefPtr GetChild(const std::string &key) {
124  return GetChild(TfToken(key));
125  }
126 
127  /// @}
128 
129 
130  /// Sets whether or not this node is expanded in a gui.
131  void SetExpanded(bool expanded) {
132  _expanded = expanded;
133  }
134 
135  /// Returns whether this node is expanded in a gui.
136  bool IsExpanded() {
137  return _expanded;
138  }
139 
140  /// Subtract \p scopeOverhead cost times the number of descendant nodes from
141  /// the inclusive time of each node. If \p numDescendantNodes is not null,
142  /// add the number of nodes descendant to this node (not including this
143  /// node) to *numDescendantNodes. Also for any nodes with descendants that
144  /// are "noisy" wrt \p timerQuantum, do not subtract their times from the
145  /// parent's exclusive time, but instead set their times to zero. This way
146  /// we retain the sample count, but do not pollute the parent node's
147  /// exclusive time with noise.
149  TimeStamp scopeOverhead, TimeStamp timerQuantum,
150  uint64_t *numDescendantNodes = nullptr);
151 
152  /// \name Recursion
153  /// @{
154 
155  /// Scans the tree for recursive calls and updates the recursive counts.
156  ///
157  /// This call leaves the tree topology intact, and only updates the
158  /// recursion-related data in the node. Prior to this call, recursion
159  /// data is invalid in the node.
161 
162  /// Returns true if this node is simply a marker for a merged recursive
163  /// subtree; otherwise returns false.
164  ///
165  /// This value is meaningless until this node or any of its ancestors have
166  /// been marked with MarkRecursiveChildren().
167  bool IsRecursionMarker() const { return _isRecursionMarker; }
168 
169  /// Returns true if this node is the head of a recursive call tree
170  /// (i.e. the function has been called recursively).
171  ///
172  /// This value is meaningless until this node or any of its ancestors have
173  /// been marked with MarkRecursiveChildren().
174  bool IsRecursionHead() const { return _isRecursionHead; }
175 
176  /// @}
177 
178 
179 private:
180 
181  TraceAggregateNode(const TfToken &key, TimeStamp ts,
182  int count, int exclusiveCount) :
183  _key(key), _ts(ts), _exclusiveTs(ts),
184  _count(count), _exclusiveCount(exclusiveCount),
185  _recursiveCount(count), _recursiveExclusiveTs(ts), _expanded(false),
186  _isRecursionMarker(false), _isRecursionHead(false),
187  _isRecursionProcessed(false) {}
188 
189  using _ChildDictionary = TfDenseHashMap<TfToken, size_t, TfHash>;
190 
191  void _MergeRecursive(const TraceAggregateNodeRefPtr &node);
192 
193  void _SetAsRecursionMarker(TraceAggregateNodePtr parent);
194 
195  TraceAggregateNode *_GetChildRaw(const TfToken &key);
196 
197  const TfToken _key;
198  TimeStamp _ts;
199  TimeStamp _exclusiveTs;
200  int _count;
201  int _exclusiveCount;
202 
203  // We keep the recursive counts separate so that we don't mess with
204  // the collected data.
205  int _recursiveCount;
206  TraceAggregateNodePtr _recursionParent;
207  TimeStamp _recursiveExclusiveTs;
208 
209  TraceAggregateNodeRefPtrVector _children;
210  _ChildDictionary _childrenByKey;
211 
212  // A structure that holds on to the inclusive and exclusive counter
213  // values. These values are usually populated together, so it's beneficial
214  // to maintain them in a tightly packed structure.
215  struct _CounterValue {
216  _CounterValue() : inclusive(0.0), exclusive(0.0) {}
217  double inclusive;
218  double exclusive;
219  };
220 
221  using _CounterValues = TfDenseHashMap<int, _CounterValue, TfHash>;
222 
223  // The counter values associated with specific counter indices
224  _CounterValues _counterValues;
225 
226  unsigned int
227  // If multiple Trace Editors are to be pointed at the same Reporter, this
228  // might have to be changed
229  _expanded:1,
230 
231  // This flag keeps track of whether or not this node is simply intended
232  // as a marker for the start of a recursive call tree.
233  _isRecursionMarker:1,
234 
235  // This flag keeps track of whether or not a node is the head of a
236  // recursive call tree. In other words, if it is a function that has been
237  // called recursively.
238  _isRecursionHead:1,
239 
240  // This flag is used during recursive traversal to mark the node as having
241  // been visited and avoid too much processing.
242  _isRecursionProcessed:1;
243 };
244 
246 
247 #endif // PXR_BASE_TRACE_AGGREGATE_NODE_H
TfRefPtr< T > TfCreateRefPtr(T *ptr)
Definition: refPtr.h:1190
TRACE_API double GetInclusiveCounterValue(int index) const
TRACE_API void MarkRecursiveChildren()
bool IsExpanded()
Returns whether this node is expanded in a gui.
const TfToken & GetKey()
Returns the node's key.
Definition: aggregateNode.h:70
TRACE_API TraceAggregateNodeRefPtr GetChild(const TfToken &key)
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLfloat * value
Definition: glcorearb.h:824
void SetExpanded(bool expanded)
Sets whether or not this node is expanded in a gui.
int GetExclusiveCount() const
Returns the exclusive count.
Definition: aggregateNode.h:88
PXR_NAMESPACE_OPEN_SCOPE TF_DECLARE_WEAK_AND_REF_PTRS(TraceAggregateNode)
TraceAggregateNodeRefPtr ThisRefPtr
Definition: aggregateNode.h:45
TRACE_API TraceAggregateNodeRefPtr Append(const TfToken &key, TimeStamp ts, int c=1, int xc=1)
TraceAggregateNodeRefPtr GetChild(const std::string &key)
bool IsRecursionHead() const
Definition: token.h:70
TRACE_API void AppendExclusiveCounterValue(int index, double value)
TRACE_API TimeStamp GetExclusiveTime(bool recursive=false)
Returns the time spent in this node but not its children.
TRACE_API void AdjustForOverheadAndNoise(TimeStamp scopeOverhead, TimeStamp timerQuantum, uint64_t *numDescendantNodes=nullptr)
TRACE_API void AppendBlind(const TfToken &key, TimeStamp ts, int c=1, int xc=1)
const TraceAggregateNodeRefPtrVector & GetChildrenRef()
TRACE_API void AppendInclusiveCounterValue(int index, double value)
const TraceAggregateNodePtrVector GetChildren()
TraceAggregateNode This
Definition: aggregateNode.h:43
static ThisRefPtr New(const TfToken &key, const TimeStamp ts, const int count=1, const int exclusiveCount=1)
Definition: aggregateNode.h:53
int GetCount(bool recursive=false) const
Definition: aggregateNode.h:83
TRACE_API double GetExclusiveCounterValue(int index) const
GLuint index
Definition: glcorearb.h:786
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
TRACE_API void CalculateInclusiveCounterValues()
uint64_t TimeStamp
Time in "ticks".
Definition: event.h:35
bool IsRecursionMarker() const
TimeStamp GetInclusiveTime()
Returns the total time of this node ands its children.
Definition: aggregateNode.h:76
TraceEvent::TimeStamp TimeStamp
Definition: aggregateNode.h:47
GLint GLsizei count
Definition: glcorearb.h:405
static ThisRefPtr New()
Definition: aggregateNode.h:49
TraceAggregateNodePtr ThisPtr
Definition: aggregateNode.h:44
#define TRACE_API
Definition: api.h:23