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 HD_PERF_H
25 #define HD_PERF_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 <boost/noncopyable.hpp>
39 #include <boost/shared_ptr.hpp>
40 #include "pxr/base/tf/hashmap.h"
41 
42 #include <mutex>
43 
44 PXR_NAMESPACE_OPEN_SCOPE
45 
46 
47 class SdfPath;
48 typedef boost::shared_ptr<class HdResourceRegistry> HdResourceRegistrySharedPtr;
49 
50 // XXX: it would be nice to move this into Trace or use the existing Trace
51 // counter mechanism, however we are restricted to TraceLite in the rocks.
52 
53 //----------------------------------------------------------------------------//
54 // PERFORMANCE INSTURMENTATION MACROS //
55 //----------------------------------------------------------------------------//
56 
57 // Emits a trace scope tagged for the function.
58 #define HD_TRACE_FUNCTION() TRACE_FUNCTION()
59 // Emits a trace scope with the specified tag.
60 #define HD_TRACE_SCOPE(tag) TRACE_SCOPE(tag)
61 
62 // Adds a cache hit for the given cache name, the id is provided for debugging,
63 // see HdPerfLog for details.
64 #define HD_PERF_CACHE_HIT(name, id) \
65  HdPerfLog::GetInstance().AddCacheHit(name, id);
66 #define HD_PERF_CACHE_HIT_TAG(name, id, tag) \
67  HdPerfLog::GetInstance().AddCacheHit(name, id, tag);
68 
69 // Adds a cache miss for the given cache name, the id is provided for debugging,
70 // see HdPerfLog for details.
71 #define HD_PERF_CACHE_MISS(name, id) \
72  HdPerfLog::GetInstance().AddCacheMiss(name, id);
73 #define HD_PERF_CACHE_MISS_TAG(name, id, tag) \
74  HdPerfLog::GetInstance().AddCacheMiss(name, id, tag);
75 
76 // Increments/Decrements/Sets/Adds/Subtracts a named performance counter
77 // see HdPerfLog for details.
78 #define HD_PERF_COUNTER_INCR(name) \
79  HdPerfLog::GetInstance().IncrementCounter(name);
80 #define HD_PERF_COUNTER_DECR(name) \
81  HdPerfLog::GetInstance().DecrementCounter(name);
82 #define HD_PERF_COUNTER_SET(name, value) \
83  HdPerfLog::GetInstance().SetCounter(name, value);
84 #define HD_PERF_COUNTER_ADD(name, value) \
85  HdPerfLog::GetInstance().AddCounter(name, value);
86 #define HD_PERF_COUNTER_SUBTRACT(name, value) \
87  HdPerfLog::GetInstance().SubtractCounter(name, value);
88 
89 //----------------------------------------------------------------------------//
90 // PERFORMANCE LOG //
91 //----------------------------------------------------------------------------//
92 
97 class HdPerfLog : public boost::noncopyable {
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(
180  HdResourceRegistrySharedPtr const &resourceRegistry);
181 
183  HD_API
185  HdResourceRegistrySharedPtr const &resourceRegistry);
186 
188  HD_API
189  std::vector<HdResourceRegistrySharedPtr> const& GetResourceRegistryVector();
190 
191 private:
192  friend class TfSingleton<HdPerfLog>;
193  HD_API HdPerfLog();
194  HD_API ~HdPerfLog();
195 
196  // Tracks number of hits and misses and provides some convenience API.
197  class _CacheEntry {
198  public:
199  _CacheEntry() : _hits(0), _misses(0) { }
200 
201  void AddHit() {++_hits;}
202  size_t GetHits() {return _hits;}
203 
204  void AddMiss() {++_misses;}
205  size_t GetMisses() {return _misses;}
206 
207  size_t GetTotal() {return _hits+_misses;}
208  double GetHitRatio() {return (double)_hits / GetTotal();}
209 
210  void Reset() { _hits = 0; _misses = 0; }
211  private:
212  size_t _hits;
213  size_t _misses;
214  };
215 
216  // Cache performance counters.
217  typedef TfHashMap<TfToken, _CacheEntry, TfToken::HashFunctor> _CacheMap;
218  _CacheMap _cacheMap;
219 
220  // Named value counters.
221  typedef TfHashMap<TfToken, double, TfToken::HashFunctor> _CounterMap;
222  _CounterMap _counterMap;
223 
224  // Resource registry vector.
225  std::vector<HdResourceRegistrySharedPtr> _resourceRegistryVector;
226 
227  // Enable / disable performance tracking.
228  bool _enabled;
229  std::mutex _mutex;
230  typedef std::lock_guard<std::mutex> _Lock;
231 };
232 
233 HD_API_TEMPLATE_CLASS(TfSingleton<HdPerfLog>);
234 
235 PXR_NAMESPACE_CLOSE_SCOPE
236 
237 #endif // HD_PERF_H
HD_API void AddResourceRegistry(HdResourceRegistrySharedPtr const &resourceRegistry)
Add a resource registry to the tracking.
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 (see.
Definition: singleton.h:122
HD_API void DecrementCounter(TfToken const &name)
Decrements a named counter by 1.0.
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:89
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.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:438
HD_API void ResetCounters()
Reset all conter values to 0.0.
HD_API std::vector< HdResourceRegistrySharedPtr > const & GetResourceRegistryVector()
Returns a vector of resource registry.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
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 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 RemoveResourceRegistry(HdResourceRegistrySharedPtr const &resourceRegistry)
Remove Resource Registry from the tracking.
Performance counter monitoring.
Definition: perfLog.h:97
HD_API void SubtractCounter(TfToken const &name, double value)
Subtracts value to a named counter.
HD_API void IncrementCounter(TfToken const &name)
Increments a named counter by 1.0.