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