Skip to content

Commit d8467c1

Browse files
authored
fix: handle no memory limit in coder stat mem (#11107)
1 parent 6d66cb2 commit d8467c1

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

cli/clistat/cgroup.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ const (
4444
cgroupV2MemoryStat = "/sys/fs/cgroup/memory.stat"
4545
)
4646

47+
const (
48+
// 9223372036854771712 is the highest positive signed 64-bit integer (263-1),
49+
// rounded down to multiples of 4096 (2^12), the most common page size on x86 systems.
50+
// This is used by docker to indicate no memory limit.
51+
UnlimitedMemory int64 = 9223372036854771712
52+
)
53+
4754
// ContainerCPU returns the CPU usage of the container cgroup.
4855
// This is calculated as difference of two samples of the
4956
// CPU usage of the container cgroup.
@@ -271,6 +278,10 @@ func (s *Statter) cGroupV1Memory(p Prefix) (*Result, error) {
271278
// Nonetheless, if it is not, assume there is no limit set.
272279
maxUsageBytes = -1
273280
}
281+
// Set to unlimited if we detect the unlimited docker value.
282+
if maxUsageBytes == UnlimitedMemory {
283+
maxUsageBytes = -1
284+
}
274285

275286
// need a space after total_rss so we don't hit something else
276287
usageBytes, err := readInt64(s.fs, cgroupV1MemoryUsageBytes)

cli/clistat/stat_internal_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,18 @@ func TestStatter(t *testing.T) {
197197
assert.Nil(t, mem.Total)
198198
assert.Equal(t, "B", mem.Unit)
199199
})
200+
t.Run("ContainerMemory/NoLimit", func(t *testing.T) {
201+
t.Parallel()
202+
fs := initFS(t, fsContainerCgroupV1DockerNoMemoryLimit)
203+
s, err := New(WithFS(fs), withNoWait)
204+
require.NoError(t, err)
205+
mem, err := s.ContainerMemory(PrefixDefault)
206+
require.NoError(t, err)
207+
require.NotNil(t, mem)
208+
assert.Equal(t, 268435456.0, mem.Used)
209+
assert.Nil(t, mem.Total)
210+
assert.Equal(t, "B", mem.Unit)
211+
})
200212
})
201213

202214
t.Run("CGroupV2", func(t *testing.T) {
@@ -384,6 +396,17 @@ proc /proc/sys proc ro,nosuid,nodev,noexec,relatime 0 0`,
384396
cgroupV1MemoryUsageBytes: "536870912",
385397
cgroupV1MemoryStat: "total_inactive_file 268435456",
386398
}
399+
fsContainerCgroupV1DockerNoMemoryLimit = map[string]string{
400+
procOneCgroup: "0::/docker/aa86ac98959eeedeae0ecb6e0c9ddd8ae8b97a9d0fdccccf7ea7a474f4e0bb1f",
401+
procMounts: `overlay / overlay rw,relatime,lowerdir=/some/path:/some/path,upperdir=/some/path:/some/path,workdir=/some/path:/some/path 0 0
402+
proc /proc/sys proc ro,nosuid,nodev,noexec,relatime 0 0`,
403+
cgroupV1CPUAcctUsage: "0",
404+
cgroupV1CFSQuotaUs: "-1",
405+
cgroupV1CFSPeriodUs: "100000",
406+
cgroupV1MemoryMaxUsageBytes: "9223372036854771712",
407+
cgroupV1MemoryUsageBytes: "536870912",
408+
cgroupV1MemoryStat: "total_inactive_file 268435456",
409+
}
387410
fsContainerCgroupV1AltPath = map[string]string{
388411
procOneCgroup: "0::/docker/aa86ac98959eeedeae0ecb6e0c9ddd8ae8b97a9d0fdccccf7ea7a474f4e0bb1f",
389412
procMounts: `overlay / overlay rw,relatime,lowerdir=/some/path:/some/path,upperdir=/some/path:/some/path,workdir=/some/path:/some/path 0 0

0 commit comments

Comments
 (0)