2019-05-22 21:16:55 +00:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
|
2020-08-11 19:54:13 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup"
|
2019-05-22 21:16:55 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
)
|
|
|
|
|
2019-10-16 21:49:54 +00:00
|
|
|
const maxInt = int(^uint(0) >> 1)
|
|
|
|
|
2019-05-22 21:16:55 +00: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)
|
|
|
|
}
|
2019-10-16 21:49:54 +00:00
|
|
|
totalMem := maxInt
|
|
|
|
if uint64(maxInt)/uint64(si.Totalram) > uint64(si.Unit) {
|
|
|
|
totalMem = int(uint64(si.Totalram) * uint64(si.Unit))
|
|
|
|
}
|
2020-08-11 19:54:13 +00:00
|
|
|
mem := cgroup.GetMemoryLimit()
|
|
|
|
if mem <= 0 || int64(int(mem)) != mem || int(mem) > totalMem {
|
2019-06-28 15:08:09 +00:00
|
|
|
return totalMem
|
|
|
|
}
|
2020-08-11 19:54:13 +00:00
|
|
|
return int(mem)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|