Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 26 Aug 2012 01:41:42 +0000 (UTC)
From:      Peter Grehan <grehan@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-projects@freebsd.org
Subject:   svn commit: r239700 - in projects/bhyve: lib/libvmmapi sys/amd64/vmm
Message-ID:  <201208260141.q7Q1fgNX034139@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: grehan
Date: Sun Aug 26 01:41:41 2012
New Revision: 239700
URL: http://svn.freebsd.org/changeset/base/239700

Log:
  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

Modified:
  projects/bhyve/lib/libvmmapi/vmmapi.c
  projects/bhyve/lib/libvmmapi/vmmapi.h
  projects/bhyve/sys/amd64/vmm/vmm_dev.c
  projects/bhyve/sys/amd64/vmm/vmm_mem.c
  projects/bhyve/sys/amd64/vmm/vmm_mem.h

Modified: projects/bhyve/lib/libvmmapi/vmmapi.c
==============================================================================
--- projects/bhyve/lib/libvmmapi/vmmapi.c	Sun Aug 26 01:21:02 2012	(r239699)
+++ projects/bhyve/lib/libvmmapi/vmmapi.c	Sun Aug 26 01:41:41 2012	(r239700)
@@ -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)

Modified: projects/bhyve/lib/libvmmapi/vmmapi.h
==============================================================================
--- projects/bhyve/lib/libvmmapi/vmmapi.h	Sun Aug 26 01:21:02 2012	(r239699)
+++ projects/bhyve/lib/libvmmapi/vmmapi.h	Sun Aug 26 01:41:41 2012	(r239700)
@@ -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);
 /*

Modified: projects/bhyve/sys/amd64/vmm/vmm_dev.c
==============================================================================
--- projects/bhyve/sys/amd64/vmm/vmm_dev.c	Sun Aug 26 01:21:02 2012	(r239699)
+++ projects/bhyve/sys/amd64/vmm/vmm_dev.c	Sun Aug 26 01:41:41 2012	(r239700)
@@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$");
 #include <machine/vmm.h>
 #include "vmm_lapic.h"
 #include "vmm_stat.h"
+#include "vmm_mem.h"
 #include "io/ppt.h"
 #include <machine/vmm_dev.h>
 
@@ -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)
 {

Modified: projects/bhyve/sys/amd64/vmm/vmm_mem.c
==============================================================================
--- projects/bhyve/sys/amd64/vmm/vmm_mem.c	Sun Aug 26 01:21:02 2012	(r239699)
+++ projects/bhyve/sys/amd64/vmm/vmm_mem.c	Sun Aug 26 01:41:41 2012	(r239700)
@@ -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)
 {

Modified: projects/bhyve/sys/amd64/vmm/vmm_mem.h
==============================================================================
--- projects/bhyve/sys/amd64/vmm/vmm_mem.h	Sun Aug 26 01:21:02 2012	(r239699)
+++ projects/bhyve/sys/amd64/vmm/vmm_mem.h	Sun Aug 26 01:41:41 2012	(r239700)
@@ -35,4 +35,7 @@ void		vmm_mem_free(vm_paddr_t start, siz
 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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201208260141.q7Q1fgNX034139>