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 #include "absl/container/internal/hashtablez_sampler.h"
16
17 #include <algorithm>
18 #include <atomic>
19 #include <cassert>
20 #include <cmath>
21 #include <functional>
22 #include <limits>
23
24 #include "absl/base/attributes.h"
25 #include "absl/base/config.h"
26 #include "absl/base/internal/raw_logging.h"
27 #include "absl/debugging/stacktrace.h"
28 #include "absl/memory/memory.h"
29 #include "absl/profiling/internal/exponential_biased.h"
30 #include "absl/profiling/internal/sample_recorder.h"
31 #include "absl/synchronization/mutex.h"
32 #include "absl/time/clock.h"
33 #include "absl/utility/utility.h"
34
35 namespace absl {
36 ABSL_NAMESPACE_BEGIN
37 namespace container_internal {
38
39 #ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
40 constexpr int HashtablezInfo::kMaxStackDepth;
41 #endif
42
43 namespace {
44 ABSL_CONST_INIT std::atomic<bool> g_hashtablez_enabled{
45 false
46 };
47 ABSL_CONST_INIT std::atomic<int32_t> g_hashtablez_sample_parameter{1 << 10};
48 std::atomic<HashtablezConfigListener> g_hashtablez_config_listener{nullptr};
49
50 #if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
51 ABSL_PER_THREAD_TLS_KEYWORD absl::profiling_internal::ExponentialBiased
52 g_exponential_biased_generator;
53 #endif
54
TriggerHashtablezConfigListener()55 void TriggerHashtablezConfigListener() {
56 auto* listener = g_hashtablez_config_listener.load(std::memory_order_acquire);
57 if (listener != nullptr) listener();
58 }
59
60 } // namespace
61
62 #if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
63 ABSL_PER_THREAD_TLS_KEYWORD SamplingState global_next_sample = {0, 0};
64 #endif // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
65
GlobalHashtablezSampler()66 HashtablezSampler& GlobalHashtablezSampler() {
67 static auto* sampler = new HashtablezSampler();
68 return *sampler;
69 }
70
71 HashtablezInfo::HashtablezInfo() = default;
72 HashtablezInfo::~HashtablezInfo() = default;
73
PrepareForSampling(int64_t stride,size_t inline_element_size_value)74 void HashtablezInfo::PrepareForSampling(int64_t stride,
75 size_t inline_element_size_value) {
76 capacity.store(0, std::memory_order_relaxed);
77 size.store(0, std::memory_order_relaxed);
78 num_erases.store(0, std::memory_order_relaxed);
79 num_rehashes.store(0, std::memory_order_relaxed);
80 max_probe_length.store(0, std::memory_order_relaxed);
81 total_probe_length.store(0, std::memory_order_relaxed);
82 hashes_bitwise_or.store(0, std::memory_order_relaxed);
83 hashes_bitwise_and.store(~size_t{}, std::memory_order_relaxed);
84 hashes_bitwise_xor.store(0, std::memory_order_relaxed);
85 max_reserve.store(0, std::memory_order_relaxed);
86
87 create_time = absl::Now();
88 weight = stride;
89 // The inliner makes hardcoded skip_count difficult (especially when combined
90 // with LTO). We use the ability to exclude stacks by regex when encoding
91 // instead.
92 depth = absl::GetStackTrace(stack, HashtablezInfo::kMaxStackDepth,
93 /* skip_count= */ 0);
94 inline_element_size = inline_element_size_value;
95 }
96
ShouldForceSampling()97 static bool ShouldForceSampling() {
98 enum ForceState {
99 kDontForce,
100 kForce,
101 kUninitialized
102 };
103 ABSL_CONST_INIT static std::atomic<ForceState> global_state{
104 kUninitialized};
105 ForceState state = global_state.load(std::memory_order_relaxed);
106 if (ABSL_PREDICT_TRUE(state == kDontForce)) return false;
107
108 if (state == kUninitialized) {
109 state = ABSL_INTERNAL_C_SYMBOL(AbslContainerInternalSampleEverything)()
110 ? kForce
111 : kDontForce;
112 global_state.store(state, std::memory_order_relaxed);
113 }
114 return state == kForce;
115 }
116
SampleSlow(SamplingState & next_sample,size_t inline_element_size)117 HashtablezInfo* SampleSlow(SamplingState& next_sample,
118 size_t inline_element_size) {
119 if (ABSL_PREDICT_FALSE(ShouldForceSampling())) {
120 next_sample.next_sample = 1;
121 const int64_t old_stride = exchange(next_sample.sample_stride, 1);
122 HashtablezInfo* result =
123 GlobalHashtablezSampler().Register(old_stride, inline_element_size);
124 return result;
125 }
126
127 #if !defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
128 next_sample = {
129 std::numeric_limits<int64_t>::max(),
130 std::numeric_limits<int64_t>::max(),
131 };
132 return nullptr;
133 #else
134 bool first = next_sample.next_sample < 0;
135
136 const int64_t next_stride = g_exponential_biased_generator.GetStride(
137 g_hashtablez_sample_parameter.load(std::memory_order_relaxed));
138
139 next_sample.next_sample = next_stride;
140 const int64_t old_stride = exchange(next_sample.sample_stride, next_stride);
141 // Small values of interval are equivalent to just sampling next time.
142 ABSL_ASSERT(next_stride >= 1);
143
144 // g_hashtablez_enabled can be dynamically flipped, we need to set a threshold
145 // low enough that we will start sampling in a reasonable time, so we just use
146 // the default sampling rate.
147 if (!g_hashtablez_enabled.load(std::memory_order_relaxed)) return nullptr;
148
149 // We will only be negative on our first count, so we should just retry in
150 // that case.
151 if (first) {
152 if (ABSL_PREDICT_TRUE(--next_sample.next_sample > 0)) return nullptr;
153 return SampleSlow(next_sample, inline_element_size);
154 }
155
156 return GlobalHashtablezSampler().Register(old_stride, inline_element_size);
157 #endif
158 }
159
UnsampleSlow(HashtablezInfo * info)160 void UnsampleSlow(HashtablezInfo* info) {
161 GlobalHashtablezSampler().Unregister(info);
162 }
163
RecordRehashSlow(HashtablezInfo * info,size_t total_probe_length)164 void RecordRehashSlow(HashtablezInfo* info, size_t total_probe_length) {
165 #ifdef ABSL_INTERNAL_HAVE_SSE2
166 total_probe_length /= 16;
167 #else
168 total_probe_length /= 8;
169 #endif
170 info->total_probe_length.store(total_probe_length, std::memory_order_relaxed);
171 info->num_erases.store(0, std::memory_order_relaxed);
172 // There is only one concurrent writer, so `load` then `store` is sufficient
173 // instead of using `fetch_add`.
174 info->num_rehashes.store(
175 1 + info->num_rehashes.load(std::memory_order_relaxed),
176 std::memory_order_relaxed);
177 }
178
RecordReservationSlow(HashtablezInfo * info,size_t target_capacity)179 void RecordReservationSlow(HashtablezInfo* info, size_t target_capacity) {
180 info->max_reserve.store(
181 (std::max)(info->max_reserve.load(std::memory_order_relaxed),
182 target_capacity),
183 std::memory_order_relaxed);
184 }
185
RecordClearedReservationSlow(HashtablezInfo * info)186 void RecordClearedReservationSlow(HashtablezInfo* info) {
187 info->max_reserve.store(0, std::memory_order_relaxed);
188 }
189
RecordStorageChangedSlow(HashtablezInfo * info,size_t size,size_t capacity)190 void RecordStorageChangedSlow(HashtablezInfo* info, size_t size,
191 size_t capacity) {
192 info->size.store(size, std::memory_order_relaxed);
193 info->capacity.store(capacity, std::memory_order_relaxed);
194 if (size == 0) {
195 // This is a clear, reset the total/num_erases too.
196 info->total_probe_length.store(0, std::memory_order_relaxed);
197 info->num_erases.store(0, std::memory_order_relaxed);
198 }
199 }
200
RecordInsertSlow(HashtablezInfo * info,size_t hash,size_t distance_from_desired)201 void RecordInsertSlow(HashtablezInfo* info, size_t hash,
202 size_t distance_from_desired) {
203 // SwissTables probe in groups of 16, so scale this to count items probes and
204 // not offset from desired.
205 size_t probe_length = distance_from_desired;
206 #ifdef ABSL_INTERNAL_HAVE_SSE2
207 probe_length /= 16;
208 #else
209 probe_length /= 8;
210 #endif
211
212 info->hashes_bitwise_and.fetch_and(hash, std::memory_order_relaxed);
213 info->hashes_bitwise_or.fetch_or(hash, std::memory_order_relaxed);
214 info->hashes_bitwise_xor.fetch_xor(hash, std::memory_order_relaxed);
215 info->max_probe_length.store(
216 std::max(info->max_probe_length.load(std::memory_order_relaxed),
217 probe_length),
218 std::memory_order_relaxed);
219 info->total_probe_length.fetch_add(probe_length, std::memory_order_relaxed);
220 info->size.fetch_add(1, std::memory_order_relaxed);
221 }
222
RecordEraseSlow(HashtablezInfo * info)223 void RecordEraseSlow(HashtablezInfo* info) {
224 info->size.fetch_sub(1, std::memory_order_relaxed);
225 // There is only one concurrent writer, so `load` then `store` is sufficient
226 // instead of using `fetch_add`.
227 info->num_erases.store(1 + info->num_erases.load(std::memory_order_relaxed),
228 std::memory_order_relaxed);
229 }
230
SetHashtablezConfigListener(HashtablezConfigListener l)231 void SetHashtablezConfigListener(HashtablezConfigListener l) {
232 g_hashtablez_config_listener.store(l, std::memory_order_release);
233 }
234
IsHashtablezEnabled()235 bool IsHashtablezEnabled() {
236 return g_hashtablez_enabled.load(std::memory_order_acquire);
237 }
238
SetHashtablezEnabled(bool enabled)239 void SetHashtablezEnabled(bool enabled) {
240 SetHashtablezEnabledInternal(enabled);
241 TriggerHashtablezConfigListener();
242 }
243
SetHashtablezEnabledInternal(bool enabled)244 void SetHashtablezEnabledInternal(bool enabled) {
245 g_hashtablez_enabled.store(enabled, std::memory_order_release);
246 }
247
GetHashtablezSampleParameter()248 int32_t GetHashtablezSampleParameter() {
249 return g_hashtablez_sample_parameter.load(std::memory_order_acquire);
250 }
251
SetHashtablezSampleParameter(int32_t rate)252 void SetHashtablezSampleParameter(int32_t rate) {
253 SetHashtablezSampleParameterInternal(rate);
254 TriggerHashtablezConfigListener();
255 }
256
SetHashtablezSampleParameterInternal(int32_t rate)257 void SetHashtablezSampleParameterInternal(int32_t rate) {
258 if (rate > 0) {
259 g_hashtablez_sample_parameter.store(rate, std::memory_order_release);
260 } else {
261 ABSL_RAW_LOG(ERROR, "Invalid hashtablez sample rate: %lld",
262 static_cast<long long>(rate)); // NOLINT(runtime/int)
263 }
264 }
265
GetHashtablezMaxSamples()266 size_t GetHashtablezMaxSamples() {
267 return GlobalHashtablezSampler().GetMaxSamples();
268 }
269
SetHashtablezMaxSamples(size_t max)270 void SetHashtablezMaxSamples(size_t max) {
271 SetHashtablezMaxSamplesInternal(max);
272 TriggerHashtablezConfigListener();
273 }
274
SetHashtablezMaxSamplesInternal(size_t max)275 void SetHashtablezMaxSamplesInternal(size_t max) {
276 if (max > 0) {
277 GlobalHashtablezSampler().SetMaxSamples(max);
278 } else {
279 ABSL_RAW_LOG(ERROR, "Invalid hashtablez max samples: 0");
280 }
281 }
282
283 } // namespace container_internal
284 ABSL_NAMESPACE_END
285 } // namespace absl
286