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 package gc.g1.humongousObjects;
 25 
 26 import gc.testlibrary.Helpers;
 27 import jdk.test.lib.Asserts;
 28 import sun.hotspot.WhiteBox;
 29 
 30 import java.lang.ref.Reference;
 31 import java.lang.ref.ReferenceQueue;
 32 import java.lang.ref.SoftReference;
 33 import java.lang.ref.WeakReference;
 34 
 35 /**
 36  * @test TestObjectCollected
 37  * @summary checks that after different type of GCs weak/soft references to humongous object behave correspondingly to
 38  * actual object behavior
 39  * @requires vm.gc.G1
 40  * @library /test/lib /
 41  * @modules java.base/jdk.internal.misc
 42  * @modules java.management
 43  * @build sun.hotspot.WhiteBox
 44  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
 45  *             sun.hotspot.WhiteBox$WhiteBoxPermission
 46  *
 47  * @run main/othervm -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
 48  *                   -XX:+WhiteBoxAPI -Xbootclasspath/a:. -Xms200m -Xmx200m -Xlog:gc
 49  *                   -XX:InitiatingHeapOccupancyPercent=100 -XX:G1HeapRegionSize=1M -Xlog:gc=info:file=TestObjectCollected.gc.log
 50  *                    gc.g1.humongousObjects.TestObjectCollected
 51  */
 52 
 53 
 54 /**
 55  * Test checks that after different type of GCs weak/soft references to humongous object behave correspondingly to
 56  * actual object behavior.
 57  * So if object was collected, reference.get() should return null and vice versa
 58  * Since we check humongous objects after such an object is collected the region where it was allocated becomes free
 59  * or/and change type to non-humongous. Two WhiteBox method were used - first returns if a region containing certain
 60  * address is free and second - if a region containing certain address is humongous
 61  */
 62 
 63 public class TestObjectCollected {
 64     /**
 65      * Provides methods to initiate GC of requested type
 66      */
 67     private enum GC {
 68         YOUNG_CG {
 69             @Override
 70             public void provoke() {
 71                 WHITE_BOX.youngGC();
 72             }
 73         },
 74         FULL_GC {
 75             @Override
 76             public void provoke() {
 77                 System.gc();
 78             }
 79         },
 80         CMC {
 81             @Override
 82             public void provoke() {
 83                 Helpers.waitTillCMCFinished(WHITE_BOX, 0);
 84                 WHITE_BOX.g1StartConcMarkCycle();
 85                 Helpers.waitTillCMCFinished(WHITE_BOX, 0);
 86             }
 87         },
 88         FULL_GC_MEMORY_PRESSURE {
 89             @Override
 90             public void provoke() {
 91                 WHITE_BOX.fullGC();
 92             }
 93         };
 94         private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
 95 
 96         public abstract void provoke();
 97     }
 98 
 99     /**
100      * Factory for weak and soft references.
101      * Allocates byte array of ALLOCATION_SIZE and returns weak/soft reference on it.
102      */
103     private enum REF_FACTORY {
104         WEAK {
105             @Override
106             public Reference<byte[]> create() {
107                 return new WeakReference<>(new byte[ALLOCATION_SIZE], referenceQueqe);
108             }
109         },
110         SOFT {
111             @Override
112             public Reference<byte[]> create() {
113                 return new SoftReference<>(new byte[ALLOCATION_SIZE], referenceQueqe);
114             }
115         };
116 
117         private static final ReferenceQueue<byte[]> referenceQueqe = new ReferenceQueue<>();
118         private static final int ALLOCATION_SIZE = WhiteBox.getWhiteBox().g1RegionSize() * 2 / 3;
119 
120         /**
121          * Factory method
122          *
123          * @return weak/soft reference on byte array of ALLOCATION_SIZE
124          */
125         public abstract Reference<byte[]> create();
126     }
127 
128 
129     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
130 
131     /**
132      * Does actual testing:
133      * Gets a reference
134      * Gets address of referenced object using WB method
135      * Calls gc of provided type
136      * Checks that object was/was not deleted using WB methods.
137      */
138     public static void doTesting(GC gc, REF_FACTORY ref) {
139 
140         System.out.println(String.format("Testing %s reference behavior after %s", ref.name(), gc.name()));
141 
142         Reference<byte[]> reference = ref.create();
143         Asserts.assertNotNull(reference, "Test Bug: failed to allocate reference");
144         long adr = WHITE_BOX.getObjectAddress(reference.get());
145 
146         //Sanity checks
147         boolean isRefNulled = (reference.get() == null);
148         boolean isRegionHumongous = WHITE_BOX.g1BelongsToHumongousRegion(adr);
149         boolean isRegionFree = WHITE_BOX.g1BelongsToFreeRegion(adr);
150 
151 
152         Asserts.assertEquals(isRefNulled, false,
153                 "We just allocated an object but reference.get() already returned null");
154 
155         Asserts.assertEquals(isRegionFree, false,
156                 "We just allocated an object but WB returns that allocation region is still considered free");
157 
158         Asserts.assertEquals(isRegionHumongous, true,
159                 "We just allocated a humongous object but WB returns that allocation region is not humongous");
160 
161         gc.provoke();
162 
163         isRefNulled = (reference.get() == null);
164         isRegionHumongous = WHITE_BOX.g1BelongsToHumongousRegion(adr);
165         isRegionFree = WHITE_BOX.g1BelongsToFreeRegion(adr);
166 
167         boolean isObjCollected = isRegionFree || !isRegionHumongous;
168 
169         Asserts.assertEquals(isRefNulled, isObjCollected,
170                 String.format("There is an inconsistensy between reference and white box "
171                                 + "method behavior - one considers object referenced with "
172                                 + "%s type collected and another doesn't!\n"
173                                 + "\treference.get() returned %snull\n"
174                                 + "\tWhiteBox methods returned that object was%s collected",
175                         reference.getClass().getSimpleName(),
176                         (isRefNulled ? "" : "not "),
177                         (isObjCollected ? "" : " not")));
178 
179         System.out.println("Passed");
180     }
181 
182     /**
183      * Entry point
184      *
185      * @param args not used
186      */
187     public static void main(String[] args) {
188         // Full gc - System.gc()
189         TestObjectCollected.doTesting(GC.FULL_GC, REF_FACTORY.WEAK);
190         TestObjectCollected.doTesting(GC.FULL_GC, REF_FACTORY.SOFT);
191 
192         // Full gc with memory pressure - WB.fullGC() emulates that no memory left
193         TestObjectCollected.doTesting(GC.FULL_GC_MEMORY_PRESSURE, REF_FACTORY.WEAK);
194         TestObjectCollected.doTesting(GC.FULL_GC_MEMORY_PRESSURE, REF_FACTORY.SOFT);
195 
196         // Young gc
197         TestObjectCollected.doTesting(GC.YOUNG_CG, REF_FACTORY.WEAK);
198         TestObjectCollected.doTesting(GC.YOUNG_CG, REF_FACTORY.SOFT);
199 
200         // Concurrent mark cycle
201         TestObjectCollected.doTesting(GC.CMC, REF_FACTORY.WEAK);
202         TestObjectCollected.doTesting(GC.CMC, REF_FACTORY.SOFT);
203     }
204 }