mirror of
https://github.com/boostorg/build.git
synced 2026-02-16 01:12:13 +00:00
Rework sysinfo cpu to avoid overcounts.
When running in Linux containers the POSIX sysconf can return "too many" cores or cpus. Instead we prefer using Linux specific sched_getaffinity there.
This commit is contained in:
@@ -7,41 +7,103 @@
|
||||
#include "jam.h"
|
||||
#include "output.h"
|
||||
|
||||
#include <thread>
|
||||
|
||||
#if defined(OS_MACOSX)
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
#endif
|
||||
|
||||
#if !defined(OS_NT)
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <thread>
|
||||
#if defined(OS_LINUX)
|
||||
#define _GNU_SOURCE
|
||||
#include <sched.h>
|
||||
#undef _GNU_SOURCE
|
||||
#endif
|
||||
|
||||
|
||||
b2::system_info::system_info()
|
||||
{
|
||||
}
|
||||
|
||||
unsigned int b2::system_info::cpu_core_count()
|
||||
namespace
|
||||
{
|
||||
if (cpu_core_count_ == 0)
|
||||
unsigned int macosx_physicalcpu()
|
||||
{
|
||||
#if defined(OS_MACOSX)
|
||||
int out_hw_ncpu = 0;
|
||||
size_t len_hw_ncpu = sizeof(out_hw_ncpu);
|
||||
int result = ::sysctlbyname(
|
||||
"hw.physicalcpu", &out_hw_ncpu, &len_hw_ncpu, nullptr, 0);
|
||||
if (result == 0)
|
||||
{
|
||||
cpu_core_count_ = out_hw_ncpu;
|
||||
}
|
||||
#elif defined(_SC_NPROCESSORS_CONF)
|
||||
cpu_thread_count_ = ::sysconf(_SC_NPROCESSORS_CONF);
|
||||
if (result == 0) return out_hw_ncpu;
|
||||
#endif
|
||||
if (cpu_core_count_ <= 0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int macosx_logicalcpu()
|
||||
{
|
||||
#if defined(OS_MACOSX)
|
||||
int out_hw_ncpu = 0;
|
||||
size_t len_hw_ncpu = sizeof(out_hw_ncpu);
|
||||
int result = ::sysctlbyname(
|
||||
"hw.logicalcpu", &out_hw_ncpu, &len_hw_ncpu, nullptr, 0);
|
||||
if (result == 0) return out_hw_ncpu;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int sched_affinity_cpu_count()
|
||||
{
|
||||
#if defined(CPU_COUNT_S)
|
||||
::cpu_set_t cpu_set;
|
||||
if (::sched_getaffinity(0, sizeof(cpu_set_t), &cpu_set) == 0)
|
||||
{
|
||||
cpu_core_count_ = 1;
|
||||
return CPU_COUNT_S(sizeof(cpu_set_t), &cpu_set);
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int sysconf_nprocs_configured()
|
||||
{
|
||||
#if defined(_SC_NPROCESSORS_ONLN)
|
||||
return ::sysconf(_SC_NPROCESSORS_CONF);
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
unsigned int sysconf_nprocs_online()
|
||||
{
|
||||
#if defined(_SC_NPROCESSORS_ONLN)
|
||||
return ::sysconf(_SC_NPROCESSORS_ONLN);
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
unsigned int std_thread_hardware_concurrency()
|
||||
{
|
||||
return std::thread::hardware_concurrency();
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int b2::system_info::cpu_core_count()
|
||||
{
|
||||
if (cpu_core_count_ == 0)
|
||||
{
|
||||
cpu_thread_count_ = macosx_physicalcpu();
|
||||
}
|
||||
if (cpu_thread_count_ == 0)
|
||||
{
|
||||
cpu_thread_count_ = sysconf_nprocs_configured();
|
||||
}
|
||||
if (cpu_core_count_ <= 0)
|
||||
{
|
||||
cpu_core_count_ = 1;
|
||||
}
|
||||
return cpu_core_count_;
|
||||
}
|
||||
@@ -50,24 +112,23 @@ unsigned int b2::system_info::cpu_thread_count()
|
||||
{
|
||||
if (cpu_thread_count_ == 0)
|
||||
{
|
||||
#if defined(OS_MACOSX)
|
||||
int out_hw_ncpu = 0;
|
||||
size_t len_hw_ncpu = sizeof(out_hw_ncpu);
|
||||
int result = ::sysctlbyname(
|
||||
"hw.logicalcpu", &out_hw_ncpu, &len_hw_ncpu, nullptr, 0);
|
||||
if (result == 0)
|
||||
{
|
||||
cpu_thread_count_ = out_hw_ncpu;
|
||||
}
|
||||
#elif defined(_SC_NPROCESSORS_ONLN)
|
||||
cpu_thread_count_ = ::sysconf(_SC_NPROCESSORS_ONLN);
|
||||
#else
|
||||
cpu_thread_count_ = std::thread::hardware_concurrency();
|
||||
#endif
|
||||
if (cpu_thread_count_ <= 0)
|
||||
{
|
||||
cpu_thread_count_ = cpu_core_count();
|
||||
}
|
||||
cpu_thread_count_ = macosx_logicalcpu();
|
||||
}
|
||||
if (cpu_thread_count_ == 0)
|
||||
{
|
||||
cpu_thread_count_ = sched_affinity_cpu_count();
|
||||
}
|
||||
if (cpu_thread_count_ == 0)
|
||||
{
|
||||
cpu_thread_count_ = sysconf_nprocs_online();
|
||||
}
|
||||
if (cpu_thread_count_ == 0)
|
||||
{
|
||||
cpu_thread_count_ = std_thread_hardware_concurrency();
|
||||
}
|
||||
if (cpu_thread_count_ == 0)
|
||||
{
|
||||
cpu_thread_count_ = cpu_core_count();
|
||||
}
|
||||
return cpu_thread_count_;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user