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