code wiki / _hdl_build / nx_poolgov_gate.nx
nx_poolgov_gate.nx source
↩ module page · 112 lines · 6806 B
1// nx_poolgov_gate.nx -- proves runtime/nx_poolgov.nx, the copy that is ACTUALLY WIRED INTO PRODUCTION.
2//
3// WHY THIS EXISTS SEPARATELY FROM nx_poolwidth_gate: the pw_* twin is 18/18 GREEN, but nx_torrent_get
4// imports the pg_* copy, which differs in exactly two places that were NEVER gated -- the INLINED dirent64
5// accessors (nx_proc_ctl lives only in _hdl_build, unreachable from the primitive layer) and the INLINED
6// loadavg parse. Promoting a live BitTorrent client on 'the other copy passed' is the deploy-blind rule.
7// T17/T18 are the whole reason this file exists: they cover exactly the deltas.
8// license_tier: ORIGINAL expect_exit: 0 No hw writes (Rule 26).
9import "nx_poolgov.nx"
10import "nx_gate_verdict.nx"
11
12const PGG_BUDGET: i64 = 32
13const PGG_INSTANCES: i64 = 9
14const PGG_TODAY: i64 = 32
15const PGG_TODAY_AGG: i64 = 288
16const PGG_QUIET: i64 = 400
17const PGG_MEASURED: i64 = 1172
18const PGG_2X: i64 = 1600
19const PGG_CEIL: i64 = 800
20const PGG_WMIN: i64 = 2
21const PGG_WMAX: i64 = 32
22const PGG_DBUF: i64 = 128
23const PGG_SANE_LOAD: i64 = 100000
24
25func pgg_eq(a: i64, b: i64) -> i64 { if a == b { return 1 } return 0 }
26func pgg_ge(a: i64, b: i64) -> i64 { if a >= b { return 1 } return 0 }
27
28func main() -> i64 {
29 let ctr: *i64 = gv_ctr()
30 gv_head("nx_poolgov_gate -- the PRODUCTION copy (pg_*), incl. the two inlined deltas" as *u8)
31
32 gv_check("T1 budget 32 across the 9 measured instances = 3 slots each" as *u8,
33 pgg_eq(pg_width(PGG_BUDGET, PGG_INSTANCES, PGG_QUIET, PGG_CEIL, PGG_WMIN, PGG_WMAX), 3), ctr)
34 gv_check("T2 TODAY's per-instance policy = 32 x 9 = 288 concurrent slots" as *u8,
35 pgg_eq(pg_aggregate(PGG_TODAY, PGG_INSTANCES), PGG_TODAY_AGG), ctr)
36 gv_check("T3 the governed aggregate 3 x 9 = 27 fits the 32 budget" as *u8,
37 pgg_eq(pg_aggregate(3, PGG_INSTANCES), 27), ctr)
38 gv_check("T4 a LONE instance still gets the full budget (no gratuitous throttling)" as *u8,
39 pgg_eq(pg_width(PGG_BUDGET, 1, PGG_QUIET, PGG_CEIL, PGG_WMIN, PGG_WMAX), PGG_BUDGET), ctr)
40 gv_check("T5 at 2x the ceiling width halves proportionally (wmin=1 so the squeeze is visible)" as *u8,
41 pgg_eq(pg_width(PGG_BUDGET, PGG_INSTANCES, PGG_2X, PGG_CEIL, 1, PGG_WMAX), 1), ctr)
42 gv_check("T6 at the REAL measured load 11.72 the 9-way split squeezes to 2" as *u8,
43 pgg_eq(pg_width(PGG_BUDGET, PGG_INSTANCES, PGG_MEASURED, PGG_CEIL, PGG_WMIN, PGG_WMAX), 2), ctr)
44 gv_bite("T7 load ABOVE the ceiling squeezes; BELOW it does not" as *u8,
45 pgg_eq(pg_width(PGG_BUDGET, 1, PGG_2X, PGG_CEIL, PGG_WMIN, PGG_WMAX), PGG_BUDGET / 2),
46 pgg_eq(pg_width(PGG_BUDGET, 1, PGG_QUIET, PGG_CEIL, PGG_WMIN, PGG_WMAX), PGG_BUDGET / 2), ctr)
47 gv_bite("T8 an UNREADABLE load yields wmin -- not knowing the pressure is not permission to grab" as *u8,
48 pgg_eq(pg_width(PGG_BUDGET, 1, 0 - 1, PGG_CEIL, PGG_WMIN, PGG_WMAX), PGG_WMIN),
49 pgg_eq(pg_width(PGG_BUDGET, 1, PGG_QUIET, PGG_CEIL, PGG_WMIN, PGG_WMAX), PGG_WMIN), ctr)
50 gv_bite("T9 n_instances=0 is treated as 1, never a divide-by-zero" as *u8,
51 pgg_eq(pg_width(PGG_BUDGET, 0, PGG_QUIET, PGG_CEIL, PGG_WMIN, PGG_WMAX), PGG_BUDGET),
52 pgg_eq(pg_width(PGG_BUDGET, PGG_INSTANCES, PGG_QUIET, PGG_CEIL, PGG_WMIN, PGG_WMAX), PGG_BUDGET), ctr)
53 gv_check("T10 clamped to wmax when the budget would exceed it" as *u8,
54 pgg_eq(pg_width(1000, 1, PGG_QUIET, PGG_CEIL, PGG_WMIN, PGG_WMAX), PGG_WMAX), ctr)
55 gv_check("T11 the wmin FLOOR keeps a pool usable -- a 0-width pool is a dead feature" as *u8,
56 pgg_eq(pg_width(1, 100, PGG_QUIET, PGG_CEIL, PGG_WMIN, PGG_WMAX), PGG_WMIN), ctr)
57 gv_bite("T12 REFUSES an inverted clamp (wmax < wmin) instead of returning nonsense" as *u8,
58 pgg_eq(pg_width(PGG_BUDGET, 1, PGG_QUIET, PGG_CEIL, 10, 2), 0 - 1),
59 pgg_eq(pg_width(PGG_BUDGET, 1, PGG_QUIET, PGG_CEIL, 2, 10), 0 - 1), ctr)
60
61 // ---- hardware derivation ----
62 let cpus: i64 = nx_hw_cpu_count()
63 gv_check("T13 nx_hw_cpu_count reports a REAL count (>=2), not the historical silent-1 failure" as *u8,
64 pgg_ge(cpus, 2), ctr)
65 gv_check("T14 budget is DERIVED from this machine (cpus x 4), never a literal" as *u8,
66 pgg_eq(pg_budget_from_hw(4, 8), cpus * 4), ctr)
67 gv_bite("T15 a nonsense slots_per_cpu falls back to the floor instead of zeroing the pool" as *u8,
68 pgg_eq(pg_budget_from_hw(0, 8), 8),
69 pgg_eq(pg_budget_from_hw(4, 8), 8), ctr)
70 gv_bite("T16 pg_budget_from_hw REFUSES a nonsense floor" as *u8,
71 pgg_eq(pg_budget_from_hw(4, 0), 0 - 1),
72 pgg_eq(pg_budget_from_hw(4, 8), 0 - 1), ctr)
73
74 // ---- DELTA 1: the INLINED dirent64 accessors (the reason this gate exists) ----
75 // linux_dirent64: d_ino(8) d_off(8) d_reclen(u16 @16) d_type(u8 @18) d_name(@19).
76 // A wrong offset here silently miscounts EVERY process, which would silently mis-size the pool.
77 let db: *u8 = sys_mmap(PGG_DBUF)
78 var z: i64 = 0
79 while z < PGG_DBUF { db[z] = 0 as u8; z = z + 1 }
80 db[16] = 40 as u8
81 db[17] = 1 as u8
82 db[18] = 8 as u8
83 db[19] = 55 as u8
84 db[20] = 56 as u8
85 db[21] = 0 as u8
86 gv_check("T17a pg_dreclen decodes the little-endian u16 at offset 16 (296)" as *u8,
87 pgg_eq(pg_dreclen(db), 296), ctr)
88 var t17b: i64 = 0
89 let nmp: *u8 = pg_dname(db)
90 if nmp[0] == (55 as u8) { if nmp[1] == (56 as u8) { if nmp[2] == (0 as u8) { t17b = 1 } } }
91 gv_check("T17b pg_dname points at offset 19 and reads the name bytes" as *u8, t17b, ctr)
92
93 // ---- DELTA 2: the INLINED /proc/loadavg parse ----
94 // Cannot be fed a synthetic fixture (it opens the real file), so assert it PARSED SOMETHING SANE:
95 // -1 would mean unreadable/garbage, and an absurd value would mean the whole/frac split is wrong.
96 let lc: i64 = pg_load_centi()
97 var t18: i64 = 0
98 if lc >= 0 { if lc < PGG_SANE_LOAD { t18 = 1 } }
99 gv_check("T18 pg_load_centi parses the LIVE /proc/loadavg into a sane centi-load" as *u8, t18, ctr)
100
101 // ---- the live end-to-end value the downloader will actually use ----
102 let livew: i64 = pg_width(pg_budget_from_hw(4, 8), pg_count_procs("nx_torrent_get" as *u8), lc, PGG_CEIL, PGG_WMIN, PGG_WMAX)
103 var t19: i64 = 0
104 if livew >= PGG_WMIN { if livew <= PGG_WMAX { t19 = 1 } }
105 gv_check("T19 the LIVE derived MAXP lands inside [wmin,wmax] on this machine" as *u8, t19, ctr)
106 gv_check("T20 pg_count_procs never returns 0 (a caller may always divide by it)" as *u8,
107 pgg_ge(pg_count_procs("nx_torrent_get" as *u8), 1), ctr)
108
109 let rc: i64 = gv_verdict("POOLGOV-GATE" as *u8, ctr, "the PRODUCTION pg_* copy proven, incl. inlined dirent + inlined loadavg" as *u8)
110 sys_exit(rc)
111 return rc
112}