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 
92 /// \class HdPerfLog
93 ///
94 /// Performance counter monitoring.
95 ///
96 class HdPerfLog
97 {
98 public:
99     HD_API
GetInstance()100     static HdPerfLog& GetInstance() {
101         return TfSingleton<HdPerfLog>::GetInstance();
102     }
103 
104     /// Tracks a cache hit for the named cache, the id and tag are reported
105     /// when debug logging is enabled.
106     HD_API
107     void AddCacheHit(TfToken const& name,
108                      SdfPath const& id,
109                      TfToken const& tag=TfToken());
110 
111     /// Tracks a cache miss for the named cache, the id and tag are reported
112     /// when debug logging is enabled.
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 
121     /// Gets the hit ratio (numHits / totalRequests) of a cache performance
122     /// counter.
123     HD_API
124     double GetCacheHitRatio(TfToken const& name);
125 
126     /// Gets the number of hit hits for a cache performance counter.
127     HD_API
128     size_t GetCacheHits(TfToken const& name);
129 
130     /// Gets the number of hit misses for a cache performance counter.
131     HD_API
132     size_t GetCacheMisses(TfToken const& name);
133 
134     /// Returns the names of all cache performance counters.
135     HD_API
136     TfTokenVector GetCacheNames();
137 
138     /// Returns a vector of all performance counter names.
139     HD_API
140     TfTokenVector GetCounterNames();
141 
142     /// Increments a named counter by 1.0.
143     HD_API
144     void IncrementCounter(TfToken const& name);
145 
146     /// Decrements a named counter by 1.0.
147     HD_API
148     void DecrementCounter(TfToken const& name);
149 
150     /// Sets the value of a named counter.
151     HD_API
152     void SetCounter(TfToken const& name, double value);
153 
154     /// Adds value to a named counter.
155     HD_API
156     void AddCounter(TfToken const& name, double value);
157 
158     /// Subtracts value to a named counter.
159     HD_API
160     void SubtractCounter(TfToken const& name, double value);
161 
162     /// Returns the current value of a named counter.
163     HD_API
164     double GetCounter(TfToken const& name);
165 
166     /// Reset all conter values to 0.0.
167     /// Note that this doesn't reset cache counters.
168     HD_API
169     void ResetCounters();
170 
171     /// Enable performance logging.
Enable()172     void Enable() { _enabled = true; }
173 
174     /// Disable performance logging.
Disable()175     void Disable() { _enabled = false; }
176 
177     /// Add a resource registry to the tracking.
178     HD_API
179     void AddResourceRegistry(HdResourceRegistry * resourceRegistry);
180 
181     /// Remove Resource Registry from the tracking.
182     HD_API
183     void RemoveResourceRegistry(HdResourceRegistry * resourceRegistry);
184 
185     /// Returns a vector of resource registry.
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:
_CacheEntry()202         _CacheEntry() : _hits(0), _misses(0) { }
203 
AddHit()204         void AddHit() {++_hits;}
GetHits()205         size_t GetHits() {return _hits;}
206 
AddMiss()207         void AddMiss() {++_misses;}
GetMisses()208         size_t GetMisses() {return _misses;}
209 
GetTotal()210         size_t GetTotal() {return _hits+_misses;}
GetHitRatio()211         double GetHitRatio() {return (double)_hits / GetTotal();}
212 
Reset()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
241