code wiki / (root) / nx_tools_timing_gate.nx

nx_tools_timing_gate.nx source

↩ module page · 73 lines · 4960 B

1// Real monotonic clock + existing journal writer, with an isolated caller-owned fixture. 2// license_tier: ORIGINAL; expect_exit: 0 3import "nx_tools_api.nx" 4import "nx_gatekit_lib.nx" 5import "nx_gate_verdict.nx" 6const TG_FILE_MODE: i64 = 0x180 7 8func tg_path(path: *u8) -> i64 { 9 let root: *u8 = "/tmp/" as *u8 10 let r: i64 = gk_len(root); let n: i64 = gk_len(path) 11 if n <= r { return 0 } 12 if n >= SYS_PATH_MAX { return 0 } 13 var i: i64 = 0 14 while i < r { if path[i] != root[i] { return 0 } i = i+1 } 15 if path[r] == (46 as u8) { return 0 } 16 while i < n { if path[i] < (32 as u8) { return 0 }; if path[i] == (47 as u8) { return 0 }; if path[i] == (92 as u8) { return 0 }; i = i+1 } 17 return 1 18} 19func tg_same(a: *u8, an: i64, b: *u8, bn: i64) -> i64 { 20 if an != bn { return 0 } 21 var i: i64 = 0 22 while i < an { if a[i] != b[i] { return 0 } i = i+1 } 23 return 1 24} 25func main(argc: i64, argv: *i64) -> i64 { 26 if argc != 2 { gv_puts("usage: timing-gate </tmp/fresh-journal>\n" as *u8); return 3 } 27 let path: *u8 = argv[1] as *u8 28 if tg_path(path) == 0 { gv_puts("REFUSE unsafe temporary path\n" as *u8); return 3 } 29 let ctr: *i64 = gv_ctr(); gv_head("Timing observations: checked monotonic clock, existing journal, no false completion" as *u8) 30 let fd: i64 = sys_openat_exclusive(path, TG_FILE_MODE) 31 gv_check("exclusive-temporary-journal-created" as *u8, fd >= 0, ctr) 32 if fd < 0 { return gv_verdict("NX-TOOLS-TIMING" as *u8, ctr, "existing fixture refused" as *u8) } 33 sys_close(fd) 34 ta_timing_begin() 35 let start: i64 = ta_timing_now_us() 36 let end: i64 = ta_timing_now_us() 37 gv_check("real-monotonic-clock-readable" as *u8, start >= 0, ctr) 38 gv_check("real-monotonic-observations-nondecreasing" as *u8, end >= start, ctr) 39 gv_check("real-clock-elapsed-matches-subtraction" as *u8, ta_timing_elapsed(start, end) == end-start, ctr) 40 gv_check("unknown-clock-stays-unknown" as *u8, ta_timing_elapsed(TT_UNKNOWN, end) == TT_UNKNOWN, ctr) 41 gv_check("backwards-clock-stays-unknown" as *u8, ta_timing_elapsed(2, 1) == TT_UNKNOWN, ctr) 42 let written: i64 = ta_timing_emit_to(path, "fixture" as *u8, 7, "server" as *u8, "async_ack_write_return" as *u8, 17, start, end, 0, 11) 43 gv_check("actual-observation-row-written" as *u8, written == 0, ctr) 44 let promoted: i64 = ta_timing_emit_to(path, "fixture" as *u8, 7, "sync-promoted" as *u8, "promoted_capture_return_unreaped" as *u8, 17, start, end, TT_UNKNOWN, 13) 45 gv_check("unreaped-observation-row-written" as *u8, promoted == 0, ctr) 46 let n: *i64 = sys_mmap(8) as *i64 47 let an: *i64 = sys_mmap(8) as *i64 48 var allocated: i64 = 0 49 if (n as i64) > 0 { if (an as i64) > 0 { allocated = 1 } } 50 gv_check("read-buffers-allocated" as *u8, allocated, ctr) 51 if allocated == 0 { return gv_verdict("NX-TOOLS-TIMING" as *u8, ctr, "allocation failed" as *u8) } 52 let before: *u8 = sys_read_file(path, n) 53 var ready: i64 = 0 54 if (before as i64) > 0 { if n[0] > 0 { ready = 1 } } 55 gv_check("complete-observation-journal-readable" as *u8, ready, ctr) 56 if ready == 0 { return gv_verdict("NX-TOOLS-TIMING" as *u8, ctr, "observation file unreadable" as *u8) } 57 gv_check("ACK-labelled-as-write-return" as *u8, gk_has(before, "\tobserve\tasync_ack_write_return\t" as *u8), ctr) 58 gv_check("unreaped-capture-not-process-completion" as *u8, gk_has(before, "\tobserve\tpromoted_capture_return_unreaped\t" as *u8), ctr) 59 gv_check("explicit-job-correlation-retained" as *u8, gk_has(before, " job=17 " as *u8), ctr) 60 gv_check("monotonic-clock-scope-explicit" as *u8, gk_has(before, "clock=monotonic_us clock_scope=same_process_tree" as *u8), ctr) 61 gv_check("client-first-byte-unobserved" as *u8, gk_has(before, "client_first_byte=unobserved" as *u8), ctr) 62 gv_check("result-first-byte-unobserved" as *u8, gk_has(before, "result_first_byte=unobserved" as *u8), ctr) 63 gv_check("phase-rows-do-not-double-count-dur-ms-census" as *u8, gk_has(before, "dur_ms=" as *u8) == 0, ctr) 64 gv_check("invalid-tool-control-byte-refused" as *u8, ta_timing_emit_to(path, "bad\nname" as *u8, 8, "server" as *u8, "fixture" as *u8, 17, start, end, 0, 0) < 0, ctr) 65 gv_check("actual-journal-open-failure-propagated" as *u8, ta_timing_emit_to("/tmp" as *u8, "fixture" as *u8, 7, "server" as *u8, "fixture" as *u8, 17, start, end, 0, 0) < 0, ctr) 66 let after: *u8 = sys_read_file(path, an) 67 var unchanged: i64 = 0 68 if (after as i64) > 0 { unchanged = tg_same(before, n[0], after, an[0]) } 69 gv_check("refused-observations-preserve-existing-journal" as *u8, unchanged, ctr) 70 gv_puts("retained_fixture=" as *u8); gv_puts(path); gv_puts(" bytes=" as *u8); gv_num(n[0]); gv_puts("\n" as *u8) 71 sys_free_file(before, n[0]); sys_free_file(after, an[0]) 72 return gv_verdict("NX-TOOLS-TIMING" as *u8, ctr, "fixture job identity only; real serve/async/promotion integration still requires live verification" as *u8) 73}