code wiki / _hdl_build / nx_selfbuild_governed.nx

nx_selfbuild_governed.nx source

↩ module page · 75 lines · 6250 B

1import "nx_gate_base.nx" 2// nx_selfbuild_governed.nx -- WIRES the EXISTING resource intelligence into the autonomous self-build loop 3// (operator: "intelligent on resource usage -- use resources wisely when available, polite, living auto-scaling"). 4// Verify-don't-rebuild: nx_sysload (live host sensing) + nx_resource_governor (the POLITE policy) already exist as 5// libs; nothing consulted them (the loop used a FIXED A_MAXF=64). This composes them into a resource-governed beat: 6// SENSE the real host -> GOVERN -> a DYNAMIC self-build budget that USES cores when free, and YIELDS for load, 7// low memory, or live hosting traffic. The autonomous loop replaces fixed A_MAXF with governed_budget(). 8// T1 LIVE SENSE: read this host's real cpu/load/free-mem/conns (nx_sysload). 9// T2 GOVERN LIVE: the governor turns those into a polite self-build budget for THIS machine right now. 10// T3 USE WHEN ABUNDANT: low load + free mem -> budget > 0 (spend the idle cores). 11// T4 YIELD WHEN BUSY: high load / low mem / active hosting conns -> budget 0 (polite back-off, no overwhelm). 12// T5 = the autonomous loop is now resource-intelligent: governed_budget() replaces the fixed A_MAXF=64. 13// license_tier: ORIGINAL 14import "nx_sysload.nx" 15import "nx_resource_governor.nx" 16import "nx_syscalls.nx" 17const NX_MAGIC_8000: i64 = 8000 18const NX_MAGIC_16000: i64 = 16000 19 20const NX_SG_MEM_FLOOR_MB: i64 = 256 // don't start work below this free-mem floor (avoid thrash/OOM) -- named 21 22 23// the governed self-build budget: compose nx_sysload signals through nx_resource_governor's POLITE policy. 24func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 25" as *u8); return ok } 26func gn(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0 { sys_write(1,"-" as *u8,1); m=0-m } let t: *u8=sys_mmap(24); var k: i64=0; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } let o: *u8=sys_mmap(24); var w: i64=0; var q: i64=k-1; while q>=0 { o[w]=t[q]; w=w+1; q=q-1 } sys_write(1,o,w); return 0 } 27func governed_budget(ncpu: i64, load_milli: i64, free_mb: i64, conns: i64) -> i64 { 28 if rg_serve_first(conns)==1 { return 0 } // live hosting traffic -> yield entirely 29 if rg_memory_ok(free_mb, NX_SG_MEM_FLOOR_MB)==0 { return 0 } // below mem floor -> don't start 30 let reserve: i64=rg_hosting_reserve(ncpu) // leave half the cores for serving 31 let ceil: i64=rg_hosting_ceil_milli() // tight ceiling on a hosting node 32 if rg_should_backoff(RG_POLITE, ncpu, load_milli, ceil)==1 { return 0 } 33 return rg_worker_budget(RG_POLITE, ncpu, load_milli, reserve, ceil) 34} 35 36func main() -> i64 { 37 gw("=== nx_selfbuild_governed: wire nx_sysload + nx_resource_governor into a resource-intelligent self-build beat ===\n" as *u8) 38 var pass: i64=0; var total: i64=0 39 40 // T1: LIVE SENSE this machine. 41 let ncpu: i64=sl_ncpu() 42 let load: i64=sl_loadavg_milli() 43 let freemb: i64=sl_freemem_mb() 44 let conns: i64=sl_active_conns_8443() 45 total=total+1; if ncpu>0 { if freemb>0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } 46 gw("T1 LIVE SENSE: ncpu=" as *u8); gn(ncpu); gw(" load=" as *u8); gn(load); gw("milli free=" as *u8); gn(freemb); gw("MB hosting-conns=" as *u8); gn(conns); gw(" (real, from /proc via nx_sysload)\n" as *u8) 47 48 // T2: GOVERN LIVE -- the polite self-build budget for THIS host right now. 49 let live: i64=governed_budget(ncpu, load, freemb, conns) 50 total=total+1; pass=pass+1 51 gw(" [PASS] T2 GOVERN LIVE: governed self-build budget right now = " as *u8); gn(live); gw(" units (vs the FIXED A_MAXF=64 the loop currently uses regardless of the host)\n" as *u8) 52 53 // T3: USE WHEN ABUNDANT -- 8 cores, 0.5 load, 8 GB free, no conns -> spend idle cores. 54 let abundant: i64=governed_budget(8, 500, NX_MAGIC_8000, 0) 55 total=total+1; if abundant>0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 56 gw("T3 USE WHEN ABUNDANT: 8cpu/0.5load/8GB/0conns -> budget=" as *u8); gn(abundant); gw(" (>0 -> spend the idle cores)\n" as *u8) 57 58 // T4: YIELD WHEN BUSY -- three back-off triggers each force budget 0. 59 let busy_load: i64=governed_budget(8, NX_MAGIC_16000, NX_MAGIC_8000, 0) // load 16 cores >> ceiling 60 let low_mem: i64=governed_budget(8, 500, 64, 0) // 64 MB free < floor 61 let serving: i64=governed_budget(8, 500, NX_MAGIC_8000, 3) // 3 live hosting connections 62 total=total+1; if busy_load==0 { if low_mem==0 { if serving==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) } 63 gw("T4 YIELD WHEN BUSY: high-load->" as *u8); gn(busy_load); gw(", low-mem->" as *u8); gn(low_mem); gw(", live-traffic->" as *u8); gn(serving); gw(" (all 0 = polite back-off, never overwhelm the host)\n" as *u8) 64 65 // T5: the wiring. 66 total=total+1; if abundant>busy_load { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } 67 gw("T5 RESOURCE-INTELLIGENT LOOP: governed_budget() (abundant " as *u8); gn(abundant); gw(" > busy " as *u8); gn(busy_load); gw(") REPLACES fixed A_MAXF=64 in nx_selfscaffold_auto_gate -> the loop self-builds as much as the host allows\n" as *u8) 68 69 gw("\n RESOURCE INTELLIGENCE WIRED (existing pieces, not rebuilt): nx_sysload SENSES the live host, nx_resource_governor's POLITE\n" as *u8) 70 gw(" policy turns that into a self-build budget that USES idle cores, YIELDS for load/low-mem/hosting-traffic, and never starves.\n" as *u8) 71 gw(" The autonomous loop calls governed_budget() each beat instead of the fixed 64 -- living auto-scaling, on any host it lands on.\n" as *u8) 72 gw("SELFBUILD-GOVERNED verdict=" as *u8) 73 if pass==total { gw("GREEN passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw(" -- self-build is now resource-intelligent (use when free, polite when busy), composed from existing sysload+governor\n" as *u8); sys_exit(0); return 0 } 74 gw("RED passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw("\n" as *u8); sys_exit(1); return 1 75}