All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
reporter.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 TRACE_REPORTER_H
26 #define TRACE_REPORTER_H
27 
28 #include "pxr/pxr.h"
29 
30 #include "pxr/base/trace/api.h"
31 #include "pxr/base/trace/collector.h"
32 #include "pxr/base/trace/event.h"
33 #include "pxr/base/trace/aggregateNode.h"
34 #include "pxr/base/trace/key.h"
35 #include "pxr/base/trace/reporterBase.h"
36 
37 #include "pxr/base/tf/declarePtrs.h"
38 #include "pxr/base/tf/mallocTag.h"
39 #include "pxr/base/tf/staticTokens.h"
40 
41 #include <tbb/concurrent_queue.h>
42 
43 #include <map>
44 #include <ostream>
45 #include <string>
46 #include <vector>
47 
48 PXR_NAMESPACE_OPEN_SCOPE
49 
50 #define TRACE_REPORTER_TOKENS \
51  ((warningString, "WARNING:"))
52 
53 TF_DECLARE_PUBLIC_TOKENS(TraceReporterTokens, TRACE_REPORTER_TOKENS);
54 
55 
60 
62 
63 class TraceAggregateNode;
65 
72 class TraceReporter :
73  public TraceReporterBase {
74 public:
75 
76  TF_MALLOC_TAG_NEW("Trace", "TraceReporter");
77 
78  using This = TraceReporter;
79  using ThisPtr = TraceReporterPtr;
80  using ThisRefPtr = TraceReporterRefPtr;
81 
82  using Event = TraceEvent;
83  using TimeStamp = TraceEvent::TimeStamp;
84  using CounterMap = TfHashMap<TfToken, double, TfToken::HashFunctor>;
85 
87  static ThisRefPtr New(const std::string& label,
88  DataSourcePtr dataSource) {
89  return TfCreateRefPtr(new This(label, std::move(dataSource)));
90  }
91 
93  static ThisRefPtr New(const std::string& label) {
94  return TfCreateRefPtr(new This(label, nullptr));
95  }
96 
98  TRACE_API static TraceReporterPtr GetGlobalReporter();
99 
101  TRACE_API virtual ~TraceReporter();
102 
104  const std::string& GetLabel() {
105  return _label;
106  }
107 
110 
113  TRACE_API void Report(
114  std::ostream &s,
115  int iterationCount=1);
116 
118  TRACE_API void ReportTimes(std::ostream &s);
119 
122  TRACE_API void ReportChromeTracing(std::ostream &s);
123 
125 
127  TRACE_API TraceAggregateNodePtr GetAggregateTreeRoot();
128 
130  TRACE_API TraceEventNodeRefPtr GetEventRoot();
131 
133  TRACE_API TraceEventTreeRefPtr GetEventTree();
134 
137 
141  TRACE_API const CounterMap & GetCounters() const;
142 
145  TRACE_API int GetCounterIndex(const TfToken &key) const;
146 
151  TRACE_API bool AddCounter(const TfToken &key, int index, double totalValue);
152 
154 
162  TRACE_API void UpdateAggregateTree();
163 
165  TRACE_API void UpdateEventTree();
166 
168  TRACE_API void ClearTree();
169 
172 
176  TRACE_API void SetGroupByFunction(bool);
177 
179  TRACE_API bool GetGroupByFunction() const;
180 
184  TRACE_API void SetFoldRecursiveCalls(bool);
185 
188  TRACE_API bool GetFoldRecursiveCalls() const;
189 
191 
196  TRACE_API static TraceAggregateNode::Id CreateValidEventId();
197 
198 protected:
199 
200  TRACE_API TraceReporter(const std::string& label,
201  DataSourcePtr dataSource);
202 
203 private:
204  void _ProcessCollection(const TraceReporterBase::CollectionPtr&) override;
205  void _RebuildEventAndAggregateTrees();
206  void _PrintRecursionMarker(std::ostream &s, const std::string &label,
207  int indent);
208  void _PrintLineTimes(std::ostream &s, double inclusive, double exclusive,
209  int count, const std::string& label, int indent,
210  bool recursive_node, int iterationCount=1);
211  void _PrintNodeTimes(std::ostream &s, TraceAggregateNodeRefPtr node,
212  int indent, int iterationCount=1);
213  void _PrintLineCalls(std::ostream &s, int inclusive, int exclusive,
214  int total, const std::string& label, int indent);
215  void _PrintTimes(std::ostream &s);
216 
217  std::string _GetKeyName(const TfToken&) const;
218 
219  std::string _label;
220 
221  bool _groupByFunction;
222  bool _foldRecursiveCalls;
223 
224  TraceAggregateTreeRefPtr _aggregateTree;
225  TraceEventTreeRefPtr _eventTree;
226 
227  bool _buildEventTree;
228 };
229 
230 PXR_NAMESPACE_CLOSE_SCOPE
231 
232 #endif // TRACE_REPORTER_H
static TRACE_API TraceAggregateNode::Id CreateValidEventId()
Creates a valid TraceAggregateNode::Id object.
TRACE_API void ClearTree()
Clears event tree and counters.
TRACE_API void SetFoldRecursiveCalls(bool)
When stack trace event reporting, this sets whether or not recursive calls are folded in the output...
static ThisRefPtr New(const std::string &label, DataSourcePtr dataSource)
Create a new reporter with label and dataSource.
Definition: reporter.h:87
TRACE_API void UpdateEventTree()
Placeholder for UpdateAggregateTree().
TRACE_API TraceEventNodeRefPtr GetEventRoot()
Returns the root node of the call tree.
TRACE_API void ReportTimes(std::ostream &s)
Generates a report of the times to the ostream s.
A representation of a call tree.
Definition: aggregateTree.h:48
This class contains a timeline call tree and a map of counters to their values over time...
Definition: eventTree.h:58
TRACE_API void SetGroupByFunction(bool)
This affects only stack trace event reporting.
TraceEventNode is used to represents call tree of a trace.
Definition: eventNode.h:53
const std::string & GetLabel()
Return the label associated with this reporter.
Definition: reporter.h:104
uint64_t TimeStamp
Time in &quot;ticks&quot;.
Definition: event.h:50
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:89
This class is a base class for report implementations.
Definition: reporterBase.h:52
TRACE_API bool AddCounter(const TfToken &key, int index, double totalValue)
Add a counter to the reporter.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
This represents an event recorded by a TraceCollector.
Definition: event.h:47
static ThisRefPtr New(const std::string &label)
Create a new reporter with label and no data source.
Definition: reporter.h:93
TRACE_API const CounterMap & GetCounters() const
Returns a map of counters (counter keys), associated with their total accumulated value...
TRACE_API bool GetGroupByFunction() const
Returns the current group-by-function state.
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:118
static TRACE_API TraceReporterPtr GetGlobalReporter()
Returns the global reporter.
virtual TRACE_API ~TraceReporter()
Destructor.
TRACE_API int GetCounterIndex(const TfToken &key) const
Returns the numeric index associated with a counter key.
A representation of a call tree.
Definition: aggregateNode.h:57
A TfNotice that is sent when the TraceCollector creates a TraceCollection.
TRACE_API void UpdateAggregateTree()
This fully re-builds the event and aggregate trees from whatever the current collection holds...
TRACE_API void ReportChromeTracing(std::ostream &s)
Generates a timeline trace report suitable for viewing in Chrome&#39;s trace viewer.
TRACE_API TraceEventTreeRefPtr GetEventTree()
Returns the event call tree.
TRACE_API void Report(std::ostream &s, int iterationCount=1)
Generates a report to the ostream s, dividing all times by iterationCount.
TRACE_API TraceAggregateNodePtr GetAggregateTreeRoot()
Returns the root node of the aggregated call tree.
This class converters streams of TraceEvent objects into call trees which can then be used as a data ...
Definition: reporter.h:72
TRACE_API bool GetFoldRecursiveCalls() const
Returns the current setting for recursion folding for stack trace event reporting.