VictoriaMetrics/lib/memory/memory_linux.go

32 lines
854 B
Go
Raw Normal View History

2019-05-22 23:16:55 +02:00
package memory
import (
"syscall"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup"
2019-05-22 23:16:55 +02:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
)
const maxInt = int(^uint(0) >> 1)
2019-05-22 23:16:55 +02:00
func sysTotalMemory() int {
var si syscall.Sysinfo_t
if err := syscall.Sysinfo(&si); err != nil {
logger.Panicf("FATAL: error in syscall.Sysinfo: %s", err)
}
totalMem := maxInt
if uint64(maxInt)/uint64(si.Totalram) > uint64(si.Unit) {
totalMem = int(uint64(si.Totalram) * uint64(si.Unit))
}
mem := cgroup.GetMemoryLimit()
if mem <= 0 || int64(int(mem)) != mem || int(mem) > totalMem {
// Try reading hierachical memory limit.
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/699
mem = cgroup.GetHierarchicalMemoryLimit()
if mem <= 0 || int64(int(mem)) != mem || int(mem) > totalMem {
return totalMem
}
}
return int(mem)
2019-05-22 23:16:55 +02:00
}