Merge pull request #40167 from singuliere/wip-49781

common/mempool: only fail tests if sharding is very bad

Reviewed-by: Adam Kupczyk <akupczyk@redhat.com>
Reviewed-by: Kefu Chai <kchai@redhat.com>
This commit is contained in:
Kefu Chai 2021-03-26 14:13:18 +08:00 committed by GitHub
commit 6e1de9ae35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 9 deletions

View File

@ -259,7 +259,7 @@ public:
// Dirt cheap, see:
// https://fossies.org/dox/glibc-2.32/pthread__self_8c_source.html
size_t me = (size_t)pthread_self();
size_t i = (me >> 12) & ((1 << num_shard_bits) - 1);
size_t i = (me >> CEPH_PAGE_SHIFT) & ((1 << num_shard_bits) - 1);
return i;
}

View File

@ -404,7 +404,7 @@ TEST(mempool, btree_map_test)
TEST(mempool, check_shard_select)
{
const size_t samples = 100;
const size_t samples = mempool::num_shards * 100;
std::atomic_int shards[mempool::num_shards] = {0};
std::vector<std::thread> workers;
for (size_t i = 0; i < samples; i++) {
@ -419,15 +419,16 @@ TEST(mempool, check_shard_select)
}
workers.clear();
double EX = (double)samples / (double)mempool::num_shards;
double VarX = 0;
size_t missed = 0;
for (size_t i = 0; i < mempool::num_shards; i++) {
VarX += (EX - shards[i]) * (EX - shards[i]);
if (shards[i] == 0) {
missed++;
}
}
//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);
// If more than half of the shards did not get anything,
// the distribution is bad enough to deserve a failure.
EXPECT_LT(missed, mempool::num_shards / 2);
}