From 7e8f49d2565f03ad3806040d3ab373c97e7a2d55 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Sun, 21 Dec 2025 22:37:39 -0500 Subject: [PATCH] xo-alloc2: misc refactoring / debug logging --- include/xo/alloc2/alloc/AllocInfo.hpp | 2 +- src/alloc2/DArenaIterator.cpp | 2 +- utest/Collector.test.cpp | 17 ++++++++---- utest/random_allocs.cpp | 39 +++++++++++++++++++++------ 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/include/xo/alloc2/alloc/AllocInfo.hpp b/include/xo/alloc2/alloc/AllocInfo.hpp index 09846ba..5f68396 100644 --- a/include/xo/alloc2/alloc/AllocInfo.hpp +++ b/include/xo/alloc2/alloc/AllocInfo.hpp @@ -48,7 +48,7 @@ namespace xo { std::uint32_t tseq() const noexcept { return p_config_->tseq(*p_header_); } /** Allocation age in garbage collector **/ std::uint32_t age() const noexcept { return p_config_->age (*p_header_); } - /** Allocation size (including allocator-supplied padding) **/ + /** Allocation size (including allocator-supplied padding, excluding alloc header) **/ size_type size() const noexcept { return p_config_->size(*p_header_); } /** Payload for this allocation. This is the memory available to application **/ span_type payload() const noexcept; diff --git a/src/alloc2/DArenaIterator.cpp b/src/alloc2/DArenaIterator.cpp index bcad394..b9873e3 100644 --- a/src/alloc2/DArenaIterator.cpp +++ b/src/alloc2/DArenaIterator.cpp @@ -91,7 +91,7 @@ namespace xo { cmpresult DArenaIterator::compare(const DArenaIterator & other_ix) const noexcept { - scope log(XO_DEBUG(true), + scope log(XO_DEBUG(false), xtag("arena", arena_), xtag("pos", pos_), xtag("other.arena", other_ix.arena_), diff --git a/utest/Collector.test.cpp b/utest/Collector.test.cpp index 9280e69..a54150e 100644 --- a/utest/Collector.test.cpp +++ b/utest/Collector.test.cpp @@ -15,6 +15,7 @@ //#include "gc/DX1Collector.hpp" #include #include +#include #include #include #include @@ -29,6 +30,7 @@ namespace xo { using xo::mm::generation; using xo::mm::c_max_generation; using xo::facet::with_facet; + using xo::scope; namespace ut { // checklist @@ -151,6 +153,8 @@ namespace xo { TEST_CASE("collector-x1-alloc", "[alloc2][gc]") { + scope log(XO_DEBUG(true), "DX1Collector alloc test"); + ArenaConfig arena_cfg = { .name_ = "_test_unused", .size_ = 4*1024*1024, .store_header_flag_ = true, @@ -190,14 +194,17 @@ namespace xo { TEST_CASE("collector-x1-alloc2", "[alloc2][gc]") { + scope log(XO_DEBUG(true), "DX1Collector alloc test2"); + ArenaConfig arena_cfg = { .name_ = "_test_unused", .size_ = 4*1024*1024, .store_header_flag_ = true, - .header_ = AllocHeaderConfig(8 /*guard_z*/, + .header_ = AllocHeaderConfig(8 /*guard_z*/, 0xfd /*guard-byte*/, - 0 /*tseq-bits*/, - 0 /*age-bits*/, - 16 /*size-bits*/), }; + 0 /*tseq-bits*/, + 0 /*age-bits*/, + 16 /*size-bits*/), + }; /* collector with one generation collapses to a non-generational copying collector */ CollectorConfig cfg = { .arena_config_ = arena_cfg, @@ -222,7 +229,7 @@ namespace xo { REQUIRE(x1alloc.data()); rng::Seed seed; - std::cerr << "ratio: seed=" << seed << std::endl; + log && log("ratio: seed=", seed); auto rng = rng::xoshiro256ss(seed); diff --git a/utest/random_allocs.cpp b/utest/random_allocs.cpp index 0d638ec..e9a1d7c 100644 --- a/utest/random_allocs.cpp +++ b/utest/random_allocs.cpp @@ -52,9 +52,9 @@ namespace utest { * * allocs sorted on Alloc::lo */ - std::map allocs_by_lo_map; + std::map allocs_by_lo_map; /* allocs sorted on Alloc::hi */ - std::map allocs_by_hi_map; + std::map allocs_by_hi_map; for (uint32_t i_alloc = 0; i_alloc < n_alloc; ++i_alloc) { std::normal_distribution ngen{5.0, 1.5}; @@ -172,18 +172,41 @@ namespace utest { .hugepage_z_ = 4*1024 }); auto range = mm.alloc_range(scratch_mm); -#ifdef NOT_YET // to verify iteration here, need iterator support in AAllocator - - /* verify iteration visits all the allocs, exactly once */ + /* limit iteration test to a few cases: + * - 1st loop + * - median loop + * - last loop + */ + if (i_alloc == 0 || i_alloc == n_alloc || 2*i_alloc == n_alloc) { + /* verify iteration visits all the allocs, exactly once */ + + /* temp copy; remove allocs from this map as we encounter + * them via range iteration below + */ auto alloc_map = allocs_by_lo_map; - for (AllocInfo info : mm) { + if (log || true) { + log(xtag("allocs_by_lo_map.size", allocs_by_lo_map.size())); + + for (auto & kv : allocs_by_lo_map) { + log(xtag("key", kv.first), xtag("value", kv.second.lo()), xtag("hi", kv.second.hi())); + } } - } -#endif + for (AllocInfo info : range) { + INFO(tostr(xtag("alloc_map.size", alloc_map.size()), + xtag("i_alloc", i_alloc))); + INFO(tostr(xtag("payload.first", info.payload().first))); + const std::byte * alloc_lo = info.payload().first; + + REQUIRE_ORFAIL(ok_flag, catch_flag, + alloc_map.find(alloc_lo) != alloc_map.end()); + + alloc_map.erase(alloc_lo); + } + } } return true;