code wiki / _hdl_build / nx_infomgmt_race.nx

nx_infomgmt_race.nx source

↩ module page · 253 lines · 10446 B

1// nx_infomgmt_race.nx -- IM6: THE BEAT-SQLITE RACE (the named EXCEED measurement 2// of the S-class-exceed information-management ladder). 3// 4// Two lanes, SAME input (elder_census.log), SAME logical work, each lane 5// SELF-TIMES only its storage-engine work (parse excluded on both sides): 6// SOV lane: /tmp/nx_race_sov_worker.sov.elf (segment store, fsync'd commit, 7// binary-search keyed reads) -> SOVW/SOVR us= count= 8// SQL lane: python3 sqlite3 (the incumbent; -> SQLW/SQLR us= count= 9// never linked -- run as an EXTERNAL COMPETITOR PROCESS; the 10// harness .py is written to /tmp at runtime so the repo stays 11// pure NishiLang; sqlite commits fsync by default = comparable 12// durability semantics) 13// 14// Verdict law: the numbers are MEASURED and logged win or lose -- a loss is 15// filed evidence naming the next optimization rung, never silenced. Exit 0 16// only when counts cross-check AND the sovereign lane wins BOTH phases (the 17// exceed claim itself). Durable: knowledge/status/infomgmt_race.log. 18// license_tier: ORIGINAL 19 20import "nx_syscalls.nx" 21const K_MAGIC_8192: i64 = 8192 22const K_MAGIC_8000: i64 = 8000 23 24func rc_p(s: *u8) -> i64 { 25 var n: i64 = 0 26 while s[n] != (0 as u8) { n = n + 1 } 27 sys_write(1, s, n) 28 return 0 29} 30 31func rc_fp(fd: i64, s: *u8) -> i64 { 32 var n: i64 = 0 33 while s[n] != (0 as u8) { n = n + 1 } 34 sys_write(fd, s, n) 35 return 0 36} 37 38func rc_fn(fd: i64, v: i64) -> i64 { 39 let bb: *u8 = sys_mmap(28) 40 var m: i64 = v 41 if m < 0 { m = 0 - m; sys_write(fd, "-" as *u8, 1) } 42 let t: *u8 = sys_mmap(28) 43 var k: i64 = 0 44 if m == 0 { t[0] = 48 as u8; k = 1 } 45 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 46 var i: i64 = 0 47 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 48 sys_write(fd, bb, k) 49 return 0 50} 51 52func rc_cat(dst: *u8, off: i64, s: *u8) -> i64 { 53 var i: i64 = 0 54 while s[i] != (0 as u8) { dst[off + i] = s[i]; i = i + 1 } 55 return off + i 56} 57 58func rc_catn(dst: *u8, off: i64, v: i64) -> i64 { 59 var m: i64 = v 60 var o: i64 = off 61 let t: *u8 = sys_mmap(28) 62 var k: i64 = 0 63 if m == 0 { t[0] = 48 as u8; k = 1 } 64 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 65 var i: i64 = 0 66 while i < k { dst[o + i] = t[k - 1 - i]; i = i + 1 } 67 return o + k 68} 69 70// fork+exec argv (up to 3 args), stdout -> outpath, stderr muted; raw status 71func rc_run(path: *u8, a1: *u8, a2: *u8, a3: *u8, outpath: *u8) -> i64 { 72 let pid: i64 = sys_fork() 73 if pid == 0 { 74 let ofd: i64 = sys_openat_wr(outpath, 0x1a4) 75 if ofd >= 0 { sys_dup3(ofd, 1, 0) } 76 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4) 77 if dn >= 0 { sys_dup3(dn, 2, 0) } 78 let argv: *i64 = sys_mmap(8 * 6) as *i64 79 argv[0] = path as i64 80 var na: i64 = 1 81 if (a1 as i64) != 0 { argv[na] = a1 as i64; na = na + 1 } 82 if (a2 as i64) != 0 { argv[na] = a2 as i64; na = na + 1 } 83 if (a3 as i64) != 0 { argv[na] = a3 as i64; na = na + 1 } 84 argv[na] = 0 85 let envp: *i64 = sys_mmap(16) as *i64 86 envp[0] = 0 87 sys_execve(path, argv, envp) 88 sys_exit(127) 89 } 90 let st: *i64 = sys_mmap(16) as *i64 91 sys_wait4(pid, st, 0) 92 return st[0] 93} 94 95// read file, find first decimal right after pattern; -1 if absent 96func rc_num_after(path: *u8, pat: *u8) -> i64 { 97 let fd: i64 = sys_openat_rd(path) 98 if fd < 0 { return 0 - 1 } 99 let b: *u8 = sys_mmap(K_MAGIC_8192) 100 let n: i64 = sys_read(fd, b, K_MAGIC_8000) 101 sys_close(fd) 102 if n <= 0 { return 0 - 1 } 103 var pl: i64 = 0 104 while pat[pl] != (0 as u8) { pl = pl + 1 } 105 var i: i64 = 0 106 while i + pl <= n { 107 var hit: i64 = 1 108 var k: i64 = 0 109 while k < pl { 110 if hit == 1 { if b[i + k] != pat[k] { hit = 0 } } 111 k = k + 1 112 } 113 if hit == 1 { 114 var v: i64 = 0 115 var p: i64 = i + pl 116 var any: i64 = 0 117 var go: i64 = 1 118 while go == 1 { 119 if p >= n { go = 0 } 120 if go == 1 { 121 let c: i64 = b[p] 122 if c >= 48 { if c <= 57 { v = v * 10 + (c - 48); any = 1; p = p + 1 } else { go = 0 } } else { go = 0 } 123 } 124 } 125 if any == 1 { return v } 126 return 0 - 1 127 } 128 i = i + 1 129 } 130 return 0 - 1 131} 132 133// write the competitor harness (.py) -- /tmp only, never the repo 134func rc_write_py(path: *u8) -> i64 { 135 let fd: i64 = sys_openat_wr(path, 0x1a4) 136 if fd < 0 { return 0 - 1 } 137 rc_fp(fd, "import sqlite3, time, sys\n" as *u8) 138 rc_fp(fd, "mode=sys.argv[1]; db=sys.argv[2]\n" as *u8) 139 rc_fp(fd, "recs={}\n" as *u8) 140 rc_fp(fd, "for L in open('knowledge/status/elder_census.log'):\n" as *u8) 141 rc_fp(fd, " L=L.strip()\n" as *u8) 142 rc_fp(fd, " if L.startswith('CENSUS-SVC name='):\n" as *u8) 143 rc_fp(fd, " sp=L[16:].split(' ',1)\n" as *u8) 144 rc_fp(fd, " if len(sp)==2 and sp[1].startswith('class='):\n" as *u8) 145 rc_fp(fd, " recs[sp[0]]=sp[1][6:]\n" as *u8) 146 rc_fp(fd, "if mode=='write':\n" as *u8) 147 rc_fp(fd, " t0=time.monotonic_ns()\n" as *u8) 148 rc_fp(fd, " c=sqlite3.connect(db)\n" as *u8) 149 rc_fp(fd, " c.execute('CREATE TABLE IF NOT EXISTS svc(k TEXT PRIMARY KEY, name TEXT, cls TEXT)')\n" as *u8) 150 rc_fp(fd, " cur=c.cursor()\n" as *u8) 151 rc_fp(fd, " for n in recs:\n" as *u8) 152 rc_fp(fd, " cur.execute('INSERT OR IGNORE INTO svc VALUES(?,?,?)',('svc:'+n,n,recs[n]))\n" as *u8) 153 rc_fp(fd, " c.commit()\n" as *u8) 154 rc_fp(fd, " c.close()\n" as *u8) 155 rc_fp(fd, " t1=time.monotonic_ns()\n" as *u8) 156 rc_fp(fd, " print('SQLW us=%d count=%d' % ((t1-t0)//1000, len(recs)))\n" as *u8) 157 rc_fp(fd, "else:\n" as *u8) 158 rc_fp(fd, " t0=time.monotonic_ns()\n" as *u8) 159 rc_fp(fd, " c=sqlite3.connect(db)\n" as *u8) 160 rc_fp(fd, " got=0\n" as *u8) 161 rc_fp(fd, " for n in recs:\n" as *u8) 162 rc_fp(fd, " r=c.execute('SELECT cls FROM svc WHERE k=?',('svc:'+n,)).fetchone()\n" as *u8) 163 rc_fp(fd, " if r is not None: got+=1\n" as *u8) 164 rc_fp(fd, " c.close()\n" as *u8) 165 rc_fp(fd, " t1=time.monotonic_ns()\n" as *u8) 166 rc_fp(fd, " print('SQLR us=%d count=%d' % ((t1-t0)//1000, got))\n" as *u8) 167 sys_close(fd) 168 return 0 169} 170 171func main() -> i64 { 172 rc_p("=== IM6 RACE: sovereign segment store vs sqlite (same input, same work, self-timed engine-only) ===\n" as *u8) 173 // self-heal the sovereign worker (durable-runner fallback pattern) 174 let wpath: *u8 = "/tmp/nx_race_sov_worker.sov.elf" as *u8 175 let probe: i64 = sys_openat_rd(wpath) 176 if probe >= 0 { sys_close(probe) } else { 177 rc_run("_offc/nx_sov_build_run.elf" as *u8, "nx_race_sov_worker" as *u8, 0 as *u8, 0 as *u8, "/tmp/race_build.out" as *u8) 178 } 179 180 let id: i64 = sys_now_us() 181 let sovpfx: *u8 = sys_mmap(128) 182 var o: i64 = 0 183 o = rc_cat(sovpfx, o, "/tmp/rsov" as *u8) 184 o = rc_catn(sovpfx, o, id) 185 o = rc_cat(sovpfx, o, "-" as *u8) 186 sovpfx[o] = 0 as u8 187 let dbp: *u8 = sys_mmap(128) 188 o = 0 189 o = rc_cat(dbp, o, "/tmp/rsql" as *u8) 190 o = rc_catn(dbp, o, id) 191 o = rc_cat(dbp, o, ".db" as *u8) 192 dbp[o] = 0 as u8 193 194 if rc_write_py("/tmp/race_baseline.py" as *u8) != 0 { rc_p(" py harness write FAILED\n" as *u8); return 1 } 195 196 // four lane-phases (write before read, per lane) 197 rc_run(wpath, "write" as *u8, sovpfx, 0 as *u8, "/tmp/race_sw.out" as *u8) 198 rc_run(wpath, "read" as *u8, sovpfx, 0 as *u8, "/tmp/race_sr.out" as *u8) 199 let py: *u8 = "/usr/bin/python3" as *u8 200 rc_run(py, "/tmp/race_baseline.py" as *u8, "write" as *u8, dbp, "/tmp/race_qw.out" as *u8) 201 rc_run(py, "/tmp/race_baseline.py" as *u8, "read" as *u8, dbp, "/tmp/race_qr.out" as *u8) 202 203 let sw: i64 = rc_num_after("/tmp/race_sw.out" as *u8, "SOVW us=" as *u8) 204 let swc: i64 = rc_num_after("/tmp/race_sw.out" as *u8, "count=" as *u8) 205 let sr: i64 = rc_num_after("/tmp/race_sr.out" as *u8, "SOVR us=" as *u8) 206 let src: i64 = rc_num_after("/tmp/race_sr.out" as *u8, "count=" as *u8) 207 let qw: i64 = rc_num_after("/tmp/race_qw.out" as *u8, "SQLW us=" as *u8) 208 let qwc: i64 = rc_num_after("/tmp/race_qw.out" as *u8, "count=" as *u8) 209 let qr: i64 = rc_num_after("/tmp/race_qr.out" as *u8, "SQLR us=" as *u8) 210 let qrc: i64 = rc_num_after("/tmp/race_qr.out" as *u8, "count=" as *u8) 211 212 // correctness cross-check: every lane-phase saw the SAME record count, >0 213 var counts_ok: i64 = 0 214 if swc > 0 { if swc == src { if swc == qwc { if swc == qrc { counts_ok = 1 } } } } 215 var win_w: i64 = 0 216 if sw >= 0 { if qw >= 0 { if sw <= qw { win_w = 1 } } } 217 var win_r: i64 = 0 218 if sr >= 0 { if qr >= 0 { if sr <= qr { win_r = 1 } } } 219 220 let lfd: i64 = sys_openat_append("knowledge/status/infomgmt_race.log" as *u8, 0x1a4) 221 var fdi: i64 = 0 222 while fdi < 2 { 223 var fd: i64 = 1 224 if fdi == 1 { fd = lfd } 225 if fd > 0 { 226 rc_fp(fd, "RACE epoch=" as *u8) 227 rc_fn(fd, sys_now_realtime_sec()) 228 rc_fp(fd, " workload=census records=" as *u8) 229 rc_fn(fd, swc) 230 rc_fp(fd, " sov_write_us=" as *u8) 231 rc_fn(fd, sw) 232 rc_fp(fd, " sql_write_us=" as *u8) 233 rc_fn(fd, qw) 234 rc_fp(fd, " sov_read_us=" as *u8) 235 rc_fn(fd, sr) 236 rc_fp(fd, " sql_read_us=" as *u8) 237 rc_fn(fd, qr) 238 rc_fp(fd, " counts_ok=" as *u8) 239 rc_fn(fd, counts_ok) 240 if counts_ok == 1 { 241 if win_w == 1 { if win_r == 1 { rc_fp(fd, " verdict=SOV-WINS-BOTH\n" as *u8) } } 242 if win_w == 1 { if win_r == 0 { rc_fp(fd, " verdict=MIXED-READ-LOSS (optimization rung: open-store handle cache)\n" as *u8) } } 243 if win_w == 0 { if win_r == 1 { rc_fp(fd, " verdict=MIXED-WRITE-LOSS (optimization rung: batch fsync)\n" as *u8) } } 244 if win_w == 0 { if win_r == 0 { rc_fp(fd, " verdict=SOV-LOSES-BOTH (file defect: slow is a bug)\n" as *u8) } } 245 } 246 if counts_ok != 1 { rc_fp(fd, " verdict=INVALID (counts disagree -- correctness before speed)\n" as *u8) } 247 } 248 fdi = fdi + 1 249 } 250 if lfd > 0 { sys_close(lfd) } 251 if counts_ok == 1 { if win_w == 1 { if win_r == 1 { return 0 } } } 252 return 1 253}