1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // -----------------------------------------------------------------------------
16 // File: hashtablez_sampler.h
17 // -----------------------------------------------------------------------------
18 //
19 // This header file defines the API for a low level library to sample hashtables
20 // and collect runtime statistics about them.
21 //
22 // `HashtablezSampler` controls the lifecycle of `HashtablezInfo` objects which
23 // store information about a single sample.
24 //
25 // `Record*` methods store information into samples.
26 // `Sample()` and `Unsample()` make use of a single global sampler with
27 // properties controlled by the flags hashtablez_enabled,
28 // hashtablez_sample_rate, and hashtablez_max_samples.
29 //
30 // WARNING
31 //
32 // Using this sampling API may cause sampled Swiss tables to use the global
33 // allocator (operator `new`) in addition to any custom allocator.  If you
34 // are using a table in an unusual circumstance where allocation or calling a
35 // linux syscall is unacceptable, this could interfere.
36 //
37 // This utility is internal-only. Use at your own risk.
38 
39 #ifndef ABSL_CONTAINER_INTERNAL_HASHTABLEZ_SAMPLER_H_
40 #define ABSL_CONTAINER_INTERNAL_HASHTABLEZ_SAMPLER_H_
41 
42 #include <atomic>
43 #include <functional>
44 #include <memory>
45 #include <vector>
46 
47 #include "absl/base/config.h"
48 #include "absl/base/internal/per_thread_tls.h"
49 #include "absl/base/optimization.h"
50 #include "absl/profiling/internal/sample_recorder.h"
51 #include "absl/synchronization/mutex.h"
52 #include "absl/utility/utility.h"
53 
54 namespace absl {
55 ABSL_NAMESPACE_BEGIN
56 namespace container_internal {
57 
58 // Stores information about a sampled hashtable.  All mutations to this *must*
59 // be made through `Record*` functions below.  All reads from this *must* only
60 // occur in the callback to `HashtablezSampler::Iterate`.
61 struct HashtablezInfo : public profiling_internal::Sample<HashtablezInfo> {
62   // Constructs the object but does not fill in any fields.
63   HashtablezInfo();
64   ~HashtablezInfo();
65   HashtablezInfo(const HashtablezInfo&) = delete;
66   HashtablezInfo& operator=(const HashtablezInfo&) = delete;
67 
68   // Puts the object into a clean state, fills in the logically `const` members,
69   // blocking for any readers that are currently sampling the object.
70   void PrepareForSampling(int64_t stride, size_t inline_element_size_value)
71       ABSL_EXCLUSIVE_LOCKS_REQUIRED(init_mu);
72 
73   // These fields are mutated by the various Record* APIs and need to be
74   // thread-safe.
75   std::atomic<size_t> capacity;
76   std::atomic<size_t> size;
77   std::atomic<size_t> num_erases;
78   std::atomic<size_t> num_rehashes;
79   std::atomic<size_t> max_probe_length;
80   std::atomic<size_t> total_probe_length;
81   std::atomic<size_t> hashes_bitwise_or;
82   std::atomic<size_t> hashes_bitwise_and;
83   std::atomic<size_t> hashes_bitwise_xor;
84   std::atomic<size_t> max_reserve;
85 
86   // All of the fields below are set by `PrepareForSampling`, they must not be
87   // mutated in `Record*` functions.  They are logically `const` in that sense.
88   // These are guarded by init_mu, but that is not externalized to clients,
89   // which can read them only during `SampleRecorder::Iterate` which will hold
90   // the lock.
91   static constexpr int kMaxStackDepth = 64;
92   absl::Time create_time;
93   int32_t depth;
94   void* stack[kMaxStackDepth];
95   size_t inline_element_size;  // How big is the slot?
96 };
97 
98 void RecordRehashSlow(HashtablezInfo* info, size_t total_probe_length);
99 
100 void RecordReservationSlow(HashtablezInfo* info, size_t target_capacity);
101 
102 void RecordClearedReservationSlow(HashtablezInfo* info);
103 
104 void RecordStorageChangedSlow(HashtablezInfo* info, size_t size,
105                               size_t capacity);
106 
107 void RecordInsertSlow(HashtablezInfo* info, size_t hash,
108                       size_t distance_from_desired);
109 
110 void RecordEraseSlow(HashtablezInfo* info);
111 
112 struct SamplingState {
113   int64_t next_sample;
114   // When we make a sampling decision, we record that distance so we can weight
115   // each sample.
116   int64_t sample_stride;
117 };
118 
119 HashtablezInfo* SampleSlow(SamplingState& next_sample,
120                            size_t inline_element_size);
121 void UnsampleSlow(HashtablezInfo* info);
122 
123 #if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
124 #error ABSL_INTERNAL_HASHTABLEZ_SAMPLE cannot be directly set
125 #endif  // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
126 
127 #if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
128 class HashtablezInfoHandle {
129  public:
HashtablezInfoHandle()130   explicit HashtablezInfoHandle() : info_(nullptr) {}
HashtablezInfoHandle(HashtablezInfo * info)131   explicit HashtablezInfoHandle(HashtablezInfo* info) : info_(info) {}
132 
133   // We do not have a destructor. Caller is responsible for calling Unregister
134   // before destroying the handle.
Unregister()135   void Unregister() {
136     if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
137     UnsampleSlow(info_);
138   }
139 
140   HashtablezInfoHandle(const HashtablezInfoHandle&) = delete;
141   HashtablezInfoHandle& operator=(const HashtablezInfoHandle&) = delete;
142 
HashtablezInfoHandle(HashtablezInfoHandle && o)143   HashtablezInfoHandle(HashtablezInfoHandle&& o) noexcept
144       : info_(absl::exchange(o.info_, nullptr)) {}
145   HashtablezInfoHandle& operator=(HashtablezInfoHandle&& o) noexcept {
146     if (ABSL_PREDICT_FALSE(info_ != nullptr)) {
147       UnsampleSlow(info_);
148     }
149     info_ = absl::exchange(o.info_, nullptr);
150     return *this;
151   }
152 
RecordStorageChanged(size_t size,size_t capacity)153   inline void RecordStorageChanged(size_t size, size_t capacity) {
154     if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
155     RecordStorageChangedSlow(info_, size, capacity);
156   }
157 
RecordRehash(size_t total_probe_length)158   inline void RecordRehash(size_t total_probe_length) {
159     if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
160     RecordRehashSlow(info_, total_probe_length);
161   }
162 
RecordReservation(size_t target_capacity)163   inline void RecordReservation(size_t target_capacity) {
164     if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
165     RecordReservationSlow(info_, target_capacity);
166   }
167 
RecordClearedReservation()168   inline void RecordClearedReservation() {
169     if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
170     RecordClearedReservationSlow(info_);
171   }
172 
RecordInsert(size_t hash,size_t distance_from_desired)173   inline void RecordInsert(size_t hash, size_t distance_from_desired) {
174     if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
175     RecordInsertSlow(info_, hash, distance_from_desired);
176   }
177 
RecordErase()178   inline void RecordErase() {
179     if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
180     RecordEraseSlow(info_);
181   }
182 
swap(HashtablezInfoHandle & lhs,HashtablezInfoHandle & rhs)183   friend inline void swap(HashtablezInfoHandle& lhs,
184                           HashtablezInfoHandle& rhs) {
185     std::swap(lhs.info_, rhs.info_);
186   }
187 
188  private:
189   friend class HashtablezInfoHandlePeer;
190   HashtablezInfo* info_;
191 };
192 #else
193 // Ensure that when Hashtablez is turned off at compile time, HashtablezInfo can
194 // be removed by the linker, in order to reduce the binary size.
195 class HashtablezInfoHandle {
196  public:
197   explicit HashtablezInfoHandle() = default;
HashtablezInfoHandle(std::nullptr_t)198   explicit HashtablezInfoHandle(std::nullptr_t) {}
199 
Unregister()200   inline void Unregister() {}
RecordStorageChanged(size_t,size_t)201   inline void RecordStorageChanged(size_t /*size*/, size_t /*capacity*/) {}
RecordRehash(size_t)202   inline void RecordRehash(size_t /*total_probe_length*/) {}
RecordReservation(size_t)203   inline void RecordReservation(size_t /*target_capacity*/) {}
RecordClearedReservation()204   inline void RecordClearedReservation() {}
RecordInsert(size_t,size_t)205   inline void RecordInsert(size_t /*hash*/, size_t /*distance_from_desired*/) {}
RecordErase()206   inline void RecordErase() {}
207 
swap(HashtablezInfoHandle &,HashtablezInfoHandle &)208   friend inline void swap(HashtablezInfoHandle& /*lhs*/,
209                           HashtablezInfoHandle& /*rhs*/) {}
210 };
211 #endif  // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
212 
213 #if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
214 extern ABSL_PER_THREAD_TLS_KEYWORD SamplingState global_next_sample;
215 #endif  // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
216 
217 // Returns an RAII sampling handle that manages registration and unregistation
218 // with the global sampler.
Sample(size_t inline_element_size ABSL_ATTRIBUTE_UNUSED)219 inline HashtablezInfoHandle Sample(
220     size_t inline_element_size ABSL_ATTRIBUTE_UNUSED) {
221 #if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
222   if (ABSL_PREDICT_TRUE(--global_next_sample.next_sample > 0)) {
223     return HashtablezInfoHandle(nullptr);
224   }
225   return HashtablezInfoHandle(
226       SampleSlow(global_next_sample, inline_element_size));
227 #else
228   return HashtablezInfoHandle(nullptr);
229 #endif  // !ABSL_PER_THREAD_TLS
230 }
231 
232 using HashtablezSampler =
233     ::absl::profiling_internal::SampleRecorder<HashtablezInfo>;
234 
235 // Returns a global Sampler.
236 HashtablezSampler& GlobalHashtablezSampler();
237 
238 using HashtablezConfigListener = void (*)();
239 void SetHashtablezConfigListener(HashtablezConfigListener l);
240 
241 // Enables or disables sampling for Swiss tables.
242 bool IsHashtablezEnabled();
243 void SetHashtablezEnabled(bool enabled);
244 void SetHashtablezEnabledInternal(bool enabled);
245 
246 // Sets the rate at which Swiss tables will be sampled.
247 int32_t GetHashtablezSampleParameter();
248 void SetHashtablezSampleParameter(int32_t rate);
249 void SetHashtablezSampleParameterInternal(int32_t rate);
250 
251 // Sets a soft max for the number of samples that will be kept.
252 size_t GetHashtablezMaxSamples();
253 void SetHashtablezMaxSamples(size_t max);
254 void SetHashtablezMaxSamplesInternal(size_t max);
255 
256 // Configuration override.
257 // This allows process-wide sampling without depending on order of
258 // initialization of static storage duration objects.
259 // The definition of this constant is weak, which allows us to inject a
260 // different value for it at link time.
261 extern "C" bool ABSL_INTERNAL_C_SYMBOL(AbslContainerInternalSampleEverything)();
262 
263 }  // namespace container_internal
264 ABSL_NAMESPACE_END
265 }  // namespace absl
266 
267 #endif  // ABSL_CONTAINER_INTERNAL_HASHTABLEZ_SAMPLER_H_
268