nx_fpsmeter.nx source
↩ module page · 36 lines · 1932 B
1// nx_fpsmeter.nx -- sovereign rolling FPS meter: count the frame timestamps within the last 1000 ms. The call
2// stack currently measures NOTHING (can't answer "what fps are we at"); this is the instrument ("monitor the
3// system" law). State m[*i64]: [0]=head, [1]=count, [2 + i]=timestamp ring (FM_CAP). Assumes a real ms clock
4// (sys_now_ms). Pure buffer ops -> wasm-friendly (client) AND daemon-reusable. license_tier: ORIGINAL
5
6const FM_CAP: i64 = 128 // tracks up to 128 fps
7
8func fm_init(m: *i64) -> i64 { m[0] = 0; m[1] = 0; var i: i64 = 0; while i < FM_CAP { m[2 + i] = 0; i = i + 1 } return 0 }
9// record a frame at wall-time now_ms
10func fm_tick(m: *i64, now_ms: i64) -> i64 {
11 let h: i64 = m[0]
12 m[2 + h] = now_ms
13 m[0] = (h + 1) % FM_CAP
14 if m[1] < FM_CAP { m[1] = m[1] + 1 }
15 return 0
16}
17// frames in the last 1000 ms = the current FPS
18func fm_fps(m: *i64, now_ms: i64) -> i64 {
19 let lo: i64 = now_ms - 999
20 var c: i64 = 0
21 var i: i64 = 0
22 // ONLY THE FILLED SLOTS. Scanning all FM_CAP slots counted the zero-initialised tail as real
23 // frames whenever 0 fell inside the window (now_ms <= 999). A live meter on a real ms clock never
24 // reaches that, which is why it went unseen -- but a meter fed a REPLAYED capture, whose
25 // timestamps are relative and start near zero, hit it every time and reported FM_CAP frames for a
26 // 3-frame trace. m[1] is the fill count this ring already maintained and never read. Once the ring
27 // is full (m[1] == FM_CAP) the arithmetic is bit-identical, so no warm caller changes behaviour --
28 // verified against the incumbent gate's three teeth, which tick 60/30/120 frames from t0=100000.
29 // Found 2026-08-28 by nx_enginelab_gate T13 replaying captured frame marks through this meter.
30 while i < m[1] {
31 let t: i64 = m[2 + i]
32 if t >= lo { if t <= now_ms { c = c + 1 } }
33 i = i + 1
34 }
35 return c
36}