Skip to content

Commit 69975b9

Browse files
committed
cri: make swapping disabled with memory limit
OCI runtime spec defines memory.swap as 'limit of memory+Swap usage' so setting them to equal should disable the swap. Also, this change should make containerd behaviour same as other runtimes e.g 'cri-dockerd/dockershim' and won't be impacted when user turn on 'NodeSwap' (kubernetes/enhancements#2400) feature. Signed-off-by: Qasim Sarfraz <[email protected]>
1 parent 698622b commit 69975b9

4 files changed

Lines changed: 51 additions & 8 deletions

File tree

integration/container_update_resources_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ func TestUpdateContainerResources_MemoryLimit(t *testing.T) {
237237
pauseImage := images.Get(images.Pause)
238238
EnsureImageExists(t, pauseImage)
239239

240+
expectedSwapLimit := func(memoryLimit int64) *int64 {
241+
if cgroups.Mode() == cgroups.Unified {
242+
memoryLimit = 0
243+
}
244+
return &memoryLimit
245+
}
246+
240247
t.Log("Create a container with memory limit")
241248
cnConfig := ContainerConfig(
242249
"container",
@@ -254,6 +261,7 @@ func TestUpdateContainerResources_MemoryLimit(t *testing.T) {
254261
spec, err := container.Spec(context.Background())
255262
require.NoError(t, err)
256263
checkMemoryLimit(t, spec, 200*1024*1024)
264+
checkMemorySwapLimit(t, spec, expectedSwapLimit(200*1024*1024))
257265

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

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

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

289301
t.Log("Check memory limit in cgroup")
290302
memLimit = getCgroupMemoryLimitForTask(t, task)
291303
assert.Equal(t, uint64(800*1024*1024), memLimit)
304+
swapLimit = getCgroupSwapLimitForTask(t, task)
305+
assert.Equal(t, uint64(800*1024*1024), swapLimit)
306+
292307
}
293308

294309
func TestUpdateContainerResources_StatusUpdated(t *testing.T) {

pkg/cri/opts/spec_linux.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,10 @@ func WithResources(resources *runtime.LinuxContainerResources, tolerateMissingHu
448448
}
449449
if limit != 0 {
450450
s.Linux.Resources.Memory.Limit = &limit
451+
// swap/memory limit should be equal to prevent container from swapping by default
452+
if swapLimit == 0 {
453+
s.Linux.Resources.Memory.Swap = &limit
454+
}
451455
}
452456
if swapLimit != 0 {
453457
s.Linux.Resources.Memory.Swap = &swapLimit

pkg/cri/sbserver/container_update_resources_linux_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ func TestUpdateOCILinuxResource(t *testing.T) {
7070
Process: &runtimespec.Process{OOMScoreAdj: oomscoreadj},
7171
Linux: &runtimespec.Linux{
7272
Resources: &runtimespec.LinuxResources{
73-
Memory: &runtimespec.LinuxMemory{Limit: proto.Int64(54321)},
73+
Memory: &runtimespec.LinuxMemory{
74+
Limit: proto.Int64(54321),
75+
Swap: proto.Int64(54321),
76+
},
7477
CPU: &runtimespec.LinuxCPU{
7578
Shares: proto.Uint64(4444),
7679
Quota: proto.Int64(5555),
@@ -113,7 +116,10 @@ func TestUpdateOCILinuxResource(t *testing.T) {
113116
Process: &runtimespec.Process{OOMScoreAdj: oomscoreadj},
114117
Linux: &runtimespec.Linux{
115118
Resources: &runtimespec.LinuxResources{
116-
Memory: &runtimespec.LinuxMemory{Limit: proto.Int64(54321)},
119+
Memory: &runtimespec.LinuxMemory{
120+
Limit: proto.Int64(54321),
121+
Swap: proto.Int64(54321),
122+
},
117123
CPU: &runtimespec.LinuxCPU{
118124
Shares: proto.Uint64(4444),
119125
Quota: proto.Int64(5555),
@@ -151,7 +157,10 @@ func TestUpdateOCILinuxResource(t *testing.T) {
151157
Process: &runtimespec.Process{OOMScoreAdj: oomscoreadj},
152158
Linux: &runtimespec.Linux{
153159
Resources: &runtimespec.LinuxResources{
154-
Memory: &runtimespec.LinuxMemory{Limit: proto.Int64(54321)},
160+
Memory: &runtimespec.LinuxMemory{
161+
Limit: proto.Int64(54321),
162+
Swap: proto.Int64(54321),
163+
},
155164
CPU: &runtimespec.LinuxCPU{
156165
Shares: proto.Uint64(4444),
157166
Quota: proto.Int64(5555),
@@ -197,7 +206,10 @@ func TestUpdateOCILinuxResource(t *testing.T) {
197206
Process: &runtimespec.Process{OOMScoreAdj: oomscoreadj},
198207
Linux: &runtimespec.Linux{
199208
Resources: &runtimespec.LinuxResources{
200-
Memory: &runtimespec.LinuxMemory{Limit: proto.Int64(54321)},
209+
Memory: &runtimespec.LinuxMemory{
210+
Limit: proto.Int64(54321),
211+
Swap: proto.Int64(54321),
212+
},
201213
CPU: &runtimespec.LinuxCPU{
202214
Shares: proto.Uint64(4444),
203215
Quota: proto.Int64(5555),

pkg/cri/server/container_update_resources_linux_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ func TestUpdateOCILinuxResource(t *testing.T) {
7070
Process: &runtimespec.Process{OOMScoreAdj: oomscoreadj},
7171
Linux: &runtimespec.Linux{
7272
Resources: &runtimespec.LinuxResources{
73-
Memory: &runtimespec.LinuxMemory{Limit: proto.Int64(54321)},
73+
Memory: &runtimespec.LinuxMemory{
74+
Limit: proto.Int64(54321),
75+
Swap: proto.Int64(54321),
76+
},
7477
CPU: &runtimespec.LinuxCPU{
7578
Shares: proto.Uint64(4444),
7679
Quota: proto.Int64(5555),
@@ -113,7 +116,10 @@ func TestUpdateOCILinuxResource(t *testing.T) {
113116
Process: &runtimespec.Process{OOMScoreAdj: oomscoreadj},
114117
Linux: &runtimespec.Linux{
115118
Resources: &runtimespec.LinuxResources{
116-
Memory: &runtimespec.LinuxMemory{Limit: proto.Int64(54321)},
119+
Memory: &runtimespec.LinuxMemory{
120+
Limit: proto.Int64(54321),
121+
Swap: proto.Int64(54321),
122+
},
117123
CPU: &runtimespec.LinuxCPU{
118124
Shares: proto.Uint64(4444),
119125
Quota: proto.Int64(5555),
@@ -151,7 +157,10 @@ func TestUpdateOCILinuxResource(t *testing.T) {
151157
Process: &runtimespec.Process{OOMScoreAdj: oomscoreadj},
152158
Linux: &runtimespec.Linux{
153159
Resources: &runtimespec.LinuxResources{
154-
Memory: &runtimespec.LinuxMemory{Limit: proto.Int64(54321)},
160+
Memory: &runtimespec.LinuxMemory{
161+
Limit: proto.Int64(54321),
162+
Swap: proto.Int64(54321),
163+
},
155164
CPU: &runtimespec.LinuxCPU{
156165
Shares: proto.Uint64(4444),
157166
Quota: proto.Int64(5555),
@@ -197,7 +206,10 @@ func TestUpdateOCILinuxResource(t *testing.T) {
197206
Process: &runtimespec.Process{OOMScoreAdj: oomscoreadj},
198207
Linux: &runtimespec.Linux{
199208
Resources: &runtimespec.LinuxResources{
200-
Memory: &runtimespec.LinuxMemory{Limit: proto.Int64(54321)},
209+
Memory: &runtimespec.LinuxMemory{
210+
Limit: proto.Int64(54321),
211+
Swap: proto.Int64(54321),
212+
},
201213
CPU: &runtimespec.LinuxCPU{
202214
Shares: proto.Uint64(4444),
203215
Quota: proto.Int64(5555),

0 commit comments

Comments
 (0)