We don't need all the platforms gnulib deals with, so
this is a cut down version of GNULIB's physmem.c
code. This also allows us to integrate libvirt's
error reporting functions closer to the error cause.
Signed-off-by: Daniel P. Berrangé
---
build-aux/syntax-check.mk | 2 +-
src/conf/capabilities.c | 1 -
src/util/virhostcpu.c | 1 -
src/util/virhostmem.c | 182 +++---
4 files changed, 153 insertions(+), 33 deletions(-)
diff --git a/build-aux/syntax-check.mk b/build-aux/syntax-check.mk
index 9138ba1fe2..2bbadd426c 100644
--- a/build-aux/syntax-check.mk
+++ b/build-aux/syntax-check.mk
@@ -2335,7 +2335,7 @@ exclude_file_name_regexp--sc_prohibit_virXXXFree = \
^(docs/|tests/|examples/|tools/|build-aux/syntax-check\.mk|src/test/test_driver.c|src/libvirt_public.syms|include/libvirt/libvirt-(domain|network|nodedev|storage|stream|secret|nwfilter|interface|domain-snapshot).h|src/libvirt-(domain|qemu|network|nodedev|storage|stream|secret|nwfilter|interface|domain-snapshot).c$$)
exclude_file_name_regexp--sc_prohibit_sysconf_pagesize = \
- ^(build-aux/syntax-check\.mk|src/util/virutil\.c)$$
+ ^(build-aux/syntax-check\.mk|src/util/vir(hostmem|util)\.c)$$
exclude_file_name_regexp--sc_prohibit_pthread_create = \
^(build-aux/syntax-check\.mk|src/util/virthread\.c|tests/.*)$$
diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c
index 9a39858280..ade04b7cd6 100644
--- a/src/conf/capabilities.c
+++ b/src/conf/capabilities.c
@@ -26,7 +26,6 @@
#include "capabilities.h"
#include "cpu_conf.h"
#include "domain_conf.h"
-#include "physmem.h"
#include "storage_conf.h"
#include "viralloc.h"
#include "virarch.h"
diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c
index c948278c9a..277b8efa62 100644
--- a/src/util/virhostcpu.c
+++ b/src/util/virhostcpu.c
@@ -41,7 +41,6 @@
#include "viralloc.h"
#define LIBVIRT_VIRHOSTCPUPRIV_H_ALLOW
#include "virhostcpupriv.h"
-#include "physmem.h"
#include "virerror.h"
#include "virintprops.h"
#include "virarch.h"
diff --git a/src/util/virhostmem.c b/src/util/virhostmem.c
index 34e6f7adcf..9c08b9bd78 100644
--- a/src/util/virhostmem.c
+++ b/src/util/virhostmem.c
@@ -33,9 +33,13 @@
# include
#endif
+#ifdef WIN32
+# define WIN32_LEAN_AND_MEAN
+# include
+#endif
+
#include "viralloc.h"
#include "virhostmem.h"
-#include "physmem.h"
#include "virerror.h"
#include "virarch.h"
#include "virfile.h"
@@ -577,13 +581,151 @@ virHostMemGetParameters(virTypedParameterPtr params
G_GNUC_UNUSED,
}
+#ifdef WIN32
+/* MEMORYSTATUSEX is missing from older windows headers, so define
+a local replacement. */
+typedef struct
+{
+ DWORD dwLength;
+ DWORD dwMemoryLoad;
+ DWORDLONG ullTotalPhys;
+ DWORDLONG ullAvailPhys;
+ DWORDLONG ullTotalPageFile;
+ DWORDLONG ullAvailPageFile;
+ DWORDLONG ullTotalVirtual;
+ DWORDLONG ullAvailVirtual;
+ DWORDLONG ullAvailExtendedVirtual;
+} lMEMORYSTATUSEX;
+typedef WINBOOL(WINAPI *PFN_MS_EX) (lMEMORYSTATUSEX*);
+#endif /* !WIN32 */
+
+static unsigned long long
+virHostMemGetTotal(void)
+{
+#if defined HAVE_SYSCTLBYNAME
+/* This works on freebsd & macOS. */
+unsigned long long physmem = 0;
+size_t len = sizeof(physmem);
+
+if (sysctlbyname("hw.physmem", , , NULL, 0) < 0) {
+virReportSystemError(errno, "%s",
+ _("Unable to query memory total"));
+return 0;
+}
+
+return physmem;
+#elif defined _SC_PHYS_PAGES && defined _SC_PAGESIZE
+/* this works on linux */
+long long pages;
+long long pagesize;
+if ((pages = sysconf(_SC_PHYS_PAGES)) < 0) {
+virReportSystemError(errno, "%s",
+ _("Unable to query memory total"));
+return 0;
+}
+if ((pagesize = sysconf(_SC_PAGESIZE)) < 0) {
+virReportSystemError(errno, "%s",
+ _("Unable to query memory page size"));
+return 0;
+}
+return (unsigned long long)pages * (unsigned long long)pagesize;
+#elif defined WIN32
+PFN_MS_EX pfnex;
+HMODULE h = GetModuleHandle("kernel32.dll");
+
+if (!h) {
+virReportSystemError(errno, "%s",
+ _("Unable to access kernel32.dll"));
+return 0;
+}
+
+/* Use GlobalMemoryStatusEx if available. */
+if ((pfnex = (PFN_MS_EX) GetProcAddress(h, "GlobalMemoryStatusEx"))) {
+lMEMORYSTATUSEX lms_ex;
+lms_ex.dwLength = sizeof(lms_ex);
+if (!pfnex(_ex)) {
+virReportSystemError(EIO, "%s",
+ _("Unable to query memory total"));
+return 0;
+}
+return lms_ex.ullTotalPhys;
+} else {
+/* Fall back to GlobalMemoryStatus which is always available.
+but returns wrong results for physical memory > 4GB. */
+MEMORYSTATUS ms;
+GlobalMemoryStatus();
+return ms.dwTotalPhys;
+}
+#endif
+}
+
+
+static unsigned long long