From b77e6704c760532f6e3fd7990c937f413740aefa Mon Sep 17 00:00:00 2001 From: Adam Kupczyk Date: Mon, 25 Jan 2021 11:33:28 +0100 Subject: [PATCH] common/mempool: Add test for mempool shards Add test that checks quality of mempool sharing. It refuses to accept cases when variance is too large. Signed-off-by: Adam Kupczyk (cherry picked from commit e4e07de8f36d0d44b341d5784a8bf56192201b95) --- src/include/mempool.h | 9 +++++++-- src/test/test_mempool.cc | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/include/mempool.h b/src/include/mempool.h index 2247cb15bc0aa..a18f6a502daad 100644 --- a/src/include/mempool.h +++ b/src/include/mempool.h @@ -255,11 +255,16 @@ public: void adjust_count(ssize_t items, ssize_t bytes); - shard_t* pick_a_shard() { + static size_t pick_a_shard_int() { // Dirt cheap, see: - // http://fossies.org/dox/glibc-2.24/pthread__self_8c_source.html + // https://fossies.org/dox/glibc-2.32/pthread__self_8c_source.html size_t me = (size_t)pthread_self(); size_t i = (me >> 3) & ((1 << num_shard_bits) - 1); + return i; + } + + shard_t* pick_a_shard() { + size_t i = pick_a_shard_int(); return &shard[i]; } diff --git a/src/test/test_mempool.cc b/src/test/test_mempool.cc index f4c2abb7d3b0b..99b87d11f265a 100644 --- a/src/test/test_mempool.cc +++ b/src/test/test_mempool.cc @@ -402,6 +402,33 @@ TEST(mempool, btree_map_test) ASSERT_EQ(0, mempool::osd::allocated_bytes()); } +TEST(mempool, check_shard_select) +{ + const size_t samples = 100; + std::atomic_int shards[mempool::num_shards] = {0}; + std::vector workers; + for (size_t i = 0; i < samples; i++) { + workers.push_back( + std::thread([&](){ + size_t i = mempool::pool_t::pick_a_shard_int(); + shards[i]++; + })); + } + for (auto& t:workers) { + t.join(); + } + workers.clear(); + + double EX = (double)samples / (double)mempool::num_shards; + double VarX = 0; + for (size_t i = 0; i < mempool::num_shards; i++) { + VarX += (EX - shards[i]) * (EX - shards[i]); + } + //random gives VarX below 200 + //when half slots are 0, we get ~300 + //when all samples go into one slot, we get ~9000 + EXPECT_LT(VarX, 200); +} int main(int argc, char **argv) -- 2.39.5