forked from Bananymous/banan-os
Kernel: Shell 'memory' now prints heap memory usage
This commit is contained in:
parent
d4123f62b2
commit
fb1c7015b1
|
@ -21,13 +21,16 @@ namespace Kernel
|
||||||
paddr_t take_free_page();
|
paddr_t take_free_page();
|
||||||
void release_page(paddr_t);
|
void release_page(paddr_t);
|
||||||
|
|
||||||
|
size_t used_pages() const;
|
||||||
|
size_t free_pages() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Heap() = default;
|
Heap() = default;
|
||||||
void initialize_impl();
|
void initialize_impl();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BAN::Vector<PhysicalRange> m_physical_ranges;
|
BAN::Vector<PhysicalRange> m_physical_ranges;
|
||||||
SpinLock m_lock;
|
mutable SpinLock m_lock;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,9 @@ namespace Kernel
|
||||||
|
|
||||||
size_t usable_memory() const { return m_reservable_pages * PAGE_SIZE; }
|
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:
|
private:
|
||||||
struct node
|
struct node
|
||||||
{
|
{
|
||||||
|
@ -39,6 +42,9 @@ namespace Kernel
|
||||||
uint64_t m_reservable_pages { 0 };
|
uint64_t m_reservable_pages { 0 };
|
||||||
uint64_t m_list_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_free_list { nullptr };
|
||||||
node* m_used_list { nullptr };
|
node* m_used_list { nullptr };
|
||||||
};
|
};
|
||||||
|
|
|
@ -74,4 +74,22 @@ namespace Kernel
|
||||||
ASSERT_NOT_REACHED();
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,6 +37,9 @@ namespace Kernel
|
||||||
m_list_pages = BAN::Math::div_round_up<uint64_t>(m_total_pages * sizeof(node), PAGE_SIZE);
|
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_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);
|
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
|
// Initialize page list so that every page points to the next one
|
||||||
|
@ -72,6 +75,9 @@ namespace Kernel
|
||||||
page->prev = m_used_list;
|
page->prev = m_used_list;
|
||||||
m_used_list = page;
|
m_used_list = page;
|
||||||
|
|
||||||
|
m_used_pages++;
|
||||||
|
m_free_pages--;
|
||||||
|
|
||||||
return page_address(page);
|
return page_address(page);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,6 +101,9 @@ namespace Kernel
|
||||||
if (m_free_list)
|
if (m_free_list)
|
||||||
m_free_list->next = page;
|
m_free_list->next = page;
|
||||||
m_free_list = page;
|
m_free_list = page;
|
||||||
|
|
||||||
|
m_used_pages--;
|
||||||
|
m_free_pages++;
|
||||||
}
|
}
|
||||||
|
|
||||||
paddr_t PhysicalRange::page_address(const node* page) const
|
paddr_t PhysicalRange::page_address(const node* page) const
|
||||||
|
|
|
@ -415,6 +415,9 @@ argument_done:
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
kmalloc_dump_info();
|
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")
|
else if (arguments.front() == "sleep")
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue