For future work we want to info for not only the free memory but overall memory size too. That's why the function must have new signature too. Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx> --- src/bhyve/bhyve_driver.c | 7 +++- src/libvirt_private.syms | 2 +- src/lxc/lxc_driver.c | 7 +++- src/nodeinfo.c | 96 +++++++++++++++++++++++++++++++++------------- src/nodeinfo.h | 3 +- src/openvz/openvz_driver.c | 5 ++- src/qemu/qemu_driver.c | 7 +++- src/uml/uml_driver.c | 7 +++- src/vbox/vbox_tmpl.c | 5 ++- 9 files changed, 105 insertions(+), 34 deletions(-) diff --git a/src/bhyve/bhyve_driver.c b/src/bhyve/bhyve_driver.c index 89f73ff..bb9bcb7 100644 --- a/src/bhyve/bhyve_driver.c +++ b/src/bhyve/bhyve_driver.c @@ -1244,10 +1244,15 @@ bhyveConnectGetMaxVcpus(virConnectPtr conn ATTRIBUTE_UNUSED, static unsigned long long bhyveNodeGetFreeMemory(virConnectPtr conn) { + unsigned long long freeMem; + if (virNodeGetFreeMemoryEnsureACL(conn) < 0) return 0; - return nodeGetFreeMemory(); + if (nodeGetMemory(NULL, &freeMem) < 0) + return 0; + + return freeMem; } static int diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 946b264..18fde54 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -875,8 +875,8 @@ nodeGetCPUBitmap; nodeGetCPUCount; nodeGetCPUMap; nodeGetCPUStats; -nodeGetFreeMemory; nodeGetInfo; +nodeGetMemory; nodeGetMemoryParameters; nodeGetMemoryStats; nodeSetMemoryParameters; diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index d2852a7..ab227d0 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -5477,10 +5477,15 @@ lxcNodeGetCellsFreeMemory(virConnectPtr conn, static unsigned long long lxcNodeGetFreeMemory(virConnectPtr conn) { + unsigned long long freeMem; + if (virNodeGetFreeMemoryEnsureACL(conn) < 0) return 0; - return nodeGetFreeMemory(); + if (nodeGetMemory(NULL, &freeMem) < 0) + return 0; + + return freeMem; } diff --git a/src/nodeinfo.c b/src/nodeinfo.c index 5332ede..fd831b4 100644 --- a/src/nodeinfo.c +++ b/src/nodeinfo.c @@ -1683,37 +1683,66 @@ nodeGetCellsFreeMemoryFake(unsigned long long *freeMems, return 1; } -static unsigned long long -nodeGetFreeMemoryFake(void) +static int +nodeGetMemoryFake(unsigned long long *mem, + unsigned long long *freeMem) { + int ret = -1; + #if defined(__FreeBSD__) unsigned long pagesize = getpagesize(); u_int value; size_t value_size = sizeof(value); - unsigned long long freemem; - if (sysctlbyname("vm.stats.vm.v_free_count", &value, - &value_size, NULL, 0) < 0) { - virReportSystemError(errno, "%s", - _("sysctl failed for vm.stats.vm.v_free_count")); - return 0; + if (mem) { + if (sysctlbyname("vm.stats.vm.v_page_count", &value, + &value_size, NULL, 0) < 0) { + virReportSystemError(errno, "%s", + _("sysctl failed for vm.stats.vm.v_page_count")); + goto cleanup; + } + *mem = value * (unsigned long long)pagesize; } - freemem = value * (unsigned long long)pagesize; + if (freeMem) { + if (sysctlbyname("vm.stats.vm.v_free_count", &value, + &value_size, NULL, 0) < 0) { + virReportSystemError(errno, "%s", + _("sysctl failed for vm.stats.vm.v_free_count")); + goto cleanup; + } + + *freeMem = value * (unsigned long long)pagesize; + } - return freemem; #else - double avail = physmem_available(); - unsigned long long ret; + if (mem) { + double total = physmem_total(); + if (!total) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Cannot determine free memory")); + goto cleanup; + } - if (!(ret = (unsigned long long)avail)) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Cannot determine free memory")); - return 0; + *mem = (unsigned long long) total; } - return ret; + if (freeMem) { + double avail = physmem_available(); + + if (!avail) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Cannot determine free memory")); + goto cleanup; + } + + *freeMem = (unsigned long long) avail; + } #endif + + ret = 0; + cleanup: + return ret; } /* returns 1 on success, 0 if the detection failed and -1 on hard error */ @@ -1914,25 +1943,40 @@ nodeGetCellsFreeMemory(unsigned long long *freeMems, return ret; } -unsigned long long -nodeGetFreeMemory(void) +int +nodeGetMemory(unsigned long long *mem, + unsigned long long *freeMem) { - unsigned long long mem; - unsigned long long freeMem = 0; int max_node; int n; + if (mem) + *mem = 0; + + if (freeMem) + *freeMem = 0; + if (!virNumaIsAvailable()) - return nodeGetFreeMemoryFake(); + return nodeGetMemoryFake(mem, freeMem); if ((max_node = virNumaGetMaxNode()) < 0) - return 0; + return -1; for (n = 0; n <= max_node; n++) { - virNumaGetNodeMemory(n, NULL, &mem); + unsigned long long tmp_mem = 0, tmp_freeMem = 0; - freeMem += mem; + if (!virNumaNodeIsAvailable(n)) + continue; + + if (virNumaGetNodeMemory(n, &tmp_mem, &tmp_freeMem) < 0) + return -1; + + if (mem) + *mem += tmp_mem; + + if (freeMem) + *freeMem += tmp_freeMem; } - return freeMem; + return 0; } diff --git a/src/nodeinfo.h b/src/nodeinfo.h index c81fcbb..e7ec144 100644 --- a/src/nodeinfo.h +++ b/src/nodeinfo.h @@ -40,7 +40,8 @@ int nodeGetMemoryStats(int cellNum, int nodeGetCellsFreeMemory(unsigned long long *freeMems, int startCell, int maxCells); -unsigned long long nodeGetFreeMemory(void); +int nodeGetMemory(unsigned long long *mem, + unsigned long long *freeMem); virBitmapPtr nodeGetCPUBitmap(int *max_id); int nodeGetCPUCount(void); diff --git a/src/openvz/openvz_driver.c b/src/openvz/openvz_driver.c index 87df2a7..4c815ed 100644 --- a/src/openvz/openvz_driver.c +++ b/src/openvz/openvz_driver.c @@ -2180,7 +2180,10 @@ openvzNodeGetCellsFreeMemory(virConnectPtr conn ATTRIBUTE_UNUSED, static unsigned long long openvzNodeGetFreeMemory(virConnectPtr conn ATTRIBUTE_UNUSED) { - return nodeGetFreeMemory(); + unsigned long long freeMem; + if (nodeGetMemory(NULL, &freeMem) < 0) + return 0; + return freeMem; } diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 7bf2020..88051c9 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -16602,10 +16602,15 @@ qemuNodeGetCellsFreeMemory(virConnectPtr conn, static unsigned long long qemuNodeGetFreeMemory(virConnectPtr conn) { + unsigned long long freeMem; + if (virNodeGetFreeMemoryEnsureACL(conn) < 0) return 0; - return nodeGetFreeMemory(); + if (nodeGetMemory(NULL, &freeMem) < 0) + return 0; + + return freeMem; } diff --git a/src/uml/uml_driver.c b/src/uml/uml_driver.c index 1e0ec0e..a5e9ea8 100644 --- a/src/uml/uml_driver.c +++ b/src/uml/uml_driver.c @@ -2806,10 +2806,15 @@ umlNodeGetCellsFreeMemory(virConnectPtr conn, static unsigned long long umlNodeGetFreeMemory(virConnectPtr conn) { + unsigned long long freeMem; + if (virNodeGetFreeMemoryEnsureACL(conn) < 0) return 0; - return nodeGetFreeMemory(); + if (nodeGetMemory(NULL, &freeMem) < 0) + return 0; + + return freeMem; } diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c index 1ed2729..66e933a 100644 --- a/src/vbox/vbox_tmpl.c +++ b/src/vbox/vbox_tmpl.c @@ -11468,7 +11468,10 @@ vboxNodeGetCellsFreeMemory(virConnectPtr conn ATTRIBUTE_UNUSED, static unsigned long long vboxNodeGetFreeMemory(virConnectPtr conn ATTRIBUTE_UNUSED) { - return nodeGetFreeMemory(); + unsigned long long freeMem; + if (nodeGetMemory(NULL, &freeMem) < 0) + return 0; + return freeMem; } -- 1.8.5.5 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list