[PATCH v2] Make tests independant of system page size

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Some code paths have special logic depending on the page size
reported by sysconf, which in turn affects the test results.
We must mock this so tests always have a consistent page size.
---
 cfg.mk                   |  8 ++++++++
 src/libvirt_private.syms |  2 ++
 src/openvz/openvz_util.c |  5 +----
 src/qemu/qemu_command.c  |  6 +++---
 src/qemu/qemu_driver.c   |  6 +-----
 src/util/virfile.c       |  2 +-
 src/util/virnuma.c       |  6 +++---
 src/util/virutil.c       | 13 +++++++++++++
 src/util/virutil.h       |  3 +++
 src/xen/xen_hypervisor.c |  4 ++--
 tests/qemuxml2argvmock.c |  8 ++++++++
 tests/qemuxml2argvtest.c |  1 +
 12 files changed, 46 insertions(+), 18 deletions(-)

diff --git a/cfg.mk b/cfg.mk
index 70612f8..d72b039 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -997,6 +997,11 @@ sc_prohibit_virXXXFree:
 	halt='avoid using 'virXXXFree', use 'virObjectUnref' instead' \
 	  $(_sc_search_regexp)
 
+sc_prohibit_sysconf_pagesize:
+	@prohibit='sysconf\(_SC_PAGESIZE' \
+	halt='use virGetSystemPageSize[KB] instead of sysconf(_SC_PAGESIZE)' \
+	  $(_sc_search_regexp)
+
 # We don't use this feature of maint.mk.
 prev_version_file = /dev/null
 
@@ -1187,3 +1192,6 @@ exclude_file_name_regexp--sc_prohibit_devname = \
 
 exclude_file_name_regexp--sc_prohibit_virXXXFree = \
   ^(docs/|tests/|examples/|tools/|cfg.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 = \
+  ^(cfg\.mk|src/util/virutil\.c)$$
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index f7f8ea2..376c69b 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2228,6 +2228,8 @@ virGetListenFDs;
 virGetSCSIHostNameByParentaddr;
 virGetSCSIHostNumber;
 virGetSelfLastChanged;
+virGetSystemPageSize;
+virGetSystemPageSizeKB;
 virGetUnprivSGIOSysfsPath;
 virGetUserCacheDirectory;
 virGetUserConfigDirectory;
diff --git a/src/openvz/openvz_util.c b/src/openvz/openvz_util.c
index 8032f6a..3cdc1c2 100644
--- a/src/openvz/openvz_util.c
+++ b/src/openvz/openvz_util.c
@@ -42,10 +42,7 @@ openvzKBPerPages(void)
     static long kb_per_pages;
 
     if (kb_per_pages == 0) {
-        kb_per_pages = sysconf(_SC_PAGESIZE);
-        if (kb_per_pages > 0) {
-            kb_per_pages /= 1024;
-        } else {
+        if ((kb_per_pages = virGetSystemPageSizeKB()) < 0) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("Can't determine page size"));
             kb_per_pages = 0;
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index ec4f35b..3b6eddc 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -4540,7 +4540,7 @@ qemuBuildMemoryBackendStr(unsigned long long size,
     virDomainHugePagePtr master_hugepage = NULL;
     virDomainHugePagePtr hugepage = NULL;
     virDomainNumatuneMemMode mode;
-    const long system_page_size = sysconf(_SC_PAGESIZE) / 1024;
+    const long system_page_size = virGetSystemPageSizeKB();
     virMemAccess memAccess = def->cpu->cells[guestNode].memAccess;
     size_t i;
     char *mem_path = NULL;
@@ -7051,7 +7051,7 @@ qemuBuildNumaArgStr(virQEMUDriverConfigPtr cfg,
     bool needBackend = false;
     int rc;
     int ret = -1;
-    const long system_page_size = sysconf(_SC_PAGESIZE) / 1024;
+    const long system_page_size = virGetSystemPageSizeKB();
 
     if (virDomainNumatuneHasPerNodeBinding(def->numatune) &&
         !(virQEMUCapsGet(qemuCaps, QEMU_CAPS_OBJECT_MEMORY_RAM) ||
@@ -8239,7 +8239,7 @@ qemuBuildCommandLine(virConnectPtr conn,
     def->mem.max_balloon = VIR_DIV_UP(def->mem.max_balloon, 1024) * 1024;
     virCommandAddArgFormat(cmd, "%llu", def->mem.max_balloon / 1024);
     if (def->mem.nhugepages && (!def->cpu || !def->cpu->ncells)) {
-        const long system_page_size = sysconf(_SC_PAGESIZE) / 1024;
+        const long system_page_size = virGetSystemPageSizeKB();
         char *mem_path = NULL;
 
         if (def->mem.hugepages[0].size == system_page_size) {
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 59a9593..cf351e6 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -1352,12 +1352,8 @@ qemuGetProcessInfo(unsigned long long *cpuTime, int *lastCpu, long *vm_rss,
     if (lastCpu)
         *lastCpu = cpu;
 
-    /* We got pages
-     * We want kiloBytes
-     * _SC_PAGESIZE is page size in Bytes
-     * So calculate, but first lower the pagesize so we don't get overflow */
     if (vm_rss)
-        *vm_rss = rss * (sysconf(_SC_PAGESIZE) >> 10);
+        *vm_rss = rss * virGetSystemPageSizeKB();
 
 
     VIR_DEBUG("Got status for %d/%d user=%llu sys=%llu cpu=%d rss=%ld",
diff --git a/src/util/virfile.c b/src/util/virfile.c
index 4024f3d..68ef6eb 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -1086,7 +1086,7 @@ safezero_mmap(int fd, off_t offset, off_t len)
 
     /* align offset and length, rounding offset down and length up */
     if (pagemask == 0)
-        pagemask = ~(sysconf(_SC_PAGESIZE) - 1);
+        pagemask = ~(virGetSystemPageSize() - 1);
     map_skip = offset - (offset & pagemask);
 
     /* memset wants the mmap'ed file to be present on disk so create a
diff --git a/src/util/virnuma.c b/src/util/virnuma.c
index 86564d4..e986c71 100644
--- a/src/util/virnuma.c
+++ b/src/util/virnuma.c
@@ -639,7 +639,7 @@ virNumaGetPageInfo(int node,
                    unsigned int *page_free)
 {
     int ret = -1;
-    long system_page_size = sysconf(_SC_PAGESIZE);
+    long system_page_size = virGetSystemPageSize();
 
     /* sysconf() returns page size in bytes,
      * the @page_size is however in kibibytes */
@@ -717,7 +717,7 @@ virNumaGetPages(int node,
 
     /* sysconf() returns page size in bytes,
      * but we are storing the page size in kibibytes. */
-    system_page_size = sysconf(_SC_PAGESIZE) / 1024;
+    system_page_size = virGetSystemPageSizeKB();
 
     /* Query huge pages at first.
      * On Linux systems, the huge pages pool cuts off the available memory and
@@ -841,7 +841,7 @@ virNumaSetPagePoolSize(int node,
     char *end;
     unsigned long long nr_count;
 
-    if (page_size == sysconf(_SC_PAGESIZE) / 1024) {
+    if (page_size == virGetSystemPageSizeKB()) {
         /* Special case as kernel handles system pages
          * differently to huge pages. */
         virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
diff --git a/src/util/virutil.c b/src/util/virutil.c
index 3037293..c905209 100644
--- a/src/util/virutil.c
+++ b/src/util/virutil.c
@@ -2577,3 +2577,16 @@ virGetListenFDs(void)
 }
 
 #endif /* WIN32 */
+
+long virGetSystemPageSize(void)
+{
+    return sysconf(_SC_PAGESIZE);
+}
+
+long virGetSystemPageSizeKB(void)
+{
+    long val = virGetSystemPageSize();
+    if (val < 0)
+        return val;
+    return val / 1024;
+}
diff --git a/src/util/virutil.h b/src/util/virutil.h
index f31bf88..d1173c1 100644
--- a/src/util/virutil.h
+++ b/src/util/virutil.h
@@ -244,4 +244,7 @@ VIR_ENUM_DECL(virTristateSwitch)
 
 unsigned int virGetListenFDs(void);
 
+long virGetSystemPageSize(void);
+long virGetSystemPageSizeKB(void);
+
 #endif /* __VIR_UTIL_H__ */
diff --git a/src/xen/xen_hypervisor.c b/src/xen/xen_hypervisor.c
index 2473532..31a2a1b 100644
--- a/src/xen/xen_hypervisor.c
+++ b/src/xen/xen_hypervisor.c
@@ -2736,7 +2736,7 @@ xenHypervisorGetMaxMemory(virConnectPtr conn,
     int ret;
 
     if (kb_per_pages == 0) {
-        kb_per_pages = sysconf(_SC_PAGESIZE) / 1024;
+        kb_per_pages = virGetSystemPageSizeKB();
         if (kb_per_pages <= 0)
             kb_per_pages = 4;
     }
@@ -2771,7 +2771,7 @@ xenHypervisorGetDomInfo(virConnectPtr conn, int id, virDomainInfoPtr info)
     uint32_t domain_flags, domain_state, domain_shutdown_cause;
 
     if (kb_per_pages == 0) {
-        kb_per_pages = sysconf(_SC_PAGESIZE) / 1024;
+        kb_per_pages = virGetSystemPageSizeKB();
         if (kb_per_pages <= 0)
             kb_per_pages = 4;
     }
diff --git a/tests/qemuxml2argvmock.c b/tests/qemuxml2argvmock.c
index eccf4b0..d24725e 100644
--- a/tests/qemuxml2argvmock.c
+++ b/tests/qemuxml2argvmock.c
@@ -22,7 +22,15 @@
 
 #include "internal.h"
 #include "virnuma.h"
+#include "virmock.h"
+#include "virutil.h"
 #include <time.h>
+#include <unistd.h>
+
+long virGetSystemPageSize(void)
+{
+    return 4096;
+}
 
 time_t time(time_t *t)
 {
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index 89afa81..77ee630 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -22,6 +22,7 @@
 # include "cpu/cpu_map.h"
 # include "virstring.h"
 # include "storage/storage_driver.h"
+# include "virmock.h"
 
 # include "testutilsqemu.h"
 
-- 
2.1.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]