feat(Containers): fix pool lease lifetimes/budgets and non-spinning MPSC ring (M-CONTAINERS-02, M-CONTAINERS-03)
This commit is contained in:
1 parent
883bbdcf38
commit
9ac76bbcf9
7 files changed
+1252
-285
No files matched your search
@@ -1,8 +1,11 @@
|
||||
#include <Void/Containers/BufferPool.hpp>
|
||||
#include <Void/Memory/BudgetResource.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Suite.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -82,5 +85,92 @@ namespace
|
||||
pool.shrink();
|
||||
Void::Testing::expect_eq(pool.stats().pooled, std::size_t{0});
|
||||
}
|
||||
|
||||
Test leases_survive_pool_move_and_destruction()
|
||||
{
|
||||
Void::Memory::BudgetResource budget{65536};
|
||||
BufferPool<std::uint32_t>::Buffer retained;
|
||||
{
|
||||
BufferPool<std::uint32_t> p1{&budget};
|
||||
auto b = p1.try_acquire(64);
|
||||
Void::Testing::require(b.has_value());
|
||||
(*b)[0] = 0xCAFEBABEu;
|
||||
auto p2 = std::move(p1);
|
||||
retained = std::move(*b);
|
||||
Void::Testing::expect_eq(p2.stats().allocations,
|
||||
std::uint64_t{1});
|
||||
}
|
||||
// Pool destroyed; retained buffer still intact and frees cleanly on drop.
|
||||
Void::Testing::expect_eq(retained[0], 0xCAFEBABEu);
|
||||
Void::Testing::expect(budget.stats().committed_live > 0);
|
||||
retained = BufferPool<std::uint32_t>::Buffer{};
|
||||
Void::Testing::expect_eq(budget.stats().committed_live,
|
||||
std::size_t{0});
|
||||
}
|
||||
|
||||
Test enforces_request_idle_total_limits_and_overflow()
|
||||
{
|
||||
using Void::BufferPool::BufferPoolError;
|
||||
|
||||
BufferPool<std::uint64_t> small_pool{
|
||||
BufferPool<std::uint64_t>::Limits{
|
||||
.max_request_bytes = 256,
|
||||
.max_idle_bytes = 256,
|
||||
.max_total_bytes = 512}};
|
||||
|
||||
const auto ovf = small_pool.try_acquire(
|
||||
std::numeric_limits<std::size_t>::max());
|
||||
Void::Testing::expect(
|
||||
!ovf.has_value() &&
|
||||
ovf.error() == BufferPoolError::size_overflow);
|
||||
|
||||
const auto too_big = small_pool.try_acquire(64); // 512B > 256B
|
||||
Void::Testing::expect(
|
||||
!too_big.has_value() &&
|
||||
too_big.error() == BufferPoolError::request_too_large);
|
||||
|
||||
auto a = small_pool.try_acquire(32); // 256B
|
||||
auto b = small_pool.try_acquire(32); // 256B (total 512B)
|
||||
auto c = small_pool.try_acquire(32); // exceeds 512B
|
||||
Void::Testing::require(a.has_value() && b.has_value());
|
||||
Void::Testing::expect(
|
||||
!c.has_value() &&
|
||||
c.error() == BufferPoolError::budget_exceeded);
|
||||
|
||||
a = {}; // fits in max_idle_bytes=256B
|
||||
b = {}; // exceeds max_idle_bytes=256B -> dropped_on_return
|
||||
Void::Testing::expect_eq(small_pool.stats().pooled,
|
||||
std::size_t{1});
|
||||
Void::Testing::expect_eq(small_pool.stats().idle_bytes,
|
||||
std::size_t{256});
|
||||
Void::Testing::expect_eq(small_pool.stats().dropped_on_return,
|
||||
std::uint64_t{1});
|
||||
}
|
||||
|
||||
Test budget_resource_zero_allocs_on_reuse_and_handles_oom()
|
||||
{
|
||||
using Void::BufferPool::BufferPoolError;
|
||||
|
||||
Void::Memory::BudgetResource budget{1024};
|
||||
BufferPool<std::uint8_t> pool{&budget};
|
||||
{
|
||||
auto b = pool.try_acquire(256);
|
||||
Void::Testing::require(b.has_value());
|
||||
}
|
||||
const auto allocs = budget.stats().allocations;
|
||||
Void::Testing::expect_eq(allocs, std::uint64_t{1});
|
||||
|
||||
{
|
||||
auto b2 = pool.try_acquire(200); // same class 256
|
||||
Void::Testing::require(b2.has_value());
|
||||
Void::Testing::expect_eq(budget.stats().allocations, allocs);
|
||||
Void::Testing::expect_eq(pool.stats().reuses, std::uint64_t{1});
|
||||
}
|
||||
|
||||
const auto fail_alloc = pool.try_acquire(2048);
|
||||
Void::Testing::expect(
|
||||
!fail_alloc.has_value() &&
|
||||
fail_alloc.error() == BufferPoolError::allocation_failed);
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
@@ -1,8 +1,13 @@
|
||||
#include <Void/Containers/Pool.hpp>
|
||||
#include <Void/Memory/BudgetResource.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
// ── ObjectPool ────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -101,3 +106,134 @@ Void::Testing::Test fixed_pool_reuse_after_release()
|
||||
Void::Testing::expect(p3 != nullptr);
|
||||
Void::Testing::expect_eq(*p3, 3);
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
struct TrackedObject
|
||||
{
|
||||
int value{0};
|
||||
std::atomic<int>* dtor_count{nullptr};
|
||||
bool should_throw{false};
|
||||
|
||||
TrackedObject(int v, std::atomic<int>* dc, bool fail = false)
|
||||
: value{v}, dtor_count{dc}, should_throw{fail}
|
||||
{
|
||||
if (should_throw) {
|
||||
throw std::runtime_error{"ctor failure"};
|
||||
}
|
||||
}
|
||||
|
||||
~TrackedObject()
|
||||
{
|
||||
if (dtor_count) {
|
||||
dtor_count->fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test object_pool_leases_survive_move_and_owner_destruction()
|
||||
{
|
||||
std::atomic<int> dtors{0};
|
||||
Void::Pool::ObjectPool<TrackedObject, 4>::Ptr retained_a;
|
||||
Void::Pool::ObjectPool<TrackedObject, 4>::Ptr retained_b;
|
||||
|
||||
{
|
||||
Void::Pool::ObjectPool<TrackedObject, 4> p1;
|
||||
auto a = p1.try_acquire(11, &dtors);
|
||||
Void::Testing::require(a.has_value());
|
||||
retained_a = std::move(*a);
|
||||
|
||||
auto p2 = std::move(p1);
|
||||
Void::Testing::expect_eq(p2.live(), std::size_t{1});
|
||||
auto b = p2.try_acquire(22, &dtors);
|
||||
Void::Testing::require(b.has_value());
|
||||
retained_b = std::move(*b);
|
||||
Void::Testing::expect_eq(p2.live(), std::size_t{2});
|
||||
} // p2 destroyed while retained_a and retained_b remain live
|
||||
|
||||
Void::Testing::expect_eq(dtors.load(), 0);
|
||||
Void::Testing::expect_eq(retained_a->value, 11);
|
||||
Void::Testing::expect_eq(retained_b->value, 22);
|
||||
retained_a.reset();
|
||||
Void::Testing::expect_eq(dtors.load(), 1);
|
||||
retained_b.reset();
|
||||
Void::Testing::expect_eq(dtors.load(), 2);
|
||||
}
|
||||
|
||||
Void::Testing::Test object_pool_enforces_limits_budget_zero_alloc_recycle_and_ctor_throw()
|
||||
{
|
||||
using Void::Pool::PoolError;
|
||||
|
||||
// Constructor exception rolls back slot cleanly without leaking live count.
|
||||
std::atomic<int> dtors{0};
|
||||
Void::Pool::ObjectPool<TrackedObject, 4> exc_pool{
|
||||
Void::Pool::ObjectPoolLimits{.max_live = 2, .max_total_slots = 4}};
|
||||
bool caught = false;
|
||||
try {
|
||||
(void)exc_pool.try_acquire(1, &dtors, true);
|
||||
} catch (const std::runtime_error&) {
|
||||
caught = true;
|
||||
}
|
||||
Void::Testing::expect(caught);
|
||||
Void::Testing::expect_eq(exc_pool.live(), std::size_t{0});
|
||||
Void::Testing::expect_eq(exc_pool.available(), std::size_t{4});
|
||||
Void::Testing::expect_eq(dtors.load(), 0);
|
||||
|
||||
// BudgetResource tracks allocations: zero upstream allocs on recycle & reuse.
|
||||
Void::Memory::BudgetResource budget{1024};
|
||||
Void::Pool::ObjectPool<int, 2> budgeted{
|
||||
&budget,
|
||||
Void::Pool::ObjectPoolLimits{.max_live = 4, .max_total_slots = 16}};
|
||||
{
|
||||
auto p1 = budgeted.try_acquire(10);
|
||||
auto p2 = budgeted.try_acquire(20);
|
||||
Void::Testing::require(p1.has_value() && p2.has_value());
|
||||
Void::Testing::expect_eq(budgeted.live(), std::size_t{2});
|
||||
}
|
||||
const auto allocs_after_first_slab = budget.stats().allocations;
|
||||
Void::Testing::expect_eq(allocs_after_first_slab, std::uint64_t{1});
|
||||
{
|
||||
auto r1 = budgeted.try_acquire(30);
|
||||
auto r2 = budgeted.try_acquire(40);
|
||||
Void::Testing::require(r1.has_value() && r2.has_value());
|
||||
Void::Testing::expect_eq(budget.stats().allocations, allocs_after_first_slab);
|
||||
}
|
||||
|
||||
// max_live limit triggers exhausted.
|
||||
Void::Pool::ObjectPool<int, 4> capped{
|
||||
Void::Pool::ObjectPoolLimits{.max_live = 2, .max_total_slots = 4}};
|
||||
auto c1 = capped.try_acquire(1);
|
||||
auto c2 = capped.try_acquire(2);
|
||||
auto c3 = capped.try_acquire(3);
|
||||
Void::Testing::require(c1.has_value() && c2.has_value());
|
||||
Void::Testing::expect(!c3.has_value() && c3.error() == PoolError::exhausted);
|
||||
|
||||
// Exhausted BudgetResource triggers allocation_failed.
|
||||
Void::Memory::BudgetResource tiny_budget{0};
|
||||
Void::Pool::ObjectPool<int, 4> oom_pool{&tiny_budget};
|
||||
const auto oom = oom_pool.try_acquire(5);
|
||||
Void::Testing::expect(!oom.has_value() && oom.error() == PoolError::allocation_failed);
|
||||
}
|
||||
|
||||
Void::Testing::Test fixed_pool_try_acquire_and_throwing_ctor()
|
||||
{
|
||||
std::atomic<int> dtors{0};
|
||||
Void::Pool::FixedPool<TrackedObject, 2> pool;
|
||||
bool caught = false;
|
||||
try {
|
||||
(void)pool.try_acquire(7, &dtors, true);
|
||||
} catch (const std::runtime_error&) {
|
||||
caught = true;
|
||||
}
|
||||
Void::Testing::expect(caught);
|
||||
Void::Testing::expect_eq(pool.available(), std::size_t{2});
|
||||
Void::Testing::expect_eq(pool.live(), std::size_t{0});
|
||||
|
||||
auto a = pool.try_acquire(1, &dtors);
|
||||
auto b = pool.try_acquire(2, &dtors);
|
||||
auto c = pool.try_acquire(3, &dtors);
|
||||
Void::Testing::require(a.has_value() && b.has_value());
|
||||
Void::Testing::expect(!c.has_value() && c.error() == Void::Pool::PoolError::exhausted);
|
||||
Void::Testing::expect_eq(pool.live(), std::size_t{2});
|
||||
}
|
||||
@@ -2,8 +2,11 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
// ── SpscRing ──────────────────────────────────────────────────────────────────
|
||||
@@ -144,3 +147,173 @@ Void::Testing::Test mpsc_two_producers_one_consumer()
|
||||
p1.join(); p2.join(); consumer.join();
|
||||
Void::Testing::expect_eq(total.load(), N * 3); // N×1 + N×2 = 3N
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
struct CountedItem
|
||||
{
|
||||
int value{0};
|
||||
std::atomic<int>* dtors{nullptr};
|
||||
|
||||
CountedItem(int v, std::atomic<int>* d) noexcept : value{v}, dtors{d} {}
|
||||
CountedItem(CountedItem&& o) noexcept
|
||||
: value{o.value}, dtors{std::exchange(o.dtors, nullptr)}
|
||||
{
|
||||
}
|
||||
CountedItem& operator=(CountedItem&& o) noexcept
|
||||
{
|
||||
if (this != &o) {
|
||||
if (dtors) dtors->fetch_add(1, std::memory_order_relaxed);
|
||||
value = o.value;
|
||||
dtors = std::exchange(o.dtors, nullptr);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
~CountedItem()
|
||||
{
|
||||
if (dtors) {
|
||||
dtors->fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct DelayedMoveItem
|
||||
{
|
||||
int id{0};
|
||||
std::atomic<bool>* entered_move{nullptr};
|
||||
std::atomic<bool>* release_move{nullptr};
|
||||
|
||||
DelayedMoveItem() noexcept = default;
|
||||
DelayedMoveItem(int v, std::atomic<bool>* entered, std::atomic<bool>* release) noexcept
|
||||
: id{v}, entered_move{entered}, release_move{release}
|
||||
{
|
||||
}
|
||||
DelayedMoveItem(DelayedMoveItem&& other) noexcept
|
||||
: id{other.id},
|
||||
entered_move{nullptr},
|
||||
release_move{nullptr}
|
||||
{
|
||||
if (other.entered_move && other.release_move) {
|
||||
other.entered_move->store(true, std::memory_order_release);
|
||||
while (!other.release_move->load(std::memory_order_acquire)) {
|
||||
std::this_thread::yield();
|
||||
}
|
||||
}
|
||||
}
|
||||
DelayedMoveItem& operator=(DelayedMoveItem&&) noexcept = default;
|
||||
~DelayedMoveItem() noexcept = default;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test spsc_and_mpsc_close_wraparound_and_destructor_cleanup()
|
||||
{
|
||||
using Void::Ring::PopStatus;
|
||||
using Void::Ring::PushStatus;
|
||||
|
||||
std::atomic<int> spsc_dtors{0};
|
||||
{
|
||||
Void::Ring::SpscRing<CountedItem, 4> spsc;
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
Void::Testing::expect(spsc.try_push(CountedItem{i, &spsc_dtors}) == PushStatus::ok);
|
||||
auto popped = spsc.try_pop();
|
||||
Void::Testing::expect(popped.status == PopStatus::ok && popped.value->value == i);
|
||||
}
|
||||
Void::Testing::expect(spsc.try_push(CountedItem{100, &spsc_dtors}) == PushStatus::ok);
|
||||
Void::Testing::expect(spsc.try_push(CountedItem{101, &spsc_dtors}) == PushStatus::ok);
|
||||
spsc.close();
|
||||
Void::Testing::expect(spsc.try_push(CountedItem{102, &spsc_dtors}) == PushStatus::closed);
|
||||
}
|
||||
Void::Testing::expect_eq(spsc_dtors.load(), 19);
|
||||
|
||||
std::atomic<int> mpsc_dtors{0};
|
||||
{
|
||||
Void::Ring::MpscRing<CountedItem, 4> mpsc;
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
Void::Testing::expect(mpsc.try_push(CountedItem{i, &mpsc_dtors}) == PushStatus::ok);
|
||||
auto popped = mpsc.try_pop();
|
||||
Void::Testing::expect(popped.status == PopStatus::ok && popped.value->value == i);
|
||||
}
|
||||
Void::Testing::expect(mpsc.try_push(CountedItem{200, &mpsc_dtors}) == PushStatus::ok);
|
||||
Void::Testing::expect(mpsc.try_push(CountedItem{201, &mpsc_dtors}) == PushStatus::ok);
|
||||
mpsc.close();
|
||||
Void::Testing::expect(mpsc.try_push(CountedItem{202, &mpsc_dtors}) == PushStatus::closed);
|
||||
}
|
||||
Void::Testing::expect_eq(mpsc_dtors.load(), 23);
|
||||
}
|
||||
|
||||
// @tag threading
|
||||
Void::Testing::Test mpsc_delayed_producer_returns_pending_without_spinning()
|
||||
{
|
||||
using Void::Ring::PopStatus;
|
||||
using Void::Ring::PushStatus;
|
||||
|
||||
Void::Ring::MpscRing<DelayedMoveItem, 8> ring;
|
||||
std::atomic<bool> entered{false};
|
||||
std::atomic<bool> release{false};
|
||||
|
||||
std::thread producer([&] {
|
||||
Void::Testing::expect(ring.try_push(DelayedMoveItem{77, &entered, &release}) == PushStatus::ok);
|
||||
});
|
||||
|
||||
while (!entered.load(std::memory_order_acquire)) {
|
||||
std::this_thread::yield();
|
||||
}
|
||||
|
||||
// Slot 0 is reserved (enqueue_pos_ == 1) but not yet published: must return pending immediately.
|
||||
const auto pending = ring.try_pop();
|
||||
Void::Testing::expect(pending.status == PopStatus::pending);
|
||||
Void::Testing::expect(!pending.has_value());
|
||||
|
||||
release.store(true, std::memory_order_release);
|
||||
producer.join();
|
||||
|
||||
const auto ready = ring.try_pop();
|
||||
Void::Testing::expect(ready.status == PopStatus::ok);
|
||||
Void::Testing::require(ready.has_value());
|
||||
Void::Testing::expect_eq(ready.value->id, 77);
|
||||
|
||||
ring.close();
|
||||
const auto closed = ring.try_pop();
|
||||
Void::Testing::expect(closed.status == PopStatus::closed);
|
||||
}
|
||||
|
||||
// @tag threading
|
||||
Void::Testing::Test mpsc_high_contention_wraparound_stress()
|
||||
{
|
||||
Void::Ring::MpscRing<std::uint64_t, 16> ring;
|
||||
constexpr int kProducers = 4;
|
||||
constexpr std::uint64_t kPerProducer = 200;
|
||||
std::vector<std::thread> producers;
|
||||
for (int p = 0; p < kProducers; ++p) {
|
||||
producers.emplace_back([&, p] {
|
||||
for (std::uint64_t i = 0; i < kPerProducer; ++i) {
|
||||
const std::uint64_t val =
|
||||
(static_cast<std::uint64_t>(p) << 32) | i;
|
||||
while (ring.try_push(val) != Void::Ring::PushStatus::ok) {
|
||||
std::this_thread::yield();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
std::uint64_t consumed = 0;
|
||||
std::uint64_t xor_sum = 0;
|
||||
while (consumed < kProducers * kPerProducer) {
|
||||
auto r = ring.try_pop();
|
||||
if (r.status == Void::Ring::PopStatus::ok) {
|
||||
xor_sum ^= *r.value;
|
||||
++consumed;
|
||||
}
|
||||
}
|
||||
for (auto& t : producers) {
|
||||
t.join();
|
||||
}
|
||||
|
||||
std::uint64_t expected_xor = 0;
|
||||
for (int p = 0; p < kProducers; ++p) {
|
||||
for (std::uint64_t i = 0; i < kPerProducer; ++i) {
|
||||
expected_xor ^= (static_cast<std::uint64_t>(p) << 32) | i;
|
||||
}
|
||||
}
|
||||
Void::Testing::expect_eq(xor_sum, expected_xor);
|
||||
}
|
||||
Reference in new issue
Block a user