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
This commit is contained in:
Aliaksandr Valialkin 2022-01-31 20:30:06 +02:00
parent 566e12874d
commit a02dde6cc7
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1

View File

@ -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.