Merge pull request #15811 from giuseppe/fix-memory-stats-api

stats: cap memory limit to the available memory
This commit is contained in:
OpenShift Merge Robot
2022-09-15 12:12:48 +02:00
committed by GitHub
3 changed files with 52 additions and 2 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/containers/podman/v4/libpod/define" "github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/api/handlers/utils" "github.com/containers/podman/v4/pkg/api/handlers/utils"
api "github.com/containers/podman/v4/pkg/api/types" api "github.com/containers/podman/v4/pkg/api/types"
"github.com/containers/storage/pkg/system"
docker "github.com/docker/docker/api/types" docker "github.com/docker/docker/api/types"
"github.com/gorilla/schema" "github.com/gorilla/schema"
runccgroups "github.com/opencontainers/runc/libcontainer/cgroups" runccgroups "github.com/opencontainers/runc/libcontainer/cgroups"
@ -139,6 +140,16 @@ streamLabel: // A label to flatten the scope
memoryLimit = uint64(*cfg.Spec.Linux.Resources.Memory.Limit) memoryLimit = uint64(*cfg.Spec.Linux.Resources.Memory.Limit)
} }
memInfo, err := system.ReadMemInfo()
if err != nil {
logrus.Errorf("Unable to get cgroup stats: %v", err)
return
}
// cap the memory limit to the available memory.
if memInfo.MemTotal > 0 && memoryLimit > uint64(memInfo.MemTotal) {
memoryLimit = uint64(memInfo.MemTotal)
}
systemUsage, _ := cgroups.GetSystemCPUUsage() systemUsage, _ := cgroups.GetSystemCPUUsage()
s := StatsJSON{ s := StatsJSON{
Stats: Stats{ Stats: Stats{
@ -177,7 +188,7 @@ streamLabel: // A label to flatten the scope
PreCPUStats: preCPUStats, PreCPUStats: preCPUStats,
MemoryStats: docker.MemoryStats{ MemoryStats: docker.MemoryStats{
Usage: cgroupStat.MemoryStats.Usage.Usage, Usage: cgroupStat.MemoryStats.Usage.Usage,
MaxUsage: cgroupStat.MemoryStats.Usage.Limit, MaxUsage: cgroupStat.MemoryStats.Usage.MaxUsage,
Stats: nil, Stats: nil,
Failcnt: 0, Failcnt: 0,
Limit: memoryLimit, Limit: memoryLimit,

View File

@ -98,6 +98,12 @@ else
fi fi
fi fi
# max_usage is not set for cgroupv2
if have_cgroupsv2; then
t GET libpod/containers/stats?containers='[$cid]' 200 \
.memory_stats.max_usage=null
fi
t DELETE libpod/containers/$cid 200 .[0].Id=$cid t DELETE libpod/containers/$cid 200 .[0].Id=$cid
# Issue #14676: make sure the stats show the memory limit specified for the container # Issue #14676: make sure the stats show the memory limit specified for the container
@ -111,6 +117,17 @@ if root; then
podman rm -f $CTRNAME podman rm -f $CTRNAME
fi fi
# Issue #15765: make sure the memory limit is capped
if root; then
CTRNAME=ctr-with-limit
podman run --name $CTRNAME -d -m 512m -v /tmp:/tmp $IMAGE top
t GET libpod/containers/$CTRNAME/stats?stream=false 200 \
.memory_stats.limit!=18446744073709552000
podman rm -f $CTRNAME
fi
# Issue #6799: it should be possible to start a container, even w/o args. # Issue #6799: it should be possible to start a container, even w/o args.
t POST libpod/containers/create?name=test_noargs Image=${IMAGE} 201 \ t POST libpod/containers/create?name=test_noargs Image=${IMAGE} 201 \
.Id~[0-9a-f]\\{64\\} .Id~[0-9a-f]\\{64\\}

View File

@ -107,6 +107,22 @@ function is() {
_show_ok 0 "$testname" "$expect" "$actual" _show_ok 0 "$testname" "$expect" "$actual"
} }
############
# is_not # Simple disequality
############
function is_not() {
local actual=$1
local expect_not=$2
local testname=$3
if [ "$actual" != "$expect_not" ]; then
# On success, include expected value; this helps readers understand
_show_ok 1 "$testname!=$expect"
return
fi
_show_ok 0 "$testname" "!= $expect" "$actual"
}
########## ##########
# like # Compare, but allowing patterns # like # Compare, but allowing patterns
########## ##########
@ -377,7 +393,13 @@ function t() {
fi fi
for i; do for i; do
if expr "$i" : "[^=~]\+=.*" >/dev/null; then if expr "$i" : '[^\!]\+\!=.\+' >/dev/null; then
# Disequality on json field
json_field=$(expr "$i" : '\([^!]*\)!')
expect_not=$(expr "$i" : '[^\!]*\!=\(.*\)')
actual=$(jq -r "$json_field" <<<"$output")
is_not "$actual" "$expect_not" "$testname : $json_field"
elif expr "$i" : "[^=~]\+=.*" >/dev/null; then
# Exact match on json field # Exact match on json field
json_field=$(expr "$i" : "\([^=]*\)=") json_field=$(expr "$i" : "\([^=]*\)=")
expect=$(expr "$i" : '[^=]*=\(.*\)') expect=$(expr "$i" : '[^=]*=\(.*\)')