< prev index next >

test/lib/jdk/test/lib/containers/cgroup/MetricsTesterCgroupV2.java

Print this page
@  rev 58200 : 8240189: [TESTBUG] Some cgroup tests are failing after JDK-8231111
|  Reviewed-by: mbaesken
~

@@ -30,16 +30,16 @@
 import java.util.Arrays;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
-import jdk.internal.platform.CgroupSubsystem;
 import jdk.internal.platform.Metrics;
 
 public class MetricsTesterCgroupV2 implements CgroupMetricsTester {
 
     private static final long UNLIMITED = -1;
+    private static final long NOT_AVAILABLE = -1;
     private static final UnifiedController UNIFIED = new UnifiedController();
     private static final String MAX = "max";
     private static final int PER_CPU_SHARES = 1024;
 
     private final long startSysVal;

@@ -123,11 +123,11 @@
             String strVal = Files.lines(filePath).filter(l -> l.startsWith(metric)).collect(Collectors.joining());
             String[] keyValues = strVal.split("\\s+");
             String value = keyValues[1];
             return convertStringToLong(value);
         } catch (IOException e) {
-            return 0;
+            return NOT_AVAILABLE;
         }
     }
 
     private String getStringVal(String file) {
         Path filePath = Paths.get(UNIFIED.getPath(), file);

@@ -150,11 +150,11 @@
         CgroupMetricsTester.warn(UnifiedController.NAME, metric, oldVal, newVal);
     }
 
     private long getCpuShares(String file) {
         long rawVal = getLongValueFromFile(file);
-        if (rawVal == 0 || rawVal == 100) {
+        if (rawVal == NOT_AVAILABLE || rawVal == 100) {
             return UNLIMITED;
         }
         int shares = (int)rawVal;
         // CPU shares (OCI) value needs to get translated into
         // a proper Cgroups v2 value. See:

@@ -198,11 +198,18 @@
         }
         return convertStringToLong(val);
     }
 
     private long convertStringToLong(String val) {
-        return CgroupMetricsTester.convertStringToLong(val, UNLIMITED);
+        return CgroupMetricsTester.convertStringToLong(val, NOT_AVAILABLE, UNLIMITED);
+    }
+
+    private long nanosOrUnlimited(long micros) {
+        if (micros < 0) {
+            return UNLIMITED;
+        }
+        return TimeUnit.MICROSECONDS.toNanos(micros);
     }
 
     @Override
     public void testMemorySubsystem() {
         Metrics metrics = Metrics.systemMetrics();

@@ -254,24 +261,24 @@
 
     @Override
     public void testCpuAccounting() {
         Metrics metrics = Metrics.systemMetrics();
         long oldVal = metrics.getCpuUsage();
-        long newVal = TimeUnit.MICROSECONDS.toNanos(getLongValueEntryFromFile("cpu.stat", "usage_usec"));
+        long newVal = nanosOrUnlimited(getLongValueEntryFromFile("cpu.stat", "usage_usec"));
 
         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
             warn("cpu.stat[usage_usec]", oldVal, newVal);
         }
 
         oldVal = metrics.getCpuUserUsage();
-        newVal = TimeUnit.MICROSECONDS.toNanos(getLongValueEntryFromFile("cpu.stat", "user_usec"));
+        newVal = nanosOrUnlimited(getLongValueEntryFromFile("cpu.stat", "user_usec"));
         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
             warn("cpu.stat[user_usec]", oldVal, newVal);
         }
 
         oldVal = metrics.getCpuSystemUsage();
-        newVal = TimeUnit.MICROSECONDS.toNanos(getLongValueEntryFromFile("cpu.stat", "system_usec"));
+        newVal = nanosOrUnlimited(getLongValueEntryFromFile("cpu.stat", "system_usec"));
         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
             warn("cpu.stat[system_usec]", oldVal, newVal);
         }
     }
 

@@ -307,75 +314,62 @@
         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
             fail("cpu.stat[nr_throttled]", oldVal, newVal);
         }
 
         oldVal = metrics.getCpuThrottledTime();
-        newVal = TimeUnit.MICROSECONDS.toNanos(getLongValueEntryFromFile("cpu.stat", "throttled_usec"));
+        newVal = nanosOrUnlimited(getLongValueEntryFromFile("cpu.stat", "throttled_usec"));
         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
             fail("cpu.stat[throttled_usec]", oldVal, newVal);
         }
     }
 
     @Override
     public void testCpuSets() {
         Metrics metrics = Metrics.systemMetrics();
-        int[] cpus = mapNullToEmpty(metrics.getCpuSetCpus());
-        Integer[] oldVal = Arrays.stream(cpus).boxed().toArray(Integer[]::new);
-        Arrays.sort(oldVal);
+        Integer[] oldVal = CgroupMetricsTester.boxedArrayOrNull(metrics.getCpuSetCpus());
+        oldVal = CgroupMetricsTester.sortAllowNull(oldVal);
 
         String cpusstr = getStringVal("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.compare(oldVal, newVal) != 0) {
             fail("cpuset.cpus", Arrays.toString(oldVal),
                                 Arrays.toString(newVal));
         }
 
-        cpus = mapNullToEmpty(metrics.getEffectiveCpuSetCpus());
-        oldVal = Arrays.stream(cpus).boxed().toArray(Integer[]::new);
-        Arrays.sort(oldVal);
+        oldVal = CgroupMetricsTester.boxedArrayOrNull(metrics.getEffectiveCpuSetCpus());
+        oldVal = CgroupMetricsTester.sortAllowNull(oldVal);
         cpusstr = getStringVal("cpuset.cpus.effective");
         newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr);
-        Arrays.sort(newVal);
+        newVal = CgroupMetricsTester.sortAllowNull(newVal);
         if (Arrays.compare(oldVal, newVal) != 0) {
             fail("cpuset.cpus.effective", Arrays.toString(oldVal),
                                           Arrays.toString(newVal));
         }
 
-        cpus = mapNullToEmpty(metrics.getCpuSetMems());
-        oldVal = Arrays.stream(cpus).boxed().toArray(Integer[]::new);
-        Arrays.sort(oldVal);
+        oldVal = CgroupMetricsTester.boxedArrayOrNull(metrics.getCpuSetMems());
+        oldVal = CgroupMetricsTester.sortAllowNull(oldVal);
         cpusstr = getStringVal("cpuset.mems");
         newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr);
-        Arrays.sort(newVal);
+        newVal = CgroupMetricsTester.sortAllowNull(newVal);
         if (Arrays.compare(oldVal, newVal) != 0) {
             fail("cpuset.mems", Arrays.toString(oldVal),
                                 Arrays.toString(newVal));
         }
 
-        cpus = mapNullToEmpty(metrics.getEffectiveCpuSetMems());
-        oldVal = Arrays.stream(cpus).boxed().toArray(Integer[]::new);
-        Arrays.sort(oldVal);
+        oldVal = CgroupMetricsTester.boxedArrayOrNull(metrics.getEffectiveCpuSetMems());
+        oldVal = CgroupMetricsTester.sortAllowNull(oldVal);
         cpusstr = getStringVal("cpuset.mems.effective");
         newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr);
-        Arrays.sort(newVal);
+        newVal = CgroupMetricsTester.sortAllowNull(newVal);
         if (Arrays.compare(oldVal, newVal) != 0) {
             fail("cpuset.mems.effective", Arrays.toString(oldVal),
                                           Arrays.toString(newVal));
         }
     }
 
-    private int[] mapNullToEmpty(int[] cpus) {
-        if (cpus == null) {
-            // Not available. For sake of testing continue with an
-            // empty array.
-            cpus = new int[0];
-        }
-        return cpus;
-    }
-
     @Override
     public void testCpuConsumption() {
         Metrics metrics = Metrics.systemMetrics();
         // make system call
         long newSysVal = metrics.getCpuSystemUsage();

@@ -460,9 +454,9 @@
                             }
                         }
                         return accumulator;
                     }).collect(Collectors.summingLong(e -> e));
         } catch (IOException e) {
-            return CgroupSubsystem.LONG_RETVAL_UNLIMITED;
+            return NOT_AVAILABLE;
         }
     }
 }
< prev index next >