code wiki / _hdl_build / nx_resmon_gate.nx
nx_resmon_gate.nx source
↩ module page · 91 lines · 4787 B
1// nx_resmon_gate.nx -- referee for nx_resmon's two policy predicates (debt seq1005).
2//
3// It gates the SAME functions the organ runs (nx_resmon_lib.nx), not a reimplementation, and it needs
4// no /proc and no fixtures on disk -- every input is synthetic, so the result is deterministic.
5//
6// T2 AND T4 ARE THE TEETH THAT MATTER:
7// T2 is the NON-VACUITY control: with thresholds set unreachably high, measurements that trip RED
8// under real thresholds must return GREEN. If T2 ever fails, T1 is passing for free.
9// T4 pins the EXACT false positive this instrument made on its first live run: a process with a huge
10// RESERVED VmData but almost nothing committed reported a worst case of 40.6 GiB on a 36.9 GB box.
11// A leaker is judged on rss+swap, never on reserved address space. That regression cannot return
12// silently now.
13//
14// license_tier: ORIGINAL expect_exit: 0
15import "nx_gate.nx"
16import "nx_resmon_lib.nx"
17
18func rg_row(name: *u8, ok: i64) -> i64 {
19 gw(" " as *u8); gw(name)
20 if ok == 1 { gw(" PASS\n" as *u8) } else { gw(" FAIL\n" as *u8) }
21 return 0
22}
23
24func main() -> i64 {
25 // ---- T1: the verdict ladder moves with the thresholds (data-driven, not hardcoded) ----
26 // same measurement (swap 934 permil, the real reading from the incident), three threshold sets.
27 var t1: i64 = 0
28 let g: i64 = rm_verdict(934, 500, 0, 940, 990, 250, 100, 3, 6) // below amber -> GREEN
29 let a: i64 = rm_verdict(934, 500, 0, 700, 990, 250, 100, 3, 6) // over amber only -> AMBER
30 let r: i64 = rm_verdict(934, 500, 0, 700, 850, 250, 100, 3, 6) // over red -> RED
31 if g == 0 { if a == 1 { if r == 2 { t1 = 1 } } }
32
33 // ---- T2 NON-VACUITY: unreachable thresholds must NOT flip the verdict ----
34 // 934 permil swap, 23 leakers -- catastrophic in reality -- but no threshold can be crossed.
35 var t2: i64 = 0
36 if rm_verdict(934, 500, 23, 9999, 9999, 0 - 1, 0 - 1, 9999, 9999) == 0 { t2 = 1 }
37
38 // ---- T3: EACH axis can raise the verdict on its own ----
39 var t3: i64 = 0
40 let byswap: i64 = rm_verdict(900, 900, 0, 700, 850, 250, 100, 3, 6) // swap alone -> RED
41 let byavail: i64 = rm_verdict(0, 50, 0, 700, 850, 250, 100, 3, 6) // avail alone -> RED
42 let byleak: i64 = rm_verdict(0, 900, 7, 700, 850, 250, 100, 3, 6) // leakers alone -> RED
43 if byswap == 2 { if byavail == 2 { if byleak == 2 { t3 = 1 } } }
44
45 // ---- T4: RESERVED-but-untouched is NOT a leak; COMMITTED is ----
46 // (a) the real first-run false positive: 40.6 GiB reserved, ~1 MiB committed -> NOT a leaker.
47 // (b) a genuine leaker: monotonic address space AND 3.02 GiB actually committed -> IS a leaker.
48 // (c) monotonic but small -> below the floor -> NOT a leaker.
49 // (d) big and committed but VmSize != VmPeak (it has shrunk) -> NOT the fingerprint.
50 var t4: i64 = 0
51 let fp: i64 = rm_is_leaker(42581844, 42581844, 1024, 0, 262144)
52 let real: i64 = rm_is_leaker(6903268, 6903268, 161228, 2111640, 262144)
53 let small: i64 = rm_is_leaker(2668, 2668, 600, 0, 262144)
54 let shrunk: i64 = rm_is_leaker(1000000, 4000000, 500000, 500000, 262144)
55 if fp == 0 { if real == 1 { if small == 0 { if shrunk == 0 { t4 = 1 } } } }
56
57 // ---- T5: rm_field parses both shapes it must serve, and reports absence honestly ----
58 var t5: i64 = 0
59 let b: *u8 = sys_mmap(512)
60 let s: *u8 = "Name:\tnx_hub_gw.elf\nVmPeak:\t 6903268 kB\nVmSize:\t 6903268 kB\nVmRSS:\t 161228 kB\nleak-min-kb = 262144\n" as *u8
61 var i: i64 = 0
62 while s[i] != (0 as u8) { b[i] = s[i]; i = i + 1 }
63 b[i] = 0 as u8
64 let vpk: i64 = rm_field(b, i, "VmPeak:" as *u8)
65 let vrs: i64 = rm_field(b, i, "VmRSS:" as *u8)
66 let cfg: i64 = rm_field(b, i, "leak-min-kb" as *u8)
67 let miss: i64 = rm_field(b, i, "VmSwap:" as *u8)
68 if vpk == 6903268 { if vrs == 161228 { if cfg == 262144 { if miss == 0 - 1 { t5 = 1 } } } }
69
70 var passes: i64 = 0
71 if t1 == 1 { passes = passes + 1 }
72 if t2 == 1 { passes = passes + 1 }
73 if t3 == 1 { passes = passes + 1 }
74 if t4 == 1 { passes = passes + 1 }
75 if t5 == 1 { passes = passes + 1 }
76 var green: i64 = 0
77 if passes == 5 { green = 1 }
78
79 gw("=== nx_resmon_gate -- pressure + leak-census predicates (seq1005) ===\n" as *u8)
80 rg_row("T1-verdict-ladder-is-threshold-driven " as *u8, t1)
81 rg_row("T2-NONVACUITY-unreachable-stays-GREEN " as *u8, t2)
82 rg_row("T3-each-axis-can-raise-alone " as *u8, t3)
83 rg_row("T4-reserved-not-leak-committed-is " as *u8, t4)
84 rg_row("T5-field-parse-status-conf-and-absent " as *u8, t5)
85 gw("verdict=" as *u8)
86 if green == 1 { gw("GREEN" as *u8) } else { gw("RED" as *u8) }
87 gw(" passes=" as *u8); gn(passes); gw("/5\n" as *u8)
88
89 if green == 1 { return 0 }
90 return 1
91}