Skip to content

Commit

Permalink
fix: Parallelism CPU calculation inside Kubernetes and Docker with li…
Browse files Browse the repository at this point in the history
…mits (#799)

The value of /sys/fs/cgroup/cpu/cpu.cfs_quota_us is not in milliseconds
and cannot be simply divided by 1000 to determine the CPU limit. As per
kernel documentation[^1], the cpu limit can be determined by dividing
that value by /sys/fs/cgroup/cpu/cpu.cfs_period_us.

[^1]: https://docs.kernel.org/scheduler/sched-bwc.html

---------

Co-authored-by: George L. Yermulnik <[email protected]>
Co-authored-by: MaxymVlasov <[email protected]>
  • Loading branch information
3 people authored Feb 1, 2025
1 parent 53cadec commit 58a89a1
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions hooks/_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ function common::is_hook_run_on_whole_repo {

#######################################################################
# Get the number of CPU logical cores available for pre-commit to use
#
# CPU quota should be calculated as `cpu.cfs_quota_us / cpu.cfs_period_us`
# For K8s see: https://docs.kernel.org/scheduler/sched-bwc.html
# For Docker see: https://docs.docker.com/engine/containers/resource_constraints/#configure-the-default-cfs-scheduler
#
# Arguments:
# parallelism_ci_cpu_cores (string) Used in edge cases when number of
# CPU cores can't be derived automatically
Expand All @@ -197,14 +202,15 @@ function common::is_hook_run_on_whole_repo {
function common::get_cpu_num {
local -r parallelism_ci_cpu_cores=$1

local millicpu
local cpu_quota cpu_period cpu_num

if [[ -f /sys/fs/cgroup/cpu/cpu.cfs_quota_us &&
! -f /proc/sys/fs/binfmt_misc/WSLInterop ]]; then # WSL have cfs_quota_us, but WSL should be checked as usual Linux host
# Inside K8s pod or DinD in K8s
millicpu=$(< /sys/fs/cgroup/cpu/cpu.cfs_quota_us)
cpu_quota=$(< /sys/fs/cgroup/cpu/cpu.cfs_quota_us)
cpu_period=$(cat /sys/fs/cgroup/cpu/cpu.cfs_period_us 2> /dev/null || echo "$cpu_quota")

if [[ $millicpu -eq -1 ]]; then
if [[ $cpu_quota -eq -1 || $cpu_period -lt 1 ]]; then
# K8s no limits or in DinD
if [[ -n $parallelism_ci_cpu_cores ]]; then
if [[ ! $parallelism_ci_cpu_cores =~ ^[[:digit:]]+$ ]]; then
Expand Down Expand Up @@ -233,21 +239,24 @@ function common::get_cpu_num {
return
fi

echo $((millicpu / 1000))
cpu_num=$((cpu_quota / cpu_period))
[[ $cpu_num -lt 1 ]] && echo 1 || echo $cpu_num
return
fi

if [[ -f /sys/fs/cgroup/cpu.max ]]; then
# Inside Linux (Docker?) container
millicpu=$(cut -d' ' -f1 /sys/fs/cgroup/cpu.max)
cpu_quota=$(cut -d' ' -f1 /sys/fs/cgroup/cpu.max)
cpu_period=$(cut -d' ' -f2 /sys/fs/cgroup/cpu.max)

if [[ $millicpu == max ]]; then
if [[ $cpu_quota == max || $cpu_period -lt 1 ]]; then
# No limits
nproc 2> /dev/null || echo 1
return
fi

echo $((millicpu / 1000))
cpu_num=$((cpu_quota / cpu_period))
[[ $cpu_num -lt 1 ]] && echo 1 || echo $cpu_num
return
fi

Expand Down

0 comments on commit 58a89a1

Please sign in to comment.