diff --git a/daemon/config/config.go b/daemon/config/config.go index cc9037a25..c8cf44636 100644 --- a/daemon/config/config.go +++ b/daemon/config/config.go @@ -134,6 +134,9 @@ type Config struct { // EnableBuilder enable builder functionality EnableBuilder bool `json:"enable-builder,omitempty"` + + // MachineMemory is the memory limit for a host. + MachineMemory uint64 `json:"-"` } // GetCgroupDriver gets cgroup driver used in runc. diff --git a/daemon/mgr/container_stats.go b/daemon/mgr/container_stats.go index 636da1925..9e34eda58 100644 --- a/daemon/mgr/container_stats.go +++ b/daemon/mgr/container_stats.go @@ -38,6 +38,11 @@ func (mgr *ContainerManager) StreamStats(ctx context.Context, name string, confi wrapContainerStats := func(metricMeta *containerdtypes.Metric, metric *cgroups.Metrics) (*types.ContainerStats, error) { stats := toContainerStats(c, metricMeta, metric) + // if the container does not set memory limit, use the machineMemory + if stats.MemoryStats.Limit > mgr.Config.MachineMemory && mgr.Config.MachineMemory > 0 { + stats.MemoryStats.Limit = mgr.Config.MachineMemory + } + systemCPUUsage, err := getSystemCPUUsage() if err != nil { return nil, err diff --git a/main.go b/main.go index 850f67ee8..2a1e5fb54 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ import ( "github.com/alibaba/pouch/lxcfs" "github.com/alibaba/pouch/pkg/debug" "github.com/alibaba/pouch/pkg/kernel" + "github.com/alibaba/pouch/pkg/system" "github.com/alibaba/pouch/pkg/utils" "github.com/alibaba/pouch/storage/quota" "github.com/alibaba/pouch/version" @@ -227,6 +228,10 @@ func runDaemon(cmd *cobra.Command) error { return err } + if cfg.MachineMemory, err = system.GetTotalMem(); err != nil { + logrus.Warnf("failed to get system mem: %v", err) + } + // initialize signal and handle method. var ( errCh = make(chan error, 1)