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 #include "pxr/base/trace/aggregateTree.h"
26 
27 #include "pxr/pxr.h"
28 
29 #include "pxr/base/trace/aggregateTreeBuilder.h"
30 #include "pxr/base/trace/collection.h"
31 #include "pxr/base/trace/eventTree.h"
32 
33 #include <stack>
34 
35 PXR_NAMESPACE_OPEN_SCOPE
36 
TraceAggregateTree()37 TraceAggregateTree::TraceAggregateTree()
38 {
39     Clear();
40 }
41 
42 void
Clear()43 TraceAggregateTree::Clear()
44 {
45     _root = TraceAggregateNode::New();
46     _eventTimes.clear();
47     _counters.clear();
48     _counterIndexMap.clear();
49     _counterIndex = 0;
50 }
51 
52 int
GetCounterIndex(const TfToken & key) const53 TraceAggregateTree::GetCounterIndex(const TfToken &key) const
54 {
55     _CounterIndexMap::const_iterator it = _counterIndexMap.find(key);
56     return it != _counterIndexMap.end() ? it->second : -1;
57 }
58 
59 bool
AddCounter(const TfToken & key,int index,double totalValue)60 TraceAggregateTree::AddCounter(const TfToken &key, int index, double totalValue)
61 {
62     // Don't add counters with invalid indices
63     if (!TF_VERIFY(index >= 0)) {
64         return false;
65     }
66 
67     // We don't expect a counter entry to exist with this key
68     if (!TF_VERIFY(_counters.find(key) == _counters.end())) {
69         return false;
70     }
71 
72     // We also don't expect the given index to be used by a different counter
73     for (const _CounterIndexMap::value_type& it : _counterIndexMap) {
74         if (!TF_VERIFY(it.second != index)) {
75             return false;
76         }
77     }
78 
79     // Add the new counter
80     _counters[key] = totalValue;
81     _counterIndexMap[key] = index;
82 
83     return true;
84 }
85 
86 void
Append(const TraceEventTreeRefPtr & eventTree,const TraceCollection & collection)87 TraceAggregateTree::Append(
88     const TraceEventTreeRefPtr& eventTree, const TraceCollection& collection)
89 {
90     Trace_AggregateTreeBuilder::AddEventTreeToAggregate(
91         this, eventTree, collection);
92 }
93 
94 PXR_NAMESPACE_CLOSE_SCOPE