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