From ff0a079a817de37db60cde620a515fbc9b672b0e Mon Sep 17 00:00:00 2001
From: Francois Bertel <francois.bertel@kitware.com>
Date: Sat, 31 May 2008 11:23:15 -0400
Subject: [PATCH] BUG:Fixed NumberOfLogicalCPU, NumberOfPhysicalCPU and
 LogicalProcessorsPerPhysical under Linux. Some part was just wrong. Some
 other part missed to take the multicore value into account.

---
 SystemInformation.cxx | 40 ++++++++++++++++++++++++----------------
 kwsysDateStamp.cmake  |  2 +-
 2 files changed, 25 insertions(+), 17 deletions(-)

diff --git a/SystemInformation.cxx b/SystemInformation.cxx
index 19cb80d..08be90f 100644
--- a/SystemInformation.cxx
+++ b/SystemInformation.cxx
@@ -2161,38 +2161,46 @@ int SystemInformationImplementation::RetreiveInformationFromCpuInfoFile()
   
   buffer.resize(fileSize-2);
 
-  // Number of CPUs
+  // Number of logical CPUs (combination of multiple processors, multi-core
+  // and hyperthreading)
   size_t pos = buffer.find("processor\t");
   while(pos != buffer.npos)
     {
     this->NumberOfLogicalCPU++;
-    this->NumberOfPhysicalCPU++;
     pos = buffer.find("processor\t",pos+1);
     }
 
-  // Count the number of physical ids that are the same
-  int currentId = -1;
-  kwsys_stl::string idc = this->ExtractValueFromCpuInfoFile(buffer,"physical id");
-
+  // Find the largest physical id.
+  int maxId = -1;
+  kwsys_stl::string idc =
+                       this->ExtractValueFromCpuInfoFile(buffer,"physical id");
   while(this->CurrentPositionInFile != buffer.npos)
     {
-    int id = atoi(idc.c_str());
-    if(id == currentId)
+      int id = atoi(idc.c_str());
+      if(id > maxId)
       {
-      this->NumberOfPhysicalCPU--;
+       maxId=id;
       }
-    currentId = id;
-    idc = this->ExtractValueFromCpuInfoFile(buffer,"physical id",this->CurrentPositionInFile+1);
+    idc = this->ExtractValueFromCpuInfoFile(buffer,"physical id",
+                                            this->CurrentPositionInFile+1);
     }
 
-   if(this->NumberOfPhysicalCPU>0)
-     {
-     this->NumberOfLogicalCPU /= this->NumberOfPhysicalCPU;
-     }
+  // Physical ids returned by Linux don't distinguish cores.
+  // We want to record the total number of cores in this->NumberOfPhysicalCPU
+  // (checking only the first proc)
+  kwsys_stl::string cores =
+                        this->ExtractValueFromCpuInfoFile(buffer,"cpu cores");
+  int numberOfCoresPerCPU=atoi(cores.c_str());
+
+  this->NumberOfPhysicalCPU=numberOfCoresPerCPU*(maxId+1);
+
+  // LogicalProcessorsPerPhysical>1 => hyperthreading.
+  this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical=
+                            this->NumberOfLogicalCPU/this->NumberOfPhysicalCPU;
 
   // CPU speed (checking only the first proc
   kwsys_stl::string CPUSpeed = this->ExtractValueFromCpuInfoFile(buffer,"cpu MHz");
-  this->CPUSpeedInMHz = (float)atof(CPUSpeed.c_str());
+  this->CPUSpeedInMHz = static_cast<float>(atof(CPUSpeed.c_str()));
 
   // Chip family
   this->ChipID.Family = atoi(this->ExtractValueFromCpuInfoFile(buffer,"cpu family").c_str());
diff --git a/kwsysDateStamp.cmake b/kwsysDateStamp.cmake
index c5bdfc2..fad7d0b 100644
--- a/kwsysDateStamp.cmake
+++ b/kwsysDateStamp.cmake
@@ -7,4 +7,4 @@ SET(KWSYS_DATE_STAMP_YEAR  2008)
 SET(KWSYS_DATE_STAMP_MONTH 05)
 
 # KWSys version date day component.  Format is DD.
-SET(KWSYS_DATE_STAMP_DAY   27)
+SET(KWSYS_DATE_STAMP_DAY   31)
-- 
GitLab