Skip to content

Commit fe50040

Browse files
authored
Merge pull request #314 from zouyee/swap
Linux.Swap is defined as memory+swap combined, while in cgroup v2 swap is a separate value
2 parents 5e7266a + 5729adb commit fe50040

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

cgroup2/utils.go

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

cgroup2/utils_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,20 @@ func TestToResources(t *testing.T) {
8080
quota int64 = 8000
8181
period uint64 = 10000
8282
shares uint64 = 5000
83+
84+
mem int64 = 300
85+
swap int64 = 500
8386
)
8487
weight := 1 + ((shares-2)*9999)/262142
85-
res := specs.LinuxResources{CPU: &specs.LinuxCPU{Quota: &quota, Period: &period, Shares: &shares}}
88+
res := specs.LinuxResources{
89+
CPU: &specs.LinuxCPU{Quota: &quota, Period: &period, Shares: &shares},
90+
Memory: &specs.LinuxMemory{Limit: &mem, Swap: &swap},
91+
}
8692
v2resources := ToResources(&res)
8793

8894
assert.Equal(t, weight, *v2resources.CPU.Weight)
8995
assert.Equal(t, CPUMax("8000 10000"), v2resources.CPU.Max)
96+
assert.Equal(t, swap-mem, *v2resources.Memory.Swap)
9097

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

0 commit comments

Comments
 (0)