[Zion] Track used vs free physical memory.
This commit is contained in:
parent
8e95a11907
commit
0b47e3b7fc
|
@ -7,6 +7,7 @@
|
||||||
#include "interrupt/apic.h"
|
#include "interrupt/apic.h"
|
||||||
#include "interrupt/apic_timer.h"
|
#include "interrupt/apic_timer.h"
|
||||||
#include "memory/kernel_heap.h"
|
#include "memory/kernel_heap.h"
|
||||||
|
#include "memory/physical_memory.h"
|
||||||
#include "scheduler/scheduler.h"
|
#include "scheduler/scheduler.h"
|
||||||
|
|
||||||
#define IDT_INTERRUPT_GATE 0x8E
|
#define IDT_INTERRUPT_GATE 0x8E
|
||||||
|
@ -142,6 +143,7 @@ extern "C" void interrupt_apic_timer(InterruptFrame*) {
|
||||||
if (cnt % 20 == 0) {
|
if (cnt % 20 == 0) {
|
||||||
if (cnt == 20) {
|
if (cnt == 20) {
|
||||||
KernelHeap::DumpDebugData();
|
KernelHeap::DumpDebugData();
|
||||||
|
phys_mem::DumpPhysicalMemoryUsage();
|
||||||
}
|
}
|
||||||
dbgln("timer: {}s", cnt * 50 / 1000);
|
dbgln("timer: {}s", cnt * 50 / 1000);
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,6 +73,7 @@ class PhysicalMemoryManager {
|
||||||
#if K_PHYS_DEBUG
|
#if K_PHYS_DEBUG
|
||||||
dbgln("Single {x}", page);
|
dbgln("Single {x}", page);
|
||||||
#endif
|
#endif
|
||||||
|
allocated_pages_ += 1;
|
||||||
return page;
|
return page;
|
||||||
}
|
}
|
||||||
uint64_t AllocateContinuous(uint64_t num_pages) {
|
uint64_t AllocateContinuous(uint64_t num_pages) {
|
||||||
|
@ -101,11 +102,35 @@ class PhysicalMemoryManager {
|
||||||
#if K_PHYS_DEBUG
|
#if K_PHYS_DEBUG
|
||||||
dbgln("Continuous {x}:{}", page, num_pages);
|
dbgln("Continuous {x}:{}", page, num_pages);
|
||||||
#endif
|
#endif
|
||||||
|
allocated_pages_ += num_pages;
|
||||||
return page;
|
return page;
|
||||||
}
|
}
|
||||||
void FreePage(uint64_t page) { AddMemoryRegion(page, 0x1000); }
|
void FreePage(uint64_t page) {
|
||||||
|
AddMemoryRegion(page, 0x1000);
|
||||||
|
allocated_pages_--;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t AllocatedPages() { return allocated_pages_; }
|
||||||
|
|
||||||
|
uint64_t AvailablePages() {
|
||||||
|
uint64_t available = 0;
|
||||||
|
for (auto iter = memory_blocks.begin(); iter != memory_blocks.end();
|
||||||
|
iter = iter.next()) {
|
||||||
|
available += iter->num_pages;
|
||||||
|
}
|
||||||
|
return available;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
struct MemBlock {
|
||||||
|
uint64_t base = 0;
|
||||||
|
uint64_t num_pages = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
glcr::LinkedList<MemBlock> memory_blocks;
|
||||||
|
|
||||||
|
uint64_t allocated_pages_ = 0;
|
||||||
|
|
||||||
void AddMemoryRegion(uint64_t base, uint64_t size) {
|
void AddMemoryRegion(uint64_t base, uint64_t size) {
|
||||||
MemBlock block{
|
MemBlock block{
|
||||||
.base = base,
|
.base = base,
|
||||||
|
@ -113,12 +138,6 @@ class PhysicalMemoryManager {
|
||||||
};
|
};
|
||||||
memory_blocks.PushFront(block);
|
memory_blocks.PushFront(block);
|
||||||
}
|
}
|
||||||
struct MemBlock {
|
|
||||||
uint64_t base = 0;
|
|
||||||
uint64_t num_pages = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
glcr::LinkedList<MemBlock> memory_blocks;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static PhysicalMemoryManager* gPmm = nullptr;
|
static PhysicalMemoryManager* gPmm = nullptr;
|
||||||
|
@ -214,4 +233,9 @@ uint64_t AllocateContinuous(uint64_t num_continuous) {
|
||||||
return gPmm->AllocateContinuous(num_continuous);
|
return gPmm->AllocateContinuous(num_continuous);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DumpPhysicalMemoryUsage() {
|
||||||
|
dbgln("Pages used: {} MiB, avail: {} MiB", gPmm->AllocatedPages() / 256,
|
||||||
|
gPmm->AvailablePages() / 256);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace phys_mem
|
} // namespace phys_mem
|
||||||
|
|
|
@ -21,4 +21,6 @@ uint64_t AllocateAndZeroPage();
|
||||||
uint64_t AllocateContinuous(uint64_t num_pages);
|
uint64_t AllocateContinuous(uint64_t num_pages);
|
||||||
void FreePage(uint64_t page);
|
void FreePage(uint64_t page);
|
||||||
|
|
||||||
|
void DumpPhysicalMemoryUsage();
|
||||||
|
|
||||||
} // namespace phys_mem
|
} // namespace phys_mem
|
||||||
|
|
Loading…
Reference in New Issue