code wiki / _hdl_build / nx_costcal.nx

nx_costcal.nx source

↩ module page · 209 lines · 12205 B

1// nx_costcal.nx -- THE COST CALIBRATION CENSUS (2026-09-16): every board's declared rung cost (u) against the 2// wall-clock its own journal recorded from the rung's first row to its last land row, written as the ledger the 3// compare emitter and the ranker read to turn u into hours. 4// nx_costcal [ledger-path] population = regen.list (published order, both trees); default ledger 5// knowledge/status/costcal.ledger, truncate-written; verdict LAST 6// exit 0 CALIBRATED | 1 UNCALIBRATED (fewer calibrated rungs than the distinct-rank floor) | 3 NO-POPULATION 7// 8// WHY. The plan's unit row says "estimates recalibrate as rungs land and PR7 actuals write back" and PR7 stayed 9// _ABSENT_ since 2026-08-20, so every u on every board has been a declaration with no ratio to hours behind it. 10// The record already holds the actuals: more than a thousand land rows across the boards (a grep of 11// buildroot/knowledge/compare hit its 1 MiB capture on them, 2026-09-16). This organ reads them all, excludes 12// by name what cannot be calibrated (a land row that is the rung's first row; a rung with no declared cost), 13// and publishes the quantiles with their n. ONE grammar for the ledger line lives in nx_costest_lib, shared 14// with every reader, so the writer and the readers cannot drift. 15// license_tier: ORIGINAL No hw writes (Rule 26). 16import "nx_syscalls.nx" 17import "nx_barfresh_lib.nx" 18import "nx_costest_lib.nx" 19 20const CC_ARG_LEDGER: i64 = 1 21const CC_STDOUT: i64 = 1 22const CC_MODE_644: i64 = 420 23const CC_REGEN_DOM: *u8 = "regen" 24const CC_REGEN_SUFFIX: *u8 = ".list" 25const CC_LEDGER_DEFAULT: *u8 = "knowledge/status/costcal.ledger" 26const CC_EXIT_OK: i64 = 0 27const CC_EXIT_UNCAL: i64 = 1 28const CC_EXIT_NOPOP: i64 = 3 29const CC_NUM_CAP: i64 = 32 30const CC_HEADER_RESERVE: i64 = 2048 31const CC_WIP_STATUS: *u8 = "knowledge/status/wip.status" // EC57: ONE canonical WIP line, truncate-written, passed through verbatim by the boot digest 32const CC_WIP_SUFFIX: *u8 = ".wip" // a run against a non-default ledger writes its WIP line beside THAT ledger, never the production status 33const CC_SEC_PER_DAY: i64 = 86400 34const CC_CH_COMMA: i64 = 44 35 36func cc_w(s: *u8) -> i64 { return sys_write(CC_STDOUT, s, ces_slen(s)) } 37func cc_wn(v: i64) -> i64 { let b: *u8 = sys_mmap(CC_NUM_CAP); let o: i64 = ces_fmt_int(b, 0, v); return sys_write(CC_STDOUT, b, o) } 38 39// <path><suffix> in a fresh buffer: where a non-default run writes what the default run writes to production 40func cc_beside(path: *u8, suffix: *u8) -> *u8 { 41 let p: *u8 = sys_mmap(ces_slen(path) + ces_slen(suffix) + 1) 42 var o: i64 = ces_cat(p, 0, path) 43 o = ces_cat(p, o, suffix) 44 return p 45} 46 47// one pass over regen.list: mode 0 sums plan bytes, mode 1 censuses. Returns the domain count. 48func cc_walk(lst: *u8, ln: i64, mode: i64, samples: *i64, scap: i64, sc: *i64, st: *i64, rows: *u8, rcap: i64, ro: *i64, bytes_out: *i64) -> i64 { 49 let which: *i64 = sys_mmap(CES_I64) as *i64 50 let plp: *i64 = sys_mmap(CES_I64) as *i64 51 var domains: i64 = 0 52 var p: i64 = 0 53 while p < ln { 54 let e: i64 = ces_line_end(lst, ln, p) 55 let l: i64 = e - p 56 if l > 0 { if lst[p] != (CES_CH_HASH as u8) { 57 let name: *u8 = sys_mmap(l + 1) 58 var i: i64 = 0 59 while i < l { name[i] = lst[p + i]; i = i + 1 } 60 name[l] = 0 as u8 61 domains = domains + 1 62 plp[0] = 0 63 let pbuf: *u8 = bf_read_plan(ct_first_published(), ct_second_published(), name, plp, which) 64 let pn: i64 = plp[0] 65 if pn > 0 { 66 if mode == 0 { bytes_out[0] = bytes_out[0] + pn } else { 67 let before_cal: i64 = st[CES_S_CALIBRATED] 68 let before_land: i64 = st[CES_S_WITHLAND] 69 let before_zero: i64 = st[CES_S_EXCL_ZERO] 70 let before_nocost: i64 = st[CES_S_EXCL_NOCOST] 71 let nr: i64 = ces_census_plan(pbuf, pn, name, samples, scap, sc, st, rows, rcap, ro) 72 st[CES_S_BOARDS] = st[CES_S_BOARDS] + 1 73 cc_w("COSTCAL domain=" as *u8); cc_w(name); cc_w(" tree=" as *u8); cc_wn(which[0]) 74 cc_w(" rungs=" as *u8); cc_wn(nr) 75 cc_w(" withland=" as *u8); cc_wn(st[CES_S_WITHLAND] - before_land) 76 cc_w(" calibrated=" as *u8); cc_wn(st[CES_S_CALIBRATED] - before_cal) 77 cc_w(" excl_zero=" as *u8); cc_wn(st[CES_S_EXCL_ZERO] - before_zero) 78 cc_w(" excl_nocost=" as *u8); cc_wn(st[CES_S_EXCL_NOCOST] - before_nocost) 79 cc_w("\n" as *u8) 80 } 81 } else { if mode == 1 { cc_w("COSTCAL domain=" as *u8); cc_w(name); cc_w(" plan=NONE\n" as *u8) } } 82 sys_free_file(pbuf, pn) 83 } } 84 p = e + 1 85 } 86 return domains 87} 88 89func main(argc: i64, argv: *i64) -> i64 { 90 var ledger: *u8 = CC_LEDGER_DEFAULT 91 if argc > CC_ARG_LEDGER { ledger = argv[CC_ARG_LEDGER] as *u8 } 92 // the population, through the same two trees as the plans 93 let path: *u8 = sys_mmap(CT_PATH_CAP) 94 let lenp: *i64 = sys_mmap(CES_I64) as *i64 95 ct_build_path(ct_first_published(), CC_REGEN_DOM, CC_REGEN_SUFFIX, path) 96 lenp[0] = 0 97 var lst: *u8 = sys_read_file(path, lenp) 98 if lenp[0] <= 0 { 99 ct_build_path(ct_second_published(), CC_REGEN_DOM, CC_REGEN_SUFFIX, path) 100 lst = sys_read_file(path, lenp) 101 } 102 if lenp[0] <= 0 { cc_w("COSTCAL regen.list UNREADABLE in both trees verdict=NO-POPULATION\n" as *u8); return CC_EXIT_NOPOP } 103 let ln: i64 = lenp[0] 104 let st: *i64 = sys_mmap(CES_S_SLOTS * CES_I64) as *i64 105 let sc: *i64 = sys_mmap(CES_I64) as *i64 106 let ro: *i64 = sys_mmap(CES_I64) as *i64 107 let bytes: *i64 = sys_mmap(CES_I64) as *i64 108 bytes[0] = 0 109 // pass 0: size the sample and row reserves from the plans themselves, never from a guess 110 cc_walk(lst, ln, 0, 0 as *i64, 0, sc, st, 0 as *u8, 0, ro, bytes) 111 let scap: i64 = bytes[0] / CES_MIN_RUNG_ROW + 1 112 let rcap: i64 = scap * CES_ROW_RESERVE + CC_HEADER_RESERVE 113 let samples: *i64 = sys_mmap(scap * CES_I64) as *i64 114 let rows: *u8 = sys_mmap(rcap) 115 sc[0] = 0 116 ro[0] = 0 117 rows[0] = 0 as u8 118 // pass 1: the census 119 let domains: i64 = cc_walk(lst, ln, 1, samples, scap, sc, st, rows, rcap, ro, bytes) 120 let n: i64 = sc[0] 121 ces_sort(samples, n) 122 st[CES_S_P10] = ces_quantile(samples, n, CES_P10) 123 st[CES_S_P50] = ces_quantile(samples, n, CES_P50) 124 st[CES_S_P90] = ces_quantile(samples, n, CES_P90) 125 if n <= 0 { st[CES_S_P10] = 0; st[CES_S_P50] = 0; st[CES_S_P90] = 0 } 126 st[CES_S_MINN] = ces_cal_min_n() 127 st[CES_S_ASOF] = sys_now_realtime_sec() 128 // the ledger: a derived artefact, truncate-written, the cal line first so a reader anchors on it 129 let hdr: *u8 = sys_mmap(CC_HEADER_RESERVE) 130 var o: i64 = ces_cat(hdr, 0, "# NX-DERIVED: regenerated artefact, not authored memory -- nx_costcal calibration of the boards' unit u\n" as *u8) 131 o = ces_cat(hdr, o, "# cal|asof|boards|rungs|logrows|unmatched|withland|calibrated|excl_zero|excl_nocost|p10|p50|p90|min_n (p10 p50 p90 = centi-hours per u over the calibrated rungs; withland = calibrated + excl_zero + excl_nocost)\n" as *u8) 132 o = ces_cat(hdr, o, "# row|domain|rung|cost_deciu|first_epoch|last_land_epoch|elapsed_s|centih_per_u (elapsed is wall-clock from the rung's first journal row to its last land row: a ceiling on effort)\n" as *u8) 133 o = ces_cat(hdr, o, "# land|domain|rung|epoch|cost_deciu and retract|domain|rung|epoch (EC56: one row per journal land or retract event on a declared rung, every one, so a reader can count what landed in ANY window; the row| lines keep only a calibrated rung's last land)\n" as *u8) 134 o = ces_cat(hdr, o, "# open|domain|rung|first_epoch|last_epoch|cost_deciu (EC57: a rung with journal rows and NO land row -- started, not landed; the WIP line in knowledge/status/wip.status summarises these)\n" as *u8) 135 o = ces_cal_line(hdr, o, st) 136 let fd: i64 = sys_openat_wr(ledger, CC_MODE_644) 137 var wrote: i64 = 0 138 if fd >= 0 { 139 sys_write(fd, hdr, o) 140 sys_write(fd, rows, ro[0]) 141 sys_close(fd) 142 wrote = 1 143 } 144 // EC57 WIP: the started-not-landed rungs, summarised at asof and written as ONE canonical line the boot digest 145 // passes through verbatim (the producer's format is the contract; nothing re-parses it into fields). A run 146 // against a non-default ledger writes beside THAT ledger and never touches the production status. 147 let wacc: *i64 = sys_mmap(CES_O_SLOTS * CES_I64) as *i64 148 let wtoff: *i64 = sys_mmap(CES_WIP_TOP * CES_I64) as *i64 149 let wtlen: *i64 = sys_mmap(CES_WIP_TOP * CES_I64) as *i64 150 let wnamed: i64 = ces_open_summary(rows, ro[0], st[CES_S_ASOF], CES_WIP_ACTIVE_S, wacc, wtoff, wtlen) 151 let wl: *u8 = sys_mmap(CC_HEADER_RESERVE) 152 var wo: i64 = ces_cat(wl, 0, "WIP asof=" as *u8) 153 wo = ces_fmt_int(wl, wo, st[CES_S_ASOF]) 154 wo = ces_cat(wl, wo, " started_not_landed=" as *u8) 155 wo = ces_fmt_int(wl, wo, wacc[CES_O_OPEN]) 156 wo = ces_cat(wl, wo, " active_7d=" as *u8) 157 wo = ces_fmt_int(wl, wo, wacc[CES_O_ACTIVE]) 158 wo = ces_cat(wl, wo, " stale=" as *u8) 159 wo = ces_fmt_int(wl, wo, wacc[CES_O_STALE]) 160 wo = ces_cat(wl, wo, " oldest_days=" as *u8) 161 if wacc[CES_O_OLDEST] >= 0 { wo = ces_fmt_int(wl, wo, (st[CES_S_ASOF] - wacc[CES_O_OLDEST]) / CC_SEC_PER_DAY) } else { wo = ces_cat(wl, wo, "none" as *u8) } 162 wo = ces_cat(wl, wo, " newest=" as *u8) 163 var wk: i64 = 0 164 while wk < wnamed { 165 if wk > 0 { wl[wo] = CC_CH_COMMA as u8; wo = wo + 1 } 166 var wq: i64 = 0 167 while wq < wtlen[wk] { 168 var wc: i64 = rows[wtoff[wk] + wq] as i64 169 if wc == CES_CH_PIPE { wc = CES_CH_COLON } 170 wl[wo] = wc as u8 171 wo = wo + 1 172 wq = wq + 1 173 } 174 wk = wk + 1 175 } 176 if wnamed == 0 { wo = ces_cat(wl, wo, "none" as *u8) } 177 wo = ces_cat(wl, wo, " landed_rungs=" as *u8) 178 wo = ces_fmt_int(wl, wo, st[CES_S_WITHLAND]) 179 wo = ces_cat(wl, wo, " boards=" as *u8) 180 wo = ces_fmt_int(wl, wo, st[CES_S_BOARDS]) 181 wo = ces_cat(wl, wo, " src=costcal.ledger-open-rows\n" as *u8) 182 var wipp: *u8 = CC_WIP_STATUS 183 if argc > CC_ARG_LEDGER { wipp = cc_beside(ledger, CC_WIP_SUFFIX) } 184 let wfd: i64 = sys_openat_wr(wipp, CC_MODE_644) 185 var wwrote: i64 = 0 186 if wfd >= 0 { sys_write(wfd, wl, wo); sys_close(wfd); wwrote = 1 } 187 sys_write(CC_STDOUT, wl, wo) 188 cc_w("COSTCAL-WIP status=" as *u8); cc_w(wipp); cc_w(" wrote=" as *u8); cc_wn(wwrote); cc_w("\n" as *u8) 189 cc_w("COSTCAL-SUMMARY domains=" as *u8); cc_wn(domains) 190 cc_w(" boards=" as *u8); cc_wn(st[CES_S_BOARDS]) 191 cc_w(" rungs=" as *u8); cc_wn(st[CES_S_RUNGS]) 192 cc_w(" logrows=" as *u8); cc_wn(st[CES_S_LOGROWS]) 193 cc_w(" unmatched=" as *u8); cc_wn(st[CES_S_UNMATCHED]) 194 cc_w(" withland=" as *u8); cc_wn(st[CES_S_WITHLAND]) 195 cc_w(" calibrated=" as *u8); cc_wn(st[CES_S_CALIBRATED]) 196 cc_w(" excl_zero=" as *u8); cc_wn(st[CES_S_EXCL_ZERO]) 197 cc_w(" excl_nocost=" as *u8); cc_wn(st[CES_S_EXCL_NOCOST]) 198 cc_w(" partition_sum=" as *u8); cc_wn(st[CES_S_CALIBRATED] + st[CES_S_EXCL_ZERO] + st[CES_S_EXCL_NOCOST]) 199 cc_w(" overflow=" as *u8); cc_wn(st[CES_S_OVERFLOW]) 200 cc_w(" land_events=" as *u8); cc_wn(st[CES_S_LANDEVENTS]); cc_w(" retracts=" as *u8); cc_wn(st[CES_S_RETRACTS]); cc_w(" started_not_landed=" as *u8); cc_wn(st[CES_S_OPEN]) 201 cc_w(" centih_per_u p10=" as *u8); cc_wn(st[CES_S_P10]); cc_w(" p50=" as *u8); cc_wn(st[CES_S_P50]); cc_w(" p90=" as *u8); cc_wn(st[CES_S_P90]) 202 cc_w(" min_n=" as *u8); cc_wn(st[CES_S_MINN]) 203 cc_w(" ledger=" as *u8); cc_w(ledger); cc_w(" wrote=" as *u8); cc_wn(wrote); cc_w(" bytes=" as *u8); cc_wn(o + ro[0]) 204 cc_w("\n" as *u8) 205 if wrote == 0 { cc_w("COSTCAL ledger UNWRITABLE verdict=UNCALIBRATED\n" as *u8); return CC_EXIT_UNCAL } 206 if st[CES_S_CALIBRATED] < st[CES_S_MINN] { cc_w("verdict=UNCALIBRATED (calibrated rungs below the distinct-rank floor)\n" as *u8); return CC_EXIT_UNCAL } 207 cc_w("verdict=CALIBRATED\n" as *u8) 208 return CC_EXIT_OK 209}