8163413: gc/metaspace/TestMetaspacePerfCounters failure
authorsjohanss
Mon, 22 Aug 2016 16:48:04 +0200
changeset 40657 a61a7a7966f2
parent 40656 d4964c292b78
child 40659 a2b2936240b9
8163413: gc/metaspace/TestMetaspacePerfCounters failure Reviewed-by: ehelin, dfazunen
hotspot/test/gc/metaspace/TestMetaspacePerfCounters.java
--- a/hotspot/test/gc/metaspace/TestMetaspacePerfCounters.java	Thu Jul 14 09:52:03 2016 +0200
+++ b/hotspot/test/gc/metaspace/TestMetaspacePerfCounters.java	Mon Aug 22 16:48:04 2016 +0200
@@ -21,6 +21,7 @@
  * questions.
  */
 
+import java.lang.management.GarbageCollectorMXBean;
 import java.util.List;
 import java.util.ArrayList;
 
@@ -28,6 +29,8 @@
 import jdk.test.lib.InMemoryJavaCompiler;
 import jdk.test.lib.Platform;
 
+import sun.management.ManagementFactoryHelper;
+
 import static jdk.test.lib.Asserts.*;
 
 /* @test TestMetaspacePerfCounters
@@ -38,7 +41,7 @@
  *          space exists and works.
  * @modules java.base/jdk.internal.misc
  *          java.compiler
- *          java.management
+ *          java.management/sun.management
  *          jdk.jvmstat/sun.jvmstat.monitor
  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UseSerialGC TestMetaspacePerfCounters
  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UseParallelGC -XX:+UseParallelOldGC TestMetaspacePerfCounters
@@ -51,6 +54,7 @@
 public class TestMetaspacePerfCounters {
     public static Class fooClass = null;
     private static final String[] counterNames = {"minCapacity", "maxCapacity", "capacity", "used"};
+    private static final List<GarbageCollectorMXBean> gcBeans = ManagementFactoryHelper.getGarbageCollectorMXBeans();
 
     public static void main(String[] args) throws Exception {
         String metaspace = "sun.gc.metaspace";
@@ -68,10 +72,27 @@
     }
 
     private static void checkPerfCounters(String ns) throws Exception {
-        long minCapacity = getMinCapacity(ns);
-        long maxCapacity = getMaxCapacity(ns);
-        long capacity = getCapacity(ns);
-        long used = getUsed(ns);
+        long gcCountBefore;
+        long gcCountAfter;
+        long minCapacity;
+        long maxCapacity;
+        long capacity;
+        long used;
+
+        // The perf counter values are updated during GC and to be able to
+        // do the assertions below we need to ensure that the values are from
+        // the same GC cycle.
+        do {
+            gcCountBefore = currentGCCount();
+
+            minCapacity = getMinCapacity(ns);
+            maxCapacity = getMaxCapacity(ns);
+            capacity = getCapacity(ns);
+            used = getUsed(ns);
+
+            gcCountAfter = currentGCCount();
+            assertGTE(gcCountAfter, gcCountBefore);
+        } while(gcCountAfter > gcCountBefore);
 
         assertGTE(minCapacity, 0L);
         assertGTE(used, minCapacity);
@@ -130,4 +151,12 @@
     private static long getUsed(String ns) throws Exception {
         return PerfCounters.findByName(ns + ".used").longValue();
     }
+
+    private static long currentGCCount() {
+        long gcCount = 0;
+        for (GarbageCollectorMXBean bean : gcBeans) {
+            gcCount += bean.getCollectionCount();
+        }
+        return gcCount;
+    }
 }