Skip to content

Commit

Permalink
stats: get mem limit from the cgroup
Browse files Browse the repository at this point in the history
b25b330 introduced this behaviour.

It was fine at the time because we didn't support "container update",
so the limit could not be changed at runtime.  Since it is not
possible to change the memory limit at runtime, read the limit as
reported from the cgroup.

containers/crun#1217 is required for crun.

[NO NEW TESTS NEEDED] needs a new crun release

Closes: containers#18621

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
  • Loading branch information
giuseppe committed May 19, 2023
1 parent a58ea23 commit 90f9c1f
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions libpod/stats_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package libpod

import (
"fmt"
"math"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -59,7 +58,7 @@ func (c *Container) getPlatformContainerStats(stats *define.ContainerStats, prev
// calc the average cpu usage for the time the container is running
stats.AvgCPU = calculateCPUPercent(cgroupStats, 0, now, uint64(c.state.StartedTime.UnixNano()))
stats.MemUsage = cgroupStats.MemoryStats.Usage.Usage
stats.MemLimit = c.getMemLimit()
stats.MemLimit = c.getMemLimit(cgroupStats.MemoryStats.Usage.Total)
stats.MemPerc = (float64(stats.MemUsage) / float64(stats.MemLimit)) * 100
stats.PIDs = 0
if conState == define.ContainerStateRunning || conState == define.ContainerStatePaused {
Expand All @@ -83,14 +82,7 @@ func (c *Container) getPlatformContainerStats(stats *define.ContainerStats, prev
}

// getMemory limit returns the memory limit for a container
func (c *Container) getMemLimit() uint64 {
memLimit := uint64(math.MaxUint64)

resources := c.LinuxResources()
if resources != nil && resources.Memory != nil && resources.Memory.Limit != nil {
memLimit = uint64(*resources.Memory.Limit)
}

func (c *Container) getMemLimit(memLimit uint64) uint64 {
si := &syscall.Sysinfo_t{}
err := syscall.Sysinfo(si)
if err != nil {
Expand Down

0 comments on commit 90f9c1f

Please sign in to comment.