Loading...
Searching...
No Matches
perfLog.h
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"
13#include "pxr/imaging/hd/debugCodes.h"
14
16
19#include "pxr/base/tf/token.h"
20
21#include "pxr/base/tf/hashmap.h"
22
23#include <memory>
24#include <mutex>
25
26PXR_NAMESPACE_OPEN_SCOPE
27
28#if !defined(HD_PERF_ENABLE)
29 #define HD_PERF_ENABLE 1
30#endif
31
32class SdfPath;
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
112{
113public:
114 HD_API
115 static HdPerfLog& GetInstance() {
117 }
118
121 HD_API
122 void AddCacheHit(TfToken const& name,
123 SdfPath const& id,
124 TfToken const& tag=TfToken());
125
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
138 HD_API
139 double GetCacheHitRatio(TfToken const& name);
140
142 HD_API
143 size_t GetCacheHits(TfToken const& name);
144
146 HD_API
147 size_t GetCacheMisses(TfToken const& name);
148
150 HD_API
152
154 HD_API
156
158 HD_API
159 void IncrementCounter(TfToken const& name);
160
162 HD_API
163 void DecrementCounter(TfToken const& name);
164
166 HD_API
167 void SetCounter(TfToken const& name, double value);
168
170 HD_API
171 void AddCounter(TfToken const& name, double value);
172
174 HD_API
175 void SubtractCounter(TfToken const& name, double value);
176
178 HD_API
179 double GetCounter(TfToken const& name);
180
183 HD_API
185
187 void Enable() { _enabled = true; }
188
190 void Disable() { _enabled = false; }
191
193 HD_API
194 void AddResourceRegistry(HdResourceRegistry * resourceRegistry);
195
197 HD_API
199
201 HD_API
202 std::vector<HdResourceRegistry*> const& GetResourceRegistryVector();
203
204private:
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.
235 typedef TfHashMap<TfToken, _CacheEntry, TfToken::HashFunctor> _CacheMap;
236 _CacheMap _cacheMap;
237
238 // Named value counters.
239 typedef TfHashMap<TfToken, double, TfToken::HashFunctor> _CounterMap;
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
251HD_API_TEMPLATE_CLASS(TfSingleton<HdPerfLog>);
252
253PXR_NAMESPACE_CLOSE_SCOPE
254
255#endif // PXR_IMAGING_HD_PERF_LOG_H
Performance counter monitoring.
Definition: perfLog.h:112
HD_API void ResetCounters()
Reset all conter values to 0.0.
void Disable()
Disable performance logging.
Definition: perfLog.h:190
HD_API void DecrementCounter(TfToken const &name)
Decrements a named counter by 1.0.
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.
HD_API void AddCounter(TfToken const &name, double value)
Adds value to a named 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.
HD_API size_t GetCacheMisses(TfToken const &name)
Gets the number of hit misses for a cache performance counter.
HD_API void RemoveResourceRegistry(HdResourceRegistry *resourceRegistry)
Remove Resource Registry from the tracking.
HD_API std::vector< HdResourceRegistry * > const & GetResourceRegistryVector()
Returns a vector of resource registry.
HD_API double GetCounter(TfToken const &name)
Returns the current value of a named counter.
HD_API TfTokenVector GetCounterNames()
Returns a vector of all performance counter names.
void Enable()
Enable performance logging.
Definition: perfLog.h:187
HD_API void SubtractCounter(TfToken const &name, double value)
Subtracts value to a named counter.
HD_API double GetCacheHitRatio(TfToken const &name)
Gets the hit ratio (numHits / totalRequests) of 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 TfTokenVector GetCacheNames()
Returns the names of all cache performance counters.
HD_API void SetCounter(TfToken const &name, double value)
Sets the value of a named counter.
HD_API void IncrementCounter(TfToken const &name)
Increments a named counter by 1.0.
HD_API void AddResourceRegistry(HdResourceRegistry *resourceRegistry)
Add a resource registry to the tracking.
A central registry for resources.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274
Manage a single instance of an object (see.
Definition: singleton.h:105
static T & GetInstance()
Return a reference to an object of type T, creating it if necessary.
Definition: singleton.h:120
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Manage a single instance of an object.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440