Skip to content

Commit 5729adb

Browse files
committed
in cgroup v2 swap is a separate value
Signed-off-by: zouyee <[email protected]>
1 parent 2f48571 commit 5729adb

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

cgroup2/utils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ func ToResources(spec *specs.LinuxResources) *Resources {
176176
resources.Memory = &Memory{}
177177
if swap := mem.Swap; swap != nil {
178178
resources.Memory.Swap = swap
179+
if l := mem.Limit; l != nil {
180+
reduce := *swap - *l
181+
resources.Memory.Swap = &reduce
182+
}
179183
}
180184
if l := mem.Limit; l != nil {
181185
resources.Memory.Max = l

cgroup2/utils_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,20 @@ func TestToResources(t *testing.T) {
4646
quota int64 = 8000
4747
period uint64 = 10000
4848
shares uint64 = 5000
49+
50+
mem int64 = 300
51+
swap int64 = 500
4952
)
5053
weight := 1 + ((shares-2)*9999)/262142
51-
res := specs.LinuxResources{CPU: &specs.LinuxCPU{Quota: &quota, Period: &period, Shares: &shares}}
54+
res := specs.LinuxResources{
55+
CPU: &specs.LinuxCPU{Quota: &quota, Period: &period, Shares: &shares},
56+
Memory: &specs.LinuxMemory{Limit: &mem, Swap: &swap},
57+
}
5258
v2resources := ToResources(&res)
5359

5460
assert.Equal(t, weight, *v2resources.CPU.Weight)
5561
assert.Equal(t, CPUMax("8000 10000"), v2resources.CPU.Max)
62+
assert.Equal(t, swap-mem, *v2resources.Memory.Swap)
5663

5764
res2 := specs.LinuxResources{CPU: &specs.LinuxCPU{Period: &period}}
5865
v2resources2 := ToResources(&res2)

0 commit comments

Comments
 (0)