code wiki / (root) / nx_resgov.nx

nx_resgov.nx source

↩ module page · 524 lines · 30121 B

1// nx_resgov.nx -- THE UNIFIED RESOURCE GOVERNOR (one-shot, cron-invoked every minute). 2// 3// nx_resgov [conf] [dry] conf default knowledge/resgov.conf ; `dry` = decide + log, never signal 4// 5// Walks EVERY process, meters VmRSS + VmSwap (the resource that actually hurts), remembers each 6// process's footprint between runs to compute GROWTH VELOCITY, and escalates gracefully long before a 7// ceiling is reached. Default-covered: an unlisted process is governed by the default cap, not ignored. 8// Death is decided here; RESPAWN stays the guard's job (single responsibility -- no dueling supervisors). 9// 10// STATE (velocity memory): knowledge/status/resgov.state, rows `pid:starttime footprint_kb epoch level_ts`. 11// ★IDENTITY IS pid+starttime, NEVER pid ALONE -- Linux reuses pids, and a reused pid would make the 12// velocity of a brand-new process look like the growth of the dead one it replaced, which is how a 13// predictive breaker kills innocents. 14// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0 15import "nx_syscalls.nx" 16import "nx_vsz_watchdog_core.nx" // proven /proc readers: vw_read, vw_status_kb_of, vw_num_at, vw_contains 17import "nx_resgov_core.nx" 18const RG_MAGIC_4096: i64 = 4096 19const RG_MAGIC_4095: i64 = 4095 20const RG_MAGIC_3600: i64 = 3600 21const RG_MAGIC_1024: i64 = 1024 22const RG_MAGIC_1048576: i64 = 1048576 23 24const RG_CONF_CAP: i64 = 16384 25const RG_STATE_CAP: i64 = 262144 26const RG_DIR_CAP: i64 = 65536 27const RG_CMD_CAP: i64 = 8192 28const RG_MAXROWS: i64 = 64 29const RG_MAXPROC: i64 = 2048 30const RG_STATE_PATH: *u8 = "knowledge/status/resgov.state" 31 32func g_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 33func g_putn(v: i64) -> i64 { 34 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 } 35 var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 36 let d: *u8 = sys_mmap(24); var k: i64 = 0 37 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 38 let o: *u8 = sys_mmap(24); var i: i64 = 0 39 while i < k { o[i] = d[k-1-i]; i = i + 1 } 40 sys_write(1, o, k) 41 return 0 42} 43func g_streq(a: *u8, b: *u8) -> i64 { 44 var i: i64 = 0 45 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } 46 if b[i] != (0 as u8) { return 0 } 47 return 1 48} 49func g_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 50// index of the first space in buf[from,to), or -1. ⚠Written as REAL scans returning a POSITION: the 51// first cut of this parser used the loop variable as a break FLAG (i = to + 1) and therefore threw the 52// position away -- it parsed 0 rows out of a perfectly good conf and every policy silently fell back to 53// the in-code defaults. ★A PARSER THAT LOSES THE POSITION REPORTS SUCCESS AND MEANS NOTHING. 54func g_find_sp(buf: *u8, from: i64, to: i64) -> i64 { 55 var i: i64 = from 56 var found: i64 = 0 - 1 57 while i < to { if buf[i] == (32 as u8) { found = i; i = to } else { i = i + 1 } } 58 return found 59} 60// index of the first non-space in buf[from,to), or -1 if only spaces remain. 61func g_skip_sp(buf: *u8, from: i64, to: i64) -> i64 { 62 var i: i64 = from 63 var found: i64 = 0 - 1 64 while i < to { if buf[i] != (32 as u8) { found = i; i = to } else { i = i + 1 } } 65 return found 66} 67func g_app(buf: *u8, p: i64, s: *u8) -> i64 { var i: i64 = 0; var q: i64 = p; while s[i] != (0 as u8) { buf[q] = s[i]; q = q + 1; i = i + 1 } return q } 68func g_appn(buf: *u8, p: i64, v: i64) -> i64 { 69 var q: i64 = p 70 if v == 0 { buf[q] = 48 as u8; return q + 1 } 71 var m: i64 = v 72 if m < 0 { buf[q] = 45 as u8; q = q + 1; m = 0 - m } 73 let t: *u8 = sys_mmap(32); var k: i64 = 0 74 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 75 var i: i64 = 0 76 while i < k { buf[q] = t[k-1-i]; q = q + 1; i = i + 1 } 77 return q 78} 79 80// process start-time (field 22 of /proc/<pid>/stat) -- the pid-reuse discriminator. 0 unreadable. 81// The comm field can contain spaces AND parens, so scanning starts after the LAST ')'. 82func g_starttime(dirname: *u8) -> i64 { 83 let path: *u8 = sys_mmap(256) 84 var o: i64 = g_app(path, 0, "/proc/" as *u8) 85 o = g_app(path, o, dirname) 86 o = g_app(path, o, "/stat" as *u8) 87 path[o] = 0 as u8 88 let b: *u8 = sys_mmap(RG_MAGIC_4096) 89 let n: i64 = vw_read(path, b, RG_MAGIC_4095) 90 if n <= 0 { return 0 } 91 var close: i64 = 0 - 1 92 var i: i64 = 0 93 while i < n { if b[i] == (41 as u8) { close = i } i = i + 1 } 94 if close < 0 { return 0 } 95 // fields after comm: state(3) ppid(4) ... starttime(22) -> the 19th token after ')' 96 var tok: i64 = 0 97 var j: i64 = close + 1 98 let ep: *i64 = sys_mmap(16) as *i64 99 var res: i64 = 0 100 var go: i64 = 1 101 while go == 1 { 102 go = 0 103 if j < n { 104 if b[j] == (32 as u8) { j = j + 1; go = 1 } else { 105 tok = tok + 1 106 if tok == 20 { res = vw_num_at(b, n, j, ep) } else { 107 while j < n { if b[j] == (32 as u8) { j = n + 1 } else { j = j + 1 } } 108 if j == n + 1 { j = j - 1 } 109 go = 1 110 } 111 } 112 } 113 } 114 return res 115} 116 117func main(argc: i64, argv: *i64) -> i64 { 118 var confp: *u8 = "knowledge/resgov.conf" as *u8 119 if argc >= 2 { confp = argv[1] as *u8 } 120 var dry: i64 = 0 121 if argc >= 3 { if g_streq(argv[2] as *u8, "dry" as *u8) == 1 { dry = 1 } } 122 123 // ---- config (data, not code -- rule 11) ---- 124 var default_cap_gb: i64 = 0 // 0 = INERT until the operator declares one (fail-safe) 125 var head_floor: i64 = 150 // permil of RAM free below which the system counts as scarce 126 var swap_ceil: i64 = 500 // permil of swap used above which the system counts as scarce 127 var eta_warn_s: i64 = RG_MAGIC_3600 128 var eta_act_s: i64 = 900 129 var cooldown_s: i64 = 600 130 // ---- AGGREGATE thresholds: the ACCUMULATED-DEBT regime (rg_aggregate_level) ---- 131 // Deliberately STRICTER than the per-process bars above. The per-process ladder governs RATE and 132 // asks "is any ONE process too big?"; this asks "is the SUM hurting us with nobody individually to 133 // blame?" -- the shape a leak epidemic actually takes. Recycling is for real distress, not busy. 134 var agg_swap_ceil: i64 = 800 // permil swap used above which ACCUMULATION counts as distress 135 var agg_head_floor: i64 = 100 // permil RAM free below which accumulation counts as distress 136 var agg_suspect_floor: i64 = 5 // how many non-exempt holders must exist before blaming the sum 137 var agg_suspect_mb: i64 = 256 // a non-exempt process this big counts as one suspect 138 var agg_min_act_mb: i64 = RG_MAGIC_1024 // never nominate a holder smaller than this (too small to matter) 139 let ex_off: *i64 = sys_mmap(RG_MAXROWS * 8) as *i64 140 var n_ex: i64 = 0 141 // RECYCLE rows: processes the operator declares RESTART-SAFE. Only these may be destroyed by the 142 // aggregate regime -- see rg_aggregate_level. Detection stays universal; destruction is opt-in. 143 let rec_off: *i64 = sys_mmap(RG_MAXROWS * 8) as *i64 144 var n_rec: i64 = 0 145 let cap_off: *i64 = sys_mmap(RG_MAXROWS * 8) as *i64 146 let cap_gb: *i64 = sys_mmap(RG_MAXROWS * 8) as *i64 147 var n_cap: i64 = 0 148 149 let conf: *u8 = sys_mmap(RG_CONF_CAP) 150 let cn: i64 = vw_read(confp, conf, RG_CONF_CAP - 1) 151 if cn <= 0 { g_puts("[resgov] INERT (no conf at " as *u8); g_puts(confp); g_puts(")\n" as *u8); return 0 } 152 let ep: *i64 = sys_mmap(16) as *i64 153 var ls: i64 = 0 154 while ls < cn { 155 var le: i64 = ls 156 var sc: i64 = 0 157 while sc == 0 { if le >= cn { sc = 1 } else { if conf[le] == (10 as u8) { sc = 1 } else { le = le + 1 } } } 158 if le > ls { if conf[ls] != (35 as u8) { 159 // key = first token, rest = args 160 let keyend: i64 = g_find_sp(conf, ls, le) 161 if keyend > ls { 162 var vstart: i64 = g_skip_sp(conf, keyend + 1, le) 163 if vstart < 0 { vstart = le } 164 conf[keyend] = 0 as u8 165 let key: *u8 = ((conf as i64) + ls) as *u8 166 if g_streq(key, "default_cap_gb" as *u8) == 1 { default_cap_gb = vw_num_at(conf, le, vstart, ep) } 167 if g_streq(key, "headroom_floor_permil" as *u8) == 1 { head_floor = vw_num_at(conf, le, vstart, ep) } 168 if g_streq(key, "swap_ceiling_permil" as *u8) == 1 { swap_ceil = vw_num_at(conf, le, vstart, ep) } 169 if g_streq(key, "eta_warn_s" as *u8) == 1 { eta_warn_s = vw_num_at(conf, le, vstart, ep) } 170 if g_streq(key, "eta_act_s" as *u8) == 1 { eta_act_s = vw_num_at(conf, le, vstart, ep) } 171 if g_streq(key, "cooldown_s" as *u8) == 1 { cooldown_s = vw_num_at(conf, le, vstart, ep) } 172 if g_streq(key, "agg_swap_ceiling_permil" as *u8) == 1 { agg_swap_ceil = vw_num_at(conf, le, vstart, ep) } 173 if g_streq(key, "agg_headroom_floor_permil" as *u8) == 1 { agg_head_floor = vw_num_at(conf, le, vstart, ep) } 174 if g_streq(key, "agg_suspect_floor" as *u8) == 1 { agg_suspect_floor = vw_num_at(conf, le, vstart, ep) } 175 if g_streq(key, "agg_suspect_mb" as *u8) == 1 { agg_suspect_mb = vw_num_at(conf, le, vstart, ep) } 176 if g_streq(key, "agg_min_act_mb" as *u8) == 1 { agg_min_act_mb = vw_num_at(conf, le, vstart, ep) } 177 // ***A NEEDLE TERMINATED AT END-OF-LINE SWALLOWS THE TRAILING COMMENT.*** This read 178 // conf[le]=0, so `exempt nx_hostctl # the supervisor itself: ...` produced the needle 179 // "nx_hostctl # the supervisor itself: ..." -- which can never match a cmdline, so the 180 // exemption was SILENTLY INERT. FOUR of five rows carried comments: nx_hostctl, 181 // nx_daemon_supervisor, nx_resgov and sites.elf -- THE EDGE THAT SERVES EVERY DOMAIN was 182 // one over-cap sweep away from being SIGTERMed by its own governor. Only the bare 183 // `exempt nx_hub_gw` row ever worked. FOUND 2026-08-06 because the new aggregate pass 184 // nominated pid 20547 = ./nx_hostctl supervise, a process it should never have been able 185 // to see. ***AN INSTRUMENT THAT REPORTS SOMETHING IMPOSSIBLE HAS FOUND A REAL BUG -- 186 // IN ITSELF OR IN WHAT IT READS. NEVER EXPLAIN IT AWAY.*** 187 // Terminate at the first blank instead, matching how `cap` already parses its needle. 188 if g_streq(key, "exempt" as *u8) == 1 { if n_ex < RG_MAXROWS { 189 var xend: i64 = g_find_sp(conf, vstart, le) 190 if xend <= vstart { xend = le } 191 if xend > le { xend = le } 192 conf[xend] = 0 as u8 193 ex_off[n_ex] = (conf as i64) + vstart; n_ex = n_ex + 1 } } 194 if g_streq(key, "recycle" as *u8) == 1 { if n_rec < RG_MAXROWS { 195 var rend: i64 = g_find_sp(conf, vstart, le) 196 if rend <= vstart { rend = le } 197 if rend > le { rend = le } 198 conf[rend] = 0 as u8 199 rec_off[n_rec] = (conf as i64) + vstart; n_rec = n_rec + 1 } } 200 if g_streq(key, "cap" as *u8) == 1 { 201 // cap <needle> <gb> 202 let nend: i64 = g_find_sp(conf, vstart, le) 203 if nend > vstart { 204 let gstart: i64 = g_skip_sp(conf, nend + 1, le) 205 if gstart > 0 { 206 let gb: i64 = vw_num_at(conf, le, gstart, ep) 207 if gb >= 1 { if n_cap < RG_MAXROWS { conf[nend] = 0 as u8; cap_off[n_cap] = (conf as i64) + vstart; cap_gb[n_cap] = gb; n_cap = n_cap + 1 } } 208 } 209 } 210 } 211 } 212 } } 213 ls = le + 1 214 } 215 216 // ---- system pressure (measured live, never assumed) ---- 217 let mi: *u8 = sys_mmap(RG_CONF_CAP) 218 let min_: i64 = vw_read("/proc/meminfo" as *u8, mi, RG_CONF_CAP - 1) 219 var headroom: i64 = 0 - 1 220 var swapper: i64 = 0 - 1 221 if min_ > 0 { 222 let mtot: i64 = rg_meminfo_kb(mi, min_, "MemTotal:" as *u8) 223 let mav: i64 = rg_meminfo_kb(mi, min_, "MemAvailable:" as *u8) 224 let stot: i64 = rg_meminfo_kb(mi, min_, "SwapTotal:" as *u8) 225 let sfree: i64 = rg_meminfo_kb(mi, min_, "SwapFree:" as *u8) 226 headroom = rg_headroom_permil(mav, mtot) 227 if stot > 0 { if sfree >= 0 { swapper = rg_swap_permil(stot - sfree, stot) } } 228 } 229 let pressured: i64 = rg_system_pressured(headroom, swapper, head_floor, swap_ceil) 230 231 // SWAP FLOW -- MEASURED AND EMITTED, DELIBERATELY NOT ACTED ON (2026-08-07). 232 // swap_permil above is OCCUPANCY, a STOCK. Measured live this morning it read 677 permil on a box 233 // doing 1 kB/s swap-in, 0 kB/s swap-out, 0 major faults/s, with the edge answering in 22 ms -- 234 // cold pages parked where they belong, which is swap doing its job, not scarcity. So `pressured=1` 235 // can be true on a completely healthy box, which is exactly what it reported. 236 // Pressure is a FLOW. These cumulative counters make the RATE derivable from any two consecutive 237 // runs (the sweep is on a fixed interval), at zero state cost. 238 // WHY THE PREDICATE IS UNTOUCHED: gate tooth T12 encodes occupancy-as-pressure deliberately, 239 // against the real 08-04 outage -- and the paging RATE during that incident was never recorded. 240 // Gating pressure on flow now would weaken a governor that prevented a real outage on the strength 241 // of an assumption about data nobody has. Collect the evidence first; let the next incident decide 242 // on numbers instead of on my reasoning about a past one. 243 let vm: *u8 = sys_mmap(RG_CONF_CAP) 244 let vmn: i64 = vw_read("/proc/vmstat" as *u8, vm, RG_CONF_CAP - 1) 245 var pswpin: i64 = 0 - 1 246 var pswpout: i64 = 0 - 1 247 if vmn > 0 { 248 pswpin = rg_meminfo_kb(vm, vmn, "pswpin " as *u8) 249 pswpout = rg_meminfo_kb(vm, vmn, "pswpout " as *u8) 250 } 251 252 // ---- prior samples (velocity memory) ---- 253 let st: *u8 = sys_mmap(RG_STATE_CAP) 254 let stn: i64 = vw_read(RG_STATE_PATH, st, RG_STATE_CAP - 1) 255 // parsed prior rows: key string offsets + footprint + epoch + last-action ts 256 let p_key: *i64 = sys_mmap(RG_MAXPROC * 8) as *i64 257 let p_kb: *i64 = sys_mmap(RG_MAXPROC * 8) as *i64 258 let p_t: *i64 = sys_mmap(RG_MAXPROC * 8) as *i64 259 let p_act: *i64 = sys_mmap(RG_MAXPROC * 8) as *i64 260 var n_prior: i64 = 0 261 if stn > 0 { 262 var s2: i64 = 0 263 while s2 < stn { 264 var e2: i64 = s2 265 var sc2: i64 = 0 266 while sc2 == 0 { if e2 >= stn { sc2 = 1 } else { if st[e2] == (10 as u8) { sc2 = 1 } else { e2 = e2 + 1 } } } 267 if e2 > s2 { if n_prior < RG_MAXPROC { 268 // key kb epoch act 269 let kend: i64 = g_find_sp(st, s2, e2) 270 if kend > s2 { 271 st[kend] = 0 as u8 272 p_key[n_prior] = (st as i64) + s2 273 var f: i64 = kend + 1 274 p_kb[n_prior] = vw_num_at(st, e2, f, ep) 275 f = ep[0] + 1 276 p_t[n_prior] = vw_num_at(st, e2, f, ep) 277 f = ep[0] + 1 278 let av: i64 = vw_num_at(st, e2, f, ep) 279 if av < 0 { p_act[n_prior] = 0 } else { p_act[n_prior] = av } 280 n_prior = n_prior + 1 281 } 282 } } 283 s2 = e2 + 1 284 } 285 } 286 287 let now: i64 = sys_now_realtime_sec() 288 let selfpid: i64 = vw_selfpid() 289 let fd: i64 = sys_openat_rd("/proc" as *u8) 290 if fd < 0 { g_puts("[resgov] cannot open /proc\n" as *u8); return 0 } 291 let dbuf: *u8 = sys_mmap(RG_DIR_CAP) 292 let clbuf: *u8 = sys_mmap(RG_CMD_CAP) 293 let out: *u8 = sys_mmap(RG_STATE_CAP) 294 var op: i64 = 0 295 var scanned: i64 = 0 296 var acted: i64 = 0 297 var warned: i64 = 0 298 var watched: i64 = 0 299 // AGGREGATE accumulators (largest NON-EXEMPT holder + suspect count), filled by the walk below. 300 var agg_worst_foot: i64 = 0 301 var agg_worst_pid: i64 = 0 302 var agg_worst_lastact: i64 = 0 303 var agg_worst_recyclable: i64 = 0 304 var agg_suspects: i64 = 0 305 var run: i64 = 1 306 while run == 1 { 307 let n: i64 = sys_getdents64(fd, dbuf, RG_DIR_CAP) 308 if n <= 0 { run = 0 } else { 309 var off: i64 = 0 310 while off < n { 311 let rec: *u8 = ((dbuf as i64 + off) as *u8) 312 let reclen: i64 = dirent_reclen(rec) 313 if reclen <= 0 { off = n } else { 314 let name: *u8 = dirent_name(rec) 315 if name[0] >= (48 as u8) { if name[0] <= (57 as u8) { 316 let pid: i64 = vw_num_at(name, g_slen(name), 0, ep) 317 if pid > 300 { if pid != selfpid { 318 let rss: i64 = vw_rss_kb_of(name) 319 if rss > 0 { 320 scanned = scanned + 1 321 let swp: i64 = vw_status_kb_of(name, "VmSwap:" as *u8) 322 let foot: i64 = rg_footprint_kb(rss, swp) 323 // cmdline for policy matching 324 var o3: i64 = g_app(clbuf, 0, "/proc/" as *u8) 325 o3 = g_app(clbuf, o3, name) 326 o3 = g_app(clbuf, o3, "/cmdline" as *u8) 327 clbuf[o3] = 0 as u8 328 let cmdpath: *u8 = sys_mmap(256) 329 var cp: i64 = g_app(cmdpath, 0, "/proc/" as *u8) 330 cp = g_app(cmdpath, cp, name) 331 cp = g_app(cmdpath, cp, "/cmdline" as *u8) 332 cmdpath[cp] = 0 as u8 333 let cln: i64 = vw_read(cmdpath, clbuf, RG_CMD_CAP - 1) 334 // policy: exemption wins, then explicit cap, then the DEFAULT (coverage inversion) 335 var exempt: i64 = 0 336 var recyclable: i64 = 0 337 var mycap: i64 = default_cap_gb 338 if cln > 0 { 339 var xi: i64 = 0 340 while xi < n_ex { 341 let nd: *u8 = ex_off[xi] as *u8 342 if vw_contains(clbuf, cln, nd, g_slen(nd)) == 1 { exempt = 1; xi = n_ex } 343 xi = xi + 1 344 } 345 var rj: i64 = 0 346 while rj < n_rec { 347 let nd3: *u8 = rec_off[rj] as *u8 348 if vw_contains(clbuf, cln, nd3, g_slen(nd3)) == 1 { recyclable = 1; rj = n_rec } 349 rj = rj + 1 350 } 351 var ci: i64 = 0 352 while ci < n_cap { 353 let nd2: *u8 = cap_off[ci] as *u8 354 if vw_contains(clbuf, cln, nd2, g_slen(nd2)) == 1 { mycap = cap_gb[ci]; ci = n_cap } 355 ci = ci + 1 356 } 357 } 358 // identity key = pid:starttime (pid reuse would poison the velocity) 359 let stt: i64 = g_starttime(name) 360 let keyb: *u8 = sys_mmap(64) 361 var kp: i64 = g_appn(keyb, 0, pid) 362 keyb[kp] = 58 as u8; kp = kp + 1 363 kp = g_appn(keyb, kp, stt) 364 keyb[kp] = 0 as u8 365 // find prior sample for this identity 366 var prev_kb: i64 = 0 367 var prev_t: i64 = 0 368 var last_act: i64 = 0 369 var pi: i64 = 0 370 while pi < n_prior { 371 if g_streq(p_key[pi] as *u8, keyb) == 1 { 372 prev_kb = p_kb[pi]; prev_t = p_t[pi]; last_act = p_act[pi]; pi = n_prior 373 } 374 pi = pi + 1 375 } 376 let vel: i64 = rg_velocity_kb_s(prev_kb, prev_t, foot, now) 377 var already_termed: i64 = 0 378 if last_act > 0 { if now - last_act < cooldown_s { already_termed = 1 } } 379 // AGGREGATE accumulation, on the SAME walk (no extra /proc traversal). 380 // NON-EXEMPT ONLY: an exemption is a promise here too, so the nominee can 381 // never be the supervisor, the edge, or the governor itself. 382 if exempt == 0 { 383 if foot >= agg_suspect_mb * RG_MAGIC_1024 { agg_suspects = agg_suspects + 1 } 384 if foot > agg_worst_foot { agg_worst_foot = foot; agg_worst_pid = pid; agg_worst_lastact = last_act; agg_worst_recyclable = recyclable } 385 } 386 let lvl: i64 = rg_level(exempt, mycap, rss, swp, vel, pressured, already_termed, eta_warn_s, eta_act_s) 387 var act_ts: i64 = last_act 388 // ★EXEMPT FROM ACTION IS NOT EXEMPT FROM SCRUTINY. rg_level returns OK for an 389 // exempt process, which would make the estate's biggest growers INVISIBLE in 390 // the log -- the blind spot this whole regime exists to end. Recompute the 391 // level it WOULD have had and report that, while still acting on nothing. 392 if exempt == 1 { 393 let shadow: i64 = rg_level(0, mycap, rss, swp, vel, pressured, 0, eta_warn_s, eta_act_s) 394 if shadow != RG_OK { 395 watched = watched + 1 396 g_puts("[resgov] EXEMPT-" as *u8); g_puts(rg_level_name(shadow)) 397 g_puts(" pid=" as *u8); g_putn(pid) 398 g_puts(" foot_mb=" as *u8); g_putn(foot / RG_MAGIC_1024) 399 g_puts(" cap_gb=" as *u8); g_putn(mycap) 400 g_puts(" vel_kb_s=" as *u8); g_putn(vel) 401 g_puts(" (action withheld by an auditable exemption; scrutiny continues)\n" as *u8) 402 } 403 } 404 if lvl != RG_OK { 405 g_puts("[resgov] " as *u8); g_puts(rg_level_name(lvl)) 406 g_puts(" pid=" as *u8); g_putn(pid) 407 g_puts(" foot_mb=" as *u8); g_putn(foot / RG_MAGIC_1024) 408 g_puts(" cap_gb=" as *u8); g_putn(mycap) 409 g_puts(" vel_kb_s=" as *u8); g_putn(vel) 410 let eta: i64 = rg_eta_s(foot, mycap * RG_MAGIC_1048576, vel) 411 g_puts(" eta_s=" as *u8); g_putn(eta) 412 g_puts(" headroom=" as *u8); g_putn(headroom) 413 g_puts(" swap=" as *u8); g_putn(swapper) 414 if lvl == RG_WATCH { watched = watched + 1; g_puts(" (over cap but system calm -- big is not harmful)\n" as *u8) } 415 if lvl == RG_WARN { warned = warned + 1; g_puts(" (forecast breach -- early signal, no action)\n" as *u8) } 416 if lvl == RG_RENICE { 417 if dry == 1 { g_puts(" DRY renice\n" as *u8) } else { 418 sys_setpriority_of(pid, 15) 419 acted = acted + 1 420 g_puts(" DEPRIORITISED (slowed while headroom remains)\n" as *u8) 421 } 422 } 423 if rg_is_destructive(lvl) == 1 { 424 if rg_cooldown_ok(last_act, now, cooldown_s) == 1 { 425 if dry == 1 { g_puts(" DRY " as *u8); g_puts(rg_level_name(lvl)); g_puts("\n" as *u8) } else { 426 if lvl == RG_TERM { nx_kill(pid, 15) } else { nx_kill(pid, 9) } 427 act_ts = now 428 acted = acted + 1 429 g_puts(" SIGNALLED (guard respawns fresh)\n" as *u8) 430 } 431 } else { g_puts(" HELD (cooldown)\n" as *u8) } 432 } 433 } 434 // persist this sample (velocity memory for the next run) 435 if op < RG_STATE_CAP - 128 { 436 op = g_app(out, op, keyb) 437 out[op] = 32 as u8; op = op + 1 438 op = g_appn(out, op, foot) 439 out[op] = 32 as u8; op = op + 1 440 op = g_appn(out, op, now) 441 out[op] = 32 as u8; op = op + 1 442 op = g_appn(out, op, act_ts) 443 out[op] = 10 as u8; op = op + 1 444 } 445 } 446 } } 447 } } 448 off = off + reclen 449 } 450 } 451 } 452 } 453 sys_close(fd) 454 455 // atomic-ish state write (whole file, one open) 456 if op > 0 { 457 let sfd: i64 = sys_openat_wr(RG_STATE_PATH, 420) 458 if sfd >= 0 { 459 var w: i64 = 0 460 while w < op { let k: i64 = sys_write(sfd, ((out as i64) + w) as *u8, op - w); if k <= 0 { w = op } else { w = w + k } } 461 sys_close(sfd) 462 } 463 } 464 465 // ---- AGGREGATE PASS: act on accumulated debt that NO per-process rule can ever name ---- 466 // Every acting branch of rg_level requires ONE process to be over ITS OWN cap. On 2026-08-06 the 467 // box sat at swap 822 permil with 14 leak suspects, each individually legal under the 6GB default; 468 // this governor scanned 504 processes, reported acted=0, was CORRECT every minute, and the live 469 // edge stalled until nishifamily.com/writer timed out for the operator. 470 // AGGREGATE COOLDOWN gets its OWN marker: the per-process state rows are keyed on pid:starttime, 471 // and a recycled nominee comes back with a NEW identity, so its own history can never throttle the 472 // next nomination. Without this the pass could walk down the table one process per minute. 473 let agg_ts_path: *u8 = "knowledge/status/resgov_agg.ts" as *u8 474 let aggb: *u8 = sys_mmap(64) 475 let aggn: i64 = vw_read(agg_ts_path, aggb, 63) 476 var agg_last: i64 = 0 477 if aggn > 0 { agg_last = vw_num_at(aggb, aggn, 0, ep) } 478 var agg_termed: i64 = 0 479 if agg_last > 0 { if now - agg_last < cooldown_s { agg_termed = 1 } } 480 var agg_lvl: i64 = RG_OK 481 if agg_termed == 0 { 482 agg_lvl = rg_aggregate_level(headroom, swapper, agg_head_floor, agg_swap_ceil, agg_suspects, agg_suspect_floor, agg_worst_foot, 0, agg_worst_recyclable, agg_min_act_mb * RG_MAGIC_1024, 0) 483 } 484 if agg_lvl != RG_OK { 485 g_puts("[resgov] AGGREGATE-" as *u8); g_puts(rg_level_name(agg_lvl)) 486 g_puts(" pid=" as *u8); g_putn(agg_worst_pid) 487 g_puts(" foot_mb=" as *u8); g_putn(agg_worst_foot / RG_MAGIC_1024) 488 g_puts(" suspects=" as *u8); g_putn(agg_suspects) 489 g_puts(" headroom=" as *u8); g_putn(headroom) 490 g_puts(" swap=" as *u8); g_putn(swapper) 491 g_puts(" (individually legal, collectively harmful -- the sum no per-process cap can name)" as *u8) 492 if rg_is_destructive(agg_lvl) == 0 { 493 watched = watched + 1 494 g_puts(" NO RESTART-SAFE NOMINEE -- named, never acted on. NEVER TERMINATE WHAT NOTHING WILL RESTART: add a `recycle <needle>` row for a daemon a guard revives, or fix the leak.\n" as *u8) 495 } else { if dry == 1 { g_puts(" DRY\n" as *u8) } else { 496 nx_kill(agg_worst_pid, 15) 497 acted = acted + 1 498 let afd: i64 = sys_openat_wr(agg_ts_path, 420) 499 if afd >= 0 { 500 let ab2: *u8 = sys_mmap(64) 501 var ao: i64 = g_appn(ab2, 0, now) 502 ab2[ao] = 10 as u8 503 sys_write(afd, ab2, ao + 1) 504 sys_close(afd) 505 } 506 g_puts(" SIGNALLED (guard respawns fresh)\n" as *u8) 507 } } 508 } 509 510 g_puts("[resgov] scanned=" as *u8); g_putn(scanned) 511 g_puts(" watch=" as *u8); g_putn(watched) 512 g_puts(" warn=" as *u8); g_putn(warned) 513 g_puts(" acted=" as *u8); g_putn(acted) 514 g_puts(" headroom_permil=" as *u8); g_putn(headroom) 515 g_puts(" swap_permil=" as *u8); g_putn(swapper) 516 g_puts(" pswpin=" as *u8); g_putn(pswpin) 517 g_puts(" pswpout=" as *u8); g_putn(pswpout) 518 g_puts(" pressured=" as *u8); g_putn(pressured) 519 g_puts(" default_cap_gb=" as *u8); g_putn(default_cap_gb) 520 g_puts(" exempt_rows=" as *u8); g_putn(n_ex) 521 g_puts(" cap_rows=" as *u8); g_putn(n_cap) 522 g_puts("\n" as *u8) 523 return 0 524}