From: Adam Kupczyk Date: Mon, 25 Jan 2021 10:33:28 +0000 (+0100) Subject: common/mempool: Add test for mempool shards X-Git-Tag: v14.2.17~19^2~4 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=5ce9d9bbaa1318cbf9a9ffca644c8cc2357372ba;p=ceph.git 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) Conflicts: src/test/test_mempool.cc - nautilus version does not have as many tests as master version --- diff --git a/src/include/mempool.h b/src/include/mempool.h index b69b759f64d..c4b0086c106 100644 --- a/src/include/mempool.h +++ b/src/include/mempool.h @@ -254,11 +254,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 51e8352099b..02e99f3fcc1 100644 --- a/src/test/test_mempool.cc +++ b/src/test/test_mempool.cc @@ -365,6 +365,35 @@ TEST(mempool, bufferlist_reassign) ASSERT_EQ(bytes_before, 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) { vector args;