mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-15 00:13:30 +01:00
lib/cgroup: attempt to obtain available CPU cores via /sys/devices/system/cpu/online
See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/685#issuecomment-674423728
This commit is contained in:
parent
4ebd2fa560
commit
89d652b583
@ -1,8 +1,11 @@
|
|||||||
package cgroup
|
package cgroup
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
||||||
)
|
)
|
||||||
@ -40,9 +43,57 @@ func getCPUQuota() float64 {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
if quotaUS <= 0 {
|
||||||
|
// The quota isn't set. This may be the case in multilevel containers.
|
||||||
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/685#issuecomment-674423728
|
||||||
|
return getOnlineCPUCount()
|
||||||
|
}
|
||||||
periodUS, err := readInt64("/sys/fs/cgroup/cpu/cpu.cfs_period_us", "cat /sys/fs/cgroup/cpu$(cat /proc/self/cgroup | grep cpu, | cut -d: -f3)/cpu.cfs_period_us")
|
periodUS, err := readInt64("/sys/fs/cgroup/cpu/cpu.cfs_period_us", "cat /sys/fs/cgroup/cpu$(cat /proc/self/cgroup | grep cpu, | cut -d: -f3)/cpu.cfs_period_us")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return float64(quotaUS) / float64(periodUS)
|
return float64(quotaUS) / float64(periodUS)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getOnlineCPUCount() float64 {
|
||||||
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/685#issuecomment-674423728
|
||||||
|
data, err := ioutil.ReadFile("/sys/devices/system/cpu/online")
|
||||||
|
if err != nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
n := float64(countCPUs(string(data)))
|
||||||
|
if n <= 0 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
// Add a half of CPU core, since it looks like actual cores is usually bigger than online cores.
|
||||||
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/685#issuecomment-674423728
|
||||||
|
return n + 0.5
|
||||||
|
}
|
||||||
|
|
||||||
|
func countCPUs(data string) int {
|
||||||
|
data = strings.TrimSpace(data)
|
||||||
|
n := 0
|
||||||
|
for _, s := range strings.Split(data, ",") {
|
||||||
|
n++
|
||||||
|
if !strings.Contains(s, "-") {
|
||||||
|
if _, err := strconv.Atoi(s); err != nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
bounds := strings.Split(s, "-")
|
||||||
|
if len(bounds) != 2 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
start, err := strconv.Atoi(bounds[0])
|
||||||
|
if err != nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
end, err := strconv.Atoi(bounds[1])
|
||||||
|
if err != nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
n += end - start
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
24
lib/cgroup/cpu_test.go
Normal file
24
lib/cgroup/cpu_test.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package cgroup
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCountCPUs(t *testing.T) {
|
||||||
|
f := func(s string, nExpected int) {
|
||||||
|
t.Helper()
|
||||||
|
n := countCPUs(s)
|
||||||
|
if n != nExpected {
|
||||||
|
t.Fatalf("unexpected result from countCPUs(%q); got %d; want %d", s, n, nExpected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f("", -1)
|
||||||
|
f("1", 1)
|
||||||
|
f("234", 1)
|
||||||
|
f("1,2", 2)
|
||||||
|
f("0-1", 2)
|
||||||
|
f("0-0", 1)
|
||||||
|
f("1-2,3,5-9,200-210", 19)
|
||||||
|
f("0-3", 4)
|
||||||
|
f("0-6", 7)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user