Skip to content

Commit

Permalink
Merge pull request #478 from r-a-sattarov/master
Browse files Browse the repository at this point in the history
platform_linux.cpp: Added sysconf use for s_numPhysicalCPUCores
  • Loading branch information
RobertBeckebans committed Jun 19, 2020
2 parents c0e76c4 + 7e25d69 commit 71bc94f
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions neo/sys/posix/platform_linux.cpp
Expand Up @@ -45,6 +45,11 @@ static const char** cmdargv = NULL;
static int cmdargc = 0;
// DG end

// RB begin
#include <stdio.h> // needed for sysconf()
#include <cstring>
// RB end

#ifdef ID_MCHECK
#include <mcheck.h>
#endif
Expand Down Expand Up @@ -164,15 +169,17 @@ double Sys_ClockTicksPerSecond()
========================
Sys_CPUCount
numLogicalCPUCores - the number of logical CPU per core
numPhysicalCPUCores - the total number of cores per package
numLogicalCPUCores - the total number of logical CPU cores (equal to the total number of threads from all CPU)
numPhysicalCPUCores - the total number of physical CPU cores
numCPUPackages - the total number of packages (physical processors)
========================
*/
// RB begin
void Sys_CPUCount( int& numLogicalCPUCores, int& numPhysicalCPUCores, int& numCPUPackages )
{
static bool init = false;
static bool CPUCoresIsFound = false; // needed for sysconf()
static bool SiblingsIsFound = false; // needed for sysconf()
static double ret;

static int s_numLogicalCPUCores;
Expand Down Expand Up @@ -217,11 +224,13 @@ void Sys_CPUCount( int& numLogicalCPUCores, int& numPhysicalCPUCores, int& numCP
if( ( processor ) > s_numPhysicalCPUCores )
{
s_numPhysicalCPUCores = processor;
CPUCoresIsFound = true;
}
}
else
{
common->Printf( "failed parsing /proc/cpuinfo\n" );
CPUCoresIsFound = false;
break;
}
}
Expand All @@ -240,17 +249,37 @@ void Sys_CPUCount( int& numLogicalCPUCores, int& numPhysicalCPUCores, int& numCP
if( ( coreId ) > s_numLogicalCPUCores )
{
s_numLogicalCPUCores = coreId;
SiblingsIsFound = true;
}
}
else
{
common->Printf( "failed parsing /proc/cpuinfo\n" );
SiblingsIsFound = false;
break;
}
}

pos = strchr( buf + pos, '\n' ) - buf + 1;
}
if( CPUCoresIsFound == false && SiblingsIsFound == false)
{
common->Printf( "failed parsing /proc/cpuinfo\n" );
common->Printf( "alternative method used\n" );
s_numPhysicalCPUCores = sysconf(_SC_NPROCESSORS_CONF); // _SC_NPROCESSORS_ONLN may not be reliable on Android
s_numLogicalCPUCores = s_numPhysicalCPUCores; // hack for CPU without Hyper-Threading (HT) technology
}
else if( CPUCoresIsFound == true && SiblingsIsFound == false)
{
s_numLogicalCPUCores = s_numPhysicalCPUCores; // hack for CPU without Hyper-Threading (HT) technology
}
}
else
{
common->Printf( "failed to read /proc/cpuinfo\n" );
common->Printf( "alternative method used\n" );
s_numPhysicalCPUCores = sysconf(_SC_NPROCESSORS_CONF); // _SC_NPROCESSORS_ONLN may not be reliable on Android
s_numLogicalCPUCores = s_numPhysicalCPUCores; // hack for CPU without Hyper-Threading (HT) technology
}

common->Printf( "/proc/cpuinfo CPU processors: %d\n", s_numPhysicalCPUCores );
Expand Down

0 comments on commit 71bc94f

Please sign in to comment.