From 7c07274310985dcdce2bbed923e01bd64b54bc47 Mon Sep 17 00:00:00 2001 From: Severin Gehwolf Date: Thu, 9 Jun 2022 12:00:32 +0200 Subject: [PATCH] Backport 2d8c6490540e3ccea23b81129b2e4073915071e0 --- .../platform/CgroupSubsystemFactory.java | 3 ++ .../cgroup/TestCgroupSubsystemFactory.java | 34 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/java.base/linux/classes/jdk/internal/platform/CgroupSubsystemFactory.java b/src/java.base/linux/classes/jdk/internal/platform/CgroupSubsystemFactory.java index 577e5de31e9..6a88f57d4d9 100644 --- a/src/java.base/linux/classes/jdk/internal/platform/CgroupSubsystemFactory.java +++ b/src/java.base/linux/classes/jdk/internal/platform/CgroupSubsystemFactory.java @@ -88,7 +88,10 @@ static CgroupMetrics create() { } catch (UncheckedIOException e) { return null; } + return create(optResult); + } + public static CgroupMetrics create(Optional optResult) { if (optResult.isEmpty()) { return null; } diff --git a/test/jdk/jdk/internal/platform/cgroup/TestCgroupSubsystemFactory.java b/test/jdk/jdk/internal/platform/cgroup/TestCgroupSubsystemFactory.java index 0fd34dffd74..76dc8babcd5 100644 --- a/test/jdk/jdk/internal/platform/cgroup/TestCgroupSubsystemFactory.java +++ b/test/jdk/jdk/internal/platform/cgroup/TestCgroupSubsystemFactory.java @@ -25,6 +25,7 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -50,7 +51,7 @@ /* * @test - * @bug 8287107 + * @bug 8287107 8287073 * @key cgroups * @requires os.family == "linux" * @modules java.base/jdk.internal.platform @@ -66,6 +67,7 @@ public class TestCgroupSubsystemFactory { private Path cgroupv1CgInfoZeroHierarchy; private Path cgroupv1MntInfoZeroHierarchy; private Path cgroupv2CgInfoZeroHierarchy; + private Path cgroupv2CgInfoZeroMinimal; private Path cgroupv2MntInfoZeroHierarchy; private Path cgroupv1CgInfoNonZeroHierarchy; private Path cgroupv1MntInfoNonZeroHierarchy; @@ -127,6 +129,9 @@ public class TestCgroupSubsystemFactory { "net_cls 0 1 1\n" + "blkio 0 1 1\n" + "perf_event 0 1 1 "; + private String cgroupsZeroHierarchyMinimal = + "#subsys_name hierarchy num_cgroups enabled\n" + + "cpu 0 1 1\n"; private String mntInfoHybrid = "30 23 0:26 / /sys/fs/cgroup ro,nosuid,nodev,noexec shared:4 - tmpfs tmpfs ro,seclabel,mode=755\n" + "31 30 0:27 / /sys/fs/cgroup/unified rw,nosuid,nodev,noexec,relatime shared:5 - cgroup2 none rw,seclabel,nsdelegate\n" + @@ -306,6 +311,9 @@ public void setup() { cgroupv1MountInfoCgroupsOnlyCPUCtrl = Paths.get(existingDirectory.toString(), "self_mountinfo_cpu_only_controller"); Files.writeString(cgroupv1MountInfoCgroupsOnlyCPUCtrl, mntInfoCpuOnly); + cgroupv2CgInfoZeroMinimal = Paths.get(existingDirectory.toString(), "cgv2_proc_cgroups_minimal"); + Files.writeString(cgroupv2CgInfoZeroMinimal, cgroupsZeroHierarchyMinimal); + cgroupv2CgInfoNoZeroHierarchyOnlyFreezer = Paths.get(existingDirectory.toString(), "cgroups_cgv2_non_zero_only_freezer"); Files.writeString(cgroupv2CgInfoNoZeroHierarchyOnlyFreezer, cgroupsNonZeroHierarchyOnlyFreezer); @@ -480,6 +488,30 @@ public void testZeroHierarchyCgroupsV2() throws IOException { assertEquals("/sys/fs/cgroup", cpuInfo.getMountPoint()); } + /* + * On some systems the memory controller might not show up in /proc/cgroups + * which may provoke a NPE on instantiation. See bug 8287073. + */ + @Test + public void testZeroHierarchyCgroupsV2Minimal() throws IOException { + String cgroups = cgroupv2CgInfoZeroMinimal.toString(); + String mountInfo = cgroupv2MntInfoZeroHierarchy.toString(); + String selfCgroup = cgroupv2SelfCgroup.toString(); + Optional result = CgroupSubsystemFactory.determineType(mountInfo, cgroups, selfCgroup); + + assertTrue("Expected non-empty cgroup result", result.isPresent()); + CgroupTypeResult res = result.get(); + + assertTrue("zero hierarchy ids with mounted controllers expected cgroups v2", res.isCgroupV2()); + assertNull("Only cpu controller present", res.getInfos().get("memory")); + try { + CgroupSubsystemFactory.create(result); + // pass + } catch (NullPointerException e) { + fail("Missing memory controller should not cause any NPE"); + } + } + @Test(expected = IOException.class) public void mountInfoFileNotFound() throws IOException { String cgroups = cgroupv1CgInfoZeroHierarchy.toString(); // any existing file