Commit 96f3227c

96f3227c76a4Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

Merge pull request #1460 from rubys/gc-stat-string-heap

GC.stat: expose string-heap footprint (str_bytes / str_count)

Details

Commit
96f3227c76a4
Parent
ba3f14d27e53
Merge parent
b6f9c1b0295b

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);}