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 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"
34 
36 #include "pxr/base/tf/mallocTag.h"
38 
39 #include <iosfwd>
40 #include <string>
41 
43 
44 #define TRACE_REPORTER_TOKENS \
45  ((warningString, "WARNING:"))
46 
48 
49 
53 
55 
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// \class TraceReporter
60 ///
61 /// This class converts streams of TraceEvent objects into call trees which
62 /// can then be used as a data source to a GUI or written out to a file.
63 ///
64 class TraceReporter :
65  public TraceReporterBase {
66 public:
67 
68  TF_MALLOC_TAG_NEW("Trace", "TraceReporter");
69 
71  using ThisPtr = TraceReporterPtr;
72  using ThisRefPtr = TraceReporterRefPtr;
73 
74  using Event = TraceEvent;
77 
78  /// Create a new reporter with \a label and \a dataSource.
80  DataSourcePtr dataSource) {
81  return TfCreateRefPtr(new This(label, std::move(dataSource)));
82  }
83 
84  /// Create a new reporter with \a label and no data source.
85  static ThisRefPtr New(const std::string& label) {
86  return TfCreateRefPtr(new This(label, nullptr));
87  }
88 
89  /// Returns the global reporter.
90  TRACE_API static TraceReporterPtr GetGlobalReporter();
91 
92  /// Destructor.
93  TRACE_API virtual ~TraceReporter();
94 
95  /// Return the label associated with this reporter.
96  const std::string& GetLabel() {
97  return _label;
98  }
99 
100  /// \name Report Generation.
101  /// @{
102 
103  /// Generates a report to the ostream \a s, dividing all times by
104  /// \a iterationCount.
105  TRACE_API void Report(
106  std::ostream &s,
107  int iterationCount=1);
108 
109  /// Generates a report of the times to the ostream \a s.
110  TRACE_API void ReportTimes(std::ostream &s);
111 
112  /// Generates a timeline trace report suitable for viewing in
113  /// Chrome's trace viewer.
114  TRACE_API void ReportChromeTracing(std::ostream &s);
115 
116  /// @}
117 
118  /// Returns the root node of the aggregated call tree.
119  TRACE_API TraceAggregateNodePtr GetAggregateTreeRoot();
120 
121  /// Returns the root node of the call tree.
122  TRACE_API TraceEventNodeRefPtr GetEventRoot();
123 
124  /// Returns the event call tree
125  TRACE_API TraceEventTreeRefPtr GetEventTree();
126 
127  /// \name Counters
128  /// @{
129 
130  /// Returns a map of counters (counter keys), associated with their total
131  /// accumulated value. Each individual event node in the tree may also hold
132  /// on to an inclusive and exclusive value for the given counter.
133  TRACE_API const CounterMap & GetCounters() const;
134 
135  /// Returns the numeric index associated with a counter key. Counter values
136  /// on the event nodes will have to be looked up by the numeric index.
137  TRACE_API int GetCounterIndex(const TfToken &key) const;
138 
139  /// Add a counter to the reporter. This method can be used to restore a
140  /// previous trace state and tree. Note, that the counter being added must
141  /// have a unique key and index. The method will return false if a key or
142  /// index already exists.
143  TRACE_API bool AddCounter(const TfToken &key, int index, double totalValue);
144 
145  /// @}
146 
147  /// This fully re-builds the event and aggregate trees from whatever the
148  /// current collection holds. It is ok to call this multiple times in case
149  /// the collection gets appended on inbetween.
150  ///
151  /// If we want to have multiple reporters per collector, this will need to
152  /// be changed so that all reporters reporting on a collector update their
153  /// respective trees.
155 
156  /// Clears event tree and counters.
157  TRACE_API void ClearTree();
158 
159  /// \name Report options.
160  /// @{
161 
162  /// This affects only stack trace event reporting. If \c true then all
163  /// events in a function are grouped together otherwise events are split
164  /// out by address.
165  TRACE_API void SetGroupByFunction(bool);
166 
167  /// Returns the current group-by-function state.
168  TRACE_API bool GetGroupByFunction() const;
169 
170  /// When stack trace event reporting, this sets whether or not recursive
171  /// calls are folded in the output. Recursion folding is useful when
172  /// the stacks contain deep recursive structures.
173  TRACE_API void SetFoldRecursiveCalls(bool);
174 
175  /// Returns the current setting for recursion folding for stack trace
176  /// event reporting.
177  TRACE_API bool GetFoldRecursiveCalls() const;
178 
179  /// @}
180 
181  /// Creates a valid TraceAggregateNode::Id object.
182  /// This should be used by very few clients for certain special cases.
183  /// For most cases, the TraceAggregateNode::Id object should be created and
184  /// populated internally within the Reporter object itself.
186 
187 protected:
188 
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 
208 
209 #endif // PXR_BASE_TRACE_REPORTER_H
TfHashMap< TfToken, double, TfToken::HashFunctor > CounterMap
Definition: reporter.h:76
TfRefPtr< T > TfCreateRefPtr(T *ptr)
Definition: refPtr.h:1248
TraceReporter This
Definition: reporter.h:70
static ThisRefPtr New(const std::string &label, DataSourcePtr dataSource)
Create a new reporter with label and dataSource.
Definition: reporter.h:79
TRACE_API void ReportTimes(std::ostream &s)
Generates a report of the times to the ostream s.
TraceReporterRefPtr ThisRefPtr
Definition: reporter.h:72
TRACE_API void Report(std::ostream &s, int iterationCount=1)
static TRACE_API TraceReporterPtr GetGlobalReporter()
Returns the global reporter.
TRACE_API TraceEventTreeRefPtr GetEventTree()
Returns the event call tree.
std::unique_ptr< TraceReporterDataSourceBase > DataSourcePtr
Definition: reporterBase.h:59
TRACE_API bool GetFoldRecursiveCalls() const
const std::string & GetLabel()
Return the label associated with this reporter.
Definition: reporter.h:96
TRACE_API void SetFoldRecursiveCalls(bool)
static TRACE_API TraceAggregateNode::Id CreateValidEventId()
TF_DECLARE_WEAK_AND_REF_PTRS(TraceAggregateTree)
TraceEvent::TimeStamp TimeStamp
Definition: reporter.h:75
Definition: token.h:87
TRACE_API bool AddCounter(const TfToken &key, int index, double totalValue)
TRACE_API TraceReporter(const std::string &label, DataSourcePtr dataSource)
static ThisRefPtr New(const std::string &label)
Create a new reporter with label and no data source.
Definition: reporter.h:85
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2544
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:58
TRACE_API int GetCounterIndex(const TfToken &key) const
TRACE_API void SetGroupByFunction(bool)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
TRACE_API TraceEventNodeRefPtr GetEventRoot()
Returns the root node of the call tree.
GLuint index
Definition: glcorearb.h:785
TraceReporterPtr ThisPtr
Definition: reporter.h:71
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
uint64_t TimeStamp
Time in "ticks".
Definition: event.h:50
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.
GLdouble s
Definition: glew.h:1395
#define TRACE_REPORTER_TOKENS
Definition: reporter.h:44
#define TRACE_API
Definition: api.h:40
TRACE_API bool GetGroupByFunction() const
Returns the current group-by-function state.