xo-gc stack: + request-gc-statistics() primitive

1. xo-gc now depends on xo-object2.
2. use genfacet for ICollector_DX1Collector
3. moves xo-gc utest previously in xo-object2 to more natural
   location in xo-gc/
This commit is contained in:
Roland Conybeare 2026-03-29 13:44:19 -04:00
commit c5da97dea0
14 changed files with 795 additions and 175 deletions

View file

@ -5,7 +5,9 @@ set(UTEST_EXE utest.gc)
set(UTEST_SRCS
gc_utest_main.cpp
Collector.test.cpp
X1Collector.test.cpp
DX1CollectorIterator.test.cpp
Object2.test.cpp
random_allocs.cpp
)

134
utest/Object2.test.cpp Normal file
View file

@ -0,0 +1,134 @@
/** @file Object2.test.cpp
*
* Tests that target xo-object2/ features, but also rely on xo-gc/
*
* @author Roland Conybeare, Mar 2026
**/
#include <xo/gc/X1Collector.hpp>
#include <xo/object2/SetupObject2.hpp>
#include <xo/object2/ListOps.hpp>
#include <xo/object2/Integer.hpp>
#include <xo/stringtable2/String.hpp>
#include <catch2/catch.hpp>
namespace ut {
using xo::scm::SetupObject2;
using xo::scm::ListOps;
using xo::scm::DList;
using xo::scm::DInteger;
using xo::scm::DString;
using xo::mm::AAllocator;
using xo::mm::ACollector;
using xo::mm::AGCObject;
using xo::mm::X1CollectorConfig;
using xo::mm::DX1Collector;
using xo::mm::ArenaConfig;
using xo::print::APrintable;
using xo::facet::FacetRegistry;
using xo::facet::with_facet;
using xo::facet::obj;
using xo::facet::typeseq;
using xo::print::ppstate_standalone;
using xo::print::ppconfig;
using xo::scope;
using xo::xtag;
using std::string;
namespace {
struct testcase_pp {
explicit testcase_pp(size_t gc_z, size_t gc_threshold, int z, const std::string & expected)
: gc_gen_size_{gc_z}, gc_trigger_threshold_{gc_threshold}, expected_{expected} {
for (int i = 0; i < z; ++i) {
list_.push_back(1000 + 197 * i);
}
}
size_t gc_gen_size_ = 0;
size_t gc_trigger_threshold_ = 0;
std::vector<int> list_;
std::string expected_;
};
std::vector<testcase_pp>
s_testcase_v = {
testcase_pp(16384, 8192, 0, "()"),
testcase_pp(16384, 8192, 1, "(01000)"),
testcase_pp(16384, 8192, 2, "(01000 1197)"),
testcase_pp(16384, 8192, 5, "(01000 1197 01394 1591 01788)"),
testcase_pp(16384, 8192, 10, "(01000 1197 01394 1591 01788 1985 02182 2379 02576 2773)"),
testcase_pp(16384, 8192, 20, "(...)"),
};
}
TEST_CASE("printable1", "[pp][x1][list]")
{
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag));
bool ok = SetupObject2::register_facets();
REQUIRE(ok);
FacetRegistry::instance().dump(&std::cerr);
for (std::size_t i_tc = 0, n_tc = s_testcase_v.size(); i_tc < n_tc; ++i_tc) {
log && log("printable1 test:", xtag("i_tc", i_tc));
try {
const testcase_pp & tc = s_testcase_v[i_tc];
X1CollectorConfig cfg{ .name_ = "pp_test",
.arena_config_ = ArenaConfig{
.size_ = tc.gc_gen_size_,
.store_header_flag_ = true},
.object_types_z_ = 16384,
.gc_trigger_v_{{tc.gc_trigger_threshold_,
tc.gc_trigger_threshold_}},
.debug_flag_ = c_debug_flag
};
DX1Collector gc(cfg);
auto gc_o = with_facet<AAllocator>::mkobj(&gc);
auto c_o = with_facet<ACollector>::mkobj(&gc);
bool ok = SetupObject2::register_types(c_o);
REQUIRE(ok);
auto l0_o = ListOps::nil();
c_o.add_gc_root(&l0_o);
for(int ip1 = tc.list_.size(); ip1 > 0; --ip1) {
obj<AGCObject> elt;
//elt = DInteger::box<AGCObject>(gc_o, tc.list_[ip1 - 1]);
if (ip1 % 2 == 0) {
elt = DInteger::box<AGCObject>(gc_o, tc.list_[ip1 - 1]);
} else {
elt = obj<AGCObject,DString>(DString::printf(gc_o, 80, "%05d", tc.list_[ip1 - 1]));
}
l0_o = ListOps::cons(gc_o, elt, l0_o);
}
// TODO: log_streambuf using DArena
std::stringstream ss;
ppconfig ppc;
ppstate_standalone pps(&ss, 0, &ppc);
obj<APrintable,DList> l0_po(static_cast<DList*>(l0_o.data()));
REQUIRE(l0_po._typeseq() == typeseq::id<DList>());
pps.pretty(l0_po);
CHECK(ss.str() == string(tc.expected_));
} catch (std::exception & ex) {
std::cerr << "caught exception: " << ex.what() << std::endl;
REQUIRE(false);
}
}
} /* TEST_CASE(printable1) */
} /*namespace ut*/
/* end Object2.test.cpp */

297
utest/X1Collector.test.cpp Normal file
View file

@ -0,0 +1,297 @@
/** @file X1Collector.test.cpp
*
* @author Roland Conybeare, Dec 2025
**/
#include "init_gc.hpp"
#include "ListOps.hpp"
#include "DFloat.hpp"
#include "DInteger.hpp"
#include "DList.hpp"
#include "DArray.hpp"
#include <xo/object2/Float.hpp>
//#include "number/IGCObject_DFloat.hpp"
#include "number/IGCObject_DInteger.hpp"
#include "list/IGCObject_DList.hpp"
#include <xo/alloc2/CollectorTypeRegistry.hpp>
#include <xo/alloc2/Collector.hpp>
#include <xo/gc/X1Collector.hpp>
#include <xo/arena/AllocInfo.hpp>
#include <xo/arena/padding.hpp>
#include <xo/subsys/Subsystem.hpp>
#include <xo/indentlog/scope.hpp>
#include <xo/indentlog/print/tag.hpp>
#include <catch2/catch.hpp>
namespace ut {
using xo::S_gc_tag;
using xo::scm::ListOps;
using xo::scm::DList;
using xo::scm::DArray;
using xo::scm::DFloat;
using xo::scm::DInteger;
using xo::mm::CollectorTypeRegistry;
using xo::mm::AAllocator;
using xo::mm::ACollector;
using xo::mm::AllocHeader;
using xo::mm::AllocInfo;
using xo::mm::AGCObject;
using xo::mm::X1CollectorConfig;
using xo::mm::DX1Collector;
using xo::mm::DArena;
using xo::mm::ArenaConfig;
using xo::mm::Generation;
using xo::mm::role;
using xo::mm::padding;
using xo::facet::obj;
using xo::facet::with_facet;
using xo::facet::typeseq;
using xo::Subsystem;
using xo::InitEvidence;
using xo::InitSubsys;
using xo::scope;
using xo::xtag;
namespace {
struct testcase_x1 {
testcase_x1(std::size_t nz,
std::size_t tz,
std::size_t n_gct,
std::size_t t_gct)
: nursery_z_{nz},
tenured_z_{tz},
incr_gc_threshold_{n_gct},
full_gc_threshold_{t_gct} {}
std::size_t nursery_z_;
std::size_t tenured_z_;
std::size_t incr_gc_threshold_;
std::size_t full_gc_threshold_;
};
std::vector<testcase_x1>
s_testcase_v = {
// n_gct: nursery gc threshold
// t_gct: tenured gc threshold
//
// nz tz n_gct t_gct
testcase_x1(4096, 8192, 1024, 1024)
};
}
static InitEvidence s_init = (InitSubsys<S_gc_tag>::require());
TEST_CASE("x1", "[gc][x1]")
{
// real purpose: ensure s_init survives static linking
REQUIRE(s_init.evidence());
Subsystem::initialize_all();
/**
* This is a basic Collector test for xo-object2 data types
**/
constexpr bool c_debug_flag = true;
scope log(XO_DEBUG(c_debug_flag));
for (std::size_t i_tc = 0, n_tc = s_testcase_v.size(); i_tc < n_tc; ++i_tc) {
scope log(XO_DEBUG(true), xtag("i_tc", i_tc));
try {
const testcase_x1 & tc = s_testcase_v[i_tc];
X1CollectorConfig cfg{ .name_ = "x1_test",
.arena_config_ = ArenaConfig{
.size_ = tc.tenured_z_,
.store_header_flag_ = true},
.object_types_z_ = 16384,
.gc_trigger_v_{{
tc.incr_gc_threshold_,
tc.full_gc_threshold_}},
.debug_flag_ = c_debug_flag,
};
DX1Collector gc(cfg);
CollectorTypeRegistry::instance().install_types(obj<ACollector,DX1Collector>(&gc));
DArena * to_0 = nullptr;
/* verify configuration */
{
REQUIRE(cfg.n_generation_ == 2);
}
/* verify initial collector state */
{
REQUIRE(gc.name() == "x1_test");
const DArena * otypes = gc.get_object_types();
REQUIRE(otypes != nullptr);
REQUIRE(otypes->reserved() >= cfg.object_types_z_);
REQUIRE(otypes->reserved() < cfg.object_types_z_ + otypes->page_z_);
const DX1Collector::RootSet * roots = gc.get_root_set();
REQUIRE(roots != nullptr);
REQUIRE(roots->store()->reserved() >= cfg.object_roots_z_);
REQUIRE(roots->store()->reserved() < cfg.object_roots_z_ + roots->store()->page_z_);
const DArena * from_0 = gc.get_space(role::from_space(), Generation{0});
REQUIRE(from_0 != nullptr);
REQUIRE(from_0->reserved() >= tc.tenured_z_);
REQUIRE(from_0->reserved() < tc.tenured_z_ + from_0->page_z_);
REQUIRE(from_0->reserved() % from_0->page_z_ == 0);
REQUIRE(from_0->allocated() == 0);
const DArena * from_1 = gc.get_space(role::from_space(), Generation{1});
REQUIRE(from_1 != nullptr);
REQUIRE(from_1->reserved() == from_0->reserved());
REQUIRE(from_1->allocated() == 0);
to_0 = gc.get_space(role::to_space(), Generation{0});
REQUIRE(to_0 != nullptr);
REQUIRE(to_0->reserved() == from_0->reserved());
REQUIRE(to_0->allocated() == 0);
const DArena * to_1 = gc.get_space(role::to_space(), Generation{1});
REQUIRE(to_1 != nullptr);
REQUIRE(to_1->reserved() == to_0->reserved());
REQUIRE(to_1->allocated() == 0);
const DArena * from_2 = gc.get_space(role::from_space(), Generation{2});
REQUIRE(from_2 == nullptr);
const DArena * to_2 = gc.get_space(role::to_space(), Generation{2});
REQUIRE(to_2 == nullptr);
REQUIRE(gc.reserved()
== otypes->reserved() + roots->store()->reserved() + 4 * from_0->reserved());
log && log(xtag("from_0", from_0->lo_), xtag("to_0", to_0->lo_));
}
/* attempt allocation */
auto gc_o = with_facet<AAllocator>::mkobj(&gc);
auto c_o = with_facet<ACollector>::mkobj(&gc);
/* register object types */
bool ok = CollectorTypeRegistry::instance().install_types(c_o);
REQUIRE(ok);
ok = c_o.is_type_installed(typeseq::id<DFloat>());
REQUIRE(ok);
ok = c_o.is_type_installed(typeseq::id<DInteger>());
REQUIRE(ok);
ok = c_o.is_type_installed(typeseq::id<DList>());
REQUIRE(ok);
ok = c_o.is_type_installed(typeseq::id<DArray>());
REQUIRE(ok);
auto x0_o = DFloat::box<AGCObject>(gc_o, 3.1415927);
c_o.add_gc_root(&x0_o);
REQUIRE(to_0->allocated() == sizeof(AllocHeader) + sizeof(DFloat));
auto n1_o = DInteger::box<AGCObject>(gc_o, 42);
c_o.add_gc_root(&n1_o);
REQUIRE(to_0->allocated() == (sizeof(AllocHeader) + sizeof(DFloat)
+ sizeof(AllocHeader) + sizeof(DInteger)));
//DList * l0 = DList::list(gc_o, x0_o);
//auto l0_o = with_facet<AGCObject>::mkobj(l0);
auto l0_o = ListOps::list(gc_o, x0_o);
c_o.add_gc_root(&l0_o);
REQUIRE(to_0->allocated() == (sizeof(AllocHeader) + sizeof(DFloat)
+ sizeof(AllocHeader) + sizeof(DInteger)
+ sizeof(AllocHeader) + sizeof(DList)));
{
{
REQUIRE(x0_o.iface() != nullptr);
REQUIRE(x0_o.data() != nullptr);
REQUIRE(gc.contains(role::to_space(), x0_o.data()));
/* check alloc info for newly-allocated object */
AllocInfo info = gc.alloc_info((std::byte *)x0_o.data());
REQUIRE(info.age() == 0);
REQUIRE(info.tseq() == typeseq::id<DFloat>().seqno());
REQUIRE(info.size() >= sizeof(DFloat));
REQUIRE(info.size() < sizeof(DFloat) + padding::c_alloc_alignment);
}
{
REQUIRE(n1_o.iface() != nullptr);
REQUIRE(n1_o.data() != nullptr);
REQUIRE(gc.contains(role::to_space(), n1_o.data()));
/* check alloc info for newly-allocated object */
AllocInfo info = gc.alloc_info((std::byte *)n1_o.data());
REQUIRE(info.age() == 0);
REQUIRE(info.tseq() == typeseq::id<DInteger>().seqno());
REQUIRE(info.size() >= sizeof(DInteger));
REQUIRE(info.size() < sizeof(DInteger) + padding::c_alloc_alignment);
}
{
REQUIRE(l0_o.iface() != nullptr);
REQUIRE(l0_o.data() != nullptr);
REQUIRE(gc.contains(role::to_space(), l0_o.data()));
/* check alloc info for newly-allocated object */
AllocInfo info = gc.alloc_info((std::byte *)l0_o.data());
REQUIRE(info.age() == 0);
REQUIRE(info.tseq() == typeseq::id<DList>().seqno());
REQUIRE(info.size() >= sizeof(DList));
REQUIRE(info.size() < sizeof(DList) + padding::c_alloc_alignment);
}
}
/* no GC roots, so GC is trivial */
c_o.request_gc(Generation{1});
log && log(xtag("l0_o.data()", l0_o.data()));
log && log(xtag("l0_o.data()->head_.data()", l0_o.data()->head_.data()));
log && log(xtag("x0_o.data()", x0_o.data()));
REQUIRE(!gc.contains(role::from_space(), x0_o.data()));
REQUIRE(gc.contains(role::to_space(), x0_o.data()));
REQUIRE(x0_o.data()->value() == 3.1415927);
REQUIRE(!gc.contains(role::from_space(), n1_o.data()));
REQUIRE(gc.contains(role::to_space(), n1_o.data()));
REQUIRE(n1_o.data()->value() == 42);
REQUIRE(!gc.contains(role::from_space(), l0_o.data()));
REQUIRE(gc.contains(role::to_space(), l0_o.data()));
REQUIRE(l0_o.data()->is_empty() == false);
REQUIRE((void*)l0_o.data()->head_.data() == (void*)x0_o.data());
REQUIRE((void*)l0_o.data()->rest_ == (void*)DList::_nil());
} catch (std::exception & ex) {
std::cerr << "caught exception: " << ex.what() << std::endl;
REQUIRE(false);
}
}
}
}
/* end X1Collector.test.cpp */