code wiki / _hdl_build / nx_mem_healer.nx
nx_mem_healer.nx
buildroot/runtime/_hdl_build/nx_mem_healer.nx
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
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
structs
| none |
consts
| 17 | const MH_MAGIC_65536: i64 = 65536 |
| 18 | const MH_MAGIC_1048576: i64 = 1048576 |
| 20 | const MH_THRESHOLD: i64 = 88 // mem used% at/above which we heal. Below = report + no-op. Data-driven cap. |
| 21 | const MH_BUF: i64 = 65536 |
| 22 | const MH_LOGP: *u8 = "mem_healer.log" // cwd-relative (nishihost); append-only heal ledger |
| 23 | const MH_HOSTCTL: *u8 = "./nx_hostctl" // consts (NOT inline literals) so they materialize as valid argv ptrs (.nx gotcha) |
| 24 | const MH_PATHENV: *u8 = "PATH=/usr/bin:/bin" |
| 25 | const MH_RESTART: *u8 = "restart" // nx_hostctl restart <elf> = cmd_restart_known -> kill-by-cmdline -> guard respawns |
| 26 | const 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
| 28 | func 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 } |
| 33 | func mh_wn(v: i64) -> i64 { nxi_out(v); return 0 } calls 1: nxi_out |
| 34 | func 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 } |
| 35 | func 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 |
| 37 | func mh_contains(hay: *u8, needle: *u8) -> i64 |
| 50 | func mh_readproc(path: *u8, buf: *u8, cap: i64) -> i64 |
| 58 | func mh_skip_sp(buf: *u8, n: i64, p: i64) -> i64 |
| 63 | func mh_pdec(buf: *u8, n: i64, p: i64, pend: *i64) -> i64 |
| 73 | func mh_find_after(buf: *u8, n: i64, key: *u8, klen: i64) -> i64 called by 1: mh_mem_pct |
| 84 | func mh_mem_pct(buf: *u8) -> i64 |
| 98 | func mh_n_into(dst: *u8, off: i64, v: i64) -> i64 |
| 105 | func mh_pid_from_name(name: *u8) -> i64 called by 1: main |
| 112 | func mh_rss_pages(pid: i64, buf: *u8) -> i64 |
| 126 | func mh_comm(pid: i64, out: *u8, cap: i64) -> i64 |
| 137 | func mh_streq(a: *u8, b: *u8) -> i64 |
| 145 | func 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 |
| 150 | func mh_kicksub(comm: *u8, out: *u8) -> i64 |
| 168 | func mh_log(comm: *u8, pid: i64, rss_mb: i64, mem_pct: i64, sub: *u8, acted: i64) -> i64 |
| 191 | func mh_actuate(sub: *u8) -> i64 |
| 209 | func mh_reap_cores() -> i64 called by 1: main calls 13: sys_openat_rdsys_mmapsys_getdents64dirent_reclendirent_namemh_contains+7 |
| 261 | func mh_gate() -> i64 |
| 281 | func main(argc: i64, argv: *i64) -> i64 |