diff --git a/internal/util/metricsinfo/hardware_info.go b/internal/util/metricsinfo/hardware_info.go index 23b7d24778..e1822865d9 100644 --- a/internal/util/metricsinfo/hardware_info.go +++ b/internal/util/metricsinfo/hardware_info.go @@ -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 } diff --git a/internal/util/metricsinfo/hardware_info_test.go b/internal/util/metricsinfo/hardware_info_test.go index 3fea77b23b..70e32aab45 100644 --- a/internal/util/metricsinfo/hardware_info_test.go +++ b/internal/util/metricsinfo/hardware_info_test.go @@ -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) {