All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
172 
178  TRACE_API void MarkRecursiveChildren();
179 
185  bool IsRecursionMarker() const { return _isRecursionMarker; }
186 
192  bool IsRecursionHead() const { return _isRecursionHead; }
193 
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 
263 PXR_NAMESPACE_CLOSE_SCOPE
264 
265 #endif // PXR_BASE_TRACE_AGGREGATE_NODE_H
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 &quot;ticks&quot;.
Definition: event.h:50
bool IsRecursionHead() const
Returns true if this node is the head of a recursive call tree (i.e.
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
const Id & GetId()
Returns the node&#39;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.
TfToken GetKey()
Returns the node&#39;s key.
Definition: aggregateNode.h:97
int GetCount(bool recursive=false) const
Returns the call count of this node.
A representation of a call tree.
Definition: aggregateNode.h:57
bool IsRecursionMarker() const
Returns true if this node is simply a marker for a merged recursive subtree; otherwise returns false...
TRACE_API void CalculateInclusiveCounterValues()
Recursively calculates the inclusive counter values from the inclusive and exclusive counts of child ...
int GetExclusiveCount() const
Returns the exclusive count.
Reference counting.
TimeStamp GetInclusiveTime()
Returns the total time of this node ands its children.
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...