From 4c824edbe46ed29678a5f5c63b1fc95036b98042 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Sun, 24 Aug 2025 17:03:04 -0400 Subject: [PATCH] xo-imgui: ex2: + average efficiency + plot --- include/xo/alloc/GcStatistics.hpp | 44 +++++++++++++++++++++++-------- src/alloc/GC.cpp | 13 ++++++++- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/include/xo/alloc/GcStatistics.hpp b/include/xo/alloc/GcStatistics.hpp index c0058b5f..b591bf22 100644 --- a/include/xo/alloc/GcStatistics.hpp +++ b/include/xo/alloc/GcStatistics.hpp @@ -163,17 +163,23 @@ namespace xo { std::size_t garbage0_z, std::size_t garbage1_z, std::size_t garbageN_z, - nanos dt) : gc_seq_{gc_seq}, - 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}, - dt_{dt} {} + nanos dt, + std::size_t sum_effort_z, + std::size_t sum_garbage_z) + : gc_seq_{gc_seq}, + 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}, + dt_{dt}, + sum_effort_z_{sum_effort_z}, + sum_garbage_z_{sum_garbage_z} + {} constexpr GcStatisticsHistoryItem(const GcStatisticsHistoryItem &) = default; std::size_t garbage_z() const { return garbage0_z_ + garbage1_z_ + garbageN_z_; } @@ -184,6 +190,11 @@ namespace xo { return gz / static_cast(effort_z_ + gz); } + /** lifetime byte-weighted average collection efficiency. Always in [0.0, 1.0] **/ + float average_efficiency() const { + return sum_garbage_z_ / static_cast(sum_effort_z_ + sum_garbage_z_); + } + /** collection rate, in bytes/sec **/ float collection_rate() const; @@ -199,6 +210,10 @@ namespace xo { garbage1_z_ = x.garbage1_z_; garbageN_z_ = x.garbageN_z_; this->dt_.scale_ = x.dt_.scale_; + + sum_effort_z_ = x.sum_effort_z_; + sum_garbage_z_ = x.sum_garbage_z_; + return *this; } @@ -229,6 +244,13 @@ namespace xo { std::size_t garbageN_z_ = 0; /** elapsed time for this GC (see @ref GC::execute_gc) **/ nanos dt_; + + // ----- cumulative statistics ----- + + /** sum (in bytes) copied by collections since inception **/ + std::size_t sum_effort_z_ = 0; + /** sum (in bytes) of garbage collected since inception **/ + std::size_t sum_garbage_z_ = 0; }; inline std::ostream & operator<< (std::ostream & os, const GcStatisticsHistoryItem & x) { diff --git a/src/alloc/GC.cpp b/src/alloc/GC.cpp index 200ac218..1b5481cb 100644 --- a/src/alloc/GC.cpp +++ b/src/alloc/GC.cpp @@ -1119,6 +1119,15 @@ namespace xo { // still want to update tenured stats for current alloc size this->gc_statistics_.update_snapshot(generation::tenured, T_after_gc); } + + std::size_t sum_effort_z = effort_z; + std::size_t sum_garbage_z = garbage0_z + garbage1_z + garbageN_z; + + if (gc_history_.size() > 0) { + sum_effort_z += gc_history_.back().sum_effort_z_; + sum_garbage_z += gc_history_.back().sum_garbage_z_; + } + GcStatisticsHistoryItem item(gc_statistics_.n_gc(), upto, new_alloc_z, @@ -1129,7 +1138,9 @@ namespace xo { garbage0_z, garbage1_z, garbageN_z, - dt); + dt, + sum_effort_z, + sum_garbage_z); log && log(xtag("gcseq_after_gc", gc_statistics_.n_gc()), xtag("item", item));