Repository navigation
Expand file tree
/
Copy pathblk_allocator_validation.cpp
More file actions
281 lines (258 loc) · 10.3 KB
/
Copy pathblk_allocator_validation.cpp
File metadata and controls
281 lines (258 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* Copyright 2026 CEA (www.cea.fr)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ============================================================================
// BlkAllocator harness: fast_allocators' size-dispatching front end, alone
// (no chunk variant). Nothing in the library calls it -- it is public API for
// code outside shrink-array -- so this is the only thing that runs it.
// ============================================================================
//
// sizes alloc_block / free_block and alloc_array / free_array at sizes on,
// between and past the ladder's buckets (1 byte to 1 MiB, the
// largest bucket being 65536): every block is fully writable and
// keeps its content until freed;
// distinct a few thousand live blocks of mixed sizes at once never overlap
// (each is filled with its own pattern and checked after all the
// others were written), then all are freed and taken again;
// array alloc_array's result is aligned to fast_alloc::alignment;
// huge (opt-in, BLK_HUGE=1) 2 GiB - 64, 2 GiB + 64 and 4 GiB + 64
// through both pairs: the size_t -> int / unsigned truncations
// (first and last 4 KiB written and read back; up to ~4 GiB
// resident, one block at a time);
// parallel (OpenMP builds) the same from every thread at once -- the
// ladder's critical sections and the per-thread free batches --
// then clear_info_free_elem() at the quiescent point after the region.
//
// EXIT STATUS: 0 when every check holds, 1 otherwise.
#include <cstdint> // std::uintptr_t
#include <cstdlib> // std::getenv (BLK_HUGE)
#include <vector> // std::vector
#include "fast_allocators/internal/static_const_array.h" // BlkAllocator, StaticConstArray
#include "fast_allocators/internal/svalloc.h" // fast_alloc::alignment
#include "chunk/print.h" // shrink_array::println
using shrink_array::println;
namespace
{
constexpr std::size_t test_sizes[] = {
1, 3, 4, 5, 8, 16, 24, 63, 64, 65, 100,
512, 513, 1000, 4096, 4097, 32768, 65535, 65536, 65537, 100000, std::size_t{1} << 20};
unsigned char pattern(std::size_t id, std::size_t offset)
{
return static_cast<unsigned char>((id * 131u + offset * 7u + 1u) & 0xFFu);
}
void fill(void* ptr, std::size_t size, std::size_t id)
{
auto* bytes = static_cast<unsigned char*>(ptr);
for (std::size_t k = 0; k < size; ++k)
{
bytes[k] = pattern(id, k);
}
}
bool intact(const void* ptr, std::size_t size, std::size_t id)
{
const auto* bytes = static_cast<const unsigned char*>(ptr);
for (std::size_t k = 0; k < size; ++k)
{
if (bytes[k] != pattern(id, k))
{
return false;
}
}
return true;
}
int fails = 0;
void expect(bool ok, const char* what)
{
println(" [{}] {}", ok ? " ok " : "FAIL", what);
if (!ok)
{
++fails;
}
}
/// @brief One block per size, both front-end pairs.
void check_sizes()
{
bool ok_blk = true;
bool ok_array = true;
bool aligned = true;
for (std::size_t i = 0; i < std::size(test_sizes); ++i)
{
const std::size_t size = test_sizes[i];
void* ptr = fast_alloc::BlkAllocator::alloc_block(size);
if (ptr == nullptr)
{
ok_blk = false;
continue;
}
fill(ptr, size, i);
ok_blk = ok_blk && intact(ptr, size, i);
fast_alloc::BlkAllocator::free_block(ptr, size);
void* arr = fast_alloc::BlkAllocator::alloc_array(size);
if (arr == nullptr)
{
ok_array = false;
continue;
}
aligned = aligned && (reinterpret_cast<std::uintptr_t>(arr) % fast_alloc::alignment) == 0;
fill(arr, size, i + 1000);
ok_array = ok_array && intact(arr, size, i + 1000);
fast_alloc::BlkAllocator::free_array(arr);
}
expect(ok_blk, "alloc_block/free_block: 1 B to 1 MiB, on, between and past the buckets");
expect(ok_array, "alloc_array/free_array: same sizes, size read back from the header");
expect(aligned, "alloc_array: aligned to fast_alloc::alignment");
}
/// @brief Sizes past INT_MAX and UINT_MAX, where the front end must not
/// narrow before routing (free_block() sending [2 GiB, 4 GiB) to the
/// ladder, alloc_block() serving 4 GiB + 64 from the 64-byte bucket,
/// an int header in alloc_array()). Opt-in (BLK_HUGE=1): it touches
/// gigabytes. A regression crashes rather than reporting FAIL.
void check_huge()
{
if (sizeof(std::size_t) < 8 || std::getenv("BLK_HUGE") == nullptr)
{
println(" [skip] sizes past 2 GiB / 4 GiB (set BLK_HUGE=1)");
return;
}
constexpr std::size_t gib = std::size_t{1} << 30;
constexpr std::size_t window = 4096; // bytes checked at each end
const std::size_t sizes[] = {2 * gib - 64, 2 * gib + 64, 4 * gib + 64};
const auto ends_ok = [](void* ptr, std::size_t size, std::size_t id)
{
auto* bytes = static_cast<unsigned char*>(ptr);
fill(bytes, window, id);
fill(bytes + size - window, window, id + 1);
return intact(bytes, window, id) && intact(bytes + size - window, window, id + 1);
};
bool ok_blk = true;
bool ok_array = true;
for (std::size_t i = 0; i < std::size(sizes); ++i)
{
const std::size_t size = sizes[i];
void* ptr = fast_alloc::BlkAllocator::alloc_block(size);
ok_blk = ok_blk && ptr != nullptr && ends_ok(ptr, size, 10 * i);
fast_alloc::BlkAllocator::free_block(ptr, size);
void* arr = fast_alloc::BlkAllocator::alloc_array(size);
ok_array = ok_array && arr != nullptr && ends_ok(arr, size, 10 * i + 5);
fast_alloc::BlkAllocator::free_array(arr);
}
expect(ok_blk, "alloc_block/free_block: 2 GiB - 64, 2 GiB + 64, 4 GiB + 64");
expect(ok_array, "alloc_array/free_array: same sizes, size_t header");
}
/// @brief Many live blocks at once: no two overlap; then reuse after free.
void check_distinct()
{
constexpr std::size_t n_items = 3000;
for (int round = 0; round < 2; ++round)
{
std::vector<void*> blk(n_items);
std::vector<void*> arr(n_items);
std::vector<std::size_t> sz(n_items);
for (std::size_t j = 0; j < n_items; ++j)
{
sz[j] = test_sizes[j % 20]; // up to 65537: the ladder and its fallback
blk[j] = fast_alloc::BlkAllocator::alloc_block(sz[j]);
arr[j] = fast_alloc::BlkAllocator::alloc_array(sz[j]);
fill(blk[j], sz[j], 2 * j);
fill(arr[j], sz[j], 2 * j + 1);
}
bool ok = true;
for (std::size_t j = 0; j < n_items; ++j)
{
ok = ok && intact(blk[j], sz[j], 2 * j) && intact(arr[j], sz[j], 2 * j + 1);
}
for (std::size_t j = 0; j < n_items; ++j)
{
fast_alloc::BlkAllocator::free_block(blk[j], sz[j]);
fast_alloc::BlkAllocator::free_array(arr[j]);
}
expect(ok, round == 0 ? "3000 + 3000 live blocks of mixed sizes never overlap"
: "same after every block was freed and taken again");
}
}
/// @brief Every thread at once (OpenMP builds); a serial build runs it on one.
void check_parallel()
{
long bad = 0;
#pragma omp parallel reduction(+ : bad)
{
for (int it = 0; it < 2000; ++it)
{
const std::size_t size = test_sizes[static_cast<std::size_t>(it) % 20];
const std::size_t id = static_cast<std::size_t>(it) * 97u;
void* ptr =
(it % 2) ? fast_alloc::BlkAllocator::alloc_array(size) : fast_alloc::BlkAllocator::alloc_block(size);
fill(ptr, size, id);
if (!intact(ptr, size, id))
{
++bad;
}
if (it % 2)
{
fast_alloc::BlkAllocator::free_array(ptr);
}
else
{
fast_alloc::BlkAllocator::free_block(ptr, size);
}
}
}
// Quiescent point: the blocks freed inside the region were batched per
// thread; hand them back to the ladder (what the chunk library's
// parallel_end callback does).
fast_alloc::StaticConstArray::clear_info_free_elem();
expect(bad == 0, "every thread allocating, writing and freeing at once");
}
/// @brief The ladder reached from inside the CALLER's own unnamed critical
/// section, by every thread. The ladder's criticals are named
/// (fast_alloc_ladder): unnamed, they were the same lock as the
/// caller's, and a critical is not reentrant -- this hung.
void check_inside_caller_critical()
{
long bad = 0;
#pragma omp parallel reduction(+ : bad)
{
for (int it = 0; it < 200; ++it)
{
const std::size_t size = test_sizes[static_cast<std::size_t>(it) % 20];
const std::size_t id = static_cast<std::size_t>(it) * 131u;
#pragma omp critical
{
void* ptr = fast_alloc::BlkAllocator::alloc_block(size);
fill(ptr, size, id);
if (!intact(ptr, size, id))
{
++bad;
}
fast_alloc::BlkAllocator::free_block(ptr, size);
}
}
}
fast_alloc::StaticConstArray::clear_info_free_elem();
expect(bad == 0, "alloc and free from inside the caller's own unnamed omp critical");
}
} // namespace
int main()
{
println("=== blk_allocator_validation ===");
check_sizes();
check_huge();
check_distinct();
check_parallel();
check_inside_caller_critical();
check_distinct(); // the ladder is still sound after the batches went back
println("\n{}", fails ? "=== BLK ALLOCATOR FAILED ===" : "=== BLK ALLOCATOR OK ===");
return fails ? 1 : 0;
}