ClickHouse/src/Common/getNumberOfPhysicalCPUCores.cpp

68 lines
1.9 KiB
C++
Raw Normal View History

#include "getNumberOfPhysicalCPUCores.h"
2022-01-18 06:51:13 +00:00
#include <Common/config.h>
#if defined(OS_LINUX)
# include <cmath>
# include <fstream>
#endif
#include <thread>
#if defined(OS_LINUX)
2022-04-15 20:54:08 +00:00
static int readFrom(const char * filename, int default_value)
{
2022-04-15 20:54:08 +00:00
std::ifstream infile(filename);
if (!infile.is_open())
return default_value;
int idata;
if (infile >> idata)
return idata;
else
return default_value;
}
2022-04-15 20:54:08 +00:00
/// Try to look at cgroups limit if it is available.
static unsigned getCGroupLimitedCPUCores(unsigned default_cpu_count)
{
unsigned quota_count = default_cpu_count;
2022-04-15 20:54:08 +00:00
/// Return the number of milliseconds per period process is guaranteed to run.
/// -1 for no quota
int cgroup_quota = readFrom("/sys/fs/cgroup/cpu/cpu.cfs_quota_us", -1);
int cgroup_period = readFrom("/sys/fs/cgroup/cpu/cpu.cfs_period_us", -1);
if (cgroup_quota > -1 && cgroup_period > 0)
quota_count = ceil(static_cast<float>(cgroup_quota) / static_cast<float>(cgroup_period));
return std::min(default_cpu_count, quota_count);
}
2022-04-15 20:54:08 +00:00
#endif
2022-04-15 20:56:51 +00:00
static unsigned getNumberOfPhysicalCPUCoresImpl()
{
2022-04-16 00:08:06 +00:00
unsigned cpu_count = std::thread::hardware_concurrency();
2022-04-16 00:08:06 +00:00
/// Most of x86_64 CPUs have 2-way Hyper-Threading
/// Aarch64 and RISC-V don't have SMT so far.
/// POWER has SMT and it can be multiple way (like 8-way), but we don't know how ClickHouse really behaves, so use all of them.
2022-04-16 00:08:06 +00:00
#if defined(__x86_64__)
/// Let's limit ourself to the number of physical cores.
/// But if the number of logical cores is small - maybe it is a small machine
/// or very limited cloud instance and it is reasonable to use all the cores.
if (cpu_count >= 8)
cpu_count /= 2;
#endif
#if defined(OS_LINUX)
2022-04-15 20:54:08 +00:00
cpu_count = getCGroupLimitedCPUCores(cpu_count);
#endif
return cpu_count;
}
2022-04-15 20:56:51 +00:00
unsigned getNumberOfPhysicalCPUCores()
{
/// Calculate once.
static auto res = getNumberOfPhysicalCPUCoresImpl();
return res;
}