code wiki / (root) / nx_fpsmeter.nx

nx_fpsmeter.nx source

↩ module page · 28 lines · 1126 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 while i < FM_CAP { 23 let t: i64 = m[2 + i] 24 if t >= lo { if t <= now_ms { c = c + 1 } } 25 i = i + 1 26 } 27 return c 28}