From 6c5ad005bed33e80c94460b6694d199348dac472 Mon Sep 17 00:00:00 2001 From: grehan Date: Sun, 26 Aug 2012 01:41:41 +0000 Subject: [PATCH] Add sysctls to display the total and free amount of hard-wired mem for VMs # sysctl hw.vmm hw.vmm.mem_free: 2145386496 hw.vmm.mem_total: 2145386496 Submitted by: Takeshi HASEGAWA hasegaw at gmail com --- lib/libvmmapi/vmmapi.c | 24 ++++++++++++++++++++++++ lib/libvmmapi/vmmapi.h | 2 ++ sys/amd64/vmm/vmm_dev.c | 19 +++++++++++++++++++ sys/amd64/vmm/vmm_mem.c | 24 ++++++++++++++++++++++++ sys/amd64/vmm/vmm_mem.h | 3 +++ 5 files changed, 72 insertions(+) diff --git a/lib/libvmmapi/vmmapi.c b/lib/libvmmapi/vmmapi.c index 5882bd2c781d..d7e6143c124b 100644 --- a/lib/libvmmapi/vmmapi.c +++ b/lib/libvmmapi/vmmapi.c @@ -117,6 +117,30 @@ vm_destroy(struct vmctx *vm) free(vm); } +size_t +vmm_get_mem_total(void) +{ + size_t mem_total = 0; + size_t oldlen = sizeof(mem_total); + int error; + error = sysctlbyname("hw.vmm.mem_total", &mem_total, &oldlen, NULL, 0); + if (error) + return -1; + return mem_total; +} + +size_t +vmm_get_mem_free(void) +{ + size_t mem_free = 0; + size_t oldlen = sizeof(mem_free); + int error; + error = sysctlbyname("hw.vmm.mem_free", &mem_free, &oldlen, NULL, 0); + if (error) + return -1; + return mem_free; +} + int vm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa, vm_paddr_t *ret_hpa, size_t *ret_len) diff --git a/lib/libvmmapi/vmmapi.h b/lib/libvmmapi/vmmapi.h index de3d4b749209..516bbc34eafb 100644 --- a/lib/libvmmapi/vmmapi.h +++ b/lib/libvmmapi/vmmapi.h @@ -34,6 +34,8 @@ struct vmctx; int vm_create(const char *name); struct vmctx *vm_open(const char *name); void vm_destroy(struct vmctx *ctx); +size_t vmm_get_mem_total(void); +size_t vmm_get_mem_free(void); int vm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa, vm_paddr_t *ret_hpa, size_t *ret_len); /* diff --git a/sys/amd64/vmm/vmm_dev.c b/sys/amd64/vmm/vmm_dev.c index 571c37c54a99..116b5f12b7ea 100644 --- a/sys/amd64/vmm/vmm_dev.c +++ b/sys/amd64/vmm/vmm_dev.c @@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$"); #include #include "vmm_lapic.h" #include "vmm_stat.h" +#include "vmm_mem.h" #include "io/ppt.h" #include @@ -458,6 +459,24 @@ sysctl_vmm_create(SYSCTL_HANDLER_ARGS) SYSCTL_PROC(_hw_vmm, OID_AUTO, create, CTLTYPE_STRING | CTLFLAG_RW, NULL, 0, sysctl_vmm_create, "A", NULL); +static int +sysctl_vmm_mem_total(SYSCTL_HANDLER_ARGS) +{ + size_t val = vmm_mem_get_mem_total(); + return sysctl_handle_long(oidp, &val, 0, req); +} +SYSCTL_PROC(_hw_vmm, OID_AUTO, mem_total, CTLTYPE_LONG | CTLFLAG_RD, + 0, 0, sysctl_vmm_mem_total, "LU", "Amount of Total memory"); + +static int +sysctl_vmm_mem_free(SYSCTL_HANDLER_ARGS) +{ + size_t val = vmm_mem_get_mem_free(); + return sysctl_handle_long(oidp, &val, 0, req); +} +SYSCTL_PROC(_hw_vmm, OID_AUTO, mem_free, CTLTYPE_LONG | CTLFLAG_RD, + 0, 0, sysctl_vmm_mem_free, "LU", "Amount of Free memory"); + void vmmdev_init(void) { diff --git a/sys/amd64/vmm/vmm_mem.c b/sys/amd64/vmm/vmm_mem.c index 764a6e901e4d..54f98acfc64c 100644 --- a/sys/amd64/vmm/vmm_mem.c +++ b/sys/amd64/vmm/vmm_mem.c @@ -63,6 +63,7 @@ static struct { } vmm_mem_avail[VMM_MEM_MAXSEGS]; static int vmm_mem_nsegs; +size_t vmm_mem_total_bytes; static vm_paddr_t maxaddr; @@ -96,6 +97,7 @@ vmm_mem_steal_memory(void) smapsize = *((uint32_t *)smapbase - 1); smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize); + vmm_mem_total_bytes = 0; nsegs = 0; for (smap = smapbase; smap < smapend; smap++) { /* @@ -131,6 +133,7 @@ vmm_mem_steal_memory(void) vmm_mem_avail[nsegs].base = base; vmm_mem_avail[nsegs].length = length; + vmm_mem_total_bytes += length; if (base + length > maxaddr) maxaddr = base + length; @@ -344,6 +347,27 @@ vmm_mem_alloc(size_t size) return (addr); } +size_t +vmm_mem_get_mem_total(void) +{ + return vmm_mem_total_bytes; +} + +size_t +vmm_mem_get_mem_free(void) +{ + size_t length = 0; + int i; + + mtx_lock(&vmm_mem_mtx); + for (i = 0; i < vmm_mem_nsegs; i++) { + length += vmm_mem_avail[i].length; + } + mtx_unlock(&vmm_mem_mtx); + + return(length); +} + void vmm_mem_free(vm_paddr_t base, size_t length) { diff --git a/sys/amd64/vmm/vmm_mem.h b/sys/amd64/vmm/vmm_mem.h index ef1bf1aee32b..a83e9be682ea 100644 --- a/sys/amd64/vmm/vmm_mem.h +++ b/sys/amd64/vmm/vmm_mem.h @@ -35,4 +35,7 @@ void vmm_mem_free(vm_paddr_t start, size_t size); vm_paddr_t vmm_mem_maxaddr(void); void vmm_mem_dump(void); +size_t vmm_mem_get_mem_total(void); +size_t vmm_mem_get_mem_free(void); + #endif