提交 e8083575 编写于 作者: M Michal Privoznik

nodeinfo: Introduce @arch to linuxNodeInfoCPUPopulate

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: NMichal Privoznik <mprivozn@redhat.com>
上级 0b33d7c9
...@@ -52,9 +52,12 @@ ...@@ -52,9 +52,12 @@
#include "virtypedparam.h" #include "virtypedparam.h"
#include "virstring.h" #include "virstring.h"
#include "virnuma.h" #include "virnuma.h"
#include "virlog.h"
#define VIR_FROM_THIS VIR_FROM_NONE #define VIR_FROM_THIS VIR_FROM_NONE
VIR_LOG_INIT("nodeinfo");
#if defined(__FreeBSD__) || defined(__APPLE__) #if defined(__FreeBSD__) || defined(__APPLE__)
static int static int
appleFreebsdNodeGetCPUCount(void) appleFreebsdNodeGetCPUCount(void)
...@@ -557,6 +560,7 @@ virNodeParseNode(const char *node, ...@@ -557,6 +560,7 @@ virNodeParseNode(const char *node,
int linuxNodeInfoCPUPopulate(FILE *cpuinfo, int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
const char *sysfs_dir, const char *sysfs_dir,
virArch arch,
virNodeInfoPtr nodeinfo) virNodeInfoPtr nodeinfo)
{ {
char line[1024]; char line[1024];
...@@ -571,9 +575,7 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo, ...@@ -571,9 +575,7 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
/* Start with parsing CPU clock speed from /proc/cpuinfo */ /* Start with parsing CPU clock speed from /proc/cpuinfo */
while (fgets(line, sizeof(line), cpuinfo) != NULL) { while (fgets(line, sizeof(line), cpuinfo) != NULL) {
# if defined(__x86_64__) || \ if (ARCH_IS_X86(arch)) {
defined(__amd64__) || \
defined(__i386__)
char *buf = line; char *buf = line;
if (STRPREFIX(buf, "cpu MHz")) { if (STRPREFIX(buf, "cpu MHz")) {
char *p; char *p;
...@@ -595,8 +597,7 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo, ...@@ -595,8 +597,7 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
nodeinfo->mhz = ui; nodeinfo->mhz = ui;
} }
# elif defined(__powerpc__) || \ } else if (ARCH_IS_PPC(arch)) {
defined(__powerpc64__)
char *buf = line; char *buf = line;
if (STRPREFIX(buf, "clock")) { if (STRPREFIX(buf, "clock")) {
char *p; char *p;
...@@ -622,7 +623,7 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo, ...@@ -622,7 +623,7 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
* and parsed in next iteration, because it is not in expected * and parsed in next iteration, because it is not in expected
* format and thus lead to error. */ * format and thus lead to error. */
} }
# elif defined(__arm__) || defined(__aarch64__) } else if (ARCH_IS_ARM(arch)) {
char *buf = line; char *buf = line;
if (STRPREFIX(buf, "BogoMIPS")) { if (STRPREFIX(buf, "BogoMIPS")) {
char *p; char *p;
...@@ -643,14 +644,14 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo, ...@@ -643,14 +644,14 @@ int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
&& (*p == '\0' || *p == '.' || c_isspace(*p))) && (*p == '\0' || *p == '.' || c_isspace(*p)))
nodeinfo->mhz = ui; nodeinfo->mhz = ui;
} }
# elif defined(__s390__) || \ } else if (ARCH_IS_S390(arch)) {
defined(__s390x__)
/* s390x has no realistic value for CPU speed, /* s390x has no realistic value for CPU speed,
* assign a value of zero to signify this */ * assign a value of zero to signify this */
nodeinfo->mhz = 0; nodeinfo->mhz = 0;
# else } else {
# warning Parser for /proc/cpuinfo needs to be adapted for your architecture VIR_WARN("Parser for /proc/cpuinfo needs to be adapted for your architecture");
# endif break;
}
} }
/* OK, we've parsed clock speed out of /proc/cpuinfo. Get the /* OK, we've parsed clock speed out of /proc/cpuinfo. Get the
...@@ -1057,7 +1058,8 @@ int nodeGetInfo(virNodeInfoPtr nodeinfo) ...@@ -1057,7 +1058,8 @@ int nodeGetInfo(virNodeInfoPtr nodeinfo)
return -1; return -1;
} }
ret = linuxNodeInfoCPUPopulate(cpuinfo, SYSFS_SYSTEM_PATH, nodeinfo); ret = linuxNodeInfoCPUPopulate(cpuinfo, SYSFS_SYSTEM_PATH,
hostarch, nodeinfo);
if (ret < 0) if (ret < 0)
goto cleanup; goto cleanup;
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
# ifdef __linux__ # ifdef __linux__
int linuxNodeInfoCPUPopulate(FILE *cpuinfo, int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
const char *sysfs_dir, const char *sysfs_dir,
virArch arch,
virNodeInfoPtr nodeinfo); virNodeInfoPtr nodeinfo);
int linuxNodeGetCPUStats(FILE *procstat, int linuxNodeGetCPUStats(FILE *procstat,
......
...@@ -73,6 +73,20 @@ typedef enum { ...@@ -73,6 +73,20 @@ typedef enum {
# define ARCH_IS_X86(arch) ((arch) == VIR_ARCH_X86_64 ||\ # define ARCH_IS_X86(arch) ((arch) == VIR_ARCH_X86_64 ||\
(arch) == VIR_ARCH_I686) (arch) == VIR_ARCH_I686)
# define ARCH_IS_PPC(arch) ((arch) == VIR_ARCH_PPC ||\
(arch) == VIR_ARCH_PPCLE ||\
(arch) == VIR_ARCH_PPC64 ||\
(arch) == VIR_ARCH_PPC64LE ||\
(arch) == VIR_ARCH_PPCEMB)
# define ARCH_IS_ARM(arch) ((arch) == VIR_ARCH_ARMV6L ||\
(arch) == VIR_ARCH_ARMV7L ||\
(arch) == VIR_ARCH_ARMV7B ||\
(arch) == VIR_ARCH_AARCH64)
# define ARCH_IS_S390(arch) ((arch) == VIR_ARCH_S390 ||\
(arch) == VIR_ARCH_S390X)
typedef enum { typedef enum {
VIR_ARCH_LITTLE_ENDIAN, VIR_ARCH_LITTLE_ENDIAN,
VIR_ARCH_BIG_ENDIAN, VIR_ARCH_BIG_ENDIAN,
......
...@@ -13,11 +13,7 @@ ...@@ -13,11 +13,7 @@
#define VIR_FROM_THIS VIR_FROM_NONE #define VIR_FROM_THIS VIR_FROM_NONE
#if ! (defined __linux__ && (defined(__x86_64__) || \ #if !(defined __linux__)
defined(__amd64__) || \
defined(__i386__) || \
defined(__powerpc__) || \
defined(__powerpc64__)))
int int
main(void) main(void)
...@@ -30,6 +26,7 @@ main(void) ...@@ -30,6 +26,7 @@ main(void)
static int static int
linuxTestCompareFiles(const char *cpuinfofile, linuxTestCompareFiles(const char *cpuinfofile,
char *sysfs_dir, char *sysfs_dir,
virArch arch,
const char *outputfile) const char *outputfile)
{ {
int ret = -1; int ret = -1;
...@@ -46,7 +43,7 @@ linuxTestCompareFiles(const char *cpuinfofile, ...@@ -46,7 +43,7 @@ linuxTestCompareFiles(const char *cpuinfofile,
goto fail; goto fail;
memset(&nodeinfo, 0, sizeof(nodeinfo)); memset(&nodeinfo, 0, sizeof(nodeinfo));
if (linuxNodeInfoCPUPopulate(cpuinfo, sysfs_dir, &nodeinfo) < 0) { if (linuxNodeInfoCPUPopulate(cpuinfo, sysfs_dir, arch, &nodeinfo) < 0) {
if (virTestGetDebug()) { if (virTestGetDebug()) {
virErrorPtr error = virSaveLastError(); virErrorPtr error = virSaveLastError();
if (error && error->code != VIR_ERR_OK) if (error && error->code != VIR_ERR_OK)
...@@ -167,31 +164,31 @@ linuxCPUStatsCompareFiles(const char *cpustatfile, ...@@ -167,31 +164,31 @@ linuxCPUStatsCompareFiles(const char *cpustatfile,
} }
struct linuxTestNodeInfoData {
const char *testName;
virArch arch;
};
static int static int
linuxTestNodeInfo(const void *data) linuxTestNodeInfo(const void *opaque)
{ {
int result = -1; int result = -1;
char *cpuinfo = NULL; char *cpuinfo = NULL;
char *sysfs_dir = NULL; char *sysfs_dir = NULL;
char *output = NULL; char *output = NULL;
const char *test = data; struct linuxTestNodeInfoData *data = (struct linuxTestNodeInfoData *) opaque;
const char *arch = "x86"; const char *archStr = virArchToString(data->arch);
# if defined(__powerpc__) || \
defined(__powerpc64__)
arch = "ppc";
# endif
if (virAsprintf(&sysfs_dir, "%s/nodeinfodata/linux-%s", 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", 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", virAsprintf(&output, "%s/nodeinfodata/linux-%s-%s.expected",
abs_srcdir, arch, test) < 0) { abs_srcdir, archStr, data->testName) < 0) {
goto cleanup; goto cleanup;
} }
result = linuxTestCompareFiles(cpuinfo, sysfs_dir, output); result = linuxTestCompareFiles(cpuinfo, sysfs_dir, data->arch, output);
cleanup: cleanup:
VIR_FREE(cpuinfo); VIR_FREE(cpuinfo);
...@@ -235,25 +232,23 @@ mymain(void) ...@@ -235,25 +232,23 @@ mymain(void)
{ {
int ret = 0; int ret = 0;
size_t i; size_t i;
const char *nodeData[] = { const struct linuxTestNodeInfoData nodeData[] = {
"test1", {"test1", VIR_ARCH_X86_64},
# if !(defined(__powerpc__) || \ {"test1", VIR_ARCH_PPC},
defined(__powerpc64__)) {"test2", VIR_ARCH_X86_64},
"test2", {"test3", VIR_ARCH_X86_64},
"test3", {"test4", VIR_ARCH_X86_64},
"test4", {"test5", VIR_ARCH_X86_64},
"test5", {"test6", VIR_ARCH_X86_64},
"test6", {"test7", VIR_ARCH_X86_64},
"test7", {"test8", VIR_ARCH_X86_64},
"test8",
# endif
}; };
if (virInitialize() < 0) if (virInitialize() < 0)
return EXIT_FAILURE; return EXIT_FAILURE;
for (i = 0; i < ARRAY_CARDINALITY(nodeData); i++) 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; ret = -1;
# define DO_TEST_CPU_STATS(name, ncpus) \ # define DO_TEST_CPU_STATS(name, ncpus) \
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册