Coverage Report

Created: 2025-11-24 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/container/internal/hashtablez_sampler.h
Line
Count
Source
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 <cstddef>
44
#include <cstdint>
45
#include <functional>
46
#include <memory>
47
#include <vector>
48
49
#include "absl/base/attributes.h"
50
#include "absl/base/config.h"
51
#include "absl/base/internal/per_thread_tls.h"
52
#include "absl/base/optimization.h"
53
#include "absl/base/thread_annotations.h"
54
#include "absl/profiling/internal/sample_recorder.h"
55
#include "absl/synchronization/mutex.h"
56
#include "absl/time/time.h"
57
#include "absl/utility/utility.h"
58
59
namespace absl {
60
ABSL_NAMESPACE_BEGIN
61
namespace container_internal {
62
63
// Stores information about a sampled hashtable.  All mutations to this *must*
64
// be made through `Record*` functions below.  All reads from this *must* only
65
// occur in the callback to `HashtablezSampler::Iterate`.
66
struct HashtablezInfo : public profiling_internal::Sample<HashtablezInfo> {
67
  // Constructs the object but does not fill in any fields.
68
  HashtablezInfo();
69
  ~HashtablezInfo();
70
  HashtablezInfo(const HashtablezInfo&) = delete;
71
  HashtablezInfo& operator=(const HashtablezInfo&) = delete;
72
73
  // Puts the object into a clean state, fills in the logically `const` members,
74
  // blocking for any readers that are currently sampling the object.
75
  void PrepareForSampling(int64_t stride, size_t inline_element_size_value,
76
                          size_t key_size, size_t value_size,
77
                          uint16_t soo_capacity_value)
78
      ABSL_EXCLUSIVE_LOCKS_REQUIRED(init_mu);
79
80
  // These fields are mutated by the various Record* APIs and need to be
81
  // thread-safe.
82
  std::atomic<size_t> capacity;
83
  std::atomic<size_t> size;
84
  std::atomic<size_t> num_erases;
85
  std::atomic<size_t> num_insert_hits;
86
  std::atomic<size_t> num_rehashes;
87
  std::atomic<size_t> max_probe_length;
88
  std::atomic<size_t> total_probe_length;
89
  std::atomic<size_t> hashes_bitwise_or;
90
  std::atomic<size_t> hashes_bitwise_and;
91
  std::atomic<size_t> max_reserve;
92
93
  // All of the fields below are set by `PrepareForSampling`, they must not be
94
  // mutated in `Record*` functions.  They are logically `const` in that sense.
95
  // These are guarded by init_mu, but that is not externalized to clients,
96
  // which can read them only during `SampleRecorder::Iterate` which will hold
97
  // the lock.
98
  static constexpr int kMaxStackDepth = 64;
99
  absl::Time create_time;
100
  uint32_t depth;
101
  // The SOO capacity for this table in elements (not bytes). Note that sampled
102
  // tables are never SOO because we need to store the infoz handle on the heap.
103
  // Tables that would be SOO if not sampled should have: soo_capacity > 0 &&
104
  // size <= soo_capacity && max_reserve <= soo_capacity.
105
  uint16_t soo_capacity;
106
  void* stack[kMaxStackDepth];
107
  size_t inline_element_size;  // How big is the slot in bytes?
108
  size_t key_size;             // sizeof(key_type)
109
  size_t value_size;           // sizeof(value_type)
110
};
111
112
void RecordRehashSlow(HashtablezInfo* info, size_t total_probe_length);
113
114
// This is inline to avoid calling convention overhead for an otherwise
115
// lightweight operation.
116
0
inline void RecordInsertHitSlow(HashtablezInfo* info) {
117
0
  // We avoid fetch_add since no other thread should be mutating the table
118
0
  // simultaneously without synchronization.
119
0
  info->num_insert_hits.store(
120
0
      info->num_insert_hits.load(std::memory_order_relaxed) + 1,
121
0
      std::memory_order_relaxed);
122
0
}
123
124
void RecordReservationSlow(HashtablezInfo* info, size_t target_capacity);
125
126
void RecordClearedReservationSlow(HashtablezInfo* info);
127
128
void RecordStorageChangedSlow(HashtablezInfo* info, size_t size,
129
                              size_t capacity);
130
131
void RecordInsertMissSlow(HashtablezInfo* info, size_t hash,
132
                          size_t distance_from_desired);
133
134
void RecordEraseSlow(HashtablezInfo* info);
135
136
struct SamplingState {
137
  int64_t next_sample;
138
  // When we make a sampling decision, we record that distance so we can weight
139
  // each sample.
140
  int64_t sample_stride;
141
};
142
143
HashtablezInfo* SampleSlow(SamplingState& next_sample,
144
                           size_t inline_element_size, size_t key_size,
145
                           size_t value_size, uint16_t soo_capacity);
146
void UnsampleSlow(HashtablezInfo* info);
147
148
#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
149
#error ABSL_INTERNAL_HASHTABLEZ_SAMPLE cannot be directly set
150
#endif  // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
151
152
#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
153
class HashtablezInfoHandle {
154
 public:
155
  explicit HashtablezInfoHandle() : info_(nullptr) {}
156
  explicit HashtablezInfoHandle(HashtablezInfo* info) : info_(info) {}
157
158
  // We do not have a destructor. Caller is responsible for calling Unregister
159
  // before destroying the handle.
160
  void Unregister() {
161
    if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
162
    UnsampleSlow(info_);
163
  }
164
165
  inline bool IsSampled() const { return ABSL_PREDICT_FALSE(info_ != nullptr); }
166
167
  inline void RecordStorageChanged(size_t size, size_t capacity) {
168
    if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
169
    RecordStorageChangedSlow(info_, size, capacity);
170
  }
171
172
  inline void RecordRehash(size_t total_probe_length) {
173
    if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
174
    RecordRehashSlow(info_, total_probe_length);
175
  }
176
177
  inline void RecordReservation(size_t target_capacity) {
178
    if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
179
    RecordReservationSlow(info_, target_capacity);
180
  }
181
182
  inline void RecordClearedReservation() {
183
    if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
184
    RecordClearedReservationSlow(info_);
185
  }
186
187
  inline void RecordInsertMiss(size_t hash, size_t distance_from_desired) {
188
    if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
189
    RecordInsertMissSlow(info_, hash, distance_from_desired);
190
  }
191
192
  inline void RecordErase() {
193
    if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
194
    RecordEraseSlow(info_);
195
  }
196
197
  inline void RecordInsertHit() {
198
    if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
199
    RecordInsertHitSlow(info_);
200
  }
201
202
  friend inline void swap(HashtablezInfoHandle& lhs,
203
                          HashtablezInfoHandle& rhs) {
204
    std::swap(lhs.info_, rhs.info_);
205
  }
206
207
 private:
208
  friend class HashtablezInfoHandlePeer;
209
  HashtablezInfo* info_;
210
};
211
#else
212
// Ensure that when Hashtablez is turned off at compile time, HashtablezInfo can
213
// be removed by the linker, in order to reduce the binary size.
214
class HashtablezInfoHandle {
215
 public:
216
  explicit HashtablezInfoHandle() = default;
217
0
  explicit HashtablezInfoHandle(std::nullptr_t) {}
218
219
0
  inline void Unregister() {}
220
8
  inline bool IsSampled() const { return false; }
221
0
  inline void RecordStorageChanged(size_t /*size*/, size_t /*capacity*/) {}
222
0
  inline void RecordRehash(size_t /*total_probe_length*/) {}
223
0
  inline void RecordReservation(size_t /*target_capacity*/) {}
224
0
  inline void RecordClearedReservation() {}
225
  inline void RecordInsertMiss(size_t /*hash*/,
226
8
                               size_t /*distance_from_desired*/) {}
227
0
  inline void RecordErase() {}
228
0
  inline void RecordInsertHit() {}
229
230
  friend inline void swap(HashtablezInfoHandle& /*lhs*/,
231
0
                          HashtablezInfoHandle& /*rhs*/) {}
232
};
233
#endif  // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
234
235
#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
236
extern ABSL_PER_THREAD_TLS_KEYWORD SamplingState global_next_sample;
237
#endif  // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
238
239
// Returns true if the next table should be sampled.
240
// This function updates the global state.
241
// If the function returns true, actual sampling should be done by calling
242
// ForcedTrySample().
243
2
inline bool ShouldSampleNextTable() {
244
#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
245
  if (ABSL_PREDICT_TRUE(--global_next_sample.next_sample > 0)) {
246
    return false;
247
  }
248
  return true;
249
#else
250
2
  return false;
251
2
#endif  // ABSL_INTERNAL_HASHTABLEZ_SAMPLE
252
2
}
253
254
// Returns a sampling handle.
255
// Must be called only if HashSetShouldBeSampled() returned true.
256
// Returned handle still can be unsampled if sampling is not possible.
257
HashtablezInfoHandle ForcedTrySample(size_t inline_element_size,
258
                                     size_t key_size, size_t value_size,
259
                                     uint16_t soo_capacity);
260
261
// In case sampling needs to be disabled and re-enabled in tests, this function
262
// can be used to reset the sampling state for the current thread.
263
// It is useful to avoid sampling attempts and sampling delays in tests.
264
void TestOnlyRefreshSamplingStateForCurrentThread();
265
266
// Returns a sampling handle.
267
inline HashtablezInfoHandle Sample(size_t inline_element_size, size_t key_size,
268
0
                                   size_t value_size, uint16_t soo_capacity) {
269
0
  if (ABSL_PREDICT_TRUE(!ShouldSampleNextTable())) {
270
0
    return HashtablezInfoHandle(nullptr);
271
0
  }
272
0
  return ForcedTrySample(inline_element_size, key_size, value_size,
273
0
                         soo_capacity);
274
0
}
275
276
using HashtablezSampler =
277
    ::absl::profiling_internal::SampleRecorder<HashtablezInfo>;
278
279
// Returns a global Sampler.
280
HashtablezSampler& GlobalHashtablezSampler();
281
282
using HashtablezConfigListener = void (*)();
283
void SetHashtablezConfigListener(HashtablezConfigListener l);
284
285
// Enables or disables sampling for Swiss tables.
286
bool IsHashtablezEnabled();
287
void SetHashtablezEnabled(bool enabled);
288
void SetHashtablezEnabledInternal(bool enabled);
289
290
// Sets the rate at which Swiss tables will be sampled.
291
int32_t GetHashtablezSampleParameter();
292
void SetHashtablezSampleParameter(int32_t rate);
293
void SetHashtablezSampleParameterInternal(int32_t rate);
294
295
// Sets a soft max for the number of samples that will be kept.
296
size_t GetHashtablezMaxSamples();
297
void SetHashtablezMaxSamples(size_t max);
298
void SetHashtablezMaxSamplesInternal(size_t max);
299
300
// Configuration override.
301
// This allows process-wide sampling without depending on order of
302
// initialization of static storage duration objects.
303
// The definition of this constant is weak, which allows us to inject a
304
// different value for it at link time.
305
extern "C" bool ABSL_INTERNAL_C_SYMBOL(AbslContainerInternalSampleEverything)();
306
307
}  // namespace container_internal
308
ABSL_NAMESPACE_END
309
}  // namespace absl
310
311
#endif  // ABSL_CONTAINER_INTERNAL_HASHTABLEZ_SAMPLER_H_