All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
eventTree.h
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"
40 
41 #include <functional>
42 #include <vector>
43 #include <unordered_map>
44 
45 PXR_NAMESPACE_OPEN_SCOPE
46 
47 class TraceCollection;
48 class JsWriter;
50 
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 
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 
90  const TraceEventNodeRefPtr& GetRoot() const { return _root; }
91 
93  const CounterValuesMap& GetCounters() const { return _counters; }
94 
96  const MarkerValuesMap& GetMarkers() const { return _markers; }
97 
99  CounterMap GetFinalCounterValues() const;
100 
103  using ExtraFieldFn = std::function<void(JsWriter&)>;
104  TRACE_API void WriteChromeTraceObject(
105  JsWriter& writer, ExtraFieldFn extraFields = ExtraFieldFn()) const;
106 
108  TRACE_API void Merge(const TraceEventTreeRefPtr& tree);
109 
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 
132 PXR_NAMESPACE_CLOSE_SCOPE
133 
134 #endif // PXR_BASE_TRACE_EVENT_TREE_H
Standard pointer typedefs.
This class contains a timeline call tree and a map of counters to their values over time...
Definition: eventTree.h:58
TRACE_API TraceEventTreeRefPtr Add(const TraceCollection &collection)
Adds the data from collection to this tree.
Pointer storage with deletion detection.
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:89
CounterMap GetFinalCounterValues() const
Return the final value of the counters in the report.
static TRACE_API TraceEventTreeRefPtr New(const TraceCollection &collection, const CounterMap *initialCounterValues=nullptr)
Creates a new TraceEventTree instance from the data in collection and initialCounterValues.
TRACE_API void Merge(const TraceEventTreeRefPtr &tree)
Adds the contexts of tree to this tree.
Enable a concrete base class for use with TfRefPtr.
Definition: refBase.h:71
static TraceEventNodeRefPtr New()
Creates a new root node.
Definition: eventNode.h:62
const TraceEventNodeRefPtr & GetRoot() const
Returns the root node of the tree.
Definition: eventTree.h:90
const CounterValuesMap & GetCounters() const
Returns the map of counter values.
Definition: eventTree.h:93
Reference counting.
This class owns lists of TraceEvent instances per thread, and allows read access to them...
Definition: collection.h:49
const MarkerValuesMap & GetMarkers() const
Returns the map of markers values.
Definition: eventTree.h:96
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:141
This class provides an interface to writing json values directly to a stream.
Definition: json.h:76
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...
std::function< void(JsWriter &)> ExtraFieldFn
Writes a JSON object representing the data in the call tree that conforms to the Chrome Trace format...
Definition: eventTree.h:103