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.
This commit is contained in:
Aliaksandr Valialkin 2024-09-23 16:09:10 +02:00
parent 3964889705
commit 109772bdc4
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB

View file

@ -37,15 +37,20 @@ func updateGOMAXPROCSToCPUQuota(cpuQuota float64) {
// Do not override explicitly set GOMAXPROCS. // Do not override explicitly set GOMAXPROCS.
return 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() numCPU := runtime.NumCPU()
if gomaxprocs > numCPU { if gomaxprocs > numCPU {
// There is no sense in setting more GOMAXPROCS than the number of available CPU cores. // There is no sense in setting more GOMAXPROCS than the number of available CPU cores.
gomaxprocs = numCPU gomaxprocs = numCPU
} }
if gomaxprocs <= 0 {
gomaxprocs = 1
}
runtime.GOMAXPROCS(gomaxprocs) runtime.GOMAXPROCS(gomaxprocs)
} }