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_EVENT_TREE_H
26 #define PXR_BASE_TRACE_EVENT_TREE_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/eventNode.h"
33 #include "pxr/base/trace/threads.h"
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"
39 #include "pxr/base/tf/declarePtrs.h"
40 
41 #include <functional>
42 #include <vector>
43 #include <unordered_map>
44 
45 PXR_NAMESPACE_OPEN_SCOPE
46 
47 class TraceCollection;
48 class JsWriter;
49 TF_DECLARE_WEAK_AND_REF_PTRS(TraceEventTree);
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// \class TraceEventTree
53 ///
54 /// This class contains a timeline call tree and a map of counters to their
55 /// values over time.
56 ///
57 ///
58 class TraceEventTree : public TfRefBase, public TfWeakBase {
59 public:
60     using CounterValues = std::vector<std::pair<TraceEvent::TimeStamp, double>>;
61     using CounterValuesMap =
62         std::unordered_map<TfToken, CounterValues, TfToken::HashFunctor>;
63     using CounterMap =
64         std::unordered_map<TfToken, double, TfToken::HashFunctor>;
65 
66     using MarkerValues = std::vector<std::pair<TraceEvent::TimeStamp, TraceThreadId>>;
67     using MarkerValuesMap =
68         std::unordered_map<TfToken, MarkerValues, TfToken::HashFunctor>;
69 
70     /// Creates a new TraceEventTree instance from the data in \p collection
71     /// and \p initialCounterValues.
72     TRACE_API static TraceEventTreeRefPtr New(
73         const TraceCollection& collection,
74         const CounterMap* initialCounterValues = nullptr);
75 
New()76     static TraceEventTreeRefPtr New() {
77         return TfCreateRefPtr(
78             new TraceEventTree(TraceEventNode::New()));
79     }
80 
New(TraceEventNodeRefPtr root,CounterValuesMap counters,MarkerValuesMap markers)81     static TraceEventTreeRefPtr New(
82             TraceEventNodeRefPtr root,
83             CounterValuesMap counters,
84             MarkerValuesMap markers) {
85         return TfCreateRefPtr(
86             new TraceEventTree(root, std::move(counters), std::move(markers)));
87     }
88 
89     /// Returns the root node of the tree.
GetRoot()90     const TraceEventNodeRefPtr& GetRoot() const { return _root; }
91 
92     /// Returns the map of counter values.
GetCounters()93     const CounterValuesMap& GetCounters() const { return _counters; }
94 
95     /// Returns the map of markers values.
GetMarkers()96     const MarkerValuesMap& GetMarkers() const { return _markers; }
97 
98     /// Return the final value of the counters in the report.
99     CounterMap GetFinalCounterValues() const;
100 
101     /// Writes a JSON object representing the data in the call tree that
102     /// conforms to the Chrome Trace format.
103     using ExtraFieldFn = std::function<void(JsWriter&)>;
104     TRACE_API void WriteChromeTraceObject(
105         JsWriter& writer, ExtraFieldFn extraFields = ExtraFieldFn()) const;
106 
107     /// Adds the contexts of \p tree to this tree.
108     TRACE_API void Merge(const TraceEventTreeRefPtr& tree);
109 
110     /// Adds the data from \p collection to this tree.
111     TRACE_API TraceEventTreeRefPtr Add(const TraceCollection& collection);
112 
113 private:
TraceEventTree(TraceEventNodeRefPtr root)114     TraceEventTree(TraceEventNodeRefPtr root)
115         : _root(root) {}
116 
TraceEventTree(TraceEventNodeRefPtr root,CounterValuesMap counters,MarkerValuesMap markers)117     TraceEventTree(  TraceEventNodeRefPtr root,
118                             CounterValuesMap counters,
119                             MarkerValuesMap markers)
120         : _root(root)
121         , _counters(std::move(counters))
122         , _markers(std::move(markers)) {}
123 
124     // Root of the call tree.
125     TraceEventNodeRefPtr _root;
126     // Counter data of the trace.
127     CounterValuesMap _counters;
128     // Marker data of the trace.
129     MarkerValuesMap _markers;
130 };
131 
132 PXR_NAMESPACE_CLOSE_SCOPE
133 
134 #endif // PXR_BASE_TRACE_EVENT_TREE_H
135