diff --git a/src/allocator/frame/buddy.rs b/src/allocator/frame/buddy.rs index 6153b19d4b..1832012a96 100644 --- a/src/allocator/frame/buddy.rs +++ b/src/allocator/frame/buddy.rs @@ -8,6 +8,7 @@ use crate::{ BumpAllocator, FrameAllocator, FrameCount, + FrameUsage, PhysicalAddress, VirtualAddress, }; @@ -268,4 +269,19 @@ impl FrameAllocator for BuddyAllocator { } } } + + unsafe fn usage(&self) -> FrameUsage { + let mut total = 0; + let mut used = 0; + for i in 0 .. Self::BUDDY_ENTRIES { + let virt = self.table_virt.add(i * mem::size_of::>()); + let entry = A::read::>(virt); + total += entry.size >> A::PAGE_SHIFT; + used += entry.used; + } + FrameUsage { + used: FrameCount::new(used), + total: FrameCount::new(total), + } + } } diff --git a/src/allocator/frame/bump.rs b/src/allocator/frame/bump.rs index ebeff49f52..5231187c87 100644 --- a/src/allocator/frame/bump.rs +++ b/src/allocator/frame/bump.rs @@ -4,6 +4,7 @@ use crate::{ Arch, FrameAllocator, FrameCount, + FrameUsage, MemoryArea, PhysicalAddress, }; @@ -56,4 +57,16 @@ impl FrameAllocator for BumpAllocator { unsafe fn free(&mut self, _address: PhysicalAddress, _count: FrameCount) { unimplemented!("BumpAllocator::free not implemented"); } + + unsafe fn usage(&self) -> FrameUsage { + let mut total = 0; + for area in self.areas.iter() { + total += area.size >> A::PAGE_SHIFT; + } + let used = self.offset >> A::PAGE_SHIFT; + FrameUsage { + used: FrameCount::new(used), + total: FrameCount::new(total), + } + } } diff --git a/src/allocator/frame/mod.rs b/src/allocator/frame/mod.rs index 33d07943bf..bdb772675e 100644 --- a/src/allocator/frame/mod.rs +++ b/src/allocator/frame/mod.rs @@ -20,6 +20,25 @@ impl FrameCount { } } +pub struct FrameUsage { + used: FrameCount, + total: FrameCount, +} + +impl FrameUsage { + pub fn used(&self) -> FrameCount { + self.used + } + + pub fn free(&self) -> FrameCount { + FrameCount(self.total.0 - self.used.0) + } + + pub fn total(&self) -> FrameCount { + self.total + } +} + pub trait FrameAllocator { unsafe fn allocate(&mut self, count: FrameCount) -> Option; @@ -32,4 +51,6 @@ pub trait FrameAllocator { unsafe fn free_one(&mut self, address: PhysicalAddress) { self.free(address, FrameCount::new(1)); } + + unsafe fn usage(&self) -> FrameUsage; } diff --git a/src/main.rs b/src/main.rs index 1c7b72d425..f6b94891da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -253,6 +253,12 @@ unsafe fn new_tables(areas: &'static [MemoryArea]) { flush_all.consume(flush); } flush_all.flush(); + + let usage = allocator.usage(); + println!("Allocator usage:"); + println!(" Used: {}", format_size(usage.used().data() * A::PAGE_SIZE)); + println!(" Free: {}", format_size(usage.free().data() * A::PAGE_SIZE)); + println!(" Total: {}", format_size(usage.total().data() * A::PAGE_SIZE)); } unsafe fn inner() {