nx_calibrate_test.nx source
↩ module page · 81 lines · 3539 B
1// nx_calibrate_test.nx -- smoke for nx_calibrate SA-2 MVP.
2//
3// Exercises:
4// - canary tamper-detect (pre + post bracketing)
5// - calibration completes without canary tamper
6// - every microbench produces a positive measurement
7// - cache hierarchy WITNESSED: l1 <= l2 <= ram (with tolerance band
8// because qemu emulation may compress the hierarchy)
9// - memory bandwidth > 0
10// - syscall costs > 0 (could be very small on qemu user-mode)
11// - tier inference returns a valid sealed-enum value
12// - sealed-enum validity gate
13// - STRUCTURAL REPRODUCIBILITY: re-run produces same SHAPE (all
14// fields populated, none NaN/negative, hierarchy still monotone)
15// -- byte-equal across runs deferred to SA-7 when byzantine
16// N-of-M reconciliation lands
17//
18// SA-2 verification gate per NISHI_SELF_ASSEMBLY_ROADMAP.md ยง8.
19
20import "nx_syscalls.nx"
21import "nx_calibrate.nx"
22
23func main() -> i64 {
24 // ----- 1. Allocate + check canaries -----
25 let r: *NxCalibrationRecord = nx_calibrate_new()
26 if (r as i64) == 0 { return 1 }
27 if r.canary_pre != NX_CALIB_CANARY_PRE { return 2 }
28 if r.canary_post != NX_CALIB_CANARY_POST { return 3 }
29 if r.schema_version != NX_CALIB_SCHEMA_VERSION { return 4 }
30 if r.int_alu_iters != NX_CALIB_INT_ALU_ITERS { return 5 }
31
32 // ----- 2. Run calibration -----
33 let rc: i64 = nx_calibrate_run(r)
34 if rc != 0 { return 6 }
35
36 // ----- 3. Every microbench produced a positive measurement -----
37 if r.int_alu_ps_per_op <= 0 { return 7 }
38 if r.mem_ns_per_access_l1 <= 0 { return 8 }
39 if r.mem_ns_per_access_l2 <= 0 { return 9 }
40 if r.mem_ns_per_access_ram <= 0 { return 10 }
41 if r.mem_bw_mib_per_s <= 0 { return 11 }
42 if r.syscall_ns_clock_gettime <= 0 { return 12 }
43 if r.syscall_ns_mmap <= 0 { return 13 }
44
45 // ----- 4. Cache hierarchy witnessed (monotone l1 <= l2 <= ram) -----
46 // qemu emulation can compress the hierarchy; we accept equality
47 // but reject ordering inversions of more than 2x (a hard inversion
48 // would indicate measurement noise dominating, not real hierarchy).
49 if r.mem_ns_per_access_l1 > r.mem_ns_per_access_l2 * 2 { return 14 }
50 if r.mem_ns_per_access_l2 > r.mem_ns_per_access_ram * 2 { return 15 }
51
52 // ----- 5. Tier inference returns a valid enum -----
53 if nx_tier_inf_is_valid(r.inferred_tier) != 1 { return 16 }
54 if nx_tier_inf_is_valid(-1) != 0 { return 17 }
55 if nx_tier_inf_is_valid(NX_TIER_INF_N) != 0 { return 18 }
56
57 // ----- 6. Timestamp populated -----
58 if r.ts_us <= 0 { return 19 }
59
60 // ----- 7. STRUCTURAL REPRODUCIBILITY -----
61 // Run calibration a second time on a fresh record; assert same
62 // shape (all fields > 0, hierarchy still monotone, tier valid).
63 // Bit-equal values are NOT asserted (real empirical measurement
64 // varies); SA-7's byzantine N-of-M will add the tolerance-band
65 // reconciliation.
66 let r2: *NxCalibrationRecord = nx_calibrate_new()
67 let rc2: i64 = nx_calibrate_run(r2)
68 if rc2 != 0 { return 20 }
69 if r2.int_alu_ps_per_op <= 0 { return 21 }
70 if r2.mem_ns_per_access_l1 <= 0 { return 22 }
71 if r2.mem_ns_per_access_ram <= 0 { return 23 }
72 if r2.mem_bw_mib_per_s <= 0 { return 24 }
73 if nx_tier_inf_is_valid(r2.inferred_tier) != 1 { return 25 }
74
75 // Same tier inferred across re-runs (substrate hardware didn't
76 // change between calls; if tier flips run-to-run the measurement
77 // is too noisy to be useful).
78 if r.inferred_tier != r2.inferred_tier { return 26 }
79
80 return 0
81}