aggregateNode.h
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 
45 PXR_NAMESPACE_OPEN_SCOPE
46 
48 
56 
57 class TraceAggregateNode : public TfRefBase, public TfWeakBase {
58 public:
59 
60  using This = TraceAggregateNode;
61  using ThisPtr = TraceAggregateNodePtr;
62  using ThisRefPtr = TraceAggregateNodeRefPtr;
63 
64  using TimeStamp = TraceEvent::TimeStamp;
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 
97  TfToken GetKey() { return _key;}
98 
100  const Id &GetId() { return _id;}
101 
104 
106  TimeStamp GetInclusiveTime() { return _ts; }
107 
109  TRACE_API TimeStamp GetExclusiveTime(bool recursive = false);
110 
113  int GetCount(bool recursive = false) const {
114  return recursive ? _recursiveCount : _count;
115  }
116 
118  int GetExclusiveCount() const { return _exclusiveCount; }
119 
121 
122 
125 
126  TRACE_API void AppendInclusiveCounterValue(int index, double value);
127 
128  TRACE_API double GetInclusiveCounterValue(int index) const;
129 
130  TRACE_API void AppendExclusiveCounterValue(int index, double value);
131 
132  TRACE_API double GetExclusiveCounterValue(int index) const;
133 
135 
138  TRACE_API void CalculateInclusiveCounterValues();
139 
140 
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 
158 
159 
161  void SetExpanded(bool expanded) {
162  _expanded = expanded;
163  }
164 
166  bool IsExpanded() {
167  return _expanded;
168  }
169 
178  TRACE_API void AdjustForOverheadAndNoise(
179  TimeStamp scopeOverhead, TimeStamp timerQuantum,
180  uint64_t *numDescendantNodes = nullptr);
181 
184 
190  TRACE_API void MarkRecursiveChildren();
191 
197  bool IsRecursionMarker() const { return _isRecursionMarker; }
198 
204  bool IsRecursionHead() const { return _isRecursionHead; }
205 
207 
208 
209 private:
210 
211  TraceAggregateNode(const Id &id, const TfToken &key, TimeStamp ts,
212  int count, int exclusiveCount) :
213  _id(id), _key(key), _ts(ts), _exclusiveTs(ts),
214  _count(count), _exclusiveCount(exclusiveCount),
215  _recursiveCount(count), _recursiveExclusiveTs(ts), _expanded(false),
216  _isRecursionMarker(false), _isRecursionHead(false),
217  _isRecursionProcessed(false) {}
218 
219  using _ChildDictionary = TfDenseHashMap<TfToken, size_t, TfHash>;
220 
221  void _MergeRecursive(const TraceAggregateNodeRefPtr &node);
222 
223  void _SetAsRecursionMarker(TraceAggregateNodePtr parent);
224 
225  Id _id;
226  TfToken _key;
227 
228  TimeStamp _ts;
229  TimeStamp _exclusiveTs;
230  int _count;
231  int _exclusiveCount;
232 
233  // We keep the recursive counts separate so that we don't mess with
234  // the collected data.
235  int _recursiveCount;
236  TraceAggregateNodePtr _recursionParent;
237  TimeStamp _recursiveExclusiveTs;
238 
239  TraceAggregateNodeRefPtrVector _children;
240  _ChildDictionary _childrenByKey;
241 
242  // A structure that holds on to the inclusive and exclusive counter
243  // values. These values are usually populated together, so it's beneficial
244  // to maintain them in a tightly packed structure.
245  struct _CounterValue {
246  _CounterValue() : inclusive(0.0), exclusive(0.0) {}
247  double inclusive;
248  double exclusive;
249  };
250 
251  using _CounterValues = TfDenseHashMap<int, _CounterValue, TfHash>;
252 
253  // The counter values associated with specific counter indices
254  _CounterValues _counterValues;
255 
256  unsigned int
257  // If multiple Trace Editors are to be pointed at the same Reporter, this
258  // might have to be changed
259  _expanded:1,
260 
261  // This flag keeps track of whether or not this node is simply intended
262  // as a marker for the start of a recursive call tree.
263  _isRecursionMarker:1,
264 
265  // This flag keeps track of whether or not a node is the head of a
266  // recursive call tree. In other words, if it is a function that has been
267  // called recursively.
268  _isRecursionHead:1,
269 
270  // This flag is used during recursive traversal to mark the node as having
271  // been visited and avoid too much processing.
272  _isRecursionProcessed:1;
273 };
274 
275 PXR_NAMESPACE_CLOSE_SCOPE
276 
277 #endif // PXR_BASE_TRACE_AGGREGATE_NODE_H
int GetCount(bool recursive=false) const
Returns the call count of this node.
bool IsRecursionMarker() const
Returns true if this node is simply a marker for a merged recursive subtree; otherwise returns false.
Standard pointer typedefs.
TRACE_API TimeStamp GetExclusiveTime(bool recursive=false)
Returns the time spent in this node but not its children.
uint64_t TimeStamp
Time in "ticks".
Definition: event.h:50
Pointer storage with deletion detection.
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:89
TRACE_API void AdjustForOverheadAndNoise(TimeStamp scopeOverhead, TimeStamp timerQuantum, uint64_t *numDescendantNodes=nullptr)
Subtract scopeOverhead cost times the number of descendant nodes from the inclusive time of each node...
const Id & GetId()
Returns the node's id.
Enable a concrete base class for use with TfRefPtr.
Definition: refBase.h:71
bool IsExpanded()
Returns whether this node is expanded in a gui.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
TRACE_API void MarkRecursiveChildren()
Scans the tree for recursive calls and updates the recursive counts.
bool IsRecursionHead() const
Returns true if this node is the head of a recursive call tree (i.e.
TfToken GetKey()
Returns the node's key.
Definition: aggregateNode.h:97
A representation of a call tree.
Definition: aggregateNode.h:57
TRACE_API void CalculateInclusiveCounterValues()
Recursively calculates the inclusive counter values from the inclusive and exclusive counts of child ...
Reference counting.
TimeStamp GetInclusiveTime()
Returns the total time of this node ands its children.
int GetExclusiveCount() const
Returns the exclusive count.
void SetExpanded(bool expanded)
Sets whether or not this node is expanded in a gui.
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:141
High-resolution, low-cost timing routines.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...