So far, we are doing compile time decisions on which architecture is
used. However, for testing purposes it's much easier if we pass host
architecture as parameter and then let the function decide which code
snippet for extracting host CPU info will be used.
Signed-off-by: Michal Privoznik <mprivozn(a)redhat.com>
---
src/nodeinfo.c | 141 +++++++++++----------
src/nodeinfopriv.h | 1 +
...86-test1.cpuinfo => linux-x86_64-test1.cpuinfo} | 0
...-test1.expected => linux-x86_64-test1.expected} | 0
...86-test2.cpuinfo => linux-x86_64-test2.cpuinfo} | 0
...-test2.expected => linux-x86_64-test2.expected} | 0
...86-test3.cpuinfo => linux-x86_64-test3.cpuinfo} | 0
...-test3.expected => linux-x86_64-test3.expected} | 0
...86-test4.cpuinfo => linux-x86_64-test4.cpuinfo} | 0
...-test4.expected => linux-x86_64-test4.expected} | 0
...86-test5.cpuinfo => linux-x86_64-test5.cpuinfo} | 0
...-test5.expected => linux-x86_64-test5.expected} | 0
...86-test6.cpuinfo => linux-x86_64-test6.cpuinfo} | 0
...-test6.expected => linux-x86_64-test6.expected} | 0
...86-test7.cpuinfo => linux-x86_64-test7.cpuinfo} | 0
...-test7.expected => linux-x86_64-test7.expected} | 0
...86-test8.cpuinfo => linux-x86_64-test8.cpuinfo} | 0
...-test8.expected => linux-x86_64-test8.expected} | 0
tests/nodeinfotest.c | 57 ++++-----
19 files changed, 100 insertions(+), 99 deletions(-)
rename tests/nodeinfodata/{linux-x86-test1.cpuinfo => linux-x86_64-test1.cpuinfo}
(100%)
rename tests/nodeinfodata/{linux-x86-test1.expected => linux-x86_64-test1.expected}
(100%)
rename tests/nodeinfodata/{linux-x86-test2.cpuinfo => linux-x86_64-test2.cpuinfo}
(100%)
rename tests/nodeinfodata/{linux-x86-test2.expected => linux-x86_64-test2.expected}
(100%)
rename tests/nodeinfodata/{linux-x86-test3.cpuinfo => linux-x86_64-test3.cpuinfo}
(100%)
rename tests/nodeinfodata/{linux-x86-test3.expected => linux-x86_64-test3.expected}
(100%)
rename tests/nodeinfodata/{linux-x86-test4.cpuinfo => linux-x86_64-test4.cpuinfo}
(100%)
rename tests/nodeinfodata/{linux-x86-test4.expected => linux-x86_64-test4.expected}
(100%)
rename tests/nodeinfodata/{linux-x86-test5.cpuinfo => linux-x86_64-test5.cpuinfo}
(100%)
rename tests/nodeinfodata/{linux-x86-test5.expected => linux-x86_64-test5.expected}
(100%)
rename tests/nodeinfodata/{linux-x86-test6.cpuinfo => linux-x86_64-test6.cpuinfo}
(100%)
rename tests/nodeinfodata/{linux-x86-test6.expected => linux-x86_64-test6.expected}
(100%)
rename tests/nodeinfodata/{linux-x86-test7.cpuinfo => linux-x86_64-test7.cpuinfo}
(100%)
rename tests/nodeinfodata/{linux-x86-test7.expected => linux-x86_64-test7.expected}
(100%)
rename tests/nodeinfodata/{linux-x86-test8.cpuinfo => linux-x86_64-test8.cpuinfo}
(100%)
rename tests/nodeinfodata/{linux-x86-test8.expected => linux-x86_64-test8.expected}
(100%)
diff --git a/src/nodeinfo.c b/src/nodeinfo.c
index 5332ede..19164d9 100644
--- a/src/nodeinfo.c
+++ b/src/nodeinfo.c
@@ -52,9 +52,12 @@
#include "virtypedparam.h"
#include "virstring.h"
#include "virnuma.h"
+#include "virlog.h"
#define VIR_FROM_THIS VIR_FROM_NONE
+VIR_LOG_INIT("nodeinfo");
+
#if defined(__FreeBSD__) || defined(__APPLE__)
static int
appleFreebsdNodeGetCPUCount(void)
@@ -557,6 +560,7 @@ virNodeParseNode(const char *node,
int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
const char *sysfs_dir,
+ virArch arch,
virNodeInfoPtr nodeinfo)
{
char line[1024];
@@ -571,86 +575,86 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
/* Start with parsing CPU clock speed from /proc/cpuinfo */
while (fgets(line, sizeof(line), cpuinfo) != NULL) {
-# if defined(__x86_64__) || \
- defined(__amd64__) || \
- defined(__i386__)
- char *buf = line;
- if (STRPREFIX(buf, "cpu MHz")) {
- char *p;
- unsigned int ui;
+ if (arch == VIR_ARCH_X86_64 || arch == VIR_ARCH_I686) {
+ char *buf = line;
+ if (STRPREFIX(buf, "cpu MHz")) {
+ char *p;
+ unsigned int ui;
- buf += 7;
- while (*buf && c_isspace(*buf))
- buf++;
+ buf += 7;
+ while (*buf && c_isspace(*buf))
+ buf++;
- if (*buf != ':' || !buf[1]) {
- virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
- _("parsing cpu MHz from cpuinfo"));
- goto cleanup;
+ if (*buf != ':' || !buf[1]) {
+ virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+ _("parsing cpu MHz from cpuinfo"));
+ goto cleanup;
+ }
+
+ if (virStrToLong_ui(buf+1, &p, 10, &ui) == 0 &&
+ /* Accept trailing fractional part. */
+ (*p == '\0' || *p == '.' || c_isspace(*p)))
+ nodeinfo->mhz = ui;
}
- if (virStrToLong_ui(buf+1, &p, 10, &ui) == 0 &&
- /* Accept trailing fractional part. */
- (*p == '\0' || *p == '.' || c_isspace(*p)))
- nodeinfo->mhz = ui;
- }
+ } else if (arch == VIR_ARCH_PPC || arch == VIR_ARCH_PPCLE ||
+ arch == VIR_ARCH_PPC64 || arch == VIR_ARCH_PPC64LE ||
+ arch == VIR_ARCH_PPCEMB) {
+ char *buf = line;
+ if (STRPREFIX(buf, "clock")) {
+ char *p;
+ unsigned int ui;
-# elif defined(__powerpc__) || \
- defined(__powerpc64__)
- char *buf = line;
- if (STRPREFIX(buf, "clock")) {
- char *p;
- unsigned int ui;
+ buf += 5;
+ while (*buf && c_isspace(*buf))
+ buf++;
- buf += 5;
- while (*buf && c_isspace(*buf))
- buf++;
+ if (*buf != ':' || !buf[1]) {
+ virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+ _("parsing cpu MHz from cpuinfo"));
+ goto cleanup;
+ }
- if (*buf != ':' || !buf[1]) {
- virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
- _("parsing cpu MHz from cpuinfo"));
- goto cleanup;
+ if (virStrToLong_ui(buf+1, &p, 10, &ui) == 0 &&
+ /* Accept trailing fractional part. */
+ (*p == '\0' || *p == '.' || c_isspace(*p)))
+ nodeinfo->mhz = ui;
+ /* No other interesting infos are available in /proc/cpuinfo.
+ * However, there is a line identifying processor's version,
+ * identification and machine, but we don't want it to be caught
+ * and parsed in next iteration, because it is not in expected
+ * format and thus lead to error. */
}
+ } else if (arch == VIR_ARCH_ARMV6L || arch == VIR_ARCH_ARMV7L ||
+ arch == VIR_ARCH_ARMV7B || arch == VIR_ARCH_AARCH64) {
+ char *buf = line;
+ if (STRPREFIX(buf, "BogoMIPS")) {
+ char *p;
+ unsigned int ui;
- if (virStrToLong_ui(buf+1, &p, 10, &ui) == 0 &&
- /* Accept trailing fractional part. */
- (*p == '\0' || *p == '.' || c_isspace(*p)))
- nodeinfo->mhz = ui;
- /* No other interesting infos are available in /proc/cpuinfo.
- * However, there is a line identifying processor's version,
- * identification and machine, but we don't want it to be caught
- * and parsed in next iteration, because it is not in expected
- * format and thus lead to error. */
- }
-# elif defined(__arm__) || defined(__aarch64__)
- char *buf = line;
- if (STRPREFIX(buf, "BogoMIPS")) {
- char *p;
- unsigned int ui;
+ buf += 8;
+ while (*buf && c_isspace(*buf))
+ buf++;
- buf += 8;
- while (*buf && c_isspace(*buf))
- buf++;
+ if (*buf != ':' || !buf[1]) {
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ "%s", _("parsing cpu MHz from
cpuinfo"));
+ goto cleanup;
+ }
- if (*buf != ':' || !buf[1]) {
- virReportError(VIR_ERR_INTERNAL_ERROR,
- "%s", _("parsing cpu MHz from
cpuinfo"));
- goto cleanup;
+ if (virStrToLong_ui(buf+1, &p, 10, &ui) == 0
+ /* Accept trailing fractional part. */
+ && (*p == '\0' || *p == '.' ||
c_isspace(*p)))
+ nodeinfo->mhz = ui;
}
-
- if (virStrToLong_ui(buf+1, &p, 10, &ui) == 0
- /* Accept trailing fractional part. */
- && (*p == '\0' || *p == '.' || c_isspace(*p)))
- nodeinfo->mhz = ui;
+ } else if (arch == VIR_ARCH_S390 || arch == VIR_ARCH_S390X) {
+ /* s390x has no realistic value for CPU speed,
+ * assign a value of zero to signify this */
+ nodeinfo->mhz = 0;
+ } else {
+ VIR_WARN("Parser for /proc/cpuinfo needs to be adapted for your
architecture");
+ break;
}
-# elif defined(__s390__) || \
- defined(__s390x__)
- /* s390x has no realistic value for CPU speed,
- * assign a value of zero to signify this */
- nodeinfo->mhz = 0;
-# else
-# warning Parser for /proc/cpuinfo needs to be adapted for your architecture
-# endif
}
/* OK, we've parsed clock speed out of /proc/cpuinfo. Get the
@@ -1057,7 +1061,8 @@ int nodeGetInfo(virNodeInfoPtr nodeinfo)
return -1;
}
- ret = linuxNodeInfoCPUPopulate(cpuinfo, SYSFS_SYSTEM_PATH, nodeinfo);
+ ret = linuxNodeInfoCPUPopulate(cpuinfo, SYSFS_SYSTEM_PATH,
+ hostarch, nodeinfo);
if (ret < 0)
goto cleanup;
diff --git a/src/nodeinfopriv.h b/src/nodeinfopriv.h
index 6692363..8bfbe1e 100644
--- a/src/nodeinfopriv.h
+++ b/src/nodeinfopriv.h
@@ -27,6 +27,7 @@
# ifdef __linux__
int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
const char *sysfs_dir,
+ virArch arch,
virNodeInfoPtr nodeinfo);
int linuxNodeGetCPUStats(FILE *procstat,
diff --git a/tests/nodeinfodata/linux-x86-test1.cpuinfo
b/tests/nodeinfodata/linux-x86_64-test1.cpuinfo
similarity index 100%
rename from tests/nodeinfodata/linux-x86-test1.cpuinfo
rename to tests/nodeinfodata/linux-x86_64-test1.cpuinfo
diff --git a/tests/nodeinfodata/linux-x86-test1.expected
b/tests/nodeinfodata/linux-x86_64-test1.expected
similarity index 100%
rename from tests/nodeinfodata/linux-x86-test1.expected
rename to tests/nodeinfodata/linux-x86_64-test1.expected
diff --git a/tests/nodeinfodata/linux-x86-test2.cpuinfo
b/tests/nodeinfodata/linux-x86_64-test2.cpuinfo
similarity index 100%
rename from tests/nodeinfodata/linux-x86-test2.cpuinfo
rename to tests/nodeinfodata/linux-x86_64-test2.cpuinfo
diff --git a/tests/nodeinfodata/linux-x86-test2.expected
b/tests/nodeinfodata/linux-x86_64-test2.expected
similarity index 100%
rename from tests/nodeinfodata/linux-x86-test2.expected
rename to tests/nodeinfodata/linux-x86_64-test2.expected
diff --git a/tests/nodeinfodata/linux-x86-test3.cpuinfo
b/tests/nodeinfodata/linux-x86_64-test3.cpuinfo
similarity index 100%
rename from tests/nodeinfodata/linux-x86-test3.cpuinfo
rename to tests/nodeinfodata/linux-x86_64-test3.cpuinfo
diff --git a/tests/nodeinfodata/linux-x86-test3.expected
b/tests/nodeinfodata/linux-x86_64-test3.expected
similarity index 100%
rename from tests/nodeinfodata/linux-x86-test3.expected
rename to tests/nodeinfodata/linux-x86_64-test3.expected
diff --git a/tests/nodeinfodata/linux-x86-test4.cpuinfo
b/tests/nodeinfodata/linux-x86_64-test4.cpuinfo
similarity index 100%
rename from tests/nodeinfodata/linux-x86-test4.cpuinfo
rename to tests/nodeinfodata/linux-x86_64-test4.cpuinfo
diff --git a/tests/nodeinfodata/linux-x86-test4.expected
b/tests/nodeinfodata/linux-x86_64-test4.expected
similarity index 100%
rename from tests/nodeinfodata/linux-x86-test4.expected
rename to tests/nodeinfodata/linux-x86_64-test4.expected
diff --git a/tests/nodeinfodata/linux-x86-test5.cpuinfo
b/tests/nodeinfodata/linux-x86_64-test5.cpuinfo
similarity index 100%
rename from tests/nodeinfodata/linux-x86-test5.cpuinfo
rename to tests/nodeinfodata/linux-x86_64-test5.cpuinfo
diff --git a/tests/nodeinfodata/linux-x86-test5.expected
b/tests/nodeinfodata/linux-x86_64-test5.expected
similarity index 100%
rename from tests/nodeinfodata/linux-x86-test5.expected
rename to tests/nodeinfodata/linux-x86_64-test5.expected
diff --git a/tests/nodeinfodata/linux-x86-test6.cpuinfo
b/tests/nodeinfodata/linux-x86_64-test6.cpuinfo
similarity index 100%
rename from tests/nodeinfodata/linux-x86-test6.cpuinfo
rename to tests/nodeinfodata/linux-x86_64-test6.cpuinfo
diff --git a/tests/nodeinfodata/linux-x86-test6.expected
b/tests/nodeinfodata/linux-x86_64-test6.expected
similarity index 100%
rename from tests/nodeinfodata/linux-x86-test6.expected
rename to tests/nodeinfodata/linux-x86_64-test6.expected
diff --git a/tests/nodeinfodata/linux-x86-test7.cpuinfo
b/tests/nodeinfodata/linux-x86_64-test7.cpuinfo
similarity index 100%
rename from tests/nodeinfodata/linux-x86-test7.cpuinfo
rename to tests/nodeinfodata/linux-x86_64-test7.cpuinfo
diff --git a/tests/nodeinfodata/linux-x86-test7.expected
b/tests/nodeinfodata/linux-x86_64-test7.expected
similarity index 100%
rename from tests/nodeinfodata/linux-x86-test7.expected
rename to tests/nodeinfodata/linux-x86_64-test7.expected
diff --git a/tests/nodeinfodata/linux-x86-test8.cpuinfo
b/tests/nodeinfodata/linux-x86_64-test8.cpuinfo
similarity index 100%
rename from tests/nodeinfodata/linux-x86-test8.cpuinfo
rename to tests/nodeinfodata/linux-x86_64-test8.cpuinfo
diff --git a/tests/nodeinfodata/linux-x86-test8.expected
b/tests/nodeinfodata/linux-x86_64-test8.expected
similarity index 100%
rename from tests/nodeinfodata/linux-x86-test8.expected
rename to tests/nodeinfodata/linux-x86_64-test8.expected
diff --git a/tests/nodeinfotest.c b/tests/nodeinfotest.c
index a00a0b7..f0b8196 100644
--- a/tests/nodeinfotest.c
+++ b/tests/nodeinfotest.c
@@ -13,11 +13,7 @@
#define VIR_FROM_THIS VIR_FROM_NONE
-#if ! (defined __linux__ && (defined(__x86_64__) || \
- defined(__amd64__) || \
- defined(__i386__) || \
- defined(__powerpc__) || \
- defined(__powerpc64__)))
+#if !(defined __linux__)
int
main(void)
@@ -30,6 +26,7 @@ main(void)
static int
linuxTestCompareFiles(const char *cpuinfofile,
char *sysfs_dir,
+ virArch arch,
const char *outputfile)
{
int ret = -1;
@@ -46,7 +43,7 @@ linuxTestCompareFiles(const char *cpuinfofile,
goto fail;
memset(&nodeinfo, 0, sizeof(nodeinfo));
- if (linuxNodeInfoCPUPopulate(cpuinfo, sysfs_dir, &nodeinfo) < 0) {
+ if (linuxNodeInfoCPUPopulate(cpuinfo, sysfs_dir, arch, &nodeinfo) < 0) {
if (virTestGetDebug()) {
virErrorPtr error = virSaveLastError();
if (error && error->code != VIR_ERR_OK)
@@ -167,31 +164,31 @@ linuxCPUStatsCompareFiles(const char *cpustatfile,
}
+struct linuxTestNodeInfoData {
+ const char *testName;
+ virArch arch;
+};
+
static int
-linuxTestNodeInfo(const void *data)
+linuxTestNodeInfo(const void *opaque)
{
int result = -1;
char *cpuinfo = NULL;
char *sysfs_dir = NULL;
char *output = NULL;
- const char *test = data;
- const char *arch = "x86";
-
-# if defined(__powerpc__) || \
- defined(__powerpc64__)
- arch = "ppc";
-# endif
+ struct linuxTestNodeInfoData *data = (struct linuxTestNodeInfoData *) opaque;
+ const char *archStr = virArchToString(data->arch);
if (virAsprintf(&sysfs_dir, "%s/nodeinfodata/linux-%s",
- abs_srcdir, test) < 0 ||
+ abs_srcdir, data->testName) < 0 ||
virAsprintf(&cpuinfo, "%s/nodeinfodata/linux-%s-%s.cpuinfo",
- abs_srcdir, arch, test) < 0 ||
+ abs_srcdir, archStr, data->testName) < 0 ||
virAsprintf(&output, "%s/nodeinfodata/linux-%s-%s.expected",
- abs_srcdir, arch, test) < 0) {
+ abs_srcdir, archStr, data->testName) < 0) {
goto cleanup;
}
- result = linuxTestCompareFiles(cpuinfo, sysfs_dir, output);
+ result = linuxTestCompareFiles(cpuinfo, sysfs_dir, data->arch, output);
cleanup:
VIR_FREE(cpuinfo);
@@ -235,25 +232,23 @@ mymain(void)
{
int ret = 0;
size_t i;
- const char *nodeData[] = {
- "test1",
-# if !(defined(__powerpc__) || \
- defined(__powerpc64__))
- "test2",
- "test3",
- "test4",
- "test5",
- "test6",
- "test7",
- "test8",
-# endif
+ const struct linuxTestNodeInfoData nodeData[] = {
+ {"test1", VIR_ARCH_X86_64},
+ {"test1", VIR_ARCH_PPC},
+ {"test2", VIR_ARCH_X86_64},
+ {"test3", VIR_ARCH_X86_64},
+ {"test4", VIR_ARCH_X86_64},
+ {"test5", VIR_ARCH_X86_64},
+ {"test6", VIR_ARCH_X86_64},
+ {"test7", VIR_ARCH_X86_64},
+ {"test8", VIR_ARCH_X86_64},
};
if (virInitialize() < 0)
return EXIT_FAILURE;
for (i = 0; i < ARRAY_CARDINALITY(nodeData); i++)
- if (virtTestRun(nodeData[i], linuxTestNodeInfo, nodeData[i]) != 0)
+ if (virtTestRun(nodeData[i].testName, linuxTestNodeInfo, &nodeData[i]) != 0)
ret = -1;
# define DO_TEST_CPU_STATS(name, ncpus) \
--
1.8.5.5