Skip to content

Commit 5e2dbca

Browse files
bazel-iofzakaria
andauthored
[8.3.0] Set memory.swap.max to 0 when memory.max is set (#26160)
If a maximum memory is set then Bazel should disable swap otherwise the maximum memory is not really accounted for. fixes #26076 Closes #26077. PiperOrigin-RevId: 761949741 Change-Id: Iedba120416c325da7ced0dcfd00db3cdc412022d Commit ab57c89 Co-authored-by: Farid Zakaria <[email protected]>
1 parent c0fc7ba commit 5e2dbca

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

src/main/java/com/google/devtools/build/lib/sandbox/CgroupsInfoV2.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public CgroupsInfo createIndividualSpawnCgroup(String dirName, int memoryLimitMb
7575
Files.asCharSink(new File(spawnCgroupDir, "memory.oom.group"), UTF_8).write("1\n");
7676
Files.asCharSink(new File(spawnCgroupDir, "memory.max"), UTF_8)
7777
.write(Long.toString(memoryLimitMb * 1024L * 1024L));
78+
// Set swap to 0 so that it doesn't unexpecedly consume more than
79+
// the memory limit when swap is enabled.
80+
Files.asCharSink(new File(spawnCgroupDir, "memory.swap.max"), UTF_8)
81+
.write(Long.toString(0L));
7882
}
7983
} catch (Exception e) {
8084
return new InvalidCgroupsInfo(Type.SPAWN, getVersion(), e);

src/main/java/com/google/devtools/build/lib/sandbox/cgroups/controller/v2/UnifiedMemory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public Memory child(String name) throws IOException {
4040
@Override
4141
public void setMaxBytes(long bytes) throws IOException {
4242
Files.writeString(path.resolve("memory.max"), Long.toString(bytes));
43+
// Set swap to 0 so that it doesn't unexpecedly consume more than
44+
// the memory limit when swap is enabled.
45+
Files.writeString(path.resolve("memory.swap.max"), Long.toString(0L));
4346
}
4447

4548
@Override

src/test/java/com/google/devtools/build/lib/sandbox/CgroupsInfoTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ public void testCreateIndividualSpawnCgroup_withLimit_v2() throws IOException {
224224
.containsExactly("1");
225225
assertThat(Files.readLines(new File(blazeSpawnsPath + "/spawn_1.scope/memory.max"), UTF_8))
226226
.containsExactly("104857600");
227+
assertThat(Files.readLines(new File(blazeSpawnsPath + "/spawn_1.scope/memory.swap.max"), UTF_8))
228+
.containsExactly("0");
227229
}
228230

229231
@Test
@@ -244,6 +246,7 @@ public void testCreateIndividualSpawnCgroup_noLimit_v2() throws IOException {
244246
// written to.
245247
assertThat(new File(blazeSpawnsPath + "/spawn_1.scope/memory.oom.group").exists()).isFalse();
246248
assertThat(new File(blazeSpawnsPath + "/spawn_1.scope/memory.max").exists()).isFalse();
249+
assertThat(new File(blazeSpawnsPath + "/spawn_1.scope/memory.swap.max").exists()).isFalse();
247250
}
248251

249252
@Test

src/test/java/com/google/devtools/build/lib/sandbox/cgroups/MemoryTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ public void getMemoryUsage_v1() throws IOException {
5757
@Test
5858
public void setMemoryLimit_v2() throws IOException {
5959
File limit = scratch.file("cgroup/memory/memory.max", "0").getPathFile();
60+
File swap = scratch.file("cgroup/memory/memory.swap.max", "0").getPathFile();
6061
Memory memory = new UnifiedMemory(scratch.path("cgroup/memory").getPathFile().toPath());
6162
memory.setMaxBytes(1000);
6263
assertThat(Files.asCharSource(limit, UTF_8).read()).isEqualTo("1000");
64+
assertThat(Files.asCharSource(swap, UTF_8).read()).isEqualTo("0");
6365
}
6466

6567
@Test

0 commit comments

Comments
 (0)