All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
eventTree.h
1//
2// Copyright 2018 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7
8#ifndef PXR_BASE_TRACE_EVENT_TREE_H
9#define PXR_BASE_TRACE_EVENT_TREE_H
10
11#include "pxr/pxr.h"
12
13#include "pxr/base/trace/api.h"
14#include "pxr/base/trace/event.h"
15#include "pxr/base/trace/eventNode.h"
16#include "pxr/base/trace/threads.h"
17#include "pxr/base/tf/refBase.h"
18#include "pxr/base/tf/refPtr.h"
19#include "pxr/base/tf/token.h"
21#include "pxr/base/tf/weakPtr.h"
23
24#include <functional>
25#include <vector>
26#include <unordered_map>
27
28PXR_NAMESPACE_OPEN_SCOPE
29
30class TraceCollection;
31class JsWriter;
33
41class TraceEventTree : public TfRefBase, public TfWeakBase {
42public:
43 using CounterValues = std::vector<std::pair<TraceEvent::TimeStamp, double>>;
44 using CounterValuesMap =
45 std::unordered_map<TfToken, CounterValues, TfToken::HashFunctor>;
46 using CounterMap =
47 std::unordered_map<TfToken, double, TfToken::HashFunctor>;
48
49 using MarkerValues = std::vector<std::pair<TraceEvent::TimeStamp, TraceThreadId>>;
50 using MarkerValuesMap =
51 std::unordered_map<TfToken, MarkerValues, TfToken::HashFunctor>;
52
55 TRACE_API static TraceEventTreeRefPtr New(
56 const TraceCollection& collection,
57 const CounterMap* initialCounterValues = nullptr);
58
59 static TraceEventTreeRefPtr New() {
60 return TfCreateRefPtr(
62 }
63
64 static TraceEventTreeRefPtr New(
65 TraceEventNodeRefPtr root,
66 CounterValuesMap counters,
67 MarkerValuesMap markers) {
68 return TfCreateRefPtr(
69 new TraceEventTree(root, std::move(counters), std::move(markers)));
70 }
71
73 const TraceEventNodeRefPtr& GetRoot() const { return _root; }
74
76 const CounterValuesMap& GetCounters() const { return _counters; }
77
79 const MarkerValuesMap& GetMarkers() const { return _markers; }
80
82 CounterMap GetFinalCounterValues() const;
83
86 using ExtraFieldFn = std::function<void(JsWriter&)>;
87 TRACE_API void WriteChromeTraceObject(
88 JsWriter& writer, ExtraFieldFn extraFields = ExtraFieldFn()) const;
89
91 TRACE_API void Merge(const TraceEventTreeRefPtr& tree);
92
94 TRACE_API TraceEventTreeRefPtr Add(const TraceCollection& collection);
95
96private:
97 TraceEventTree(TraceEventNodeRefPtr root)
98 : _root(root) {}
99
100 TraceEventTree( TraceEventNodeRefPtr root,
101 CounterValuesMap counters,
102 MarkerValuesMap markers)
103 : _root(root)
104 , _counters(std::move(counters))
105 , _markers(std::move(markers)) {}
106
107 // Root of the call tree.
108 TraceEventNodeRefPtr _root;
109 // Counter data of the trace.
110 CounterValuesMap _counters;
111 // Marker data of the trace.
112 MarkerValuesMap _markers;
113};
114
115PXR_NAMESPACE_CLOSE_SCOPE
116
117#endif // PXR_BASE_TRACE_EVENT_TREE_H
This class provides an interface to writing json values directly to a stream.
Definition: json.h:59
Enable a concrete base class for use with TfRefPtr.
Definition: refBase.h:56
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:124
This class owns lists of TraceEvent instances per thread, and allows read access to them.
Definition: collection.h:32
static TraceEventNodeRefPtr New()
Creates a new root node.
Definition: eventNode.h:45
This class contains a timeline call tree and a map of counters to their values over time.
Definition: eventTree.h:41
TRACE_API TraceEventTreeRefPtr Add(const TraceCollection &collection)
Adds the data from collection to this tree.
static TRACE_API TraceEventTreeRefPtr New(const TraceCollection &collection, const CounterMap *initialCounterValues=nullptr)
Creates a new TraceEventTree instance from the data in collection and initialCounterValues.
CounterMap GetFinalCounterValues() const
Return the final value of the counters in the report.
TRACE_API void Merge(const TraceEventTreeRefPtr &tree)
Adds the contexts of tree to this tree.
const TraceEventNodeRefPtr & GetRoot() const
Returns the root node of the tree.
Definition: eventTree.h:73
const MarkerValuesMap & GetMarkers() const
Returns the map of markers values.
Definition: eventTree.h:79
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:86
const CounterValuesMap & GetCounters() const
Returns the map of counter values.
Definition: eventTree.h:76
Standard pointer typedefs.
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:72
STL namespace.
Reference counting.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...
Pointer storage with deletion detection.