code wiki / (root) / nx_organism_test.nx

nx_organism_test.nx source

↩ module page · 113 lines · 4806 B

1// nx_organism_test.nx -- smoke for nx_organism. 2 3import "nx_syscalls.nx" 4import "nx_tier.nx" 5import "nx_budget.nx" 6import "nx_attention_class.nx" 7import "nx_evict_journal.nx" 8import "nx_tropism.nx" 9import "nx_ribosome.nx" 10import "nx_pathway.nx" 11import "nx_symbiote.nx" 12import "nx_organism.nx" 13 14func _add_cell(p: *NxPathway, cell_id: nx_int, ac: nx_int, 15 ram_max: nx_size, vram_max: nx_size) -> nx_int { 16 let b: *NxBudget = nx_budget_new(cell_id, ram_max, vram_max, 0, 0, 0) 17 let s: *NxCellSpec = (sys_mmap(64)) as *NxCellSpec 18 s.cell_id = cell_id 19 s.attention_class = ac 20 s.tropism_prefer = NX_TROP_LOCAL_CPU 21 s.ribosome_target = NX_RBT_X86_64 22 s.budget = b 23 s.site_id_hint = 0 24 s.initial_tier = NX_TIER_WORKSTATION 25 return nx_pathway_add_cell(p, s) 26} 27 28func main() -> i64 { 29 // 1: construct organism with a 16GB RAM host budget 30 let host: *NxBudget = nx_budget_new(0, 31 16000000000, 12000000000, 0, 0, 0) 32 let journal: *NxEvictJournal = nx_evict_journal_new(16) 33 let o: *NxOrganism = nx_organism_new(host, 4, 4, journal) 34 if o.n_pathways != 0 { return 1 } 35 if o.n_symbiotes != 0 { return 2 } 36 if nx_organism_count_members(o) != 0 { return 3 } 37 38 // 2: add a pathway with two cells (foreground game + idle build) 39 let p1: *NxPathway = nx_pathway_new(8, 16) 40 _add_cell(p1, 100, NX_AC_INTERACTIVE_FOREGROUND_GAME, 4000000000, 4000000000) 41 _add_cell(p1, 101, NX_AC_BACKGROUND_BUILD, 2000000000, 0) 42 if nx_organism_add_pathway(o, p1) != NX_ORG_OK { return 4 } 43 if o.n_pathways != 1 { return 5 } 44 45 // 3: NULL pathway rejected 46 let null_p: *NxPathway = (0 as i64) as *NxPathway 47 if nx_organism_add_pathway(o, null_p) != NX_ORG_ERR_BAD_MEMBER { return 6 } 48 49 // 4: aggregate RAM = 4G + 2G = 6G 50 if nx_organism_total_ram_max(o) != 6000000000 { return 7 } 51 // aggregate VRAM = 4G + 0 = 4G 52 if nx_organism_total_vram_max(o) != 4000000000 { return 8 } 53 54 // 5: add Steam-NMS symbiote (8G RAM declared, 6G VRAM declared) 55 let nms_budget: *NxBudget = nx_budget_new(2000, 56 8000000000, 6000000000, 0, 80000000000, 0) 57 let nms: *NxSymbiote = nx_symbiote_new(2000, 58 (0 as i64) as *u8, 59 NX_AC_INTERACTIVE_FOREGROUND_GAME, nms_budget, 0) 60 nx_symbiote_update(nms, 5500000000, 4500000000, 0, 0, 100) 61 if nx_organism_add_symbiote(o, nms) != NX_ORG_OK { return 9 } 62 if o.n_symbiotes != 1 { return 10 } 63 64 // 6: aggregate now includes symbiote DECLARED max (not observed) 65 if nx_organism_total_ram_max(o) != 14000000000 { return 11 } // 6G + 8G 66 if nx_organism_total_vram_max(o) != 10000000000 { return 12 } // 4G + 6G 67 68 // 7: observed totals reflect symbiote ACTUAL consumption 69 if nx_organism_total_observed_ram(o) != 11500000000 { return 13 } 70 // 6G (native max as conservative bound) + 5.5G symbiote observed 71 if nx_organism_total_observed_vram(o) != 8500000000 { return 14 } 72 // 4G native + 4.5G observed = 8.5G 73 74 // 8: host RAM pressure -- observed 11.5G / max 16G ~ 0.719 Q10 736 75 let ram_q: nx_int = nx_organism_host_pressure_q10(o, NX_RES_RAM) 76 if ram_q < 730 { return 15 } 77 if ram_q > 745 { return 16 } 78 79 // 9: host VRAM pressure -- 8.5G / 12G ~ 0.708 Q10 725 80 let vram_q: nx_int = nx_organism_host_pressure_q10(o, NX_RES_VRAM) 81 if vram_q < 720 { return 17 } 82 if vram_q > 735 { return 18 } 83 84 // 10: at moderate pressure (below 90%), pick_displacement returns NONE 85 let out_id: *i64 = (sys_mmap(8)) as *i64 86 out_id[0] = 0 87 let v1: nx_int = nx_organism_pick_displacement(o, NX_RES_RAM, out_id) 88 if v1 != NX_DISP_NONE { return 19 } 89 90 // 11: raise NMS observation -- now consuming 13G VRAM (overcommit) 91 nx_symbiote_update(nms, 13000000000, 12000000000, 0, 0, 200) 92 // host VRAM observed: 4G + 12G = 16G vs 12G max -> Q10 1365 > 1024 93 let vram_q2: nx_int = nx_organism_host_pressure_q10(o, NX_RES_VRAM) 94 if vram_q2 < 921 { return 20 } 95 96 // 12: pick_displacement returns the BACKGROUND_BUILD cell (prio 4) 97 // because it's already the lowest-priority member -- below both 98 // foreground NMS (prio 0) and foreground voxel (prio 0). 99 let v2: nx_int = nx_organism_pick_displacement(o, NX_RES_VRAM, out_id) 100 if v2 != NX_DISP_NATIVE_CELL { return 21 } 101 if out_id[0] != 101 { return 22 } // BACKGROUND_BUILD cell id 102 103 // 13: add a strictly LOWER priority idle cell -> it becomes target 104 _add_cell(p1, 102, NX_AC_IDLE_OPPORTUNISTIC, 1000000000, 1000000000) 105 let v3: nx_int = nx_organism_pick_displacement(o, NX_RES_VRAM, out_id) 106 if v3 != NX_DISP_NATIVE_CELL { return 23 } 107 if out_id[0] != 102 { return 24 } // idle cell id 108 109 // 14: total member count 110 if nx_organism_count_members(o) != 2 { return 25 } // 1 pathway + 1 symbiote 111 112 return 0 113}