Fix GetMemoryCount, get min as total memory (#15931)

Signed-off-by: bigsheeper <yihao.dai@zilliz.com>
This commit is contained in:
bigsheeper 2022-03-24 20:41:25 +08:00 committed by GitHub
parent 9deb245d77
commit 2fcf7b9760
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 11 deletions

View File

@ -66,22 +66,28 @@ func GetMemoryCount() uint64 {
log.Error(icErr.Error())
return 0
}
if ic {
// in container, calculate by `cgroups`
limit, err := getContainerMemLimit()
if err != nil {
log.Error(err.Error())
return 0
}
return limit
}
// not in container, calculate by `gopsutil`
// get host memory by `gopsutil`
stats, err := mem.VirtualMemory()
if err != nil {
log.Warn("failed to get memory count",
zap.Error(err))
return 0
}
// not in container, return host memory
if !ic {
return stats.Total
}
// get container memory by `cgroups`
limit, err := getContainerMemLimit()
if err != nil {
log.Error(err.Error())
return 0
}
// in container, return min(hostMem, containerMem)
if limit < stats.Total {
return limit
}
return stats.Total
}

View File

@ -14,8 +14,10 @@ package metricsinfo
import (
"testing"
"github.com/milvus-io/milvus/internal/log"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"github.com/milvus-io/milvus/internal/log"
)
func Test_GetCPUCoreCount(t *testing.T) {
@ -34,6 +36,8 @@ func Test_GetCPUUsage(t *testing.T) {
func Test_GetMemoryCount(t *testing.T) {
log.Info("TestGetMemoryCount",
zap.Uint64("MemoryCount", GetMemoryCount()))
assert.NotZero(t, GetMemoryCount())
}
func Test_GetUsedMemoryCount(t *testing.T) {