Skip to content

Commit

Permalink
Merge pull request #314 from zouyee/swap
Browse files Browse the repository at this point in the history
Linux.Swap is defined as memory+swap combined, while in cgroup v2 swap is a separate value
  • Loading branch information
AkihiroSuda authored Dec 29, 2023
2 parents 5e7266a + 5729adb commit fe50040
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 @@ -177,6 +177,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 @@ -80,13 +80,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 fe50040

Please sign in to comment.