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/aienos-c1-tree/src/pool.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 / aienos-c1-tree / src / pool.rs` 100 lines ยท 3279 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 //! Physical page pool managing accelerator memory allocation and reference counting. 17 18 use crate::page::{PageId, PhysicalKvPage, PAGE_KV_BYTES}; 19 use std::collections::HashMap; 20 21 /// Pool managing physical KV pages with atomic reference tracking. 22 #[derive(Default)] 23 pub struct PhysicalPagePool { 24 pages: HashMap, 25 next_id: u64, 26 } 27 28 impl PhysicalPagePool { 29 /// Initialize an empty physical page pool. 30 pub fn new() -> Self { 31 Self { 32 pages: HashMap::new(), 33 next_id: 1, 34 } 35 } 36 37 /// Allocate a new physical KV page. 38 pub fn allocate(&mut self) -> PageId { 39 let id = PageId(self.next_id); 40 self.next_id += 1; 41 self.pages.insert(id, PhysicalKvPage::new(id)); 42 id 43 } 44 45 /// Reference an existing page by ID. 46 pub fn get(&self, id: PageId) -> Option<&PhysicalKvPage> { 47 self.pages.get(&id) 48 } 49 50 /// Mutable reference to an existing page by ID. 51 pub fn get_mut(&mut self, id: PageId) -> Option<&mut PhysicalKvPage> { 52 self.pages.get_mut(&id) 53 } 54 55 /// Increment reference count when sharing a page across branches. 56 pub fn retain(&mut self, id: PageId) { 57 if let Some(p) = self.pages.get_mut(&id) { 58 p.ref_count += 1; 59 } 60 } 61 62 /// Decrement reference count. If count reaches 0, free page frame immediately. 63 pub fn release(&mut self, id: PageId) { 64 if let Some(p) = self.pages.get_mut(&id) { 65 p.ref_count = p.ref_count.saturating_sub(1); 66 if p.ref_count == 0 { 67 self.pages.remove(&id); 68 } 69 } 70 } 71 72 /// Perform Copy-On-Write: clone page contents into a new private frame, 73 /// decrementing the shared parent's ref_count. 74 pub fn clone_on_write(&mut self, source_id: PageId) -> PageId { 75 let new_id = PageId(self.next_id); 76 self.next_id += 1; 77 78 let mut new_page = PhysicalKvPage::new(new_id); 79 if let Some(source) = self.pages.get(&source_id) { 80 new_page.tokens = source.tokens; 81 new_page.len = source.len; 82 new_page.kv_cache = source.kv_cache.clone(); 83 new_page.recompute_checksum(); 84 } 85 86 self.release(source_id); 87 self.pages.insert(new_id, new_page); 88 new_id 89 } 90 91 /// Total active physical pages currently residing in accelerator memory. 92 pub fn allocated_page_count(&self) -> usize { 93 self.pages.len() 94 } 95 96 /// Total physical bytes occupied by active KV pages. 97 pub fn physical_memory_bytes(&self) -> usize { 98 self.pages.len() * PAGE_KV_BYTES 99 } 100 }