Update getNumberOfPhysicalCPUCores.cpp

In Kubernetes shares are calculated from resources.requests.cpu, quota & period - from limits.

When you configure only requests without limits - the container still can use all the cores. So you see all the cores the system has.
(because of requests the pod just can't be scheduled on the node which have less cpus)
So the effective CPU count is all the available cores if you don't have limits, or calculated from quota & period if you have the limits.
This commit is contained in:
filimonov 2022-03-31 17:21:54 +02:00 committed by GitHub
parent 1d480178c4
commit 24d72a18d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -38,21 +38,7 @@ unsigned getCGroupLimitedCPUCores(unsigned default_cpu_count)
quota_count = ceil(static_cast<float>(cgroup_quota) / static_cast<float>(cgroup_period)); quota_count = ceil(static_cast<float>(cgroup_quota) / static_cast<float>(cgroup_period));
} }
// Share number (typically a number relative to 1024) (2048 typically expresses 2 CPUs worth of processing) return std::min(default_cpu_count, quota_count);
// -1 for no share setup
int cgroup_share = read_from("/sys/fs/cgroup/cpu/cpu.shares", -1);
// Convert 1024 to no shares setup
if (cgroup_share == 1024)
cgroup_share = -1;
# define PER_CPU_SHARES 1024
unsigned share_count = default_cpu_count;
if (cgroup_share > -1)
{
share_count = ceil(static_cast<float>(cgroup_share) / static_cast<float>(PER_CPU_SHARES));
}
return std::min(default_cpu_count, std::min(share_count, quota_count));
} }
#endif // OS_LINUX #endif // OS_LINUX
@ -91,6 +77,7 @@ unsigned getNumberOfPhysicalCPUCores()
cpu_count = std::thread::hardware_concurrency(); cpu_count = std::thread::hardware_concurrency();
#if defined(OS_LINUX) #if defined(OS_LINUX)
/// TODO: add a setting for disabling that, similar to UseContainerSupport in java
cpu_count = getCGroupLimitedCPUCores(cpu_count); cpu_count = getCGroupLimitedCPUCores(cpu_count);
#endif // OS_LINUX #endif // OS_LINUX
return cpu_count; return cpu_count;