Add a debug function to dump heap usage

This commit is contained in:
apio 2022-11-20 16:33:54 +01:00
parent d54c882c63
commit 29defdf54d
2 changed files with 35 additions and 1 deletions

View File

@ -329,3 +329,35 @@ Result<void*> kcalloc(usize nmemb, usize size)
void* ptr = TRY(kmalloc(realsize));
return memset(ptr, 0, realsize);
}
void dump_heap_usage()
{
Serial::println("-- Dumping usage stats for kernel heap:");
if (!heap_start)
{
Serial::println("- Heap is not currently being used");
return;
}
usize alloc_total = 0;
usize alloc_used = 0;
HeapBlock* block = heap_start;
while (block)
{
if (is_block_free(block))
{
Serial::printf("- Available block, of size %zu\n", block->full_size);
alloc_total += block->full_size + sizeof(HeapBlock);
}
else
{
Serial::printf("- Used block, of size %zu, of which %zu bytes are being used\n", block->full_size,
block->req_size);
alloc_total += block->full_size + sizeof(HeapBlock);
alloc_used += block->req_size;
}
block = block->next;
}
Serial::printf("-- Total memory allocated for heap: %zu bytes\n", alloc_total);
Serial::printf("-- Heap memory in use by the kernel: %zu bytes\n", alloc_used);
}

View File

@ -18,3 +18,5 @@ template <typename T> Result<void> destroy(T* item)
item->~T();
return kfree(item);
}
void dump_heap_usage();