code wiki / _hdl_build / nx_polite_loop.nx

nx_polite_loop.nx source

↩ module page · 113 lines · 6465 B

1// nx_polite_loop.nx -- the team's RESOURCE-AWARE autonomous backlog loop (operator: "it needs to be resource 2// aware so when im playing a game or doing other things it should pause or run minimally based on the hardware 3// resources"). Composes the team's OWN governor: nx_sysload reads real /proc (load, ncpu, free mem) and 4// nx_resource_governor (POLITE) decides -> PAUSE when over the load ceiling (you're gaming/busy), run MINIMAL 5// (fewer tasks) under partial load, full when idle; always nice; memory-floor admission; AIMD backoff. A 6// /tmp/nishi_pause flag forces PAUSE (escape hatch + the hook a Windows GPU/foreground sensor can touch, since 7// WSL /proc can't see a Windows game directly). When allowed, it runs a backlog TICK by ITERATIVE reach (no 8// recursion -> dodges the regalloc segfault): the Builder composes each task or RAISES A HAND -> escalation 9// queue. license_tier: ORIGINAL 10import "nx_sysload.nx" 11import "nx_resource_governor.nx" 12import "nx_syscalls.nx" 13 14func pl_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 15func pl_putn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1}; sys_write(1,b,k); return 0 } 16 17// POLITE policy params (DATA, not magic ints buried in logic). 18const PL_RESERVE: i64 = 1 // always leave 1 core for the foreground (your game/app) 19const PL_CEIL_MILLI: i64 = 800 // per-core ceiling: load > 0.8*ncpu -> PAUSE (you're busy) 20const PL_MEM_FLOOR_MB: i64 = 1024 // don't start work under 1 GB free 21const PL_BASE_BACKOFF: i64 = 1000 // ms, grows AIMD with overload 22 23const NTYPE: i64 = 8 24const NP: i64 = 8 25const NTASK: i64 = 6 26 27// iterative type-reachability (no recursion): can available primitives chain tin -> tout? 28func pl_reach(tin: i64, tout: i64, pin: *i64, pout: *i64, pav: *i64) -> i64 { 29 let reach: *i64 = sys_mmap(8*NTYPE) as *i64 30 var i: i64 = 0; while i < NTYPE { reach[i]=0; i=i+1 } 31 reach[tin] = 1 32 var changed: i64 = 1 33 while changed == 1 { 34 changed = 0 35 var p: i64 = 0 36 while p < NP { 37 if pav[p] == 1 { if reach[pin[p]] == 1 { if reach[pout[p]] == 0 { reach[pout[p]] = 1; changed = 1 } } } 38 p = p + 1 39 } 40 } 41 return reach[tout] 42} 43 44func main() -> i64 { 45 // ---- 1. SENSE the real host (the team's sovereign /proc readers) ---- 46 let load: i64 = sl_loadavg_milli() 47 let ncpu: i64 = sl_ncpu() 48 let freemb: i64 = sl_freemem_mb() 49 50 pl_puts("=== POLITE LOOP -- resource-aware tick ===\n host: ncpu=" as *u8); pl_putn(ncpu) 51 pl_puts(" load_milli=" as *u8); pl_putn(load); pl_puts(" freeMB=" as *u8); pl_putn(freemb); pl_puts("\n" as *u8) 52 53 // ---- 2. DECIDE via the governor (POLITE) + the pause-flag escape hatch ---- 54 let pfbox: *i64 = sys_mmap(16) as *i64 55 let pf: *u8 = sys_read_file("/tmp/nishi_pause" as *u8, pfbox) 56 let forced_pause: i64 = pf as i64 // flag present -> non-zero -> force pause (e.g. gaming sensor) 57 58 let backoff: i64 = rg_should_backoff(RG_POLITE, ncpu, load, PL_CEIL_MILLI) 59 let memok: i64 = rg_memory_ok(freemb, PL_MEM_FLOOR_MB) 60 let budget: i64 = rg_worker_budget(RG_POLITE, ncpu, load, PL_RESERVE, PL_CEIL_MILLI) 61 let nice: i64 = rg_nice(RG_POLITE) 62 63 if forced_pause != 0 { 64 pl_puts(" DECISION: PAUSE (/tmp/nishi_pause set -- foreground/gaming sensor). 0 work this tick.\n" as *u8) 65 sys_exit(0); return 0 66 } 67 if backoff == 1 { 68 let ms: i64 = rg_backoff_ms(RG_POLITE, ncpu, load, PL_CEIL_MILLI, PL_BASE_BACKOFF) 69 pl_puts(" DECISION: PAUSE (load over ceiling = you are busy). nice=" as *u8); pl_putn(nice) 70 pl_puts(" AIMD-backoff_ms=" as *u8); pl_putn(ms); pl_puts(". 0 work this tick.\n" as *u8) 71 sys_exit(0); return 0 72 } 73 if memok == 0 { 74 pl_puts(" DECISION: PAUSE (free memory below floor). 0 work this tick.\n" as *u8) 75 sys_exit(0); return 0 76 } 77 pl_puts(" DECISION: RUN " as *u8) 78 if budget <= 1 { pl_puts("MINIMAL" as *u8) } else { pl_puts("up to " as *u8); pl_putn(budget) } 79 pl_puts(" task(s), nice=" as *u8); pl_putn(nice); pl_puts(" (spare capacity).\n" as *u8) 80 81 // ---- 3. WORK a TICK within budget: the Builder composes-or-raises-hand (iterative, no recursion) ---- 82 let pin: *i64=sys_mmap(8*NP) as *i64; let pout:*i64=sys_mmap(8*NP) as *i64; let pav:*i64=sys_mmap(8*NP) as *i64 83 pin[0]=1;pout[0]=2;pav[0]=1; pin[1]=0;pout[1]=2;pav[1]=1; pin[2]=2;pout[2]=3;pav[2]=1; pin[3]=2;pout[3]=4;pav[3]=1 84 pin[4]=2;pout[4]=4;pav[4]=1; pin[5]=2;pout[5]=5;pav[5]=0; pin[6]=2;pout[6]=7;pav[6]=0; pin[7]=7;pout[7]=6;pav[7]=0 85 let tname:*i64=sys_mmap(8*NTASK) as *i64; let tin:*i64=sys_mmap(8*NTASK) as *i64; let tout:*i64=sys_mmap(8*NTASK) as *i64 86 tname[0]="secure-token" as *u8 as i64; tin[0]=0; tout[0]=3 87 tname[1]="csrf-token" as *u8 as i64; tin[1]=0; tout[1]=3 88 tname[2]="intake-form-parse" as *u8 as i64; tin[2]=2; tout[2]=4 89 tname[3]="content-load" as *u8 as i64; tin[3]=1; tout[3]=4 90 tname[4]="password-hash" as *u8 as i64; tin[4]=2; tout[4]=5 91 tname[5]="html-sanitize" as *u8 as i64; tin[5]=2; tout[5]=6 92 93 let esc: i64 = sys_openat_append("/tmp/nishi_escalations.log" as *u8, 0x1a4) 94 var built: i64=0; var hands: i64=0; var did: i64=0; var i: i64=0 95 while i < NTASK { 96 if did >= budget { i = NTASK } // honor the worker budget = run MINIMAL under partial load 97 else { 98 let ok: i64 = pl_reach(tin[i], tout[i], pin, pout, pav) 99 pl_puts(" " as *u8); pl_puts(tname[i] as *u8) 100 if ok == 1 { pl_puts(" -> COMPOSED -> BANKED\n" as *u8); built=built+1 } 101 else { 102 pl_puts(" -> RAISED HAND -> escalated\n" as *u8); hands=hands+1 103 var n: i64=0; let s: *u8=tname[i] as *u8; while s[n]!=(0 as u8){n=n+1} 104 sys_write(esc, "ESCALATE polite-loop task=" as *u8, 26); sys_write(esc, s, n); sys_write(esc, "\n" as *u8, 1) 105 } 106 did = did + 1; i = i + 1 107 } 108 } 109 sys_close(esc) 110 pl_puts(" TICK: banked=" as *u8); pl_putn(built); pl_puts(" raised-hands=" as *u8); pl_putn(hands) 111 pl_puts(" (within budget). Re-tick respects live resources each beat.\n" as *u8) 112 sys_exit(0); return 0 113}