code wiki / (root) / nx_metabolism_test.nx

nx_metabolism_test.nx source

↩ module page · 77 lines · 2571 B

1// nx_metabolism_test.nx -- smoke for nx_metabolism. 2 3import "nx_syscalls.nx" 4import "nx_tier.nx" 5import "nx_attention_class.nx" 6import "nx_metabolism.nx" 7 8func main() -> i64 { 9 // 1: construction 10 let m: *NxMetabolism = nx_metab_new(16) 11 if m.capacity != 16 { return 1 } 12 if m.count != 0 { return 2 } 13 14 // 2: first record creates a row 15 nx_metab_record(m, 1001, 500, 1024, 16 NX_AC_BACKGROUND_INFERENCE) 17 if m.count != 1 { return 3 } 18 if nx_metab_hit_count(m, 1001) != 1 { return 4 } 19 if nx_metab_avg_cycles(m, 1001) != 500 { return 5 } 20 21 // 3: subsequent records update same row 22 nx_metab_record(m, 1001, 1500, 2048, 23 NX_AC_BACKGROUND_INFERENCE) 24 if m.count != 1 { return 6 } 25 if nx_metab_hit_count(m, 1001) != 2 { return 7 } 26 if nx_metab_avg_cycles(m, 1001) != 1000 { return 8 } 27 28 // 4: cold site stays at family-device baseline 29 let t_cold: nx_int = nx_metab_suggest_target(m, 1001) 30 if t_cold != NX_TIER_FAMILY_DEVICE { return 9 } 31 32 // 5: warm site (>=64 hits) suggests workstation 33 var i: nx_int = 0 34 while i < 70 { 35 nx_metab_record(m, 2002, 100, 256, NX_AC_DEV) 36 i = i + 1 37 } 38 let t_warm: nx_int = nx_metab_suggest_target(m, 2002) 39 if t_warm != NX_TIER_WORKSTATION { return 10 } 40 41 // 6: hot site (>=4096 hits) suggests server tier 42 i = 0 43 while i < 4200 { 44 nx_metab_record(m, 3003, 50, 128, NX_AC_DEV) 45 i = i + 1 46 } 47 let t_hot: nx_int = nx_metab_suggest_target(m, 3003) 48 if t_hot != NX_TIER_SERVER { return 11 } 49 50 // 7: unknown site defaults to workstation 51 let t_unk: nx_int = nx_metab_suggest_target(m, 9999) 52 if t_unk != NX_TIER_WORKSTATION { return 12 } 53 54 // 8: capacity-exceeded returns FULL 55 let m2: *NxMetabolism = nx_metab_new(2) 56 nx_metab_record(m2, 1, 10, 0, NX_AC_DEV) 57 nx_metab_record(m2, 2, 10, 0, NX_AC_DEV) 58 let rc: nx_int = nx_metab_record(m2, 3, 10, 0, NX_AC_DEV) 59 if rc != NX_METAB_ERR_FULL { return 13 } 60 61 // 9: ram_peak tracks max not last 62 nx_metab_record(m, 4004, 100, 5000, NX_AC_DEV) 63 nx_metab_record(m, 4004, 100, 100, NX_AC_DEV) 64 nx_metab_record(m, 4004, 100, 3000, NX_AC_DEV) 65 // Verify via private struct field; metabolism does not expose a 66 // getter for ram_peak in V1, so we check via the underlying slot. 67 var j: nx_size = 0 68 while j < m.count { 69 let s: *NxCallSite = (m.sites as i64 + (j as i64) * 48) as *NxCallSite 70 if s.site_id == 4004 { 71 if s.ram_peak != 5000 { return 14 } 72 } 73 j = j + 1 74 } 75 76 return 0 77}