mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
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:
parent
ead66155ef
commit
f50cf60534
1 changed files with 11 additions and 9 deletions
|
@ -19,24 +19,26 @@ func AvailableCPUs() int {
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cpuCoresAvailable := getCPUQuota()
|
cpuQuota := getCPUQuota()
|
||||||
updateGOMAXPROCSToCPUQuota(cpuCoresAvailable)
|
if cpuQuota > 0 {
|
||||||
|
updateGOMAXPROCSToCPUQuota(cpuQuota)
|
||||||
|
}
|
||||||
|
cpuCoresAvailable := cpuQuota
|
||||||
|
if cpuCoresAvailable <= 0 {
|
||||||
|
cpuCoresAvailable = float64(runtime.NumCPU())
|
||||||
|
}
|
||||||
metrics.NewGauge(`process_cpu_cores_available`, func() float64 {
|
metrics.NewGauge(`process_cpu_cores_available`, func() float64 {
|
||||||
return cpuCoresAvailable
|
return cpuCoresAvailable
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateGOMAXPROCSToCPUQuota updates GOMAXPROCS to cpuCoresAvailable if GOMAXPROCS isn't set in environment var.
|
// updateGOMAXPROCSToCPUQuota updates GOMAXPROCS to cpuQuota if GOMAXPROCS isn't set in environment var.
|
||||||
func updateGOMAXPROCSToCPUQuota(cpuCoresAvailable float64) {
|
func updateGOMAXPROCSToCPUQuota(cpuQuota float64) {
|
||||||
if v := os.Getenv("GOMAXPROCS"); v != "" {
|
if v := os.Getenv("GOMAXPROCS"); v != "" {
|
||||||
// Do not override explicitly set GOMAXPROCS.
|
// Do not override explicitly set GOMAXPROCS.
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if cpuCoresAvailable <= 0 {
|
gomaxprocs := int(cpuQuota + 0.5)
|
||||||
// Do not change GOMAXPROCS if cpuCoresAvailable is incorrectly set.
|
|
||||||
return
|
|
||||||
}
|
|
||||||
gomaxprocs := int(cpuCoresAvailable + 0.5)
|
|
||||||
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.
|
||||||
|
|
Loading…
Reference in a new issue