Skip to content
Closed
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
5 changes: 3 additions & 2 deletions jdk/test/jdk/internal/platform/docker/MetricsCpuTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import jdk.internal.platform.Metrics;

public class MetricsCpuTester {
Expand Down Expand Up @@ -96,7 +97,7 @@ private static void testCpuSets(String cpuset) {
}

// Check to see if this metric is supported on this platform
if (effectiveCpus.length != 0) {
if (effectiveCpus != null) {
if (!Arrays.equals(ipCpuSet, effectiveCpus)) {
throw new RuntimeException("Effective Cpusets not equal, expected : "
+ Arrays.toString(ipCpuSet) + ", got : "
Expand Down Expand Up @@ -131,7 +132,7 @@ private static void testCpuSetMemNodes(String cpusetMems) {
}

// Check to see if this metric is supported on this platform
if (effectiveMems.length != 0) {
if (effectiveMems != null) {
if (!Arrays.equals(ipCpuSet, effectiveMems)) {
throw new RuntimeException("Effective mem nodes not equal, expected : "
+ Arrays.toString(ipCpuSet) + ", got : "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private static void testKernelMemoryLimit(String value) {
+ kmemlimit + "]");
}
} else {
throw new RuntimeException("oomKillFlag test not supported for cgroups v2");
throw new RuntimeException("kernel memory limit test not supported for cgroups v2");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.io.IOException;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;

Expand All @@ -41,9 +42,9 @@ interface CgroupMetricsTester {
public void testMemoryUsage() throws Exception;
public void testMisc();

public static long convertStringToLong(String strval, long overflowRetval) {
long retval = 0;
if (strval == null) return 0L;
public static long convertStringToLong(String strval, long initialVal, long overflowRetval) {
long retval = initialVal;
if (strval == null) return retval;

try {
retval = Long.parseLong(strval);
Expand Down Expand Up @@ -93,7 +94,7 @@ public static void warn(String controller, String metric, long oldVal, long test

public static Integer[] convertCpuSetsToArray(String cpusstr) {
if (cpusstr == null || EMPTY_STR.equals(cpusstr)) {
return new Integer[0];
return null;
}
// Parse range string in the format 1,2-6,7
Integer[] cpuSets = Stream.of(cpusstr.split(",")).flatMap(a -> {
Expand All @@ -108,4 +109,19 @@ public static Integer[] convertCpuSetsToArray(String cpusstr) {
return cpuSets;
}

public static Integer[] boxedArrayOrNull(int[] primitiveArray) {
if (primitiveArray == null) {
return null;
}
return Arrays.stream(primitiveArray).boxed().toArray(Integer[]::new);
}

public static Integer[] sortAllowNull(Integer[] array) {
if (array == null) {
return null;
}
Arrays.sort(array);
return array;
}

}
123 changes: 65 additions & 58 deletions jdk/test/lib/jdk/test/lib/containers/cgroup/MetricsTesterCgroupV1.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@
import java.util.stream.LongStream;
import java.util.stream.Stream;

import jdk.internal.platform.Metrics;
import jdk.internal.platform.CgroupSubsystem;
import jdk.internal.platform.CgroupV1Metrics;
import jdk.internal.platform.Metrics;
import jdk.test.lib.Asserts;

public class MetricsTesterCgroupV1 implements CgroupMetricsTester {

// Aliased for readability
private static final long RETVAL_UNAVAILABLE = CgroupSubsystem.LONG_RETVAL_UNLIMITED;
private static long unlimited_minimum = 0x7FFFFFFFFF000000L;
long startSysVal;
long startUserVal;
Expand Down Expand Up @@ -127,9 +131,6 @@ public void setup() {
startUserVal = metrics.getCpuUserUsage();
startUsage = metrics.getCpuUsage();
startPerCpu = metrics.getPerCpuUsage();
if (startPerCpu == null) {
startPerCpu = new long[0];
}

try {
Stream<String> lines = Files.lines(Paths.get("/proc/self/mountinfo"));
Expand Down Expand Up @@ -159,11 +160,11 @@ private static String getFileContents(Controller subSystem, String fileName) {

private static long getLongValueFromFile(Controller subSystem, String fileName) {
String data = getFileContents(subSystem, fileName);
return (data == null || data.isEmpty()) ? 0L : convertStringToLong(data);
return (data == null || data.isEmpty()) ? RETVAL_UNAVAILABLE : convertStringToLong(data);
}

private static long convertStringToLong(String strval) {
return CgroupMetricsTester.convertStringToLong(strval, Long.MAX_VALUE);
return CgroupMetricsTester.convertStringToLong(strval, RETVAL_UNAVAILABLE, Long.MAX_VALUE);
}

private static long getLongValueFromFile(Controller subSystem, String metric, String subMetric) {
Expand All @@ -175,12 +176,12 @@ private static long getLongValueFromFile(Controller subSystem, String metric, St
return convertStringToLong(strval);
}
}
return 0L;
return RETVAL_UNAVAILABLE;
}

private static double getDoubleValueFromFile(Controller subSystem, String fileName) {
String data = getFileContents(subSystem, fileName);
return data.isEmpty() ? 0.0 : Double.parseDouble(data);
return data == null || data.isEmpty() ? RETVAL_UNAVAILABLE : Double.parseDouble(data);
}

private static void fail(Controller system, String metric, long oldVal, long testVal) {
Expand All @@ -203,6 +204,13 @@ private static void warn(Controller system, String metric, long oldVal, long tes
CgroupMetricsTester.warn(system.value, metric, oldVal, testVal);
}

private Long[] boxedArrayOrNull(long[] primitiveArray) {
if (primitiveArray == null) {
return null;
}
return LongStream.of(primitiveArray).boxed().toArray(Long[]::new);
}

public void testMemorySubsystem() {
CgroupV1Metrics metrics = (CgroupV1Metrics)Metrics.systemMetrics();

Expand All @@ -215,7 +223,7 @@ public void testMemorySubsystem() {

oldVal = metrics.getMemoryLimit();
newVal = getLongValueFromFile(Controller.MEMORY, "memory.limit_in_bytes");
newVal = newVal > unlimited_minimum ? -1L : newVal;
newVal = newVal > unlimited_minimum ? CgroupSubsystem.LONG_RETVAL_UNLIMITED : newVal;
if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
fail(Controller.MEMORY, "memory.limit_in_bytes", oldVal, newVal);
}
Expand All @@ -241,7 +249,7 @@ public void testMemorySubsystem() {

oldVal = metrics.getKernelMemoryLimit();
newVal = getLongValueFromFile(Controller.MEMORY, "memory.kmem.limit_in_bytes");
newVal = newVal > unlimited_minimum ? -1L : newVal;
newVal = newVal > unlimited_minimum ? CgroupSubsystem.LONG_RETVAL_UNLIMITED : newVal;
if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
fail(Controller.MEMORY, "memory.kmem.limit_in_bytes", oldVal, newVal);
}
Expand All @@ -267,7 +275,7 @@ public void testMemorySubsystem() {

oldVal = metrics.getTcpMemoryLimit();
newVal = getLongValueFromFile(Controller.MEMORY, "memory.kmem.tcp.limit_in_bytes");
newVal = newVal > unlimited_minimum ? -1L : newVal;
newVal = newVal > unlimited_minimum ? CgroupSubsystem.LONG_RETVAL_UNLIMITED: newVal;
if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
fail(Controller.MEMORY, "memory.kmem.tcp.limit_in_bytes", oldVal, newVal);
}
Expand Down Expand Up @@ -295,7 +303,7 @@ public void testMemorySubsystem() {

oldVal = metrics.getMemoryAndSwapLimit();
newVal = getLongValueFromFile(Controller.MEMORY, "memory.memsw.limit_in_bytes");
newVal = newVal > unlimited_minimum ? -1L : newVal;
newVal = newVal > unlimited_minimum ? CgroupSubsystem.LONG_RETVAL_UNLIMITED : newVal;
if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
fail(Controller.MEMORY, "memory.memsw.limit_in_bytes", oldVal, newVal);
}
Expand All @@ -318,7 +326,7 @@ public void testMemorySubsystem() {

oldVal = metrics.getMemorySoftLimit();
newVal = getLongValueFromFile(Controller.MEMORY, "memory.soft_limit_in_bytes");
newVal = newVal > unlimited_minimum ? -1L : newVal;
newVal = newVal > unlimited_minimum ? CgroupSubsystem.LONG_RETVAL_UNLIMITED : newVal;
if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
fail(Controller.MEMORY, "memory.soft_limit_in_bytes", oldVal, newVal);
}
Expand All @@ -343,20 +351,22 @@ public void testCpuAccounting() {
}

String newValsStr = getFileContents(Controller.CPUACCT, "cpuacct.usage_percpu");
Long[] newVals = new Long[0];
Long[] newVals = null;
if (newValsStr != null) {
newVals = Stream.of(newValsStr
.split("\\s+"))
.map(Long::parseLong)
.toArray(Long[]::new);
}
long[] oldValsPrim = metrics.getPerCpuUsage();
Long[] oldVals = LongStream.of(oldValsPrim == null ? new long[0] : oldValsPrim)
.boxed().toArray(Long[]::new);
for (int i = 0; i < oldVals.length; i++) {
if (!CgroupMetricsTester.compareWithErrorMargin(oldVals[i], newVals[i])) {
warn(Controller.CPUACCT, "cpuacct.usage_percpu", oldVals[i], newVals[i]);
Long[] oldVals = boxedArrayOrNull(metrics.getPerCpuUsage());
if (oldVals != null) {
for (int i = 0; i < oldVals.length; i++) {
if (!CgroupMetricsTester.compareWithErrorMargin(oldVals[i], newVals[i])) {
warn(Controller.CPUACCT, "cpuacct.usage_percpu", oldVals[i], newVals[i]);
}
}
} else {
Asserts.assertNull(newVals, Controller.CPUACCT.value() + "cpuacct.usage_percpu not both null");
}

oldVal = metrics.getCpuUserUsage();
Expand Down Expand Up @@ -414,56 +424,50 @@ public void testCpuSchedulingMetrics() {

public void testCpuSets() {
CgroupV1Metrics metrics = (CgroupV1Metrics)Metrics.systemMetrics();
Integer[] oldVal = Arrays.stream(metrics.getCpuSetCpus()).boxed().toArray(Integer[]::new);
Arrays.sort(oldVal);
Integer[] oldVal = CgroupMetricsTester.boxedArrayOrNull(metrics.getCpuSetCpus());
oldVal = CgroupMetricsTester.sortAllowNull(oldVal);

String cpusstr = getFileContents(Controller.CPUSET, "cpuset.cpus");
// Parse range string in the format 1,2-6,7
Integer[] newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr);
Arrays.sort(newVal);
newVal = CgroupMetricsTester.sortAllowNull(newVal);
if (!Arrays.equals(oldVal, newVal)) {
fail(Controller.CPUSET, "cpuset.cpus", Arrays.toString(oldVal),
Arrays.toString(newVal));
}

int [] cpuSets = metrics.getEffectiveCpuSetCpus();

// Skip this test if this metric is not supported on this platform
if (cpuSets.length != 0) {
oldVal = Arrays.stream(cpuSets).boxed().toArray(Integer[]::new);
Arrays.sort(oldVal);
cpusstr = getFileContents(Controller.CPUSET, "cpuset.effective_cpus");
newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr);
Arrays.sort(newVal);
if (!Arrays.equals(oldVal, newVal)) {
fail(Controller.CPUSET, "cpuset.effective_cpus", Arrays.toString(oldVal),
Arrays.toString(newVal));
}
oldVal = CgroupMetricsTester.boxedArrayOrNull(cpuSets);
oldVal = CgroupMetricsTester.sortAllowNull(oldVal);
cpusstr = getFileContents(Controller.CPUSET, "cpuset.effective_cpus");
newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr);
newVal = CgroupMetricsTester.sortAllowNull(newVal);
if (!Arrays.equals(oldVal, newVal)) {
fail(Controller.CPUSET, "cpuset.effective_cpus", Arrays.toString(oldVal),
Arrays.toString(newVal));
}

oldVal = Arrays.stream(metrics.getCpuSetMems()).boxed().toArray(Integer[]::new);
Arrays.sort(oldVal);
oldVal = CgroupMetricsTester.boxedArrayOrNull(metrics.getCpuSetMems());
oldVal = CgroupMetricsTester.sortAllowNull(oldVal);
cpusstr = getFileContents(Controller.CPUSET, "cpuset.mems");
newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr);
Arrays.sort(newVal);
newVal = CgroupMetricsTester.sortAllowNull(newVal);
if (!Arrays.equals(oldVal, newVal)) {
fail(Controller.CPUSET, "cpuset.mems", Arrays.toString(oldVal),
Arrays.toString(newVal));
}

int [] cpuSetMems = metrics.getEffectiveCpuSetMems();

// Skip this test if this metric is not supported on this platform
if (cpuSetMems.length != 0) {
oldVal = Arrays.stream(cpuSetMems).boxed().toArray(Integer[]::new);
Arrays.sort(oldVal);
cpusstr = getFileContents(Controller.CPUSET, "cpuset.effective_mems");
newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr);
Arrays.sort(newVal);
if (!Arrays.equals(oldVal, newVal)) {
fail(Controller.CPUSET, "cpuset.effective_mems", Arrays.toString(oldVal),
Arrays.toString(newVal));
}
oldVal = CgroupMetricsTester.boxedArrayOrNull(cpuSetMems);
oldVal = CgroupMetricsTester.sortAllowNull(oldVal);
cpusstr = getFileContents(Controller.CPUSET, "cpuset.effective_mems");
newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr);
newVal = CgroupMetricsTester.sortAllowNull(newVal);
if (!Arrays.equals(oldVal, newVal)) {
fail(Controller.CPUSET, "cpuset.effective_mems", Arrays.toString(oldVal),
Arrays.toString(newVal));
}

double oldValue = metrics.getCpuSetMemoryPressure();
Expand Down Expand Up @@ -504,9 +508,6 @@ public void testCpuConsumption() throws IOException, InterruptedException {
long newUserVal = metrics.getCpuUserUsage();
long newUsage = metrics.getCpuUsage();
long[] newPerCpu = metrics.getPerCpuUsage();
if (newPerCpu == null) {
newPerCpu = new long[0];
}

// system/user CPU usage counters may be slowly increasing.
// allow for equal values for a pass
Expand All @@ -524,16 +525,22 @@ public void testCpuConsumption() throws IOException, InterruptedException {
fail(Controller.CPU, "getCpuUsage", newUsage, startUsage);
}

boolean success = false;
for (int i = 0; i < startPerCpu.length; i++) {
if (newPerCpu[i] > startPerCpu[i]) {
success = true;
break;
if (startPerCpu != null) {
boolean success = false;
for (int i = 0; i < startPerCpu.length; i++) {
if (newPerCpu[i] > startPerCpu[i]) {
success = true;
break;
}
}
if (!success) {
fail(Controller.CPU, "getPerCpuUsage", Arrays.toString(newPerCpu),
Arrays.toString(startPerCpu));
}
} else {
Asserts.assertNull(newPerCpu, Controller.CPU.value() + " getPerCpuUsage not both null");
}

if(!success) fail(Controller.CPU, "getPerCpuUsage", Arrays.toString(newPerCpu),
Arrays.toString(startPerCpu));
}

public void testMemoryUsage() throws Exception {
Expand Down
Loading