code wiki / _hdl_build / nx_mem_healer.nx

nx_mem_healer.nx

buildroot/runtime/_hdl_build/nx_mem_healer.nx

18404 B339 linesdepth 3pulls 3 transitivereach 0 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

about

nx_mem_healer.nx -- the swarm ADDRESS/PREVENT pillar: a periodic memory-cap AUTO-HEAL. Dispatched ARG-LESS by the sovereign clock (nx_clock_tickless, a root supervisor child) every ~60s. Reads /proc/meminfo; if used% >= MH_THRESHOLD it scans /proc (getdents, bounded reads -- never sys_read_file) for the TOP-RSS process, and if that process is a known guard-supervised daemon it restarts it via `./nx_hostctl <kicksub>` (root -> kill by name; the hostctl guard respawns it fresh, reclaiming the leaked pages). This catches ANY daemon memory leak BY CONSTRUCTION -- the docportal per-request-mmap class and any future one -- with zero per-daemon code and NO edit to the supervisor. Composes the /proc idioms from nx_node_beacon + nx_zombie_audit. Run: nx_mem_healer (the live/clock path): sample mem; heal the top offender iff mem>=threshold. Building via nx_sov_build_run runs this once on WSL (mem<threshold -> OK -> exit 0). nx_mem_healer gate self-gate: pure-logic KATs (threshold decision + comm->kicksub map), no /proc, no heal. license_tier: ORIGINAL

dependencies 2 imports · 0 importers

nx_syscalls.nx nx_itoa_lib.nx nx_mem_healer.nx

imports: nx_syscalls.nxnx_itoa_lib.nx

imported by: nobody (leaf or entry point)

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main mh_streq mh_gate sys_mmap mh_kicksub mh_streq ↻ mh_setout mh_streq ↻ mh_w sys_write mh_reap_cores sys_openat_rd sys_mmap ↻ sys_getdents64 dirent_reclen dirent_name mh_contains mh_slen sys_fstatat mh_cat mh_n_into sys_mmap ↻ sys_now_realtime_sec sys_mmap ↻ sys_clock_gettime_real sys_openat_append sys_write ↻ sys_close sys_mmap ↻ mh_mem_pct mh_readproc sys_openat_rd ↻ sys_read sys_close ↻ sys_mmap ↻ mh_find_after mh_pdec mh_skip_sp mh_cat ↻ mh_n_into ↻

structs

none

consts

17const MH_MAGIC_65536: i64 = 65536
18const MH_MAGIC_1048576: i64 = 1048576
20const MH_THRESHOLD: i64 = 88 // mem used% at/above which we heal. Below = report + no-op. Data-driven cap.
21const MH_BUF: i64 = 65536
22const MH_LOGP: *u8 = "mem_healer.log" // cwd-relative (nishihost); append-only heal ledger
23const MH_HOSTCTL: *u8 = "./nx_hostctl" // consts (NOT inline literals) so they materialize as valid argv ptrs (.nx gotcha)
24const MH_PATHENV: *u8 = "PATH=/usr/bin:/bin"
25const MH_RESTART: *u8 = "restart" // nx_hostctl restart <elf> = cmd_restart_known -> kill-by-cmdline -> guard respawns
26const MH_CORE_MIN: i64 = 1073741824 // 1 GiB: reap Synology crash core-dumps (@*core.gz*) >= this from /volume1 (a 333 GB nx_hostctl core filled the volume)

functions

28func mh_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
called by 1: mh_gate calls 1: sys_write
33func mh_wn(v: i64) -> i64 { nxi_out(v); return 0 }
calls 1: nxi_out
34func mh_cat(dst: *u8, o: i64, s: *u8) -> i64 { var x: i64 = o; var i: i64 = 0; while s[i] != (0 as u8) { dst[x] = s[i]; x = x + 1; i = i + 1 } return x }
called by 2: mh_reap_coresmain
35func mh_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
called by 1: mh_contains
37func mh_contains(hay: *u8, needle: *u8) -> i64
called by 1: mh_reap_cores calls 1: mh_slen
50func mh_readproc(path: *u8, buf: *u8, cap: i64) -> i64
58func mh_skip_sp(buf: *u8, n: i64, p: i64) -> i64
63func mh_pdec(buf: *u8, n: i64, p: i64, pend: *i64) -> i64
73func mh_find_after(buf: *u8, n: i64, key: *u8, klen: i64) -> i64
called by 1: mh_mem_pct
84func mh_mem_pct(buf: *u8) -> i64
98func mh_n_into(dst: *u8, off: i64, v: i64) -> i64
105func mh_pid_from_name(name: *u8) -> i64
called by 1: main
112func mh_rss_pages(pid: i64, buf: *u8) -> i64
126func mh_comm(pid: i64, out: *u8, cap: i64) -> i64
called by 1: main calls 3: sys_mmapmh_n_intomh_readproc
137func mh_streq(a: *u8, b: *u8) -> i64
145func mh_setout(out: *u8, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { out[i] = s[i]; i = i + 1 } out[i] = 0 as u8; return 1 }
called by 1: mh_kicksub
150func mh_kicksub(comm: *u8, out: *u8) -> i64
called by 2: mh_gatemain calls 2: mh_streqmh_setout
168func mh_log(comm: *u8, pid: i64, rss_mb: i64, mem_pct: i64, sub: *u8, acted: i64) -> i64
191func mh_actuate(sub: *u8) -> i64
209func mh_reap_cores() -> i64
261func mh_gate() -> i64
called by 1: main calls 4: sys_mmapmh_kicksubmh_streqmh_w
281func main(argc: i64, argv: *i64) -> i64