< prev index next >

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

Print this page
@  rev 57446 : Review changes
|
o  rev 57445 : 8231111: Cgroups v2: Rework Metrics in java.base so as to recognize unified hierarchy
|  Reviewed-by: bobv
~
o  rev 56863 : 8231111: Cgroups v2: Rework Metrics in java.base so as to recognize unified hierarchy
|  Reviewed-by: bobv
~

@@ -147,14 +147,10 @@
 
     private void fail(String metric, String oldVal, String newVal) {
         CgroupMetricsTester.fail(UnifiedController.NAME, metric, oldVal, newVal);
     }
 
-    private void fail(String metric, String message) {
-        throw new RuntimeException(metric + ": " + message);
-    }
-
     private void warn(String metric, long oldVal, long newVal) {
         CgroupMetricsTester.warn(UnifiedController.NAME, metric, oldVal, newVal);
     }
 
     private void verifyNotSupported(long metricVal) {

@@ -210,10 +206,13 @@
         return getCpuValueFromFile(file, 1 /* $PERIOD index */);
     }
 
     private long getCpuValueFromFile(String file, int index) {
         String maxPeriod = getStringVal(file);
+        if (maxPeriod == null) {
+            return UNLIMITED;
+        }
         String[] tokens = maxPeriod.split("\\s+");
         String val = tokens[index];
         if (MAX.equals(val)) {
             return UNLIMITED;
         }

@@ -226,11 +225,10 @@
 
     @Override
     public void testMemorySubsystem() {
         Metrics metrics = Metrics.systemMetrics();
 
-        long memoryMaxUsageMin;
         // User Memory
         long oldVal = metrics.getMemoryFailCount();
         long newVal = getLongValueEntryFromFile("memory.events", "max");
         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
             fail("memory.events[max]", oldVal, newVal);

@@ -240,24 +238,16 @@
         newVal = getLongLimitValueFromFile("memory.max");
         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
             fail("memory.max", oldVal, newVal);
         }
 
-        memoryMaxUsageMin = oldVal = metrics.getMemoryUsage();
+        oldVal = metrics.getMemoryUsage();
         newVal = getLongValueFromFile("memory.current");
         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
             fail("memory.current", oldVal, newVal);
         }
-
-        // Memory max has no knowledge of out of band read to "memory.current"
-        // above. Fix that by calling it again.
-        metrics.getMemoryUsage();
-        long memoryMax = metrics.getMemoryMaxUsage();
-        if (memoryMax < memoryMaxUsageMin || memoryMax < newVal) {
-            fail("memoryMaxUsage", "old usages [ " + memoryMaxUsageMin + ", " +
-                                   newVal + " ] >= " + memoryMax);
-        }
+        verifyNotSupported(metrics.getMemoryMaxUsage());
 
         // Kernel memory
         verifyNotSupported(metrics.getKernelMemoryFailCount());
         verifyNotSupported(metrics.getKernelMemoryLimit());
         verifyNotSupported(metrics.getKernelMemoryMaxUsage());

@@ -265,11 +255,16 @@
 
         //TCP Memory
         verifyNotSupported(metrics.getTcpMemoryFailCount());
         verifyNotSupported(metrics.getTcpMemoryLimit());
         verifyNotSupported(metrics.getTcpMemoryMaxUsage());
-        verifyNotSupported(metrics.getTcpMemoryUsage());
+
+        oldVal = metrics.getTcpMemoryUsage();
+        newVal = getLongValueEntryFromFile("memory.stat", "sock");
+        if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
+            fail("memory.stat[sock]", oldVal, newVal);
+        }
 
         //  Memory and Swap
         verifyNotSupported(metrics.getMemoryAndSwapFailCount());
         verifyNotSupported(metrics.getMemoryAndSwapMaxUsage());
 

@@ -289,11 +284,11 @@
         newVal = getLongLimitValueFromFile("memory.high");
         if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
             fail("memory.high", oldVal, newVal);
         }
 
-        Asserts.assertFalse(metrics.isMemoryOOMKillEnabled(), "Not supported");
+        Asserts.assertNull(metrics.isMemoryOOMKillEnabled(), "Not supported");
     }
 
     @Override
     public void testCpuAccounting() {
         Metrics metrics = Metrics.systemMetrics();

@@ -360,11 +355,12 @@
     }
 
     @Override
     public void testCpuSets() {
         Metrics metrics = Metrics.systemMetrics();
-        Integer[] oldVal = Arrays.stream(metrics.getCpuSetCpus()).boxed().toArray(Integer[]::new);
+        int[] cpus = mapNullToEmpty(metrics.getCpuSetCpus());
+        Integer[] oldVal = Arrays.stream(cpus).boxed().toArray(Integer[]::new);
         Arrays.sort(oldVal);
 
         String cpusstr = getStringVal("cpuset.cpus");
         // Parse range string in the format 1,2-6,7
         Integer[] newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr);

@@ -372,42 +368,54 @@
         if (Arrays.compare(oldVal, newVal) != 0) {
             fail("cpuset.cpus", Arrays.toString(oldVal),
                                 Arrays.toString(newVal));
         }
 
-        oldVal = Arrays.stream(metrics.getEffectiveCpuSetCpus()).boxed().toArray(Integer[]::new);
+        cpus = mapNullToEmpty(metrics.getEffectiveCpuSetCpus());
+        oldVal = Arrays.stream(cpus).boxed().toArray(Integer[]::new);
         Arrays.sort(oldVal);
         cpusstr = getStringVal("cpuset.cpus.effective");
         newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr);
         Arrays.sort(newVal);
         if (Arrays.compare(oldVal, newVal) != 0) {
             fail("cpuset.cpus.effective", Arrays.toString(oldVal),
                                           Arrays.toString(newVal));
         }
 
-        oldVal = Arrays.stream(metrics.getCpuSetMems()).boxed().toArray(Integer[]::new);
+        cpus = mapNullToEmpty(metrics.getCpuSetMems());
+        oldVal = Arrays.stream(cpus).boxed().toArray(Integer[]::new);
         Arrays.sort(oldVal);
         cpusstr = getStringVal("cpuset.mems");
         newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr);
         Arrays.sort(newVal);
         if (Arrays.compare(oldVal, newVal) != 0) {
             fail("cpuset.mems", Arrays.toString(oldVal),
                                 Arrays.toString(newVal));
         }
 
-        oldVal = Arrays.stream(metrics.getEffectiveCpuSetMems()).boxed().toArray(Integer[]::new);
+        cpus = mapNullToEmpty(metrics.getEffectiveCpuSetMems());
+        oldVal = Arrays.stream(cpus).boxed().toArray(Integer[]::new);
         Arrays.sort(oldVal);
         cpusstr = getStringVal("cpuset.mems.effective");
         newVal = CgroupMetricsTester.convertCpuSetsToArray(cpusstr);
         Arrays.sort(newVal);
         if (Arrays.compare(oldVal, newVal) != 0) {
             fail("cpuset.mems.effective", Arrays.toString(oldVal),
                                           Arrays.toString(newVal));
         }
 
         verifyNotSupported(metrics.getCpuSetMemoryPressure());
-        Asserts.assertFalse(metrics.isCpuSetMemoryPressureEnabled(), "Should be not supported");
+        Asserts.assertNull(metrics.isCpuSetMemoryPressureEnabled(), "Should be not supported");
+    }
+
+    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();

@@ -465,9 +473,47 @@
         }
     }
 
     @Override
     public void testMisc() {
-        // nothing for cgroups v2
+        testIOStat();
+    }
+
+    private void testIOStat() {
+        Metrics metrics = Metrics.systemMetrics();
+        long oldVal = metrics.getBlkIOServiceCount();
+        long newVal = getIoStatAccumulate(new String[] { "rios", "wios" });
+        if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
+            fail("io.stat->rios/wios: ", oldVal, newVal);
+        }
+
+        oldVal = metrics.getBlkIOServiced();
+        newVal = getIoStatAccumulate(new String[] { "rbytes", "wbytes" });
+        if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
+            fail("io.stat->rbytes/wbytes: ", oldVal, newVal);
+        }
     }
 
+    private long getIoStatAccumulate(String[] matchNames) {
+        try {
+            return Files.lines(Paths.get(UNIFIED.getPath(), "io.stat"))
+                    .map(line -> {
+                        long accumulator = 0;
+                        String[] tokens = line.split("\\s+");
+                        for (String t: tokens) {
+                            String[] keyVal = t.split("=");
+                            if (keyVal.length != 2) {
+                                continue;
+                            }
+                            for (String match: matchNames) {
+                                if (match.equals(keyVal[0])) {
+                                    accumulator += Long.parseLong(keyVal[1]);
+                                }
+                            }
+                        }
+                        return accumulator;
+                    }).collect(Collectors.summingLong(e -> e));
+        } catch (IOException e) {
+            return Metrics.LONG_RETVAL_NOT_SUPPORTED;
+        }
+    }
 }
< prev index next >