code wiki / _hdl_build / nx_budget_run.nx
nx_budget_run.nx source
↩ module page · 125 lines · 4144 B
1// nx_budget_run.nx -- TIME-BUDGETED step runner: turns silent slowness
2// into a FILED defect automatically.
3//
4// Operator 2026-06-10: "slowness anywhere in our entire ecosystem is a
5// total failure." The 885KB sovereign assembly that silently ate 90
6// minutes would have been auto-filed by this organ at its budget line.
7//
8// Usage: nx_budget_run <budget_seconds> <path> [args...]
9// - runs <path>, polling wait4(WNOHANG) on a 100ms beat
10// - inside budget -> "BUDGET-OK <path> elapsed=<s>s limit=<s>s",
11// exit = child's exit code
12// - budget blown -> kill -9, "BUDGET-EXCEEDED <path> limit=<s>s"
13// appended to the sentinel log (the PM/loop
14// dispatcher's queue), exit 99
15// The wrapper is the PREVENTION pillar: every build/step the team runs
16// through it converts "mysteriously slow" into a logged, gated verdict.
17// license_tier: ORIGINAL
18
19import "nx_syscalls.nx"
20import "_hdl_build/nx_kill_portable.nx"
21const BR_MAGIC_1000000: i64 = 1000000
22
23const BR_EXIT_USAGE: i64 = 2
24const BR_EXIT_BUDGET: i64 = 99
25const BR_LOG_PATH: *u8 = "/tmp/nishi_perf_sentinel.log" as *u8
26const BR_BEAT_MS: i64 = 100
27
28func br_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
29func br_app(dst: *u8, off: i64, s: *u8) -> i64 {
30 var i: i64 = 0
31 while s[i] != (0 as u8) { dst[off + i] = s[i]; i = i + 1 }
32 return off + i
33}
34func br_app_n(dst: *u8, off: i64, v: i64) -> i64 {
35 var o: i64 = off
36 if v == 0 { dst[o] = 48; return o + 1 }
37 var x: i64 = v
38 var d: i64 = 0
39 var y: i64 = x
40 while y > 0 { d = d + 1; y = y / 10 }
41 var i: i64 = d - 1
42 y = x
43 while i >= 0 { dst[o + i] = (48 + (y % 10)) as u8; y = y / 10; i = i - 1 }
44 return o + d
45}
46// parse decimal argv string
47func br_atoi(s: *u8) -> i64 {
48 var v: i64 = 0
49 var i: i64 = 0
50 var run: i64 = 1
51 while run == 1 {
52 let c: i64 = s[i] as i64
53 var dig: i64 = 0
54 if c >= 48 { if c <= 57 { dig = 1 } }
55 if dig == 1 { v = v * 10 + (c - 48); i = i + 1 } else { run = 0 }
56 }
57 return v
58}
59
60func main(argc: i64, argv: *i64) -> i64 {
61 if argc < 3 {
62 sys_write(1, "usage: nx_budget_run <budget_s> <path> [args...]\n" as *u8, 49)
63 return BR_EXIT_USAGE
64 }
65 let budget_s: i64 = br_atoi(argv[1] as *u8)
66 let path: *u8 = argv[2] as *u8
67
68 // child argv = argv[2..]
69 let cav: *i64 = sys_mmap(8 * 16) as *i64
70 var ai: i64 = 0
71 while ai + 2 < argc { cav[ai] = argv[ai + 2]; ai = ai + 1 }
72 cav[ai] = 0
73 let env: *i64 = sys_mmap(16) as *i64
74 env[0] = 0
75
76 let t_start: i64 = sys_now_us()
77 let pid: i64 = sys_fork()
78 if pid == 0 {
79 sys_execve(path, cav, env)
80 sys_exit(127)
81 }
82 if pid < 0 { return 126 }
83
84 let status: *i64 = sys_mmap(16) as *i64
85 let line: *u8 = sys_mmap(256)
86 var done: i64 = 0
87 var child_rc: i64 = 0
88 var blown: i64 = 0
89 while done == 0 {
90 let r: i64 = sys_wait4(pid, status, 1) // WNOHANG
91 if r == pid {
92 child_rc = (status[0] >> 8) & 0xff
93 done = 1
94 } else {
95 let el_s: i64 = (sys_now_us() - t_start) / BR_MAGIC_1000000
96 if el_s >= budget_s {
97 nxk_kill(pid, 9)
98 sys_wait4(pid, status, 0)
99 blown = 1
100 done = 1
101 } else {
102 sys_sleep_ms(BR_BEAT_MS)
103 }
104 }
105 }
106 let elapsed_s: i64 = (sys_now_us() - t_start) / BR_MAGIC_1000000
107
108 var w: i64 = 0
109 if blown == 1 { w = br_app(line, w, "PERF ESCALATE BUDGET-EXCEEDED step=" as *u8) }
110 else { w = br_app(line, w, "BUDGET-OK step=" as *u8) }
111 w = br_app(line, w, path)
112 w = br_app(line, w, " elapsed_s=" as *u8)
113 w = br_app_n(line, w, elapsed_s)
114 w = br_app(line, w, " limit_s=" as *u8)
115 w = br_app_n(line, w, budget_s)
116 line[w] = 10
117 w = w + 1
118 sys_write(1, line, w)
119 if blown == 1 {
120 let lfd: i64 = sys_openat_append(BR_LOG_PATH, 420)
121 if lfd >= 0 { sys_write(lfd, line, w); sys_close(lfd) }
122 return BR_EXIT_BUDGET
123 }
124 return child_rc
125}