nx_symbiote_test.nx source
↩ module page · 99 lines · 3853 B
1// nx_symbiote_test.nx -- smoke for nx_symbiote.
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_symbiote.nx"
9
10func main() -> i64 {
11 // 1: state enum sealed
12 if NX_SYM_N_STATES != 5 { return 1 }
13 if nx_sym_state_is_valid(NX_SYM_RUNNING) != 1 { return 2 }
14 if nx_sym_state_is_valid(NX_SYM_GONE) != 1 { return 3 }
15 if nx_sym_state_is_valid(-1) != 0 { return 4 }
16 if nx_sym_state_is_valid(5) != 0 { return 5 }
17
18 // 2: construct (Steam-NMS scenario)
19 let name: *u8 = (sys_mmap(16)) as *u8
20 name[0] = 110 as u8 // 'n'
21 name[1] = 109 as u8 // 'm'
22 name[2] = 115 as u8 // 's'
23 name[3] = 0 as u8
24 let budget: *NxBudget = nx_budget_new(1234,
25 8000000000, 6000000000, 16000, 80000000000, 1000000)
26 let s: *NxSymbiote = nx_symbiote_new(1234, name,
27 NX_AC_INTERACTIVE_FOREGROUND_GAME, budget, 0)
28 if s.pid != 1234 { return 6 }
29 if s.state != NX_SYM_RUNNING { return 7 }
30 if s.observed_ram_bytes != 0 { return 8 }
31 if s.cooperative != 0 { return 9 }
32
33 // 3: update observed -- NMS consuming 5.5GB RAM 4.5GB VRAM
34 nx_symbiote_update(s, 5500000000, 4500000000, 9000, 40000000000, 100000)
35 if s.observed_ram_bytes != 5500000000 { return 10 }
36 if s.observed_vram_bytes != 4500000000 { return 11 }
37 if s.last_observe_us != 100000 { return 12 }
38
39 // 4: overshoot computation (5.5GB used / 8GB max = 0.6875 = Q10 704)
40 let ram_q: nx_int = nx_symbiote_overshoot_q10(s, NX_RES_RAM)
41 if ram_q < 700 { return 13 }
42 if ram_q > 710 { return 14 }
43
44 // 5: overshoot at >1024 detects breach (push VRAM past declared)
45 nx_symbiote_update(s, 5500000000, 7000000000, 9000, 40000000000, 200000)
46 let vram_q: nx_int = nx_symbiote_overshoot_q10(s, NX_RES_VRAM)
47 if vram_q <= 1024 { return 15 } // observed > declared = overshoot
48
49 // 6: throttle transitions state and logs to journal
50 let j: *NxEvictJournal = nx_evict_journal_new(8)
51 let rc1: nx_int = nx_symbiote_throttle(s, j, 300000, 5555)
52 if rc1 != NX_SYM_OK { return 16 }
53 if s.state != NX_SYM_THROTTLED { return 17 }
54 if j.count != 1 { return 18 }
55 let e1: *NxEvictEntry = nx_evict_at(j, 0)
56 if e1.cell_id != 1234 { return 19 }
57 if e1.reason != NX_EVR_YIELD_THROTTLED { return 20 }
58 if e1.displaced_by != 5555 { return 21 }
59
60 // 7: not responsive when throttled
61 if nx_symbiote_is_responsive(s) != 0 { return 22 }
62
63 // 8: pause from throttled is OK
64 let rc2: nx_int = nx_symbiote_pause(s, j, 400000, 5555)
65 if rc2 != NX_SYM_OK { return 23 }
66 if s.state != NX_SYM_PAUSED { return 24 }
67
68 // 9: resume goes back to RUNNING
69 nx_symbiote_resume(s)
70 if s.state != NX_SYM_RUNNING { return 25 }
71 if nx_symbiote_is_responsive(s) != 1 { return 26 }
72
73 // 10: terminate
74 let rc3: nx_int = nx_symbiote_terminate(s, j, 500000, 5555)
75 if rc3 != NX_SYM_OK { return 27 }
76 if s.state != NX_SYM_TERMINATED { return 28 }
77
78 // 11: terminate again -> ALREADY_GONE
79 let rc4: nx_int = nx_symbiote_terminate(s, j, 600000, 5555)
80 if rc4 != NX_SYM_ERR_ALREADY_GONE { return 29 }
81
82 // 12: resume rejected from terminated
83 let rc5: nx_int = nx_symbiote_resume(s)
84 if rc5 != NX_SYM_ERR_ALREADY_GONE { return 30 }
85
86 // 13: stale observation detection
87 let s2: *NxSymbiote = nx_symbiote_new(5678, name,
88 NX_AC_BACKGROUND_INFERENCE, budget, 1)
89 nx_symbiote_update(s2, 1000, 0, 0, 0, 1000)
90 if nx_symbiote_stale_observation(s2, 2000, 5000) != 0 { return 31 }
91 if nx_symbiote_stale_observation(s2, 10000, 5000) != 1 { return 32 }
92
93 // 14: unobserved symbiote is stale by default
94 let s3: *NxSymbiote = nx_symbiote_new(9999, name,
95 NX_AC_DEV, budget, 0)
96 if nx_symbiote_stale_observation(s3, 1, 5000) != 1 { return 33 }
97
98 return 0
99}