| 1 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 2 |
// software; you can redistribute it and/or modify it under the |
| 3 |
// terms of the GNU General Public License as published by the |
| 4 |
// Free Software Foundation; either version 3, or (at your option) |
| 5 |
// any later version. |
| 6 |
|
| 7 |
// This library is distributed in the hope that it will be useful, |
| 8 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 |
// GNU General Public License for more details. |
| 11 |
|
| 12 |
// You should have received a copy of the GNU General Public License along |
| 13 |
// with this library; see the file COPYING3. If not see |
| 14 |
// <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
// Override the -std flag in the check_performance script: STD=gnu++17 |
| 17 |
|
| 18 |
// Run the test as both single- and multi-threaded: TEST_B |
| 19 |
|
| 20 |
#include <memory_resource> |
| 21 |
#include <list> |
| 22 |
#include <string> |
| 23 |
#include <testsuite_performance.h> |
| 24 |
|
| 25 |
const int iterations = 100; |
| 26 |
|
| 27 |
// Insert and remove elements of various sizes in std::list containers. |
| 28 |
// If timers!=nullptr the function will pause the timer while the lists |
| 29 |
// are cleared and deallocated, so that only insertions/removals are timed. |
| 30 |
// Otherwise, the time taken to deallocate the lists is also counted. |
| 31 |
void |
| 32 |
populate_lists(std::pmr::memory_resource* r, __gnu_test::time_counter* timers, |
| 33 |
int kmax = iterations) |
| 34 |
{ |
| 35 |
struct size16 { char c[16]; }; |
| 36 |
struct size32 { char c[32]; }; |
| 37 |
struct size64 { char c[64]; }; |
| 38 |
struct size128 { char c[128]; }; |
| 39 |
|
| 40 |
std::pmr::list<int> l4(r); |
| 41 |
std::pmr::list<size16> l16(r); |
| 42 |
std::pmr::list<size32> l32(r); |
| 43 |
std::pmr::list<size64> l64(r); |
| 44 |
std::pmr::list<size128> l128(r); |
| 45 |
|
| 46 |
const int imax = 1000; |
| 47 |
const int jmax = 100; |
| 48 |
for (int k = 0; k < kmax; ++k) |
| 49 |
{ |
| 50 |
for (int i = 0; i < imax; ++i) |
| 51 |
{ |
| 52 |
for (int j = 0; j < jmax; ++j) |
| 53 |
{ |
| 54 |
l4.emplace_back(); |
| 55 |
l16.emplace_back(); |
| 56 |
l32.emplace_back(); |
| 57 |
l64.emplace_back(); |
| 58 |
l128.emplace_back(); |
| 59 |
} |
| 60 |
l4.pop_front(); |
| 61 |
l16.pop_front(); |
| 62 |
l32.pop_front(); |
| 63 |
l64.pop_front(); |
| 64 |
l128.pop_front(); |
| 65 |
} |
| 66 |
|
| 67 |
if (timers) |
| 68 |
timers->stop(); |
| 69 |
|
| 70 |
// Deallocate everything: |
| 71 |
l4.clear(); |
| 72 |
l16.clear(); |
| 73 |
l32.clear(); |
| 74 |
l64.clear(); |
| 75 |
l128.clear(); |
| 76 |
|
| 77 |
if (timers) |
| 78 |
timers->restart(); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// Test allocations and deallocations of node-based containers (std::list). |
| 83 |
// In this test pmr::unsynchronized_pool_resource should be faster than |
| 84 |
// pmr::new_delete_resource(). |
| 85 |
void test_lists_single_thread() |
| 86 |
{ |
| 87 |
std::pmr::memory_resource* newdel = std::pmr::new_delete_resource(); |
| 88 |
std::pmr::unsynchronized_pool_resource pool; |
| 89 |
#ifndef NOTHREAD |
| 90 |
std::pmr::synchronized_pool_resource syncpool; |
| 91 |
#endif |
| 92 |
|
| 93 |
auto run_test = [](auto* memres, std::string name, bool time_dtors) { |
| 94 |
name += " std::list push/pop"; |
| 95 |
if (time_dtors) |
| 96 |
name += "/destroy"; |
| 97 |
__gnu_test::time_counter time; |
| 98 |
__gnu_test::resource_counter resource; |
| 99 |
start_counters(time, resource); |
| 100 |
populate_lists(memres, time_dtors ? nullptr : &time); |
| 101 |
stop_counters(time, resource); |
| 102 |
report_performance(__FILE__, name, time, resource); |
| 103 |
}; |
| 104 |
|
| 105 |
for (auto time_dtors : {false, true}) |
| 106 |
{ |
| 107 |
run_test(newdel, "new-delete-1 ", time_dtors); |
| 108 |
run_test(newdel, "new-delete-2 ", time_dtors); |
| 109 |
run_test(newdel, "new-delete-3 ", time_dtors); |
| 110 |
|
| 111 |
// Start with an empty set of pools: |
| 112 |
pool.release(); |
| 113 |
run_test(&pool, "unsync-pool-1", time_dtors); |
| 114 |
// Destroy pools and start fresh: |
| 115 |
pool.release(); |
| 116 |
run_test(&pool, "unsync-pool-2", time_dtors); |
| 117 |
// Do not destroy pools, reuse allocated memory: |
| 118 |
run_test(&pool, "unsync-pool-3", time_dtors); |
| 119 |
|
| 120 |
#ifndef NOTHREAD |
| 121 |
syncpool.release(); |
| 122 |
run_test(&syncpool, "sync-pool-1 ", time_dtors); |
| 123 |
// Destroy pools and start fresh: |
| 124 |
syncpool.release(); |
| 125 |
run_test(&syncpool, "sync-pool-2 ", time_dtors); |
| 126 |
// Do not destroy pools, reuse allocated memory: |
| 127 |
run_test(&syncpool, "sync-pool-3 ", time_dtors); |
| 128 |
#endif |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
// TODO test non-pooled large allocations from (un)synchronized_pool_resource |
| 133 |
|
| 134 |
#ifndef NOTHREAD |
| 135 |
# include <thread> |
| 136 |
# include <mutex> |
| 137 |
# include <cassert> |
| 138 |
|
| 139 |
// Multithreaded std::list test with each thread having its own resource. |
| 140 |
// (pmr::new_delete vs pmr::unsynchronized_pool vs pmr::synchronized_pool) |
| 141 |
// |
| 142 |
// In this test both pmr::unsynchronized_pool_resource and |
| 143 |
// pmr::synchronized_pool_resource should be faster than |
| 144 |
// pmr::new_delete_resource(). |
| 145 |
void test_lists_resource_per_thread() |
| 146 |
{ |
| 147 |
std::mutex mx; |
| 148 |
std::unique_lock<std::mutex> gate(mx, std::defer_lock); |
| 149 |
|
| 150 |
struct state |
| 151 |
{ |
| 152 |
std::thread thread; |
| 153 |
|
| 154 |
// Per-thread pool resources: |
| 155 |
std::pmr::unsynchronized_pool_resource unsync; |
| 156 |
std::pmr::synchronized_pool_resource sync; |
| 157 |
|
| 158 |
std::pmr::memory_resource* memres[3] = { |
| 159 |
std::pmr::new_delete_resource(), &unsync, &sync |
| 160 |
}; |
| 161 |
}; |
| 162 |
|
| 163 |
state states[4]; |
| 164 |
|
| 165 |
const std::string resnames[] = {"new-delete ", "unsync-pool", "sync-pool "}; |
| 166 |
|
| 167 |
auto run_test = [&mx] (std::pmr::memory_resource* memres, |
| 168 |
__gnu_test::time_counter* timers) |
| 169 |
{ |
| 170 |
{ |
| 171 |
// block until the mutex can be locked |
| 172 |
std::lock_guard<std::mutex> wait_for_gate_to_be_unlocked{mx}; |
| 173 |
} |
| 174 |
populate_lists(memres, timers); |
| 175 |
}; |
| 176 |
|
| 177 |
auto time_threads = [&] (std::string testname, bool time_dtors, int which) { |
| 178 |
__gnu_test::time_counter time; |
| 179 |
__gnu_test::resource_counter resource; |
| 180 |
gate.lock(); |
| 181 |
auto* time_ptr = time_dtors ? nullptr : &time; |
| 182 |
for (auto& s : states) |
| 183 |
s.thread = std::thread{ run_test, s.memres[which], time_ptr }; |
| 184 |
start_counters(time, resource); |
| 185 |
gate.unlock(); // let the threads run |
| 186 |
for (auto& s : states) |
| 187 |
s.thread.join(); |
| 188 |
stop_counters(time, resource); |
| 189 |
report_performance(__FILE__, resnames[which] + testname, time, resource); |
| 190 |
}; |
| 191 |
|
| 192 |
for (auto time_dtors : {false, true}) |
| 193 |
{ |
| 194 |
std::string testname = " resource-per-thread std::list push/pop"; |
| 195 |
if (time_dtors) |
| 196 |
testname += "/destroy"; |
| 197 |
for (int which : {0, 1, 2}) |
| 198 |
time_threads(testname, time_dtors, which); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
// A naive memory_resource that adds a mutex to unsynchronized_pool_resource |
| 203 |
struct locking_pool_resource : std::pmr::unsynchronized_pool_resource |
| 204 |
{ |
| 205 |
void* do_allocate(std::size_t b, std::size_t a) override |
| 206 |
{ |
| 207 |
std::lock_guard<std::mutex> l(m); |
| 208 |
return unsynchronized_pool_resource::do_allocate(b, a); |
| 209 |
} |
| 210 |
|
| 211 |
void do_deallocate(void* p, std::size_t b, std::size_t a) override |
| 212 |
{ |
| 213 |
std::lock_guard<std::mutex> l(m); |
| 214 |
return unsynchronized_pool_resource::do_deallocate(p, b, a); |
| 215 |
} |
| 216 |
|
| 217 |
std::mutex m; |
| 218 |
}; |
| 219 |
|
| 220 |
// Multithreaded std::list test with all threads sharing the same resource. |
| 221 |
// (new_delete vs unsynchronized_pool+mutex vs synchronized_pool) |
| 222 |
// |
| 223 |
// pmr::synchronized_pool_resource is not expected to be anywhere near |
| 224 |
// as fast as pmr::new_delete_resource() here, but should perform much |
| 225 |
// better than the naive locking_pool_resource type. |
| 226 |
void test_lists_shared_resource() |
| 227 |
{ |
| 228 |
std::mutex mx; |
| 229 |
std::unique_lock<std::mutex> gate(mx, std::defer_lock); |
| 230 |
|
| 231 |
locking_pool_resource unsync; |
| 232 |
std::pmr::synchronized_pool_resource sync; |
| 233 |
|
| 234 |
std::pmr::memory_resource* memres[3] = { |
| 235 |
std::pmr::new_delete_resource(), &unsync, &sync |
| 236 |
}; |
| 237 |
|
| 238 |
std::thread threads[4]; |
| 239 |
|
| 240 |
const std::string resnames[3] = { "new-delete", "mutex-pool", "sync-pool " }; |
| 241 |
|
| 242 |
auto run_test = [&mx] (std::pmr::memory_resource* memres, |
| 243 |
__gnu_test::time_counter* timers) |
| 244 |
{ |
| 245 |
{ |
| 246 |
// block until the mutex can be locked |
| 247 |
std::lock_guard<std::mutex> wait_for_gate_to_be_unlocked{mx}; |
| 248 |
} |
| 249 |
populate_lists(memres, timers); |
| 250 |
}; |
| 251 |
|
| 252 |
auto time_threads = [&] (std::string testname, bool time_dtors, int which) { |
| 253 |
__gnu_test::time_counter time; |
| 254 |
__gnu_test::resource_counter resource; |
| 255 |
gate.lock(); |
| 256 |
auto* time_ptr = time_dtors ? nullptr : &time; |
| 257 |
for (auto& t : threads) |
| 258 |
t = std::thread{ run_test, memres[which], time_ptr }; |
| 259 |
start_counters(time, resource); |
| 260 |
gate.unlock(); // let the threads run |
| 261 |
for (auto& t : threads) |
| 262 |
t.join(); |
| 263 |
stop_counters(time, resource); |
| 264 |
report_performance(__FILE__, resnames[which] + testname, time, resource); |
| 265 |
}; |
| 266 |
|
| 267 |
for (auto time_dtors : {false, true}) |
| 268 |
{ |
| 269 |
std::string testname = " shared-resource std::list push/pop"; |
| 270 |
if (time_dtors) |
| 271 |
testname += "/destroy"; |
| 272 |
for (int which : {0, 1, 2}) |
| 273 |
time_threads(testname, time_dtors, which); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
// TODO threaded test just doing loads of allocations, no deallocs |
| 278 |
// both with per-thread resource (unsync vs sync vs newdel) |
| 279 |
// and shared resource (locked vs sync vs newdel) |
| 280 |
|
| 281 |
// TODO threaded test just doing loads of deallocations, no allocs |
| 282 |
// both with per-thread resource (unsync vs sync vs newdel) |
| 283 |
// and shared resource (locked vs sync vs newdel) |
| 284 |
|
| 285 |
// Multithreaded test where deallocations happen on different threads. |
| 286 |
// (new_delete vs unsynchronized_pool+mutex vs synchronized_pool) |
| 287 |
// |
| 288 |
// This hits the slow path for pmr::synchronized_pool_resource, where |
| 289 |
// an exclusive lock must be taken to access other threads' pools. |
| 290 |
// pmr::synchronized_pool_resource is not expected to be anywhere near |
| 291 |
// as fast as pmr::new_delete_resource() here, but should perform much |
| 292 |
// better than the naive locking_pool_resource type. |
| 293 |
void test_cross_thread_dealloc() |
| 294 |
{ |
| 295 |
const int num_threads = 4; |
| 296 |
|
| 297 |
struct X { |
| 298 |
void* ptr; |
| 299 |
unsigned size; |
| 300 |
}; |
| 301 |
|
| 302 |
// A buffer for each thread, and extra buffers for half of the threads: |
| 303 |
std::vector<X> allocs[num_threads * 3 / 2]; |
| 304 |
for (auto& v : allocs) |
| 305 |
v.resize(1000 * iterations); |
| 306 |
|
| 307 |
// Use a few different pools |
| 308 |
const std::size_t sizes[] = { 8, 16, 8, 16, 32, 64, 8, 16, 32, 64 }; |
| 309 |
|
| 310 |
std::mutex mx; |
| 311 |
|
| 312 |
auto run_test = |
| 313 |
[&, num_threads] (std::pmr::memory_resource* memres, int i, bool with_exit) |
| 314 |
{ |
| 315 |
std::size_t counter = 0; |
| 316 |
{ |
| 317 |
// block until the mutex can be locked |
| 318 |
std::lock_guard<std::mutex> wait_for_gate_to_be_unlocked{mx}; |
| 319 |
} |
| 320 |
// Fill this thread's buffer with allocations: |
| 321 |
for (X& x : allocs[i]) |
| 322 |
{ |
| 323 |
x.size = sizes[counter++ % 10]; |
| 324 |
x.ptr = memres->allocate(x.size, 1); |
| 325 |
} |
| 326 |
|
| 327 |
if (with_exit && i == 0) |
| 328 |
{ |
| 329 |
// One of the threads exits, so that its pools transfer to the |
| 330 |
// non-thread-specific list of pools. |
| 331 |
return; |
| 332 |
} |
| 333 |
else if (i < num_threads / 2) |
| 334 |
{ |
| 335 |
// Other threads continue allocating, into the extra buffers: |
| 336 |
for (X& x : allocs[num_threads + i]) |
| 337 |
{ |
| 338 |
x.size = sizes[counter++ % 10]; |
| 339 |
x.ptr = memres->allocate(x.size, 1); |
| 340 |
} |
| 341 |
} |
| 342 |
else |
| 343 |
{ |
| 344 |
// Half of the threads start deallocating their own memory and the |
| 345 |
// memory belonging to another pool |
| 346 |
const int other = i - num_threads / 2; |
| 347 |
for (unsigned n = 0; n < allocs[i].size(); ++n) |
| 348 |
{ |
| 349 |
// Deallocate memory allocated in this thread: |
| 350 |
X& x1 = allocs[i][n]; |
| 351 |
memres->deallocate(x1.ptr, x1.size, 1); |
| 352 |
x1 = {}; |
| 353 |
// Deallocate memory allocated in another thread: |
| 354 |
X& x2 = allocs[other][n]; |
| 355 |
memres->deallocate(x2.ptr, x2.size, 1); |
| 356 |
x2 = {}; |
| 357 |
} |
| 358 |
} |
| 359 |
}; |
| 360 |
|
| 361 |
std::thread threads[num_threads]; |
| 362 |
|
| 363 |
locking_pool_resource unsync; |
| 364 |
std::pmr::synchronized_pool_resource sync; |
| 365 |
|
| 366 |
std::pmr::memory_resource* memres[3] = { |
| 367 |
std::pmr::new_delete_resource(), &unsync, &sync |
| 368 |
}; |
| 369 |
const std::string resnames[3] = { "new-delete", "mutex-pool", "sync-pool " }; |
| 370 |
|
| 371 |
auto time_threads = [&] (std::string name, int which, bool with_exit) |
| 372 |
{ |
| 373 |
__gnu_test::time_counter time; |
| 374 |
__gnu_test::resource_counter resource; |
| 375 |
std::unique_lock<std::mutex> gate(mx); |
| 376 |
for (auto& t : threads) |
| 377 |
t = std::thread{ run_test, memres[which], &t - threads, with_exit }; |
| 378 |
start_counters(time, resource); |
| 379 |
gate.unlock(); |
| 380 |
for (auto& t : threads) |
| 381 |
t.join(); |
| 382 |
stop_counters(time, resource); |
| 383 |
report_performance(__FILE__, resnames[which] + name, time, resource); |
| 384 |
|
| 385 |
// Clean up: |
| 386 |
for (auto& a : allocs) |
| 387 |
{ |
| 388 |
const int i = (&a - allocs); |
| 389 |
if (i < num_threads) // These allocations were freed |
| 390 |
for (auto& x : a) |
| 391 |
{ |
| 392 |
assert(x.ptr == nullptr); |
| 393 |
} |
| 394 |
else if (with_exit && i == num_threads) |
| 395 |
; |
| 396 |
else |
| 397 |
for (auto& x : a) |
| 398 |
{ |
| 399 |
memres[which]->deallocate(x.ptr, x.size, 1); |
| 400 |
x = {}; |
| 401 |
} |
| 402 |
} |
| 403 |
}; |
| 404 |
|
| 405 |
for (int which : {0, 1, 2}) |
| 406 |
time_threads(" cross-thread dealloc", which, false); |
| 407 |
for (int which : {0, 1, 2}) |
| 408 |
time_threads(" cross-thread dealloc w/exit", which, true); |
| 409 |
} |
| 410 |
#endif |
| 411 |
|
| 412 |
int main() |
| 413 |
{ |
| 414 |
test_lists_single_thread(); |
| 415 |
#ifndef NOTHREAD |
| 416 |
test_lists_resource_per_thread(); |
| 417 |
test_lists_shared_resource(); |
| 418 |
test_cross_thread_dealloc(); |
| 419 |
#endif |
| 420 |
} |