8132723: Add tests which check that soft references to humongous objects should work correctly
authorkzhaldyb
Thu, 17 Dec 2015 16:46:41 +0300
changeset 35206 aaf0f36a013a
parent 35202 506ccf1717fd
child 35207 a80789b54a6d
8132723: Add tests which check that soft references to humongous objects should work correctly 8132724: Add tests which check that weak references to humongous objects should work correctly Reviewed-by: jmasa, dfazunen
hotspot/src/share/vm/prims/whitebox.cpp
hotspot/test/gc/g1/humongousObjects/TestObjectCollected.java
--- a/hotspot/src/share/vm/prims/whitebox.cpp	Mon Dec 21 14:09:21 2015 +0000
+++ b/hotspot/src/share/vm/prims/whitebox.cpp	Thu Dec 17 16:46:41 2015 +0300
@@ -309,6 +309,18 @@
   return hr->is_humongous();
 WB_END
 
+WB_ENTRY(jboolean, WB_G1BelongsToHumongousRegion(JNIEnv* env, jobject o, jlong addr))
+  G1CollectedHeap* g1 = G1CollectedHeap::heap();
+  const HeapRegion* hr = g1->heap_region_containing((void*) addr);
+  return hr->is_humongous();
+WB_END
+
+WB_ENTRY(jboolean, WB_G1BelongsToFreeRegion(JNIEnv* env, jobject o, jlong addr))
+  G1CollectedHeap* g1 = G1CollectedHeap::heap();
+  const HeapRegion* hr = g1->heap_region_containing((void*) addr);
+  return hr->is_free();
+WB_END
+
 WB_ENTRY(jlong, WB_G1NumMaxRegions(JNIEnv* env, jobject o))
   G1CollectedHeap* g1 = G1CollectedHeap::heap();
   size_t nr = g1->max_regions();
@@ -1478,7 +1490,9 @@
   {CC"isSharedClass", CC"(Ljava/lang/Class;)Z",       (void*)&WB_IsSharedClass },
 #if INCLUDE_ALL_GCS
   {CC"g1InConcurrentMark", CC"()Z",                   (void*)&WB_G1InConcurrentMark},
-  {CC"g1IsHumongous0",      CC"(Ljava/lang/Object;)Z", (void*)&WB_G1IsHumongous     },
+  {CC"g1IsHumongous0",      CC"(Ljava/lang/Object;)Z",(void*)&WB_G1IsHumongous     },
+  {CC"g1BelongsToHumongousRegion0", CC"(J)Z",         (void*)&WB_G1BelongsToHumongousRegion},
+  {CC"g1BelongsToFreeRegion0", CC"(J)Z",              (void*)&WB_G1BelongsToFreeRegion},
   {CC"g1NumMaxRegions",    CC"()J",                   (void*)&WB_G1NumMaxRegions  },
   {CC"g1NumFreeRegions",   CC"()J",                   (void*)&WB_G1NumFreeRegions  },
   {CC"g1RegionSize",       CC"()I",                   (void*)&WB_G1RegionSize      },
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/gc/g1/humongousObjects/TestObjectCollected.java	Thu Dec 17 16:46:41 2015 +0300
@@ -0,0 +1,207 @@
+/*
+ * Copyright (c) 2015, 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.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ *
+ */
+
+package gc.g1.humongousObjects;
+
+import gc.testlibrary.Helpers;
+import jdk.test.lib.Asserts;
+import sun.hotspot.WhiteBox;
+
+import java.lang.ref.Reference;
+import java.lang.ref.ReferenceQueue;
+import java.lang.ref.SoftReference;
+import java.lang.ref.WeakReference;
+
+/**
+ * @test TestObjectCollected
+ * @summary checks that after different type of GCs weak/soft references to humongous object behave correspondingly to
+ * actual object behavior
+ * @requires vm.gc=="G1" | vm.gc=="null"
+ * @library /testlibrary /test/lib /
+ * @modules java.management
+ * @build sun.hotspot.WhiteBox
+ *        gc.testlibrary.Helpers
+ *        gc.g1.humongousObjects.TestObjectCollected
+ *
+ * @run driver ClassFileInstaller sun.hotspot.WhiteBox
+ *             sun.hotspot.WhiteBox$WhiteBoxPermission
+ *
+ * @run main/othervm -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
+ *                   -XX:+WhiteBoxAPI -Xbootclasspath/a:. -Xms200m -Xmx200m -Xlog:gc
+ *                   -XX:InitiatingHeapOccupancyPercent=100 -XX:G1HeapRegionSize=1M -Xlog:gc=info:file=TestObjectCollected.gc.log
+ *                    gc.g1.humongousObjects.TestObjectCollected
+ */
+
+
+/**
+ * Test checks that after different type of GCs weak/soft references to humongous object behave correspondingly to
+ * actual object behavior.
+ * So if object was collected, reference.get() should return null and vice versa
+ * Since we check humongous objects after such an object is collected the region where it was allocated becomes free
+ * or/and change type to non-humongous. Two WhiteBox method were used - first returns if a region containing certain
+ * address is free and second - if a region containing certain address is humongous
+ */
+
+public class TestObjectCollected {
+    /**
+     * Provides methods to initiate GC of requested type
+     */
+    private enum GC {
+        YOUNG_CG {
+            @Override
+            public void provoke() {
+                WHITE_BOX.youngGC();
+            }
+        },
+        FULL_GC {
+            @Override
+            public void provoke() {
+                System.gc();
+            }
+        },
+        CMC {
+            @Override
+            public void provoke() {
+                Helpers.waitTillCMCFinished(WHITE_BOX, 0);
+                WHITE_BOX.g1StartConcMarkCycle();
+                Helpers.waitTillCMCFinished(WHITE_BOX, 0);
+            }
+        },
+        FULL_GC_MEMORY_PRESSURE {
+            @Override
+            public void provoke() {
+                WHITE_BOX.fullGC();
+            }
+        };
+        private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
+
+        public abstract void provoke();
+    }
+
+    /**
+     * Factory for weak and soft references.
+     * Allocates byte array of ALLOCATION_SIZE and returns weak/soft reference on it.
+     */
+    private enum REF_FACTORY {
+        WEAK {
+            @Override
+            public Reference<byte[]> create() {
+                return new WeakReference<>(new byte[ALLOCATION_SIZE], referenceQueqe);
+            }
+        },
+        SOFT {
+            @Override
+            public Reference<byte[]> create() {
+                return new SoftReference<>(new byte[ALLOCATION_SIZE], referenceQueqe);
+            }
+        };
+
+        private static final ReferenceQueue<byte[]> referenceQueqe = new ReferenceQueue<>();
+        private static final int ALLOCATION_SIZE = WhiteBox.getWhiteBox().g1RegionSize() * 2 / 3;
+
+        /**
+         * Factory method
+         *
+         * @return weak/soft reference on byte array of ALLOCATION_SIZE
+         */
+        public abstract Reference<byte[]> create();
+    }
+
+
+    private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
+
+    /**
+     * Does actual testing:
+     * Gets a reference
+     * Gets address of referenced object using WB method
+     * Calls gc of provided type
+     * Checks that object was/was not deleted using WB methods.
+     */
+    public static void doTesting(GC gc, REF_FACTORY ref) {
+
+        System.out.println(String.format("Testing %s reference behavior after %s", ref.name(), gc.name()));
+
+        Reference reference = ref.create();
+        Asserts.assertNotNull(reference, "Test Bug: failed to allocate reference");
+        long adr = WHITE_BOX.getObjectAddress(reference.get());
+
+        //Sanity checks
+        boolean isRefNulled = (reference.get() == null);
+        boolean isRegionHumongous = WHITE_BOX.g1BelongsToHumongousRegion(adr);
+        boolean isRegionFree = WHITE_BOX.g1BelongsToFreeRegion(adr);
+
+
+        Asserts.assertEquals(isRefNulled, false,
+                "We just allocated an object but reference.get() already returned null");
+
+        Asserts.assertEquals(isRegionFree, false,
+                "We just allocated an object but WB returns that allocation region is still considered free");
+
+        Asserts.assertEquals(isRegionHumongous, true,
+                "We just allocated a humongous object but WB returns that allocation region is not humongous");
+
+        gc.provoke();
+
+        isRefNulled = (reference.get() == null);
+        isRegionHumongous = WHITE_BOX.g1BelongsToHumongousRegion(adr);
+        isRegionFree = WHITE_BOX.g1BelongsToFreeRegion(adr);
+
+        boolean isObjCollected = isRegionFree || !isRegionHumongous;
+
+        Asserts.assertEquals(isRefNulled, isObjCollected,
+                String.format("There is an inconsistensy between reference and white box "
+                                + "method behavior - one considers object referenced with "
+                                + "%s type collected and another doesn't!\n"
+                                + "\treference.get() returned %snull\n"
+                                + "\tWhiteBox methods returned that object was%s collected",
+                        reference.getClass().getSimpleName(),
+                        (isRefNulled ? "" : "not "),
+                        (isObjCollected ? "" : " not")));
+
+        System.out.println("Passed");
+    }
+
+    /**
+     * Entry point
+     *
+     * @param args not used
+     */
+    public static void main(String[] args) {
+        // Full gc - System.gc()
+        TestObjectCollected.doTesting(GC.FULL_GC, REF_FACTORY.WEAK);
+        TestObjectCollected.doTesting(GC.FULL_GC, REF_FACTORY.SOFT);
+
+        // Full gc with memory pressure - WB.fullGC() emulates that no memory left
+        TestObjectCollected.doTesting(GC.FULL_GC_MEMORY_PRESSURE, REF_FACTORY.WEAK);
+        TestObjectCollected.doTesting(GC.FULL_GC_MEMORY_PRESSURE, REF_FACTORY.SOFT);
+
+        // Young gc
+        TestObjectCollected.doTesting(GC.YOUNG_CG, REF_FACTORY.WEAK);
+        TestObjectCollected.doTesting(GC.YOUNG_CG, REF_FACTORY.SOFT);
+
+        // Concurrent mark cycle
+        TestObjectCollected.doTesting(GC.CMC, REF_FACTORY.WEAK);
+        TestObjectCollected.doTesting(GC.CMC, REF_FACTORY.SOFT);
+    }
+}