package jdk.internal.platform;
import java.io.IOException;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import jdk.internal.platform.cgroupv1.CgroupV1Subsystem;
import jdk.internal.platform.cgroupv2.CgroupV2Subsystem;
public class CgroupSubsystemFactory {
private static final String CPU_CTRL = "cpu";
private static final String CPUACCT_CTRL = "cpuacct";
private static final String CPUSET_CTRL = "cpuset";
private static final String BLKIO_CTRL = "blkio";
private static final String MEMORY_CTRL = "memory";
static CgroupMetrics create() {
Optional<CgroupTypeResult> optResult = null;
try {
optResult = determineType("/proc/self/mountinfo", "/proc/cgroups");
} catch (IOException e) {
return null;
}
if (optResult.isEmpty()) {
return null;
}
CgroupTypeResult result = optResult.get();
if (!result.isAnyControllersEnabled()) {
return null;
}
if (result.isAnyCgroupV1Controllers() && result.isAnyCgroupV2Controllers()) {
Logger logger = System.getLogger("jdk.internal.platform");
logger.log(Level.DEBUG, "Mixed cgroupv1 and cgroupv2 not supported. Metrics disabled.");
return null;
}
if (result.isCgroupV2()) {
CgroupSubsystem subsystem = CgroupV2Subsystem.getInstance();
return subsystem != null ? new CgroupMetrics(subsystem) : null;
} else {
CgroupV1Subsystem subsystem = CgroupV1Subsystem.getInstance();
return subsystem != null ? new CgroupV1MetricsImpl(subsystem) : null;
}
}
public static Optional<CgroupTypeResult> determineType(String mountInfo, String cgroups) throws IOException {
Map<String, CgroupInfo> infos = new HashMap<>();
List<String> lines = CgroupUtil.readAllLinesPrivileged(Paths.get(cgroups));
for (String line : lines) {
if (line.startsWith("#")) {
continue;
}
CgroupInfo info = CgroupInfo.fromCgroupsLine(line);
switch (info.getName()) {
case CPU_CTRL: infos.put(CPU_CTRL, info); break;
case CPUACCT_CTRL: infos.put(CPUACCT_CTRL, info); break;
case CPUSET_CTRL: infos.put(CPUSET_CTRL, info); break;
case MEMORY_CTRL: infos.put(MEMORY_CTRL, info); break;
case BLKIO_CTRL: infos.put(BLKIO_CTRL, info); break;
}
}
boolean isCgroupsV2 = true;
boolean anyControllersEnabled = false;
boolean anyCgroupsV2Controller = false;
boolean anyCgroupsV1Controller = false;
for (CgroupInfo info: infos.values()) {
anyCgroupsV1Controller = anyCgroupsV1Controller || info.getHierarchyId() != 0;
anyCgroupsV2Controller = anyCgroupsV2Controller || info.getHierarchyId() == 0;
isCgroupsV2 = isCgroupsV2 && info.getHierarchyId() == 0;
anyControllersEnabled = anyControllersEnabled || info.isEnabled();
}
try (Stream<String> mntInfo = CgroupUtil.readFilePrivileged(Paths.get(mountInfo))) {
boolean anyCgroupMounted = mntInfo.anyMatch(line -> line.contains("cgroup"));
if (!anyCgroupMounted && isCgroupsV2) {
return Optional.empty();
}
}
CgroupTypeResult result = new CgroupTypeResult(isCgroupsV2, anyControllersEnabled, anyCgroupsV2Controller, anyCgroupsV1Controller);
return Optional.of(result);
}
public static final class CgroupTypeResult {
private final boolean isCgroupV2;
private final boolean anyControllersEnabled;
private final boolean anyCgroupV2Controllers;
private final boolean anyCgroupV1Controllers;
private CgroupTypeResult(boolean isCgroupV2,
boolean anyControllersEnabled,
boolean anyCgroupV2Controllers,
boolean anyCgroupV1Controllers) {
this.isCgroupV2 = isCgroupV2;
this.anyControllersEnabled = anyControllersEnabled;
this.anyCgroupV1Controllers = anyCgroupV1Controllers;
this.anyCgroupV2Controllers = anyCgroupV2Controllers;
}
public boolean isCgroupV2() {
return isCgroupV2;
}
public boolean isAnyControllersEnabled() {
return anyControllersEnabled;
}
public boolean isAnyCgroupV2Controllers() {
return anyCgroupV2Controllers;
}
public boolean isAnyCgroupV1Controllers() {
return anyCgroupV1Controllers;
}
}
}