]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/mempool: Add test for mempool shards
authorAdam Kupczyk <akupczyk@redhat.com>
Mon, 25 Jan 2021 10:33:28 +0000 (11:33 +0100)
committerNathan Cutler <ncutler@suse.com>
Tue, 23 Feb 2021 21:05:26 +0000 (22:05 +0100)
Add test that checks quality of mempool sharing.
It refuses to accept cases when variance is too large.

Signed-off-by: Adam Kupczyk <akupczyk@redhat.com>
(cherry picked from commit e4e07de8f36d0d44b341d5784a8bf56192201b95)

Conflicts:
src/test/test_mempool.cc
- nautilus version does not have as many tests as master version

src/include/mempool.h
src/test/test_mempool.cc

index b69b759f64dbec203d09213a0021b24b6ae2cb6a..c4b0086c106c0500756055063121ea66db1d132b 100644 (file)
@@ -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];
   }
 
index 51e8352099bf8c58048aed537d62f7842d2f66fc..02e99f3fcc19d9345745ad340344cc3744d613c1 100644 (file)
@@ -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<std::thread> 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<const char*> args;