From f50cf605346227cdd4fb80b3eb25d043b5b7f60f Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 31 Jan 2022 20:30:06 +0200 Subject: [PATCH] lib/cgroup: fall back to runtime.NumCPU() when determining process_cpu_cores_available metric if it is impossible to determine cpu quota via cgroups Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2107 --- lib/cgroup/cpu.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/cgroup/cpu.go b/lib/cgroup/cpu.go index 14f16e6c9..218764804 100644 --- a/lib/cgroup/cpu.go +++ b/lib/cgroup/cpu.go @@ -19,24 +19,26 @@ func AvailableCPUs() int { } func init() { - cpuCoresAvailable := getCPUQuota() - updateGOMAXPROCSToCPUQuota(cpuCoresAvailable) + cpuQuota := getCPUQuota() + if cpuQuota > 0 { + updateGOMAXPROCSToCPUQuota(cpuQuota) + } + cpuCoresAvailable := cpuQuota + if cpuCoresAvailable <= 0 { + cpuCoresAvailable = float64(runtime.NumCPU()) + } metrics.NewGauge(`process_cpu_cores_available`, func() float64 { return cpuCoresAvailable }) } -// updateGOMAXPROCSToCPUQuota updates GOMAXPROCS to cpuCoresAvailable if GOMAXPROCS isn't set in environment var. -func updateGOMAXPROCSToCPUQuota(cpuCoresAvailable float64) { +// updateGOMAXPROCSToCPUQuota updates GOMAXPROCS to cpuQuota if GOMAXPROCS isn't set in environment var. +func updateGOMAXPROCSToCPUQuota(cpuQuota float64) { if v := os.Getenv("GOMAXPROCS"); v != "" { // Do not override explicitly set GOMAXPROCS. return } - if cpuCoresAvailable <= 0 { - // Do not change GOMAXPROCS if cpuCoresAvailable is incorrectly set. - return - } - gomaxprocs := int(cpuCoresAvailable + 0.5) + gomaxprocs := int(cpuQuota + 0.5) numCPU := runtime.NumCPU() if gomaxprocs > numCPU { // There is no sense in setting more GOMAXPROCS than the number of available CPU cores.