nx_loadceil.nx source
↩ module page · 89 lines · 4522 B
1// nx_loadceil.nx -- CLI for the build-admission load-ceiling calibrator (LV3, loadgov.plan, 2026-08-24).
2// Thin main; the ruler is nx_loadceil_lib.lc_recommend so nx_loadceil_gate composes it in-process.
3//
4// nx_loadceil [report] [logpath] [confpath]
5// Reads a resmon.log, splits beats by state=GREEN, prints the GREEN load1_centi distribution and RECOMMENDS
6// max_centiload = (green load at loadceil-quantile-permil) / BA_HARD_FACTOR. Refuses below
7// loadceil-min-green-samples. Arm by writing the value to build_admit.conf as max_centiload= (a conf value
8// re-armed without a rebuild). exit 0 RECOMMENDED / 2 unreadable / 3 insufficient-history.
9// license_tier: ORIGINAL
10
11import "nx_loadceil_lib.nx"
12
13func lc_w(fd: i64, s: *u8) -> i64 { let n: i64 = lc_slen(s); if n > 0 { sys_write(fd, s, n) } return 0 }
14func lc_out(s: *u8) -> i64 { return lc_w(1, s) }
15func lc_num(v0: i64) -> i64 {
16 let b: *u8 = sys_mmap(32)
17 var v: i64 = v0
18 var neg: i64 = 0
19 if v < 0 { neg = 1; v = 0 - v }
20 var i: i64 = 31
21 b[i] = 0 as u8
22 if v == 0 { i = i - 1; b[i] = 48 as u8 }
23 while v > 0 { i = i - 1; b[i] = ((v % 10) + 48) as u8; v = v / 10 }
24 if neg == 1 { i = i - 1; b[i] = 45 as u8 }
25 var n: i64 = 0
26 while b[i+n] != (0 as u8) { n = n + 1 }
27 sys_write(1, b + i, n)
28 sys_munmap(b, 32)
29 return 0
30}
31
32func main(argc: i64, argv: *i64) -> i64 {
33 var logp: *u8 = "knowledge/status/resmon.log" as *u8
34 var confp: *u8 = "knowledge/status/loadceil.conf" as *u8
35 // one verb, `report`; skip it if present, then any positional is logpath then confpath
36 var a: i64 = 1
37 if argc >= 2 { let op: *u8 = argv[1] as *u8; if op[0] == (114 as u8) { a = 2 } }
38 if argc > a { logp = argv[a] as *u8; a = a + 1 }
39 if argc > a { confp = argv[a] as *u8 }
40
41 let cb: *u8 = sys_mmap(LC_CONFCAP)
42 let cn0: i64 = lc_read(confp, cb, LC_CONFCAP)
43 var cn: i64 = cn0
44 if cn < 0 { cn = 0 }
45 // p99 of GREEN load: the load the box exceeds in only 1% of its HEALTHY beats. 200 min samples ~= most
46 // of a day at the 300s beat -- enough that a ceiling is history, not a coincidence. Both are conf rows.
47 let q: i64 = lc_conf_num(cb, cn, "loadceil-quantile-permil=" as *u8, 990)
48 let minn: i64 = lc_conf_num(cb, cn, "loadceil-min-green-samples=" as *u8, 200)
49
50 let buf: *u8 = sys_mmap(LC_LOGCAP)
51 let n: i64 = lc_read(logp, buf, LC_LOGCAP)
52 let truncated: i64 = n >= LC_LOGCAP
53 let out: *i64 = sys_mmap(LC_O_N * 8) as *i64
54 let v: i64 = lc_recommend(buf, n, q, minn, out)
55
56 if v == LC_UNREADABLE { lc_out("NX-LOADCEIL REFUSED log-unreadable path=" as *u8); lc_out(logp); lc_out("\n" as *u8); return 2 }
57
58 var complete: i64 = 1
59 if truncated == 1 { complete = 0 }
60 if out[LC_O_CAPPED] == 1 { complete = 0 }
61
62 lc_out("NX-LOADCEIL log=" as *u8); lc_out(logp)
63 lc_out(" beats=" as *u8); lc_num(out[LC_O_TOTAL])
64 lc_out(" green=" as *u8); lc_num(out[LC_O_GREEN])
65 lc_out(" missing_load=" as *u8); lc_num(out[LC_O_MISSING])
66 lc_out(" coverage_complete=" as *u8); lc_num(complete); lc_out("\n" as *u8)
67 if out[LC_O_GREEN] > 0 {
68 lc_out(" green load1_centi: min=" as *u8); lc_num(out[LC_O_MIN])
69 lc_out(" p50=" as *u8); lc_num(out[LC_O_P50])
70 lc_out(" p90=" as *u8); lc_num(out[LC_O_P90])
71 lc_out(" p99=" as *u8); lc_num(out[LC_O_P99])
72 lc_out(" max=" as *u8); lc_num(out[LC_O_MAX]); lc_out("\n" as *u8)
73 }
74 if v == LC_INSUFFICIENT {
75 lc_out(" verdict=INSUFFICIENT-HISTORY green=" as *u8); lc_num(out[LC_O_GREEN])
76 lc_out(" need>=" as *u8); lc_num(minn)
77 lc_out(" -- the load ceiling stays DISABLED (a ceiling from too few healthy beats is a permanently-red detector)\n" as *u8)
78 return 3
79 }
80 lc_out(" storm_trigger=" as *u8); lc_num(out[LC_O_TRIGGER])
81 lc_out(" (green load at q=" as *u8); lc_num(q); lc_out("permil) hard_factor=" as *u8); lc_num(LC_HARD_FACTOR)
82 lc_out("\n RECOMMEND max_centiload=" as *u8); lc_num(out[LC_O_REC])
83 lc_out(" -- ba_verdict QUEUEs when load1 > " as *u8); lc_num(out[LC_O_REC] * LC_HARD_FACTOR)
84 lc_out(" (2x), only above the top " as *u8); lc_num(1000 - q); lc_out("permil of healthy load. Write it to build_admit.conf as max_centiload= to arm.\n" as *u8)
85 lc_out("NX-LOADCEIL verdict=RECOMMENDED sample=" as *u8); lc_num(out[LC_O_GREEN])
86 if complete == 0 { lc_out(" (coverage partial: a bound was reached, so the sample is a FLOOR -- re-run with a larger cap)" as *u8) }
87 lc_out("\n" as *u8)
88 return 0
89}