HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
reporter.h
Go to the documentation of this file.
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_REPORTER_H
9 #define PXR_BASE_TRACE_REPORTER_H
10 
11 #include "pxr/pxr.h"
12 
13 #include "pxr/base/trace/api.h"
14 #include "pxr/base/trace/event.h"
17 
19 #include "pxr/base/tf/mallocTag.h"
21 
22 #include <iosfwd>
23 #include <string>
24 
26 
27 #define TRACE_REPORTER_TOKENS \
28  ((warningString, "WARNING:"))
29 
31 
32 
36 
38 
40 
41 ////////////////////////////////////////////////////////////////////////////////
42 /// \class TraceReporter
43 ///
44 /// This class converts streams of TraceEvent objects into call trees which
45 /// can then be used as a data source to a GUI or written out to a file.
46 ///
47 class TraceReporter :
48  public TraceReporterBase {
49 public:
50 
51  TF_MALLOC_TAG_NEW("Trace", "TraceReporter");
52 
54  using ThisPtr = TraceReporterPtr;
55  using ThisRefPtr = TraceReporterRefPtr;
56 
57  using Event = TraceEvent;
60 
61  /// Create a new reporter with \a label and \a dataSource.
62  static ThisRefPtr New(const std::string& label,
63  DataSourcePtr dataSource) {
64  return TfCreateRefPtr(new This(label, std::move(dataSource)));
65  }
66 
67  /// Create a new reporter with \a label and no data source.
68  static ThisRefPtr New(const std::string& label) {
69  return TfCreateRefPtr(new This(label, nullptr));
70  }
71 
72  /// Returns the global reporter.
73  TRACE_API static TraceReporterPtr GetGlobalReporter();
74 
75  /// Destructor.
76  TRACE_API virtual ~TraceReporter();
77 
78  /// Return the label associated with this reporter.
79  const std::string& GetLabel() {
80  return _label;
81  }
82 
83  /// \name Report Generation.
84  /// @{
85 
86  /// Generates a report to the ostream \a s, dividing all times by
87  /// \a iterationCount.
88  TRACE_API void Report(
89  std::ostream &s,
90  int iterationCount=1);
91 
92  /// Generates a report of the times to the ostream \a s.
93  TRACE_API void ReportTimes(std::ostream &s);
94 
95  /// Generates a timeline trace report suitable for viewing in
96  /// Chrome's trace viewer.
97  TRACE_API void ReportChromeTracing(std::ostream &s);
98 
99  /// @}
100 
101  /// \name Report Loading.
102  /// @{
103 
104  /// Aggregate tree and its iteration count, parsed from a report.
105  struct ParsedTree {
106  TraceAggregateTreeRefPtr tree;
108  };
109 
110  /// Load an aggregate tree report from the \p stream, as written by
111  /// Report().
112  ///
113  /// Since multiple reports may be appended to a given trace file, this will
114  /// return a vector of each tree and their iteration count.
115  ///
116  /// This will multiply the parsed values for each aggregate tree by their
117  /// iteration count.
118  TRACE_API static std::vector<ParsedTree> LoadReport(
119  std::istream &stream);
120 
121  /// @}
122 
123  /// Returns the root node of the aggregated call tree.
124  TRACE_API TraceAggregateNodePtr GetAggregateTreeRoot();
125 
126  /// Returns the root node of the call tree.
127  TRACE_API TraceEventNodeRefPtr GetEventRoot();
128 
129  /// Returns the event call tree
130  TRACE_API TraceEventTreeRefPtr GetEventTree();
131 
132  /// \name Counters
133  /// @{
134 
135  /// Returns a map of counters (counter keys), associated with their total
136  /// accumulated value. Each individual event node in the tree may also hold
137  /// on to an inclusive and exclusive value for the given counter.
138  TRACE_API const CounterMap & GetCounters() const;
139 
140  /// Returns the numeric index associated with a counter key. Counter values
141  /// on the event nodes will have to be looked up by the numeric index.
142  TRACE_API int GetCounterIndex(const TfToken &key) const;
143 
144  /// Add a counter to the reporter. This method can be used to restore a
145  /// previous trace state and tree. Note, that the counter being added must
146  /// have a unique key and index. The method will return false if a key or
147  /// index already exists.
148  TRACE_API bool AddCounter(const TfToken &key, int index, double totalValue);
149 
150  /// @}
151 
152  /// This fully re-builds the event and aggregate trees from whatever the
153  /// current collection holds. It is ok to call this multiple times in case
154  /// the collection gets appended on inbetween.
155  ///
156  /// If we want to have multiple reporters per collector, this will need to
157  /// be changed so that all reporters reporting on a collector update their
158  /// respective trees.
160 
161  /// Clears event tree and counters.
162  TRACE_API void ClearTree();
163 
164  /// \name Report options.
165  /// @{
166 
167  /// This affects only stack trace event reporting. If \c true then all
168  /// events in a function are grouped together otherwise events are split
169  /// out by address.
170  TRACE_API void SetGroupByFunction(bool);
171 
172  /// Returns the current group-by-function state.
173  TRACE_API bool GetGroupByFunction() const;
174 
175  /// When stack trace event reporting, this sets whether or not recursive
176  /// calls are folded in the output. Recursion folding is useful when
177  /// the stacks contain deep recursive structures.
178  TRACE_API void SetFoldRecursiveCalls(bool);
179 
180  /// Returns the current setting for recursion folding for stack trace
181  /// event reporting.
182  TRACE_API bool GetFoldRecursiveCalls() const;
183 
184  /// Set whether or not the reporter should adjust scope times for overhead
185  /// and noise.
187 
188  /// Returns the current setting for addjusting scope times for overhead and
189  /// noise.
191 
192  /// @}
193 
194  /// Creates a valid TraceAggregateNode::Id object.
195  /// This should be used by very few clients for certain special cases.
196  /// For most cases, the TraceAggregateNode::Id object should be created and
197  /// populated internally within the Reporter object itself.
199 
200 protected:
201 
202  TRACE_API TraceReporter(const std::string& label,
203  DataSourcePtr dataSource);
204 
205 private:
206  void _ProcessCollection(const TraceReporterBase::CollectionPtr&) override;
207  void _RebuildEventAndAggregateTrees();
208  void _PrintTimes(std::ostream &s);
209 
210 private:
211  std::string _label;
212 
213  bool _groupByFunction;
214  bool _foldRecursiveCalls;
215  bool _shouldAdjustForOverheadAndNoise;
216 
217  TraceAggregateTreeRefPtr _aggregateTree;
218  TraceEventTreeRefPtr _eventTree;
219 };
220 
222 
223 #endif // PXR_BASE_TRACE_REPORTER_H
GLuint GLuint stream
Definition: glcorearb.h:1832
TfRefPtr< T > TfCreateRefPtr(T *ptr)
Definition: refPtr.h:1190
TraceReporter This
Definition: reporter.h:53
static ThisRefPtr New(const std::string &label, DataSourcePtr dataSource)
Create a new reporter with label and dataSource.
Definition: reporter.h:62
TRACE_API void ReportTimes(std::ostream &s)
Generates a report of the times to the ostream s.
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2545
TraceReporterRefPtr ThisRefPtr
Definition: reporter.h:55
TRACE_API void Report(std::ostream &s, int iterationCount=1)
static TRACE_API TraceReporterPtr GetGlobalReporter()
Returns the global reporter.
static TRACE_API std::vector< ParsedTree > LoadReport(std::istream &stream)
TRACE_API TraceEventTreeRefPtr GetEventTree()
Returns the event call tree.
std::unique_ptr< TraceReporterDataSourceBase > DataSourcePtr
Definition: reporterBase.h:42
TRACE_API bool GetFoldRecursiveCalls() const
GLdouble s
Definition: glad.h:3009
const std::string & GetLabel()
Return the label associated with this reporter.
Definition: reporter.h:79
TRACE_API void SetFoldRecursiveCalls(bool)
static TRACE_API TraceAggregateNode::Id CreateValidEventId()
Aggregate tree and its iteration count, parsed from a report.
Definition: reporter.h:105
TF_DECLARE_WEAK_AND_REF_PTRS(TraceAggregateTree)
TraceEvent::TimeStamp TimeStamp
Definition: reporter.h:58
Definition: token.h:70
TRACE_API bool AddCounter(const TfToken &key, int index, double totalValue)
TRACE_API TraceReporter(const std::string &label, DataSourcePtr dataSource)
TRACE_API void SetShouldAdjustForOverheadAndNoise(bool adjust)
static ThisRefPtr New(const std::string &label)
Create a new reporter with label and no data source.
Definition: reporter.h:68
TRACE_API void ClearTree()
Clears event tree and counters.
TRACE_API TraceAggregateNodePtr GetAggregateTreeRoot()
Returns the root node of the aggregated call tree.
std::shared_ptr< TraceCollection > CollectionPtr
Definition: reporterBase.h:41
TRACE_API int GetCounterIndex(const TfToken &key) const
TRACE_API void SetGroupByFunction(bool)
TRACE_API bool ShouldAdjustForOverheadAndNoise() const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
TRACE_API TraceEventNodeRefPtr GetEventRoot()
Returns the root node of the call tree.
GLuint index
Definition: glcorearb.h:786
TraceReporterPtr ThisPtr
Definition: reporter.h:54
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
uint64_t TimeStamp
Time in "ticks".
Definition: event.h:33
TRACE_API void UpdateTraceTrees()
TF_DECLARE_PUBLIC_TOKENS(TraceReporterTokens, TRACE_API, TRACE_REPORTER_TOKENS)
TRACE_API const CounterMap & GetCounters() const
TRACE_API void ReportChromeTracing(std::ostream &s)
TF_MALLOC_TAG_NEW("Trace","TraceReporter")
virtual TRACE_API ~TraceReporter()
Destructor.
#define TRACE_REPORTER_TOKENS
Definition: reporter.h:27
TraceAggregateTreeRefPtr tree
Definition: reporter.h:106
#define TRACE_API
Definition: api.h:23
TRACE_API bool GetGroupByFunction() const
Returns the current group-by-function state.