gc.c: add optional GC statistics counters

Add gc_total_count, minor_gc_count, major_gc_count (uint32_t) to
mrb_gc, guarded by MRB_GC_STATS. Zero cost when disabled.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2026-03-10 16:08:35 +09:00
parent 78658d67e1
commit 52fb294172
2 changed files with 20 additions and 0 deletions
+6
View File
@@ -64,6 +64,12 @@ typedef struct mrb_gc {
int arena_capa; /* size of protection array */
#endif
int arena_idx;
#ifdef MRB_GC_STATS
uint32_t gc_total_count; /* total GC invocations */
uint32_t minor_gc_count; /* minor GC count */
uint32_t major_gc_count; /* major GC count */
#endif
} mrb_gc;
MRB_API mrb_bool mrb_object_dead_p(mrb_state *mrb, struct RBasic *object);
+14
View File
@@ -1322,9 +1322,19 @@ mrb_incremental_gc(mrb_state *mrb)
if (gc->disabled || gc->iterating) return;
if (is_minor_gc(gc)) {
#ifdef MRB_GC_STATS
gc->gc_total_count++;
gc->minor_gc_count++;
#endif
incremental_gc_finish(mrb, gc);
}
else {
#ifdef MRB_GC_STATS
if (gc->state == MRB_GC_STATE_ROOT) {
gc->gc_total_count++;
gc->major_gc_count++;
}
#endif
incremental_gc_step(mrb, gc);
}
@@ -1364,6 +1374,10 @@ mrb_full_gc(mrb_state *mrb)
if (!mrb->c) return;
if (gc->disabled || gc->iterating) return;
#ifdef MRB_GC_STATS
gc->gc_total_count++;
gc->major_gc_count++;
#endif
if (is_generational(gc)) {
/* clear all the old objects back to young */
clear_all_old(mrb, gc);