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_TREE_H
26 #define PXR_BASE_TRACE_AGGREGATE_TREE_H
27 
28 #include "pxr/pxr.h"
29 
30 #include "pxr/base/trace/api.h"
31 #include "pxr/base/trace/aggregateNode.h"
32 
33 PXR_NAMESPACE_OPEN_SCOPE
34 
35 class TraceCollection;
36 
37 TF_DECLARE_WEAK_AND_REF_PTRS(TraceAggregateTree);
38 TF_DECLARE_WEAK_AND_REF_PTRS(TraceEventTree);
39 
40 ////////////////////////////////////////////////////////////////////////////////
41 /// \class TraceAggregateTree
42 ///
43 /// A representation of a call tree. Each node represents one or more calls that
44 /// occurred in the trace. Multiple calls to a child node are aggregated into one
45 /// node.
46 ///
47 
48 class TraceAggregateTree : public TfRefBase, public TfWeakBase {
49 public:
50     using This = TraceAggregateTree;
51     using ThisPtr = TraceAggregateTreePtr;
52     using ThisRefPtr = TraceAggregateTreeRefPtr;
53 
54     using TimeStamp = TraceEvent::TimeStamp;
55     using EventTimes = std::map<TfToken, TimeStamp>;
56     using CounterMap = TfHashMap<TfToken, double, TfToken::HashFunctor>;
57 
58     /// Create an empty tree
New()59     static ThisRefPtr New() {
60         return TfCreateRefPtr(new This());
61     }
62 
63     /// Returns the root node of the tree.
GetRoot()64     TraceAggregateNodePtr GetRoot() { return _root; }
65 
66     /// Returns a map of event keys to total inclusive time.
GetEventTimes()67     const EventTimes& GetEventTimes() const { return _eventTimes; }
68 
69     /// Returns a map of counters (counter keys), associated with their total
70     /// accumulated value. Each individual event node in the tree may also hold
71     /// on to an inclusive and exclusive value for the given counter.
GetCounters()72     const CounterMap& GetCounters() const { return _counters; }
73 
74     /// Returns the numeric index associated with a counter key. Counter values
75     /// on the event nodes will have to be looked up by the numeric index.
76     TRACE_API int GetCounterIndex(const TfToken &key) const;
77 
78     /// Add a counter to the tree. This method can be used to restore a
79     /// previous trace state and tree. Note, that the counter being added must
80     /// have a unique key and index. The method will return false if a key or
81     /// index already exists.
82     TRACE_API bool AddCounter(const TfToken &key, int index, double totalValue);
83 
84     /// Removes all data and nodes from the tree.
85     TRACE_API void Clear();
86 
87     /// Creates new nodes and counter data from data in \p eventTree and \p
88     /// collection.
89     TRACE_API void Append(
90         const TraceEventTreeRefPtr& eventTree,
91         const TraceCollection& collection);
92 
93 private:
94     TRACE_API TraceAggregateTree();
95 
96     using _CounterIndexMap =TfHashMap<TfToken, int, TfToken::HashFunctor>;
97 
98     TraceAggregateNodeRefPtr _root;
99     EventTimes _eventTimes;
100     CounterMap _counters;
101     _CounterIndexMap _counterIndexMap;
102     int _counterIndex;
103 
104     friend class Trace_AggregateTreeBuilder;
105 };
106 
107 PXR_NAMESPACE_CLOSE_SCOPE
108 
109 #endif // PXR_BASE_TRACE_AGGREGATE_TREE_H
110