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

[8.0.1] Fix parsing cpu.max file in cgroup v2 implementation #24753

Merged
merged 1 commit into from
Dec 19, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Path getPath() {
public void setCpus(double cpus) throws IOException {
long period;
try (Scanner scanner = new Scanner(Files.newBufferedReader(path.resolve("cpu.max")))) {
period = scanner.skip(".*\\s").nextInt();
period = scanner.skip("\\S+\\s").nextInt();
}
long quota = Math.round(period * cpus);
String limit = String.format("%d %d", quota, period);
Expand All @@ -52,6 +52,10 @@ public void setCpus(double cpus) throws IOException {
@Override
public long getCpus() throws IOException {
try (Scanner scanner = new Scanner(Files.newBufferedReader(path.resolve("cpu.max")))) {
if (!scanner.hasNextLong()) {
// Not a number, assume no limit and all processors available
return Runtime.getRuntime().availableProcessors();
}
long quota = scanner.nextLong();
long period = scanner.nextLong();
return quota / period;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,32 @@ public void setCpuLimit_v2() throws IOException {
assertThat(Files.asCharSource(limit, UTF_8).read()).isEqualTo("500000 100000");
}

@Test
public void setCpuLimitNewLine_v2() throws IOException {
File limit = scratch.file("cgroup/cpu/cpu.max", "-1 100000\n").getPathFile();
Cpu cpu = new UnifiedCpu(scratch.path("cgroup/cpu").getPathFile().toPath());
cpu.setCpus(5);
assertThat(Files.asCharSource(limit, UTF_8).read()).isEqualTo("500000 100000");
}

@Test
public void getCpuLimit_v2() throws IOException {
scratch.file("cgroup/cpu/cpu.max", "6000 1000");
Cpu memory = new UnifiedCpu(scratch.path("cgroup/cpu").getPathFile().toPath());
assertThat(memory.getCpus()).isEqualTo(6);
Cpu cpu = new UnifiedCpu(scratch.path("cgroup/cpu").getPathFile().toPath());
assertThat(cpu.getCpus()).isEqualTo(6);
}

@Test
public void getCpuLimitNewLine_v2() throws IOException {
scratch.file("cgroup/cpu/cpu.max", "6000 1000\n");
Cpu cpu = new UnifiedCpu(scratch.path("cgroup/cpu").getPathFile().toPath());
assertThat(cpu.getCpus()).isEqualTo(6);
}

@Test
public void getCpuLimitMax_v2() throws IOException {
scratch.file("cgroup/cpu/cpu.max", "max 1000\n");
Cpu cpu = new UnifiedCpu(scratch.path("cgroup/cpu").getPathFile().toPath());
assertThat(cpu.getCpus()).isEqualTo(Runtime.getRuntime().availableProcessors());
}
}
Loading