code wiki / (root) / nx_segsweep.nx

nx_segsweep.nx

buildroot/runtime/nx_segsweep.nx

21013 B354 linesdepth 3pulls 3 transitivereach 0 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

about

nx_segsweep.nx -- SOVEREIGN seg-store sweeper. The organ that retires nx_segguard.sh (2026-08-06). OPERATOR DOCTRINE: ".sh are not nishi ecosystem". nx_segguard.sh was a SHELL WRAPPER around an organ that already existed -- it parsed segguard.conf with sed+head, enumerated the planes, and forked nx_seg_compact_cli.elf ONCE PER PLANE under a lock. The real work is ss_compact_cap in nx_seg_store.nx, a LIBRARY function callable in-process. With ~1087 manifests under knowledge/store that wrapper paid a SUBSHELL PLUS A CLI FORK PER PLANE, plus date/sed/head/mkdir every tick. MEASURED COST OF THE WRAPPER: 717s per run against a DECLARED 600s period (three independent measurements: 655s, 700s+, 717s), and a sibling seat measured it consuming 1399s of an 1800s clock window = 78pct, starving 31 of 45 jobs. It is also the direct source of the RED forks_per_sec=57 in nx_procchurn. This organ replaces ~2x1087 forks with ZERO. (STAR)A SHELL SCRIPT THAT WRAPS A SOVEREIGN ORGAN PAYS A PROCESS PER ITEM TO DO WORK THE LIBRARY WOULD DO PER CALL -- THE COST IS NOT THE ALGORITHM, IT IS THE WRAPPER. (STAR)THE SLOWEST THING ON A SOVEREIGN BOX IS THE PART THAT IS NOT SOVEREIGN YET. THE ONE THING THIS MUST NOT INHERIT: nx_seg_compact_cli.elf takes a BLOCKING flock(LOCK_EX) and holds it until PROCESS EXIT. Its own header records what that cost: "48 nx_segguard.sh + 24 nx_seg_compact_cli stacked, all State=S, oldest ~6h, thread count climbing ~260/hr on a box already 92pct into swap." A blocking lock is survivable in a fork-per-plane design because the process dies; in a SINGLE long-lived sweeper it would wedge the whole sweep forever. So this organ takes LOCK_EX|LOCK_NB and SKIPS a contended plane, and CLOSES the fd every iteration so locks can never accumulate. (STAR)A LOCK DISCIPLINE THAT IS SAFE ONLY BECAUSE THE PROCESS EXITS IS NOT SAFE IN A PROCESS THAT DOES NOT. BOUNDED + RESUMABLE (rule 10, idempotent): a wall-clock budget caps one run, and a persisted CURSOR makes the next run continue where this one stopped. A budget WITHOUT a cursor would re-scan the same alphabetical head forever and never reach the tail -- silent permanent blindness for whatever sorts last. nx_segsweep [threshold] [budget_secs] license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0

dependencies 2 imports · 0 importers

nx_syscalls.nx nx_seg_store.nx nx_segsweep.nx

imports: nx_syscalls.nxnx_seg_store.nx

imported by: nobody (leaf or entry point)

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main sys_now_realtime_sec sys_mmap nxa_die sys_write sys_exit nxa_lock_take nxa_lock_addr sys_write ↻ nxa_lock_give nxa_lock_addr ↻ nxa_report_overrun sys_write ↻ nxa_dump_printable sys_write ↻ nxa_dump_sizes sys_write ↻ sys_clock_gettime_real sw_conf_int sys_mmap ↻ sys_read_file sys_openat_rd sys_lseek sys_mmap ↻ sys_read sys_munmap sys_close sw_len sw_atoi sys_mmap ↻ sw_opendir sw_o sys_write ↻ sys_exit ↻ sys_getdents64 sw_is_manifest sw_len ↻ sw_slot sys_close ↻ sys_read_file ↻

structs

none

consts

32const SW_STORE: *u8 = "knowledge/store"
33const SW_CONF: *u8 = "knowledge/status/segguard.conf"
34const SW_LOG: *u8 = "knowledge/status/segsweep.log"
35const SW_CURSOR: *u8 = "knowledge/status/segsweep.cursor"
36const SW_SUFFIX: *u8 = "-manifest.txt"
37const SW_DIRBUF: i64 = 262144
38const SW_NAMEW: i64 = 256
39const SW_MAXPLANES: i64 = 4096
40const SW_LOCK_NB: i64 = 6 // LOCK_EX(2) | LOCK_NB(4) -- NEVER plain LOCK_EX here, see header
41const SW_MODE: i64 = 0x1a4
42const SW_DEF_THRESH: i64 = 8
43const SW_DEF_BUDGET: i64 = 120
44const SW_COMPACT_CAP: i64 = 100000
47const SW_CLI: *u8 = "/volume1/homes/elderwesto/nishihost/nx_seg_compact_cli.elf"
50const SW_CLI_TIMEOUT: i64 = 300
59const SW_MAXFOLD: i64 = 256
65const SW_REAP_TRIES: i64 = 20
77const SW_POISON: *u8 = "knowledge/status/segsweep_poison.txt"
78const SW_POISONCAP: i64 = 65536

functions

80func sw_o(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
called by 1: main calls 1: sys_write
81func sw_cat(d: *u8, o: i64, s: *u8) -> i64 { var x: i64=o; var i: i64=0; while s[i]!=(0 as u8){d[x]=s[i];x=x+1;i=i+1} return x }
82func sw_num(d: *u8, o: i64, v: i64) -> i64 { var x: i64=o; var mm: i64=v; if mm<0{d[x]=45 as u8;x=x+1;mm=0-mm} if mm==0{d[x]=48 as u8;return x+1} let t:*u8=sys_mmap(24); var k:i64=0; while mm>0{t[k]=(48+(mm%10)) as u8;mm=mm/10;k=k+1} var j:i64=0; while j<k{d[x]=t[k-1-j];x=x+1;j=j+1} return x }
called by 2: sw_beatmain calls 1: sys_mmap
83func sw_len(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
87func sw_opendir(p: *u8) -> i64 { return __syscall(257, 0-100, p as i64, 0x10000, 0, 0, 0) }
called by 1: main
88func sw_slot(base: *u8, i: i64) -> *u8 { return ((base as i64) + i*SW_NAMEW) as *u8 }
called by 1: main
97func sw_poisoned(pois: *u8, pn: i64, pfx: *u8) -> i64
called by 1: main calls 1: sw_len
113func sw_poison_add(pfx: *u8) -> i64
121func sw_beat(tag: *u8, pfx: *u8, idx: i64, live: i64) -> i64
133func sw_atoi(s: *u8) -> i64 { var v: i64=0; var i: i64=0; while s[i]!=(0 as u8){ if s[i]<(48 as u8){return v} if s[i]>(57 as u8){return v} v=v*10+((s[i]-(48 as u8)) as i64); i=i+1 } return v }
called by 1: main
136func sw_is_manifest(nm: *u8) -> i64
called by 1: main calls 1: sw_len
146func sw_conf_int(key: *u8, dflt: i64) -> i64
called by 1: main calls 3: sys_mmapsys_read_filesw_len
188func main(argc: i64, argv: *i64) -> i64