Commit 1980d7b9 by Evan Brown Committed by Copybara-Service

Do hashtablez sampling on the first insertion into an empty SOO hashtable.

When sampling triggers, we skip SOO and allocate a backing array. We must do this because the HashtablezInfoHandle is part of the heap allocation (along with the control bytes and slots). By default, we sample 1 in ~1024 hashtables when sampling is enabled. This will impact performance because (a) we won't benefit from SOO so we would have worse data locality (more cache/TLB misses), and (b) the backing array capacity will be 3 instead of 1 so (b.1) we skip the rehash after the second insertion and (b.2) we potentially waste up to two slots worth of memory.

We also add an soo_capacity field to HashtablezInfo to allow for distinguishing which sampled tables may otherwise have been SOO - this will allow us to know approximately what fraction of tables are in SOO mode.

PiperOrigin-RevId: 617252334
Change-Id: Ib48b7a4870bd74ea3ba923ed8f350a3b75dbb7d3
parent 43c36ffa
...@@ -701,8 +701,10 @@ cc_test( ...@@ -701,8 +701,10 @@ cc_test(
"//absl/base:config", "//absl/base:config",
"//absl/base:core_headers", "//absl/base:core_headers",
"//absl/base:prefetch", "//absl/base:prefetch",
"//absl/functional:function_ref",
"//absl/hash", "//absl/hash",
"//absl/log", "//absl/log",
"//absl/log:check",
"//absl/memory", "//absl/memory",
"//absl/meta:type_traits", "//absl/meta:type_traits",
"//absl/strings", "//absl/strings",
......
...@@ -753,11 +753,13 @@ absl_cc_test( ...@@ -753,11 +753,13 @@ absl_cc_test(
${ABSL_TEST_COPTS} ${ABSL_TEST_COPTS}
DEPS DEPS
absl::base absl::base
absl::check
absl::config absl::config
absl::container_memory absl::container_memory
absl::core_headers absl::core_headers
absl::flat_hash_map absl::flat_hash_map
absl::flat_hash_set absl::flat_hash_set
absl::function_ref
absl::hash absl::hash
absl::hash_function_defaults absl::hash_function_defaults
absl::hash_policy_testing absl::hash_policy_testing
......
...@@ -18,13 +18,18 @@ ...@@ -18,13 +18,18 @@
#include <atomic> #include <atomic>
#include <cassert> #include <cassert>
#include <cmath> #include <cmath>
#include <cstddef>
#include <cstdint>
#include <functional> #include <functional>
#include <limits> #include <limits>
#include "absl/base/attributes.h" #include "absl/base/attributes.h"
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/base/internal/per_thread_tls.h"
#include "absl/base/internal/raw_logging.h" #include "absl/base/internal/raw_logging.h"
#include "absl/base/macros.h"
#include "absl/base/no_destructor.h" #include "absl/base/no_destructor.h"
#include "absl/base/optimization.h"
#include "absl/debugging/stacktrace.h" #include "absl/debugging/stacktrace.h"
#include "absl/memory/memory.h" #include "absl/memory/memory.h"
#include "absl/profiling/internal/exponential_biased.h" #include "absl/profiling/internal/exponential_biased.h"
...@@ -73,7 +78,8 @@ HashtablezInfo::HashtablezInfo() = default; ...@@ -73,7 +78,8 @@ HashtablezInfo::HashtablezInfo() = default;
HashtablezInfo::~HashtablezInfo() = default; HashtablezInfo::~HashtablezInfo() = default;
void HashtablezInfo::PrepareForSampling(int64_t stride, void HashtablezInfo::PrepareForSampling(int64_t stride,
size_t inline_element_size_value) { size_t inline_element_size_value,
uint16_t soo_capacity_value) {
capacity.store(0, std::memory_order_relaxed); capacity.store(0, std::memory_order_relaxed);
size.store(0, std::memory_order_relaxed); size.store(0, std::memory_order_relaxed);
num_erases.store(0, std::memory_order_relaxed); num_erases.store(0, std::memory_order_relaxed);
...@@ -93,6 +99,7 @@ void HashtablezInfo::PrepareForSampling(int64_t stride, ...@@ -93,6 +99,7 @@ void HashtablezInfo::PrepareForSampling(int64_t stride,
depth = absl::GetStackTrace(stack, HashtablezInfo::kMaxStackDepth, depth = absl::GetStackTrace(stack, HashtablezInfo::kMaxStackDepth,
/* skip_count= */ 0); /* skip_count= */ 0);
inline_element_size = inline_element_size_value; inline_element_size = inline_element_size_value;
soo_capacity = soo_capacity_value;
} }
static bool ShouldForceSampling() { static bool ShouldForceSampling() {
...@@ -116,12 +123,12 @@ static bool ShouldForceSampling() { ...@@ -116,12 +123,12 @@ static bool ShouldForceSampling() {
} }
HashtablezInfo* SampleSlow(SamplingState& next_sample, HashtablezInfo* SampleSlow(SamplingState& next_sample,
size_t inline_element_size) { size_t inline_element_size, uint16_t soo_capacity) {
if (ABSL_PREDICT_FALSE(ShouldForceSampling())) { if (ABSL_PREDICT_FALSE(ShouldForceSampling())) {
next_sample.next_sample = 1; next_sample.next_sample = 1;
const int64_t old_stride = exchange(next_sample.sample_stride, 1); const int64_t old_stride = exchange(next_sample.sample_stride, 1);
HashtablezInfo* result = HashtablezInfo* result = GlobalHashtablezSampler().Register(
GlobalHashtablezSampler().Register(old_stride, inline_element_size); old_stride, inline_element_size, soo_capacity);
return result; return result;
} }
...@@ -151,10 +158,11 @@ HashtablezInfo* SampleSlow(SamplingState& next_sample, ...@@ -151,10 +158,11 @@ HashtablezInfo* SampleSlow(SamplingState& next_sample,
// that case. // that case.
if (first) { if (first) {
if (ABSL_PREDICT_TRUE(--next_sample.next_sample > 0)) return nullptr; if (ABSL_PREDICT_TRUE(--next_sample.next_sample > 0)) return nullptr;
return SampleSlow(next_sample, inline_element_size); return SampleSlow(next_sample, inline_element_size, soo_capacity);
} }
return GlobalHashtablezSampler().Register(old_stride, inline_element_size); return GlobalHashtablezSampler().Register(old_stride, inline_element_size,
soo_capacity);
#endif #endif
} }
......
...@@ -40,15 +40,20 @@ ...@@ -40,15 +40,20 @@
#define ABSL_CONTAINER_INTERNAL_HASHTABLEZ_SAMPLER_H_ #define ABSL_CONTAINER_INTERNAL_HASHTABLEZ_SAMPLER_H_
#include <atomic> #include <atomic>
#include <cstddef>
#include <cstdint>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "absl/base/attributes.h"
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/base/internal/per_thread_tls.h" #include "absl/base/internal/per_thread_tls.h"
#include "absl/base/optimization.h" #include "absl/base/optimization.h"
#include "absl/base/thread_annotations.h"
#include "absl/profiling/internal/sample_recorder.h" #include "absl/profiling/internal/sample_recorder.h"
#include "absl/synchronization/mutex.h" #include "absl/synchronization/mutex.h"
#include "absl/time/time.h"
#include "absl/utility/utility.h" #include "absl/utility/utility.h"
namespace absl { namespace absl {
...@@ -67,7 +72,8 @@ struct HashtablezInfo : public profiling_internal::Sample<HashtablezInfo> { ...@@ -67,7 +72,8 @@ struct HashtablezInfo : public profiling_internal::Sample<HashtablezInfo> {
// Puts the object into a clean state, fills in the logically `const` members, // Puts the object into a clean state, fills in the logically `const` members,
// blocking for any readers that are currently sampling the object. // blocking for any readers that are currently sampling the object.
void PrepareForSampling(int64_t stride, size_t inline_element_size_value) void PrepareForSampling(int64_t stride, size_t inline_element_size_value,
uint16_t soo_capacity_value)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(init_mu); ABSL_EXCLUSIVE_LOCKS_REQUIRED(init_mu);
// These fields are mutated by the various Record* APIs and need to be // These fields are mutated by the various Record* APIs and need to be
...@@ -91,8 +97,13 @@ struct HashtablezInfo : public profiling_internal::Sample<HashtablezInfo> { ...@@ -91,8 +97,13 @@ struct HashtablezInfo : public profiling_internal::Sample<HashtablezInfo> {
static constexpr int kMaxStackDepth = 64; static constexpr int kMaxStackDepth = 64;
absl::Time create_time; absl::Time create_time;
int32_t depth; int32_t depth;
// The SOO capacity for this table in elements (not bytes). Note that sampled
// tables are never SOO because we need to store the infoz handle on the heap.
// Tables that would be SOO if not sampled should have: soo_capacity > 0 &&
// size <= soo_capacity && max_reserve <= soo_capacity.
uint16_t soo_capacity;
void* stack[kMaxStackDepth]; void* stack[kMaxStackDepth];
size_t inline_element_size; // How big is the slot? size_t inline_element_size; // How big is the slot in bytes?
}; };
void RecordRehashSlow(HashtablezInfo* info, size_t total_probe_length); void RecordRehashSlow(HashtablezInfo* info, size_t total_probe_length);
...@@ -117,7 +128,7 @@ struct SamplingState { ...@@ -117,7 +128,7 @@ struct SamplingState {
}; };
HashtablezInfo* SampleSlow(SamplingState& next_sample, HashtablezInfo* SampleSlow(SamplingState& next_sample,
size_t inline_element_size); size_t inline_element_size, uint16_t soo_capacity);
void UnsampleSlow(HashtablezInfo* info); void UnsampleSlow(HashtablezInfo* info);
#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE) #if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
...@@ -204,16 +215,16 @@ class HashtablezInfoHandle { ...@@ -204,16 +215,16 @@ class HashtablezInfoHandle {
extern ABSL_PER_THREAD_TLS_KEYWORD SamplingState global_next_sample; extern ABSL_PER_THREAD_TLS_KEYWORD SamplingState global_next_sample;
#endif // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE) #endif // defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
// Returns an RAII sampling handle that manages registration and unregistation // Returns a sampling handle.
// with the global sampler.
inline HashtablezInfoHandle Sample( inline HashtablezInfoHandle Sample(
size_t inline_element_size ABSL_ATTRIBUTE_UNUSED) { ABSL_ATTRIBUTE_UNUSED size_t inline_element_size,
ABSL_ATTRIBUTE_UNUSED uint16_t soo_capacity) {
#if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE) #if defined(ABSL_INTERNAL_HASHTABLEZ_SAMPLE)
if (ABSL_PREDICT_TRUE(--global_next_sample.next_sample > 0)) { if (ABSL_PREDICT_TRUE(--global_next_sample.next_sample > 0)) {
return HashtablezInfoHandle(nullptr); return HashtablezInfoHandle(nullptr);
} }
return HashtablezInfoHandle( return HashtablezInfoHandle(
SampleSlow(global_next_sample, inline_element_size)); SampleSlow(global_next_sample, inline_element_size, soo_capacity));
#else #else
return HashtablezInfoHandle(nullptr); return HashtablezInfoHandle(nullptr);
#endif // !ABSL_PER_THREAD_TLS #endif // !ABSL_PER_THREAD_TLS
......
...@@ -15,8 +15,12 @@ ...@@ -15,8 +15,12 @@
#include "absl/container/internal/hashtablez_sampler.h" #include "absl/container/internal/hashtablez_sampler.h"
#include <atomic> #include <atomic>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <limits> #include <limits>
#include <random> #include <random>
#include <vector>
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
...@@ -67,7 +71,7 @@ std::vector<size_t> GetSizes(HashtablezSampler* s) { ...@@ -67,7 +71,7 @@ std::vector<size_t> GetSizes(HashtablezSampler* s) {
HashtablezInfo* Register(HashtablezSampler* s, size_t size) { HashtablezInfo* Register(HashtablezSampler* s, size_t size) {
const int64_t test_stride = 123; const int64_t test_stride = 123;
const size_t test_element_size = 17; const size_t test_element_size = 17;
auto* info = s->Register(test_stride, test_element_size); auto* info = s->Register(test_stride, test_element_size, /*soo_capacity=*/0);
assert(info != nullptr); assert(info != nullptr);
info->size.store(size); info->size.store(size);
return info; return info;
...@@ -79,7 +83,8 @@ TEST(HashtablezInfoTest, PrepareForSampling) { ...@@ -79,7 +83,8 @@ TEST(HashtablezInfoTest, PrepareForSampling) {
const size_t test_element_size = 17; const size_t test_element_size = 17;
HashtablezInfo info; HashtablezInfo info;
absl::MutexLock l(&info.init_mu); absl::MutexLock l(&info.init_mu);
info.PrepareForSampling(test_stride, test_element_size); info.PrepareForSampling(test_stride, test_element_size,
/*soo_capacity_value=*/1);
EXPECT_EQ(info.capacity.load(), 0); EXPECT_EQ(info.capacity.load(), 0);
EXPECT_EQ(info.size.load(), 0); EXPECT_EQ(info.size.load(), 0);
...@@ -94,6 +99,7 @@ TEST(HashtablezInfoTest, PrepareForSampling) { ...@@ -94,6 +99,7 @@ TEST(HashtablezInfoTest, PrepareForSampling) {
EXPECT_GE(info.create_time, test_start); EXPECT_GE(info.create_time, test_start);
EXPECT_EQ(info.weight, test_stride); EXPECT_EQ(info.weight, test_stride);
EXPECT_EQ(info.inline_element_size, test_element_size); EXPECT_EQ(info.inline_element_size, test_element_size);
EXPECT_EQ(info.soo_capacity, 1);
info.capacity.store(1, std::memory_order_relaxed); info.capacity.store(1, std::memory_order_relaxed);
info.size.store(1, std::memory_order_relaxed); info.size.store(1, std::memory_order_relaxed);
...@@ -106,7 +112,8 @@ TEST(HashtablezInfoTest, PrepareForSampling) { ...@@ -106,7 +112,8 @@ TEST(HashtablezInfoTest, PrepareForSampling) {
info.max_reserve.store(1, std::memory_order_relaxed); info.max_reserve.store(1, std::memory_order_relaxed);
info.create_time = test_start - absl::Hours(20); info.create_time = test_start - absl::Hours(20);
info.PrepareForSampling(test_stride * 2, test_element_size); info.PrepareForSampling(test_stride * 2, test_element_size,
/*soo_capacity_value=*/0);
EXPECT_EQ(info.capacity.load(), 0); EXPECT_EQ(info.capacity.load(), 0);
EXPECT_EQ(info.size.load(), 0); EXPECT_EQ(info.size.load(), 0);
EXPECT_EQ(info.num_erases.load(), 0); EXPECT_EQ(info.num_erases.load(), 0);
...@@ -120,6 +127,7 @@ TEST(HashtablezInfoTest, PrepareForSampling) { ...@@ -120,6 +127,7 @@ TEST(HashtablezInfoTest, PrepareForSampling) {
EXPECT_EQ(info.weight, 2 * test_stride); EXPECT_EQ(info.weight, 2 * test_stride);
EXPECT_EQ(info.inline_element_size, test_element_size); EXPECT_EQ(info.inline_element_size, test_element_size);
EXPECT_GE(info.create_time, test_start); EXPECT_GE(info.create_time, test_start);
EXPECT_EQ(info.soo_capacity, 0);
} }
TEST(HashtablezInfoTest, RecordStorageChanged) { TEST(HashtablezInfoTest, RecordStorageChanged) {
...@@ -127,7 +135,8 @@ TEST(HashtablezInfoTest, RecordStorageChanged) { ...@@ -127,7 +135,8 @@ TEST(HashtablezInfoTest, RecordStorageChanged) {
absl::MutexLock l(&info.init_mu); absl::MutexLock l(&info.init_mu);
const int64_t test_stride = 21; const int64_t test_stride = 21;
const size_t test_element_size = 19; const size_t test_element_size = 19;
info.PrepareForSampling(test_stride, test_element_size); info.PrepareForSampling(test_stride, test_element_size,
/*soo_capacity_value=*/0);
RecordStorageChangedSlow(&info, 17, 47); RecordStorageChangedSlow(&info, 17, 47);
EXPECT_EQ(info.size.load(), 17); EXPECT_EQ(info.size.load(), 17);
EXPECT_EQ(info.capacity.load(), 47); EXPECT_EQ(info.capacity.load(), 47);
...@@ -141,7 +150,8 @@ TEST(HashtablezInfoTest, RecordInsert) { ...@@ -141,7 +150,8 @@ TEST(HashtablezInfoTest, RecordInsert) {
absl::MutexLock l(&info.init_mu); absl::MutexLock l(&info.init_mu);
const int64_t test_stride = 25; const int64_t test_stride = 25;
const size_t test_element_size = 23; const size_t test_element_size = 23;
info.PrepareForSampling(test_stride, test_element_size); info.PrepareForSampling(test_stride, test_element_size,
/*soo_capacity_value=*/0);
EXPECT_EQ(info.max_probe_length.load(), 0); EXPECT_EQ(info.max_probe_length.load(), 0);
RecordInsertSlow(&info, 0x0000FF00, 6 * kProbeLength); RecordInsertSlow(&info, 0x0000FF00, 6 * kProbeLength);
EXPECT_EQ(info.max_probe_length.load(), 6); EXPECT_EQ(info.max_probe_length.load(), 6);
...@@ -165,7 +175,8 @@ TEST(HashtablezInfoTest, RecordErase) { ...@@ -165,7 +175,8 @@ TEST(HashtablezInfoTest, RecordErase) {
const size_t test_element_size = 29; const size_t test_element_size = 29;
HashtablezInfo info; HashtablezInfo info;
absl::MutexLock l(&info.init_mu); absl::MutexLock l(&info.init_mu);
info.PrepareForSampling(test_stride, test_element_size); info.PrepareForSampling(test_stride, test_element_size,
/*soo_capacity_value=*/1);
EXPECT_EQ(info.num_erases.load(), 0); EXPECT_EQ(info.num_erases.load(), 0);
EXPECT_EQ(info.size.load(), 0); EXPECT_EQ(info.size.load(), 0);
RecordInsertSlow(&info, 0x0000FF00, 6 * kProbeLength); RecordInsertSlow(&info, 0x0000FF00, 6 * kProbeLength);
...@@ -174,6 +185,7 @@ TEST(HashtablezInfoTest, RecordErase) { ...@@ -174,6 +185,7 @@ TEST(HashtablezInfoTest, RecordErase) {
EXPECT_EQ(info.size.load(), 0); EXPECT_EQ(info.size.load(), 0);
EXPECT_EQ(info.num_erases.load(), 1); EXPECT_EQ(info.num_erases.load(), 1);
EXPECT_EQ(info.inline_element_size, test_element_size); EXPECT_EQ(info.inline_element_size, test_element_size);
EXPECT_EQ(info.soo_capacity, 1);
} }
TEST(HashtablezInfoTest, RecordRehash) { TEST(HashtablezInfoTest, RecordRehash) {
...@@ -181,7 +193,8 @@ TEST(HashtablezInfoTest, RecordRehash) { ...@@ -181,7 +193,8 @@ TEST(HashtablezInfoTest, RecordRehash) {
const size_t test_element_size = 31; const size_t test_element_size = 31;
HashtablezInfo info; HashtablezInfo info;
absl::MutexLock l(&info.init_mu); absl::MutexLock l(&info.init_mu);
info.PrepareForSampling(test_stride, test_element_size); info.PrepareForSampling(test_stride, test_element_size,
/*soo_capacity_value=*/0);
RecordInsertSlow(&info, 0x1, 0); RecordInsertSlow(&info, 0x1, 0);
RecordInsertSlow(&info, 0x2, kProbeLength); RecordInsertSlow(&info, 0x2, kProbeLength);
RecordInsertSlow(&info, 0x4, kProbeLength); RecordInsertSlow(&info, 0x4, kProbeLength);
...@@ -201,6 +214,7 @@ TEST(HashtablezInfoTest, RecordRehash) { ...@@ -201,6 +214,7 @@ TEST(HashtablezInfoTest, RecordRehash) {
EXPECT_EQ(info.num_erases.load(), 0); EXPECT_EQ(info.num_erases.load(), 0);
EXPECT_EQ(info.num_rehashes.load(), 1); EXPECT_EQ(info.num_rehashes.load(), 1);
EXPECT_EQ(info.inline_element_size, test_element_size); EXPECT_EQ(info.inline_element_size, test_element_size);
EXPECT_EQ(info.soo_capacity, 0);
} }
TEST(HashtablezInfoTest, RecordReservation) { TEST(HashtablezInfoTest, RecordReservation) {
...@@ -208,7 +222,8 @@ TEST(HashtablezInfoTest, RecordReservation) { ...@@ -208,7 +222,8 @@ TEST(HashtablezInfoTest, RecordReservation) {
absl::MutexLock l(&info.init_mu); absl::MutexLock l(&info.init_mu);
const int64_t test_stride = 35; const int64_t test_stride = 35;
const size_t test_element_size = 33; const size_t test_element_size = 33;
info.PrepareForSampling(test_stride, test_element_size); info.PrepareForSampling(test_stride, test_element_size,
/*soo_capacity_value=*/0);
RecordReservationSlow(&info, 3); RecordReservationSlow(&info, 3);
EXPECT_EQ(info.max_reserve.load(), 3); EXPECT_EQ(info.max_reserve.load(), 3);
...@@ -229,7 +244,8 @@ TEST(HashtablezSamplerTest, SmallSampleParameter) { ...@@ -229,7 +244,8 @@ TEST(HashtablezSamplerTest, SmallSampleParameter) {
for (int i = 0; i < 1000; ++i) { for (int i = 0; i < 1000; ++i) {
SamplingState next_sample = {0, 0}; SamplingState next_sample = {0, 0};
HashtablezInfo* sample = SampleSlow(next_sample, test_element_size); HashtablezInfo* sample = SampleSlow(next_sample, test_element_size,
/*soo_capacity=*/0);
EXPECT_GT(next_sample.next_sample, 0); EXPECT_GT(next_sample.next_sample, 0);
EXPECT_EQ(next_sample.next_sample, next_sample.sample_stride); EXPECT_EQ(next_sample.next_sample, next_sample.sample_stride);
EXPECT_NE(sample, nullptr); EXPECT_NE(sample, nullptr);
...@@ -244,7 +260,8 @@ TEST(HashtablezSamplerTest, LargeSampleParameter) { ...@@ -244,7 +260,8 @@ TEST(HashtablezSamplerTest, LargeSampleParameter) {
for (int i = 0; i < 1000; ++i) { for (int i = 0; i < 1000; ++i) {
SamplingState next_sample = {0, 0}; SamplingState next_sample = {0, 0};
HashtablezInfo* sample = SampleSlow(next_sample, test_element_size); HashtablezInfo* sample = SampleSlow(next_sample, test_element_size,
/*soo_capacity=*/0);
EXPECT_GT(next_sample.next_sample, 0); EXPECT_GT(next_sample.next_sample, 0);
EXPECT_EQ(next_sample.next_sample, next_sample.sample_stride); EXPECT_EQ(next_sample.next_sample, next_sample.sample_stride);
EXPECT_NE(sample, nullptr); EXPECT_NE(sample, nullptr);
...@@ -260,7 +277,8 @@ TEST(HashtablezSamplerTest, Sample) { ...@@ -260,7 +277,8 @@ TEST(HashtablezSamplerTest, Sample) {
int64_t total = 0; int64_t total = 0;
double sample_rate = 0.0; double sample_rate = 0.0;
for (int i = 0; i < 1000000; ++i) { for (int i = 0; i < 1000000; ++i) {
HashtablezInfoHandle h = Sample(test_element_size); HashtablezInfoHandle h = Sample(test_element_size,
/*soo_capacity=*/0);
++total; ++total;
if (h.IsSampled()) { if (h.IsSampled()) {
++num_sampled; ++num_sampled;
...@@ -275,7 +293,8 @@ TEST(HashtablezSamplerTest, Handle) { ...@@ -275,7 +293,8 @@ TEST(HashtablezSamplerTest, Handle) {
auto& sampler = GlobalHashtablezSampler(); auto& sampler = GlobalHashtablezSampler();
const int64_t test_stride = 41; const int64_t test_stride = 41;
const size_t test_element_size = 39; const size_t test_element_size = 39;
HashtablezInfoHandle h(sampler.Register(test_stride, test_element_size)); HashtablezInfoHandle h(sampler.Register(test_stride, test_element_size,
/*soo_capacity=*/0));
auto* info = HashtablezInfoHandlePeer::GetInfo(&h); auto* info = HashtablezInfoHandlePeer::GetInfo(&h);
info->hashes_bitwise_and.store(0x12345678, std::memory_order_relaxed); info->hashes_bitwise_and.store(0x12345678, std::memory_order_relaxed);
...@@ -358,11 +377,13 @@ TEST(HashtablezSamplerTest, MultiThreaded) { ...@@ -358,11 +377,13 @@ TEST(HashtablezSamplerTest, MultiThreaded) {
std::vector<HashtablezInfo*> infoz; std::vector<HashtablezInfo*> infoz;
while (!stop.HasBeenNotified()) { while (!stop.HasBeenNotified()) {
if (infoz.empty()) { if (infoz.empty()) {
infoz.push_back(sampler.Register(sampling_stride, elt_size)); infoz.push_back(sampler.Register(sampling_stride, elt_size,
/*soo_capacity=*/0));
} }
switch (std::uniform_int_distribution<>(0, 2)(gen)) { switch (std::uniform_int_distribution<>(0, 2)(gen)) {
case 0: { case 0: {
infoz.push_back(sampler.Register(sampling_stride, elt_size)); infoz.push_back(sampler.Register(sampling_stride, elt_size,
/*soo_capacity=*/0));
break; break;
} }
case 1: { case 1: {
......
...@@ -52,7 +52,9 @@ ...@@ -52,7 +52,9 @@
#include "absl/container/internal/hashtable_debug.h" #include "absl/container/internal/hashtable_debug.h"
#include "absl/container/internal/hashtablez_sampler.h" #include "absl/container/internal/hashtablez_sampler.h"
#include "absl/container/internal/test_allocator.h" #include "absl/container/internal/test_allocator.h"
#include "absl/functional/function_ref.h"
#include "absl/hash/hash.h" #include "absl/hash/hash.h"
#include "absl/log/check.h"
#include "absl/log/log.h" #include "absl/log/log.h"
#include "absl/memory/memory.h" #include "absl/memory/memory.h"
#include "absl/meta/type_traits.h" #include "absl/meta/type_traits.h"
...@@ -2035,6 +2037,9 @@ TYPED_TEST_P(SooTest, FindFullDeletedRegression) { ...@@ -2035,6 +2037,9 @@ TYPED_TEST_P(SooTest, FindFullDeletedRegression) {
} }
TYPED_TEST_P(SooTest, ReplacingDeletedSlotDoesNotRehash) { TYPED_TEST_P(SooTest, ReplacingDeletedSlotDoesNotRehash) {
// We need to disable hashtablez to avoid issues related to SOO and sampling.
SetHashtablezEnabled(false);
size_t n; size_t n;
{ {
// Compute n such that n is the maximum number of elements before rehash. // Compute n such that n is the maximum number of elements before rehash.
...@@ -2388,6 +2393,9 @@ TYPED_TEST_P(SooTest, IterationOrderChangesOnRehash) { ...@@ -2388,6 +2393,9 @@ TYPED_TEST_P(SooTest, IterationOrderChangesOnRehash) {
// Verify that pointers are invalidated as soon as a second element is inserted. // Verify that pointers are invalidated as soon as a second element is inserted.
// This prevents dependency on pointer stability on small tables. // This prevents dependency on pointer stability on small tables.
TYPED_TEST_P(SooTest, UnstablePointers) { TYPED_TEST_P(SooTest, UnstablePointers) {
// We need to disable hashtablez to avoid issues related to SOO and sampling.
SetHashtablezEnabled(false);
TypeParam table; TypeParam table;
const auto addr = [&](int i) { const auto addr = [&](int i) {
...@@ -2523,7 +2531,7 @@ TYPED_TEST_P(RawHashSamplerTest, Sample) { ...@@ -2523,7 +2531,7 @@ TYPED_TEST_P(RawHashSamplerTest, Sample) {
constexpr bool soo_enabled = std::is_same<SooIntTable, TypeParam>::value; constexpr bool soo_enabled = std::is_same<SooIntTable, TypeParam>::value;
// Enable the feature even if the prod default is off. // Enable the feature even if the prod default is off.
SetHashtablezEnabled(true); SetHashtablezEnabled(true);
SetHashtablezSampleParameter(100); SetHashtablezSampleParameter(100); // Sample ~1% of tables.
auto& sampler = GlobalHashtablezSampler(); auto& sampler = GlobalHashtablezSampler();
size_t start_size = 0; size_t start_size = 0;
...@@ -2557,25 +2565,26 @@ TYPED_TEST_P(RawHashSamplerTest, Sample) { ...@@ -2557,25 +2565,26 @@ TYPED_TEST_P(RawHashSamplerTest, Sample) {
absl::flat_hash_map<size_t, int> observed_checksums; absl::flat_hash_map<size_t, int> observed_checksums;
absl::flat_hash_map<ssize_t, int> reservations; absl::flat_hash_map<ssize_t, int> reservations;
end_size += sampler.Iterate([&](const HashtablezInfo& info) { end_size += sampler.Iterate([&](const HashtablezInfo& info) {
if (preexisting_info.count(&info) == 0) {
observed_checksums[info.hashes_bitwise_xor.load(
std::memory_order_relaxed)]++;
reservations[info.max_reserve.load(std::memory_order_relaxed)]++;
}
EXPECT_EQ(info.inline_element_size, sizeof(typename TypeParam::value_type));
++end_size; ++end_size;
if (preexisting_info.contains(&info)) return;
observed_checksums[info.hashes_bitwise_xor.load(
std::memory_order_relaxed)]++;
reservations[info.max_reserve.load(std::memory_order_relaxed)]++;
EXPECT_EQ(info.inline_element_size, sizeof(typename TypeParam::value_type));
if (soo_enabled) {
EXPECT_EQ(info.soo_capacity, SooCapacity());
} else {
EXPECT_EQ(info.soo_capacity, 0);
}
}); });
// Expect that we sampled at the requested sampling rate of ~1%.
EXPECT_NEAR((end_size - start_size) / static_cast<double>(tables.size()), EXPECT_NEAR((end_size - start_size) / static_cast<double>(tables.size()),
0.01, 0.005); 0.01, 0.005);
if (soo_enabled) { EXPECT_EQ(observed_checksums.size(), 5);
EXPECT_EQ(observed_checksums.size(), 9); for (const auto& [_, count] : observed_checksums) {
} else { EXPECT_NEAR((100 * count) / static_cast<double>(tables.size()), 0.2, 0.05);
EXPECT_EQ(observed_checksums.size(), 5);
for (const auto& [_, count] : observed_checksums) {
EXPECT_NEAR((100 * count) / static_cast<double>(tables.size()), 0.2,
0.05);
}
} }
EXPECT_EQ(reservations.size(), 10); EXPECT_EQ(reservations.size(), 10);
...@@ -2591,12 +2600,141 @@ TYPED_TEST_P(RawHashSamplerTest, Sample) { ...@@ -2591,12 +2600,141 @@ TYPED_TEST_P(RawHashSamplerTest, Sample) {
REGISTER_TYPED_TEST_SUITE_P(RawHashSamplerTest, Sample); REGISTER_TYPED_TEST_SUITE_P(RawHashSamplerTest, Sample);
using RawHashSamplerTestTypes = ::testing::Types<SooIntTable, NonSooIntTable>; using RawHashSamplerTestTypes = ::testing::Types<SooIntTable, NonSooIntTable>;
INSTANTIATE_TYPED_TEST_SUITE_P(My, RawHashSamplerTest, RawHashSamplerTestTypes); INSTANTIATE_TYPED_TEST_SUITE_P(My, RawHashSamplerTest, RawHashSamplerTestTypes);
std::vector<const HashtablezInfo*> SampleSooMutation(
absl::FunctionRef<void(SooIntTable&)> mutate_table) {
// Enable the feature even if the prod default is off.
SetHashtablezEnabled(true);
SetHashtablezSampleParameter(100); // Sample ~1% of tables.
auto& sampler = GlobalHashtablezSampler();
size_t start_size = 0;
absl::flat_hash_set<const HashtablezInfo*> preexisting_info;
start_size += sampler.Iterate([&](const HashtablezInfo& info) {
preexisting_info.insert(&info);
++start_size;
});
std::vector<SooIntTable> tables;
for (int i = 0; i < 1000000; ++i) {
tables.emplace_back();
mutate_table(tables.back());
}
size_t end_size = 0;
std::vector<const HashtablezInfo*> infos;
end_size += sampler.Iterate([&](const HashtablezInfo& info) {
++end_size;
if (preexisting_info.contains(&info)) return;
infos.push_back(&info);
});
// Expect that we sampled at the requested sampling rate of ~1%.
EXPECT_NEAR((end_size - start_size) / static_cast<double>(tables.size()),
0.01, 0.005);
return infos;
}
TEST(RawHashSamplerTest, SooTableInsertToEmpty) {
if (SooIntTable().capacity() != SooCapacity()) {
CHECK_LT(sizeof(void*), 8) << "missing SOO coverage";
GTEST_SKIP() << "not SOO on this platform";
}
std::vector<const HashtablezInfo*> infos =
SampleSooMutation([](SooIntTable& t) { t.insert(1); });
for (const HashtablezInfo* info : infos) {
ASSERT_EQ(info->inline_element_size,
sizeof(typename SooIntTable::value_type));
ASSERT_EQ(info->soo_capacity, SooCapacity());
ASSERT_EQ(info->capacity, NextCapacity(SooCapacity()));
ASSERT_EQ(info->size, 1);
ASSERT_EQ(info->max_reserve, 0);
ASSERT_EQ(info->num_erases, 0);
ASSERT_EQ(info->max_probe_length, 0);
ASSERT_EQ(info->total_probe_length, 0);
}
}
TEST(RawHashSamplerTest, SooTableReserveToEmpty) {
if (SooIntTable().capacity() != SooCapacity()) {
CHECK_LT(sizeof(void*), 8) << "missing SOO coverage";
GTEST_SKIP() << "not SOO on this platform";
}
std::vector<const HashtablezInfo*> infos =
SampleSooMutation([](SooIntTable& t) { t.reserve(100); });
for (const HashtablezInfo* info : infos) {
ASSERT_EQ(info->inline_element_size,
sizeof(typename SooIntTable::value_type));
ASSERT_EQ(info->soo_capacity, SooCapacity());
ASSERT_GE(info->capacity, 100);
ASSERT_EQ(info->size, 0);
ASSERT_EQ(info->max_reserve, 100);
ASSERT_EQ(info->num_erases, 0);
ASSERT_EQ(info->max_probe_length, 0);
ASSERT_EQ(info->total_probe_length, 0);
}
}
// This tests that reserve on a full SOO table doesn't incorrectly result in new
// (over-)sampling.
TEST(RawHashSamplerTest, SooTableReserveToFullSoo) {
if (SooIntTable().capacity() != SooCapacity()) {
CHECK_LT(sizeof(void*), 8) << "missing SOO coverage";
GTEST_SKIP() << "not SOO on this platform";
}
std::vector<const HashtablezInfo*> infos =
SampleSooMutation([](SooIntTable& t) {
t.insert(1);
t.reserve(100);
});
for (const HashtablezInfo* info : infos) {
ASSERT_EQ(info->inline_element_size,
sizeof(typename SooIntTable::value_type));
ASSERT_EQ(info->soo_capacity, SooCapacity());
ASSERT_GE(info->capacity, 100);
ASSERT_EQ(info->size, 1);
ASSERT_EQ(info->max_reserve, 100);
ASSERT_EQ(info->num_erases, 0);
ASSERT_EQ(info->max_probe_length, 0);
ASSERT_EQ(info->total_probe_length, 0);
}
}
// This tests that rehash(0) on a sampled table with size that fits in SOO
// doesn't incorrectly result in losing sampling.
TEST(RawHashSamplerTest, SooTableRehashShrinkWhenSizeFitsInSoo) {
if (SooIntTable().capacity() != SooCapacity()) {
CHECK_LT(sizeof(void*), 8) << "missing SOO coverage";
GTEST_SKIP() << "not SOO on this platform";
}
std::vector<const HashtablezInfo*> infos =
SampleSooMutation([](SooIntTable& t) {
t.reserve(100);
t.insert(1);
EXPECT_GE(t.capacity(), 100);
t.rehash(0);
});
for (const HashtablezInfo* info : infos) {
ASSERT_EQ(info->inline_element_size,
sizeof(typename SooIntTable::value_type));
ASSERT_EQ(info->soo_capacity, SooCapacity());
ASSERT_EQ(info->capacity, NextCapacity(SooCapacity()));
ASSERT_EQ(info->size, 1);
ASSERT_EQ(info->max_reserve, 100);
ASSERT_EQ(info->num_erases, 0);
ASSERT_EQ(info->max_probe_length, 0);
ASSERT_EQ(info->total_probe_length, 0);
}
}
#endif // ABSL_INTERNAL_HASHTABLEZ_SAMPLE #endif // ABSL_INTERNAL_HASHTABLEZ_SAMPLE
TEST(RawHashSamplerTest, DoNotSampleCustomAllocators) { TEST(RawHashSamplerTest, DoNotSampleCustomAllocators) {
// Enable the feature even if the prod default is off. // Enable the feature even if the prod default is off.
SetHashtablezEnabled(true); SetHashtablezEnabled(true);
SetHashtablezSampleParameter(100); SetHashtablezSampleParameter(100); // Sample ~1% of tables.
auto& sampler = GlobalHashtablezSampler(); auto& sampler = GlobalHashtablezSampler();
size_t start_size = 0; size_t start_size = 0;
...@@ -2974,7 +3112,7 @@ TYPED_TEST_P(SooTable, Basic) { ...@@ -2974,7 +3112,7 @@ TYPED_TEST_P(SooTable, Basic) {
bool frozen = true; bool frozen = true;
TypeParam t{FreezableAlloc<typename TypeParam::value_type>(&frozen)}; TypeParam t{FreezableAlloc<typename TypeParam::value_type>(&frozen)};
if (t.capacity() != SooCapacity()) { if (t.capacity() != SooCapacity()) {
EXPECT_LT(sizeof(void*), 8); CHECK_LT(sizeof(void*), 8) << "missing SOO coverage";
GTEST_SKIP() << "not SOO on this platform"; GTEST_SKIP() << "not SOO on this platform";
} }
...@@ -3006,14 +3144,18 @@ using FreezableSooTableTypes = ...@@ -3006,14 +3144,18 @@ using FreezableSooTableTypes =
FreezableSizedValueSooTable<16>>; FreezableSizedValueSooTable<16>>;
INSTANTIATE_TYPED_TEST_SUITE_P(My, SooTable, FreezableSooTableTypes); INSTANTIATE_TYPED_TEST_SUITE_P(My, SooTable, FreezableSooTableTypes);
TEST(Table, RehashToSoo) { TEST(Table, RehashToSooUnsampled) {
SooIntTable t; SooIntTable t;
if (t.capacity() != SooCapacity()) { if (t.capacity() != SooCapacity()) {
EXPECT_LT(sizeof(void*), 8); CHECK_LT(sizeof(void*), 8) << "missing SOO coverage";
GTEST_SKIP() << "not SOO on this platform"; GTEST_SKIP() << "not SOO on this platform";
} }
t.reserve(10); // We disable hashtablez sampling for this test to ensure that the table isn't
// sampled. When the table is sampled, it won't rehash down to SOO.
SetHashtablezEnabled(false);
t.reserve(100);
t.insert(0); t.insert(0);
EXPECT_EQ(*t.begin(), 0); EXPECT_EQ(*t.begin(), 0);
...@@ -3026,7 +3168,7 @@ TEST(Table, RehashToSoo) { ...@@ -3026,7 +3168,7 @@ TEST(Table, RehashToSoo) {
EXPECT_EQ(t.find(1), t.end()); EXPECT_EQ(t.find(1), t.end());
} }
TEST(Table, ResizeToNonSoo) { TEST(Table, ReserveToNonSoo) {
for (int reserve_capacity : {8, 100000}) { for (int reserve_capacity : {8, 100000}) {
SooIntTable t; SooIntTable t;
t.insert(0); t.insert(0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment