HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
eventTree.h
Go to the documentation of this file.
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"
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"
40 
41 #include <functional>
42 #include <vector>
43 #include <unordered_map>
44 
46 
47 class TraceCollection;
48 class JsWriter;
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 
76  static TraceEventTreeRefPtr New() {
77  return TfCreateRefPtr(
79  }
80 
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.
90  const TraceEventNodeRefPtr& GetRoot() const { return _root; }
91 
92  /// Returns the map of counter values.
93  const CounterValuesMap& GetCounters() const { return _counters; }
94 
95  /// Returns the map of markers values.
96  const MarkerValuesMap& GetMarkers() const { return _markers; }
97 
98  /// Return the final value of the counters in the report.
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&)>;
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:
114  TraceEventTree(TraceEventNodeRefPtr root)
115  : _root(root) {}
116 
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 
133 
134 #endif // PXR_BASE_TRACE_EVENT_TREE_H
TfRefPtr< T > TfCreateRefPtr(T *ptr)
Definition: refPtr.h:1223
TRACE_API TraceEventTreeRefPtr Add(const TraceCollection &collection)
Adds the data from collection to this tree.
std::unordered_map< TfToken, CounterValues, TfToken::HashFunctor > CounterValuesMap
Definition: eventTree.h:62
const MarkerValuesMap & GetMarkers() const
Returns the map of markers values.
Definition: eventTree.h:96
std::unordered_map< TfToken, double, TfToken::HashFunctor > CounterMap
Definition: eventTree.h:64
static TraceEventTreeRefPtr New()
Definition: eventTree.h:76
std::function< void(JsWriter &)> ExtraFieldFn
Definition: eventTree.h:103
CounterMap GetFinalCounterValues() const
Return the final value of the counters in the report.
std::unordered_map< TfToken, MarkerValues, TfToken::HashFunctor > MarkerValuesMap
Definition: eventTree.h:68
const TraceEventNodeRefPtr & GetRoot() const
Returns the root node of the tree.
Definition: eventTree.h:90
static TraceEventNodeRefPtr New()
Definition: eventNode.h:62
TRACE_API void Merge(const TraceEventTreeRefPtr &tree)
Adds the contexts of tree to this tree.
TRACE_API void WriteChromeTraceObject(JsWriter &writer, ExtraFieldFn extraFields=ExtraFieldFn()) const
const CounterValuesMap & GetCounters() const
Returns the map of counter values.
Definition: eventTree.h:93
static TraceEventTreeRefPtr New(TraceEventNodeRefPtr root, CounterValuesMap counters, MarkerValuesMap markers)
Definition: eventTree.h:81
std::vector< std::pair< TraceEvent::TimeStamp, TraceThreadId >> MarkerValues
Definition: eventTree.h:66
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
Definition: json.h:76
std::vector< std::pair< TraceEvent::TimeStamp, double >> CounterValues
Definition: eventTree.h:60
TF_DECLARE_WEAK_AND_REF_PTRS(TraceEventTree)
#define TRACE_API
Definition: api.h:40