1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 package gc.g1.humongousObjects;
  26 
  27 import gc.testlibrary.Helpers;
  28 import jdk.test.lib.Asserts;
  29 import sun.hotspot.WhiteBox;
  30 
  31 import java.lang.ref.Reference;
  32 import java.lang.ref.ReferenceQueue;
  33 import java.lang.ref.SoftReference;
  34 import java.lang.ref.WeakReference;
  35 
  36 /**
  37  * @test TestObjectCollected
  38  * @summary checks that after different type of GCs weak/soft references to humongous object behave correspondingly to
  39  * actual object behavior
  40  * @requires vm.gc.G1
  41  * @library /testlibrary /test/lib /
  42  * @modules java.base/jdk.internal.misc
  43  * @modules java.management
  44  * @build sun.hotspot.WhiteBox
  45  *        gc.testlibrary.Helpers
  46  *        gc.g1.humongousObjects.TestObjectCollected
  47  *
  48  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  49  *             sun.hotspot.WhiteBox$WhiteBoxPermission
  50  *
  51  * @run main/othervm -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  52  *                   -XX:+WhiteBoxAPI -Xbootclasspath/a:. -Xms200m -Xmx200m -Xlog:gc
  53  *                   -XX:InitiatingHeapOccupancyPercent=100 -XX:G1HeapRegionSize=1M -Xlog:gc=info:file=TestObjectCollected.gc.log
  54  *                    gc.g1.humongousObjects.TestObjectCollected
  55  */
  56 
  57 
  58 /**
  59  * Test checks that after different type of GCs weak/soft references to humongous object behave correspondingly to
  60  * actual object behavior.
  61  * So if object was collected, reference.get() should return null and vice versa
  62  * Since we check humongous objects after such an object is collected the region where it was allocated becomes free
  63  * or/and change type to non-humongous. Two WhiteBox method were used - first returns if a region containing certain
  64  * address is free and second - if a region containing certain address is humongous
  65  */
  66 
  67 public class TestObjectCollected {
  68     /**
  69      * Provides methods to initiate GC of requested type
  70      */
  71     private enum GC {
  72         YOUNG_CG {
  73             @Override
  74             public void provoke() {
  75                 WHITE_BOX.youngGC();
  76             }
  77         },
  78         FULL_GC {
  79             @Override
  80             public void provoke() {
  81                 System.gc();
  82             }
  83         },
  84         CMC {
  85             @Override
  86             public void provoke() {
  87                 Helpers.waitTillCMCFinished(WHITE_BOX, 0);
  88                 WHITE_BOX.g1StartConcMarkCycle();
  89                 Helpers.waitTillCMCFinished(WHITE_BOX, 0);
  90             }
  91         },
  92         FULL_GC_MEMORY_PRESSURE {
  93             @Override
  94             public void provoke() {
  95                 WHITE_BOX.fullGC();
  96             }
  97         };
  98         private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  99 
 100         public abstract void provoke();
 101     }
 102 
 103     /**
 104      * Factory for weak and soft references.
 105      * Allocates byte array of ALLOCATION_SIZE and returns weak/soft reference on it.
 106      */
 107     private enum REF_FACTORY {
 108         WEAK {
 109             @Override
 110             public Reference<byte[]> create() {
 111                 return new WeakReference<>(new byte[ALLOCATION_SIZE], referenceQueqe);
 112             }
 113         },
 114         SOFT {
 115             @Override
 116             public Reference<byte[]> create() {
 117                 return new SoftReference<>(new byte[ALLOCATION_SIZE], referenceQueqe);
 118             }
 119         };
 120 
 121         private static final ReferenceQueue<byte[]> referenceQueqe = new ReferenceQueue<>();
 122         private static final int ALLOCATION_SIZE = WhiteBox.getWhiteBox().g1RegionSize() * 2 / 3;
 123 
 124         /**
 125          * Factory method
 126          *
 127          * @return weak/soft reference on byte array of ALLOCATION_SIZE
 128          */
 129         public abstract Reference<byte[]> create();
 130     }
 131 
 132 
 133     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
 134 
 135     /**
 136      * Does actual testing:
 137      * Gets a reference
 138      * Gets address of referenced object using WB method
 139      * Calls gc of provided type
 140      * Checks that object was/was not deleted using WB methods.
 141      */
 142     public static void doTesting(GC gc, REF_FACTORY ref) {
 143 
 144         System.out.println(String.format("Testing %s reference behavior after %s", ref.name(), gc.name()));
 145 
 146         Reference reference = ref.create();
 147         Asserts.assertNotNull(reference, "Test Bug: failed to allocate reference");
 148         long adr = WHITE_BOX.getObjectAddress(reference.get());
 149 
 150         //Sanity checks
 151         boolean isRefNulled = (reference.get() == null);
 152         boolean isRegionHumongous = WHITE_BOX.g1BelongsToHumongousRegion(adr);
 153         boolean isRegionFree = WHITE_BOX.g1BelongsToFreeRegion(adr);
 154 
 155 
 156         Asserts.assertEquals(isRefNulled, false,
 157                 "We just allocated an object but reference.get() already returned null");
 158 
 159         Asserts.assertEquals(isRegionFree, false,
 160                 "We just allocated an object but WB returns that allocation region is still considered free");
 161 
 162         Asserts.assertEquals(isRegionHumongous, true,
 163                 "We just allocated a humongous object but WB returns that allocation region is not humongous");
 164 
 165         gc.provoke();
 166 
 167         isRefNulled = (reference.get() == null);
 168         isRegionHumongous = WHITE_BOX.g1BelongsToHumongousRegion(adr);
 169         isRegionFree = WHITE_BOX.g1BelongsToFreeRegion(adr);
 170 
 171         boolean isObjCollected = isRegionFree || !isRegionHumongous;
 172 
 173         Asserts.assertEquals(isRefNulled, isObjCollected,
 174                 String.format("There is an inconsistensy between reference and white box "
 175                                 + "method behavior - one considers object referenced with "
 176                                 + "%s type collected and another doesn't!\n"
 177                                 + "\treference.get() returned %snull\n"
 178                                 + "\tWhiteBox methods returned that object was%s collected",
 179                         reference.getClass().getSimpleName(),
 180                         (isRefNulled ? "" : "not "),
 181                         (isObjCollected ? "" : " not")));
 182 
 183         System.out.println("Passed");
 184     }
 185 
 186     /**
 187      * Entry point
 188      *
 189      * @param args not used
 190      */
 191     public static void main(String[] args) {
 192         // Full gc - System.gc()
 193         TestObjectCollected.doTesting(GC.FULL_GC, REF_FACTORY.WEAK);
 194         TestObjectCollected.doTesting(GC.FULL_GC, REF_FACTORY.SOFT);
 195 
 196         // Full gc with memory pressure - WB.fullGC() emulates that no memory left
 197         TestObjectCollected.doTesting(GC.FULL_GC_MEMORY_PRESSURE, REF_FACTORY.WEAK);
 198         TestObjectCollected.doTesting(GC.FULL_GC_MEMORY_PRESSURE, REF_FACTORY.SOFT);
 199 
 200         // Young gc
 201         TestObjectCollected.doTesting(GC.YOUNG_CG, REF_FACTORY.WEAK);
 202         TestObjectCollected.doTesting(GC.YOUNG_CG, REF_FACTORY.SOFT);
 203 
 204         // Concurrent mark cycle
 205         TestObjectCollected.doTesting(GC.CMC, REF_FACTORY.WEAK);
 206         TestObjectCollected.doTesting(GC.CMC, REF_FACTORY.SOFT);
 207     }
 208 }