From 2b901abfb1eb1dbef6cb43edb2d36bd67b98329f Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sat, 3 Jun 2023 02:22:18 +0300 Subject: [PATCH] Kernel: Shell 'memory' now prints heap memory usage --- kernel/include/kernel/Memory/Heap.h | 5 ++++- kernel/include/kernel/Memory/PhysicalRange.h | 6 ++++++ kernel/kernel/Memory/Heap.cpp | 18 ++++++++++++++++++ kernel/kernel/Memory/PhysicalRange.cpp | 9 +++++++++ kernel/kernel/Shell.cpp | 3 +++ 5 files changed, 40 insertions(+), 1 deletion(-) diff --git a/kernel/include/kernel/Memory/Heap.h b/kernel/include/kernel/Memory/Heap.h index 92ed0d50..78003d5e 100644 --- a/kernel/include/kernel/Memory/Heap.h +++ b/kernel/include/kernel/Memory/Heap.h @@ -21,13 +21,16 @@ namespace Kernel paddr_t take_free_page(); void release_page(paddr_t); + size_t used_pages() const; + size_t free_pages() const; + private: Heap() = default; void initialize_impl(); private: BAN::Vector m_physical_ranges; - SpinLock m_lock; + mutable SpinLock m_lock; }; } diff --git a/kernel/include/kernel/Memory/PhysicalRange.h b/kernel/include/kernel/Memory/PhysicalRange.h index 39335146..317a052b 100644 --- a/kernel/include/kernel/Memory/PhysicalRange.h +++ b/kernel/include/kernel/Memory/PhysicalRange.h @@ -21,6 +21,9 @@ namespace Kernel size_t usable_memory() const { return m_reservable_pages * PAGE_SIZE; } + size_t used_pages() const { return m_used_pages; } + size_t free_pages() const { return m_free_pages; } + private: struct node { @@ -39,6 +42,9 @@ namespace Kernel uint64_t m_reservable_pages { 0 }; uint64_t m_list_pages { 0 }; + size_t m_used_pages { 0 }; + size_t m_free_pages { 0 }; + node* m_free_list { nullptr }; node* m_used_list { nullptr }; }; diff --git a/kernel/kernel/Memory/Heap.cpp b/kernel/kernel/Memory/Heap.cpp index 5c766180..08e1a7dd 100644 --- a/kernel/kernel/Memory/Heap.cpp +++ b/kernel/kernel/Memory/Heap.cpp @@ -74,4 +74,22 @@ namespace Kernel ASSERT_NOT_REACHED(); } + size_t Heap::used_pages() const + { + LockGuard _(m_lock); + size_t result = 0; + for (const auto& range : m_physical_ranges) + result += range.used_pages(); + return result; + } + + size_t Heap::free_pages() const + { + LockGuard _(m_lock); + size_t result = 0; + for (const auto& range : m_physical_ranges) + result += range.free_pages(); + return result; + } + } diff --git a/kernel/kernel/Memory/PhysicalRange.cpp b/kernel/kernel/Memory/PhysicalRange.cpp index 039db1e2..a0320206 100644 --- a/kernel/kernel/Memory/PhysicalRange.cpp +++ b/kernel/kernel/Memory/PhysicalRange.cpp @@ -37,6 +37,9 @@ namespace Kernel m_list_pages = BAN::Math::div_round_up(m_total_pages * sizeof(node), PAGE_SIZE); m_reservable_pages = m_total_pages - m_list_pages; + m_used_pages = 0; + m_free_pages = m_reservable_pages; + PageTable::kernel().identity_map_range(m_start, m_list_pages * PAGE_SIZE, PageTable::Flags::ReadWrite | PageTable::Flags::Present); // Initialize page list so that every page points to the next one @@ -72,6 +75,9 @@ namespace Kernel page->prev = m_used_list; m_used_list = page; + m_used_pages++; + m_free_pages--; + return page_address(page); } @@ -95,6 +101,9 @@ namespace Kernel if (m_free_list) m_free_list->next = page; m_free_list = page; + + m_used_pages--; + m_free_pages++; } paddr_t PhysicalRange::page_address(const node* page) const diff --git a/kernel/kernel/Shell.cpp b/kernel/kernel/Shell.cpp index 3eee4ae4..50753d32 100644 --- a/kernel/kernel/Shell.cpp +++ b/kernel/kernel/Shell.cpp @@ -415,6 +415,9 @@ argument_done: return {}; } kmalloc_dump_info(); + TTY_PRINTLN("heap:"); + TTY_PRINTLN(" used: 0x{8H}", Heap::get().used_pages() * PAGE_SIZE); + TTY_PRINTLN(" free: 0x{8H}", Heap::get().free_pages() * PAGE_SIZE); } else if (arguments.front() == "sleep") {