From 109772bdc4ae682030636186d26c3ef560c98b7d Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 23 Sep 2024 16:09:10 +0200 Subject: [PATCH] lib/cgroup: round GOMAXPROCS to the lower integer value of cpuQuota Rounding GOMAXPROCS to the upper interger value of cpuQuota increases chances of CPU starvation, non-optimimal goroutine scheduling and additional CPU overhead related to context switching. So it is better to round GOMAXPROCS to the lower integer value of cpuQuota. --- lib/cgroup/cpu.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/cgroup/cpu.go b/lib/cgroup/cpu.go index 15a007adf..0904973e9 100644 --- a/lib/cgroup/cpu.go +++ b/lib/cgroup/cpu.go @@ -37,15 +37,20 @@ func updateGOMAXPROCSToCPUQuota(cpuQuota float64) { // Do not override explicitly set GOMAXPROCS. return } - gomaxprocs := int(cpuQuota + 0.5) + + // Round gomaxprocs to the floor of cpuQuota, since Go runtime doesn't work well + // with fractional available CPU cores. + gomaxprocs := int(cpuQuota) + if gomaxprocs <= 0 { + gomaxprocs = 1 + } + numCPU := runtime.NumCPU() if gomaxprocs > numCPU { // There is no sense in setting more GOMAXPROCS than the number of available CPU cores. gomaxprocs = numCPU } - if gomaxprocs <= 0 { - gomaxprocs = 1 - } + runtime.GOMAXPROCS(gomaxprocs) }