Kernel: Shell 'memory' now prints heap memory usage

This commit is contained in:
Bananymous 2023-06-03 02:22:18 +03:00
parent d4123f62b2
commit fb1c7015b1
5 changed files with 40 additions and 1 deletions

View File

@ -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<PhysicalRange> m_physical_ranges;
SpinLock m_lock;
mutable SpinLock m_lock;
};
}

View File

@ -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 };
};

View File

@ -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;
}
}

View File

@ -37,6 +37,9 @@ namespace Kernel
m_list_pages = BAN::Math::div_round_up<uint64_t>(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

View File

@ -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")
{