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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 
25 #ifndef PXR_BASE_TRACE_AGGREGATE_NODE_H
26 #define PXR_BASE_TRACE_AGGREGATE_NODE_H
27 
28 #include "pxr/pxr.h"
29 
30 #include "pxr/base/trace/api.h"
31 #include "pxr/base/trace/event.h"
32 #include "pxr/base/trace/threads.h"
33 
34 #include "pxr/base/tf/refBase.h"
35 #include "pxr/base/tf/refPtr.h"
36 #include "pxr/base/tf/token.h"
37 #include "pxr/base/tf/weakBase.h"
38 #include "pxr/base/tf/weakPtr.h"
40 #include "pxr/base/arch/timing.h"
41 
42 #include <vector>
44 
46 
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// \class TraceAggregateNode
51 ///
52 /// A representation of a call tree. Each node represents one or more calls that
53 /// occurred in the trace. Multiple calls to a child node are aggregated into one
54 /// node.
55 ///
56 
57 class TraceAggregateNode : public TfRefBase, public TfWeakBase {
58 public:
59 
61  using ThisPtr = TraceAggregateNodePtr;
62  using ThisRefPtr = TraceAggregateNodeRefPtr;
63 
65 
66  // This class is only used for validity checks.
67  // FIXME: This class should be removed.
68  class Id
69  {
70  public:
71  Id() : _valid(false) {}
72  Id(const TraceThreadId&) : _valid(true) {}
73  bool IsValid() const { return _valid; }
74  private:
75  bool _valid;
76  };
77 
78  static ThisRefPtr New() {
79  return This::New(Id(), TfToken("root"), 0, 0);
80  }
81 
82  static ThisRefPtr New(const Id &id,
83  const TfToken &key,
84  const TimeStamp ts,
85  const int count = 1,
86  const int exclusiveCount = 1) {
87  return TfCreateRefPtr(new This(id, key, ts, count, exclusiveCount));
88  }
89 
90  TRACE_API TraceAggregateNodeRefPtr
91  Append(Id id, const TfToken &key, TimeStamp ts,
92  int c = 1, int xc = 1);
93 
94  TRACE_API void Append(TraceAggregateNodeRefPtr child);
95 
96  /// Returns the node's key.
97  TfToken GetKey() { return _key;}
98 
99  /// Returns the node's id.
100  const Id &GetId() { return _id;}
101 
102  /// \name Profile Data Accessors
103  /// @{
104 
105  /// Returns the total time of this node ands its children.
106  TimeStamp GetInclusiveTime() { return _ts; }
107 
108  /// Returns the time spent in this node but not its children.
109  TRACE_API TimeStamp GetExclusiveTime(bool recursive = false);
110 
111  /// Returns the call count of this node. \p recursive determines if
112  /// recursive calls are counted.
113  int GetCount(bool recursive = false) const {
114  return recursive ? _recursiveCount : _count;
115  }
116 
117  /// Returns the exclusive count.
118  int GetExclusiveCount() const { return _exclusiveCount; }
119 
120  /// @}
121 
122 
123  /// \name Counter Value Accessors
124  /// @{
125 
127 
128  TRACE_API double GetInclusiveCounterValue(int index) const;
129 
131 
132  TRACE_API double GetExclusiveCounterValue(int index) const;
133 
134  /// @}
135 
136  /// Recursively calculates the inclusive counter values from the inclusive
137  /// and exclusive counts of child nodes.
139 
140 
141  /// \name Children Accessors
142  /// @{
143  const TraceAggregateNodePtrVector GetChildren() {
144  // convert to a vector of weak ptrs
145  return TraceAggregateNodePtrVector( _children.begin(),_children.end() );
146  }
147 
148  const TraceAggregateNodeRefPtrVector &GetChildrenRef() {
149  return _children;
150  }
151 
152  TRACE_API TraceAggregateNodeRefPtr GetChild(const TfToken &key);
153  TraceAggregateNodeRefPtr GetChild(const std::string &key) {
154  return GetChild(TfToken(key));
155  }
156 
157  /// @}
158 
159 
160  /// Sets whether or not this node is expanded in a gui.
161  void SetExpanded(bool expanded) {
162  _expanded = expanded;
163  }
164 
165  /// Returns whether this node is expanded in a gui.
166  bool IsExpanded() {
167  return _expanded;
168  }
169 
170  /// \name Recursion
171  /// @{
172 
173  /// Scans the tree for recursive calls and updates the recursive counts.
174  ///
175  /// This call leaves the tree topology intact, and only updates the
176  /// recursion-related data in the node. Prior to this call, recursion
177  /// data is invalid in the node.
179 
180  /// Returns true if this node is simply a marker for a merged recursive
181  /// subtree; otherwise returns false.
182  ///
183  /// This value is meaningless until this node or any of its ancestors have
184  /// been marked with MarkRecursiveChildren().
185  bool IsRecursionMarker() const { return _isRecursionMarker; }
186 
187  /// Returns true if this node is the head of a recursive call tree
188  /// (i.e. the function has been called recursively).
189  ///
190  /// This value is meaningless until this node or any of its ancestors have
191  /// been marked with MarkRecursiveChildren().
192  bool IsRecursionHead() const { return _isRecursionHead; }
193 
194  /// @}
195 
196 
197 private:
198 
199  TraceAggregateNode(const Id &id, const TfToken &key, TimeStamp ts,
200  int count, int exclusiveCount) :
201  _id(id), _key(key), _ts(ts), _exclusiveTs(ts),
202  _count(count), _exclusiveCount(exclusiveCount),
203  _recursiveCount(count), _recursiveExclusiveTs(ts), _expanded(false),
204  _isRecursionMarker(false), _isRecursionHead(false),
205  _isRecursionProcessed(false) {}
206 
207  using _ChildDictionary = TfDenseHashMap<TfToken, size_t, TfHash>;
208 
209  void _MergeRecursive(const TraceAggregateNodeRefPtr &node);
210 
211  void _SetAsRecursionMarker(TraceAggregateNodePtr parent);
212 
213  Id _id;
214  TfToken _key;
215 
216  TimeStamp _ts;
217  TimeStamp _exclusiveTs;
218  int _count;
219  int _exclusiveCount;
220 
221  // We keep the recursive counts separate so that we don't mess with
222  // the collected data.
223  int _recursiveCount;
224  TraceAggregateNodePtr _recursionParent;
225  TimeStamp _recursiveExclusiveTs;
226 
227  TraceAggregateNodeRefPtrVector _children;
228  _ChildDictionary _childrenByKey;
229 
230  // A structure that holds on to the inclusive and exclusive counter
231  // values. These values are usually populated together, so it's beneficial
232  // to maintain them in a tightly packed structure.
233  struct _CounterValue {
234  _CounterValue() : inclusive(0.0), exclusive(0.0) {}
235  double inclusive;
236  double exclusive;
237  };
238 
239  using _CounterValues = TfDenseHashMap<int, _CounterValue, TfHash>;
240 
241  // The counter values associated with specific counter indices
242  _CounterValues _counterValues;
243 
244  unsigned int
245  // If multiple Trace Editors are to be pointed at the same Reporter, this
246  // might have to be changed
247  _expanded:1,
248 
249  // This flag keeps track of whether or not this node is simply intended
250  // as a marker for the start of a recursive call tree.
251  _isRecursionMarker:1,
252 
253  // This flag keeps track of whether or not a node is the head of a
254  // recursive call tree. In other words, if it is a function that has been
255  // called recursively.
256  _isRecursionHead:1,
257 
258  // This flag is used during recursive traversal to mark the node as having
259  // been visited and avoid too much processing.
260  _isRecursionProcessed:1;
261 };
262 
264 
265 #endif // PXR_BASE_TRACE_AGGREGATE_NODE_H
TfRefPtr< T > TfCreateRefPtr(T *ptr)
Definition: refPtr.h:1248
TRACE_API double GetInclusiveCounterValue(int index) const
TfToken GetKey()
Returns the node's key.
Definition: aggregateNode.h:97
TRACE_API void MarkRecursiveChildren()
bool IsValid() const
Definition: aggregateNode.h:73
bool IsExpanded()
Returns whether this node is expanded in a gui.
TRACE_API TraceAggregateNodeRefPtr GetChild(const TfToken &key)
void SetExpanded(bool expanded)
Sets whether or not this node is expanded in a gui.
int GetExclusiveCount() const
Returns the exclusive count.
const GLfloat * c
Definition: glew.h:16631
PXR_NAMESPACE_OPEN_SCOPE TF_DECLARE_WEAK_AND_REF_PTRS(TraceAggregateNode)
TraceAggregateNodeRefPtr ThisRefPtr
Definition: aggregateNode.h:62
TraceAggregateNodeRefPtr GetChild(const std::string &key)
bool IsRecursionHead() const
Definition: token.h:87
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:82
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
const TraceAggregateNodeRefPtrVector & GetChildrenRef()
TRACE_API void AppendInclusiveCounterValue(int index, double value)
const TraceAggregateNodePtrVector GetChildren()
GLint GLsizei count
Definition: glcorearb.h:404
TraceAggregateNode This
Definition: aggregateNode.h:60
Id(const TraceThreadId &)
Definition: aggregateNode.h:72
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
int GetCount(bool recursive=false) const
TRACE_API double GetExclusiveCounterValue(int index) const
GLuint index
Definition: glcorearb.h:785
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
TRACE_API void CalculateInclusiveCounterValues()
GLsizei const GLfloat * value
Definition: glcorearb.h:823
uint64_t TimeStamp
Time in "ticks".
Definition: event.h:50
bool IsRecursionMarker() const
TimeStamp GetInclusiveTime()
Returns the total time of this node ands its children.
TraceEvent::TimeStamp TimeStamp
Definition: aggregateNode.h:64
static ThisRefPtr New()
Definition: aggregateNode.h:78
TRACE_API TraceAggregateNodeRefPtr Append(Id id, const TfToken &key, TimeStamp ts, int c=1, int xc=1)
TraceAggregateNodePtr ThisPtr
Definition: aggregateNode.h:61
#define TRACE_API
Definition: api.h:40
const Id & GetId()
Returns the node's id.