Loading...
Searching...
No Matches
aggregateNode.h
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"
21#include "pxr/base/tf/weakPtr.h"
24
25#include <vector>
27
28PXR_NAMESPACE_OPEN_SCOPE
29
31
39
40class TraceAggregateNode : public TfRefBase, public TfWeakBase {
41public:
42
44 using ThisPtr = TraceAggregateNodePtr;
45 using ThisRefPtr = TraceAggregateNodeRefPtr;
46
47 using TimeStamp = TraceEvent::TimeStamp;
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
80 TfToken GetKey() { return _key;}
81
83 const Id &GetId() { return _id;}
84
87
89 TimeStamp GetInclusiveTime() { return _ts; }
90
92 TRACE_API TimeStamp GetExclusiveTime(bool recursive = false);
93
96 int GetCount(bool recursive = false) const {
97 return recursive ? _recursiveCount : _count;
98 }
99
101 int GetExclusiveCount() const { return _exclusiveCount; }
102
104
105
108
109 TRACE_API void AppendInclusiveCounterValue(int index, double value);
110
111 TRACE_API double GetInclusiveCounterValue(int index) const;
112
113 TRACE_API void AppendExclusiveCounterValue(int index, double value);
114
115 TRACE_API double GetExclusiveCounterValue(int index) const;
116
118
122
123
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
141
142
144 void SetExpanded(bool expanded) {
145 _expanded = expanded;
146 }
147
149 bool IsExpanded() {
150 return _expanded;
151 }
152
162 TimeStamp scopeOverhead, TimeStamp timerQuantum,
163 uint64_t *numDescendantNodes = nullptr);
164
167
173 TRACE_API void MarkRecursiveChildren();
174
180 bool IsRecursionMarker() const { return _isRecursionMarker; }
181
187 bool IsRecursionHead() const { return _isRecursionHead; }
188
190
191
192private:
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
258PXR_NAMESPACE_CLOSE_SCOPE
259
260#endif // PXR_BASE_TRACE_AGGREGATE_NODE_H
This is a space efficient container that mimics the TfHashMap API that uses a vector for storage when...
Definition: denseHashMap.h:41
Enable a concrete base class for use with TfRefPtr.
Definition: refBase.h:56
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:124
A representation of a call tree.
Definition: aggregateNode.h:40
int GetExclusiveCount() const
Returns the exclusive count.
int GetCount(bool recursive=false) const
Returns the call count of this node.
Definition: aggregateNode.h:96
bool IsExpanded()
Returns whether this node is expanded in a gui.
TRACE_API void CalculateInclusiveCounterValues()
Recursively calculates the inclusive counter values from the inclusive and exclusive counts of child ...
TfToken GetKey()
Returns the node's key.
Definition: aggregateNode.h:80
bool IsRecursionHead() const
Returns true if this node is the head of a recursive call tree (i.e.
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...
TRACE_API void MarkRecursiveChildren()
Scans the tree for recursive calls and updates the recursive counts.
TRACE_API TimeStamp GetExclusiveTime(bool recursive=false)
Returns the time spent in this node but not its children.
const Id & GetId()
Returns the node's id.
Definition: aggregateNode.h:83
TimeStamp GetInclusiveTime()
Returns the total time of this node ands its children.
Definition: aggregateNode.h:89
void SetExpanded(bool expanded)
Sets whether or not this node is expanded in a gui.
bool IsRecursionMarker() const
Returns true if this node is simply a marker for a merged recursive subtree; otherwise returns false.
uint64_t TimeStamp
Time in "ticks".
Definition: event.h:33
This class represents an identifier for a thread.
Definition: threads.h:23
Standard pointer typedefs.
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:72
Reference counting.
High-resolution, low-cost timing routines.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...
Pointer storage with deletion detection.