Commit b6f9c1b0
b6f9c1b0295bMerged
Sam Ruby <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
GC.stat: expose string-heap footprint (str_bytes / str_count)
Spinel keeps string data on a separate malloc'd heap (sp_str_heap), deliberately excluded from sp_gc_bytes so the GC-threshold heuristic isn't skewed by string churn (see the comment in sp_str_alloc). A consequence is that GC.stat — which reports only the object heap — is blind to where a string-heavy workload (e.g. an HTTP server building responses) actually spends memory. You can sit at multi-GB RSS with GC.stat["bytes"] reading a few hundred KB and no way to see why.
Surface the string heap in the same hash:
GC.stat["str_bytes"] # total bytes held in sp_str_heap GC.stat["str_count"] # number of live string allocations
This makes "RSS huge but bytes tiny" a one-line diagnosis. Found while chasing a server memory blow-up (matz/spinel#1450) where bytes stayed ~218 KB while RSS climbed to 15 GB — str_bytes reads the 15 GB directly, and str_count ("how many strings are pinned") points at the cause.
Implementation is an O(n) walk of sp_str_heap at call time (GC.stat is a diagnostic, not a hot path); if an O(1) figure is preferred I'm happy to maintain a running counter in sp_str_alloc / sp_str_sweep instead.
Motivated by the introspection RFC in matz/spinel#1021 (GC.stat landed; this extends it to the string heap).
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- b6f9c1b0295b
- Parent
- 6edcb4097963
lib/sp_runtime.hmodifiedView file
/* Keys are spinel rodata literals (SPL: 0xff marker prefix) so the str-hash header cache's s[-1] read is in-bounds -- a bare C literal here would overread (and could alias a heap marker on some rodata layouts). */static sp_StrIntHash*sp_gc_stat(void){sp_StrIntHash*h=sp_StrIntHash_new();sp_StrIntHash_set(h,SPL("bytes"),(mrb_int)sp_gc_bytes);sp_StrIntHash_set(h,SPL("old_bytes"),(mrb_int)sp_gc_old_bytes);sp_StrIntHash_set(h,SPL("threshold"),(mrb_int)sp_gc_threshold);sp_StrIntHash_set(h,SPL("cycle"),(mrb_int)sp_gc_cycle);sp_StrIntHash_set(h,SPL("full_runs"),(mrb_int)(sp_gc_cycle/SP_GC_FULL_INTERVAL));return h;}static sp_StrIntHash*sp_gc_stat(void){ /* The string heap (sp_str_heap) is malloc'd separately and deliberately excluded from sp_gc_bytes (see sp_str_alloc). Surface its footprint so GC.stat can explain "RSS huge but bytes tiny" for string-heavy workloads. Prototype: O(n) walk; a production version maintains a running counter. */ size_t str_bytes=0; mrb_int str_count=0; for(sp_str_hdr*sh=sp_str_heap; sh; sh=sh->next){ str_bytes+=sh->size; str_count++; } sp_StrIntHash*h=sp_StrIntHash_new();sp_StrIntHash_set(h,SPL("bytes"),(mrb_int)sp_gc_bytes);sp_StrIntHash_set(h,SPL("old_bytes"),(mrb_int)sp_gc_old_bytes);sp_StrIntHash_set(h,SPL("threshold"),(mrb_int)sp_gc_threshold);sp_StrIntHash_set(h,SPL("cycle"),(mrb_int)sp_gc_cycle);sp_StrIntHash_set(h,SPL("full_runs"),(mrb_int)(sp_gc_cycle/SP_GC_FULL_INTERVAL));sp_StrIntHash_set(h,SPL("str_bytes"),(mrb_int)str_bytes);sp_StrIntHash_set(h,SPL("str_count"),str_count);return h;}static void sp_StrStrHash_fin(void*p){sp_StrStrHash*h=(sp_StrStrHash*)p;free(h->keys);free(h->vals);free(h->order);}static void sp_StrStrHash_scan(void*p){sp_StrStrHash*h=(sp_StrStrHash*)p;for(mrb_int i=0;i<h->cap;i++){if(h->keys[i]){sp_mark_string(h->keys[i]);sp_mark_string(h->vals[i]);}}if(h->default_v)sp_mark_string(h->default_v);}test/gc_stat_string_heap.rbaddedView file
Loading diff…
test/gc_stat_string_heap.rb.expectedaddedView file
Loading diff…