-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathpmr_alloc_test.cpp
More file actions
433 lines (329 loc) · 12.3 KB
/
pmr_alloc_test.cpp
File metadata and controls
433 lines (329 loc) · 12.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2018 IncludeOS AS, Oslo, Norway
//
// 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.
#define DEBUG_UNIT
#include <common.cxx>
#include <util/alloc_pmr.hpp>
#include <util/units.hpp>
#if __has_include(<experimental/memory_resource>)
#include <experimental/map>
#endif
#include <map>
#include <unordered_map>
CASE("pmr::default_pmr_resource") {
std::pmr::vector<int> numbers;
for (int i = 0; i < 1000; i++) {
numbers.push_back(i * 42);
}
EXPECT(numbers.size() == 1000);
}
CASE("pmr::Pmr_pool usage") {
using namespace util;
constexpr auto pool_cap = 40_MiB;
// Using default resource capacity, which is pool_cap / allocator count
os::mem::Pmr_pool pool{pool_cap, pool_cap};
EXPECT(pool.total_capacity() == pool_cap);
auto res = pool.get_resource();
EXPECT(res->capacity() == pool_cap);
auto sub_free = res->allocatable();
std::pmr::polymorphic_allocator<uintptr_t> alloc{res.get()};
std::pmr::vector<uintptr_t> numbers{alloc};
EXPECT(numbers.capacity() < 1000);
numbers.reserve(1000);
EXPECT(numbers.capacity() == 1000);
for (auto i : test::random)
numbers.push_back(i);
for (size_t i = 0; i < numbers.size(); i++) {
EXPECT(numbers.at(i) == test::random.at(i));
}
EXPECT(res->allocatable() <= sub_free - 1000);
// Apparently the same allocator can just be passed to any other container.
std::pmr::map<int, std::string> maps{alloc};
maps[42] = "Allocators are interchangeable";
EXPECT(maps[42] == "Allocators are interchangeable");
// Allocator storage is kept by the container itself.
using my_alloc = std::pmr::polymorphic_allocator<uintptr_t>;
auto unique_alloc = std::make_unique<my_alloc>(res.get());
std::pmr::vector<std::string> my_strings(*unique_alloc);
my_strings.push_back("Hello PMR");
my_strings.push_back("Hello again PMR");
unique_alloc.reset();
my_strings.push_back("Still works");
EXPECT(my_strings.back() == "Still works");
// Using small res capacity
constexpr auto alloc_cap = 4_KiB;
os::mem::Pmr_pool pool2{pool_cap, alloc_cap};
EXPECT(pool2.total_capacity() == pool_cap);
EXPECT(pool2.allocatable() == pool_cap);
auto res2 = pool2.get_resource();
EXPECT(res2->capacity() == alloc_cap);
auto sub_free2 = res2->allocatable();
std::pmr::polymorphic_allocator<int> alloc2{res2.get()};
std::pmr::vector<int> numbers2{alloc2};
EXPECT(numbers2.capacity() < 1000);
numbers2.reserve(1000);
EXPECT(numbers2.capacity() == 1000);
EXPECT(res2->allocatable() <= sub_free2 - 1000);
EXPECT_THROWS(numbers2.reserve(8_KiB));
numbers2.clear();
numbers2.shrink_to_fit();
EXPECT(res2->allocatable() == alloc_cap);
EXPECT(res2->allocated() == 0);
EXPECT(pool2.allocatable() == pool_cap);
EXPECT(pool2.allocated() == 0);
numbers2.push_back(1);
EXPECT(numbers2.capacity() > 0);
EXPECT(numbers2.capacity() < 1000);
EXPECT(res2->allocatable() < alloc_cap);
EXPECT(res2->allocatable() > alloc_cap - 1000);
}
CASE("pmr::resource usage") {
using namespace util;
constexpr auto pool_cap = 400_KiB;
constexpr auto resource_cap = 4_KiB;
constexpr auto resource_count = 100;
os::mem::Pmr_pool pool{pool_cap, resource_cap, resource_count};
// Get resources enought to saturate pool.
std::vector<os::mem::Pmr_pool::Resource_ptr> resources;
// Get first resource for comparing it's pool ptr.
auto res1 = pool.get_resource();
EXPECT(res1 != nullptr);
auto pool_ptr = res1->pool();
resources.emplace_back(std::move(res1));
// Resources are created on-demand, up to max
for (int i = 0; i < resource_count - 1; i++) {
EXPECT(pool.resource_count() == i + 1);
auto res = pool.get_resource();
EXPECT(res != nullptr);
EXPECT(res->capacity() == resource_cap);
EXPECT(res->allocated() == 0);
EXPECT(res->pool() == pool_ptr);
resources.emplace_back(std::move(res));
}
// You now can't get more resources
auto unavail = pool.get_resource();
EXPECT(unavail == nullptr);
EXPECT(resources.size() == resource_count);
EXPECT(pool.resource_count() == resource_count);
EXPECT(pool_ptr->free_resources() == 0);
EXPECT(pool_ptr->used_resources() == resource_count);
std::unordered_map<os::mem::Pmr_resource*, std::vector<void*>> allocations{};
// Drain all the resources
for (auto& res : resources) {
auto exp_alloc = resource_cap;
EXPECT(not res->full());
EXPECT(pool.allocatable() >= exp_alloc);
EXPECT(res->allocatable() == exp_alloc);
EXPECT(res->allocated() == 0);
auto* p1 = res->allocate(1_KiB);
exp_alloc -= 1_KiB;
EXPECT(res->allocated() == 1_KiB);
EXPECT(res->capacity() == resource_cap);
EXPECT(pool.allocatable() >= exp_alloc);
EXPECT(res->allocatable() == exp_alloc);
auto* p2 = res->allocate(1_KiB);
exp_alloc -= 1_KiB;
EXPECT(res->allocated() == 2_KiB);
EXPECT(pool.allocatable() >= exp_alloc);
EXPECT(res->allocatable() == exp_alloc);
auto* p3 = res->allocate(1_KiB);
exp_alloc -= 1_KiB;
EXPECT(res->allocated() == 3_KiB);
EXPECT(pool.allocatable() >= exp_alloc);
EXPECT(res->allocatable() == exp_alloc);
auto* p4 = res->allocate(1_KiB);
exp_alloc -= 1_KiB;
EXPECT(res->allocated() == 4_KiB);
EXPECT(pool.allocatable() >= exp_alloc);
EXPECT(res->allocatable() == exp_alloc);
EXPECT(p1 != nullptr);
EXPECT(p2 != nullptr);
EXPECT(p3 != nullptr);
EXPECT(p4 != nullptr);
allocations[res.get()].push_back(p1);
allocations[res.get()].push_back(p2);
allocations[res.get()].push_back(p3);
allocations[res.get()].push_back(p4);
EXPECT(res->full());
EXPECT_THROWS(res->allocate(1_KiB));
EXPECT(res->full());
}
// The pool is now also full
EXPECT(pool.full());
// So resources can't allocate more from it.
// (Pools pmr api is hidden behind implementation for now.)
EXPECT_THROWS(pool_ptr->allocate(1_KiB));
int i = 0;
// Return all resources
for (auto& res : resources) {
EXPECT(pool_ptr->free_resources() == i++);
res.reset();
}
for (auto& res : resources) {
EXPECT(res == nullptr);
}
// The resource count remains constant, but all free no used.
EXPECT(pool.resource_count() == resource_count);
EXPECT(pool_ptr->free_resources() == resource_count);
EXPECT(pool_ptr->used_resources() == 0);
// Pool is still full - deallocations haven't happened
EXPECT(pool.full());
EXPECT(pool.allocatable() == 0);
// NOTE: it's currently possible to deallocate directly to the detail::Pool_ptr
// this is an implementation detail prone to change as each allocator's state
// won't e updated accordingly.
for (auto[pool, vec] : allocations)
for (auto alloc : vec)
pool->deallocate(alloc, 1_KiB);
EXPECT(pool.empty());
EXPECT(not pool.full());
EXPECT(pool.allocatable() == pool_cap);
EXPECT(pool.resource_count() == resource_count);
auto res_reused = pool.get_resource();
EXPECT(pool.resource_count() == resource_count);
EXPECT(res_reused->empty());
EXPECT(res_reused->allocatable() == resource_cap);
EXPECT(pool_ptr->free_resources() == resource_count - 1);
EXPECT(pool_ptr->used_resources() == 1);
res_reused.reset();
auto res2 = pool.get_resource();
EXPECT(not res2->full());
EXPECT(res2->allocatable() == resource_cap);
auto ptr = res2->allocate(1_KiB);
EXPECT(ptr != nullptr);
res2->deallocate(ptr, 1_KiB);
EXPECT(res2->allocatable() == resource_cap);
}
CASE("pmr::on_non_full event") {
using namespace util;
constexpr auto pool_cap = 400_KiB;
// Using default resource capacity, which is pool_cap / allocator count
os::mem::Pmr_pool pool{pool_cap, pool_cap};
auto res = pool.get_resource();
bool event_fired = false;
res->on_non_full([&](auto& r){
EXPECT(&r == res.get());
EXPECT(not r.full());
event_fired = true;
});
std::pmr::polymorphic_allocator<uintptr_t> alloc{res.get()};
std::pmr::vector<char> numbers{alloc};
auto reserved = pool_cap - 2;
numbers.reserve(reserved);
EXPECT(numbers.capacity() == reserved);
EXPECT(res->allocated() == reserved);
EXPECT(not event_fired);
numbers.push_back(0);
numbers.push_back(1);
// In order to shrink, it needs to allocate new space for 2 chars then copy.
numbers.shrink_to_fit();
EXPECT(res->allocated() < reserved);
EXPECT(event_fired);
event_fired = false;
EXPECT(not event_fired);
for (size_t i = 2; i < pool_cap / 2; i++) {
numbers.push_back(i);
}
EXPECT(not event_fired);
EXPECT(not res->full());
// Reduce capacity, making the resource full right now
pool.set_resource_capacity(pool_cap / 3);
numbers.clear();
numbers.shrink_to_fit();
EXPECT(event_fired);
}
CASE("pmr::on_avail event") {
using namespace util;
constexpr auto pool_cap = 400_KiB;
// Using default resource capacity, which is pool_cap / allocator count
os::mem::Pmr_pool pool{pool_cap, pool_cap};
auto res = pool.get_resource();
bool event_fired = false;
res->on_avail(200_KiB, [&](auto& r){
EXPECT(&r == res.get());
EXPECT(not r.full());
EXPECT(r.allocatable() >= 200_KiB);
event_fired = true;
});
std::pmr::polymorphic_allocator<uintptr_t> alloc{res.get()};
std::pmr::vector<char> numbers{alloc};
numbers.push_back(0);
numbers.push_back(1);
EXPECT(not event_fired);
auto reserved = 201_KiB;
numbers.reserve(reserved);
EXPECT(numbers.capacity() == reserved);
EXPECT(res->allocated() == reserved);
EXPECT(not event_fired);
// In order to shrink, it needs to allocate new space for 2 chars then copy.
numbers.shrink_to_fit();
EXPECT(res->allocated() < reserved);
EXPECT(event_fired);
event_fired = false;
EXPECT(not event_fired);
for (size_t i = 2; i < 40_KiB; i++) {
numbers.push_back(i);
}
EXPECT(not event_fired);
EXPECT(not res->full());
numbers.clear();
numbers.shrink_to_fit();
EXPECT(not event_fired);
}
CASE("pmr::default resource cap") {
// Not providing a resource cap will give each resource a proportion of max
using namespace util;
constexpr auto pool_cap = 400_KiB;
// Using default resource capacity, which is pool_cap / allocator count
os::mem::Pmr_pool pool{pool_cap};
auto res1 = pool.get_resource();
auto expected = pool_cap / (1 + os::mem::Pmr_pool::resource_division_offset);
EXPECT(res1->allocatable() == expected);
auto res2 = pool.get_resource();
expected = pool_cap / (2 + os::mem::Pmr_pool::resource_division_offset);
EXPECT(res2->allocatable() == expected);
auto res3 = pool.get_resource();
expected = pool_cap / (3 + os::mem::Pmr_pool::resource_division_offset);
EXPECT(res3->allocatable() == expected);
auto res4 = pool.get_resource();
expected = pool_cap / (4 + os::mem::Pmr_pool::resource_division_offset);
EXPECT(res4->allocatable() == expected);
}
CASE("allocation alignment") {
// Check that allocator uses the size of the data type as alignment
using namespace util;
constexpr auto pool_cap = 400_KiB;
os::mem::Pmr_pool pool{pool_cap, pool_cap};
auto res = pool.get_resource();
std::pmr::polymorphic_allocator<uintptr_t> alloc{res.get()};
// Allocate 1 entry per type size
std::pmr::vector<uint8_t> numbers_1{alloc};
std::pmr::vector<uint16_t> numbers_2{alloc};
std::pmr::vector<uint32_t> numbers_4{alloc};
std::pmr::vector<uint64_t> numbers_8{alloc};
auto reserved = 1;
auto previously_allocated = res->allocated();
numbers_1.reserve(reserved);
EXPECT(res->allocated() == reserved + previously_allocated);
previously_allocated = res->allocated();
numbers_2.reserve(reserved);
EXPECT(res->allocated() == reserved + 1 + previously_allocated);
previously_allocated = res->allocated();
numbers_4.reserve(reserved);
EXPECT(res->allocated() == reserved + 3 + previously_allocated);
previously_allocated = res->allocated();
numbers_8.reserve(reserved);
EXPECT(res->allocated() == reserved + 7 + previously_allocated);
}