Skip to content

Commit

Permalink
Linux.Swap is defined as memory+swap combined, while in cgroup v2 swa…
Browse files Browse the repository at this point in the history
…p is a separate value

Signed-off-by: zouyee <zouyee1989@gmail.com>
  • Loading branch information
zouyee committed Dec 2, 2023
1 parent 2f48571 commit b3158fe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cgroup2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ func ToResources(spec *specs.LinuxResources) *Resources {
resources.Memory = &Memory{}
if swap := mem.Swap; swap != nil {
resources.Memory.Swap = swap
if l := mem.Limit; l != nil {
reduce := *swap - *l
resources.Memory.Swap = &reduce
}
}
if l := mem.Limit; l != nil {
resources.Memory.Max = l
Expand Down
9 changes: 8 additions & 1 deletion cgroup2/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,20 @@ func TestToResources(t *testing.T) {
quota int64 = 8000
period uint64 = 10000
shares uint64 = 5000

mem int64 = 300
swap int64 = 500
)
weight := 1 + ((shares-2)*9999)/262142
res := specs.LinuxResources{CPU: &specs.LinuxCPU{Quota: &quota, Period: &period, Shares: &shares}}
res := specs.LinuxResources{
CPU: &specs.LinuxCPU{Quota: &quota, Period: &period, Shares: &shares},
Memory: &specs.LinuxMemory{Limit: &mem, Swap: &swap},
}
v2resources := ToResources(&res)

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

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

0 comments on commit b3158fe

Please sign in to comment.