package jdk.internal.platform.cgroupv2;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import jdk.internal.platform.CgroupInfo;
import jdk.internal.platform.CgroupSubsystem;
import jdk.internal.platform.CgroupSubsystemController;
import jdk.internal.platform.CgroupUtil;
public class CgroupV2Subsystem implements CgroupSubsystem {
private static volatile CgroupV2Subsystem INSTANCE;
private static final long[] LONG_ARRAY_NOT_SUPPORTED = null;
private static final int[] INT_ARRAY_UNAVAILABLE = null;
private final CgroupSubsystemController unified;
private static final String PROVIDER_NAME = "cgroupv2";
private static final int PER_CPU_SHARES = 1024;
private static final String MAX_VAL = "max";
private static final Object EMPTY_STR = "";
private static final long NO_SWAP = 0;
private CgroupV2Subsystem(CgroupSubsystemController unified) {
this.unified = unified;
}
private long getLongVal(String file, long defaultValue) {
return CgroupSubsystemController.getLongValue(unified,
file,
CgroupV2SubsystemController::convertStringToLong,
defaultValue);
}
private long getLongVal(String file) {
return getLongVal(file, CgroupSubsystem.LONG_RETVAL_UNLIMITED);
}
public static CgroupSubsystem getInstance(CgroupInfo anyController) {
if (INSTANCE == null) {
CgroupSubsystemController unified = new CgroupV2SubsystemController(
anyController.getMountPoint(),
anyController.getCgroupPath());
CgroupV2Subsystem tmpCgroupSystem = new CgroupV2Subsystem(unified);
synchronized (CgroupV2Subsystem.class) {
if (INSTANCE == null) {
INSTANCE = tmpCgroupSystem;
}
}
}
return INSTANCE;
}
@Override
public String getProvider() {
return PROVIDER_NAME;
}
@Override
public long getCpuUsage() {
long micros = CgroupV2SubsystemController.getLongEntry(unified, "cpu.stat", "usage_usec");
if (micros < 0) {
return micros;
}
return TimeUnit.MICROSECONDS.toNanos(micros);
}
@Override
public long[] getPerCpuUsage() {
return LONG_ARRAY_NOT_SUPPORTED;
}
@Override
public long getCpuUserUsage() {
long micros = CgroupV2SubsystemController.getLongEntry(unified, "cpu.stat", "user_usec");
if (micros < 0) {
return micros;
}
return TimeUnit.MICROSECONDS.toNanos(micros);
}
@Override
public long getCpuSystemUsage() {
long micros = CgroupV2SubsystemController.getLongEntry(unified, "cpu.stat", "system_usec");
if (micros < 0) {
return micros;
}
return TimeUnit.MICROSECONDS.toNanos(micros);
}
@Override
public long getCpuPeriod() {
return getFromCpuMax(1 );
}
@Override
public long getCpuQuota() {
return getFromCpuMax(0 );
}
private long getFromCpuMax(int tokenIdx) {
String cpuMaxRaw = CgroupSubsystemController.getStringValue(unified, "cpu.max");
if (cpuMaxRaw == null) {
return CgroupSubsystem.LONG_RETVAL_UNLIMITED;
}
String[] tokens = cpuMaxRaw.split("\\s+");
if (tokens.length != 2) {
return CgroupSubsystem.LONG_RETVAL_UNLIMITED;
}
String quota = tokens[tokenIdx];
return limitFromString(quota);
}
private long limitFromString(String strVal) {
if (strVal == null || MAX_VAL.equals(strVal)) {
return CgroupSubsystem.LONG_RETVAL_UNLIMITED;
}
return Long.parseLong(strVal);
}
@Override
public long getCpuShares() {
long sharesRaw = getLongVal("cpu.weight");
if (sharesRaw == 100 || sharesRaw <= 0) {
return CgroupSubsystem.LONG_RETVAL_UNLIMITED;
}
int shares = (int)sharesRaw;
int x = 262142 * shares - 1;
double frac = x/9999.0;
x = ((int)frac) + 2;
if ( x <= PER_CPU_SHARES ) {
return PER_CPU_SHARES;
}
int f = x/PER_CPU_SHARES;
int lower_multiple = f * PER_CPU_SHARES;
int upper_multiple = (f + 1) * PER_CPU_SHARES;
int distance_lower = Math.max(lower_multiple, x) - Math.min(lower_multiple, x);
int distance_upper = Math.max(upper_multiple, x) - Math.min(upper_multiple, x);
x = distance_lower <= distance_upper ? lower_multiple : upper_multiple;
return x;
}
@Override
public long getCpuNumPeriods() {
return CgroupV2SubsystemController.getLongEntry(unified, "cpu.stat", "nr_periods");
}
@Override
public long getCpuNumThrottled() {
return CgroupV2SubsystemController.getLongEntry(unified, "cpu.stat", "nr_throttled");
}
@Override
public long getCpuThrottledTime() {
long micros = CgroupV2SubsystemController.getLongEntry(unified, "cpu.stat", "throttled_usec");
if (micros < 0) {
return micros;
}
return TimeUnit.MICROSECONDS.toNanos(micros);
}
@Override
public long getEffectiveCpuCount() {
return Runtime.getRuntime().availableProcessors();
}
@Override
public int[] getCpuSetCpus() {
String cpuSetVal = CgroupSubsystemController.getStringValue(unified, "cpuset.cpus");
return getCpuSet(cpuSetVal);
}
@Override
public int[] getEffectiveCpuSetCpus() {
String effCpuSetVal = CgroupSubsystemController.getStringValue(unified, "cpuset.cpus.effective");
return getCpuSet(effCpuSetVal);
}
@Override
public int[] getCpuSetMems() {
String cpuSetMems = CgroupSubsystemController.getStringValue(unified, "cpuset.mems");
return getCpuSet(cpuSetMems);
}
@Override
public int[] getEffectiveCpuSetMems() {
String effCpuSetMems = CgroupSubsystemController.getStringValue(unified, "cpuset.mems.effective");
return getCpuSet(effCpuSetMems);
}
private int[] getCpuSet(String cpuSetVal) {
if (cpuSetVal == null || EMPTY_STR.equals(cpuSetVal)) {
return INT_ARRAY_UNAVAILABLE;
}
return CgroupSubsystemController.stringRangeToIntArray(cpuSetVal);
}
@Override
public long getMemoryFailCount() {
return CgroupV2SubsystemController.getLongEntry(unified, "memory.events", "max");
}
@Override
public long getMemoryLimit() {
String strVal = CgroupSubsystemController.getStringValue(unified, "memory.max");
return limitFromString(strVal);
}
@Override
public long getMemoryUsage() {
return getLongVal("memory.current");
}
@Override
public long getTcpMemoryUsage() {
return CgroupV2SubsystemController.getLongEntry(unified, "memory.stat", "sock");
}
@Override
public long getMemoryAndSwapLimit() {
String strVal = CgroupSubsystemController.getStringValue(unified, "memory.swap.max");
if (strVal == null) {
return getMemoryLimit();
}
long swapLimit = limitFromString(strVal);
if (swapLimit >= 0) {
long memoryLimit = getMemoryLimit();
assert memoryLimit >= 0;
return memoryLimit + swapLimit;
}
return swapLimit;
}
@Override
public long getMemoryAndSwapUsage() {
long memoryUsage = getMemoryUsage();
if (memoryUsage >= 0) {
long swapUsage = getLongVal("memory.swap.current", NO_SWAP);
return memoryUsage + swapUsage;
}
return memoryUsage;
}
@Override
public long getMemorySoftLimit() {
String softLimitStr = CgroupSubsystemController.getStringValue(unified, "memory.low");
return limitFromString(softLimitStr);
}
@Override
public long getBlkIOServiceCount() {
return sumTokensIOStat(CgroupV2Subsystem::lineToRandWIOs);
}
@Override
public long getBlkIOServiced() {
return sumTokensIOStat(CgroupV2Subsystem::lineToRBytesAndWBytesIO);
}
private long sumTokensIOStat(Function<String, Long> mapFunc) {
try {
return CgroupUtil.readFilePrivileged(Paths.get(unified.path(), "io.stat"))
.map(mapFunc)
.collect(Collectors.summingLong(e -> e));
} catch (UncheckedIOException e) {
return CgroupSubsystem.LONG_RETVAL_UNLIMITED;
} catch (IOException e) {
return CgroupSubsystem.LONG_RETVAL_UNLIMITED;
}
}
private static String[] getRWIOMatchTokenNames() {
return new String[] { "rios", "wios" };
}
private static String[] getRWBytesIOMatchTokenNames() {
return new String[] { "rbytes", "wbytes" };
}
public static Long lineToRandWIOs(String line) {
String[] matchNames = getRWIOMatchTokenNames();
return ioStatLineToLong(line, matchNames);
}
public static Long lineToRBytesAndWBytesIO(String line) {
String[] matchNames = getRWBytesIOMatchTokenNames();
return ioStatLineToLong(line, matchNames);
}
private static Long ioStatLineToLong(String line, String[] matchNames) {
if (line == null || EMPTY_STR.equals(line)) {
return Long.valueOf(0);
}
String[] tokens = line.split("\\s+");
long retval = 0;
for (String t: tokens) {
String[] valKeys = t.split("=");
if (valKeys.length != 2) {
continue;
}
for (String match: matchNames) {
if (match.equals(valKeys[0])) {
retval += longOrZero(valKeys[1]);
}
}
}
return Long.valueOf(retval);
}
private static long longOrZero(String val) {
long lVal = 0;
try {
lVal = Long.parseLong(val);
} catch (NumberFormatException e) {
}
return lVal;
}
}