From 9d37968084e204c6b966ab07ae82b473a1ec3fac Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 20 Aug 2025 02:13:04 +0000 Subject: [PATCH] some todo changes --- kernel/src/console.rs | 6 ++++++ kernel/src/memory/kmalloc.rs | 22 ++++++++++++++++++++++ kernel/src/memory/vmalloc.rs | 14 ++++++++++++++ kernel/src/shell.rs | 24 +++++++++++++++++++++--- 4 files changed, 63 insertions(+), 3 deletions(-) diff --git a/kernel/src/console.rs b/kernel/src/console.rs index 3a8ddbb..1eb553a 100644 --- a/kernel/src/console.rs +++ b/kernel/src/console.rs @@ -267,6 +267,12 @@ pub fn write_str(s: &str) { console.write_str(s); } +/// Clear the console screen +pub fn clear() { + let mut console = CONSOLE.lock(); + console.clear_screen(); +} + struct ConsoleWriter<'a>(&'a mut Console); impl Write for ConsoleWriter<'_> { diff --git a/kernel/src/memory/kmalloc.rs b/kernel/src/memory/kmalloc.rs index d98a8fc..6c737b0 100644 --- a/kernel/src/memory/kmalloc.rs +++ b/kernel/src/memory/kmalloc.rs @@ -90,10 +90,32 @@ impl SlabAllocator { Err(Error::InvalidArgument) } } + + pub fn stats(&self) -> (usize, usize, usize) { + let mut allocated_count = 0; + let mut allocated_bytes = 0; + for (_, size_class) in &self.allocated_blocks { + allocated_count += 1; + allocated_bytes += size_class; + } + + let mut free_count = 0; + for (_, free_list) in &self.size_classes { + free_count += free_list.len(); + } + + (allocated_count, allocated_bytes, free_count) + } } static SLAB_ALLOCATOR: Spinlock = Spinlock::new(SlabAllocator::new()); +/// Get kmalloc statistics +pub fn get_stats() -> (usize, usize, usize) { + let allocator = SLAB_ALLOCATOR.lock(); + allocator.stats() +} + /// Allocate kernel memory pub fn kmalloc(size: usize) -> Result<*mut u8> { // Increment performance counter diff --git a/kernel/src/memory/vmalloc.rs b/kernel/src/memory/vmalloc.rs index 2b0487c..745d598 100644 --- a/kernel/src/memory/vmalloc.rs +++ b/kernel/src/memory/vmalloc.rs @@ -128,10 +128,24 @@ impl VmallocAllocator { self.next_addr = addr + size; Ok(addr) } + + pub fn stats(&self) -> (usize, usize) { + let mut allocated_bytes = 0; + for (_, area) in &self.areas { + allocated_bytes += area.size; + } + (self.areas.len(), allocated_bytes) + } } static VMALLOC_ALLOCATOR: Spinlock = Spinlock::new(VmallocAllocator::new()); +/// Get vmalloc statistics +pub fn get_stats() -> (usize, usize) { + let allocator = VMALLOC_ALLOCATOR.lock(); + allocator.stats() +} + /// Allocate virtual memory pub fn vmalloc(size: usize) -> Result { let mut allocator = VMALLOC_ALLOCATOR.lock(); diff --git a/kernel/src/shell.rs b/kernel/src/shell.rs index 5bd3266..f8a55a2 100644 --- a/kernel/src/shell.rs +++ b/kernel/src/shell.rs @@ -231,8 +231,27 @@ impl KernelShell { info!(" Total pages: {}", total); info!(" Allocated pages: {}", allocated); info!(" Free pages: {}", free); + info!( + " Memory usage: {} / {} KB", + (allocated * 4096) / 1024, + (total * 4096) / 1024 + ); - // TODO: Add more memory statistics (kmalloc, vmalloc, etc.) + let (kmalloc_alloc_count, kmalloc_alloc_bytes, kmalloc_free_count) = + crate::memory::kmalloc::get_stats(); + info!("\nKmalloc (slab) statistics:"); + info!( + " Allocated: {} blocks ({} bytes)", + kmalloc_alloc_count, kmalloc_alloc_bytes + ); + info!(" Free: {} blocks", kmalloc_free_count); + + let (vmalloc_areas, vmalloc_bytes) = crate::memory::vmalloc::get_stats(); + info!("\nVmalloc statistics:"); + info!( + " Allocated: {} areas ({} bytes)", + vmalloc_areas, vmalloc_bytes + ); } /// Process command @@ -258,8 +277,7 @@ impl KernelShell { /// Clear command fn cmd_clear(&self) { - // TODO: Clear console screen - info!("Clear screen not implemented yet"); + crate::console::clear(); } /// Network command