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.
---
src/libvirt_private.syms | 2 ++
src/openvz/openvz_util.c | 5 +----
src/qemu/qemu_command.c | 4 ++--
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 | 13 +++++++++++++
tests/qemuxml2argvtest.c | 1 +
11 files changed, 42 insertions(+), 17 deletions(-)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index bd7870f..aa68797 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2215,6 +2215,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 100deed..30f202f 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -6694,7 +6694,7 @@ qemuBuildNumaArgStr(virQEMUDriverConfigPtr cfg,
char *nodemask = NULL;
char *mem_path = NULL;
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) ||
@@ -7986,7 +7986,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 1d3bee6..0a1c88e 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 5f56005..f2b9738 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..f3e8920 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 = sysconf(_SC_PAGESIZE);
+ 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..7adf781 100644
--- a/tests/qemuxml2argvmock.c
+++ b/tests/qemuxml2argvmock.c
@@ -22,7 +22,20 @@
#include "internal.h"
#include "virnuma.h"
+#include "virmock.h"
+#include "virutil.h"
#include <time.h>
+#include <unistd.h>
+
+long virGetSystemPageSize(void)
+{
+ return 4096;
+}
+
+long virGetSystemPageSizeKB(void)
+{
+ return 4;
+}
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