Commit 1f269f44

1f269f441da6Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Add opt-in SPINEL_MAX_HEAP_MB memory governor for the analysis pass

spinel_analyze can reach 100+ GB RSS compiling large auto-generated, mutually-referential gem class graphs (APIMATIC-style SDKs with hundreds of class Model < BaseModel), running the host out of memory and letting the kernel OOM-killer take down unrelated processes.

Add an opt-in governor: when SPINEL_MAX_HEAP_MB is set (>0), abort with a diagnostic once process RSS exceeds it, rather than allocating without limit. Checked only at GC-trigger points (infrequent) against real RSS from /proc/self/statm -- not the sp_gc_bytes running counter, which is unsigned and transiently underflows as finalizers subtract. Default (unset) is no limit, so behavior is unchanged for every program and there is no per-alloc overhead on the common path.

This bounds the failure (the issue's "Expected": fail with a diagnostic instead of OOMing the host); it does not reduce the analyzer's memory consumption, which is a separate, larger change.

Refs #1302

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
1f269f441da6
Parent
fa88c4832cfa

lib/sp_runtime.hmodifiedView file

@@ -754,7 +754,26 @@
754754 "unhandled exception" style so post-mortem scripts treat it the
755755 same way. */
756756 static void sp_oom_die(void){fputs("unhandled exception: out of memory\n",stderr);exit(1);}
757-void*sp_gc_alloc(size_t sz,void(*fin)(void*),void(*scn)(void*)){if(sp_gc_bytes>sp_gc_threshold){size_t before=sp_gc_bytes;sp_gc_collect();size_t freed=before-sp_gc_bytes;if(freed<before/4){sp_gc_threshold=before*2;}else if(sp_gc_bytes>0){sp_gc_threshold=sp_gc_bytes*4;if(sp_gc_threshold<sp_gc_threshold_init)sp_gc_threshold=sp_gc_threshold_init;}else{sp_gc_threshold=sp_gc_threshold_init;}}size_t need=sizeof(sp_gc_hdr)+sz;sp_gc_hdr*h=(sp_gc_hdr*)calloc(1,need);if(!h)sp_oom_die();h->finalize=fin;h->scan=scn;h->size=need;h->marked=0;h->next=sp_gc_heap;sp_gc_heap=h;sp_gc_bytes+=need;return(char*)h+sizeof(sp_gc_hdr);}
757+/* Issue #1302: optional memory ceiling. When SPINEL_MAX_HEAP_MB is set (>0),
758+ abort with a diagnostic once process RSS exceeds it, rather than allocating
759+ without limit until the kernel OOM-killer fires and takes down unrelated
760+ processes (observed: spinel_analyze reaching 100+ GB on huge auto-generated,
761+ mutually-referential gem class graphs). Checked only at GC-trigger points
762+ (infrequent) and against REAL RSS from /proc/self/statm — not the sp_gc_bytes
763+ running counter, which is unsigned and can transiently underflow as
764+ finalizers subtract. Default (unset) is no limit: behavior is unchanged for
765+ every program; this is an opt-in safety governor. */
766+static size_t sp_gc_max_bytes=0; static int sp_gc_max_bytes_init=0;
767+static void sp_gc_enforce_mem_limit(void){
768+ if(!sp_gc_max_bytes_init){const char*e=getenv("SPINEL_MAX_HEAP_MB");long v=(e&&*e)?atol(e):0;sp_gc_max_bytes=(v>0)?(size_t)v*1024*1024:0;sp_gc_max_bytes_init=1;}
769+ if(!sp_gc_max_bytes)return;
770+#if defined(__linux__)
771+ FILE*sf=fopen("/proc/self/statm","r");if(!sf)return;long tot=0,res=0;int n=fscanf(sf,"%ld %ld",&tot,&res);fclose(sf);if(n!=2||res<=0)return;
772+ size_t rss=(size_t)res*(size_t)sysconf(_SC_PAGESIZE);
773+ if(rss>sp_gc_max_bytes){fprintf(stderr,"unhandled exception: out of memory (RSS %zu MB exceeded SPINEL_MAX_HEAP_MB=%zu MB)\n",rss/(1024*1024),sp_gc_max_bytes/(1024*1024));exit(1);}
774+#endif
775+}
776+void*sp_gc_alloc(size_t sz,void(*fin)(void*),void(*scn)(void*)){if(sp_gc_bytes>sp_gc_threshold){size_t before=sp_gc_bytes;sp_gc_collect();size_t freed=before-sp_gc_bytes;if(freed<before/4){sp_gc_threshold=before*2;}else if(sp_gc_bytes>0){sp_gc_threshold=sp_gc_bytes*4;if(sp_gc_threshold<sp_gc_threshold_init)sp_gc_threshold=sp_gc_threshold_init;}else{sp_gc_threshold=sp_gc_threshold_init;}sp_gc_enforce_mem_limit();}size_t need=sizeof(sp_gc_hdr)+sz;sp_gc_hdr*h=(sp_gc_hdr*)calloc(1,need);if(!h)sp_oom_die();h->finalize=fin;h->scan=scn;h->size=need;h->marked=0;h->next=sp_gc_heap;sp_gc_heap=h;sp_gc_bytes+=need;return(char*)h+sizeof(sp_gc_hdr);}
758777 void*sp_gc_alloc_nogc(size_t sz,void(*fin)(void*),void(*scn)(void*)){size_t need=sizeof(sp_gc_hdr)+sz;sp_gc_hdr*h=(sp_gc_hdr*)calloc(1,need);if(!h)sp_oom_die();h->finalize=fin;h->scan=scn;h->size=need;h->marked=0;h->next=sp_gc_heap;sp_gc_heap=h;sp_gc_bytes+=need;return(char*)h+sizeof(sp_gc_hdr);}
759778 /* GC-header frozen bit — used for containers whose mutators are NOT
760779 on a hot path (hashes), so the extra cache line vs. a struct field