Skip to content

Commit

Permalink
pool: change memusage_test to use int64_t, add allocation check
Browse files Browse the repository at this point in the history
If alignment of the PoolAllocator would be insufficient, then the test would fail. This also catches the issue with ARM 32bit,
where int64_t is aligned to 8 bytes but void* is aligned to 4 bytes. The test adds a check to ensure the pool has allocated
a minimum number of chunks

Github-Pull: bitcoin#28913
Rebased-From: d5b4c0b
  • Loading branch information
martinus authored and fanquake committed Nov 22, 2023
1 parent bcc183c commit 1488648
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/test/pool_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,20 @@ BOOST_AUTO_TEST_CASE(random_allocations)

BOOST_AUTO_TEST_CASE(memusage_test)
{
auto std_map = std::unordered_map<int, int>{};

using Map = std::unordered_map<int,
int,
std::hash<int>,
std::equal_to<int>,
PoolAllocator<std::pair<const int, int>,
sizeof(std::pair<const int, int>) + sizeof(void*) * 4>>;
auto std_map = std::unordered_map<int64_t, int64_t>{};

using Map = std::unordered_map<int64_t,
int64_t,
std::hash<int64_t>,
std::equal_to<int64_t>,
PoolAllocator<std::pair<const int64_t, int64_t>,
sizeof(std::pair<const int64_t, int64_t>) + sizeof(void*) * 4>>;
auto resource = Map::allocator_type::ResourceType(1024);

PoolResourceTester::CheckAllDataAccountedFor(resource);

{
auto resource_map = Map{0, std::hash<int>{}, std::equal_to<int>{}, &resource};
auto resource_map = Map{0, std::hash<int64_t>{}, std::equal_to<int64_t>{}, &resource};

// can't have the same resource usage
BOOST_TEST(memusage::DynamicUsage(std_map) != memusage::DynamicUsage(resource_map));
Expand All @@ -181,6 +181,11 @@ BOOST_AUTO_TEST_CASE(memusage_test)

// Eventually the resource_map should have a much lower memory usage because it has less malloc overhead
BOOST_TEST(memusage::DynamicUsage(resource_map) <= memusage::DynamicUsage(std_map) * 90 / 100);

// Make sure the pool is actually used by the nodes
auto max_nodes_per_chunk = resource.ChunkSizeBytes() / sizeof(Map::value_type);
auto min_num_allocated_chunks = resource_map.size() / max_nodes_per_chunk + 1;
BOOST_TEST(resource.NumAllocatedChunks() >= min_num_allocated_chunks);
}

PoolResourceTester::CheckAllDataAccountedFor(resource);
Expand Down

0 comments on commit 1488648

Please sign in to comment.