Blame SOURCES/0005-detect-arch-at-runtime-for-proc-cpuinfo-parsing.patch

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