code wiki / _hdl_build / nx_video_swarm_probe.nx
nx_video_swarm_probe.nx source
↩ module page · 198 lines · 11089 B
1// nx_video_swarm_probe.nx -- the TRUE fan-out load measurement (the 8-party hardware question). The
2// ping-pong QoE probe measures per-flow cadence; THIS one measures what an 8-person family call actually
3// does to the edge: 8 REAL TLS-1.3+WSS peers in ONE room (= the relay's per-room cap), peer P0 paces
4// 6KB room-framed video frames at the target cadence, and the relay broadcast fans each frame to the 7
5// receivers -- 7x write amplification through sites.elf pumps + nx_signaling_v2, all TLS. Every receiver
6// drains every frame before the next send (backpressure-safe). Read order ROTATES each frame so no
7// receiver is systematically read last. Per-frame MAX across receivers = "everyone sees it by".
8// TWO PASSES (2026-07-03, operator: "we want way more than a 20 fps setup"): 20fps (ledger continuity
9// with the existing vqoe_swarm8_* baselines) THEN 60fps (the new ladder top rung, vqoe_swarm8f60_*) --
10// the 60fps pass is the honest transport proof for the lifted ladder.
11// HONESTY BOUNDS (printed): 1-sender/7-receiver from ONE Texas LAN host; a full 8x8 mesh is 8x the send
12// load (extrapolate, don't claim); Minsk legs are task #25. Ledger-reported (return-and-report).
13// expect_exit: 0 = BOTH passes SOUND license_tier: ORIGINAL
14import "nx_ws_client_session.nx"
15import "nx_video_client_wasm.nx"
16import "nx_metric_ledger.nx"
17const SW_MAGIC_16384: i64 = 16384
18const SW_MAGIC_8192: i64 = 8192
19const SW_MAGIC_150000: i64 = 150000
20const SW_MAGIC_500000: i64 = 500000
21const SW_MAGIC_10000000: i64 = 10000000
22const SW_MAGIC_4194304: i64 = 4194304
23const SW_MAGIC_50000: i64 = 50000
24const SW_MAGIC_6000: i64 = 6000
25const SW_MAGIC_16666: i64 = 16666
26
27const SW_PEERS: i64 = 8 // room cap: 1 sender + 7 receivers
28const SW_FRAMES: i64 = 200
29const SW_PAY: i64 = 6000 // realistic ladder-frame payload
30
31func sw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
32func sn(v: i64) -> i64 {
33 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
34 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}
35 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
36
37func swsortn(a: *i64, n: i64) -> i64 {
38 var i: i64 = 1
39 while i < n {
40 let v: i64 = a[i]
41 var j: i64 = i - 1
42 var g: i64 = 1
43 while g == 1 { if j >= 0 { if a[j] > v { a[j+1] = a[j]; j = j - 1 } else { g = 0 } } else { g = 0 } }
44 a[j+1] = v
45 i = i + 1
46 }
47 return 0
48}
49
50// one full swarm pass at intv_us. Returns 1 = SOUND, 0 = a control failed, -1 = connect/send failure.
51// url is per-pass: pass-1's 8 closed peers LINGER in the relay's room until its reaper notices, so a
52// same-room rejoin at the 8-cap hits a closed pump -> our write = SIGPIPE (exit 141, seen live
53// 2026-07-03). Distinct rooms isolate the passes. (Relay-side fast-reap on rejoin = a follow-up.)
54func swarm_pass(store: *TrustStore, url: *u8, intv_us: i64, pay_len: i64, tag: *u8, mn_p95: *u8, mn_worst: *u8, mn_fps: *u8, mn_late: *u8, mn_del: *u8) -> i64 {
55 let sess: *i64 = sys_mmap(SW_PEERS * 8) as *i64
56 let fds: *i64 = sys_mmap(SW_PEERS * 8) as *i64
57 let sb: *i64 = sys_mmap(16) as *i64
58 let fb: *i64 = sys_mmap(16) as *i64
59 let t_c0: i64 = sys_now_us()
60 var p: i64 = 0
61 while p < SW_PEERS {
62 if wscs_connect(store, url, 0x11 * (p + 3), sb, fb) != 1 { sw(" peer connect FAILED p=" as *u8); sn(p); sw("\n" as *u8); return 0 - 1 }
63 sess[p] = sb[0]
64 fds[p] = fb[0]
65 p = p + 1
66 }
67 let t_c1: i64 = sys_now_us()
68 sw(" == " as *u8); sw(tag); sw(" == (8 peers connected in " as *u8); sn((t_c1 - t_c0) / 1000); sw("ms)\n" as *u8)
69
70 let lat: *i64 = sys_mmap(7 * SW_FRAMES * 8 + 64) as *i64
71 let got: *u8 = sys_mmap(SW_MAGIC_16384)
72 let winfo: *i64 = sys_mmap(64) as *i64
73 let id8: *u8 = sys_mmap(16)
74 var z: i64 = 0
75 while z < 8 { id8[z] = (74 + z) as u8; z = z + 1 }
76 let payload: *u8 = sys_mmap(pay_len + 64)
77 var pz: i64 = 0
78 while pz < pay_len { payload[pz] = ((pz * 11 + 5) & 0xff) as u8; pz = pz + 1 }
79 let wirebuf: *u8 = sys_mmap(pay_len + 256)
80 let framemax: *i64 = sys_mmap(SW_FRAMES * 8 + 64) as *i64
81
82 let sender: *Tls13ClientSession = sess[0] as *Tls13ClientSession
83 var received: i64 = 0
84 var expected: i64 = 0
85 var late150: i64 = 0
86 var gap500: i64 = 0
87 var mismatch: i64 = 0
88 let t_start: i64 = sys_now_us()
89 var i: i64 = 0
90 var aborted: i64 = 0
91 while i < SW_FRAMES {
92 let wlen: i64 = vc_wire_pack(wirebuf, 0x56, id8, i, payload, pay_len)
93 let frame: *u8 = sys_mmap(SW_MAGIC_8192)
94 let mask: i64 = 0x5A6B7C8D
95 let hn: i64 = ws_build_header(frame, SW_MAGIC_8192, 1, WS_OP_BINARY, 1, mask, wlen)
96 var c: i64 = 0
97 while c < wlen { frame[hn + c] = wirebuf[c]; c = c + 1 }
98 ws_apply_mask(frame, hn, wlen, mask)
99 let t0: i64 = sys_now_us()
100 if wscs_send(sender, fds[0], frame, hn + wlen) != 0 { sw(" sender send FAILED at frame " as *u8); sn(i); sw("\n" as *u8); aborted = 1; i = SW_FRAMES } else {
101 var fmax: i64 = 0
102 var j: i64 = 0
103 while j < 7 {
104 let rx: i64 = 1 + ((i + j) % 7)
105 expected = expected + 1
106 let sx: *Tls13ClientSession = sess[rx] as *Tls13ClientSession
107 let glen: i64 = wscs_ws_recv(sx, fds[rx], got, SW_MAGIC_16384)
108 let t1: i64 = sys_now_us()
109 if glen > 0 {
110 if vc_wire_parse(got, glen, winfo) == 0 {
111 if winfo[1] != i { mismatch = mismatch + 1 }
112 let l: i64 = t1 - t0
113 let li: *i64 = ((lat as i64) + ((rx - 1) * SW_FRAMES + i) * 8) as *i64
114 li[0] = l
115 received = received + 1
116 if l > SW_MAGIC_150000 { late150 = late150 + 1 }
117 if l > SW_MAGIC_500000 { gap500 = gap500 + 1 }
118 if l > fmax { fmax = l }
119 } else { mismatch = mismatch + 1 }
120 }
121 j = j + 1
122 }
123 framemax[i] = fmax
124 let t_next: i64 = t_start + (i + 1) * intv_us
125 let now: i64 = sys_now_us()
126 if now < t_next { sys_sleep_ms((t_next - now) / 1000) }
127 i = i + 1
128 }
129 }
130 let t_end: i64 = sys_now_us()
131 p = 0
132 while p < SW_PEERS { sys_close(fds[p]); p = p + 1 }
133 if aborted == 1 { return 0 - 1 }
134
135 sw(" per-receiver path latency (rotated read order):\n" as *u8)
136 let tmp: *i64 = sys_mmap(SW_FRAMES * 8 + 64) as *i64
137 var rxi: i64 = 0
138 var worst_p95: i64 = 0
139 while rxi < 7 {
140 var k: i64 = 0
141 while k < SW_FRAMES { let li2: *i64 = ((lat as i64) + (rxi * SW_FRAMES + k) * 8) as *i64; tmp[k] = li2[0]; k = k + 1 }
142 swsortn(tmp, SW_FRAMES)
143 let p50: i64 = tmp[SW_FRAMES / 2]
144 let p95: i64 = tmp[(SW_FRAMES * 95) / 100]
145 if p95 > worst_p95 { worst_p95 = p95 }
146 sw(" R" as *u8); sn(rxi + 1); sw(": p50=" as *u8); sn(p50 / 1000); sw("." as *u8); sn((p50 % 1000) / 100); sw("ms p95=" as *u8); sn(p95 / 1000); sw("." as *u8); sn((p95 % 1000) / 100); sw("ms\n" as *u8)
147 rxi = rxi + 1
148 }
149 swsortn(framemax, SW_FRAMES)
150 let em50: i64 = framemax[SW_FRAMES / 2]
151 let em95: i64 = framemax[(SW_FRAMES * 95) / 100]
152 let emmax: i64 = framemax[SW_FRAMES - 1]
153 let dur_us: i64 = t_end - t_start
154 var fps_x10: i64 = 0
155 if dur_us > 0 { fps_x10 = (SW_FRAMES * SW_MAGIC_10000000) / dur_us }
156 sw(" ALL-RECEIVERS-HAVE-IT-BY: p50=" as *u8); sn(em50 / 1000); sw("." as *u8); sn((em50 % 1000) / 100); sw("ms p95=" as *u8); sn(em95 / 1000); sw("." as *u8); sn((em95 % 1000) / 100); sw("ms max=" as *u8); sn(emmax / 1000); sw("ms\n" as *u8)
157 sw(" delivered=" as *u8); sn(received); sw("/" as *u8); sn(expected)
158 sw(" sender_fps=" as *u8); sn(fps_x10 / 10); sw("." as *u8); sn(fps_x10 % 10)
159 sw(" late>150ms=" as *u8); sn(late150); sw(" gaps>500ms=" as *u8); sn(gap500); sw(" seq_mismatch=" as *u8); sn(mismatch); sw("\n" as *u8)
160
161 sw(" -- return-and-report --\n" as *u8)
162 ml_report(mn_p95, em95, 0 - 1, "us" as *u8, "nx_video_swarm_probe" as *u8)
163 ml_report(mn_worst, worst_p95, 0 - 1, "us" as *u8, "nx_video_swarm_probe" as *u8)
164 ml_report(mn_fps, fps_x10, 1, "fps_x10" as *u8, "nx_video_swarm_probe" as *u8)
165 ml_report(mn_late, late150, 0 - 1, "frames" as *u8, "nx_video_swarm_probe" as *u8)
166 ml_report(mn_del, received, 1, "frames" as *u8, "nx_video_swarm_probe" as *u8)
167
168 var okmeas: i64 = 1
169 if mismatch != 0 { okmeas = 0 }
170 if received * 10 < expected * 9 { okmeas = 0 }
171 return okmeas
172}
173
174func main() -> i64 {
175 sw("=== nx_video_swarm_probe: 8-peer room fan-out through the LIVE edge (20fps pass + 60fps pass) ===\n" as *u8)
176 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt\x00" as *u8, 512, SW_MAGIC_4194304)
177 if r <= 0 { sw("trust store load failed\n" as *u8); return 1 }
178 let store: *TrustStore = r as *TrustStore
179 let url20: *u8 = "https://nishifamily.com/signal/nxswarm\x00" as *u8
180 let url60: *u8 = "https://nishifamily.com/signal/nxswarm60\x00" as *u8
181 let url1k: *u8 = "https://nishifamily.com/signal/nxswarm1k\x00" as *u8
182 let ok20: i64 = swarm_pass(store, url20, SW_MAGIC_50000, SW_MAGIC_6000, "PASS-1: 20fps x 6KB (baseline continuity)" as *u8,
183 "vqoe_swarm8_everyone_p95_us" as *u8, "vqoe_swarm8_worstrx_p95_us" as *u8,
184 "vqoe_swarm8_fps_x10" as *u8, "vqoe_swarm8_late150" as *u8, "vqoe_swarm8_delivered" as *u8)
185 let ok60: i64 = swarm_pass(store, url60, SW_MAGIC_16666, SW_MAGIC_6000, "PASS-2: 60fps x 6KB (the NEW ladder top rung)" as *u8,
186 "vqoe_swarm8f60_everyone_p95_us" as *u8, "vqoe_swarm8f60_worstrx_p95_us" as *u8,
187 "vqoe_swarm8f60_fps_x10" as *u8, "vqoe_swarm8f60_late150" as *u8, "vqoe_swarm8f60_delivered" as *u8)
188 // DIAGNOSTIC pass: same 60fps rate, 1/6th the bytes. If the ~40ms cycle persists -> fixed
189 // per-cycle cost (scheduling/serialization); if it collapses -> bytes/CPU-bound (TLS encrypt).
190 let okd: i64 = swarm_pass(store, url1k, SW_MAGIC_16666, 1000, "PASS-3 DIAG: 60fps x 1KB (bytes-vs-cycle discriminator)" as *u8,
191 "vqoe_swarm8f60_1k_p95_us" as *u8, "vqoe_swarm8f60_1k_worstrx_us" as *u8,
192 "vqoe_swarm8f60_1k_fps_x10" as *u8, "vqoe_swarm8f60_1k_late150" as *u8, "vqoe_swarm8f60_1k_delivered" as *u8)
193 sw(" fan-out load: 60fps x 6KB x 7 receivers = ~2.5MB/s TLS through the edge (8x8 mesh = 8x send load -- extrapolation, not a claim)\n" as *u8)
194 sw(" HONESTY BOUNDS: Texas-LAN 1-sender/7-receiver fan-out; full-mesh + Minsk legs are separate rungs.\n" as *u8)
195 if ok20 == 1 { if ok60 == 1 { sw("SWARM-PROBE verdict=SOUND -- 20fps AND 60fps fan-out numbers are LIVE-MEASURED\n" as *u8); return 0 } }
196 sw("SWARM-PROBE verdict=UNSOUND -- a pass failed its controls (see above); do not cite\n" as *u8)
197 return 1
198}