code wiki / _hdl_build / nx_resource_prove.nx

nx_resource_prove.nx source

↩ module page · 92 lines · 5968 B

1// nx_resource_prove.nx -- PROVE-NOT-ASSERT that the polite resource governor is REAL: read THIS host live 2// (nx_sysload over /proc) and show the governor's actual decision (nx_resource_governor), then demonstrate the 3// "sensor -> supercomputer, any hardware" scaling by computing the polite budget for hypothetical core counts 4// at the SAME live load. Read-only (no side effects, no work fired). Composes the shipped organs (Cardinal 15). 5// 6// Operator: "resource polite -- run in low-resource periods, step back when high; intelligent SOTA management 7// on any hardware dynamically, in conjunction with other parallel systems." This proves that capability EXISTS 8// and WORKS on the real machine, rather than asserting it. license_tier: ORIGINAL expect_exit: 0 9import "nx_syscalls.nx" 10import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 11import "nx_sysload.nx" 12import "nx_resource_governor.nx" 13 14func rp_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 15// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 16// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 17// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 18// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 19func rp_putn(v: i64) -> i64 { nxi_out(v); return 0 } 20 21// print the polite decision for a given (real or hypothetical) core count at the LIVE load. 22func rp_decide(label: *u8, ncpu: i64, load_milli: i64, free_mb: i64) -> i64 { 23 let reserve: i64 = rg_hw_class_reserve(ncpu) 24 let budget: i64 = rg_hw_budget(RG_POLITE, ncpu, load_milli, 800) 25 let backoff: i64 = rg_should_backoff(RG_POLITE, ncpu, load_milli, 800) 26 let memok: i64 = rg_memory_ok(free_mb, 512) 27 rp_puts(" " as *u8); rp_puts(label) 28 rp_puts(": ncpu=" as *u8); rp_putn(ncpu) 29 rp_puts(" reserve=" as *u8); rp_putn(reserve) 30 rp_puts(" -> polite_worker_budget=" as *u8); rp_putn(budget) 31 if backoff == 1 { rp_puts(" [BACK OFF: over load ceiling]" as *u8) } else { rp_puts(" [OK to run]" as *u8) } 32 if memok == 0 { rp_puts(" [MEM LOW: admission denied]" as *u8) } 33 rp_puts("\n" as *u8) 34 return budget 35} 36 37func main() -> i64 { 38 rp_puts("=== nx_resource_prove -- the polite governor, reading THIS host LIVE (prove-not-assert) ===\n" as *u8) 39 40 // --- read the REAL host --- 41 let load: i64 = sl_loadavg_milli() 42 let ncpu: i64 = sl_ncpu() 43 let freemb: i64 = sl_freemem_mb() 44 let conns: i64 = sl_active_conns_8443() 45 46 var sensors_ok: i64 = 1 47 if load < 0 { sensors_ok = 0 } 48 if freemb < 0 { sensors_ok = 0 } 49 50 rp_puts(" LIVE HOST STATE (/proc):\n" as *u8) 51 rp_puts(" load(1min) = " as *u8); rp_putn(load); rp_puts(" milli-cores (" as *u8); rp_putn(load/1000); rp_puts(".xx cores busy)\n" as *u8) 52 rp_puts(" ncpu = " as *u8); rp_putn(ncpu); rp_puts("\n" as *u8) 53 rp_puts(" free mem = " as *u8); rp_putn(freemb); rp_puts(" MB\n" as *u8) 54 rp_puts(" live :8443 connections = " as *u8); rp_putn(conns); rp_puts(" (serve-first: " as *u8) 55 if rg_serve_first(conns) == 1 { rp_puts("YIELD to serving)" as *u8) } else { rp_puts("clear)" as *u8) } 56 rp_puts("\n" as *u8) 57 58 // --- the governor's decision for THIS host, right now --- 59 rp_puts("\n GOVERNOR DECISION (POLITE mode, this host):\n" as *u8) 60 let this_budget: i64 = rp_decide("THIS-HOST", ncpu, load, freemb) 61 rp_puts(" nice level = " as *u8); rp_putn(rg_nice(RG_POLITE)); rp_puts(" (polite workers yield to foreground)\n" as *u8) 62 rp_puts(" AIMD backoff sleep if over ceiling = " as *u8); rp_putn(rg_backoff_ms(RG_POLITE, ncpu, load, 800, 1000)); rp_puts(" ms\n" as *u8) 63 64 // --- SENSOR -> SUPERCOMPUTER: same live load, different substrate classes (any hardware) --- 65 rp_puts("\n ANY-HARDWARE SCALING (same live load " as *u8); rp_putn(load); rp_puts(" milli, hw-class-aware reserve):\n" as *u8) 66 rp_decide("1-core SENSOR " as *u8, 1, load, freemb) 67 rp_decide("2-core edge " as *u8, 2, load, freemb) 68 rp_decide("8-core workstn " as *u8, 8, load, freemb) 69 rp_decide("32-core server " as *u8, 32, load, freemb) 70 rp_decide("128-core SUPER " as *u8, 128, load, freemb) 71 72 // --- CRASH-GATES override (operator-chosen full-throttle) --- 73 rp_puts("\n CRASH-GATES override (operator full-throttle, ignores politeness):\n" as *u8) 74 rp_puts(" budget = " as *u8); rp_putn(rg_hw_budget(RG_CRASH_GATES, ncpu, load, 800)); rp_puts(" (all cores) nice=" as *u8); rp_putn(rg_nice(RG_CRASH_GATES)); rp_puts(" backoff=" as *u8); rp_putn(rg_should_backoff(RG_CRASH_GATES, ncpu, load, 800)); rp_puts("\n" as *u8) 75 76 rp_puts("\n ---- VERDICT ----\n" as *u8) 77 if sensors_ok == 0 { 78 rp_puts(" SENSOR READ FAILED (/proc unavailable) -> governor cannot sense the host. INVALID.\n" as *u8) 79 rp_puts("NX-RESOURCE-PROVE: BROKEN\n" as *u8); sys_exit(1); return 1 80 } 81 // prove the scaling law holds: a 128-core super must get a >= budget than a 1-core sensor at the same load 82 let super_b: i64 = rg_hw_budget(RG_POLITE, 128, load, 800) 83 let sensor_b: i64 = rg_hw_budget(RG_POLITE, 1, load, 800) 84 if super_b < sensor_b { 85 rp_puts(" SCALING BROKEN: supercomputer budget < sensor budget -> the any-hardware law failed.\n" as *u8) 86 rp_puts("NX-RESOURCE-PROVE: BROKEN\n" as *u8); sys_exit(1); return 1 87 } 88 rp_puts(" PROVEN: governor read the REAL host (load/ncpu/mem/conns from /proc), decided a polite budget,\n" as *u8) 89 rp_puts(" scales sensor->supercomputer (super budget " as *u8); rp_putn(super_b); rp_puts(" >= sensor " as *u8); rp_putn(sensor_b); rp_puts("), yields to live serving + memory admission.\n" as *u8) 90 rp_puts("NX-RESOURCE-PROVE GREEN: the polite governor is REAL on this host (compose it into every loop/beat).\n" as *u8) 91 sys_exit(0); return 0 92}