code wiki / (root) / nx_clock_sched_reserved_v2_ar.nx

nx_clock_sched_reserved_v2_ar.nx source

↩ module page · 1454 lines · 88347 B

1// nx_clock_sched.nx -- RENAMED FROM nx_clock.nx 2026-07-31 (lib-reconcile). PERMANENT FIX for a 2// two-libraries-one-name collision: runtime/nx_clock.nx is the TIMING library (nx_clock_monotonic_ns, 3// 62 importers); THIS file is an unrelated tickless JOB SCHEDULER (clk_* registry + dispatcher) that 4// only shared the filename. nx_cc binds an import to the IMPORTER'S OWN DIRECTORY FIRST, so every 5// _hdl_build organ importing nx_clock.nx silently got the SCHEDULER -- _clk_probe.nx, whose whole 6// purpose is to prove nx_clock_monotonic_ns works, could not resolve it. The RENAME is the fix. 7// so we avoid issues ... dont have a million pulses and daemons"). Researched (knowledge/fetched/sched_*.raw: 8// clock-distribution = ONE oscillator -> a tree of DIVIDERS; PLL derives every frequency from ONE reference; 9// cron = ONE daemon reads ONE table of timed jobs; tickless = don't burn a constant tick when idle). The S-class 10// pattern is identical: ONE tick source + ONE durable JOB REGISTRY + ONE dispatcher. Every periodic capability 11// REGISTERS a job (name, interval-in-ticks) = a divider off the single clock -- it does NOT spin up its own 12// daemon/pulse loop. So N capabilities cost ONE loop, not N. This library is the registry + dispatcher; the single 13// tick source drives it (one external spark, like a crystal). license_tier: ORIGINAL 14import "nx_syscalls.nx" 15import "nx_store_seed_lib.nx" 16import "nx_clock_caps.nx" 17import "nx_ioadmit_lib.nx" // ioa_measure / ioa_spawn_budget: THE I/O-storm ruler nx_build_admit uses -- composed here so the clock and the build gate can never disagree about whether the box is in a storm (2026-09-02) 18const CLK_MAGIC_1000000: i64 = 1000000 19 20const CLK_MAXJOBS: i64 = 512 // WAS 128 (2026-08-19): the desired plane hit 128/128 and every 21 // over-cap path was a SILENT drop -- a beat that vanishes at merge. 22 // 4x headroom; clk_register now refuses LOUDLY at the cap. Residual, 23 // named: the inline merge-at-cap skips stay quiet above 512. 24// DUTY-CYCLE CEILING FOR A COST-OVERRUNNING JOB (2026-08-06). Re-arming an overrunning job to exactly 25// its own runtime still leaves it consuming HALF the serial scheduler -- MEASURED: nx_worldgen_gate runs 26// 289s against a declared 120s period, so plain backoff took the blackout from ~96pct to ~50pct and 27// sitecheck stayed frozen through every run. Backing off to runtime*9 caps any single job at ~1/10 of 28// the scheduler. NOT A MAGIC NUMBER: it is the reciprocal of the duty-cycle budget (9 => <=10pct), and 29// it is the ONLY knob here, so raising it loosens the ceiling monotonically and nothing else moves. 30// (STAR)A SERIAL SCHEDULER MUST BOUND WHAT FRACTION OF ITSELF ONE JOB CAN OWN, BECAUSE EVERY OTHER JOB'S 31// LIVENESS IS THAT FRACTION'S COMPLEMENT. 32const CLK_HOGDUTY: i64 = 9 33// ---- PER-DISPATCH DEADLINE (2026-08-06): ADOPTION, NOT INVENTION ------------------------------- 34// nx_guarded_run.nx ALREADY solved "fork + wait4 WNOHANG + deadline + SIGKILL" and a dozen organs use 35// it (nx_tool_run, nx_sweep_core, nx_boot_revive, nx_web_crawl_step...). The clock simply never adopted 36// it and kept a bare BLOCKING sys_wait4(pid, st, 0) -- which is why one child could own the scheduler 37// indefinitely (debt 1786056459; proven live: 475s+ stall and the 1800s budget overrun by 42s). 38// (STAR)THE ESTATE HAD ALREADY SOLVED IT AND THE CLOCK DID NOT LOOK -- an ADOPTION gap, not a missing 39// primitive. Inlined rather than imported because nx_guarded_run.nx carries its own main() self-test. 40// nx_kill is the SANCTIONED wrapper: nx_syscalls notes the compiler BAKES bodies BY NAME, so a function 41// literally named sys_kill emits syscall 8; nx_kill passes rv64 129 which the sovereign table maps to 42// x86_64 62. Using the raw x86 62 here would have silently become lseek(8) and never killed anything. 43// DEADLINE = ONE FULL WINDOW: a single dispatch longer than the entire window is starving that window 44// BY DEFINITION, and it reaps nothing measured legitimate (worldgen 289s/286s, segguard ~700s). 45const CLK_SIGKILL: i64 = 9 46const CLK_DISPATCH_POLL_MS: i64 = 250 47// PER-JOB KILL DEADLINE. 900000ms = 900s = HALF the 1800s scheduling window (TLN_WINDOW_SECS). 48// WAS 1800000ms = the ENTIRE window, which is not a deadline at all: a single job could legally consume 49// the whole scheduler and the guard could not fire before the window ended anyway. A BOUND EQUAL TO THE 50// BUDGET CANNOT BIND. The call site below sells this as the fix for "the single line that let one child 51// own the clock" -- the bounded wait DID replace a bare blocking wait4, but the bound was then set to the 52// full budget, so the protection was nominal. 53// DERIVED, NOT CHOSEN: half the window means no single job can ever take more than half the scheduler, 54// and it carries 2.2x margin over the largest LEGITIMATE runtime measured on 2026-08-14 (max_exec across 55// three consecutive windows: 41s, 92s, and 408s for nx_compare_beat.elf). If that margin is ever wrong 56// the failure is VISIBLE, not silent: a job killed here is logged by clk_actlog with code 124 (the shell 57// timeout convention), so an over-tight bound surfaces as a named row rather than a mystery. 58// HONEST SCOPE: this bounds the PATHOLOGICAL case only. It does NOT fix the starvation measured that day 59// (b=11 -> 16 -> 23), which is cumulative time on a SERIAL dispatcher -- exec 761s -> 990s -> 1167s of an 60// 1800s window -- not one job monopolising. That needs bounded concurrency in clk_run_edf (debt 3952). 61const CLK_DISPATCH_DEADLINE_MS: i64 = 900000 62const CLK_NAMEW: i64 = CLK_CMD_CAP // bytes per name/organ slot -- DERIVED from the ONE shared 63 // command cap in nx_clock_caps.nx, the SAME const nx_clockjob's 64 // CJ_ROWCAP is derived from, so the reader can never hold less than 65 // the writer admits. WAS 128 (2026-08-03..08-22): every command 66 // longer than 127 bytes was SILENTLY CUT at merge/save/load -- 7 67 // live rows executed truncated argv (gateroster beat ran with 68 // deadline 1800 instead of 180000; admit/refusalshape/alertscore 69 // wrote knowledge/gateroster.c, knowledge/stat and kno). Before 70 // that it WAS 48 (debt 1784413227): 51/63-char paths fork-failed 71 // 127 on every tick. A SLOT NARROWER THAN WHAT THE WRITER ADMITS 72 // IS A SILENT TRUNCATOR WEARING A CONSTANT -- bind both ends to 73 // ONE imported const, never mirror a number. 74const CLK_ROWW: i64 = 2*CLK_NAMEW + 64 // per-row serialization budget, DERIVED, not chosen: 75 // 2 slots + 2 i64 decimals (<=20 bytes each) + 3 tabs + newline 76 // = 2*CLK_NAMEW + 44, held with 64 so the two numbers can never 77 // outgrow it (a hand-counted width beside a widened slot is the 78 // drift class this whole change retires) 79const CLK_REG: *u8 = "knowledge/sched/jobs.tsv" // RETIRED LEGACY PATH (2026-08-03, debt 1785792856): 80 // kept only so old fixtures parse; the live state SSOT is the 81 // clocksched- seg-store plane (clk_load_state / clk_save_plane below) 82 83// ---- STORM-AWARE DISPATCH FOR HEAVY BEATS (2026-09-02, /compare/loadgov LV14) ---------------------- 84// MEASURED THE SAME DAY: procchurn read cpu busy 24.5 percent with blocked >> running (I/O-bound), the 85// D-state roster carried nx_web_crawl_step (706 s), nx_secret_scan and nx_web_shard_compact -- all CLOCK 86// BEATS -- and every seat build was refused by admission for the same storm those beats were feeding. 87// This dispatcher had NO admission at all: a due job forked regardless of the box, so the beats that 88// cause a storm fired INTO it while the one lane that does check admission (the build queue) waited. 89// A SCHEDULER THAT CHECKS NOTHING IS THE STORM'S FEEDER, AND THE POLITE LANE PAYS FOR IT. 90// DESIGN, data-driven and fail-open: a job is HEAVY only if its NAME is listed in CLK_HEAVY_CONF (one 91// exact name per line; an unlisted job dispatches byte-for-byte as before, and an absent conf lists 92// nothing). A heavy job that is due while the shared ruler reports ZERO spawn budget (procs_blocked at 93// the storm line, the same conjunct nx_build_admit refuses on) is DEFERRED: re-armed to now plus a 94// fraction of its own period, counted, and announced on the in-flight beat as deferred-storm. It still 95// runs -- one storm cannot park it past CLK_DEFER_MAX_S per probe -- and EDF's own starvation report 96// still names it if the deferral compounds. An UNREADABLE ruler never defers: an axis that cannot see 97// must abstain, and here abstaining means the pre-change behaviour, never a stalled clock. 98// The instruments themselves (resmon, memvel, procchurn, sitecheck, netobs, tlsprobe, kaprobe) must 99// NEVER be listed: a detector deferred by the storm it detects is the axis-blind defect wearing a conf. 100const CLK_HEAVY_CONF: *u8 = "knowledge/status/clock_heavy.conf" 101const CLK_DEFER_DIV: i64 = 4 // re-arm at interval/4: a 1800 s beat re-checks every 450 s, so a storm costs it at most 3 probes before its own next period 102const CLK_DEFER_MIN_S: i64 = 60 // floor: the shortest re-check that is not a busy loop against /proc/stat 103const CLK_DEFER_MAX_S: i64 = 900 // cap: half a window, the same bound CLK_DISPATCH_DEADLINE_MS already places on one job 104// PURE: exact-whole-line membership of `name` in a newline-separated conf (CR-tolerant), the same 105// grammar the tools daemon's async_only_tools.conf reader uses, so a name with a suffix can never match. 106const CLKRS_CONF: *u8 = "knowledge/status/clock_reserved.conf" 107const CLKRS_NONE: i64 = 0 - 1 108const CLKRS_INVALID: i64 = 0 - 2 109const CLKRS_EINTR: i64 = 0 - 4 110const CLKRS_WNOHANG: i64 = 1 111const CLKRS_SIGNAL_EXIT_BASE: i64 = 128 // shell signal-exit convention 112const CLKRS_DISP: i64 = 0 113const CLKRS_STARVED: i64 = 1 114const CLKRS_LATE: i64 = 2 115const CLKRS_MAXMISSED: i64 = 3 116const CLKRS_STARVIDX: i64 = 4 117const CLKRS_MAXEXEC: i64 = 5 118const CLKRS_FOREGROUND_EXEC: i64 = 6 119const CLKRS_ERRORS: i64 = 7 120const CLKRS_N: i64 = 14 121const CLKRS_FATAL: i64 = 8 122const CLKRS_LOST_INDEX: i64 = 9 123const CLKRS_LOST_PID: i64 = 10 124const CLKRS_LOST_WAIT: i64 = 11 125const CLKRS_FOREGROUND_DISP: i64 = 12 126const CLKRS_KNOWN_OWNED: i64 = 13 127const CLKRS_FATAL_RC: i64 = 0 - 3 128const CLKRS_WAIT_RUNNING: i64 = 0 129const CLKRS_WAIT_REAPED: i64 = 1 130const CLKRS_WAIT_INTERRUPTED: i64 = 2 131const CLKRS_WAIT_LOST: i64 = 3 132const CLKRS_OUT_ERRORS: i64 = 12 133const CLKRS_OUT_FATAL: i64 = 13 134const CLKRS_OUT_LOST_INDEX: i64 = 14 135const CLKRS_OUT_LOST_PID: i64 = 15 136const CLKRS_OUT_LOST_WAIT: i64 = 16 137const CLKRS_OUT_KNOWN_OWNED: i64 = 17 138const CLKRS_OUT_N: i64 = 18 139 140func clk_heavy_listed(conf: *u8, n: i64, name: *u8) -> i64 { 141 if n <= 0 { return 0 } 142 var nl: i64 = 0 143 while name[nl] != (0 as u8) { nl = nl + 1 } 144 if nl == 0 { return 0 } 145 var i: i64 = 0 146 var hit: i64 = 0 147 while i < n { 148 var e: i64 = i 149 var eol: i64 = 0 150 while eol == 0 { 151 if e >= n { eol = 1 } else { if conf[e] == (10 as u8) { eol = 1 } else { e = e + 1 } } 152 } 153 var len: i64 = e - i 154 if len > 0 { if conf[i + len - 1] == (13 as u8) { len = len - 1 } } 155 if len == nl { if hit == 0 { 156 var k: i64 = 0 157 var same: i64 = 1 158 while k < nl { if conf[i + k] != name[k] { same = 0; k = nl } else { k = k + 1 } } 159 if same == 1 { hit = 1 } 160 } } 161 i = e + 1 162 } 163 return hit 164} 165// PURE: how long a deferred heavy job waits before EDF may pick it again -- a fraction of its own 166// period, clamped so a 60 s beat is not busy-polled and a daily beat is not parked for hours. 167func clk_defer_secs(interval: i64) -> i64 { 168 var d: i64 = interval / CLK_DEFER_DIV 169 if d < CLK_DEFER_MIN_S { d = CLK_DEFER_MIN_S } 170 if d > CLK_DEFER_MAX_S { d = CLK_DEFER_MAX_S } 171 return d 172} 173// PURE: the decision. heavy=1 AND budget==0 -> DEFER. budget<0 is UNOBSERVABLE (the ruler could not 174// read /proc/stat) and must NOT defer: abstain toward the pre-change behaviour. A light job never defers. 175func clk_storm_defer(heavy: i64, budget: i64) -> i64 { 176 if heavy != 1 { return 0 } 177 if budget == 0 { return 1 } 178 return 0 179} 180// The live measurement, composed from the shared ruler with its OWN calibration constants, so the 181// clock refuses on exactly the D-state line the build gate refuses on. ioa[0]=ncpu ioa[1]=procs_blocked. 182func clk_storm_budget(ioa: *i64) -> i64 { 183 if ioa_measure(ioa) != 0 { return IOA_UNREADABLE } 184 return ioa_spawn_budget(ioa[0], ioa[1], IOA_BLOCKED_PER_CPU, IOA_RESERVE_SLOTS) 185} 186 187func clk_slot(names: *u8, i: i64) -> *u8 { return ((names as i64) + i*CLK_NAMEW) as *u8 } 188func clk_streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while a[i]!=(0 as u8){ if a[i]!=b[i]{return 0} i=i+1 } if b[i]!=(0 as u8){return 0} return 1 } 189 190func clk_find(names: *u8, n: i64, name: *u8) -> i64 { var i: i64=0; while i<n { if clk_streq(clk_slot(names,i), name)==1 { return i } i=i+1 } return 0-1 } 191 192// REGISTER a periodic job = add a divider off the one clock. Idempotent: re-registering the same name is a no-op 193// (so capabilities can declare their job every boot without ever creating a duplicate pulse). returns 1 if added. 194func clk_register(names: *u8, organs: *u8, intervals: *i64, next_due: *i64, np: *i64, name: *u8, organ: *u8, interval: i64) -> i64 { 195 let n: i64 = np[0] 196 // A re-registration whose ORGAN or INTERVAL differs must LAND. This returned 0 for both 197 // "already present, identical" and "already present, DIFFERENT", so every correction was a 198 // silent no-op. Measured over the whole clockjobs- plane 2026-08-07: four names carry different 199 // commands across rows and the live beat was the OLD one in every case -- segguard still ran 200 // nx_segguard.sh while nx_segsweep.elf had been re-registered three times; lmexport, stalesweep 201 // and ddqbeat likewise still ran their .cron.sh. A shell-to-organ migration of four beats was 202 // written and never took effect, and nothing could see it because 0 was the only signal. 203 // An IDENTICAL re-declaration stays a no-op, so capabilities may still declare their job every 204 // boot without churn. Returns 0 unchanged, 2 updated. 205 // ★★★★★★ IDEMPOTENCE AND IMMUTABILITY ARE NOT THE SAME PROPERTY: AN IDEMPOTENT-BY-NAME 206 // REGISTRAR MAKES THE FIRST WRITE PERMANENT AND EVERY LATER CORRECTION A SILENT NO-OP. 207 let ex: i64 = clk_find(names, n, name) 208 if ex >= 0 { 209 var iv0: i64 = interval 210 if iv0 < 1 { iv0 = 1 } 211 var eo: *u8 = clk_slot(organs, ex) 212 var same: i64 = 1 213 var q: i64 = 0 214 var st: i64 = 0 215 while st == 0 { 216 let a: i64 = eo[q] as i64 217 let b: i64 = organ[q] as i64 218 if a != b { same = 0; st = 1 } 219 if st == 0 { 220 if a == 0 { st = 1 } 221 if a != 0 { 222 q = q + 1 223 if q >= CLK_NAMEW { st = 1 } 224 } 225 } 226 } 227 if intervals[ex] != iv0 { same = 0 } 228 if same == 1 { return 0 } 229 var w: i64 = 0 230 while w < CLK_NAMEW - 1 { 231 if organ[w] == (0 as u8) { eo[w] = 0 as u8; w = CLK_NAMEW } else { eo[w] = organ[w]; w = w + 1 } 232 } 233 if w == CLK_NAMEW - 1 { eo[w] = 0 as u8 } 234 intervals[ex] = iv0 235 next_due[ex] = iv0 236 return 2 237 } 238 if n >= CLK_MAXJOBS { 239 // A CAP REACHED IN SILENCE BECOMES A REGISTRATION NOBODY KNOWS WAS DROPPED (2026-08-19: 240 // measured 128/128 with every over-cap path a quiet return). Say WHICH row was refused. 241 let cm: *u8 = "CLOCK-CAP-DROP register refused at CLK_MAXJOBS: " as *u8 242 var cl: i64 = 0 243 while cm[cl] != (0 as u8) { cl = cl + 1 } 244 sys_write(1, cm, cl) 245 var nl2: i64 = 0 246 while name[nl2] != (0 as u8) { nl2 = nl2 + 1 } 247 sys_write(1, name, nl2) 248 sys_write(1, "\n" as *u8, 1) 249 return 0 250 } 251 var d: *u8 = clk_slot(names, n); var i: i64=0 252 while i<CLK_NAMEW-1 { if name[i]==(0 as u8){ d[i]=0 as u8; i=CLK_NAMEW } else { d[i]=name[i]; i=i+1 } } 253 if i==CLK_NAMEW-1 { d[i]=0 as u8 } 254 var e: *u8 = clk_slot(organs, n); var j: i64=0 255 while j<CLK_NAMEW-1 { if organ[j]==(0 as u8){ e[j]=0 as u8; j=CLK_NAMEW } else { e[j]=organ[j]; j=j+1 } } 256 if j==CLK_NAMEW-1 { e[j]=0 as u8 } 257 var iv: i64 = interval; if iv < 1 { iv = 1 } 258 intervals[n] = iv; next_due[n] = iv; np[0] = n + 1 259 return 1 260} 261 262// THE DISPATCHER: advance to logical tick `now`; mark + return how many jobs are DUE (next_due<=now), advancing 263// each due job's next_due by its interval (catch-up safe: a job never fires more than once for a missed window). 264func clk_tick(intervals: *i64, next_due: *i64, n: i64, now: i64, fired: *i64) -> i64 { 265 var count: i64 = 0; var i: i64 = 0 266 while i < n { 267 if next_due[i] <= now { 268 fired[i] = 1; count = count + 1 269 while next_due[i] <= now { next_due[i] = next_due[i] + intervals[i] } // re-arm past now (no runaway on a skipped tick) 270 } else { fired[i] = 0 } 271 i = i + 1 272 } 273 return count 274} 275 276// THE FUNCTIONAL DISPATCH: advance to `now` and actually RUN each due job by fork+exec of its organ elf path 277// (the same fork+exec idiom nx_god_pulse / nx_aw_hostctl use). One dispatcher runs N jobs at their divided rates; 278// there is NO per-job daemon. Parent waits each child so a slow job can't be lost (a real scheduler can make this 279// bounded-concurrent / fire-and-forget). `organs` is a parallel slot array (organ[i] = an executable path). Returns 280// the number of jobs dispatched this tick. 281func clk_dispatch_run(organs: *u8, intervals: *i64, next_due: *i64, n: i64, now: i64, fired: *i64) -> i64 { 282 var count: i64 = 0; var i: i64 = 0 283 while i < n { 284 if next_due[i] <= now { 285 fired[i] = 1 286 while next_due[i] <= now { next_due[i] = next_due[i] + intervals[i] } // re-arm past now (catch-up safe) 287 let path: *u8 = clk_slot(organs, i) 288 __syscall(90, path as i64, 0x1ed, 0, 0, 0, 0) // chmod 0755 first: recv-shipped organs aren't reliably +x (HC_MGMT_CMD chmods for the same reason) -> without this execve fails 127 = silent no-dispatch 289 let pid: i64 = sys_fork() 290 if pid == 0 { 291 let argv: *i64 = sys_mmap(32) as *i64; argv[0] = path as i64; argv[1] = 0 292 let envp: *i64 = sys_mmap(16) as *i64; envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64; envp[1] = 0 293 sys_execve(path, argv, envp) 294 sys_exit(127) 295 } 296 let st: *i64 = sys_mmap(16) as *i64; st[0] = 0 297 sys_wait4(pid, st, 0) 298 let raw: i64 = st[0]; let sig: i64 = raw & 0x7f; let code: i64 = (raw >> 8) & 0xff 299 // HONEST count: a job whose organ failed to exec (child exit 127 = missing/broken organ) did NOT run, 300 // so it is not counted as dispatched (it gets fired[]=1 so a caller can flag it, mirroring the 301 // publisher's dead-letter). A crash (signalled) or any other exit means the organ DID run. 302 clk_actlog(path, code) 303 if sig != 0 { count = count + 1 } else { if code != 127 { count = count + 1 } } 304 } else { fired[i] = 0 } 305 i = i + 1 306 } 307 return count 308} 309 310// TICKLESS run (sched_tickless lesson): from start_tick, run up to maxbeats beats, but before each beat SLEEP 311// exactly until the MINIMUM next_due across all jobs -- skipping every idle tick -- then dispatch the due organs. 312// Advances next_due[] in place (the caller persists for resume). out[0..3] = beats, dispatches, skipped_idle, 313// final_tick. ONE implementation, shared by the driver (nx_clock_tickless) and its gate (no parallel copy). 314func clk_run_tickless(organs: *u8, intervals: *i64, next_due: *i64, n: i64, start_tick: i64, maxbeats: i64, tick_ms: i64, out: *i64) -> i64 { 315 let fired: *i64 = sys_mmap(CLK_MAXJOBS*8) as *i64 316 var T: i64 = start_tick; var beats: i64 = 0; var disp: i64 = 0; var skipped: i64 = 0 317 while beats < maxbeats { 318 var minnd: i64 = next_due[0]; var i: i64 = 1 319 while i < n { if next_due[i] < minnd { minnd = next_due[i] } i = i + 1 } 320 if minnd <= T { minnd = T + 1 } 321 let skip: i64 = minnd - T 322 if skip > 1 { skipped = skipped + (skip - 1) } 323 let totms: i64 = skip * tick_ms 324 let ts: *i64 = sys_mmap(16) as *i64; ts[0] = totms / 1000; ts[1] = (totms - (totms/1000)*1000) * CLK_MAGIC_1000000 325 __syscall(35, ts as i64, 0, 0, 0, 0, 0) // nanosleep until the next due event (tickless) 326 T = minnd 327 disp = disp + clk_dispatch_run(organs, intervals, next_due, n, T, fired) 328 beats = beats + 1 329 } 330 out[0] = beats; out[1] = disp; out[2] = skipped; out[3] = T 331 return disp 332} 333 334// STATUS helpers (the consolidation payoff: ONE place shows every periodic job). clk_due_in = ticks until a job 335// fires (<=0 means due now). clk_twin_ok = 1 iff the job's organ elf actually exists (a 0 means it is registered 336// but un-blessed -> the dispatcher would honestly report it didn't run -> surface it in status, do not hide it). 337func clk_due_in(next_due_i: i64, now: i64) -> i64 { return next_due_i - now } 338func clk_twin_ok(organ: *u8) -> i64 { let fd: i64 = sys_openat_rd(organ); if fd < 0 { return 0 } sys_close(fd); return 1 } 339 340func clk_itoa(buf: *u8, o: i64, v: i64) -> i64 { var w: i64=o; var m: i64=v; if m==0{buf[w]=48 as u8;return w+1} if m<0{buf[w]=45 as u8;w=w+1;m=0-m} let t:*u8=sys_mmap(24); var k:i64=0; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var j:i64=0; while j<k{buf[w]=t[k-1-j];w=w+1;j=j+1} return w } 341 342// persist the registry (the ONE crontab): name<TAB>interval<TAB>next_due per line. atomic via tmp+rename. 343func clk_msgcat(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 } 344func clk_msgnum(d: *u8, o: i64, v: i64) -> i64 { 345 var x: i64 = o 346 var m: i64 = v 347 if m < 0 { d[x] = 45 as u8; x = x + 1; m = 0 - m } 348 if m == 0 { d[x] = 48 as u8; return x + 1 } 349 let t: *u8 = sys_mmap(24) 350 var k: i64 = 0 351 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 352 var j: i64 = 0 353 while j < k { d[x] = t[k - 1 - j]; x = x + 1; j = j + 1 } 354 sys_munmap(t, 24) 355 return x 356} 357// ADOPTION VISIBILITY (2026-08-07). The clock captured per-job stdout to logs/<job>.log but wrote 358// NOTHING to knowledge/status/actlog.jrnl -- the ledger nx_catalog reads to decide INVOKED. 359// MEASURED: 19 REGISTERED TOOLS ARE CLOCK-DRIVEN, so every one of them could ONLY EVER report 360// REGISTERED-DARK, callable and authorised and NEVER RUN, no matter how often it actually ran. The 361// estate adoption headline was structurally blind to its entire scheduled population -- not wrong 362// about a few organs, blind to a whole class. 363// The row carries the ORGAN command, NOT the job name, because nx_catalog matches the ORGAN via a 364// substring scan of the ledger and the two names differ (job raidwatch vs organ nx_raidwatch). 365// FAIL-OPEN BY CONSTRUCTION: if the ledger cannot be opened this returns immediately and the beat is 366// untouched. Telemetry must never be able to stop the clock -- the same rule the per-job log capture 367// already follows. Tabs and newline are written as BYTES so no source escape can be mangled. 368func clk_actlog(organ: *u8, code: i64) -> i64 { 369 var fd: i64 = sys_openat_append("knowledge/status/actlog.jrnl" as *u8, 420) 370 if fd < 0 { fd = sys_openat_append("/volume1/homes/elderwesto/nishihost/knowledge/status/actlog.jrnl" as *u8, 420) } 371 if fd < 0 { return 0 } 372 let ln: *u8 = sys_mmap(CLK_NAMEW + 128) // composes the ORGAN slot + <=56 fixed bytes; DERIVED from the slot so a widened command can never outgrow the ledger line 373 var o: i64 = clk_msgnum(ln, 0, sys_now_realtime_sec()) 374 ln[o] = 9 as u8; o = o + 1 375 o = clk_msgcat(ln, o, "clock" as *u8) 376 ln[o] = 9 as u8; o = o + 1 377 o = clk_msgcat(ln, o, organ) 378 ln[o] = 9 as u8; o = o + 1 379 o = clk_msgcat(ln, o, "run" as *u8) 380 ln[o] = 9 as u8; o = o + 1 381 if code == 0 { o = clk_msgcat(ln, o, "ok" as *u8) } else { o = clk_msgcat(ln, o, "fail" as *u8) } 382 ln[o] = 9 as u8; o = o + 1 383 o = clk_msgcat(ln, o, "clockjob lane=clock exit=" as *u8) 384 o = clk_msgnum(ln, o, code) 385 ln[o] = 10 as u8; o = o + 1 386 sys_write(fd, ln, o) 387 sys_close(fd) 388 sys_munmap(ln, CLK_NAMEW + 128) 389 return 0 390} 391func clk_save(path: *u8, names: *u8, organs: *u8, intervals: *i64, next_due: *i64, n: i64) -> i64 { 392 // ---- RELOAD-MERGE BEFORE SAVE (2026-07-31, id 1785559048) ------------------------------------------- 393 // PROVEN CLOBBER, by controlled experiment not inference: a well-formed row appended to the registry via 394 // the sovereign write path returned OK and read back present IMMEDIATELY -- and was GONE 75 seconds later, 395 // with no error, no log and no rejection. Reproduced twice. Cause is right here: this function serialized 396 // ONLY the caller's in-memory set, so the registry could contain nothing but what THIS process happened to 397 // load, and every row added between our load and our save was erased by the atomic rename below. 398 // WHY IT MATTERED: it silently closed what was then the only working extension point for scheduling. 399 // (⚠A CLAIM HERE WAS STALE AND IS RETRACTED 2026-08-03: this comment used to assert the clockjobs- 400 // plane was "retired/dead". MEASURED FALSE -- the live tickless clock merges that plane every window 401 // and rows registered via `nx_store_put knowledge/store/clockjobs- put <actor> <name> <interval> 402 // <organ>` demonstrably arm and fire. The plane IS the sanctioned add lane.) 403 // THE REMEDY IS THE ONE sts_append_row ALREADY APPLIED TO THE SEG-STORE: re-read the file we are about to 404 // overwrite and keep whatever we did not know about. Rows WE hold win outright (ours carry the advanced 405 // next_due); a row we have never seen is appended verbatim, keeping its own next_due so it arms exactly 406 // when its author intended instead of being silently re-armed or dropped. 407 // u00e2u02dcu2026A WRITER THAT SERIALIZES ONLY ITS OWN MEMORY CANNOT COEXIST WITH ANY OTHER WRITER. 408 // u00e2u02dcu2026AN EXTENSION POINT THAT SILENTLY DISCARDS EXTENSIONS IS A CLOSED SYSTEM WEARING OPEN DOCUMENTATION. 409 var nn: i64 = n 410 let rb: *u8 = sys_mmap(CLK_MAXJOBS*CLK_ROWW) 411 let nm2: *u8 = sys_mmap(CLK_NAMEW) 412 let og2: *u8 = sys_mmap(CLK_NAMEW) 413 let rfd: i64 = sys_openat_rd(path) 414 if rfd >= 0 { 415 var rn: i64 = 0 416 var rr: i64 = 1 417 while rr > 0 { 418 rr = sys_read(rfd, ((rb as i64) + rn) as *u8, CLK_MAXJOBS*CLK_ROWW - rn) 419 if rr > 0 { rn = rn + rr } 420 } 421 sys_close(rfd) 422 var p: i64 = 0 423 var ls: i64 = 0 424 while p <= rn { 425 var eol: i64 = 0 426 if p == rn { eol = 1 } else { if rb[p] == (10 as u8) { eol = 1 } } 427 if eol == 1 { 428 if p > ls { 429 var f: i64 = 0 430 var q: i64 = ls 431 var iv2: i64 = 0 432 var nd2: i64 = 0 433 var w2: i64 = 0 434 var g2: i64 = 0 435 while q < p { 436 if rb[q] == (9 as u8) { f = f + 1 } else { 437 if f == 0 { if w2 < CLK_NAMEW - 1 { nm2[w2] = rb[q]; w2 = w2 + 1 } } 438 else { if f == 1 { if rb[q] >= (48 as u8) { if rb[q] <= (57 as u8) { iv2 = iv2*10 + ((rb[q] - (48 as u8)) as i64) } } } 439 else { if f == 2 { if rb[q] >= (48 as u8) { if rb[q] <= (57 as u8) { nd2 = nd2*10 + ((rb[q] - (48 as u8)) as i64) } } } 440 else { if g2 < CLK_NAMEW - 1 { og2[g2] = rb[q]; g2 = g2 + 1 } } } } 441 } 442 q = q + 1 443 } 444 nm2[w2] = 0 as u8 445 og2[g2] = 0 as u8 446 if w2 > 0 { if g2 > 0 { if clk_find(names, nn, nm2) < 0 { if nn < CLK_MAXJOBS { 447 var d2: *u8 = clk_slot(names, nn) 448 var a2: i64 = 0 449 while a2 < w2 { d2[a2] = nm2[a2]; a2 = a2 + 1 } 450 d2[w2] = 0 as u8 451 var e2: *u8 = clk_slot(organs, nn) 452 var b2: i64 = 0 453 while b2 < g2 { e2[b2] = og2[b2]; b2 = b2 + 1 } 454 e2[g2] = 0 as u8 455 if iv2 < 1 { iv2 = 1 } 456 intervals[nn] = iv2 457 next_due[nn] = nd2 458 nn = nn + 1 459 } } } } 460 } 461 ls = p + 1 462 } 463 p = p + 1 464 } 465 } 466 let buf: *u8 = sys_mmap(CLK_MAXJOBS*CLK_ROWW); var o: i64 = 0 467 var i: i64 = 0 468 while i < nn { 469 let nm: *u8 = clk_slot(names, i); var k: i64=0; while nm[k]!=(0 as u8){ buf[o]=nm[k]; o=o+1; k=k+1 } 470 buf[o]=9 as u8; o=o+1; o = clk_itoa(buf, o, intervals[i]); buf[o]=9 as u8; o=o+1; o = clk_itoa(buf, o, next_due[i]); buf[o]=9 as u8; o=o+1 471 let og: *u8 = clk_slot(organs, i); var g: i64=0; while og[g]!=(0 as u8){ buf[o]=og[g]; o=o+1; g=g+1 } 472 buf[o]=10 as u8; o=o+1 473 i = i + 1 474 } 475 sys_mkdir("knowledge" as *u8, 0x1ed); sys_mkdir("knowledge/sched" as *u8, 0x1ed) 476 let tmp: *u8 = sys_mmap(256); var t: i64=0; let pp: *u8 = path; while pp[t]!=(0 as u8){ tmp[t]=pp[t]; t=t+1 } tmp[t]=46 as u8; tmp[t+1]=116 as u8; tmp[t+2]=109 as u8; tmp[t+3]=112 as u8; tmp[t+4]=0 as u8 // path + ".tmp" 477 let fd: i64 = sys_openat_wr(tmp, 0x1a4); if fd<0 { return 0 } sys_write(fd, buf, o); sys_close(fd) 478 __syscall(82, tmp as i64, path as i64, 0, 0, 0, 0) // atomic rename .tmp -> registry 479 return nn 480} 481 482// ---- PLANE-NATIVE STATE PERSISTENCE (2026-08-03, debts 1784828927 + 1784868625: the operator law is 483// planes, never tsv). The mutable schedule state (name·interval·next_due·organ per row, the thing the 484// clock resumes from) lives in a seg-store plane, written once per window via sts_seed. The plane is 485// CLOCK-EXCLUSIVE by doctrine (external adds ride the separate clockjobs- ADD plane, which the clock 486// is read-only on) -- but the same reload-merge that saved the tsv from the 1785559048 clobber is kept 487// here: rows we have never seen are preserved with their own next_due, because A WRITER THAT 488// SERIALIZES ONLY ITS OWN MEMORY CANNOT COEXIST WITH ANY OTHER WRITER. -------------------------------- 489 490// load schedule state from the plane; same 4-col row grammar as the legacy file. returns #jobs. 491func clk_load_plane(prefix: *u8, names: *u8, organs: *u8, intervals: *i64, next_due: *i64, np: *i64) -> i64 { 492 np[0] = 0 493 let data: *u8 = sys_mmap(CLK_MAXJOBS*CLK_ROWW) 494 let dn: i64 = sts_load(prefix, data, CLK_MAXJOBS*CLK_ROWW) 495 if dn <= 0 { return 0 } 496 var i: i64=0; var ls: i64=0; var n: i64=0 497 while i < dn { 498 if data[i] == (10 as u8) { 499 if i > ls { if n < CLK_MAXJOBS { 500 let line: *u8 = ((data as i64)+ls) as *u8; let ll: i64 = i-ls 501 let d: *u8 = clk_slot(names, n); let e: *u8 = clk_slot(organs, n) 502 var f: i64=0; var p: i64=0; var iv: i64=0; var nd: i64=0; var w: i64=0; var g: i64=0 503 while p < ll { 504 if line[p] == (9 as u8) { f = f + 1 } 505 else { 506 if f == 0 { if w < CLK_NAMEW-1 { d[w]=line[p]; w=w+1 } } 507 else { if f == 3 { if g < CLK_NAMEW-1 { e[g]=line[p]; g=g+1 } } 508 else { if line[p] >= (48 as u8) { if line[p] <= (57 as u8) { 509 let dig: i64 = (line[p]-(48 as u8)) as i64 510 if f == 1 { iv = iv*10 + dig } else { nd = nd*10 + dig } 511 } } } } 512 } 513 p = p + 1 514 } 515 d[w] = 0 as u8; e[g] = 0 as u8 516 if w > 0 { if g > 0 { 517 intervals[n]=iv; next_due[n]=nd; n=n+1 518 } } 519 } } 520 ls = i + 1 521 } 522 i = i + 1 523 } 524 np[0] = n 525 // B-hunk (2026-08-22): this buffer is per-call and was never freed -- at CLK_ROWW=2112 that is a 526 // 1,081,344 B address-space leak per window on a daemon whose life is 120 windows. Everything it 527 // held was COPIED into the caller's slot arrays above, so the unmap is safe by construction. 528 sys_munmap(data, CLK_MAXJOBS*CLK_ROWW) 529 return n 530} 531 532// persist schedule state to the plane (reload-merge first, then ONE whole-plane sts_seed commit). 533// Rows WE hold win (ours carry the advanced next_due); unknown rows are preserved verbatim. 534func clk_save_plane(prefix: *u8, names: *u8, organs: *u8, intervals: *i64, next_due: *i64, n: i64) -> i64 { 535 var nn: i64 = n 536 let rnames: *u8 = sys_mmap(CLK_MAXJOBS*CLK_NAMEW) 537 let rorgs: *u8 = sys_mmap(CLK_MAXJOBS*CLK_NAMEW) 538 let riv: *i64 = sys_mmap(CLK_MAXJOBS*8) as *i64 539 let rnd: *i64 = sys_mmap(CLK_MAXJOBS*8) as *i64 540 let rnp: *i64 = sys_mmap(8) as *i64 541 clk_load_plane(prefix, rnames, rorgs, riv, rnd, rnp) 542 var ri: i64 = 0 543 while ri < rnp[0] { 544 if clk_find(names, nn, clk_slot(rnames, ri)) < 0 { if nn < CLK_MAXJOBS { 545 var d2: *u8 = clk_slot(names, nn); let sm: *u8 = clk_slot(rnames, ri) 546 var a2: i64 = 0 547 while a2 < CLK_NAMEW-1 { if sm[a2]==(0 as u8){ d2[a2]=0 as u8; a2=CLK_NAMEW } else { d2[a2]=sm[a2]; a2=a2+1 } } 548 if a2 == CLK_NAMEW-1 { d2[a2]=0 as u8 } 549 var e2: *u8 = clk_slot(organs, nn); let so: *u8 = clk_slot(rorgs, ri) 550 var b2: i64 = 0 551 while b2 < CLK_NAMEW-1 { if so[b2]==(0 as u8){ e2[b2]=0 as u8; b2=CLK_NAMEW } else { e2[b2]=so[b2]; b2=b2+1 } } 552 if b2 == CLK_NAMEW-1 { e2[b2]=0 as u8 } 553 intervals[nn] = riv[ri]; next_due[nn] = rnd[ri]; nn = nn + 1 554 } } 555 ri = ri + 1 556 } 557 let buf: *u8 = sys_mmap(CLK_MAXJOBS*CLK_ROWW); var o: i64 = 0 558 var i: i64 = 0 559 while i < nn { 560 let nm: *u8 = clk_slot(names, i); var k: i64=0; while nm[k]!=(0 as u8){ buf[o]=nm[k]; o=o+1; k=k+1 } 561 buf[o]=9 as u8; o=o+1; o = clk_itoa(buf, o, intervals[i]); buf[o]=9 as u8; o=o+1; o = clk_itoa(buf, o, next_due[i]); buf[o]=9 as u8; o=o+1 562 let og: *u8 = clk_slot(organs, i); var g: i64=0; while og[g]!=(0 as u8){ buf[o]=og[g]; o=o+1; g=g+1 } 563 buf[o]=10 as u8; o=o+1 564 i = i + 1 565 } 566 sts_seed(prefix, buf, o) 567 // B-hunk (2026-08-22): free the per-call buffers -- at the widened CLK_NAMEW/CLK_ROWW these 568 // mappings are ~3.2 MB per save (1-2 saves/window) of address space the GC-free substrate 569 // otherwise leaks until the 120-window life recycle. All contents are already committed/copied. 570 sys_munmap(rnames, CLK_MAXJOBS*CLK_NAMEW) 571 sys_munmap(rorgs, CLK_MAXJOBS*CLK_NAMEW) 572 sys_munmap(riv as *u8, CLK_MAXJOBS*8) 573 sys_munmap(rnd as *u8, CLK_MAXJOBS*8) 574 sys_munmap(rnp as *u8, 8) 575 sys_munmap(buf, CLK_MAXJOBS*CLK_ROWW) 576 return nn 577} 578 579// THE ONE STATE LOADER for the live clock: plane first; if the plane is empty AND a legacy tsv 580// exists, load it (the one-time migration path) -- the caller's next clk_save_plane completes the 581// cutover. Returns 0=loaded-from-plane, 1=migrated-from-legacy, -1=nothing anywhere. 582func clk_load_state(prefix: *u8, legacy: *u8, names: *u8, organs: *u8, intervals: *i64, next_due: *i64, np: *i64) -> i64 { 583 if clk_load_plane(prefix, names, organs, intervals, next_due, np) > 0 { return 0 } 584 if clk_load(legacy, names, organs, intervals, next_due, np) > 0 { return 1 } 585 return 0 - 1 586} 587 588// MERGE new job declarations from an nx_store PLANE (off-tsv, additive) into the in-memory registry. 589// THE CLOBBER FIX: the clock is READ-ONLY on this plane -- external adds go via 590// `nx_store_put <plane> put <actor> <name> <interval> <organ>` (upsert-by-name, additive) and can NEVER be 591// clobbered by the clock's own (now clock-private) tsv save. Plane row cols (TAB): 0=name 1=interval(sec) 2=organ. 592// A job already present (by name) is skipped (idempotent -- re-merge is a no-op). New jobs arm at base_tick+interval. 593// Returns the number of NEW jobs added this call. 594func clk_merge_store(prefix: *u8, names: *u8, organs: *u8, intervals: *i64, next_due: *i64, np: *i64, base_tick: i64) -> i64 { 595 let buf: *u8 = sys_mmap(CLK_MAXJOBS*CLK_ROWW) 596 let dn: i64 = sts_load(prefix, buf, CLK_MAXJOBS*CLK_ROWW) 597 if dn <= 0 { return 0 } 598 // HOISTED: allocating this inside the row loop would leak a page per row (the nx_ts_lumadiff scar). 599 let msg: *u8 = sys_mmap(CLK_ROWW) 600 // DESIRED interval per job index, 0 = not declared this pass. mmap returns zeroed pages. 601 let want: *i64 = sys_mmap(CLK_MAXJOBS*8) as *i64 602 // DESIRED ORGAN per job index (2026-08-07). This reconciler recorded only the desired INTERVAL, so a 603 // declaration could re-PERIOD a job but never re-TARGET it. MEASURED: I declared segguard 600s -> 604 // nx_segsweep.elf and the plane came back interval=600 organ=nx_segguard.sh -- the cadence moved and the 605 // command did not, which is strictly WORSE than either alone: it put the 717s shell script back on a 606 // 600s period it cannot meet. Caught only because I read the organ column back instead of trusting the 607 // interval that DID change. 608 // (STAR)A RECONCILER THAT SYNCS ONE FIELD OF A ROW WILL SILENTLY DESYNC THE OTHERS -- AND A PARTIAL 609 // RECONCILE IS MORE DANGEROUS THAN NONE, BECAUSE THE FIELD THAT MOVED PROVES IT WORKED. 610 let worg: *u8 = sys_mmap(CLK_MAXJOBS*CLK_NAMEW) 611 // B-hunk: ONE pair of row-scratch buffers for the whole scan (see the hoist note at `msg` above). 612 let nmrow: *u8 = sys_mmap(CLK_NAMEW) 613 let ogrow: *u8 = sys_mmap(CLK_NAMEW) 614 var added: i64 = 0 615 var reper: i64 = 0 616 var i: i64=0; var ls: i64=0 617 while i < dn { 618 if buf[i] == (10 as u8) { 619 if i > ls { 620 let line: *u8 = ((buf as i64)+ls) as *u8; let ll: i64 = i-ls 621 let nm: *u8 = nmrow; let og: *u8 = ogrow // B-hunk: hoisted buffers (were per-row mmaps = a page per desired row per window; the HOISTED note at `msg` above already names the class) 622 var f: i64=0; var p: i64=0; var iv: i64=0; var w: i64=0; var g: i64=0 623 while p < ll { 624 if line[p]==(9 as u8) { f=f+1 } 625 else { 626 if f==0 { if w<CLK_NAMEW-1 { nm[w]=line[p]; w=w+1 } } 627 else { if f==1 { if line[p]>=(48 as u8) { if line[p]<=(57 as u8) { iv=iv*10+((line[p]-(48 as u8)) as i64) } } } 628 else { if f==2 { if g<CLK_NAMEW-1 { og[g]=line[p]; g=g+1 } } } } 629 } 630 p=p+1 631 } 632 nm[w]=0 as u8; og[g]=0 as u8 633 // MALFORMED-ROW GUARD (2026-08-03, debt 1784604739): a row lacking a name or an organ 634 // (e.g. a put that dropped an arg) must be SEEN, never silently skipped -- but never 635 // registered either (a nameless job / organless exec is garbage in the dispatcher). 636 if w == 0 { sts_werr("clk_merge_store: MALFORMED plane row (empty name) SKIPPED\n" as *u8) } 637 if w > 0 { if g == 0 { sts_werr("clk_merge_store: MALFORMED plane row (no organ) SKIPPED\n" as *u8) } } 638 if w > 0 { if g > 0 { 639 if iv < 1 { iv = 1 } 640 let n: i64 = np[0] 641 // ---- RECONCILE, NOT JUST ADD (2026-08-06) ---------------------------------------- 642 // MEASURED DEFECT: this merge was ADD-ONLY -- a row whose name already existed was 643 // silently skipped -- and clocksched- (the runtime state) is rewritten by the clock 644 // every window. So there was NO SUPPORTED PATH TO RE-PERIOD OR REMOVE A CLOCK JOB: 645 // you could add one and never change it again. That is not a hypothetical. Three jobs 646 // were stuck at pathological cadences -- bootstrap-gate at 60s and worldgen-gate at 647 // 120s are GATES (test suites) running 60-360x more often than every other gate in the 648 // plane (p384kat/tls12prf 21600, uigensitegate 86400), and together with intakekeep 649 // they are 60 of the ~273 dispatches/window = 22pct of ALL demand on a clock measured 650 // at 51 completed vs 273 demanded (5.4x oversubscribed, 31 jobs STARVED per window). 651 // Whoever added them could not fix them, and the starvation was blamed on scheduling 652 // policy for days while EDF was working exactly as designed. 653 // THE FIX IS THE ESTATE'S OWN PATTERN, USED TWICE ALREADY: cron.reg -> crontab via 654 // nx_cron_reconcile, and edge443 -> iptables via nx_edge443_reconcile.sh. The declared 655 // registry is the SSOT and the runtime state is RECONCILED toward it every window. 656 // clockjobs- is now DESIRED STATE, not an append-only inbox. 657 // SAFETY: re-arm to base_tick+iv rather than leaving the old deadline, so a re-period 658 // can never fire a herd (shortening) nor strand a job past its new period (lengthening). 659 // ★A REGISTRY YOU CAN ONLY APPEND TO IS A REGISTRY THAT ACCUMULATES ITS OWN MISTAKES. 660 let ex: i64 = clk_find(names, n, nm) 661 // RECORD the intent; do not apply it here. See the apply loop after this scan. 662 if ex >= 0 { want[ex] = iv 663 var wd: *u8 = clk_slot(worg, ex); var wk: i64 = 0 664 while wk < CLK_NAMEW-1 { if og[wk]==(0 as u8){wd[wk]=0 as u8;wk=CLK_NAMEW} else {wd[wk]=og[wk];wk=wk+1} } 665 if wk == CLK_NAMEW-1 { wd[wk] = 0 as u8 } 666 } 667 if ex < 0 { if n < CLK_MAXJOBS { 668 var d: *u8=clk_slot(names,n); var k: i64=0; while k<CLK_NAMEW-1 { if nm[k]==(0 as u8){d[k]=0 as u8;k=CLK_NAMEW} else {d[k]=nm[k];k=k+1} } if k==CLK_NAMEW-1 {d[k]=0 as u8} 669 var e: *u8=clk_slot(organs,n); var j: i64=0; while j<CLK_NAMEW-1 { if og[j]==(0 as u8){e[j]=0 as u8;j=CLK_NAMEW} else {e[j]=og[j];j=j+1} } if j==CLK_NAMEW-1 {e[j]=0 as u8} 670 if iv<1 { iv=1 } 671 intervals[n]=iv; next_due[n]=base_tick+iv; np[0]=n+1; added=added+1 672 } } 673 } } 674 } 675 ls=i+1 676 } 677 i=i+1 678 } 679 // APPLY ONCE PER JOB, AFTER EVERY ROW IS READ -- LAST DECLARATION WINS. 680 // Applying inside the row loop was the first cut and it was wrong: this plane ACCUMULATES rows 681 // (pubreconcile appears THREE times in it today), so a name carrying two differing declarations 682 // would flip-flop -- two writes and two log lines every window, forever, converging on exactly 683 // the same last-row-wins answer this loop reaches quietly. Collecting the intent and applying it 684 // once is both quieter and identical in outcome. 685 var wi: i64 = 0 686 while wi < np[0] { 687 if want[wi] > 0 { if intervals[wi] != want[wi] { 688 var mo: i64 = clk_msgcat(msg, 0, "clk_merge_store: REPERIOD " as *u8) 689 mo = clk_msgcat(msg, mo, clk_slot(names, wi)) 690 mo = clk_msgcat(msg, mo, " " as *u8) 691 mo = clk_itoa(msg, mo, intervals[wi]) 692 mo = clk_msgcat(msg, mo, "s -> " as *u8) 693 mo = clk_itoa(msg, mo, want[wi]) 694 mo = clk_msgcat(msg, mo, "s (declared in clockjobs-; re-armed at now+interval)\n" as *u8) 695 msg[mo] = 0 as u8 696 sts_werr(msg) 697 intervals[wi] = want[wi] 698 next_due[wi] = base_tick + want[wi] 699 reper = reper + 1 700 } } 701 // RE-TARGET: the organ is a field of the row too, and a declaration that names a different 702 // binary must MOVE the job, not just its cadence. Separate from the interval branch so a 703 // pure re-target (same period, new organ) is honoured. 704 if wi < np[0] { let wo: *u8 = clk_slot(worg, wi) 705 if wo[0] != (0 as u8) { if clk_streq(wo, clk_slot(organs, wi)) == 0 { 706 var mo2: i64 = clk_msgcat(msg, 0, "clk_merge_store: RETARGET " as *u8) 707 mo2 = clk_msgcat(msg, mo2, clk_slot(names, wi)) 708 mo2 = clk_msgcat(msg, mo2, " -> " as *u8) 709 mo2 = clk_msgcat(msg, mo2, wo) 710 mo2 = clk_msgcat(msg, mo2, " (declared in clockjobs-)\n" as *u8) 711 msg[mo2] = 0 as u8 712 sts_werr(msg) 713 var od: *u8 = clk_slot(organs, wi); var ok2: i64 = 0 714 while ok2 < CLK_NAMEW-1 { if wo[ok2]==(0 as u8){od[ok2]=0 as u8;ok2=CLK_NAMEW} else {od[ok2]=wo[ok2];ok2=ok2+1} } 715 if ok2 == CLK_NAMEW-1 { od[ok2] = 0 as u8 } 716 reper = reper + 1 717 } } } 718 wi = wi + 1 719 } 720 // RETURN ADDS *AND* RE-PERIODS (2026-08-06). This returned only `added`, so a caller could not tell 721 // that a RECONCILE had occurred -- reper was counted, logged and then dropped on the floor. The single 722 // caller discarded the value entirely, so widening it is safe and makes the reconcile actionable. 723 // (STAR)A COUNTER THAT IS COMPUTED, LOGGED AND NOT RETURNED IS A FACT THE CALLER CANNOT ACT ON. 724 // B-hunk: free the per-call transients (buf/msg/want/worg + the hoisted row scratch); everything 725 // they held was applied to the caller's arrays above. 726 sys_munmap(buf, CLK_MAXJOBS*CLK_ROWW) 727 sys_munmap(msg, CLK_ROWW) 728 sys_munmap(want as *u8, CLK_MAXJOBS*8) 729 sys_munmap(worg, CLK_MAXJOBS*CLK_NAMEW) 730 sys_munmap(nmrow, CLK_NAMEW) 731 sys_munmap(ogrow, CLK_NAMEW) 732 return added + reper 733} 734 735// ============================================================================================ 736// EDF / WALL-CLOCK SCHEDULING (2026-08-04, debt 1785872141 -- operator: "get the clock to sota") 737// 738// THE MEASURED DEFECT clk_run_tickless has BY CONSTRUCTION: its logical tick T advances ONLY by the 739// sleep amount (T = minnd), while REAL time also advances by however long the dispatched children 740// took (clk_dispatch_run forks and sys_wait4s EVERY job, serially, inside the beat). So logical time 741// drifts behind wall-clock time in proportion to dispatch load, and EVERY job's period silently 742// STRETCHES: measured 2026-08-04, evidencebeat (interval 21600 = "6 hours") fired ONCE in 21 HOURS 743// while the clock was demonstrably alive (tick current, window-end a=30 b=132). Even 60s netobs 744// slipped to 243s. The fast jobs pay the drift too, but a 6h job pays it 360x over. 745// u2605u2605u2605u2605u2605u2605 A SCHEDULER THAT ADVANCES ITS OWN CLOCK BY WHAT IT SLEPT -- NOT BY WHAT ELAPSED -- 746// MEASURES ITS OWN IDLENESS AND CALLS IT TIME. Every deadline it derives is then a lie under load. 747// 748// THE SOTA SHAPE (what real schedulers do; cron/systemd-timers/EDF literature all agree): 749// 1. deadlines are ABSOLUTE WALL-CLOCK instants, never a self-advanced counter 750// 2. re-read the clock AFTER every dispatch, so job runtime cannot be lost 751// 3. dispatch EARLIEST-DEADLINE-FIRST so the most-overdue job goes first (EDF is optimal for 752// meeting deadlines on one resource -- and it is exactly what stops slow-job starvation) 753// 4. catch-up without runaway: re-arm past now by WHOLE intervals, and REPORT missed periods 754// instead of pretending they happened 755// The decision math is factored into PURE functions below precisely so a gate can prove a 6-hour 756// period behaves correctly in MILLISECONDS with fabricated clock values -- a scheduler you can only 757// test by waiting 6 hours is a scheduler nobody tests. 758 759// deadlines below this are LEGACY LOGICAL TICKS (the old counter ran ~3e6; epochs are ~1.78e9), so the 760// one-time migration is unambiguous and needs no flag day. NEVER compare an epoch against a tick. 761const CLK_EPOCH_FLOOR: i64 = 1000000000 762 763// PURE: index of the overdue job with the EARLIEST deadline (EDF), or -1 if nothing is due. 764// Ties break toward the lower index = stable, so a tie can never rotate two jobs into each other's slot. 765// DEMAND vs CAPACITY (2026-08-06, measured on the live plane). EDF decides WHO runs next; it can never 766// decide HOW MANY CAN RUN. If the registry asks for more dispatches per window than the window can 767// complete, then EVERY ordering starves someone and the scheduler is merely choosing the victim -- so 768// the deficit has to be a NUMBER the clock PUBLISHES, not something inferred from a late heartbeat days 769// later. MEASURED THE DAY THIS SHIPPED: 45 jobs demanded ~273 dispatches per 1800s window while the 770// window completed 51 (~35s of real child time per dispatch) = ~5.4x oversubscribed, and 273 exceeds even 771// TLN_MAXDISPATCH=240, so the set could not be served at INFINITE speed. That is why netobs (60s period) 772// was firing at 284s+ intervals with its registry row and its elf both perfectly healthy. 773// CONSERVATIVE BY CONSTRUCTION: integer division FLOORS, and any job whose period exceeds the window 774// contributes 0. So this is a LOWER BOUND on demand -- if even this floor exceeds capacity, 775// oversubscription is PROVEN, never merely suspected. 776func clk_demand_per_window(intervals: *i64, n: i64, window_secs: i64) -> i64 { 777 var need: i64 = 0 778 var i: i64 = 0 779 while i < n { 780 if intervals[i] > 0 { need = need + (window_secs / intervals[i]) } 781 i = i + 1 782 } 783 return need 784} 785 786// PARALLEL-SAFE PICK (2026-08-06): identical to clk_edf_pick except it SKIPS jobs already IN FLIGHT. 787// THE HAZARD IT EXISTS FOR, and it is not hypothetical: a job is re-armed only AFTER its child is reaped, 788// so while a run is in progress its deadline is still in the past -- and a job whose runtime EXCEEDS its 789// period (worldgen: 289s measured against a 120s declared period) is overdue for the WHOLE of its own run. 790// Under bounded-parallel dispatch a picker without this mask would immediately dispatch a SECOND copy of 791// the exact organ already saturating the box, then a third, up to the slot cap. Two concurrent four-world 792// renders is strictly WORSE than the serial starvation parallelism was introduced to fix. 793// (STAR)PARALLELISM TURNS "ALWAYS OVERDUE" FROM A STARVATION BUG INTO A FORK BOMB -- THE BUSY MASK IS NOT 794// AN OPTIMISATION, IT IS THE CORRECTNESS CONDITION. Kept as a SEPARATE pure function so it is gate-provable 795// on fabricated inputs (T10/T11) before it is ever wired to a live dispatcher. 796func clk_edf_pick_free(deadlines: *i64, n: i64, now: i64, busy: *i64) -> i64 { 797 var best: i64 = 0 - 1 798 var bestd: i64 = 0 799 var i: i64 = 0 800 while i < n { 801 if busy[i] == 0 { 802 if deadlines[i] <= now { 803 if best < 0 { best = i; bestd = deadlines[i] } 804 else { if deadlines[i] < bestd { best = i; bestd = deadlines[i] } } 805 } 806 } 807 i = i + 1 808 } 809 return best 810} 811 812func clk_edf_pick(deadlines: *i64, n: i64, now: i64) -> i64 { 813 var best: i64 = 0 - 1 814 var bestd: i64 = 0 815 var i: i64 = 0 816 while i < n { 817 if deadlines[i] <= now { 818 if best < 0 { best = i; bestd = deadlines[i] } 819 else { if deadlines[i] < bestd { best = i; bestd = deadlines[i] } } 820 } 821 i = i + 1 822 } 823 return best 824} 825 826// PURE: soonest deadline across all jobs (what a tickless sleeper must sleep until). -1 if n==0. 827func clk_edf_next(deadlines: *i64, n: i64) -> i64 { 828 if n <= 0 { return 0 - 1 } 829 var m: i64 = deadlines[0] 830 var i: i64 = 1 831 while i < n { if deadlines[i] < m { m = deadlines[i] } i = i + 1 } 832 return m 833} 834 835// PURE: how many whole periods job i has MISSED at `now` (0 = on time / early). This is the honest 836// starvation measure -- a job 3 periods late is a 3, not a "fired". 837func clk_edf_missed(deadlines: *i64, intervals: *i64, i: i64, now: i64) -> i64 { 838 if deadlines[i] > now { return 0 } 839 var iv: i64 = intervals[i] 840 if iv < 1 { iv = 1 } 841 return (now - deadlines[i]) / iv 842} 843 844// PURE: re-arm job i past `now` by WHOLE intervals (catch-up safe: never fires twice for one missed 845// window, never drifts off-phase). Returns the periods skipped so the caller can REPORT them. 846func clk_edf_rearm(deadlines: *i64, intervals: *i64, i: i64, now: i64) -> i64 { 847 var iv: i64 = intervals[i] 848 if iv < 1 { iv = 1 } 849 var skipped: i64 = 0 850 while deadlines[i] <= now { deadlines[i] = deadlines[i] + iv; skipped = skipped + 1 } 851 if skipped > 0 { skipped = skipped - 1 } // the first advance is the fire itself, not a miss 852 return skipped 853} 854 855// PURE: one-time migration of legacy logical-tick deadlines to wall-clock instants. A tick value can 856// never be a valid epoch, so this is exact. Jobs arm at now+interval (their author's intent) rather 857// than firing a thundering herd at now. Returns how many rows were converted. 858func clk_edf_migrate(deadlines: *i64, intervals: *i64, n: i64, now: i64) -> i64 { 859 var c: i64 = 0 860 var i: i64 = 0 861 while i < n { 862 if deadlines[i] < CLK_EPOCH_FLOOR { 863 var iv: i64 = intervals[i] 864 if iv < 1 { iv = 1 } 865 deadlines[i] = now + iv 866 c = c + 1 867 } 868 i = i + 1 869 } 870 return c 871} 872 873// THE EDF WINDOW: wall-clock anchored, EDF-ordered, starvation-reporting. Runs until `maxdispatch` 874// jobs have been dispatched or `budget_secs` of REAL time is gone, sleeping only when nothing is due. 875// out[0]=dispatched out[1]=slept_secs out[2]=final_now out[3]=starved_jobs out[4]=max_lateness_secs 876// out[5]=exec_secs (real time inside children -- the number the old design silently threw away). 877// ---- IN-FLIGHT BEAT (2026-08-06) ------------------------------------------------------------- 878// PROVEN LIVE THIS SESSION, NOT HYPOTHESISED: the clock overran its own 1800s budget by 42s and 879// stopped dispatching for 475s+ while one child held it, because clk_dispatch_one waits with NO 880// deadline (debt 1786056459). sitecheck (30s period) froze; every beat in the estate went dead. 881// THE INSTRUMENT PROBLEM THAT INCIDENT EXPOSED: max_exec, starved, and the entire window-end report 882// are written AFTER clk_run_edf RETURNS. So the one failure mode that prevents returning is exactly 883// the one the report can never describe -- during the stall the newest window-end beat was 1800s old 884// and named a completely different window. 885// (STAR)AN INSTRUMENT THAT REPORTS ONLY AT COMPLETION CANNOT REPORT WHAT PREVENTS COMPLETION. 886// This beat is written BEFORE every dispatch and overwritten AFTER it, so a hang is visible the 887// moment it starts and NAMES the organ holding the scheduler. Cost is 2 file writes per dispatch, 888// negligible beside the fork+execve it brackets. 889func clk_ifw(s: *u8, dst: *u8, o: i64) -> i64 { var x: i64=o; var i: i64=0; while s[i]!=(0 as u8){dst[x]=s[i];x=x+1;i=i+1} return x } 890func clk_ifn(dst: *u8, o: i64, v: i64) -> i64 { var x: i64=o; var mm: i64=v; if mm<0{dst[x]=45 as u8;x=x+1;mm=0-mm} if mm==0{dst[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{dst[x]=t[k-1-j];x=x+1;j=j+1} return x } 891func clk_inflight(tag: *u8, organ: *u8, a: i64, b: i64) -> i64 { 892 let st: *u8 = sys_mmap(CLK_NAMEW + 128); var o: i64 = 0 // composes the ORGAN slot + <=97 fixed bytes; DERIVED from the slot (1024 was safe only by mmap page rounding once the slot widened) 893 o = clk_ifw("CLOCKBEAT " as *u8, st, o); o = clk_ifw(tag, st, o) 894 o = clk_ifw(" organ=" as *u8, st, o); o = clk_ifw(organ, st, o) 895 o = clk_ifw(" a=" as *u8, st, o); o = clk_ifn(st, o, a) 896 o = clk_ifw(" b=" as *u8, st, o); o = clk_ifn(st, o, b) 897 o = clk_ifw(" t=" as *u8, st, o); o = clk_ifn(st, o, sys_now_realtime_sec()) 898 st[o]=10 as u8; o=o+1; st[o]=0 as u8 899 let fd: i64 = sys_openat_wr("sites/nishifamily/clock_inflight.txt" as *u8, 0x1a4) 900 if fd >= 0 { sys_write(fd, st, o); sys_close(fd) } 901 return 0 902} 903 904func clk_run_edf_core(organs: *u8, names: *u8, intervals: *i64, deadlines: *i64, n: i64, maxdispatch: i64, budget_secs: i64, tick_ms: i64, out: *i64, reserved_mode: i64) -> i64 { 905 let fired: *i64 = sys_mmap(CLK_MAXJOBS*8) as *i64 906 let t0: i64 = sys_now_realtime_sec() 907 var disp: i64 = 0 908 var slept: i64 = 0 909 var starved: i64 = 0 910 var maxlate: i64 = 0 911 var execs: i64 = 0 912 var maxexec: i64 = 0 913 var maxidx: i64 = 0 - 1 914 // STARVED AXIS, NAMED (2026-08-14). Mirrors maxexec/maxidx below, which already record the worst 915 // single dispatch AND which job owned it. That law was applied to the SLOW axis and never to the 916 // STARVED axis, so out[3] shipped as a bare count. 917 var maxmissed: i64 = 0 918 var starvidx: i64 = 0 - 1 919 // STORM-AWARE DISPATCH state (2026-09-02): the heavy list is read ONCE per window (sys_read_file sizes 920 // from the file, cannot short-read; an absent conf -> hn=0 -> nothing is heavy), the ruler's scratch is 921 // allocated ONCE, and the deferral count is published in out[10]. 922 var deferred: i64 = 0 923 var assisted: i64 = 0 924 var errors: i64 = 0 925 var fatal: i64 = 0 926 let reserved_result: *i64 = sys_mmap(CLKRS_N*8) as *i64 927 let ioa: *i64 = sys_mmap(16) as *i64 928 let hlen: *i64 = sys_mmap(16) as *i64 929 hlen[0] = 0 930 let hconf: *u8 = sys_read_file(CLK_HEAVY_CONF, hlen) 931 var hn: i64 = 0 932 if (hconf as i64) != 0 { hn = hlen[0] } 933 let rlen: *i64 = sys_mmap(8) as *i64 934 var rconf: *u8 = 0 as *u8 935 if reserved_mode == 1 { rconf = sys_read_file(CLKRS_CONF,rlen) } 936 var rn: i64 = 0 937 if (rconf as i64) != 0 { rn = rlen[0] } 938 let reserved: i64 = clk_reserved_resolve(rconf,rn,names,organs,intervals,n,hconf,hn) 939 if reserved == CLKRS_INVALID { errors = errors+1; sts_werr("CLOCK-RESERVE configuration refused; legacy serial service remains, no reservation active\n" as *u8) } 940 if reserved >= 0 { sts_werr("CLOCK-RESERVE active: one exact light job, at most one extra child; foreground dispatch ceiling excludes assisted service\n" as *u8) } 941 var run: i64 = 1 942 while run == 1 { 943 // RE-READ THE CLOCK EVERY ITERATION -- this single line is the fix: child runtime is now 944 // observed, not assumed away. 945 let now: i64 = sys_now_realtime_sec() 946 if now - t0 >= budget_secs { run = 0 } 947 if disp >= maxdispatch { run = 0 } 948 if run == 1 { 949 var k: i64 = clk_edf_pick(deadlines, n, now) 950 if reserved >= 0 { if deadlines[reserved] <= now { k = reserved } } 951 // STORM-AWARE DISPATCH (2026-09-02): a HEAVY job (listed in CLK_HEAVY_CONF) that is due while the 952 // shared I/O-storm ruler reports zero spawn budget is re-armed to now + clk_defer_secs(interval) 953 // and NOT forked this pass; k becomes -1 so the else-branch below sleeps until the next real 954 // deadline exactly as if nothing were due. Light jobs, an absent conf and an UNREADABLE ruler 955 // take the pre-change path byte-for-byte. The deferral is announced on the in-flight beat with 956 // b = procs_blocked, so the docroot witness names the storm that caused it. 957 if k >= 0 { if clk_heavy_listed(hconf, hn, clk_slot(names, k)) == 1 { 958 let bud: i64 = clk_storm_budget(ioa) 959 if clk_storm_defer(1, bud) == 1 { 960 deferred = deferred + 1 961 deadlines[k] = now + clk_defer_secs(intervals[k]) 962 clk_inflight("deferred-storm" as *u8, clk_slot(organs, k), ioa[1], k) 963 k = 0 - 1 964 } 965 } } 966 if k >= 0 { 967 let missed: i64 = clk_edf_missed(deadlines, intervals, k, now) 968 let late: i64 = now - deadlines[k] 969 if late > maxlate { maxlate = late } 970 if missed > 0 { 971 starved = starved + 1 972 // NAME THE STARVER, DO NOT MERELY COUNT IT. A window reported "STARVED 3" and nothing 973 // anywhere said WHICH 3, so a job persistently losing EDF was undiagnosable. 974 // MEASURED 2026-08-14: a window published b=3 STARVED while nx_resmon's leak axis was 975 // flipping to UNOBSERVABLE off a stale memvel snapshot -- and there was no way to tell 976 // whether memvel was one of the three. A COUNT WITHOUT A WORKLIST IS NOT ACTIONABLE. 977 // Worst-offender (most periods missed) rather than a list: it needs no allocation, it 978 // matches the maxexec/maxidx idiom already proven here, and the worst starver is the 979 // one whose period is actually unschedulable. 980 if missed > maxmissed { maxmissed = missed; starvidx = k } 981 } 982 clk_edf_rearm(deadlines, intervals, k, now) 983 let e0: i64 = now 984 clk_inflight("in-flight" as *u8, clk_slot(organs, k), e0, k) 985 var ran: i64 = 0 986 var ownexec: i64 = 0 987 if reserved >= 0 { 988 let dispatch_rc: i64 = clk_dispatch_reserved(organs,names,intervals,deadlines,k,reserved,CLK_DISPATCH_DEADLINE_MS,CLK_DISPATCH_POLL_MS,reserved_result) 989 ran = reserved_result[CLKRS_FOREGROUND_DISP] 990 errors = errors+reserved_result[CLKRS_ERRORS] 991 if dispatch_rc == CLKRS_FATAL_RC { fatal = 1; run = 0 } 992 ownexec = reserved_result[CLKRS_FOREGROUND_EXEC] 993 assisted = assisted+reserved_result[CLKRS_DISP] 994 starved = starved+reserved_result[CLKRS_STARVED] 995 if reserved_result[CLKRS_LATE] > maxlate { maxlate = reserved_result[CLKRS_LATE] } 996 if reserved_result[CLKRS_MAXMISSED] > maxmissed { maxmissed = reserved_result[CLKRS_MAXMISSED]; starvidx = reserved_result[CLKRS_STARVIDX] } 997 if reserved_result[CLKRS_MAXEXEC] > maxexec { maxexec = reserved_result[CLKRS_MAXEXEC]; maxidx = reserved } 998 } else { ran = clk_dispatch_one(organs,names,k) } 999 let e1: i64 = sys_now_realtime_sec() 1000 if fatal == 0 { clk_inflight("idle" as *u8, clk_slot(organs, k), e1-e0, k) } 1001 else { clk_inflight("fatal-ownership-uncertain" as *u8,clk_slot(organs,k),reserved_result[CLKRS_LOST_PID],reserved_result[CLKRS_LOST_WAIT]) } 1002 // ---- COST-ADAPTIVE BACKOFF (2026-08-06) -------------------------------------- 1003 // PROVEN LIVE, NAMED BY THE IN-FLIGHT BEAT ABOVE: nx_worldgen_gate.sov.elf holds a 1004 // 120s-period slot for 500s+ per run. A job whose RUNTIME EXCEEDS ITS PERIOD is always 1005 // overdue, so EDF correctly picks it again immediately, and on a SERIAL dispatcher with 1006 // no deadline that is a PERMANENT DENIAL OF SERVICE to every other job -- sitecheck (30s) 1007 // and netobs (60s) both froze behind it and every beat in the estate went dead. 1008 // (STAR)A JOB WHOSE RUNTIME EXCEEDS ITS PERIOD CANNOT BE SCHEDULED AT THAT PERIOD -- THE 1009 // REGISTRY IS ASSERTING A COST NOBODY EVER MEASURED. So MEASURE IT AND BELIEVE THE 1010 // MEASUREMENT: re-arm such a job to now + its OWN observed runtime instead of now + 1011 // its declared interval. It still runs, it still makes progress, but it can never again 1012 // starve the whole registry, and the declared interval becomes a FLOOR not a fiction. 1013 // Chosen over SIGKILL deliberately: killing needs a raw signal syscall inside a forking 1014 // scheduler, and this tree has a live trap where literal syscall numbers are rv64->x86 1015 // translated. This fix needs NO new syscall and cannot mis-target a pid. 1016 if reserved < 0 { ownexec = e1-e0 } 1017 if ownexec > intervals[k] { deadlines[k] = e1 + ownexec * CLK_HOGDUTY } // dt is declared BELOW this point; use the operands already in scope 1018 let dt: i64 = e1 - e0 1019 execs = execs + dt 1020 // ★PUBLISH A MAX BEFORE YOU ACT ON A MEAN (2026-08-06). Aggregate exec_secs cannot separate 1021 // 'every child is a bit slow' from 'ONE child hung for most of the window' -- both produce the 1022 // same dispatch count and the same mean, and they have OPPOSITE remedies (raise capacity vs 1023 // kill one hog). clk_dispatch_one waits on the child with NO deadline (debt 1786056459), so the 1024 // hung case is not hypothetical. Record the worst SINGLE dispatch and WHICH job owned it. 1025 if ownexec > maxexec { maxexec = ownexec; maxidx = k } 1026 if ran == 1 { disp = disp + 1; fired[k] = 1 } 1027 } else { 1028 let nd: i64 = clk_edf_next(deadlines, n) 1029 if nd < 0 { run = 0 } else { 1030 var wait: i64 = nd - now 1031 if wait < 1 { wait = 1 } 1032 let left: i64 = budget_secs - (now - t0) 1033 if wait > left { wait = left } 1034 if wait < 1 { run = 0 } else { 1035 let ts: *i64 = sys_mmap(16) as *i64 1036 ts[0] = wait 1037 ts[1] = 0 1038 __syscall(35, ts as i64, 0, 0, 0, 0, 0) 1039 slept = slept + wait 1040 } 1041 } 1042 } 1043 } 1044 } 1045 out[0] = disp+assisted; out[1] = slept; out[2] = sys_now_realtime_sec() 1046 out[3] = starved; out[4] = maxlate; out[5] = execs 1047 out[6] = maxexec; out[7] = maxidx // worst single dispatch, and the job index that owned it 1048 out[8] = starvidx; out[9] = maxmissed // WORST STARVER: job index, and how many of its periods it missed 1049 if reserved_mode == 1 { out[11] = assisted } // service beside a foreground child; excluded from the foreground-start count ceiling 1050 out[10] = deferred // STORM-DEFERRED (2026-09-02): heavy dispatches this window declined on the shared I/O-storm ruler; the caller allocates 16 slots (128 bytes), so slot 10 is inside the mapping 1051 // CALLER CONTRACT: `out` must now be at least 10 i64 slots (80 bytes). nx_clock_tickless allocated 1052 // exactly 64 bytes = 8 slots, so writing out[8] there would have run off the end of the mapping -- 1053 // the caller was widened in the same change. Any new caller must allocate >= 80 bytes. 1054 if reserved_mode == 1 { 1055 out[CLKRS_OUT_ERRORS] = errors; out[CLKRS_OUT_FATAL] = fatal 1056 out[CLKRS_OUT_LOST_INDEX] = CLKRS_NONE 1057 out[CLKRS_OUT_LOST_PID] = 0; out[CLKRS_OUT_LOST_WAIT] = 0; out[CLKRS_OUT_KNOWN_OWNED] = 0 1058 if fatal != 0 { 1059 out[CLKRS_OUT_LOST_INDEX] = reserved_result[CLKRS_LOST_INDEX] 1060 out[CLKRS_OUT_LOST_PID] = reserved_result[CLKRS_LOST_PID] 1061 out[CLKRS_OUT_LOST_WAIT] = reserved_result[CLKRS_LOST_WAIT] 1062 out[CLKRS_OUT_KNOWN_OWNED] = reserved_result[CLKRS_KNOWN_OWNED] 1063 return CLKRS_FATAL_RC 1064 } 1065 } 1066 return disp+assisted 1067} 1068 1069// THE ARGV SPLIT, AS A PURE FUNCTION (2026-08-22, command-slot truncation fix). Copies the organ 1070// slot into `buf` (>= CLK_NAMEW+8 bytes), splits on single spaces IN PLACE, fills `argv` 1071// (>= 8*(CLK_ARGV_MAX+1) bytes) and returns the token count; argv[ac] is NULL. Factored out of 1072// clk_dispatch_one for the same reason the EDF math is factored into pure functions above: so a gate 1073// can prove an 18-token, 300-byte command survives the split WITHOUT a fork, and so the mutation 1074// site (CLK_NAMEW) is reachable from a tooth. 1075// OVERFLOW IS ANNOUNCED, NEVER SILENT: a command carrying more than CLK_ARGV_MAX tokens keeps the 1076// first CLK_ARGV_MAX and reports ARGV-OVERFLOW naming the job and the dropped count -- the old 1077// `if ac < 16` cap dropped every token past 15 with NO diagnostic, the same silent class as the 1078// 127-byte cut this change retires. Token-accept logic is otherwise byte-identical to the old code. 1079func clk_split_argv(raw0: *u8, buf: *u8, argv: *i64, label: *u8) -> i64 { 1080 var bl: i64 = 0 1081 var cdone: i64 = 0 1082 while cdone == 0 { 1083 if bl >= CLK_NAMEW - 1 { cdone = 1 } 1084 else { if raw0[bl] == (0 as u8) { cdone = 1 } else { buf[bl] = raw0[bl]; bl = bl + 1 } } 1085 } 1086 buf[bl] = 0 as u8 1087 argv[0] = buf as i64 1088 var ac: i64 = 1 1089 var dropped: i64 = 0 1090 var sp: i64 = 0 1091 while sp < bl { 1092 if buf[sp] == (32 as u8) { 1093 buf[sp] = 0 as u8 1094 if buf[sp+1] != (0 as u8) { 1095 if ac < CLK_ARGV_MAX { argv[ac] = ((buf as i64) + sp + 1); ac = ac + 1 } else { dropped = dropped + 1 } 1096 } 1097 } 1098 sp = sp + 1 1099 } 1100 argv[ac] = 0 1101 if dropped > 0 { 1102 let om: *u8 = sys_mmap(CLK_ROWW) 1103 var oo: i64 = clk_msgcat(om, 0, "clk_split_argv: ARGV-OVERFLOW job=" as *u8) 1104 oo = clk_msgcat(om, oo, label) 1105 oo = clk_msgcat(om, oo, " kept=" as *u8) 1106 oo = clk_itoa(om, oo, ac) 1107 oo = clk_msgcat(om, oo, " dropped=" as *u8) 1108 oo = clk_itoa(om, oo, dropped) 1109 oo = clk_msgcat(om, oo, " tail arguments NOT passed (CLK_ARGV_MAX in nx_clock_caps.nx bounds the vector; the bound is NAMED and this line is its announcement)\n" as *u8) 1110 om[oo] = 0 as u8 1111 sts_werr(om) 1112 sys_munmap(om, CLK_ROWW) 1113 } 1114 return ac 1115} 1116 1117// dispatch EXACTLY ONE job by index (the EDF loop picks the victim; this just runs it). Split out of 1118// clk_dispatch_run so ordering policy and exec mechanics are separable -- and so the EDF loop can 1119// re-read the clock between jobs. Returns 1 if the organ really ran, 0 if exec failed (127). 1120func clk_dispatch_one(organs: *u8, names: *u8, i: i64) -> i64 { 1121 // ARGV SUPPORT (2026-08-07). This built argv[0]=path, argv[1]=0 -- an organ could take NO ARGUMENTS. 1122 // (STAR)A DISPATCHER THAT CANNOT PASS ARGUMENTS MANUFACTURES SHELL SCRIPTS -- THE WRAPPERS ARE A 1123 // SYMPTOM OF THE CALLING CONVENTION, NOT OF LAZINESS. 1124 // The organ field is SPLIT ON SPACES into a real argv, so a row may read 1125 // fallbackharden<TAB>600<TAB>nx_actlog.elf harden knowledge/status/fallback.jrnl 1126 // BACK-COMPATIBLE BY CONSTRUCTION: a field with no space yields argv[0] only, and the slot is 1127 // CLK_NAMEW = CLK_CMD_CAP (nx_clock_caps.nx, shared with the writer nx_clockjob), so a command the 1128 // writer admits ALWAYS fits. The split itself is clk_split_argv above -- pure, gate-provable, 1129 // and it announces ARGV-OVERFLOW instead of silently dropping tokens. 1130 let buf: *u8 = sys_mmap(CLK_NAMEW + 8) 1131 let argv: *i64 = sys_mmap(8*(CLK_ARGV_MAX+1)) as *i64 1132 clk_split_argv(clk_slot(organs, i), buf, argv, clk_slot(names, i)) 1133 let path: *u8 = buf 1134 let path: *u8 = buf 1135 __syscall(90, path as i64, 0x1ed, 0, 0, 0, 0) // chmod +x the EXECUTABLE only, after the split 1136 let pid: i64 = sys_fork() 1137 if pid == 0 { 1138 // PER-JOB OUTPUT CAPTURE (2026-08-07). The SECOND service the .sh wrappers rendered, after argv: 1139 // nx_fallback_harden.cron.sh appends to logs/fallback_harden.log and its own header calls that 1140 // "the trend IS the telemetry". Replacing a wrapper with a bare organ row would have SILENTLY 1141 // LOST that log -- stripping a feature (rule 25) to remove a shell script. 1142 // (STAR)BEFORE REPLACING A WRAPPER, ENUMERATE EVERYTHING IT PROVIDES -- THE ARGUMENT YOU NOTICED 1143 // IS RARELY THE ONLY SERVICE IT RENDERS. 1144 // Every clock organ now gets, for free, the capture each wrapper hand-rolled: stdout AND stderr 1145 // append to logs/<jobname>.log. Named by JOB, not organ, so two jobs sharing a binary stay 1146 // distinguishable (clobbertest and frontdoor both run nx_atlas_frontdoor.elf). Fail-open: if the 1147 // log cannot be opened the child still execs -- telemetry must never be able to stop the beat. 1148 // Uses the sys_dup3 redirect idiom already proven in nx_guarded_run. 1149 let lp: *u8 = sys_mmap(CLK_NAMEW + 16); var lo: i64 = 0 // "logs/" + NAME slot + ".log" + NUL; DERIVED from the slot 1150 lo = clk_msgcat(lp, lo, "logs/" as *u8) 1151 lo = clk_msgcat(lp, lo, clk_slot(names, i)) 1152 lo = clk_msgcat(lp, lo, ".log" as *u8) 1153 lp[lo] = 0 as u8 1154 let ofd: i64 = sys_openat_append(lp, 0x1a4) 1155 if ofd >= 0 { sys_dup3(ofd, 1, 0); sys_dup3(ofd, 2, 0) } 1156 let envp: *i64 = sys_mmap(16) as *i64; envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64; envp[1] = 0 1157 sys_execve(path, argv, envp) 1158 sys_exit(127) 1159 } 1160 let st: *i64 = sys_mmap(16) as *i64; st[0] = 0 1161 // BOUNDED WAIT (was a bare blocking wait4 -- the single line that let one child own the clock). 1162 let t0ms: i64 = sys_now_ms() 1163 var reaped: i64 = 0 1164 var timedout: i64 = 0 1165 while reaped == 0 { 1166 let w: i64 = sys_wait4(pid, st, 1) 1167 if w == pid { reaped = 1 } 1168 else { if w < 0 { reaped = 1 } 1169 else { if sys_now_ms() - t0ms >= CLK_DISPATCH_DEADLINE_MS { nx_kill(pid, CLK_SIGKILL); sys_wait4(pid, st, 0); reaped = 1; timedout = 1 } else { sys_sleep_ms(CLK_DISPATCH_POLL_MS) } } } 1170 } 1171 // A REAPED-ON-DEADLINE CHILD DID EXECUTE -- report it as dispatched so the duty ceiling above backs 1172 // it off from its OWN measured cost. Reporting 0 here would hide the hog from the very rule that tames it. 1173 // LIVE-PATH TELEMETRY (2026-08-07). clk_actlog was first added to clk_dispatch_run -- which is the 1174 // DEAD path: nx_clock_tickless calls clk_run_edf (it REPLACES clk_run_tickless), and clk_run_edf 1175 // dispatches through THIS function. The instrumented binary was verifiably the one running, and it 1176 // still logged nothing, because I had checked WHICH BINARY IS LIVE and never WHICH CODE PATH IS. 1177 // Logged on EVERY exit including the deadline kill, since a job that had to be killed is exactly 1178 // the one a reader needs to see. 124 mirrors the shell timeout convention. 1179 if timedout == 1 { clk_actlog(path, 124); return 1 } 1180 let raw: i64 = st[0]; let sig: i64 = raw & 0x7f; let code: i64 = (raw >> 8) & 0xff 1181 clk_actlog(path, code) 1182 if sig != 0 { return 1 } 1183 if code != 127 { return 1 } 1184 return 0 1185} 1186 1187// load the registry into the arrays. returns #jobs (also written to np[0]). 1188func clk_load(path: *u8, names: *u8, organs: *u8, intervals: *i64, next_due: *i64, np: *i64) -> i64 { 1189 np[0] = 0 1190 let lenp: *i64 = sys_mmap(8) as *i64 1191 let data: *u8 = sys_read_file(path, lenp) 1192 if (data as i64) == 0 { return 0 } 1193 let dn: i64 = lenp[0]; var i: i64=0; var ls: i64=0; var n: i64=0 1194 while i < dn { 1195 if data[i] == (10 as u8) { 1196 if i > ls { if n < CLK_MAXJOBS { 1197 let line: *u8 = ((data as i64)+ls) as *u8; let ll: i64 = i-ls 1198 let d: *u8 = clk_slot(names, n); let e: *u8 = clk_slot(organs, n) 1199 var f: i64=0; var p: i64=0; var iv: i64=0; var nd: i64=0; var w: i64=0; var g: i64=0 // f: 0=name 1=interval 2=next_due 3=organ 1200 while p < ll { 1201 if line[p] == (9 as u8) { f = f + 1 } 1202 else { 1203 if f == 0 { if w < CLK_NAMEW-1 { d[w]=line[p]; w=w+1 } } 1204 else { if f == 3 { if g < CLK_NAMEW-1 { e[g]=line[p]; g=g+1 } } 1205 else { if line[p] >= (48 as u8) { if line[p] <= (57 as u8) { 1206 let dig: i64 = (line[p]-(48 as u8)) as i64 1207 if f == 1 { iv = iv*10 + dig } else { nd = nd*10 + dig } 1208 } } } } 1209 } 1210 p = p + 1 1211 } 1212 d[w] = 0 as u8; e[g] = 0 as u8 1213 intervals[n]=iv; next_due[n]=nd; n=n+1 1214 } } 1215 ls = i + 1 1216 } 1217 i = i + 1 1218 } 1219 np[0] = n 1220 return n 1221} 1222 1223// Reserved service uses the existing job registry and deadline arrays, never a second clock. 1224// Exactly one explicitly bound light job may run beside one foreground child. 1225// Pure, strict activation: one non-comment name<TAB>exact-command row; no wildcard or name-only grant. 1226func clk_reserved_resolve(conf: *u8, cn: i64, names: *u8, organs: *u8, intervals: *i64, n: i64, heavy: *u8, hn: i64) -> i64 { 1227 if cn <= 0 { return CLKRS_NONE } 1228 let name: *u8 = sys_mmap(CLK_NAMEW) 1229 let command: *u8 = sys_mmap(CLK_NAMEW) 1230 var rows: i64 = 0 1231 var p: i64 = 0 1232 while p < cn { 1233 var e: i64 = p 1234 while e < cn { if conf[e] == (10 as u8) { break } e = e + 1 } 1235 var end: i64 = e 1236 if end > p { if conf[end-1] == (13 as u8) { end = end - 1 } } 1237 if end > p { if conf[p] != (35 as u8) { 1238 rows = rows + 1 1239 if rows > 1 { return CLKRS_INVALID } 1240 var tab: i64 = 0 - 1 1241 var q: i64 = p 1242 while q < end { 1243 if conf[q] == (0 as u8) { return CLKRS_INVALID } 1244 if conf[q] == (9 as u8) { if tab >= 0 { return CLKRS_INVALID } tab = q } 1245 q = q + 1 1246 } 1247 if tab <= p { return CLKRS_INVALID } 1248 if tab+1 >= end { return CLKRS_INVALID } 1249 if tab-p >= CLK_NAMEW { return CLKRS_INVALID } 1250 if end-tab-1 >= CLK_NAMEW { return CLKRS_INVALID } 1251 q = p 1252 while q < tab { name[q-p] = conf[q]; q = q + 1 } 1253 name[tab-p] = 0 as u8 1254 q = tab+1 1255 while q < end { command[q-tab-1] = conf[q]; q = q + 1 } 1256 command[end-tab-1] = 0 as u8 1257 } } 1258 p = e + 1 1259 } 1260 if rows == 0 { return CLKRS_NONE } 1261 var found: i64 = CLKRS_NONE 1262 var i: i64 = 0 1263 while i < n { 1264 if clk_streq(clk_slot(names,i),name) == 1 { 1265 if found >= 0 { return CLKRS_INVALID } 1266 if clk_streq(clk_slot(organs,i),command) != 1 { return CLKRS_INVALID } 1267 if intervals[i] <= 0 { return CLKRS_INVALID } 1268 if clk_heavy_listed(heavy,hn,name) == 1 { return CLKRS_INVALID } 1269 found = i 1270 } 1271 i = i + 1 1272 } 1273 if found < 0 { return CLKRS_INVALID } 1274 // A second registry name must not conceal the same producer command. 1275 i = 0 1276 while i < n { 1277 if i != found { if clk_streq(clk_slot(organs,i),command) == 1 { return CLKRS_INVALID } } 1278 i = i+1 1279 } 1280 return found 1281} 1282// Pure service predicate. One occupied reservation cannot spawn another copy. 1283func clk_reserved_due(index: i64, foreground: i64, pid: i64, due: i64, now: i64, foreground_live: i64) -> i64 { 1284 if index < 0 { return 0 } 1285 if index == foreground { return 0 } 1286 if pid != 0 { return 0 } 1287 if foreground_live != 1 { return 0 } 1288 if due > now { return 0 } 1289 return 1 1290} 1291// Preserve argv, per-job append capture and executable preparation, but move child file work 1292// after fork so it cannot block the parent's wait/service loop before the child exists. 1293func clk_reserved_spawn(organs: *u8, names: *u8, i: i64) -> i64 { 1294 let pid: i64 = sys_fork() 1295 if pid != 0 { return pid } 1296 let buf: *u8 = sys_mmap(CLK_NAMEW+8) 1297 let argv: *i64 = sys_mmap(8*(CLK_ARGV_MAX+1)) as *i64 1298 clk_split_argv(clk_slot(organs,i),buf,argv,clk_slot(names,i)) 1299 __syscall(90,buf as i64,0x1ed,0,0,0,0) 1300 let lp: *u8 = sys_mmap(CLK_NAMEW+16) 1301 var lo: i64 = clk_msgcat(lp,0,"logs/" as *u8) 1302 lo = clk_msgcat(lp,lo,clk_slot(names,i)); lo = clk_msgcat(lp,lo,".log" as *u8); lp[lo] = 0 as u8 1303 let fd: i64 = sys_openat_append(lp,MODE_0644) 1304 if fd >= 0 { sys_dup3(fd,1,0); sys_dup3(fd,2,0); if fd > 2 { sys_close(fd) } } 1305 let envp: *i64 = sys_mmap(16) as *i64 1306 envp[0] = "PATH=/usr/bin:/bin" as *u8 as i64; envp[1] = 0 1307 sys_execve(buf,argv,envp) 1308 sys_exit(127); return 0 1309} 1310func clk_reserved_log(organs: *u8, names: *u8, i: i64, code: i64) -> i64 { 1311 let buf: *u8 = sys_mmap(CLK_NAMEW+8) 1312 let av: *i64 = sys_mmap(8*(CLK_ARGV_MAX+1)) as *i64 1313 clk_split_argv(clk_slot(organs,i),buf,av,clk_slot(names,i)) 1314 clk_actlog(buf,code) 1315 sys_munmap(buf,CLK_NAMEW+8); sys_munmap(av as *u8,8*(CLK_ARGV_MAX+1)) 1316 return 0 1317} 1318// Production syscall seams have no fixture flags or configurable fault behavior. 1319func clk_reserved_wait_owned(pid: i64, status: *i64, flags: i64) -> i64 { 1320 return sys_wait4(pid,status,flags) 1321} 1322func clk_reserved_signal_owned(pid: i64, signal: i64) -> i64 { 1323 return nx_kill(pid,signal) 1324} 1325func clk_reserved_wait_kind(result: i64, pid: i64) -> i64 { 1326 if pid <= 0 { return CLKRS_WAIT_LOST } 1327 if result == pid { return CLKRS_WAIT_REAPED } 1328 if result == 0 { return CLKRS_WAIT_RUNNING } 1329 if result == CLKRS_EINTR { return CLKRS_WAIT_INTERRUPTED } 1330 return CLKRS_WAIT_LOST 1331} 1332func clk_reserved_halt_required(out: *i64) -> i64 { 1333 if out[CLKRS_OUT_FATAL] != 0 { return 1 } 1334 return 0 1335} 1336func clk_reserved_lost(ro: *i64, names: *u8, i: i64, pid: i64, result: i64, other_owned: i64) -> i64 { 1337 ro[CLKRS_ERRORS] = ro[CLKRS_ERRORS]+1 1338 if ro[CLKRS_FATAL] == 0 { 1339 ro[CLKRS_LOST_INDEX] = i; ro[CLKRS_LOST_PID] = pid; ro[CLKRS_LOST_WAIT] = result 1340 } 1341 ro[CLKRS_FATAL] = 1 1342 let msg: *u8 = sys_mmap(CLK_ROWW) 1343 var o: i64 = clk_msgcat(msg,0,"CLOCK-RESERVE FATAL ownership lost job=" as *u8) 1344 o = clk_msgcat(msg,o,clk_slot(names,i)); o = clk_msgcat(msg,o," pid=" as *u8); o = clk_msgnum(msg,o,pid) 1345 o = clk_msgcat(msg,o," wait_result=" as *u8); o = clk_msgnum(msg,o,result) 1346 o = clk_msgcat(msg,o," known_owned_to_drain=" as *u8); o = clk_msgnum(msg,o,other_owned) 1347 o = clk_msgcat(msg,o,"; no further forks or access to unowned PID; reconciliation required\n" as *u8) 1348 sys_write(2,msg,o); sys_munmap(msg,CLK_ROWW) 1349 return 0 1350} 1351// Owned slots are capabilities, not a guess that a numeric PID remains our child. 1352// A non-EINTR wait failure invalidates the slot once and globally inhibits new forks. 1353// Only still-owned children are drained. Unknown outcomes are never logged as success. 1354func clk_dispatch_reserved(organs: *u8, names: *u8, intervals: *i64, deadlines: *i64, i: i64, reserved: i64, deadline_ms: i64, poll_ms: i64, ro: *i64) -> i64 { 1355 var z: i64 = 0 1356 while z < CLKRS_N { ro[z] = 0; z = z+1 } 1357 ro[CLKRS_STARVIDX] = CLKRS_NONE; ro[CLKRS_LOST_INDEX] = CLKRS_NONE 1358 if deadline_ms <= 0 { ro[CLKRS_ERRORS] = 1; return 0 } 1359 if poll_ms <= 0 { ro[CLKRS_ERRORS] = 1; return 0 } 1360 let pid: i64 = clk_reserved_spawn(organs,names,i) 1361 if pid < 0 { ro[CLKRS_ERRORS] = 1; sts_werr("CLOCK-RESERVE foreground fork failed; no wait or signal issued\n" as *u8); return 0 } 1362 if pid == 0 { return 0 } 1363 let start: i64 = sys_now_ms() 1364 let st: *i64 = sys_mmap(16) as *i64 1365 let rst: *i64 = sys_mmap(16) as *i64 1366 var owned: i64 = 1 1367 var attempted: i64 = 0; var signaled: i64 = 0 1368 var rp: i64 = 0; var rowned: i64 = 0; var rs: i64 = 0 1369 var rattempted: i64 = 0; var rsignaled: i64 = 0 1370 while owned+rowned > 0 { 1371 let nowms: i64 = sys_now_ms() 1372 if owned == 1 { 1373 let result: i64 = clk_reserved_wait_owned(pid,st,CLKRS_WNOHANG) 1374 let kind: i64 = clk_reserved_wait_kind(result,pid) 1375 if kind == CLKRS_WAIT_REAPED { 1376 owned = 0 1377 ro[CLKRS_FOREGROUND_EXEC] = (nowms-start)/1000 1378 let sig: i64 = st[0] & 0x7f 1379 var code: i64 = (st[0] >> 8) & 0xff 1380 if sig != 0 { code = CLKRS_SIGNAL_EXIT_BASE+sig } 1381 if signaled == 1 { if sig == CLK_SIGKILL { code = 124 } } 1382 if sig != 0 { ro[CLKRS_FOREGROUND_DISP] = 1 } else { if code != 127 { ro[CLKRS_FOREGROUND_DISP] = 1 } } 1383 clk_reserved_log(organs,names,i,code) 1384 } else { 1385 if kind == CLKRS_WAIT_LOST { 1386 owned = 0 1387 clk_reserved_lost(ro,names,i,pid,result,rowned) 1388 } else { if kind == CLKRS_WAIT_RUNNING { if attempted == 0 { if nowms-start >= deadline_ms { 1389 let kr: i64 = clk_reserved_signal_owned(pid,CLK_SIGKILL); attempted = 1 1390 if kr == 0 { signaled = 1 } else { ro[CLKRS_ERRORS] = ro[CLKRS_ERRORS]+1 } 1391 sts_werr("CLOCK-RESERVE foreground deadline signal attempted; still owned until wait result, no repeat signal\n" as *u8) 1392 } } } } 1393 } 1394 } 1395 if rowned == 1 { 1396 let result: i64 = clk_reserved_wait_owned(rp,rst,CLKRS_WNOHANG) 1397 let kind: i64 = clk_reserved_wait_kind(result,rp) 1398 if kind == CLKRS_WAIT_REAPED { 1399 rowned = 0 1400 let dt: i64 = (nowms-rs)/1000 1401 if dt > ro[CLKRS_MAXEXEC] { ro[CLKRS_MAXEXEC] = dt } 1402 let sig: i64 = rst[0] & 0x7f 1403 var code: i64 = (rst[0] >> 8) & 0xff 1404 if sig != 0 { code = CLKRS_SIGNAL_EXIT_BASE+sig } 1405 if rsignaled == 1 { if sig == CLK_SIGKILL { code = 124 } } 1406 if sig != 0 { ro[CLKRS_DISP] = ro[CLKRS_DISP]+1 } else { if code != 127 { ro[CLKRS_DISP] = ro[CLKRS_DISP]+1 } } 1407 clk_reserved_log(organs,names,reserved,code) 1408 if dt > intervals[reserved] { deadlines[reserved] = sys_now_realtime_sec()+dt*CLK_HOGDUTY } 1409 rp = 0 1410 } else { 1411 if kind == CLKRS_WAIT_LOST { 1412 rowned = 0 1413 clk_reserved_lost(ro,names,reserved,rp,result,owned) 1414 rp = 0 1415 } else { if kind == CLKRS_WAIT_RUNNING { if rattempted == 0 { if nowms-rs >= deadline_ms { 1416 let kr: i64 = clk_reserved_signal_owned(rp,CLK_SIGKILL); rattempted = 1 1417 if kr == 0 { rsignaled = 1 } else { ro[CLKRS_ERRORS] = ro[CLKRS_ERRORS]+1 } 1418 sts_werr("CLOCK-RESERVE essential deadline signal attempted; still owned until wait result, no repeat signal\n" as *u8) 1419 } } } } 1420 } 1421 } 1422 if ro[CLKRS_FATAL] == 0 { 1423 let now: i64 = sys_now_realtime_sec() 1424 var due: i64 = now+1 1425 if reserved >= 0 { due = deadlines[reserved] } 1426 if clk_reserved_due(reserved,i,rp,due,now,owned) == 1 { 1427 let missed: i64 = clk_edf_missed(deadlines,intervals,reserved,now) 1428 let late: i64 = now-deadlines[reserved] 1429 if late > ro[CLKRS_LATE] { ro[CLKRS_LATE] = late } 1430 if missed > 0 { ro[CLKRS_STARVED] = ro[CLKRS_STARVED]+1 } 1431 if missed > ro[CLKRS_MAXMISSED] { ro[CLKRS_MAXMISSED] = missed; ro[CLKRS_STARVIDX] = reserved } 1432 clk_edf_rearm(deadlines,intervals,reserved,now) 1433 rp = clk_reserved_spawn(organs,names,reserved) 1434 if rp < 0 { rp = 0; ro[CLKRS_ERRORS] = ro[CLKRS_ERRORS]+1; sts_werr("CLOCK-RESERVE essential fork failed; no wait or signal issued\n" as *u8) } 1435 else { if rp > 0 { rowned = 1; rs = sys_now_ms(); rattempted = 0; rsignaled = 0; sts_werr("CLOCK-RESERVE essential dispatched beside foreground\n" as *u8) } } 1436 } 1437 } 1438 ro[CLKRS_KNOWN_OWNED] = owned+rowned 1439 if owned+rowned > 0 { sys_sleep_ms(poll_ms) } 1440 } 1441 sys_munmap(st as *u8,16); sys_munmap(rst as *u8,16) 1442 if ro[CLKRS_FATAL] != 0 { return CLKRS_FATAL_RC } 1443 return ro[CLKRS_FOREGROUND_DISP] 1444} 1445 1446func clk_run_edf(organs: *u8, names: *u8, intervals: *i64, deadlines: *i64, n: i64, maxdispatch: i64, budget_secs: i64, tick_ms: i64, out: *i64) -> i64 { 1447 return clk_run_edf_core(organs,names,intervals,deadlines,n,maxdispatch,budget_secs,tick_ms,out,0) 1448} 1449// Extended reserved contract: >=CLKRS_OUT_N i64 slots; out[11] assisted; 1450// named out[12..17] expose errors and fatal ownership evidence. Legacy wrapper extent is unchanged. 1451func clk_run_edf_reserved(organs: *u8, names: *u8, intervals: *i64, deadlines: *i64, n: i64, maxdispatch: i64, budget_secs: i64, tick_ms: i64, out: *i64) -> i64 { 1452 return clk_run_edf_core(organs,names,intervals,deadlines,n,maxdispatch,budget_secs,tick_ms,out,1) 1453} 1454