All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
perfLog.h
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"
30 #include "pxr/imaging/hd/debugCodes.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 
43 PXR_NAMESPACE_OPEN_SCOPE
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 
96 class HdPerfLog
97 {
98 public:
99  HD_API
100  static HdPerfLog& GetInstance() {
102  }
103 
106  HD_API
107  void AddCacheHit(TfToken const& name,
108  SdfPath const& id,
109  TfToken const& tag=TfToken());
110 
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 
123  HD_API
124  double GetCacheHitRatio(TfToken const& name);
125 
127  HD_API
128  size_t GetCacheHits(TfToken const& name);
129 
131  HD_API
132  size_t GetCacheMisses(TfToken const& name);
133 
135  HD_API
137 
139  HD_API
141 
143  HD_API
144  void IncrementCounter(TfToken const& name);
145 
147  HD_API
148  void DecrementCounter(TfToken const& name);
149 
151  HD_API
152  void SetCounter(TfToken const& name, double value);
153 
155  HD_API
156  void AddCounter(TfToken const& name, double value);
157 
159  HD_API
160  void SubtractCounter(TfToken const& name, double value);
161 
163  HD_API
164  double GetCounter(TfToken const& name);
165 
168  HD_API
169  void ResetCounters();
170 
172  void Enable() { _enabled = true; }
173 
175  void Disable() { _enabled = false; }
176 
178  HD_API
179  void AddResourceRegistry(HdResourceRegistry * resourceRegistry);
180 
182  HD_API
183  void RemoveResourceRegistry(HdResourceRegistry * resourceRegistry);
184 
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.
220  typedef TfHashMap<TfToken, _CacheEntry, TfToken::HashFunctor> _CacheMap;
221  _CacheMap _cacheMap;
222 
223  // Named value counters.
224  typedef TfHashMap<TfToken, double, TfToken::HashFunctor> _CounterMap;
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 
236 HD_API_TEMPLATE_CLASS(TfSingleton<HdPerfLog>);
237 
238 PXR_NAMESPACE_CLOSE_SCOPE
239 
240 #endif // PXR_IMAGING_HD_PERF_LOG_H
HD_API void AddCacheMiss(TfToken const &name, SdfPath const &id, TfToken const &tag=TfToken())
Tracks a cache miss for the named cache, the id and tag are reported when debug logging is enabled...
Manage a single instance of an object.
Manage a single instance of an object (see.
Definition: singleton.h:122
HD_API void DecrementCounter(TfToken const &name)
Decrements a named counter by 1.0.
HD_API void RemoveResourceRegistry(HdResourceRegistry *resourceRegistry)
Remove Resource Registry from the tracking.
HD_API size_t GetCacheMisses(TfToken const &name)
Gets the number of hit misses for a cache performance counter.
void Disable()
Disable performance logging.
Definition: perfLog.h:175
void Enable()
Enable performance logging.
Definition: perfLog.h:172
HD_API double GetCacheHitRatio(TfToken const &name)
Gets the hit ratio (numHits / totalRequests) of a cache performance counter.
HD_API void AddCacheHit(TfToken const &name, SdfPath const &id, TfToken const &tag=TfToken())
Tracks a cache hit for the named cache, the id and tag are reported when debug logging is enabled...
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
HD_API TfTokenVector GetCounterNames()
Returns a vector of all performance counter names.
HD_API size_t GetCacheHits(TfToken const &name)
Gets the number of hit hits for a cache performance counter.
A central registry for resources.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
HD_API void ResetCounters()
Reset all conter values to 0.0.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:288
static T & GetInstance()
Return a reference to an object of type T, creating it if necessary.
Definition: singleton.h:137
HD_API void SetCounter(TfToken const &name, double value)
Sets the value of a named counter.
HD_API std::vector< HdResourceRegistry * > const & GetResourceRegistryVector()
Returns a vector of resource registry.
HD_API TfTokenVector GetCacheNames()
Returns the names of all cache performance counters.
HD_API double GetCounter(TfToken const &name)
Returns the current value of a named counter.
HD_API void AddCounter(TfToken const &name, double value)
Adds value to a named counter.
HD_API void AddResourceRegistry(HdResourceRegistry *resourceRegistry)
Add a resource registry to the tracking.
Performance counter monitoring.
Definition: perfLog.h:96
HD_API void SubtractCounter(TfToken const &name, double value)
Subtracts value to a named counter.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...
HD_API void IncrementCounter(TfToken const &name)
Increments a named counter by 1.0.