Commit a5aded52

a5aded520b5bMerged

Sam Ruby <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

string-heap GC trigger + delete read-first (matz/spinel#1450)

sp_str_alloc never triggered a collection (string sweep only runs from sp_gc_collect, which fires on object-heap pressure), so a string-only workload never collects -> unbounded RSS. Add a string-heap byte counter (sp_str_heap_bytes) + separate threshold; trigger sp_gc_collect from sp_str_alloc on string-heap growth, WITHOUT refolding into sp_gc_bytes (that over-fired the object heuristic). Also restructure sp_str_delete/_n to read the source into a malloc temp BEFORE the result sp_str_alloc, so a string-heap collection can't sweep an unrooted source mid-copy.

VALIDATED: leak repro 18GB OOM -> 4MB; normal 'make test' 931/931; SPINEL_GC_STRESS=1 'make test' 925 pass / 6 fail (master baseline: 926/5 -- the +1 is bundle_tiny_string, a separate cross-op rooting gap, NOT delete).

NOT merge-ready standalone: turning the trigger on activates latent unrooted-string-temp gaps (#1057/#1068/#1445 class) that GC_STRESS surfaces. See HANDOFF.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
a5aded520b5b
Parent
6edcb4097963

lib/sp_runtime.hmodifiedView file

@@ -367,6 +367,18 @@
367367 /* ---- String GC ---- */
368368 /* __thread: each Ractor owns a private string heap. */
369369 static __thread sp_str_hdr *sp_str_heap = NULL;
370+/* String-heap collection trigger (matz/spinel#1450). Strings live on a separate
371+ malloc'd heap and are reaped only by sp_str_sweep, which runs from
372+ sp_gc_collect -- which only fires on OBJECT-heap pressure (sp_gc_bytes). A
373+ string-only workload (e.g. an HTTP server building responses, or a tight
374+ `s = s + x` loop) thus never triggers a collection and RSS grows without
375+ bound. Drive collection off the string heap's own live-byte count, with a
376+ SEPARATE threshold so string bytes are NOT refolded into sp_gc_bytes (doing
377+ so over-fired the object heuristic -- the reason they were excluded; see the
378+ comment in sp_str_alloc). */
379+static __thread size_t sp_str_heap_bytes = 0; /* live string-heap bytes */
380+static __thread size_t sp_str_threshold = 256*1024; /* mirrors sp_gc_threshold init */
381+static __thread int sp_str_stress_checked = 0;
370382 #define SPL(s) (&("\xff" s)[1])
371383 static const char sp_str_empty_data[] = "\xff";
372384 #define sp_str_empty (sp_str_empty_data + 1)
@@ -428,6 +440,21 @@
428440
429441 static char *sp_str_alloc(size_t len) {
430442 size_t total = sizeof(sp_str_hdr) + 1 + len + 1;
443+ /* String-heap pressure drives its own collection (see sp_str_heap_bytes).
444+ Collect BEFORE the new allocation, like sp_gc_alloc, so the string being
445+ built isn't yet live during the sweep. Operands of the calling op (e.g. the
446+ arguments to sp_str_concat) must be reachable across this point -- they are
447+ for rooted locals; the codegen's SP_GC_ROOT discipline is what keeps them
448+ so. Threshold recompute mirrors sp_gc_alloc's. */
449+ if (!sp_str_stress_checked) { sp_str_stress_checked = 1; const char *e = getenv("SPINEL_GC_STRESS"); if (e && *e && *e != '0') sp_str_threshold = 2048; }
450+ if (sp_str_heap_bytes > sp_str_threshold) {
451+ size_t before = sp_str_heap_bytes;
452+ sp_gc_collect(); /* runs sp_str_sweep, which decrements sp_str_heap_bytes */
453+ size_t freed = before - sp_str_heap_bytes;
454+ if (freed < before/4) sp_str_threshold = before*2;
455+ else if (sp_str_heap_bytes > 0) { sp_str_threshold = sp_str_heap_bytes*4; if (sp_str_threshold < 256*1024) sp_str_threshold = 256*1024; }
456+ else sp_str_threshold = 256*1024;
457+ }
431458 sp_str_hdr *h = (sp_str_hdr *)malloc(total);
432459 if (!h) sp_oom_die();
433460 h->next = sp_str_heap;
@@ -435,6 +462,7 @@
435462 h->len = (uint32_t)len;
436463 h->hash = 0;
437464 sp_str_heap = h;
465+ sp_str_heap_bytes += total;
438466 /* Don't fold string-heap pressure into sp_gc_bytes : the
439467 threshold heuristic in sp_gc_alloc is keyed on heap survivors, and
440468 the str-heap mark-sweep that runs alongside (sp_str_sweep,
@@ -674,8 +702,7 @@
674702 }
675703 else {
676704 *pp = h->next;
677- /* String allocs no longer fold into sp_gc_bytes (see
678- sp_str_alloc); the matching subtract here drops too. */
705+ sp_str_heap_bytes -= h->size; /* keep the string-heap live-byte count in step */
679706 free(h);
680707 }
681708 }
@@ -689,6 +716,7 @@
689716 sp_str_hdr *h = sp_str_heap;
690717 while (h) { sp_str_hdr *n = h->next; free(h); h = n; }
691718 sp_str_heap = NULL;
719+ sp_str_heap_bytes = 0;
692720 sp_str_lcache_clear();
693721 }
694722
@@ -1817,7 +1845,10 @@
18171845 free(buf); free(fcps); free(tcps);
18181846 return r;
18191847 }
1820-static const char*sp_str_delete(const char*s,const char*chars){if(!s)return sp_str_empty;if(!chars)return s;int negate=0;const char*csp=chars;if(*csp=='^'&&*(csp+1)){negate=1;csp++;}size_t setn;uint32_t*set=sp_utf8_decode_charset(csp,&setn);size_t bl=strlen(s);char*r=sp_str_alloc_raw(bl+1);size_t n=0;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);int in_set=sp_utf8_set_has(set,setn,cp);if(negate)in_set=!in_set;if(!in_set){memcpy(r+n,p,cn);n+=cn;}p+=cn;}r[n]=0;sp_str_set_len(r,n);free(set);return r;}
1848+/* Build into a malloc temp and read all of `s` BEFORE the result sp_str_alloc:
1849+ that allocation can now trigger a string-heap collection, which would sweep
1850+ an unrooted `s` mid-copy (the read-first pattern sp_str_tr/sp_str_format use). */
1851+static const char*sp_str_delete(const char*s,const char*chars){if(!s)return sp_str_empty;if(!chars)return s;int negate=0;const char*csp=chars;if(*csp=='^'&&*(csp+1)){negate=1;csp++;}size_t setn;uint32_t*set=sp_utf8_decode_charset(csp,&setn);size_t bl=strlen(s);char*buf=(char*)malloc(bl+1);size_t n=0;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);int in_set=sp_utf8_set_has(set,setn,cp);if(negate)in_set=!in_set;if(!in_set){memcpy(buf+n,p,cn);n+=cn;}p+=cn;}buf[n]=0;free(set);char*r=sp_str_alloc(n);memcpy(r,buf,n+1);free(buf);return r;}
18211852 /* Issue #921: shrink the heap-string header length to match the
18221853 squeezed payload — the alloc gives bl+1 bytes, the squeezed
18231854 write fills n<=bl, leaving the header's stored length stale.
@@ -1831,7 +1862,7 @@
18311862 /* Multi-arg delete/squeeze: delete (or squeeze runs of) characters that
18321863 are in the INTERSECTION of all n charset args, mirroring
18331864 sp_str_count_n. Each arg is a charset spec (^negation, a-z ranges). */
1834-static const char*sp_str_delete_n(const char*s,const char**chars,mrb_int n){if(!s)return sp_str_empty;if(n<=0)return s;size_t*setns=(size_t*)malloc(n*sizeof(size_t));uint32_t**sets=(uint32_t**)malloc(n*sizeof(uint32_t*));int*negs=(int*)malloc(n*sizeof(int));for(mrb_int i=0;i<n;i++){if(!chars[i])sp_raise_cls("TypeError","no implicit conversion of nil into String");const char*cs=chars[i];negs[i]=0;if(*cs=='^'&&*(cs+1)){negs[i]=1;cs++;}sets[i]=sp_utf8_decode_charset(cs,&setns[i]);}size_t bl=strlen(s);char*r=sp_str_alloc_raw(bl+1);size_t m=0;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);int all=1;for(mrb_int i=0;i<n;i++){int in_set=sp_utf8_set_has(sets[i],setns[i],cp);if(negs[i])in_set=!in_set;if(!in_set){all=0;break;}}if(!all){memcpy(r+m,p,cn);m+=cn;}p+=cn;}r[m]=0;sp_str_set_len(r,m);for(mrb_int i=0;i<n;i++)free(sets[i]);free(sets);free(setns);free(negs);return r;}
1865+static const char*sp_str_delete_n(const char*s,const char**chars,mrb_int n){if(!s)return sp_str_empty;if(n<=0)return s;size_t*setns=(size_t*)malloc(n*sizeof(size_t));uint32_t**sets=(uint32_t**)malloc(n*sizeof(uint32_t*));int*negs=(int*)malloc(n*sizeof(int));for(mrb_int i=0;i<n;i++){if(!chars[i])sp_raise_cls("TypeError","no implicit conversion of nil into String");const char*cs=chars[i];negs[i]=0;if(*cs=='^'&&*(cs+1)){negs[i]=1;cs++;}sets[i]=sp_utf8_decode_charset(cs,&setns[i]);}size_t bl=strlen(s);char*buf=(char*)malloc(bl+1);size_t m=0;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);int all=1;for(mrb_int i=0;i<n;i++){int in_set=sp_utf8_set_has(sets[i],setns[i],cp);if(negs[i])in_set=!in_set;if(!in_set){all=0;break;}}if(!all){memcpy(buf+m,p,cn);m+=cn;}p+=cn;}buf[m]=0;for(mrb_int i=0;i<n;i++)free(sets[i]);free(sets);free(setns);free(negs);char*r=sp_str_alloc(m);memcpy(r,buf,m+1);free(buf);return r;}
18351866 static const char*sp_str_squeeze_n(const char*s,const char**chars,mrb_int n){if(!s)return sp_str_empty;if(n<=0)return sp_str_squeeze(s);size_t*setns=(size_t*)malloc(n*sizeof(size_t));uint32_t**sets=(uint32_t**)malloc(n*sizeof(uint32_t*));int*negs=(int*)malloc(n*sizeof(int));for(mrb_int i=0;i<n;i++){if(!chars[i])sp_raise_cls("TypeError","no implicit conversion of nil into String");const char*cs=chars[i];negs[i]=0;if(*cs=='^'&&*(cs+1)){negs[i]=1;cs++;}sets[i]=sp_utf8_decode_charset(cs,&setns[i]);}size_t bl=strlen(s);char*r=sp_str_alloc_raw(bl+1);size_t m=0;uint32_t prev=0xFFFFFFFFu;const char*p=s;while(*p){uint32_t cp;int cn=sp_utf8_decode(p,&cp);int all=1;for(mrb_int i=0;i<n;i++){int in_set=sp_utf8_set_has(sets[i],setns[i],cp);if(negs[i])in_set=!in_set;if(!in_set){all=0;break;}}if(!all){memcpy(r+m,p,cn);m+=cn;prev=0xFFFFFFFFu;}else if(cp!=prev){memcpy(r+m,p,cn);m+=cn;prev=cp;}p+=cn;}r[m]=0;sp_str_set_len(r,m);for(mrb_int i=0;i<n;i++)free(sets[i]);free(sets);free(setns);free(negs);return r;}
18361867
18371868 /* Forward decl from sp_crypto.h (libspinel_rt.a). Used by