Skip to content

Commit 58a89a1

Browse files
daniel-samplineryermulnikMaxymVlasov
authored
fix: Parallelism CPU calculation inside Kubernetes and Docker with limits (antonbabenko#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]>
1 parent 53cadec commit 58a89a1

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

hooks/_common.sh

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ function common::is_hook_run_on_whole_repo {
188188

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

200-
local millicpu
205+
local cpu_quota cpu_period cpu_num
201206

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

207-
if [[ $millicpu -eq -1 ]]; then
213+
if [[ $cpu_quota -eq -1 || $cpu_period -lt 1 ]]; then
208214
# K8s no limits or in DinD
209215
if [[ -n $parallelism_ci_cpu_cores ]]; then
210216
if [[ ! $parallelism_ci_cpu_cores =~ ^[[:digit:]]+$ ]]; then
@@ -233,21 +239,24 @@ function common::get_cpu_num {
233239
return
234240
fi
235241

236-
echo $((millicpu / 1000))
242+
cpu_num=$((cpu_quota / cpu_period))
243+
[[ $cpu_num -lt 1 ]] && echo 1 || echo $cpu_num
237244
return
238245
fi
239246

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

244-
if [[ $millicpu == max ]]; then
252+
if [[ $cpu_quota == max || $cpu_period -lt 1 ]]; then
245253
# No limits
246254
nproc 2> /dev/null || echo 1
247255
return
248256
fi
249257

250-
echo $((millicpu / 1000))
258+
cpu_num=$((cpu_quota / cpu_period))
259+
[[ $cpu_num -lt 1 ]] && echo 1 || echo $cpu_num
251260
return
252261
fi
253262

0 commit comments

Comments
 (0)