< prev index next >

test/jdk/jdk/internal/platform/docker/MetricsMemoryTester.java

Print this page
@  rev 57734 : Review feedback
|
o  rev 57733 : 8231111: Cgroups v2: Rework Metrics in java.base so as to recognize unified hierarchy
|  Reviewed-by: bobv, mchung
~

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -21,10 +21,11 @@
  * questions.
  */
 
 import java.util.Arrays;
 import jdk.internal.platform.Metrics;
+import jdk.internal.platform.MetricsCgroupV1;
 
 public class MetricsMemoryTester {
     public static void main(String[] args) {
         System.out.println(Arrays.toString(args));
         switch (args[0]) {

@@ -63,22 +64,30 @@
     private static void testMemoryFailCount() {
         long count = Metrics.systemMetrics().getMemoryFailCount();
 
         // Allocate 512M of data
         byte[][] bytes = new byte[64][];
+        boolean atLeastOneAllocationWorked = false;
         for (int i = 0; i < 64; i++) {
             try {
                 bytes[i] = new byte[8 * 1024 * 1024];
+                atLeastOneAllocationWorked = true;
                 // Break out as soon as we see an increase in failcount
                 // to avoid getting killed by the OOM killer.
                 if (Metrics.systemMetrics().getMemoryFailCount() > count) {
                     break;
                 }
             } catch (Error e) { // OOM error
                 break;
             }
         }
+        if (!atLeastOneAllocationWorked) {
+            System.out.println("Allocation failed immediately. Ignoring test!");
+            return;
+        }
+        // Be sure bytes allocations don't get optimized out
+        System.out.println("DEBUG: Bytes allocation length 1: " + bytes[0].length);
         if (Metrics.systemMetrics().getMemoryFailCount() <= count) {
             throw new RuntimeException("Memory fail count : new : ["
                     + Metrics.systemMetrics().getMemoryFailCount() + "]"
                     + ", old : [" + count + "]");
         }

@@ -97,18 +106,24 @@
         }
         System.out.println("TEST PASSED!!!");
     }
 
     private static void testKernelMemoryLimit(String value) {
+        Metrics m = Metrics.systemMetrics();
+        if (m instanceof MetricsCgroupV1) {
+            MetricsCgroupV1 mCgroupV1 = (MetricsCgroupV1)m;
+            System.out.println("TEST PASSED!!!");
         long limit = getMemoryValue(value);
-        long kmemlimit = Metrics.systemMetrics().getKernelMemoryLimit();
+            long kmemlimit = mCgroupV1.getKernelMemoryLimit();
         if (kmemlimit != 0 && limit != kmemlimit) {
             throw new RuntimeException("Kernel Memory limit not equal, expected : ["
                     + limit + "]" + ", got : ["
                     + kmemlimit + "]");
         }
-        System.out.println("TEST PASSED!!!");
+        } else {
+            throw new RuntimeException("oomKillFlag test not supported for cgroups v2");
+        }
     }
 
     private static void testMemoryAndSwapLimit(String memory, String memAndSwap) {
         long expectedMem = getMemoryValue(memory);
         long expectedMemAndSwap = getMemoryValue(memAndSwap);

@@ -136,11 +151,19 @@
         }
         return result;
     }
 
     private static void testOomKillFlag(boolean oomKillFlag) {
-        if (!(oomKillFlag ^ Metrics.systemMetrics().isMemoryOOMKillEnabled())) {
+        Metrics m = Metrics.systemMetrics();
+        if (m instanceof MetricsCgroupV1) {
+            MetricsCgroupV1 mCgroupV1 = (MetricsCgroupV1)m;
+            Boolean expected = Boolean.valueOf(oomKillFlag);
+            Boolean actual = mCgroupV1.isMemoryOOMKillEnabled();
+            if (!(expected.equals(actual))) {
             throw new RuntimeException("oomKillFlag error");
         }
         System.out.println("TEST PASSED!!!");
+        } else {
+            throw new RuntimeException("oomKillFlag test not supported for cgroups v2");
+        }
     }
 }
< prev index next >