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