lib/cgroup: limit the maximum GOMAXPROCS value to the number of available CPU cores

There is no sense in setting GOMAXPROCS to value higher than the number of available CPU cores.
This commit is contained in:
Aliaksandr Valialkin 2020-08-28 09:40:53 +03:00
parent 08b76cb26f
commit 45e770ed20

View file

@ -12,6 +12,8 @@ import (
// This function must be called after logger.Init(). // This function must be called after logger.Init().
func UpdateGOMAXPROCSToCPUQuota() { func UpdateGOMAXPROCSToCPUQuota() {
if v := os.Getenv("GOMAXPROCS"); v != "" { if v := os.Getenv("GOMAXPROCS"); v != "" {
// Do not override explicitly set GOMAXPROCS.
logger.Infof("using GOMAXPROCS=%q set via environment variable", v)
return return
} }
q := getCPUQuota() q := getCPUQuota()
@ -20,6 +22,12 @@ func UpdateGOMAXPROCSToCPUQuota() {
return return
} }
gomaxprocs := int(q + 0.5) gomaxprocs := int(q + 0.5)
numCPU := runtime.NumCPU()
if gomaxprocs > numCPU {
// There is no sense in setting more GOMAXPROCS than the number of available CPU cores.
logger.Infof("cgroup CPU quota=%d exceeds NumCPU=%d; using GOMAXPROCS=NumCPU", gomaxprocs, numCPU)
return
}
if gomaxprocs <= 0 { if gomaxprocs <= 0 {
gomaxprocs = 1 gomaxprocs = 1
} }