Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cri: make swapping disabled with memory limit #7783

Merged
merged 1 commit into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions integration/container_update_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,13 @@ func TestUpdateContainerResources_MemoryLimit(t *testing.T) {
pauseImage := images.Get(images.Pause)
EnsureImageExists(t, pauseImage)

expectedSwapLimit := func(memoryLimit int64) *int64 {
if cgroups.Mode() == cgroups.Unified {
memoryLimit = 0
}
return &memoryLimit
}

t.Log("Create a container with memory limit")
cnConfig := ContainerConfig(
"container",
Expand All @@ -254,6 +261,7 @@ func TestUpdateContainerResources_MemoryLimit(t *testing.T) {
spec, err := container.Spec(context.Background())
require.NoError(t, err)
checkMemoryLimit(t, spec, 200*1024*1024)
checkMemorySwapLimit(t, spec, expectedSwapLimit(200*1024*1024))

t.Log("Update container memory limit after created")
err = runtimeService.UpdateContainerResources(cn, &runtime.LinuxContainerResources{
Expand All @@ -265,6 +273,7 @@ func TestUpdateContainerResources_MemoryLimit(t *testing.T) {
spec, err = container.Spec(context.Background())
require.NoError(t, err)
checkMemoryLimit(t, spec, 400*1024*1024)
checkMemorySwapLimit(t, spec, expectedSwapLimit(400*1024*1024))

t.Log("Start the container")
require.NoError(t, runtimeService.StartContainer(cn))
Expand All @@ -274,6 +283,8 @@ func TestUpdateContainerResources_MemoryLimit(t *testing.T) {
t.Log("Check memory limit in cgroup")
memLimit := getCgroupMemoryLimitForTask(t, task)
assert.Equal(t, uint64(400*1024*1024), memLimit)
swapLimit := getCgroupSwapLimitForTask(t, task)
assert.Equal(t, uint64(400*1024*1024), swapLimit)

t.Log("Update container memory limit after started")
err = runtimeService.UpdateContainerResources(cn, &runtime.LinuxContainerResources{
Expand All @@ -285,10 +296,14 @@ func TestUpdateContainerResources_MemoryLimit(t *testing.T) {
spec, err = container.Spec(context.Background())
require.NoError(t, err)
checkMemoryLimit(t, spec, 800*1024*1024)
checkMemorySwapLimit(t, spec, expectedSwapLimit(800*1024*1024))

t.Log("Check memory limit in cgroup")
memLimit = getCgroupMemoryLimitForTask(t, task)
assert.Equal(t, uint64(800*1024*1024), memLimit)
swapLimit = getCgroupSwapLimitForTask(t, task)
assert.Equal(t, uint64(800*1024*1024), swapLimit)

}

func TestUpdateContainerResources_StatusUpdated(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/cri/opts/spec_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,10 @@ func WithResources(resources *runtime.LinuxContainerResources, tolerateMissingHu
}
if limit != 0 {
s.Linux.Resources.Memory.Limit = &limit
// swap/memory limit should be equal to prevent container from swapping by default
if swapLimit == 0 {
s.Linux.Resources.Memory.Swap = &limit
}
}
if swapLimit != 0 {
s.Linux.Resources.Memory.Swap = &swapLimit
fuweid marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
20 changes: 16 additions & 4 deletions pkg/cri/sbserver/container_update_resources_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ func TestUpdateOCILinuxResource(t *testing.T) {
Process: &runtimespec.Process{OOMScoreAdj: oomscoreadj},
Linux: &runtimespec.Linux{
Resources: &runtimespec.LinuxResources{
Memory: &runtimespec.LinuxMemory{Limit: proto.Int64(54321)},
Memory: &runtimespec.LinuxMemory{
Limit: proto.Int64(54321),
Swap: proto.Int64(54321),
},
CPU: &runtimespec.LinuxCPU{
Shares: proto.Uint64(4444),
Quota: proto.Int64(5555),
Expand Down Expand Up @@ -113,7 +116,10 @@ func TestUpdateOCILinuxResource(t *testing.T) {
Process: &runtimespec.Process{OOMScoreAdj: oomscoreadj},
Linux: &runtimespec.Linux{
Resources: &runtimespec.LinuxResources{
Memory: &runtimespec.LinuxMemory{Limit: proto.Int64(54321)},
Memory: &runtimespec.LinuxMemory{
Limit: proto.Int64(54321),
Swap: proto.Int64(54321),
},
CPU: &runtimespec.LinuxCPU{
Shares: proto.Uint64(4444),
Quota: proto.Int64(5555),
Expand Down Expand Up @@ -151,7 +157,10 @@ func TestUpdateOCILinuxResource(t *testing.T) {
Process: &runtimespec.Process{OOMScoreAdj: oomscoreadj},
Linux: &runtimespec.Linux{
Resources: &runtimespec.LinuxResources{
Memory: &runtimespec.LinuxMemory{Limit: proto.Int64(54321)},
Memory: &runtimespec.LinuxMemory{
Limit: proto.Int64(54321),
Swap: proto.Int64(54321),
},
CPU: &runtimespec.LinuxCPU{
Shares: proto.Uint64(4444),
Quota: proto.Int64(5555),
Expand Down Expand Up @@ -197,7 +206,10 @@ func TestUpdateOCILinuxResource(t *testing.T) {
Process: &runtimespec.Process{OOMScoreAdj: oomscoreadj},
Linux: &runtimespec.Linux{
Resources: &runtimespec.LinuxResources{
Memory: &runtimespec.LinuxMemory{Limit: proto.Int64(54321)},
Memory: &runtimespec.LinuxMemory{
Limit: proto.Int64(54321),
Swap: proto.Int64(54321),
},
CPU: &runtimespec.LinuxCPU{
Shares: proto.Uint64(4444),
Quota: proto.Int64(5555),
Expand Down
20 changes: 16 additions & 4 deletions pkg/cri/server/container_update_resources_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ func TestUpdateOCILinuxResource(t *testing.T) {
Process: &runtimespec.Process{OOMScoreAdj: oomscoreadj},
Linux: &runtimespec.Linux{
Resources: &runtimespec.LinuxResources{
Memory: &runtimespec.LinuxMemory{Limit: proto.Int64(54321)},
Memory: &runtimespec.LinuxMemory{
Limit: proto.Int64(54321),
Swap: proto.Int64(54321),
},
CPU: &runtimespec.LinuxCPU{
Shares: proto.Uint64(4444),
Quota: proto.Int64(5555),
Expand Down Expand Up @@ -113,7 +116,10 @@ func TestUpdateOCILinuxResource(t *testing.T) {
Process: &runtimespec.Process{OOMScoreAdj: oomscoreadj},
Linux: &runtimespec.Linux{
Resources: &runtimespec.LinuxResources{
Memory: &runtimespec.LinuxMemory{Limit: proto.Int64(54321)},
Memory: &runtimespec.LinuxMemory{
Limit: proto.Int64(54321),
Swap: proto.Int64(54321),
},
CPU: &runtimespec.LinuxCPU{
Shares: proto.Uint64(4444),
Quota: proto.Int64(5555),
Expand Down Expand Up @@ -151,7 +157,10 @@ func TestUpdateOCILinuxResource(t *testing.T) {
Process: &runtimespec.Process{OOMScoreAdj: oomscoreadj},
Linux: &runtimespec.Linux{
Resources: &runtimespec.LinuxResources{
Memory: &runtimespec.LinuxMemory{Limit: proto.Int64(54321)},
Memory: &runtimespec.LinuxMemory{
Limit: proto.Int64(54321),
Swap: proto.Int64(54321),
},
CPU: &runtimespec.LinuxCPU{
Shares: proto.Uint64(4444),
Quota: proto.Int64(5555),
Expand Down Expand Up @@ -197,7 +206,10 @@ func TestUpdateOCILinuxResource(t *testing.T) {
Process: &runtimespec.Process{OOMScoreAdj: oomscoreadj},
Linux: &runtimespec.Linux{
Resources: &runtimespec.LinuxResources{
Memory: &runtimespec.LinuxMemory{Limit: proto.Int64(54321)},
Memory: &runtimespec.LinuxMemory{
Limit: proto.Int64(54321),
Swap: proto.Int64(54321),
},
CPU: &runtimespec.LinuxCPU{
Shares: proto.Uint64(4444),
Quota: proto.Int64(5555),
Expand Down