1 /* 2 * Copyright (c) 2004, 2018, 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 * @test 26 * @key stress gc 27 * 28 * @summary converted from VM Testbase gc/gctests/PhantomReference/PhantomReferenceEvilTest. 29 * VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, jrockit] 30 * VM Testbase readme: 31 * DESCRIPTION 32 * Test that verifies the PhantomReference handling in a more evil way. 33 * In this test, it will only keep every Xth object, thus causing more 34 * fragmentation and fill the heap with unused objects. This test should 35 * not throw any OOME during the test execution. 36 * 37 * COMMENTS 38 * This test was ported from JRockit test suite. 39 * 40 * @library /vmTestbase 41 * /test/lib 42 * @run driver jdk.test.lib.FileInstaller . . 43 * @run main/othervm 44 * -XX:-UseGCOverheadLimit 45 * gc.gctests.PhantomReference.PhantomReferenceEvilTest.PhantomReferenceEvilTest 46 */ 47 48 package gc.gctests.PhantomReference.PhantomReferenceEvilTest; 49 50 import gc.gctests.PhantomReference.PhantomHelper; 51 import gc.gctests.PhantomReference.PRHelper; 52 import java.lang.ref.ReferenceQueue; 53 import java.util.ArrayList; 54 import java.util.Random; 55 import java.util.HashMap; 56 import nsk.share.TestFailure; 57 import nsk.share.gc.GC; 58 import nsk.share.gc.GCTestBase; 59 import nsk.share.gc.Memory; 60 import nsk.share.gc.gp.GarbageUtils; 61 import nsk.share.test.Stresser; 62 63 /** 64 * Tests for the PhantomReference handling in a more evil way. 65 * 66 * This test must be run with a mx value set to ensure 67 * Runtime.maxMemory() doesn't return 0. 68 */ 69 public class PhantomReferenceEvilTest extends GCTestBase { 70 71 /** 72 * Test that verifies the PhantomReference handling in a more evil way. 73 * In this test, it will only keep every Xth object, thus causing more 74 * fragmentation and fill the heap with unused objects. This test should 75 * not throw any OOME during the test execution. 76 * 77 * @return success if all phantom references were enqueued 78 */ 79 public final void run() { 80 long seed; 81 int minSize; 82 int maxSize; 83 int keepEveryXthObject; 84 double memPercentToFill; 85 long maxWaitTime; 86 long counter = 0; 87 long totalMemAlloced = 0; 88 long memToAlloc = 0; 89 long nrOfPrs = 0; 90 Runtime r = Runtime.getRuntime(); 91 92 93 seed = runParams.getSeed(); 94 minSize = 2048; 95 maxSize = 32768; 96 keepEveryXthObject = 5; 97 memPercentToFill = 0.45; 98 maxWaitTime = 30000; 99 memToAlloc = (long) (r.maxMemory() * memPercentToFill); 100 Random rndGenerator = new Random(seed); 101 long multiplier = maxSize - minSize; 102 ReferenceQueue rq = new ReferenceQueue(); 103 HashMap hmHelper = new HashMap(); 104 ArrayList alPhantomRefs = new ArrayList(); 105 106 try { 107 try { 108 while (totalMemAlloced + Memory.getReferenceSize() 109 * hmHelper.size() < memToAlloc) { 110 int allocationSize = ((int) (rndGenerator.nextDouble() 111 * multiplier)) + minSize; 112 byte[] tmp = new byte[allocationSize]; 113 114 if (counter % keepEveryXthObject == 0) { 115 Integer ik = new Integer(tmp.hashCode()); 116 if (hmHelper.containsKey(ik)) { 117 PhantomHelper ph = (PhantomHelper) hmHelper.get(ik); 118 ph.increaseHashCounter(); 119 hmHelper.put(ik, ph); 120 } else { 121 hmHelper.put(ik, new PhantomHelper(tmp.hashCode())); 122 } 123 124 PRHelper prh = new PRHelper(tmp, rq); 125 prh.setReferentHashCode(tmp.hashCode()); 126 alPhantomRefs.add(prh); 127 totalMemAlloced += 128 Memory.getArraySize(allocationSize, Memory.getByteSize()) 129 + Memory.getReferenceSize() 130 + Memory.getReferenceObjectSize(); 131 132 //Make sure the temporary object is dereferenced 133 prh = null; 134 nrOfPrs++; 135 } 136 137 // Make sure the temporary object is dereferenced 138 tmp = null; 139 counter++; 140 if (counter == Long.MAX_VALUE) { 141 counter = 0; 142 } 143 } 144 } catch (OutOfMemoryError oome) { 145 alPhantomRefs.clear(); 146 hmHelper.clear(); 147 log.info(nrOfPrs + " phantom refs had been allocated when " 148 + "OOME occured"); 149 throw new TestFailure("OutOfMemoryException was thrown. This should " 150 + "not happen during the execution of this test."); 151 } 152 153 154 Stresser stresser = new Stresser(runParams.getStressOptions()); 155 stresser.start(0); 156 GarbageUtils.eatMemory(stresser); 157 if (!stresser.continueExecution()) { 158 return; //we couldn't be sure that FullGC is triggered 159 } 160 161 String retInfo = PhantomHelper.checkAllHashCodes( 162 rq, hmHelper, maxWaitTime); 163 if (retInfo != null) { 164 alPhantomRefs.clear(); 165 hmHelper.clear(); 166 throw new TestFailure(retInfo); 167 } 168 169 log.info(nrOfPrs + " phantom refs were allocated during the test"); 170 } finally { 171 // Make sure the ArrayList:s are live at the end of the test 172 // to make sure that the references gets enqueued. 173 alPhantomRefs.clear(); 174 hmHelper.clear(); 175 alPhantomRefs = null; 176 hmHelper = null; 177 } 178 } 179 180 public static void main(String[] args) { 181 GC.runTest(new PhantomReferenceEvilTest(), args); 182 } 183 }