Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/crates/allen-reflex-kernel/tests/heap_budget.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / crates / allen-reflex-kernel / tests / heap_budget.rs` 145 lines ยท 5801 bytes Raw Blame History 1 //! The kernel's run, on the host, under the kernel's own heap: this test installs the same bump 2 //! heap as the global allocator over a region of the size the kernel hands over 3 //! (`ALLEN_HEAP_BYTES` in `native/kernel/core/allen.c`) and runs the kernel's sequence: card index, 4 //! then per mode train + every case twice with the per-decision mark/reset. It checks that the 5 //! run fits, that every decision returns the heap to its mark (repeated decisions do not grow it), 6 //! that the second pass gives the same lines, and prints the peaks used in the docs. 7 //! Single-threaded on purpose (`harness = false`): the bump heap has no locking. Allocations 8 //! made by the std runtime before `main` hands the region over go to the system allocator. 9 use allen_reflex_kernel::heap::Bump; 10 use allen_reflex_kernel::{card, runner}; 11 use std::alloc::{GlobalAlloc, Layout, System}; 12 use std::cell::UnsafeCell; 13 14 /// Must equal ALLEN_HEAP_BYTES in native/kernel/core/allen.c. 15 const ALLEN_HEAP_BYTES: usize = 32 << 20; 16 17 struct Region(UnsafeCell<[u64; ALLEN_HEAP_BYTES / 8]>); 18 unsafe impl Sync for Region {} 19 static REGION: Region = Region(UnsafeCell::new([0; ALLEN_HEAP_BYTES / 8])); 20 21 struct H { 22 main: Bump, 23 } 24 unsafe impl Sync for H {} 25 26 fn mine(p: *mut u8) -> bool { 27 let b = REGION.0.get() as usize; 28 (b..b + ALLEN_HEAP_BYTES).contains(&(p as usize)) 29 } 30 31 unsafe impl GlobalAlloc for H { 32 unsafe fn alloc(&self, l: Layout) -> *mut u8 { 33 if self.main.stats().cap == 0 { 34 return System.alloc(l); 35 } 36 self.main.alloc(l.size(), l.align()) as *mut u8 37 } 38 unsafe fn dealloc(&self, p: *mut u8, l: Layout) { 39 if mine(p) { 40 self.main.dealloc(p as usize, l.size()) 41 } else { 42 System.dealloc(p, l) 43 } 44 } 45 unsafe fn realloc(&self, p: *mut u8, l: Layout, n: usize) -> *mut u8 { 46 if !mine(p) { 47 return System.realloc(p, l, n); 48 } 49 let q = self.main.resize_in_place(p as usize, l.size(), n); 50 if q != 0 { 51 return q as *mut u8; 52 } 53 let m = self.main.alloc(n, l.align()); 54 if m == 0 { 55 return std::ptr::null_mut(); 56 } 57 std::ptr::copy_nonoverlapping(p, m as *mut u8, l.size().min(n)); 58 self.main.dealloc(p as usize, l.size()); 59 m as *mut u8 60 } 61 } 62 63 #[global_allocator] 64 static A: H = H { main: Bump::new() }; 65 66 fn main() { 67 // The region is handed over before this unit allocates anything (as the kernel does). 68 A.main.init(REGION.0.get() as usize, ALLEN_HEAP_BYTES); 69 // std allocates the stdout buffer on the first print: do it now, outside the measured marks 70 println!("heap_budget: region {ALLEN_HEAP_BYTES} bytes"); 71 let exam = concat!( 72 env!("CARGO_MANIFEST_DIR"), 73 "/../../docs/native/pi5-8/orchestrator-exam/" 74 ); 75 let mut buf = b"AIENALX1 QUALIF-ALLEN-EXAM\n".to_vec(); 76 for split in ["cases-train.psv", "cases-dev.psv"] { 77 let s = std::fs::read_to_string(format!("{exam}{split}")).unwrap(); 78 for l in s.lines().filter(|l| !l.starts_with('#') && !l.is_empty()) { 79 let f: Vec<&str> = l.splitn(5, '|').collect(); 80 buf.extend_from_slice(format!("{}|{}\n", f[0], f[4]).as_bytes()); 81 } 82 } 83 // the longest allowed turns field, with more turns than the core reads 84 let long: String = std::iter::repeat_n("approve the plan ~~ ok", 180) 85 .collect::>() 86 .join(" ~~ "); 87 let long = &long[..4096]; 88 buf.extend_from_slice(format!("MAXTURNS|{long}\n").as_bytes()); 89 let cases = card::parse(&buf).expect("card"); 90 let mut fails = 0; 91 let mut line = String::with_capacity(8192); 92 // pass-1 lines kept in buffers allocated before any mark, so no decision leaves a live block 93 let mut first = String::with_capacity(64 << 10); 94 let mut ends: Vec = Vec::with_capacity(cases.len()); 95 let after_cases = A.main.mark(); 96 for mode in 0..3 { 97 assert!(A.main.reset_to(after_cases), "heap back to the case index"); 98 A.main.reset_peaks(); 99 let sess = runner::Session::train(runner::mode_of(mode).unwrap()).expect("train"); 100 let after_train = A.main.stats(); 101 first.clear(); 102 ends.clear(); 103 for pass in 0..2 { 104 for (k, c) in cases.iter().enumerate() { 105 let id = std::str::from_utf8(&buf[c.id.0..c.id.1]).unwrap(); 106 let turns = std::str::from_utf8(&buf[c.turns.0..c.turns.1]).unwrap(); 107 let m = A.main.mark(); 108 let json = sess.decide_json(turns); 109 runner::line(&mut line, sess.mode(), id, &json); 110 drop(json); 111 if pass == 0 { 112 first.push_str(&line); 113 ends.push(first.len()); 114 } else { 115 let s = if k == 0 { 0 } else { ends[k - 1] }; 116 if &first[s..ends[k]] != line.as_str() { 117 fails += 1; 118 } 119 } 120 if !A.main.reset_to(m) { 121 fails += 1; 122 } 123 } 124 } 125 let st = A.main.stats(); 126 assert_eq!(st.fails, 0, "no refused allocation"); 127 // stdout of a harness=false test is shown by cargo test --nocapture style runs and CI logs 128 println!( 129 "heap_budget mode={} cap={} used_after_train={} peak={} live_peak={} memory_bytes={} cases={}", 130 sess.mode().name(), 131 st.cap, 132 after_train.used, 133 st.peak, 134 st.live_peak, 135 sess.memory_bytes(), 136 cases.len() 137 ); 138 drop(sess); 139 } 140 assert_eq!( 141 fails, 0, 142 "pass 2 identical and every decision reset the heap" 143 ); 144 println!("heap_budget: PASS"); 145 }