From 43c452bf97360ab20f21067bd168fd51f255aa5b Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 7 May 2024 04:12:46 +0800 Subject: [PATCH] add a call to get the free memory status from the kernel this will be helpful for debugging our page tracker --- kernel/src/mem.rs | 2 ++ kernel/src/swap.rs | 57 +++++++++++++++++++++++++++++++++++++++++++ kernel/src/syscall.rs | 1 + 3 files changed, 60 insertions(+) diff --git a/kernel/src/mem.rs b/kernel/src/mem.rs index a640204c6..7f2e3d141 100644 --- a/kernel/src/mem.rs +++ b/kernel/src/mem.rs @@ -178,6 +178,8 @@ impl MemoryManager { owned_bytes } + pub fn ram_size(&self) -> usize { self.ram_size } + #[cfg(all(baremetal, feature = "print-debug"))] pub fn print_ownership(&self) { println!("Ownership ({} bytes in all):", unsafe { MEMORY_ALLOCATIONS.len() }); diff --git a/kernel/src/swap.rs b/kernel/src/swap.rs index 9a053380e..6e8f2a95e 100644 --- a/kernel/src/swap.rs +++ b/kernel/src/swap.rs @@ -10,16 +10,23 @@ use crate::arch::mem::PAGE_SIZE; use crate::mem::MemoryManager; use crate::services::SystemServices; +/// This ABI is copy-paste synchronized with what's in the userspace handler. It's left out of +/// xous-rs so that we can change it without having to push crates to crates.io. +/// Since there is only one place the ABI could be used, we're going to stick with +/// this primitive method of synchronization because it reduces the activation barrier +/// to fix bugs. #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum SwapAbi { Invalid = 0, Evict = 1, + GetFreeMem = 2, } impl SwapAbi { pub fn from(val: usize) -> SwapAbi { use SwapAbi::*; match val { 1 => Evict, + 2 => GetFreeMem, _ => Invalid, } } @@ -88,6 +95,20 @@ impl Swap { SWAP.with(|ss| f(&mut ss.borrow_mut())) } + #[cfg(baremetal)] + pub fn with(f: F) -> R + where + F: FnOnce(&Swap) -> R, + { + #[cfg(baremetal)] + unsafe { + f(&*core::ptr::addr_of!(SWAP)) + } + + #[cfg(not(baremetal))] + SWAP.with(|ss| f(&ss.borrow_mut())) + } + pub fn register_handler( &mut self, s0: u32, @@ -342,6 +363,42 @@ impl Swap { } } + pub fn get_free_mem(&self) -> SysCallResult { + println!("RAM usage:"); + let mut total_bytes = 0; + let mut ram_size = 0; + crate::services::SystemServices::with(|system_services| { + crate::mem::MemoryManager::with(|mm| { + for process in &system_services.processes { + if !process.free() { + let bytes_used = mm.ram_used_by(process.pid); + total_bytes += bytes_used; + println!( + " PID {:>3}: {:>4} k {}", + process.pid, + bytes_used / 1024, + system_services.process_name(process.pid).unwrap_or("") + ); + } + } + ram_size = mm.ram_size(); + }); + }); + println!("{} k total", total_bytes / 1024); + + /* + crate::services::SystemServices::with(|system_services| { + let current_pid = system_services.current_pid(); + let process = system_services.get_process(PID::new(9).unwrap()).unwrap(); + println!("PID {} {}:", process.pid, system_services.process_name(process.pid).unwrap_or("")); + process.activate().unwrap(); + crate::arch::mem::MemoryMapping::current().print_map(); + system_services.get_process(current_pid).unwrap().activate().unwrap(); + }); */ + + Ok(xous_kernel::Result::Scalar5(total_bytes, ram_size, 0, 0, 0)) + } + /// The address space on entry to `retrieve_page` is `target_pid`; it must ensure /// that the address space is still `target_pid` on return. /// diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 320480f9a..974591ecf 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -1029,6 +1029,7 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR } match SwapAbi::from(op) { SwapAbi::Evict => Swap::with_mut(|swap| swap.evict_page(a1, a2)), + SwapAbi::GetFreeMem => Swap::with(|swap| swap.get_free_mem()), SwapAbi::Invalid => { println!( "Invalid SwapOp: {:x} {:x} {:x} {:x} {:x} {:x} {:x}",