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