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

@ -36,9 +36,24 @@ namespace xo {
std::size_t z,
bool debug_flag);
/** size of virtual address range reserved for this allocator **/
std::size_t reserved() const { return this->size(); }
std::size_t page_size() const { return page_z_; }
std::byte * free_ptr() const { return free_ptr_; }
void set_free_ptr(std::byte * x);
/** if address @p x is allocated from this arena,
* return true along with offset relative to base address @ref lo_
* otherwise return false with 0
**/
std::pair<bool, std::size_t> location_of(const void * x) const;
/** allocated span **/
std::pair<const std::byte *, const std::byte *> allocated_span() const {
return std::make_pair(lo_, free_ptr_);
}
/** Reset to empty state **/
void reset(std::size_t /*z_ignored*/) { this->clear(); }

View file

@ -50,6 +50,8 @@ namespace xo {
bool allow_incremental_gc_ = true;
/** true to report statistics **/
bool stats_flag_ = false;
/** remember basic gc statistics for this many GC's; separately for incremental + full GCs **/
std::size_t stats_history_z_ = 256;
/** true to enable debug logging **/
bool debug_flag_ = false;
};
@ -147,17 +149,24 @@ namespace xo {
const GCRunstate & runstate() const { return runstate_; }
const GcStatistics & native_gc_statistics() const { return gc_statistics_; }
GcStatisticsExt get_gc_statistics() const;
const GcStatisticsHistory & gc_history() const { return gc_history_; }
/** true iff GC permitted in current state **/
bool is_gc_enabled() const { return gc_enabled_ == 0; }
/** true during (and only during) a GC cycle **/
bool gc_in_progress() const { return runstate_.in_progress(); }
/** @return reserved size of Nursery to-space **/
std::size_t nursery_to_reserved() const;
/** @return committed size of Nursery to-space **/
std::size_t nursery_to_committed() const;
/** @return nursery bytes used before checkpoint **/
std::size_t nursery_before_checkpoint() const;
/** @return nursery bytes used after checkpoint **/
std::size_t nursery_after_checkpoint() const;
/** @return allocated memory range for nursery **/
std::pair<const std::byte *, const std::byte *> nursery_span(role role) const;
/** @return reserved size of Tenured to-space **/
std::size_t tenured_to_reserved() const;
/** @return committed size of Tenured to-space **/
std::size_t tenured_to_committed() const;
/** @return tenured bytes used before checkpoint **/
@ -167,8 +176,24 @@ namespace xo {
/** @return generation to which object at @p x belongs **/
generation_result tospace_generation_of(const void * x) const;
/** @return generation to which object at @p x belongs,
* location relative to base address for that generation,
* and allocated size of that generation
* @p role chooses between to-space and from-space
**/
std::tuple<generation_result, std::size_t, std::size_t> location_of(role role, const void * x) const;
/** @return generation to which object at @p x belongs,
* location relative to base address for @p x,
* and allocated size of generation
**/
std::tuple<generation_result,std::size_t,std::size_t> tospace_location_of(const void * x) const;
/** @return generation that contains @p x, given it's in from-space **/
generation_result fromspace_generation_of(const void * x) const;
/** @return generation to which object at @p x belongs,
* location relative to base address for @p x,
* and allocated size of generation
**/
std::tuple<generation_result,std::size_t,std::size_t> fromspace_location_of(const void * x) const;
/** true iff from-space contains @p x **/
bool fromspace_contains(const void * x) const;
/** @return free pointer for generation @p gen, i.e. nursery or tenured space **/
@ -361,6 +386,9 @@ namespace xo {
/** enabled when 0. disabled when <0 **/
int gc_enabled_ = 0;
/** rotating per-gc statistics history **/
GcStatisticsHistory gc_history_;
/** for (optional) viz: invoke when copying individual objects **/
GcCopyCallbackSet gc_copy_cbset_;
};

View file

@ -6,6 +6,7 @@
#pragma once
#include "generation.hpp"
#include "CircularBuffer.hpp"
#include "xo/reflect/TypeDescr.hpp"
#include "xo/indentlog/print/pretty.hpp"
#include <ostream>
@ -57,6 +58,8 @@ namespace xo {
**/
class GcStatistics {
public:
GcStatistics() = default;
/** update statistics after a GC cycle
* @param upto. nursery -> incremental collection; tenured -> full collection
* @param alloc_z. new allocations (since preceding GC)
@ -108,6 +111,7 @@ namespace xo {
**/
class GcStatisticsExt : public GcStatistics {
public:
GcStatisticsExt() = default;
explicit GcStatisticsExt(const GcStatistics & x) : GcStatistics{x} {}
/** @param os. write stats on this output stream **/
@ -128,6 +132,63 @@ namespace xo {
return os;
}
/** @class GcStatisticsHistoryItem
* @brief info we want to record over time (won't have cumulative things in it)
**/
class GcStatisticsHistoryItem {
public:
GcStatisticsHistoryItem() = default;
GcStatisticsHistoryItem(generation upto,
std::size_t new_alloc_z,
std::size_t survive_z,
std::size_t promote_z,
std::size_t persist_z,
std::size_t effort_z,
std::size_t garbage0_z,
std::size_t garbage1_z,
std::size_t garbageN_z)
: upto_{upto},
new_alloc_z_{new_alloc_z},
survive_z_{survive_z},
promote_z_{promote_z},
persist_z_{persist_z},
effort_z_{effort_z},
garbage0_z_{garbage0_z},
garbage1_z_{garbage1_z},
garbageN_z_{garbageN_z}
{}
/** @param os. write stats on this output stream **/
void display(std::ostream & os) const;
/** type of GC that generated this record **/
generation upto_;
/** #of bytes new allocation **/
std::size_t new_alloc_z_ = 0;
/** #of bytes surviving their first collection (i.e. N0->N1) **/
std::size_t survive_z_ = 0;
/** #of bytes promoted to tenured.
* Comprises all objects surviving their 2nd collection (i.e. N1->T)
**/
std::size_t promote_z_ = 0;
/** #of bytes surviving 3rd of later collection **/
std::size_t persist_z_ = 0;
/** #of bytes copied **/
std::size_t effort_z_ = 0;
/** #of bytes garbage from N0 (i.e. survived 0 GCs) **/
std::size_t garbage0_z_ = 0;
/** #of bytes garbage from N1 (i.e. survived 1 GCs) **/
std::size_t garbage1_z_ = 0;
/** #of bytes garbage from T (i.e. survived 2+ GCs) **/
std::size_t garbageN_z_ = 0;
};
inline std::ostream & operator<< (std::ostream & os, const GcStatisticsHistoryItem & x) {
x.display(os);
return os;
}
using GcStatisticsHistory = CircularBuffer<GcStatisticsHistoryItem>;
} /*namespace gc*/
namespace print {
@ -145,6 +206,11 @@ namespace xo {
struct ppdetail<xo::gc::GcStatisticsExt> {
static bool print_pretty(const ppindentinfo &, const xo::gc::GcStatisticsExt &);
};
template<>
struct ppdetail<xo::gc::GcStatisticsHistoryItem> {
static bool print_pretty(const ppindentinfo &, const xo::gc::GcStatisticsHistoryItem &);
};
} /*namespace print*/
} /*namespace xo*/

View file

@ -34,6 +34,9 @@ namespace xo {
static up<ListAlloc> make(const std::string & name, std::size_t cz, std::size_t nz, bool debug_flag);
/** page size used by underlying ArenaAlloc **/
std::size_t page_size() const;
/** reset to have at least @p z bytes of storage **/
bool reset(std::size_t z);

View file

@ -30,6 +30,7 @@ namespace xo {
tenured,
not_found
};
} /*namespace gc*/
} /*namespace xo*/