From 58a89a1cc0760daa515f58da9bb8b167f01044bb Mon Sep 17 00:00:00 2001 From: Daniel Sampliner Date: Sat, 1 Feb 2025 17:51:46 -0500 Subject: [PATCH] fix: Parallelism CPU calculation inside Kubernetes and Docker with limits (#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 Co-authored-by: MaxymVlasov --- hooks/_common.sh | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index fc0c930a7..e9defff52 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -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 @@ -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 @@ -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