Skip to content

Commit 4ce318b

Browse files
fix: parallelism CPU calculation inside k8s
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
1 parent dfbde65 commit 4ce318b

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

hooks/_common.sh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,15 @@ function common::is_hook_run_on_whole_repo {
197197
function common::get_cpu_num {
198198
local -r parallelism_ci_cpu_cores=$1
199199

200+
local cpu_quota cpu_period cpu_num
200201
local millicpu
201202

202203
if [[ -f /sys/fs/cgroup/cpu/cpu.cfs_quota_us &&
203204
! -f /proc/sys/fs/binfmt_misc/WSLInterop ]]; then # WSL have cfs_quota_us, but WSL should be checked as usual Linux host
204205
# Inside K8s pod or DinD in K8s
205-
millicpu=$(< /sys/fs/cgroup/cpu/cpu.cfs_quota_us)
206+
cpu_quota=$(< /sys/fs/cgroup/cpu/cpu.cfs_quota_us)
206207

207-
if [[ $millicpu -eq -1 ]]; then
208+
if [[ $cpu_quota -eq -1 ]]; then
208209
# K8s no limits or in DinD
209210
if [[ -n $parallelism_ci_cpu_cores ]]; then
210211
if [[ ! $parallelism_ci_cpu_cores =~ ^[[:digit:]]+$ ]]; then
@@ -233,7 +234,13 @@ function common::get_cpu_num {
233234
return
234235
fi
235236

236-
echo $((millicpu / 1000))
237+
cpu_period=$(< /sys/fs/cgroup/cpu/cpu.cfs_period_us)
238+
cpu_num=$((cpu_quota / cpu_period))
239+
if ((cpu_num < 1)); then
240+
echo 1
241+
else
242+
echo $cpu_num
243+
fi
237244
return
238245
fi
239246

0 commit comments

Comments
 (0)