code wiki / _hdl_build / nx_room_bwe.nx
nx_room_bwe.nx source
↩ module page · 95 lines · 5017 B
1// nx_room_bwe.nx -- X-ROOM (overseas-grade): sovereign DETERMINISTIC delay-gradient
2// BANDWIDTH ESTIMATOR / CONGESTION CONTROLLER (Google-Congestion-Control style). The
3// estimator nx_room_abr rides on: ABR picks a quality layer, but only as well as the
4// bandwidth estimate it is given. On a variable-bandwidth intercontinental link, a
5// DELAY-based detector senses a building queue from RISING one-way delay and backs off
6// BEFORE any packet is lost -- whereas a loss-based controller only reacts AFTER the
7// overseas queue has already overflowed (by which point latency has spiked).
8//
9// MODEL: per packet-group, m[i] = inter-group one-way-delay gradient (ms; >0 = queue
10// growing). Overuse detector: acc accumulates positive gradients, drains on flat/neg;
11// acc>thresh => OVERUSE (signal rate DECREASE) detected at the delay rise. A separate
12// loss flag marks where a loss-based controller would FIRST react.
13//
14// EXCEED axis (honest) vs loss-based congestion control: PROACTIVE -- the delay detector
15// fires strictly EARLIER than the loss event on the same trace (measured lead), and is
16// deterministic + audit-replayable + sovereign. HONEST SCOPE: a simplified accumulator
17// overuse detector (the GCC trendline+adaptive-threshold + rate AIMD = deeper rung).
18//
19// main() is the SELF-VALIDATING GATE. Evidence -> knowledge/status/room_bwe.log.
20// license_tier: ORIGINAL
21import "nx_syscalls.nx"
22const BWE_MAGIC_100000: i64 = 100000
23
24const BWE_LOG: *u8 = "knowledge/status/room_bwe.log"
25
26func bw(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 }
27func bwn(fd: i64, v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64=v; if m<0 {m=0-m; sys_write(fd,"-" as *u8,1)}; let t: *u8 = sys_mmap(28); var k: i64=0; if m==0 {t[0]=48;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 {bb[i]=t[k-1-i]; i=i+1}; sys_write(fd, bb, k); return 0 }
28
29// run the gradient trace. out[0]=first OVERUSE group (delay-based, -1 if none),
30// out[1]=first LOSS group (-1 if none).
31func bwe_run(m: *i64, lossf: *i64, N: i64, thresh: i64, drain: i64, out: *i64) -> i64 {
32 var acc: i64 = 0
33 var first_over: i64 = 0 - 1
34 var first_loss: i64 = 0 - 1
35 var i: i64 = 0
36 while i < N {
37 if m[i] > 0 { acc = acc + m[i] }
38 else { acc = acc - drain; if acc < 0 { acc = 0 } }
39 if acc > thresh { if first_over < 0 { first_over = i } }
40 if lossf[i] == 1 { if first_loss < 0 { first_loss = i } }
41 i = i + 1
42 }
43 out[0] = first_over
44 out[1] = first_loss
45 return 0
46}
47
48func main() -> i64 {
49 let N: i64 = 20
50 let m: *i64 = sys_mmap(8 * N) as *i64
51 let lossf: *i64 = sys_mmap(8 * N) as *i64
52 var i: i64 = 0
53 while i < N { m[i] = 0; lossf[i] = 0; i = i + 1 }
54 // congestion episode: one-way delay RISES from group 8 (queue building), the queue
55 // finally overflows -> a LOSS at group 14, then drains (negative gradients).
56 m[8]=5; m[9]=5; m[10]=5; m[11]=5; m[12]=5; m[13]=5; m[14]=5
57 lossf[14]=1
58 m[15]=0-5; m[16]=0-5; m[17]=0-5; m[18]=0-5; m[19]=0-5
59 let out: *i64 = sys_mmap(8 * 2) as *i64
60 var ok: i64 = 1
61
62 let THRESH: i64 = 12
63 let DRAIN: i64 = 3
64 bwe_run(m, lossf, N, THRESH, DRAIN, out)
65 let delay_detect: i64 = out[0]
66 let loss_detect: i64 = out[1]
67 var lead: i64 = 0
68 if delay_detect >= 0 { lead = loss_detect - delay_detect }
69
70 if delay_detect < 0 { ok = 0 } // the delay detector MUST fire
71 if delay_detect < 8 { ok = 0 } // no false overuse before the rise (calm phase clean)
72 if loss_detect != 14 { ok = 0 }
73 if delay_detect >= loss_detect { ok = 0 } // PROACTIVE: reacts strictly BEFORE the loss
74 if lead <= 0 { ok = 0 } // positive lead time = the overseas exceed
75
76 // tamper / neg-control: break the overuse threshold (huge) -> detector never fires,
77 // so it degrades to loss-only reaction (proves the delay detector is load-bearing).
78 bwe_run(m, lossf, N, BWE_MAGIC_100000, DRAIN, out)
79 let delay_tamper: i64 = out[0]
80 if delay_tamper >= 0 { ok = 0 } // with a broken threshold it must NOT detect early
81
82 bw(1, "ROOMBWEGATE delay_detect=" as *u8); bwn(1, delay_detect); bw(1, " loss_detect=" as *u8); bwn(1, loss_detect)
83 bw(1, " lead_groups=" as *u8); bwn(1, lead); bw(1, " tamper_detect=" as *u8); bwn(1, delay_tamper)
84 if ok == 1 { bw(1, " verdict=GREEN\n" as *u8) } else { bw(1, " verdict=RED\n" as *u8) }
85
86 let lf: i64 = sys_openat_append(BWE_LOG, 420)
87 if lf >= 0 {
88 bw(lf, "ROOMBWEGATE delay_detect=" as *u8); bwn(lf, delay_detect); bw(lf, " loss_detect=" as *u8); bwn(lf, loss_detect)
89 bw(lf, " lead_groups=" as *u8); bwn(lf, lead); bw(lf, " tamper_detect=" as *u8); bwn(lf, delay_tamper)
90 if ok == 1 { bw(lf, " verdict=GREEN\n" as *u8) } else { bw(lf, " verdict=RED\n" as *u8) }
91 sys_close(lf)
92 }
93 if ok == 1 { sys_exit(0) } else { sys_exit(1) }
94 return 0
95}