[libvirt] [PATCH] nodeinfo: fix detection of physical memory on uclibc/musl libc

Natanael Copa ncopa at alpinelinux.org
Tue Apr 15 09:31:23 UTC 2014


The gnulib's physmem_total will as a fallback report 64MB as total
memory if sysconf(_SC_PHYS_PAGES) is unimplemented on linux. This makes
it impossible to detect if physmem_total works or not, so we try first
the linux only sysinfo(2) before falling back to gnulibs physmem_total.

This makes the total memory be correctly reported on musl libc and
uclibc.

Signed-off-by: Natanael Copa <ncopa at alpinelinux.org>
---
 configure.ac   |  2 +-
 src/nodeinfo.c | 22 ++++++++++++++++------
 2 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/configure.ac b/configure.ac
index 52c50df..32a2e5a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -310,7 +310,7 @@ dnl Availability of various common headers (non-fatal if missing).
 AC_CHECK_HEADERS([pwd.h paths.h regex.h sys/un.h \
   sys/poll.h syslog.h mntent.h net/ethernet.h linux/magic.h \
   sys/un.h sys/syscall.h sys/sysctl.h netinet/tcp.h ifaddrs.h \
-  libtasn1.h sys/ucred.h sys/mount.h])
+  libtasn1.h sys/ucred.h sys/mount.h sys/sysinfo.h])
 dnl Check whether endian provides handy macros.
 AC_CHECK_DECLS([htole64], [], [], [[#include <endian.h>]])
 
diff --git a/src/nodeinfo.c b/src/nodeinfo.c
index c88f86c..3c76f54 100644
--- a/src/nodeinfo.c
+++ b/src/nodeinfo.c
@@ -40,6 +40,10 @@
 # include <sys/resource.h>
 #endif
 
+#if HAVE_SYS_SYSINFO_H
+# include <sys/sysinfo.h>
+#endif
+
 #include "c-ctype.h"
 #include "viralloc.h"
 #include "nodeinfopriv.h"
@@ -1041,7 +1045,18 @@ int nodeGetInfo(virNodeInfoPtr nodeinfo)
 #ifdef __linux__
     {
     int ret = -1;
-    FILE *cpuinfo = fopen(CPUINFO_PATH, "r");
+    FILE *cpuinfo;
+#if HAVE_SYS_SYSINFO_H
+    struct sysinfo si;
+
+    /* Convert to KB. */
+    if (sysinfo(&si) == 0) {
+        nodeinfo->memory = si.totalram / 1024;
+    } else
+#endif
+        nodeinfo->memory = physmem_total() / 1024;
+
+    cpuinfo = fopen(CPUINFO_PATH, "r");
     if (!cpuinfo) {
         virReportSystemError(errno,
                              _("cannot open %s"), CPUINFO_PATH);
@@ -1049,11 +1064,6 @@ int nodeGetInfo(virNodeInfoPtr nodeinfo)
     }
 
     ret = linuxNodeInfoCPUPopulate(cpuinfo, SYSFS_SYSTEM_PATH, nodeinfo);
-    if (ret < 0)
-        goto cleanup;
-
-    /* Convert to KB. */
-    nodeinfo->memory = physmem_total() / 1024;
 
  cleanup:
     VIR_FORCE_FCLOSE(cpuinfo);
-- 
1.9.2




More information about the libvir-list mailing list