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 PXR_BASE_TRACE_REPORTER_H
26 #define PXR_BASE_TRACE_REPORTER_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/aggregateNode.h"
33 #include "pxr/base/trace/reporterBase.h"
34 
36 #include "pxr/base/tf/mallocTag.h"
38 
39 #include <iosfwd>
40 #include <string>
41 
42 PXR_NAMESPACE_OPEN_SCOPE
43 
44 #define TRACE_REPORTER_TOKENS \
45  ((warningString, "WARNING:"))
46 
47 TF_DECLARE_PUBLIC_TOKENS(TraceReporterTokens, TRACE_API, TRACE_REPORTER_TOKENS);
48 
49 
53 
55 
57 
64 class TraceReporter :
65  public TraceReporterBase {
66 public:
67 
68  TF_MALLOC_TAG_NEW("Trace", "TraceReporter");
69 
70  using This = TraceReporter;
71  using ThisPtr = TraceReporterPtr;
72  using ThisRefPtr = TraceReporterRefPtr;
73 
74  using Event = TraceEvent;
75  using TimeStamp = TraceEvent::TimeStamp;
76  using CounterMap = TfHashMap<TfToken, double, TfToken::HashFunctor>;
77 
79  static ThisRefPtr New(const std::string& label,
80  DataSourcePtr dataSource) {
81  return TfCreateRefPtr(new This(label, std::move(dataSource)));
82  }
83 
85  static ThisRefPtr New(const std::string& label) {
86  return TfCreateRefPtr(new This(label, nullptr));
87  }
88 
90  TRACE_API static TraceReporterPtr GetGlobalReporter();
91 
93  TRACE_API virtual ~TraceReporter();
94 
96  const std::string& GetLabel() {
97  return _label;
98  }
99 
102 
105  TRACE_API void Report(
106  std::ostream &s,
107  int iterationCount=1);
108 
110  TRACE_API void ReportTimes(std::ostream &s);
111 
114  TRACE_API void ReportChromeTracing(std::ostream &s);
115 
117 
119  TRACE_API TraceAggregateNodePtr GetAggregateTreeRoot();
120 
122  TRACE_API TraceEventNodeRefPtr GetEventRoot();
123 
125  TRACE_API TraceEventTreeRefPtr GetEventTree();
126 
129 
133  TRACE_API const CounterMap & GetCounters() const;
134 
137  TRACE_API int GetCounterIndex(const TfToken &key) const;
138 
143  TRACE_API bool AddCounter(const TfToken &key, int index, double totalValue);
144 
146 
154  TRACE_API void UpdateTraceTrees();
155 
157  TRACE_API void ClearTree();
158 
161 
165  TRACE_API void SetGroupByFunction(bool);
166 
168  TRACE_API bool GetGroupByFunction() const;
169 
173  TRACE_API void SetFoldRecursiveCalls(bool);
174 
177  TRACE_API bool GetFoldRecursiveCalls() const;
178 
180 
185  TRACE_API static TraceAggregateNode::Id CreateValidEventId();
186 
187 protected:
188 
189  TRACE_API TraceReporter(const std::string& label,
190  DataSourcePtr dataSource);
191 
192 private:
193  void _ProcessCollection(const TraceReporterBase::CollectionPtr&) override;
194  void _RebuildEventAndAggregateTrees();
195  void _PrintTimes(std::ostream &s);
196 
197 private:
198  std::string _label;
199 
200  bool _groupByFunction;
201  bool _foldRecursiveCalls;
202 
203  TraceAggregateTreeRefPtr _aggregateTree;
204  TraceEventTreeRefPtr _eventTree;
205 };
206 
207 PXR_NAMESPACE_CLOSE_SCOPE
208 
209 #endif // PXR_BASE_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:79
TRACE_API int GetCounterIndex(const TfToken &key) const
Returns the numeric index associated with a counter key.
TRACE_API TraceEventNodeRefPtr GetEventRoot()
Returns the root node of the call tree.
Standard pointer typedefs.
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:96
uint64_t TimeStamp
Time in "ticks".
Definition: event.h:50
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:89
TRACE_API void UpdateTraceTrees()
This fully re-builds the event and aggregate trees from whatever the current collection holds.
TRACE_API bool GetFoldRecursiveCalls() const
Returns the current setting for recursion folding for stack trace event reporting.
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:87
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:85
#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.
A TfNotice that is sent when the TraceCollector creates a TraceCollection.
TRACE_API void ReportChromeTracing(std::ostream &s)
Generates a timeline trace report suitable for viewing in Chrome'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.
This file defines some macros that are useful for declaring and using static TfTokens.
TRACE_API TraceAggregateNodePtr GetAggregateTreeRoot()
Returns the root node of the aggregated call tree.
This class converts streams of TraceEvent objects into call trees which can then be used as a data so...
Definition: reporter.h:64
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.