diff --git a/cgroup2/utils.go b/cgroup2/utils.go index e471aee4..20be57ca 100644 --- a/cgroup2/utils.go +++ b/cgroup2/utils.go @@ -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 diff --git a/cgroup2/utils_test.go b/cgroup2/utils_test.go index b194defa..37e315fb 100644 --- a/cgroup2/utils_test.go +++ b/cgroup2/utils_test.go @@ -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: "a, Period: &period, Shares: &shares}} + res := specs.LinuxResources{ + CPU: &specs.LinuxCPU{Quota: "a, 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)