nx_homeostasis_test.nx source
↩ module page · 95 lines · 3760 B
1// nx_homeostasis_test.nx -- smoke for nx_homeostasis.
2
3import "nx_syscalls.nx"
4import "nx_tier.nx"
5import "nx_budget.nx"
6import "nx_evict_journal.nx"
7import "nx_metabolism.nx"
8import "nx_attention_class.nx"
9import "nx_homeostasis.nx"
10
11func main() -> i64 {
12 // 1: NULL budget -> STEADY (initialization safety)
13 let nullb: *NxBudget = (0 as i64) as *NxBudget
14 let s_null: nx_int = nx_homeo_check(nullb,
15 (0 as i64) as *NxEvictJournal,
16 (0 as i64) as *NxMetabolism, 0, 0)
17 if s_null != NX_HOMEO_STEADY { return 1 }
18
19 // 2: idle budget -> STEADY
20 let b: *NxBudget = nx_budget_new(1, 1000, 1000, 1000, 1000, 1000)
21 let s_idle: nx_int = nx_homeo_check(b,
22 (0 as i64) as *NxEvictJournal,
23 (0 as i64) as *NxMetabolism, 1, 0)
24 if s_idle != NX_HOMEO_STEADY { return 2 }
25
26 // 3: CPU pressure -> MIGRATE_THERMAL (CPU has priority)
27 nx_budget_request(b, NX_RES_CPU, 950) // 95% > 90% threshold
28 let s_cpu: nx_int = nx_homeo_check(b,
29 (0 as i64) as *NxEvictJournal,
30 (0 as i64) as *NxMetabolism, 1, 0)
31 if s_cpu != NX_HOMEO_MIGRATE_THERMAL { return 3 }
32
33 // 4: clear CPU, RAM pressure -> MIGRATE_RAM
34 nx_budget_release(b, NX_RES_CPU, 950)
35 nx_budget_request(b, NX_RES_RAM, 950)
36 let s_ram: nx_int = nx_homeo_check(b,
37 (0 as i64) as *NxEvictJournal,
38 (0 as i64) as *NxMetabolism, 1, 0)
39 if s_ram != NX_HOMEO_MIGRATE_RAM { return 4 }
40
41 // 5: clear RAM, VRAM pressure -> MIGRATE_VRAM
42 nx_budget_release(b, NX_RES_RAM, 950)
43 nx_budget_request(b, NX_RES_VRAM, 950)
44 let s_vram: nx_int = nx_homeo_check(b,
45 (0 as i64) as *NxEvictJournal,
46 (0 as i64) as *NxMetabolism, 1, 0)
47 if s_vram != NX_HOMEO_MIGRATE_VRAM { return 5 }
48
49 // 6: clear VRAM, disk pressure -> MIGRATE_DISK
50 nx_budget_release(b, NX_RES_VRAM, 950)
51 nx_budget_request(b, NX_RES_DISK, 950)
52 let s_disk: nx_int = nx_homeo_check(b,
53 (0 as i64) as *NxEvictJournal,
54 (0 as i64) as *NxMetabolism, 1, 0)
55 if s_disk != NX_HOMEO_MIGRATE_DISK { return 6 }
56
57 // 7: eviction storm with low budget pressure -> EVICTION_STORM
58 nx_budget_release(b, NX_RES_DISK, 950)
59 let j: *NxEvictJournal = nx_evict_journal_new(16)
60 var k: nx_int = 0
61 while k < 5 {
62 nx_evict_log(j, k, 1, NX_EVR_YIELD_DEMOTED, 0, 3, 0)
63 k = k + 1
64 }
65 let s_storm: nx_int = nx_homeo_check(b, j,
66 (0 as i64) as *NxMetabolism, 1, 0)
67 if s_storm != NX_HOMEO_EVICTION_STORM { return 7 }
68
69 // 8: PROMOTE_TIER fired when metabolism suggests > workstation
70 let m: *NxMetabolism = nx_metab_new(8)
71 var i: nx_int = 0
72 while i < 4200 { // crosses VECTOR threshold -> SERVER
73 nx_metab_record(m, 5555, 10, 0, NX_AC_DEV)
74 i = i + 1
75 }
76 // need empty journal so storm doesn't fire first
77 let j2: *NxEvictJournal = nx_evict_journal_new(4)
78 let s_promo: nx_int = nx_homeo_check(b, j2, m, 1, 5555)
79 if s_promo != NX_HOMEO_PROMOTE_TIER { return 8 }
80
81 // 9: migration predicate matches all four physical pressures
82 if nx_homeo_signal_is_migration(NX_HOMEO_MIGRATE_RAM) != 1 { return 9 }
83 if nx_homeo_signal_is_migration(NX_HOMEO_MIGRATE_VRAM) != 1 { return 10 }
84 if nx_homeo_signal_is_migration(NX_HOMEO_MIGRATE_DISK) != 1 { return 11 }
85 if nx_homeo_signal_is_migration(NX_HOMEO_MIGRATE_THERMAL) != 1 { return 12 }
86 if nx_homeo_signal_is_migration(NX_HOMEO_STEADY) != 0 { return 13 }
87 if nx_homeo_signal_is_migration(NX_HOMEO_PROMOTE_TIER) != 0 { return 14 }
88
89 // 10: signal validation
90 if nx_homeo_signal_is_valid(NX_HOMEO_STEADY) != 1 { return 15 }
91 if nx_homeo_signal_is_valid(NX_HOMEO_N_SIGNALS) != 0 { return 16 }
92 if nx_homeo_signal_is_valid(-1) != 0 { return 17 }
93
94 return 0
95}