xo-alloc: + gc history xo-imgui: gui examples

This commit is contained in:
Roland Conybeare 2025-08-14 09:50:59 -05:00
commit f4be2e765e
20 changed files with 2015 additions and 18 deletions

View file

@ -113,6 +113,7 @@ namespace xo {
}
this->committed_z_ = align_offset_z;
this->limit_ = this->lo_ + committed_z_;
return true;
}
@ -133,6 +134,16 @@ namespace xo {
}
}
std::pair<bool, std::size_t>
ArenaAlloc::location_of(const void * x) const
{
if ((lo_ <= x) && (x < hi_)) {
return std::make_pair(true, reinterpret_cast<const std::byte *>(x) - lo_);
} else {
return std::make_pair(false, 0);
}
}
void
ArenaAlloc::capture_object_statistics(capture_phase phase,
ObjectStatistics * p_dest) const
@ -276,6 +287,7 @@ namespace xo {
xtag("z0", z0),
xtag("+pad", dz),
xtag("z1", z1),
xtag("size", this->size()),
xtag("avail", this->available()));
this->free_ptr_ += z1;

View file

@ -79,6 +79,8 @@ namespace xo {
mutation_log_[role2int(role::to_space)] = std::make_unique<MutationLog>();
defer_mutation_log_ = std::make_unique<MutationLog>();
this->gc_history_ = CircularBuffer<GcStatisticsHistoryItem>(config.stats_history_z_);
this->checkpoint();
}
@ -191,6 +193,12 @@ namespace xo {
return retval;
}
std::size_t
GC::nursery_to_reserved() const
{
return nursery_to()->reserved();
}
std::size_t
GC::nursery_to_committed() const
{
@ -209,6 +217,17 @@ namespace xo {
return nursery_to()->after_checkpoint();
}
std::pair<const std::byte *, const std::byte *>
GC::nursery_span(role role) const {
return nursery(role)->allocated_span();
}
std::size_t
GC::tenured_to_reserved() const
{
return tenured_to()->reserved();
}
std::size_t
GC::tenured_to_committed() const
{
@ -239,6 +258,40 @@ namespace xo {
return generation_result::not_found;
}
std::tuple<generation_result, std::size_t, std::size_t>
GC::location_of(role role, const void *x) const
{
{
auto space = this->tenured(role);
auto [is_tenured, offset] = space->location_of(x);
if (is_tenured)
return std::make_tuple(generation_result::tenured, offset, space->allocated());
}
{
auto space = this->nursery(role);
auto [is_nursery, offset] = nursery(role)->location_of(x);
if (is_nursery)
return std::make_tuple(generation_result::nursery, offset, space->allocated());
}
return std::make_tuple(generation_result::not_found, 0, 0);
}
std::tuple<generation_result, std::size_t, std::size_t>
GC::tospace_location_of(const void * x) const
{
return location_of(role::to_space, x);
}
std::tuple<generation_result, std::size_t, std::size_t>
GC::fromspace_location_of(const void * x) const
{
return location_of(role::from_space, x);
}
generation_result
GC::fromspace_generation_of(const void * x) const
{
@ -988,8 +1041,11 @@ namespace xo {
{
scope log(XO_DEBUG(config_.debug_flag_));
std::size_t N_allocated = nursery_from()->after_checkpoint();
std::size_t T_allocated = tenured_from()->after_checkpoint();
std::size_t N0_before_gc = nursery_from()->after_checkpoint();
std::size_t N1_before_gc = nursery_from()->before_checkpoint();
std::size_t T0_before_gc = tenured_from()->after_checkpoint();
std::size_t T1_before_gc = tenured_from()->before_checkpoint();
std::size_t N_before_gc = nursery_from()->allocated();
std::size_t T_before_gc = tenured_from()->allocated();
@ -998,9 +1054,37 @@ namespace xo {
std::size_t T_after_gc = tenured_to()->allocated();
//std::byte * N_free_ptr = nursery_[role2int(role::to_space)]->free_ptr();
std::size_t new_alloc_z = N0_before_gc;
/* survive_z: bytes surviving first collection */
std::size_t survive_z = N_after_gc;
/* promote_z: bytes surviving 2nd collection */
std::size_t promote_z = (gc_statistics_.total_promoted_
- gc_statistics_.total_promoted_sab_);
/* #of bytes copied by this collection cycle */
std::size_t effort_z = 0;
if (upto == generation::nursery) {
effort_z = N_after_gc + promote_z;
} else {
effort_z += N_after_gc + T_after_gc;
}
/* persist_z: bytes surviving 3rd or later collection */
std::size_t persist_z = 0;
if (upto == generation::tenured)
persist_z = T_after_gc - promote_z;
/* #of bytes found to be garbage on first collection
* (reminder: N_after_gc consists *entirely* of survives from N0_before_gc;
* + all such survivors are in N_after_gc)
*/
std::size_t garbage0_z = (N0_before_gc - N_after_gc);
/* #of bytes found to be garbage on 2nd collection */
std::size_t garbage1_z = (N1_before_gc - promote_z);
/* #of bytes found to be garbage on 3rd or later collection */
std::size_t garbageN_z = 0;
if (upto == generation::tenured)
garbageN_z = (T_before_gc - T_after_gc + promote_z);
/* Don't reset from-space here, it's unnecessary.
* Would be permissible, but interferes with GC object modelling in
* xo-object/utest/GC.test.cpp
@ -1014,25 +1098,38 @@ namespace xo {
this->tenured_to()->checkpoint();
if (log) {
log(xtag("N_allocated", N_allocated));
log(xtag("N_before_gc", N_before_gc));
log(xtag("N0_before_gc", N0_before_gc));
log(xtag("N1_before_gc", N1_before_gc));
log(xtag("N_after_gc", N_after_gc));
log(xtag("T_allocated", T_allocated));
log(xtag("T_before_gc", T_before_gc));
log(xtag("T0_before_gc", T0_before_gc));
log(xtag("T1_before_gc", T1_before_gc));
log(xtag("T_after_gc", T_after_gc));
}
GcStatisticsHistoryItem item(upto,
new_alloc_z,
survive_z,
promote_z,
persist_z,
effort_z,
garbage0_z,
garbage1_z,
garbageN_z);
this->gc_history_.push_back(item);
this->incr_gc_pending_ = false;
this->gc_statistics_.include_gc(generation::nursery, N_allocated, N_before_gc, N_after_gc, promote_z);
this->gc_statistics_.include_gc(generation::nursery, N0_before_gc, N_before_gc, N_after_gc, promote_z);
if (upto == generation::tenured) {
this->full_gc_pending_ = false;
this->gc_statistics_.include_gc(generation::tenured, T_allocated, T_before_gc, T_after_gc, 0);
this->gc_statistics_.include_gc(generation::tenured, T0_before_gc, T_before_gc, T_after_gc, 0);
} else {
// still want to update tenured stats for current alloc size
this->gc_statistics_.update_snapshot(generation::tenured, T_after_gc);
}
}
} /*cleanup_phase*/
void
GC::execute_gc(generation upto)
@ -1090,6 +1187,9 @@ namespace xo {
this->runstate_ = GCRunstate();
// not this way.. reports cumulative stats
// this->gc_history_.push_back(this->get_gc_statistics());
log && log("statistics:");
log && log(gc_statistics_);
}

View file

@ -92,6 +92,22 @@ namespace xo {
// << xtag("per_type_stats", per_type_stats_)
<< ">";
}
void
GcStatisticsHistoryItem::display(std::ostream & os) const
{
os << "<GcStatisticsHistoryItem"
<< xrtag("upto", upto_)
<< xrtag("survive_z", survive_z_)
<< xrtag("promote_z", promote_z_)
<< xrtag("persist_z", persist_z_)
<< xrtag("effort_z", effort_z_)
<< xrtag("garbage0_z", garbage0_z_)
<< xrtag("garbage1_z", garbage1_z_)
<< xrtag("garbageN_z", garbageN_z_)
<< ">";
}
} /*namespace gc*/
namespace print {
@ -148,7 +164,21 @@ namespace xo {
refrtag("tenured_z", x.tenured_z_));
}
bool
ppdetail<xo::gc::GcStatisticsHistoryItem>::print_pretty(const ppindentinfo & ppii,
const xo::gc::GcStatisticsHistoryItem & x)
{
return ppii.pps()->pretty_struct(ppii,
"GcStatisticsHistoryItem",
refrtag("upto", gen2str(x.upto_)),
refrtag("survive_z", x.survive_z_),
refrtag("promote_z", x.promote_z_),
refrtag("persist_z", x.persist_z_),
refrtag("effort_z", x.effort_z_),
refrtag("garbage0_z", x.garbage0_z_),
refrtag("garbage1_z", x.garbage1_z_),
refrtag("garbageN_z", x.garbageN_z_));
}
} /*namespace print*/
} /*namespace xo*/

View file

@ -70,6 +70,11 @@ namespace xo {
return s_default_name;
}
std::size_t
ListAlloc::page_size() const {
return hd_->page_size();
}
std::size_t
ListAlloc::size() const {
return total_z_;
@ -109,8 +114,9 @@ namespace xo {
ListAlloc::allocated() const {
std::size_t total = 0;
if (hd_)
if (hd_) {
total += hd_->allocated();
}
for (const auto & alloc : full_l_)
total += alloc->allocated();
@ -363,10 +369,17 @@ namespace xo {
ListAlloc::alloc(std::size_t z) {
scope log(XO_DEBUG(debug_flag_));
/* ArenaAlloc::alloc() may modify its own size */
std::size_t z_pre = hd_->size();
std::byte * retval = hd_->alloc(z);
if (retval)
if (retval) {
std::size_t z_post = hd_->size();
this->total_z_ += (z_post - z_pre);
return retval;
}
log && log("space exhausted -> expand");