From f4c4ab811b383ed7c2abac03ac653551fb9a6965 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 28 Aug 2020 09:40:53 +0300 Subject: [PATCH] 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. --- lib/cgroup/cpu.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/cgroup/cpu.go b/lib/cgroup/cpu.go index 9b1a0ceac..54f4fcdd6 100644 --- a/lib/cgroup/cpu.go +++ b/lib/cgroup/cpu.go @@ -12,6 +12,8 @@ import ( // This function must be called after logger.Init(). func UpdateGOMAXPROCSToCPUQuota() { if v := os.Getenv("GOMAXPROCS"); v != "" { + // Do not override explicitly set GOMAXPROCS. + logger.Infof("using GOMAXPROCS=%q set via environment variable", v) return } q := getCPUQuota() @@ -20,6 +22,12 @@ func UpdateGOMAXPROCSToCPUQuota() { return } 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 { gomaxprocs = 1 }