code wiki / _hdl_build / nx_cms_exceed_census.nx

nx_cms_exceed_census.nx source

↩ module page · 141 lines · 6028 B

1// nx_cms_exceed_census.nx -- consolidates the EXCEED track into a single MEASURED number (twin of 2// nx_cms_census, which measures REACH). Reads the WP feature surface cms_incumbent_ref.tsv (the 30 3// features) and COMPUTES, per feature, whether a head-to-head measured AHEAD exists in cms_exceed.log 4// (a line with `feature=<name> ` AND `verdict=AHEAD`) -- never asserted. Emits exceed_coverage_permil 5// over the 30 features (directly comparable to reach permil) plus the raw measured-AHEAD count. 6// Self-validating: a known-won feature MUST read won (pos control), a bogus feature MUST NOT (neg). 7// module: nishi-core.cms.exceed_census 8// depends: nishi-core.io.syscalls 9// capability: CMS 10// license_tier: ORIGINAL 11import "nx_syscalls.nx" 12import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 13const EC_MAGIC_80000: i64 = 80000 14 15const EC_REF: *u8 = "knowledge/registry/cms_incumbent_ref.tsv" 16const EC_LOG: *u8 = "knowledge/status/cms_exceed.log" 17const EC_OUT: *u8 = "knowledge/status/cms_exceed_census.log" 18const EC_MAXF: i64 = 256 19 20func ec_msg(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 21// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 22// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 23// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 24// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 25func ec_putn(v: i64) -> i64 { nxi_out(v); return 0 } 26func ec_cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var k: i64=0; while s[k]!=(0 as u8){dst[o]=s[k];o=o+1;k=k+1} return o } 27func ec_catn(dst: *u8, off: i64, v: i64) -> i64 { 28 var o: i64=off; let t: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m}; var k: i64=0 29 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} 30 var i: i64=0; while i<k {dst[o]=t[k-1-i]; o=o+1; i=i+1} return o 31} 32 33func ec_read_file(path: *u8, buf: *u8, cap: i64) -> i64 { 34 let fd: i64 = sys_openat_rd(path) 35 if fd < 0 { return 0 - 1 } 36 var tot: i64 = 0 37 var r: i64 = 1 38 while r > 0 { 39 let dst: *u8 = ((buf as i64) + tot) as *u8 40 r = sys_read(fd, dst, cap - tot) 41 if r > 0 { tot = tot + r } 42 } 43 sys_close(fd) 44 return tot 45} 46 47func ec_scan_to(buf: *u8, n: i64, start: i64, delim: i64) -> i64 { 48 var i: i64 = start 49 var s: i64 = 1 50 while s == 1 { if i >= n { s = 0 } else { if buf[i] == (delim as u8) { s = 0 } else { i = i + 1 } } } 51 return i 52} 53 54// find NUL-terminated needle within buf[lo..hi); -1 if absent 55func ec_find(buf: *u8, lo: i64, hi: i64, needle: *u8) -> i64 { 56 var nl: i64 = 0; while needle[nl] != (0 as u8) { nl = nl + 1 } 57 var i: i64 = lo 58 while i + nl <= hi { 59 var q: i64 = 0; var ok: i64 = 1 60 while q < nl { if (buf[i+q] as i64) != (needle[q] as i64) { ok = 0; q = nl } q = q + 1 } 61 if ok == 1 { return i } 62 i = i + 1 63 } 64 return 0 - 1 65} 66 67// does the log carry a measured AHEAD for this feature? (a single line with `feature=<feat> ` AND verdict=AHEAD) 68func ec_feat_won(log: *u8, ln: i64, feat: *u8) -> i64 { 69 let needle: *u8 = sys_mmap(160) 70 var o: i64 = ec_cat(needle, 0, "feature=" as *u8) 71 o = ec_cat(needle, o, feat) 72 needle[o] = 32 as u8; o = o + 1 // trailing space -> no prefix collision 73 needle[o] = 0 as u8 74 var i: i64 = 0 75 while i < ln { 76 var e: i64 = i 77 var go: i64 = 1 78 while go == 1 { if e >= ln { go = 0 } else { if (log[e] as i64) == 10 { go = 0 } else { e = e + 1 } } } 79 if ec_find(log, i, e, needle) >= 0 { if ec_find(log, i, e, "verdict=AHEAD" as *u8) >= 0 { return 1 } } 80 i = e + 1 81 } 82 return 0 83} 84 85func main() -> i64 { 86 let rb: *u8 = sys_mmap(EC_MAGIC_80000) 87 let rbn: i64 = ec_read_file(EC_REF, rb, EC_MAGIC_80000) 88 if rbn <= 0 { ec_msg("EXCEEDCENSUS verdict=RED reason=ref-unreadable\n" as *u8); return 1 } 89 let lg: *u8 = sys_mmap(EC_MAGIC_80000) 90 var lgn: i64 = ec_read_file(EC_LOG, lg, EC_MAGIC_80000) 91 if lgn < 0 { lgn = 0 } // empty log = 0 wins, still valid 92 93 // parse feature names (col 0), skipping # comments and blank lines 94 let feat: *i64 = sys_mmap(8 * EC_MAXF) as *i64 95 var nf: i64 = 0 96 var p: i64 = 0 97 while p < rbn { 98 if rb[p] == (35 as u8) { let e: i64 = ec_scan_to(rb, rbn, p, 10); p = e + 1 } 99 else { if rb[p] == (10 as u8) { p = p + 1 } 100 else { 101 let a0: i64 = p 102 let t1: i64 = ec_scan_to(rb, rbn, a0, 9); rb[t1] = 0 as u8 // NUL-terminate feature 103 if nf < EC_MAXF { feat[nf] = (rb as i64) + a0; nf = nf + 1 } 104 let e: i64 = ec_scan_to(rb, rbn, t1 + 1, 10); p = e + 1 105 } } 106 } 107 108 var won: i64 = 0 109 var i: i64 = 0 110 while i < nf { 111 if ec_feat_won(lg, lgn, (feat[i]) as *u8) == 1 { won = won + 1 } 112 i = i + 1 113 } 114 115 let cpos: i64 = ec_feat_won(lg, lgn, "redirects-404" as *u8) 116 let cneg: i64 = ec_feat_won(lg, lgn, "zzznonexistent-feature" as *u8) 117 var permil: i64 = 0 118 if nf > 0 { permil = (won * 1000) / nf } 119 120 // emit + record 121 let ob: *u8 = sys_mmap(512) 122 var o: i64 = ec_cat(ob, 0, "EXCEEDCENSUS authored=organ source=cms_exceed.log features=" as *u8) 123 o = ec_catn(ob, o, nf) 124 o = ec_cat(ob, o, " measured_ahead=" as *u8); o = ec_catn(ob, o, won) 125 o = ec_cat(ob, o, " exceed_coverage_permil=" as *u8); o = ec_catn(ob, o, permil) 126 o = ec_cat(ob, o, " control_pos=" as *u8); o = ec_catn(ob, o, cpos) 127 o = ec_cat(ob, o, " control_neg=" as *u8); o = ec_catn(ob, o, cneg) 128 129 var ok: i64 = 1 130 if cpos != 1 { ok = 0 } 131 if cneg != 0 { ok = 0 } 132 if nf <= 0 { ok = 0 } 133 if ok == 1 { o = ec_cat(ob, o, " verdict=GREEN\n" as *u8) } else { o = ec_cat(ob, o, " verdict=RED\n" as *u8) } 134 135 sys_write(1, ob, o) 136 let gf: i64 = sys_openat_append(EC_OUT, 0x1a4) 137 if gf >= 0 { sys_write(gf, ob, o); sys_close(gf) } 138 139 if ok == 1 { return 0 } 140 return 1 141}