nx_yield_test.nx source
↩ module page · 71 lines · 2676 B
1// nx_yield_test.nx -- smoke for nx_yield.
2
3import "nx_syscalls.nx"
4import "nx_attention_class.nx"
5import "nx_yield.nx"
6
7func main() -> i64 {
8 // 1: contract construction
9 let now: nx_size = 1000
10 let c: *NxYieldContract = nx_yield_contract_new(
11 7, NX_AC_BACKGROUND_INFERENCE, 5000, now)
12 if c.cell_id != 7 { return 1 }
13 if c.attention_class != NX_AC_BACKGROUND_INFERENCE { return 2 }
14 if c.quantum_us != 5000 { return 3 }
15 if c.misses != 0 { return 4 }
16
17 // 2: lower-priority requester denied
18 let v1: nx_int = nx_yield_request(c,
19 NX_AC_IDLE_OPPORTUNISTIC, now + 100)
20 if v1 != NX_YIELD_DENIED { return 5 }
21
22 // 3: equal-priority requester denied (anti-livelock)
23 let v2: nx_int = nx_yield_request(c,
24 NX_AC_BACKGROUND_INFERENCE, now + 200)
25 if v2 != NX_YIELD_DENIED { return 6 }
26
27 // 4: higher-priority WITHIN quantum granted, no miss recorded
28 let v3: nx_int = nx_yield_request(c,
29 NX_AC_INTERACTIVE_FOREGROUND_GAME, now + 1000)
30 if v3 != NX_YIELD_GRANTED { return 7 }
31 if c.misses != 0 { return 8 }
32
33 // 5: higher-priority PAST quantum -> DEMOTED on first miss
34 // Use a fresh contract so we know the clock state.
35 let c2: *NxYieldContract = nx_yield_contract_new(
36 8, NX_AC_BACKGROUND_INFERENCE, 5000, 0)
37 let v4: nx_int = nx_yield_request(c2,
38 NX_AC_INTERACTIVE_FOREGROUND_GAME, 6000) // 6ms held vs 5ms quantum
39 if v4 != NX_YIELD_DEMOTED { return 9 }
40 if c2.misses != 1 { return 10 }
41
42 // 6: second miss past quantum keeps demoted (not yet throttle)
43 let v5: nx_int = nx_yield_request(c2,
44 NX_AC_INTERACTIVE_FOREGROUND_GAME, 12000)
45 if v5 != NX_YIELD_DEMOTED { return 11 }
46 if c2.misses != 2 { return 12 }
47
48 // 7: third miss -> THROTTLED, misses reset, throttle_until set
49 let v6: nx_int = nx_yield_request(c2,
50 NX_AC_INTERACTIVE_FOREGROUND_GAME, 18000)
51 if v6 != NX_YIELD_THROTTLED { return 13 }
52 if c2.misses != 0 { return 14 }
53 if c2.throttle_until_us != 68000 { return 15 } // 18000 + 50000
54
55 // 8: during throttle window even EQUAL-priority requester wins
56 let v7: nx_int = nx_yield_request(c2,
57 NX_AC_BACKGROUND_INFERENCE, 20000)
58 if v7 != NX_YIELD_GRANTED { return 16 }
59 if nx_yield_is_throttled(c2, 20000) != 1 { return 17 }
60 if nx_yield_is_throttled(c2, 100000) != 0 { return 18 }
61
62 // 9: checkpoint resets held time and decrements misses
63 let c3: *NxYieldContract = nx_yield_contract_new(
64 9, NX_AC_DEV, 5000, 0)
65 c3.misses = 2
66 nx_yield_checkpoint(c3, 1000)
67 if c3.misses != 1 { return 19 }
68 if c3.last_grant_tick_us != 1000 { return 20 }
69
70 return 0
71}