Branch data Line data Source code
1 : : // Copyright (c) 2022 The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <memusage.h>
6 : : #include <support/allocators/pool.h>
7 : : #include <test/util/poolresourcetester.h>
8 : : #include <test/util/random.h>
9 : : #include <test/util/setup_common.h>
10 : :
11 : : #include <boost/test/unit_test.hpp>
12 : :
13 : : #include <cstddef>
14 : : #include <cstdint>
15 : : #include <unordered_map>
16 : : #include <vector>
17 : :
18 : 0 : BOOST_FIXTURE_TEST_SUITE(pool_tests, BasicTestingSetup)
19 : :
20 : 0 : BOOST_AUTO_TEST_CASE(basic_allocating)
21 : : {
22 : 0 : auto resource = PoolResource<8, 8>();
23 : 0 : PoolResourceTester::CheckAllDataAccountedFor(resource);
24 : :
25 : : // first chunk is already allocated
26 : 0 : size_t expected_bytes_available = resource.ChunkSizeBytes();
27 : 0 : BOOST_TEST(expected_bytes_available == PoolResourceTester::AvailableMemoryFromChunk(resource));
28 : :
29 : : // chunk is used, no more allocation
30 : 0 : void* block = resource.Allocate(8, 8);
31 : 0 : expected_bytes_available -= 8;
32 : 0 : BOOST_TEST(expected_bytes_available == PoolResourceTester::AvailableMemoryFromChunk(resource));
33 : :
34 : 0 : BOOST_TEST(0 == PoolResourceTester::FreeListSizes(resource)[1]);
35 : 0 : resource.Deallocate(block, 8, 8);
36 : 0 : PoolResourceTester::CheckAllDataAccountedFor(resource);
37 : 0 : BOOST_TEST(1 == PoolResourceTester::FreeListSizes(resource)[1]);
38 : :
39 : : // alignment is too small, but the best fitting freelist is used. Nothing is allocated.
40 : 0 : void* b = resource.Allocate(8, 1);
41 : 0 : BOOST_TEST(b == block); // we got the same block of memory as before
42 : 0 : BOOST_TEST(0 == PoolResourceTester::FreeListSizes(resource)[1]);
43 : 0 : BOOST_TEST(expected_bytes_available == PoolResourceTester::AvailableMemoryFromChunk(resource));
44 : :
45 : 0 : resource.Deallocate(block, 8, 1);
46 : 0 : PoolResourceTester::CheckAllDataAccountedFor(resource);
47 : 0 : BOOST_TEST(1 == PoolResourceTester::FreeListSizes(resource)[1]);
48 : 0 : BOOST_TEST(expected_bytes_available == PoolResourceTester::AvailableMemoryFromChunk(resource));
49 : :
50 : : // can't use resource because alignment is too big, allocate system memory
51 : 0 : b = resource.Allocate(8, 16);
52 : 0 : BOOST_TEST(b != block);
53 : 0 : block = b;
54 : 0 : PoolResourceTester::CheckAllDataAccountedFor(resource);
55 : 0 : BOOST_TEST(1 == PoolResourceTester::FreeListSizes(resource)[1]);
56 : 0 : BOOST_TEST(expected_bytes_available == PoolResourceTester::AvailableMemoryFromChunk(resource));
57 : :
58 : 0 : resource.Deallocate(block, 8, 16);
59 : 0 : PoolResourceTester::CheckAllDataAccountedFor(resource);
60 : 0 : BOOST_TEST(1 == PoolResourceTester::FreeListSizes(resource)[1]);
61 : 0 : BOOST_TEST(expected_bytes_available == PoolResourceTester::AvailableMemoryFromChunk(resource));
62 : :
63 : : // can't use chunk because size is too big
64 : 0 : block = resource.Allocate(16, 8);
65 : 0 : PoolResourceTester::CheckAllDataAccountedFor(resource);
66 : 0 : BOOST_TEST(1 == PoolResourceTester::FreeListSizes(resource)[1]);
67 : 0 : BOOST_TEST(expected_bytes_available == PoolResourceTester::AvailableMemoryFromChunk(resource));
68 : :
69 : 0 : resource.Deallocate(block, 16, 8);
70 : 0 : PoolResourceTester::CheckAllDataAccountedFor(resource);
71 : 0 : BOOST_TEST(1 == PoolResourceTester::FreeListSizes(resource)[1]);
72 : 0 : BOOST_TEST(expected_bytes_available == PoolResourceTester::AvailableMemoryFromChunk(resource));
73 : :
74 : 0 : // it's possible that 0 bytes are allocated, make sure this works. In that case the call is forwarded to operator new
75 : : // 0 bytes takes one entry from the first freelist
76 : 0 : void* p = resource.Allocate(0, 1);
77 : 0 : BOOST_TEST(0 == PoolResourceTester::FreeListSizes(resource)[1]);
78 : 0 : BOOST_TEST(expected_bytes_available == PoolResourceTester::AvailableMemoryFromChunk(resource));
79 : :
80 : 0 : resource.Deallocate(p, 0, 1);
81 : 0 : PoolResourceTester::CheckAllDataAccountedFor(resource);
82 : 0 : BOOST_TEST(1 == PoolResourceTester::FreeListSizes(resource)[1]);
83 : 0 : BOOST_TEST(expected_bytes_available == PoolResourceTester::AvailableMemoryFromChunk(resource));
84 : 0 : }
85 : :
86 : : // Allocates from 0 to n bytes were n > the PoolResource's data, and each should work
87 : 0 : BOOST_AUTO_TEST_CASE(allocate_any_byte)
88 : : {
89 : 0 : auto resource = PoolResource<128, 8>(1024);
90 : :
91 : 0 : uint8_t num_allocs = 200;
92 : :
93 : 0 : auto data = std::vector<Span<uint8_t>>();
94 : :
95 : : // allocate an increasing number of bytes
96 : 0 : for (uint8_t num_bytes = 0; num_bytes < num_allocs; ++num_bytes) {
97 : 0 : uint8_t* bytes = new (resource.Allocate(num_bytes, 1)) uint8_t[num_bytes];
98 : 0 : BOOST_TEST(bytes != nullptr);
99 : 0 : data.emplace_back(bytes, num_bytes);
100 : :
101 : : // set each byte to num_bytes
102 : 0 : std::fill(bytes, bytes + num_bytes, num_bytes);
103 : 0 : }
104 : :
105 : : // now that we got all allocated, test if all still have the correct values, and give everything back to the allocator
106 : 0 : uint8_t val = 0;
107 : 0 : for (auto const& span : data) {
108 : 0 : for (auto x : span) {
109 : 0 : BOOST_TEST(val == x);
110 : : }
111 : 0 : std::destroy(span.data(), span.data() + span.size());
112 : 0 : resource.Deallocate(span.data(), span.size(), 1);
113 : 0 : ++val;
114 : : }
115 : :
116 : 0 : PoolResourceTester::CheckAllDataAccountedFor(resource);
117 : 0 : }
118 : :
119 : 0 : BOOST_AUTO_TEST_CASE(random_allocations)
120 : : {
121 : : struct PtrSizeAlignment {
122 : : void* ptr;
123 : : size_t bytes;
124 : : size_t alignment;
125 : : };
126 : :
127 : : // makes a bunch of random allocations and gives all of them back in random order.
128 : 0 : auto resource = PoolResource<128, 8>(65536);
129 : 0 : std::vector<PtrSizeAlignment> ptr_size_alignment{};
130 : 0 : for (size_t i = 0; i < 1000; ++i) {
131 : : // make it a bit more likely to allocate than deallocate
132 : 0 : if (ptr_size_alignment.empty() || 0 != InsecureRandRange(4)) {
133 : : // allocate a random item
134 : 0 : std::size_t alignment = std::size_t{1} << InsecureRandRange(8); // 1, 2, ..., 128
135 : 0 : std::size_t size = (InsecureRandRange(200) / alignment + 1) * alignment; // multiple of alignment
136 : 0 : void* ptr = resource.Allocate(size, alignment);
137 : 0 : BOOST_TEST(ptr != nullptr);
138 : 0 : BOOST_TEST((reinterpret_cast<uintptr_t>(ptr) & (alignment - 1)) == 0);
139 : 0 : ptr_size_alignment.push_back({ptr, size, alignment});
140 : 0 : } else {
141 : : // deallocate a random item
142 : 0 : auto& x = ptr_size_alignment[InsecureRandRange(ptr_size_alignment.size())];
143 : 0 : resource.Deallocate(x.ptr, x.bytes, x.alignment);
144 : 0 : x = ptr_size_alignment.back();
145 : 0 : ptr_size_alignment.pop_back();
146 : : }
147 : 0 : }
148 : :
149 : : // deallocate all the rest
150 : 0 : for (auto const& x : ptr_size_alignment) {
151 : 0 : resource.Deallocate(x.ptr, x.bytes, x.alignment);
152 : : }
153 : :
154 : 0 : PoolResourceTester::CheckAllDataAccountedFor(resource);
155 : 0 : }
156 : :
157 : 0 : BOOST_AUTO_TEST_CASE(memusage_test)
158 : : {
159 : 0 : auto std_map = std::unordered_map<int, int>{};
160 : :
161 : : using Map = std::unordered_map<int,
162 : : int,
163 : : std::hash<int>,
164 : : std::equal_to<int>,
165 : : PoolAllocator<std::pair<const int, int>,
166 : : sizeof(std::pair<const int, int>) + sizeof(void*) * 4,
167 : : alignof(void*)>>;
168 : 0 : auto resource = Map::allocator_type::ResourceType(1024);
169 : :
170 : 0 : PoolResourceTester::CheckAllDataAccountedFor(resource);
171 : :
172 : : {
173 : 0 : auto resource_map = Map{0, std::hash<int>{}, std::equal_to<int>{}, &resource};
174 : :
175 : : // can't have the same resource usage
176 : 0 : BOOST_TEST(memusage::DynamicUsage(std_map) != memusage::DynamicUsage(resource_map));
177 : :
178 : 0 : for (size_t i = 0; i < 10000; ++i) {
179 : 0 : std_map[i];
180 : 0 : resource_map[i];
181 : 0 : }
182 : :
183 : : // Eventually the resource_map should have a much lower memory usage because it has less malloc overhead
184 : 0 : BOOST_TEST(memusage::DynamicUsage(resource_map) <= memusage::DynamicUsage(std_map) * 90 / 100);
185 : 0 : }
186 : :
187 : 0 : PoolResourceTester::CheckAllDataAccountedFor(resource);
188 : 0 : }
189 : :
190 : 0 : BOOST_AUTO_TEST_SUITE_END()
|