From d5b7792f4721b3215e948e6d6d91d287ce17b0f1 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Tue, 12 Aug 2025 21:20:37 -0400 Subject: kernel: vm: Add virtual memory statistics Introduce the '/ctl/vm/stat' control file for providing virtual memory related statistics to userland. Signed-off-by: Ian Moffett --- sys/vm/vm_physmem.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'sys/vm/vm_physmem.c') diff --git a/sys/vm/vm_physmem.c b/sys/vm/vm_physmem.c index 89f9ee6..debec1f 100644 --- a/sys/vm/vm_physmem.c +++ b/sys/vm/vm_physmem.c @@ -36,6 +36,10 @@ #include #include +#define BYTES_PER_MIB 8388608 + +static size_t pages_free = 0; +static size_t pages_used = 0; static size_t highest_frame_idx = 0; static size_t bitmap_size = 0; static size_t bitmap_free_start = 0; @@ -63,6 +67,7 @@ physmem_populate_bitmap(void) if (ent->type != LIMINE_MEMMAP_USABLE) { /* This memory is not usable */ + pages_used += ent->length / DEFAULT_PAGESIZE; continue; } @@ -73,6 +78,8 @@ physmem_populate_bitmap(void) for (size_t j = 0; j < ent->length; j += DEFAULT_PAGESIZE) { clrbit(bitmap, (ent->base + j) / DEFAULT_PAGESIZE); } + + pages_free += ent->length / DEFAULT_PAGESIZE; } } @@ -203,6 +210,26 @@ vm_free_frame(uintptr_t base, size_t count) spinlock_release(&lock); } +/* + * Return the amount of memory in MiB that is + * currently allocated. + */ +uint32_t +vm_mem_used(void) +{ + return (pages_used * DEFAULT_PAGESIZE) / BYTES_PER_MIB; +} + +/* + * Return the amount of memory in MiB that is + * currently free. + */ +uint32_t +vm_mem_free(void) +{ + return (pages_free * DEFAULT_PAGESIZE) / BYTES_PER_MIB; +} + void vm_physmem_init(void) { -- cgit v1.2.3