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

@@ -1242,7 +1242,14 @@
12421242 /* Keys are spinel rodata literals (SPL: 0xff marker prefix) so the str-hash
12431243 header cache's s[-1] read is in-bounds -- a bare C literal here would
12441244 overread (and could alias a heap marker on some rodata layouts). */
1245-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;}
1245+static sp_StrIntHash*sp_gc_stat(void){
1246+ /* The string heap (sp_str_heap) is malloc'd separately and deliberately
1247+ excluded from sp_gc_bytes (see sp_str_alloc). Surface its footprint so
1248+ GC.stat can explain "RSS huge but bytes tiny" for string-heavy workloads.
1249+ Prototype: O(n) walk; a production version maintains a running counter. */
1250+ size_t str_bytes=0; mrb_int str_count=0;
1251+ for(sp_str_hdr*sh=sp_str_heap; sh; sh=sh->next){ str_bytes+=sh->size; str_count++; }
1252+ 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;}
12461253
12471254 static void sp_StrStrHash_fin(void*p){sp_StrStrHash*h=(sp_StrStrHash*)p;free(h->keys);free(h->vals);free(h->order);}
12481255 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);}