code wiki / _hdl_build / nx_ws_exceed_git_live_gate.nx
nx_ws_exceed_git_live_gate.nx source
↩ module page · 146 lines · 9161 B
1import "nx_gate_base.nx"
2// nx_ws_exceed_git_live_gate.nx -- SCLASS-NEUTRAL-01: the WMS-vs-git durability exceed measured against
3// LIVE git 2.43 (not a documented reference column). 12 workers x 12 writes = 144 truly-concurrent writes
4// to (a) a real git repo [git commit --allow-empty, contending on index.lock + ref-lock] and (b) the WMS
5// locked seg-store [ws_put_locked]. MEASURES how many of 144 concurrent git writes git REFUSES under
6// contention (no caller retry) vs how many WMS completes (0 lost). NEG-CONTROL: the UNLOCKED WMS path
7// (ws_put_p) over the same 144 LOSES updates -> proves WMS's 0-lost is the lock doing real work.
8// The WMS lanes MIRROR nx_ws_cas_gate EXACTLY (flat epoch-stamped /tmp prefix; ss_open ONCE + ss_hget per
9// key) -- that is the proven concurrent-WMS harness. Running git here MEASURES the incumbent; Nishi needs
10// no git. HONEST: git's fail-fast on index.lock is deliberate anti-corruption design; the exceed is precise
11// -- WMS reaches the SAME safety (0 lost) AND auto-completes all 144 via bounded-wait flock (no caller retry).
12// REUSE: ws_put_locked/ws_put_p (nx_ws_cas), ss_open/ss_hget (nx_seg_store), fork/execve/wait4 (gate_sweep).
13// license_tier: ORIGINAL expect_exit: 0
14import "nx_ws_cas.nx"
15import "nx_workstream_store.nx"
16import "nx_seg_store.nx"
17import "nx_syscalls.nx"
18
19const GH_GIT: *u8 = "/usr/bin/git"
20const GH_N: i64 = 12 // concurrent workers
21const GH_K: i64 = 12 // writes per worker -> 144 total each lane (mirrors nx_ws_cas_gate)
22
23func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
24" as *u8); return ok }
25func gn(v0: i64) -> i64 { var v: i64=v0; if v<0 { sys_write(1,"-" as *u8,1); v=0-v } let t: *u8=sys_mmap(28); var k: i64=0; if v==0{t[0]=48 as u8;k=1} while v>0{t[k]=(48+(v%10)) as u8;v=v/10;k=k+1} let b: *u8=sys_mmap(28); var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
26func gcat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var i: i64=0; while s[i]!=(0 as u8){ dst[o]=s[i]; o=o+1; i=i+1 } return o }
27func gcatn(dst: *u8, off: i64, v: i64) -> i64 { var m: i64=v; var o: i64=off; if m==0{dst[o]=48 as u8; return o+1} let t: *u8=sys_mmap(28); var k: i64=0; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{dst[o+i]=t[k-1-i];i=i+1} return o+k }
28
29// ---- WMS lanes (verbatim shape from nx_ws_cas_gate -- the proven concurrent harness) ----
30func cg_key(out: *u8, w: i64, i: i64) -> i64 { var o: i64=0; o=gcat(out,o,"k:w" as *u8); o=gcatn(out,o,w); o=gcat(out,o,"i" as *u8); o=gcatn(out,o,i); out[o]=0 as u8; return o }
31func cas_worker(prefix: *u8, w: i64, locked: i64) -> i64 {
32 let key: *u8 = sys_mmap(64); let val: *u8 = sys_mmap(64)
33 var i: i64 = 0
34 while i < GH_K {
35 cg_key(key, w, i)
36 var o: i64 = gcat(val, 0, "v" as *u8); o = gcatn(val, o, w); o = gcat(val, o, "_" as *u8); o = gcatn(val, o, i); val[o]=0 as u8
37 if locked == 1 { ws_put_locked(prefix, key, val) } else { ws_put_p(prefix, key, val) }
38 i = i + 1
39 }
40 return 0
41}
42func cas_spawn(prefix: *u8, locked: i64) -> i64 {
43 let pids: *i64 = sys_mmap(8 * (GH_N + 4)) as *i64
44 var w: i64 = 0
45 while w < GH_N {
46 let pid: i64 = sys_fork()
47 if pid == 0 { cas_worker(prefix, w, locked); sys_exit(0) }
48 pids[w] = pid; w = w + 1
49 }
50 let st: *i64 = sys_mmap(16) as *i64
51 w = 0
52 while w < GH_N { sys_wait4(pids[w], st, 0); w = w + 1 }
53 return 0
54}
55func cas_retrievable(prefix: *u8) -> i64 {
56 let h: *i64 = ss_open(prefix)
57 let pq: *i64 = sys_mmap(16) as *i64; let lq: *i64 = sys_mmap(16) as *i64
58 let key: *u8 = sys_mmap(64)
59 var got: i64 = 0
60 var w: i64 = 0
61 while w < GH_N { var i: i64 = 0; while i < GH_K { cg_key(key, w, i); if ss_hget(h, key, pq, lq) == 1 { got = got + 1 } i = i + 1 } w = w + 1 }
62 return got
63}
64func cas_prefix(out: *u8, tag: *u8, epoch: i64) -> i64 { var o: i64=gcat(out,0,"/tmp/" as *u8); o=gcat(out,o,tag); o=gcatn(out,o,epoch); o=gcat(out,o,"-" as *u8); out[o]=0 as u8; return o }
65
66// ---- git lane ----
67func gh_env() -> *i64 {
68 let e: *i64 = sys_mmap(8 * 16) as *i64
69 e[0]="PATH=/usr/bin:/bin" as *u8 as i64; e[1]="HOME=/tmp" as *u8 as i64
70 e[2]="GIT_AUTHOR_NAME=nx" as *u8 as i64; e[3]="GIT_AUTHOR_EMAIL=nx@nx" as *u8 as i64
71 e[4]="GIT_COMMITTER_NAME=nx" as *u8 as i64; e[5]="GIT_COMMITTER_EMAIL=nx@nx" as *u8 as i64
72 e[6]="GIT_CONFIG_GLOBAL=/dev/null" as *u8 as i64; e[7]="GIT_CONFIG_SYSTEM=/dev/null" as *u8 as i64; e[8]=0
73 return e
74}
75func gh_run(argv: *i64) -> i64 {
76 let pid: i64 = sys_fork()
77 if pid == 0 {
78 let dn: i64 = sys_openat_wr("/dev/null\x00" as *u8, 420)
79 if dn >= 0 { sys_dup3(dn, 1, 0); sys_dup3(dn, 2, 0) }
80 sys_execve(GH_GIT, argv, gh_env())
81 sys_exit(127)
82 }
83 let st: *i64 = sys_mmap(16) as *i64
84 sys_wait4(pid, st, 0)
85 return (st[0] >> 8) & 0xff
86}
87func gh_init(repo: *u8) -> i64 { let a: *i64=sys_mmap(8*8) as *i64; a[0]=GH_GIT as i64; a[1]="init" as *u8 as i64; a[2]="-q" as *u8 as i64; a[3]=repo as i64; a[4]=0; return gh_run(a) }
88func gh_commit(repo: *u8) -> i64 { let a: *i64=sys_mmap(8*12) as *i64; a[0]=GH_GIT as i64; a[1]="-C" as *u8 as i64; a[2]=repo as i64; a[3]="commit" as *u8 as i64; a[4]="--allow-empty" as *u8 as i64; a[5]="-q" as *u8 as i64; a[6]="-m" as *u8 as i64; a[7]="c" as *u8 as i64; a[8]=0; return gh_run(a) }
89// fork GH_N workers, each does GH_K sequential commits; worker exits with its #failures; parent sums.
90func gh_spawn(repo: *u8) -> i64 {
91 let pids: *i64 = sys_mmap(8 * (GH_N + 4)) as *i64
92 var w: i64 = 0
93 while w < GH_N {
94 let pid: i64 = sys_fork()
95 if pid == 0 { var f: i64=0; var j: i64=0; while j<GH_K { if gh_commit(repo)!=0 { f=f+1 } j=j+1 } sys_exit(f) }
96 pids[w] = pid; w = w + 1
97 }
98 var fails: i64 = 0
99 let st: *i64 = sys_mmap(16) as *i64
100 w = 0
101 while w < GH_N { sys_wait4(pids[w], st, 0); fails = fails + ((st[0] >> 8) & 0xff); w = w + 1 }
102 return fails
103}
104
105func main() -> i64 {
106 gw("=== nx_ws_exceed_git_live (SCLASS-NEUTRAL-01): WMS vs LIVE git -- 144 concurrent writes, measured ===\n" as *u8)
107 let epoch: i64 = sys_now_realtime_sec()
108 let total: i64 = GH_N * GH_K
109
110 let repo: *u8 = sys_mmap(128); var ro: i64 = gcat(repo, 0, "/tmp/nxgithd" as *u8); ro = gcatn(repo, ro, epoch); repo[ro]=0 as u8
111 let ic: i64 = gh_init(repo)
112 let ic2: i64 = gh_commit(repo)
113 gw("-- setup: git init rc=" as *u8); gn(ic); gw(" root-commit rc=" as *u8); gn(ic2); gw(" repo=" as *u8); gw(repo); gw("\n" as *u8)
114
115 let git_fail: i64 = gh_spawn(repo)
116 let git_ok: i64 = total - git_fail
117 gw("-- LIVE git: " as *u8); gn(total); gw(" concurrent commits -> ok=" as *u8); gn(git_ok); gw(" REFUSED(index/ref-lock, no retry)=" as *u8); gn(git_fail); gw("\n" as *u8)
118
119 let pfx_g: *u8 = sys_mmap(128); cas_prefix(pfx_g, "nxgh2hL" as *u8, epoch)
120 cas_spawn(pfx_g, 1)
121 let wms_ok: i64 = cas_retrievable(pfx_g)
122 let wms_lost: i64 = total - wms_ok
123 gw("-- WMS locked: " as *u8); gn(total); gw(" concurrent ws_put_locked -> completed=" as *u8); gn(wms_ok); gw(" LOST=" as *u8); gn(wms_lost); gw("\n" as *u8)
124
125 let pfx_b: *u8 = sys_mmap(128); cas_prefix(pfx_b, "nxgh2hU" as *u8, epoch)
126 cas_spawn(pfx_b, 0)
127 let unl_ok: i64 = cas_retrievable(pfx_b)
128 let unl_lost: i64 = total - unl_ok
129 gw("-- WMS UNLOCKED (neg-control): completed=" as *u8); gn(unl_ok); gw(" LOST=" as *u8); gn(unl_lost); gw(" (proves the lock is load-bearing)\n" as *u8)
130
131 gw("\n=== MEASURED LIVE H2H (no overclaim: git fail-fast is by-design safety; WMS = same safety + no caller retry) ===\n" as *u8)
132 var pass: i64=0; var fail: i64=0
133 if ic == 0 { if ic2 == 0 { gw(" T1 git ran (init+root commit). PASS\n" as *u8); pass=pass+1 } else { gw(" T1 FAIL git setup\n" as *u8); fail=fail+1 } } else { gw(" T1 FAIL git init\n" as *u8); fail=fail+1 }
134 if wms_lost == 0 { if wms_ok == total { gw(" T2 WMS completed ALL " as *u8); gn(total); gw(" concurrent writes, 0 lost. PASS\n" as *u8); pass=pass+1 } else { gw(" T2 FAIL\n" as *u8); fail=fail+1 } } else { gw(" T2 FAIL wms_lost>0\n" as *u8); fail=fail+1 }
135 if git_fail > 0 { gw(" T3 EXCEED: live git REFUSED " as *u8); gn(git_fail); gw("/" as *u8); gn(total); gw(" under contention (no retry); WMS refused 0. PASS\n" as *u8); pass=pass+1 } else { gw(" T3 PARITY this run: git refused 0 (raise N/K)\n" as *u8); fail=fail+1 }
136 if unl_lost > 0 { gw(" T4 NEG-CONTROL: unlocked WMS LOST " as *u8); gn(unl_lost); gw(" -> the 0-lost result is the lock working, not luck. PASS\n" as *u8); pass=pass+1 } else { gw(" T4 FAIL: neg-control didn't fire\n" as *u8); fail=fail+1 }
137
138 gw("\npass=" as *u8); gn(pass); gw(" fail=" as *u8); gn(fail); gw("\n" as *u8)
139 if fail == 0 {
140 gw("VERDICT: GREEN -- MEASURED LIVE EXCEED: under " as *u8); gn(total); gw(" concurrent writers, WMS completed all (0 lost) while live git 2.43.0 refused " as *u8); gn(git_fail); gw(" (caller must retry). Sovereign, neg-controlled.\n" as *u8)
141 sys_exit(0); return 0
142 }
143 gw("VERDICT: RED\n" as *u8)
144 sys_exit(1)
145 return 1
146}