From 1818256f5ea228a26d660a1eff5d8fdec58e52ba Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 10 May 2021 14:18:05 +0800 Subject: [PATCH] kernel: mem: add ram_used_by() function This can be used to determine the amount of ram used by a particular process ID. Signed-off-by: Sean Cross --- kernel/src/mem.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/kernel/src/mem.rs b/kernel/src/mem.rs index ca05c0a57..cdf39b5b7 100644 --- a/kernel/src/mem.rs +++ b/kernel/src/mem.rs @@ -114,6 +114,18 @@ impl MemoryManager { MEMORY_MANAGER.with(|ss| f(&mut ss.borrow_mut())) } + pub fn with(f: F) -> R + where + F: FnOnce(&MemoryManager) -> R, + { + #[cfg(baremetal)] + unsafe { + f(&MEMORY_MANAGER) + } + + #[cfg(not(baremetal))] + MEMORY_MANAGER.with(|ss| f(&ss.borrow_mut())) + } #[cfg(baremetal)] pub fn init_from_memory( &mut self, @@ -168,6 +180,20 @@ impl MemoryManager { Ok(()) } + /// Print the number of RAM bytes used by the specified process. + /// This does not include memory such as peripherals and CSRs. + pub fn ram_used_by(&self, pid: PID) -> usize { + let mut owned_bytes = 0; + unsafe { + for owner in &MEMORY_ALLOCATIONS[0..self.ram_size / PAGE_SIZE] { + if owner == &Some(pid) { + owned_bytes += PAGE_SIZE; + } + } + } + owned_bytes + } + #[cfg(all(baremetal, feature = "print-debug"))] pub fn print_ownership(&self) { println!("Ownership ({} bytes in all):", unsafe {