From 1a778e7a7ec7a646596ed5bf6daba278f36902f6 Mon Sep 17 00:00:00 2001
From: Dan Callaghan <dcallagh@redhat.com>
Date: Thu, 18 Jun 2015 17:36:53 +1000
Subject: [PATCH 05/26] detect arch at runtime for /proc/cpuinfo parsing
This patch makes it possible to run cross-platform unit tests by
faking the architecture returned by uname(2) to match the format of
/proc/cpuinfo.
---
src/core/cpuinfo.cc | 75 +++++++++++++++++++++++------------------------------
src/core/osutils.cc | 9 ++-----
2 files changed, 35 insertions(+), 49 deletions(-)
diff --git a/src/core/cpuinfo.cc b/src/core/cpuinfo.cc
index 5a2b8c0..afde4c2 100644
--- a/src/core/cpuinfo.cc
+++ b/src/core/cpuinfo.cc
@@ -62,7 +62,6 @@ int n = 0)
}
-#ifdef __powerpc__
static void cpuinfo_ppc_ibm(hwNode & node,
const string & description, const string & version)
{
@@ -103,9 +102,7 @@ string value)
}
}
}
-#endif
-#ifdef __s390x__
static vector <string> s390x_features;
static string s390x_vendor;
static void cpuinfo_s390x(hwNode & node,
@@ -157,9 +154,7 @@ string value)
cpu->describeCapability("te", "transactional/constraint transactional execution facilities");
}
}
-#endif
-#ifdef __arm__
static void cpuinfo_arm(hwNode & node,
string id,
string value)
@@ -218,9 +213,7 @@ static void cpuinfo_arm(hwNode & node,
cpu->describeCapability("evtstrm", "Unknown");
}
}
-#endif
-#ifdef __aarch64__
static vector <string> aarch64_features;
static string aarch64_processor_name;
static void cpuinfo_aarch64(hwNode & node,
@@ -281,9 +274,7 @@ static void cpuinfo_aarch64(hwNode & node,
}
}
}
-#endif
-#ifdef __ia64__
static void cpuinfo_ia64(hwNode & node,
string id,
string value)
@@ -335,9 +328,7 @@ string value)
}
}
}
-#endif
-#ifdef __hppa__
static void cpuinfo_hppa(hwNode & node,
string id,
string value)
@@ -371,9 +360,7 @@ string value)
}
}
}
-#endif
-#ifdef __alpha__
static void cpuinfo_alpha(hwNode & node,
string id,
string value)
@@ -431,9 +418,7 @@ string value)
mycpu->enable();
}
}
-#endif
-#if defined(__i386__) || defined(__x86_64__)
static void cpuinfo_x86(hwNode & node,
string id,
string value)
@@ -577,7 +562,6 @@ string value)
if(node.getWidth()==0) node.setWidth(cpu->getWidth());
}
}
-#endif
bool scan_cpuinfo(hwNode & n)
{
@@ -599,6 +583,7 @@ bool scan_cpuinfo(hwNode & n)
size_t count;
string cpuinfo_str = "";
string description = "", version = "";
+ string plat = platform();
while ((count = read(cpuinfo, buffer, sizeof(buffer))) > 0)
{
@@ -624,48 +609,53 @@ bool scan_cpuinfo(hwNode & n)
id = hw::strip(cpuinfo_lines[i].substr(0, pos));
value = hw::strip(cpuinfo_lines[i].substr(pos + 1));
-#if defined(__i386__) || defined(__x86_64__)
- cpuinfo_x86(n, id, value);
-#endif
-#ifdef __powerpc__
-
- // All cores have same product name and version on power systems
- if (is_system_ppc_ibm(n))
+ if (plat == "ppc" || plat == "ppc64" || plat == "ppc64le")
{
- if (id == "cpu")
- description = value;
- if (id == "revision")
- version = value;
+ // All cores have same product name and version on power systems
+ if (is_system_ppc_ibm(n))
+ {
+ if (id == "cpu")
+ description = value;
+ if (id == "revision")
+ version = value;
- if (description != "" && version != "")
- {
- cpuinfo_ppc_ibm(n, description, version);
- break;
- }
+ if (description != "" && version != "")
+ {
+ cpuinfo_ppc_ibm(n, description, version);
+ break;
+ }
+ }
+ else
+ cpuinfo_ppc(n, id, value);
+ }
+ else if (plat == "hppa")
+ {
+ cpuinfo_hppa(n, id, value);
+ }
+ else if (plat == "alpha")
+ {
+ cpuinfo_alpha(n, id, value);
+ }
+ else if (plat == "ia64")
+ {
+ cpuinfo_ia64(n, id, value);
+ }
+ else if (plat == "s390" || plat == "s390x")
+ {
+ cpuinfo_s390x(n, id, value);
+ }
+ else if (plat.compare(0, 3, "arm") == 0)
+ {
+ cpuinfo_arm(n, id, value);
+ }
+ else if (plat == "aarch64")
+ {
+ cpuinfo_aarch64(n, id, value);
}
else
- cpuinfo_ppc(n, id, value);
-#endif
-#ifdef __s390x__
- cpuinfo_s390x(n, id, value);
-#endif
-#ifdef __hppa__
- cpuinfo_hppa(n, id, value);
-#endif
-#ifdef __alpha__
- cpuinfo_alpha(n, id, value);
-#endif
-#ifdef __ia64__
- cpuinfo_ia64(n, id, value);
-#endif
-
-#ifdef __arm__
- cpuinfo_arm(n, id, value);
-#endif
-#ifdef __aarch64__
- cpuinfo_aarch64(n, id, value);
-#endif
-
+ {
+ cpuinfo_x86(n, id, value);
+ }
}
}
}
diff --git a/src/core/osutils.cc b/src/core/osutils.cc
index cb7f638..c9543b9 100644
--- a/src/core/osutils.cc
+++ b/src/core/osutils.cc
@@ -832,15 +832,10 @@ string operating_system()
string platform()
{
- string p = "";
struct utsname u;
-#ifdef __i386__
- p = "i386";
-#endif
-
if(uname(&u) != 0)
- return p;
+ return string("i386");
else
- return p + (p!=""?"/":"") + string(u.machine);
+ return string(u.machine);
}
--
2.10.2