Skip to content

Commit

Permalink
stats: cap memory limit to the available memory
Browse files Browse the repository at this point in the history
Docker compatibility: cap the memory limit reported by the cgroup to
the maximum available memory.

Closes: containers#15765

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Sep 15, 2022
1 parent cf2118e commit 60ef4ad
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
11 changes: 11 additions & 0 deletions pkg/api/handlers/compat/containers_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/api/handlers/utils"
api "github.com/containers/podman/v4/pkg/api/types"
"github.com/containers/storage/pkg/system"
docker "github.com/docker/docker/api/types"
"github.com/gorilla/schema"
runccgroups "github.com/opencontainers/runc/libcontainer/cgroups"
Expand Down Expand Up @@ -139,6 +140,16 @@ streamLabel: // A label to flatten the scope
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()
s := StatsJSON{
Stats: Stats{
Expand Down
11 changes: 11 additions & 0 deletions test/apiv2/20-containers.at
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ if root; then
podman rm -f $CTRNAME
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.
t POST libpod/containers/create?name=test_noargs Image=${IMAGE} 201 \
.Id~[0-9a-f]\\{64\\}
Expand Down
24 changes: 23 additions & 1 deletion test/apiv2/test-apiv2
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ function is() {
_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
##########
Expand Down Expand Up @@ -377,7 +393,13 @@ function t() {
fi

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
json_field=$(expr "$i" : "\([^=]*\)=")
expect=$(expr "$i" : '[^=]*=\(.*\)')
Expand Down

0 comments on commit 60ef4ad

Please sign in to comment.