package jdk.internal.platform.cgroupv1;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
import jdk.internal.platform.CgroupSubsystem;
import jdk.internal.platform.CgroupSubsystemController;
import jdk.internal.platform.CgroupUtil;
import jdk.internal.platform.CgroupV1Metrics;
public class CgroupV1Subsystem implements CgroupSubsystem, CgroupV1Metrics {
private CgroupV1MemorySubSystemController memory;
private CgroupV1SubsystemController cpu;
private CgroupV1SubsystemController cpuacct;
private CgroupV1SubsystemController cpuset;
private CgroupV1SubsystemController blkio;
private boolean activeSubSystems;
private static final CgroupV1Subsystem INSTANCE = initSubSystem();
private static final String PROVIDER_NAME = "cgroupv1";
private CgroupV1Subsystem() {
activeSubSystems = false;
}
public static CgroupV1Subsystem getInstance() {
return INSTANCE;
}
private static CgroupV1Subsystem initSubSystem() {
CgroupV1Subsystem subsystem = new CgroupV1Subsystem();
try (Stream<String> lines =
CgroupUtil.readFilePrivileged(Paths.get("/proc/self/mountinfo"))) {
lines.filter(line -> line.contains(" - cgroup "))
.map(line -> line.split(" "))
.forEach(entry -> createSubSystemController(subsystem, entry));
} catch (UncheckedIOException e) {
return null;
} catch (IOException e) {
return null;
}
try (Stream<String> lines =
CgroupUtil.readFilePrivileged(Paths.get("/proc/self/cgroup"))) {
lines.map(line -> line.split(":"))
.filter(line -> (line.length >= 3))
.forEach(line -> setSubSystemControllerPath(subsystem, line));
} catch (UncheckedIOException e) {
return null;
} catch (IOException e) {
return null;
}
if (subsystem.activeSubSystems()) {
return subsystem;
}
return null;
}
private static void createSubSystemController(CgroupV1Subsystem subsystem, String[] mountentry) {
if (mountentry.length < 5) return;
Path p = Paths.get(mountentry[4]);
String[] subsystemNames = p.getFileName().toString().split(",");
for (String subsystemName: subsystemNames) {
switch (subsystemName) {
case "memory":
subsystem.setMemorySubSystem(new CgroupV1MemorySubSystemController(mountentry[3], mountentry[4]));
break;
case "cpuset":
subsystem.setCpuSetController(new CgroupV1SubsystemController(mountentry[3], mountentry[4]));
break;
case "cpuacct":
subsystem.setCpuAcctController(new CgroupV1SubsystemController(mountentry[3], mountentry[4]));
break;
case "cpu":
subsystem.setCpuController(new CgroupV1SubsystemController(mountentry[3], mountentry[4]));
break;
case "blkio":
subsystem.setBlkIOController(new CgroupV1SubsystemController(mountentry[3], mountentry[4]));
break;
default:
break;
}
}
}
private static void setSubSystemControllerPath(CgroupV1Subsystem subsystem, String[] entry) {
String controllerName = entry[1];
String base = entry[2];
if (controllerName != null && base != null) {
for (String cName: controllerName.split(",")) {
switch (cName) {
case "memory":
setPath(subsystem, subsystem.memoryController(), base);
break;
case "cpuset":
setPath(subsystem, subsystem.cpuSetController(), base);
break;
case "cpu":
setPath(subsystem, subsystem.cpuController(), base);
break;
case "cpuacct":
setPath(subsystem, subsystem.cpuAcctController(), base);
break;
case "blkio":
setPath(subsystem, subsystem.blkIOController(), base);
break;
default:
break;
}
}
}
}
private static void setPath(CgroupV1Subsystem subsystem, CgroupV1SubsystemController controller, String base) {
if (controller != null) {
controller.setPath(base);
if (controller instanceof CgroupV1MemorySubSystemController) {
CgroupV1MemorySubSystemController memorySubSystem = (CgroupV1MemorySubSystemController)controller;
boolean isHierarchial = getHierarchical(memorySubSystem);
memorySubSystem.setHierarchical(isHierarchial);
boolean isSwapEnabled = getSwapEnabled(memorySubSystem);
memorySubSystem.setSwapEnabled(isSwapEnabled);
}
subsystem.setActiveSubSystems();
}
}
private static boolean getSwapEnabled(CgroupV1MemorySubSystemController controller) {
long retval = getLongValue(controller, "memory.memsw.limit_in_bytes");
return retval > 0;
}
private static boolean getHierarchical(CgroupV1MemorySubSystemController controller) {
long hierarchical = getLongValue(controller, "memory.use_hierarchy");
return hierarchical > 0;
}
private void setActiveSubSystems() {
activeSubSystems = true;
}
private boolean activeSubSystems() {
return activeSubSystems;
}
private void setMemorySubSystem(CgroupV1MemorySubSystemController memory) {
this.memory = memory;
}
private void setCpuController(CgroupV1SubsystemController cpu) {
this.cpu = cpu;
}
private void setCpuAcctController(CgroupV1SubsystemController cpuacct) {
this.cpuacct = cpuacct;
}
private void setCpuSetController(CgroupV1SubsystemController cpuset) {
this.cpuset = cpuset;
}
private void setBlkIOController(CgroupV1SubsystemController blkio) {
this.blkio = blkio;
}
private CgroupV1SubsystemController memoryController() {
return memory;
}
private CgroupV1SubsystemController cpuController() {
return cpu;
}
private CgroupV1SubsystemController cpuAcctController() {
return cpuacct;
}
private CgroupV1SubsystemController cpuSetController() {
return cpuset;
}
private CgroupV1SubsystemController blkIOController() {
return blkio;
}
private static long getLongValue(CgroupSubsystemController controller,
String parm) {
return CgroupSubsystemController.getLongValue(controller,
parm,
CgroupV1SubsystemController::convertStringToLong,
CgroupSubsystem.LONG_RETVAL_UNLIMITED);
}
public String getProvider() {
return PROVIDER_NAME;
}
public long getCpuUsage() {
return getLongValue(cpuacct, "cpuacct.usage");
}
public long[] getPerCpuUsage() {
String usagelist = CgroupSubsystemController.getStringValue(cpuacct, "cpuacct.usage_percpu");
if (usagelist == null) {
return null;
}
String list[] = usagelist.split(" ");
long percpu[] = new long[list.length];
for (int i = 0; i < list.length; i++) {
percpu[i] = Long.parseLong(list[i]);
}
return percpu;
}
public long getCpuUserUsage() {
return CgroupV1SubsystemController.getLongEntry(cpuacct, "cpuacct.stat", "user");
}
public long getCpuSystemUsage() {
return CgroupV1SubsystemController.getLongEntry(cpuacct, "cpuacct.stat", "system");
}
public long getCpuPeriod() {
return getLongValue(cpu, "cpu.cfs_period_us");
}
public long getCpuQuota() {
return getLongValue(cpu, "cpu.cfs_quota_us");
}
public long getCpuShares() {
long retval = getLongValue(cpu, "cpu.shares");
if (retval == 0 || retval == 1024)
return CgroupSubsystem.LONG_RETVAL_UNLIMITED;
else
return retval;
}
public long getCpuNumPeriods() {
return CgroupV1SubsystemController.getLongEntry(cpu, "cpu.stat", "nr_periods");
}
public long getCpuNumThrottled() {
return CgroupV1SubsystemController.getLongEntry(cpu, "cpu.stat", "nr_throttled");
}
public long getCpuThrottledTime() {
return CgroupV1SubsystemController.getLongEntry(cpu, "cpu.stat", "throttled_time");
}
public long getEffectiveCpuCount() {
return Runtime.getRuntime().availableProcessors();
}
public int[] getCpuSetCpus() {
return CgroupSubsystemController.stringRangeToIntArray(CgroupSubsystemController.getStringValue(cpuset, "cpuset.cpus"));
}
public int[] getEffectiveCpuSetCpus() {
return CgroupSubsystemController.stringRangeToIntArray(CgroupSubsystemController.getStringValue(cpuset, "cpuset.effective_cpus"));
}
public int[] getCpuSetMems() {
return CgroupSubsystemController.stringRangeToIntArray(CgroupSubsystemController.getStringValue(cpuset, "cpuset.mems"));
}
public int[] getEffectiveCpuSetMems() {
return CgroupSubsystemController.stringRangeToIntArray(CgroupSubsystemController.getStringValue(cpuset, "cpuset.effective_mems"));
}
public double getCpuSetMemoryPressure() {
return CgroupV1SubsystemController.getDoubleValue(cpuset, "cpuset.memory_pressure");
}
public Boolean isCpuSetMemoryPressureEnabled() {
long val = getLongValue(cpuset, "cpuset.memory_pressure_enabled");
return (val == 1);
}
public long getMemoryFailCount() {
return getLongValue(memory, "memory.failcnt");
}
public long getMemoryLimit() {
long retval = getLongValue(memory, "memory.limit_in_bytes");
if (retval > CgroupV1SubsystemController.UNLIMITED_MIN) {
if (memory.isHierarchical()) {
String match = "hierarchical_memory_limit";
retval = CgroupV1SubsystemController.getLongValueMatchingLine(memory,
"memory.stat",
match);
}
}
return CgroupV1SubsystemController.longValOrUnlimited(retval);
}
public long getMemoryMaxUsage() {
return getLongValue(memory, "memory.max_usage_in_bytes");
}
public long getMemoryUsage() {
return getLongValue(memory, "memory.usage_in_bytes");
}
public long getKernelMemoryFailCount() {
return getLongValue(memory, "memory.kmem.failcnt");
}
public long getKernelMemoryLimit() {
return CgroupV1SubsystemController.longValOrUnlimited(getLongValue(memory, "memory.kmem.limit_in_bytes"));
}
public long getKernelMemoryMaxUsage() {
return getLongValue(memory, "memory.kmem.max_usage_in_bytes");
}
public long getKernelMemoryUsage() {
return getLongValue(memory, "memory.kmem.usage_in_bytes");
}
public long getTcpMemoryFailCount() {
return getLongValue(memory, "memory.kmem.tcp.failcnt");
}
public long getTcpMemoryLimit() {
return CgroupV1SubsystemController.longValOrUnlimited(getLongValue(memory, "memory.kmem.tcp.limit_in_bytes"));
}
public long getTcpMemoryMaxUsage() {
return getLongValue(memory, "memory.kmem.tcp.max_usage_in_bytes");
}
public long getTcpMemoryUsage() {
return getLongValue(memory, "memory.kmem.tcp.usage_in_bytes");
}
public long getMemoryAndSwapFailCount() {
if (memory != null && !memory.isSwapEnabled()) {
return getMemoryFailCount();
}
return getLongValue(memory, "memory.memsw.failcnt");
}
public long getMemoryAndSwapLimit() {
if (memory != null && !memory.isSwapEnabled()) {
return getMemoryLimit();
}
long retval = getLongValue(memory, "memory.memsw.limit_in_bytes");
if (retval > CgroupV1SubsystemController.UNLIMITED_MIN) {
if (memory.isHierarchical()) {
String match = "hierarchical_memsw_limit";
retval = CgroupV1SubsystemController.getLongValueMatchingLine(memory,
"memory.stat",
match);
}
}
return CgroupV1SubsystemController.longValOrUnlimited(retval);
}
public long getMemoryAndSwapMaxUsage() {
if (memory != null && !memory.isSwapEnabled()) {
return getMemoryMaxUsage();
}
return getLongValue(memory, "memory.memsw.max_usage_in_bytes");
}
public long getMemoryAndSwapUsage() {
if (memory != null && !memory.isSwapEnabled()) {
return getMemoryUsage();
}
return getLongValue(memory, "memory.memsw.usage_in_bytes");
}
public Boolean isMemoryOOMKillEnabled() {
long val = CgroupV1SubsystemController.getLongEntry(memory, "memory.oom_control", "oom_kill_disable");
return (val == 0);
}
public long getMemorySoftLimit() {
return CgroupV1SubsystemController.longValOrUnlimited(getLongValue(memory, "memory.soft_limit_in_bytes"));
}
public long getBlkIOServiceCount() {
return CgroupV1SubsystemController.getLongEntry(blkio, "blkio.throttle.io_service_bytes", "Total");
}
public long getBlkIOServiced() {
return CgroupV1SubsystemController.getLongEntry(blkio, "blkio.throttle.io_serviced", "Total");
}
}