HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
perfLog.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 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 #ifndef PXR_IMAGING_HD_PERF_LOG_H
25 #define PXR_IMAGING_HD_PERF_LOG_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hd/api.h"
29 #include "pxr/imaging/hd/version.h"
31 
32 #include "pxr/base/trace/trace.h"
33 
34 #include "pxr/base/tf/mallocTag.h"
35 #include "pxr/base/tf/singleton.h"
36 #include "pxr/base/tf/token.h"
37 
38 #include "pxr/base/tf/hashmap.h"
39 
40 #include <memory>
41 #include <mutex>
42 
44 
45 
46 class SdfPath;
47 class HdResourceRegistry;
48 
49 // XXX: it would be nice to move this into Trace or use the existing Trace
50 // counter mechanism, however we are restricted to TraceLite in the rocks.
51 
52 //----------------------------------------------------------------------------//
53 // PERFORMANCE INSTURMENTATION MACROS //
54 //----------------------------------------------------------------------------//
55 
56 // Emits a trace scope tagged for the function.
57 #define HD_TRACE_FUNCTION() TRACE_FUNCTION()
58 // Emits a trace scope with the specified tag.
59 #define HD_TRACE_SCOPE(tag) TRACE_SCOPE(tag)
60 
61 // Adds a cache hit for the given cache name, the id is provided for debugging,
62 // see HdPerfLog for details.
63 #define HD_PERF_CACHE_HIT(name, id) \
64  HdPerfLog::GetInstance().AddCacheHit(name, id);
65 #define HD_PERF_CACHE_HIT_TAG(name, id, tag) \
66  HdPerfLog::GetInstance().AddCacheHit(name, id, tag);
67 
68 // Adds a cache miss for the given cache name, the id is provided for debugging,
69 // see HdPerfLog for details.
70 #define HD_PERF_CACHE_MISS(name, id) \
71  HdPerfLog::GetInstance().AddCacheMiss(name, id);
72 #define HD_PERF_CACHE_MISS_TAG(name, id, tag) \
73  HdPerfLog::GetInstance().AddCacheMiss(name, id, tag);
74 
75 // Increments/Decrements/Sets/Adds/Subtracts a named performance counter
76 // see HdPerfLog for details.
77 #define HD_PERF_COUNTER_INCR(name) \
78  HdPerfLog::GetInstance().IncrementCounter(name);
79 #define HD_PERF_COUNTER_DECR(name) \
80  HdPerfLog::GetInstance().DecrementCounter(name);
81 #define HD_PERF_COUNTER_SET(name, value) \
82  HdPerfLog::GetInstance().SetCounter(name, value);
83 #define HD_PERF_COUNTER_ADD(name, value) \
84  HdPerfLog::GetInstance().AddCounter(name, value);
85 #define HD_PERF_COUNTER_SUBTRACT(name, value) \
86  HdPerfLog::GetInstance().SubtractCounter(name, value);
87 
88 //----------------------------------------------------------------------------//
89 // PERFORMANCE LOG //
90 //----------------------------------------------------------------------------//
91 
92 /// \class HdPerfLog
93 ///
94 /// Performance counter monitoring.
95 ///
96 class HdPerfLog
97 {
98 public:
99  HD_API
100  static HdPerfLog& GetInstance() {
102  }
103 
104  /// Tracks a cache hit for the named cache, the id and tag are reported
105  /// when debug logging is enabled.
106  HD_API
107  void AddCacheHit(TfToken const& name,
108  SdfPath const& id,
109  TfToken const& tag=TfToken());
110 
111  /// Tracks a cache miss for the named cache, the id and tag are reported
112  /// when debug logging is enabled.
113  HD_API
114  void AddCacheMiss(TfToken const& name,
115  SdfPath const& id,
116  TfToken const& tag=TfToken());
117 
118  HD_API
119  void ResetCache(TfToken const& name);
120 
121  /// Gets the hit ratio (numHits / totalRequests) of a cache performance
122  /// counter.
123  HD_API
124  double GetCacheHitRatio(TfToken const& name);
125 
126  /// Gets the number of hit hits for a cache performance counter.
127  HD_API
128  size_t GetCacheHits(TfToken const& name);
129 
130  /// Gets the number of hit misses for a cache performance counter.
131  HD_API
132  size_t GetCacheMisses(TfToken const& name);
133 
134  /// Returns the names of all cache performance counters.
135  HD_API
137 
138  /// Returns a vector of all performance counter names.
139  HD_API
141 
142  /// Increments a named counter by 1.0.
143  HD_API
144  void IncrementCounter(TfToken const& name);
145 
146  /// Decrements a named counter by 1.0.
147  HD_API
148  void DecrementCounter(TfToken const& name);
149 
150  /// Sets the value of a named counter.
151  HD_API
152  void SetCounter(TfToken const& name, double value);
153 
154  /// Adds value to a named counter.
155  HD_API
156  void AddCounter(TfToken const& name, double value);
157 
158  /// Subtracts value to a named counter.
159  HD_API
160  void SubtractCounter(TfToken const& name, double value);
161 
162  /// Returns the current value of a named counter.
163  HD_API
164  double GetCounter(TfToken const& name);
165 
166  /// Reset all conter values to 0.0.
167  /// Note that this doesn't reset cache counters.
168  HD_API
169  void ResetCounters();
170 
171  /// Enable performance logging.
172  void Enable() { _enabled = true; }
173 
174  /// Disable performance logging.
175  void Disable() { _enabled = false; }
176 
177  /// Add a resource registry to the tracking.
178  HD_API
179  void AddResourceRegistry(HdResourceRegistry * resourceRegistry);
180 
181  /// Remove Resource Registry from the tracking.
182  HD_API
183  void RemoveResourceRegistry(HdResourceRegistry * resourceRegistry);
184 
185  /// Returns a vector of resource registry.
186  HD_API
187  std::vector<HdResourceRegistry*> const& GetResourceRegistryVector();
188 
189 private:
190 
191  // Don't allow copies
192  HdPerfLog(const HdPerfLog &) = delete;
193  HdPerfLog &operator=(const HdPerfLog &) = delete;
194 
195  friend class TfSingleton<HdPerfLog>;
196  HD_API HdPerfLog();
197  HD_API ~HdPerfLog();
198 
199  // Tracks number of hits and misses and provides some convenience API.
200  class _CacheEntry {
201  public:
202  _CacheEntry() : _hits(0), _misses(0) { }
203 
204  void AddHit() {++_hits;}
205  size_t GetHits() {return _hits;}
206 
207  void AddMiss() {++_misses;}
208  size_t GetMisses() {return _misses;}
209 
210  size_t GetTotal() {return _hits+_misses;}
211  double GetHitRatio() {return (double)_hits / GetTotal();}
212 
213  void Reset() { _hits = 0; _misses = 0; }
214  private:
215  size_t _hits;
216  size_t _misses;
217  };
218 
219  // Cache performance counters.
221  _CacheMap _cacheMap;
222 
223  // Named value counters.
225  _CounterMap _counterMap;
226 
227  // Resource registry vector.
228  std::vector<HdResourceRegistry *> _resourceRegistryVector;
229 
230  // Enable / disable performance tracking.
231  bool _enabled;
232  std::mutex _mutex;
233  typedef std::lock_guard<std::mutex> _Lock;
234 };
235 
237 
239 
240 #endif // PXR_IMAGING_HD_PERF_LOG_H
static T & GetInstance()
Definition: singleton.h:137
HD_API void IncrementCounter(TfToken const &name)
Increments a named counter by 1.0.
void Disable()
Disable performance logging.
Definition: perfLog.h:175
HD_API void SubtractCounter(TfToken const &name, double value)
Subtracts value to a named counter.
HD_API void ResetCache(TfToken const &name)
HD_API void AddCounter(TfToken const &name, double value)
Adds value to a named counter.
HD_API TfTokenVector GetCacheNames()
Returns the names of all cache performance counters.
HD_API_TEMPLATE_CLASS(TfSingleton< HdPerfLog >)
#define HD_API
Definition: api.h:40
HD_API TfTokenVector GetCounterNames()
Returns a vector of all performance counter names.
HD_API void SetCounter(TfToken const &name, double value)
Sets the value of a named counter.
HD_API double GetCounter(TfToken const &name)
Returns the current value of a named counter.
HD_API void ResetCounters()
void Enable()
Enable performance logging.
Definition: perfLog.h:172
Definition: token.h:87
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:457
GLuint const GLchar * name
Definition: glcorearb.h:786
Definition: path.h:290
HD_API void AddCacheHit(TfToken const &name, SdfPath const &id, TfToken const &tag=TfToken())
HD_API void AddCacheMiss(TfToken const &name, SdfPath const &id, TfToken const &tag=TfToken())
HD_API void DecrementCounter(TfToken const &name)
Decrements a named counter by 1.0.
HD_API void AddResourceRegistry(HdResourceRegistry *resourceRegistry)
Add a resource registry to the tracking.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
Definition: core.h:1131
static HD_API HdPerfLog & GetInstance()
Definition: perfLog.h:100
HD_API std::vector< HdResourceRegistry * > const & GetResourceRegistryVector()
Returns a vector of resource registry.
HD_API size_t GetCacheMisses(TfToken const &name)
Gets the number of hit misses for a cache performance counter.
HD_API size_t GetCacheHits(TfToken const &name)
Gets the number of hit hits for a cache performance counter.
HD_API void RemoveResourceRegistry(HdResourceRegistry *resourceRegistry)
Remove Resource Registry from the tracking.
HD_API double GetCacheHitRatio(TfToken const &name)