1 /* 2 * Copyright (c) 2004, 2020, 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 randomness 27 * 28 * @summary converted from VM Testbase gc/gctests/SoftReference/soft001. 29 * VM Testbase keywords: [gc, stress, stressopt, nonconcurrent] 30 * VM Testbase readme: 31 * DESCRIPTION 32 * The test checks that Garbage Collector correctly works with 33 * SoftReferences. It also checks that no unexpected exceptions and errors 34 * are thrown or the JVM is not crashed. 35 * The test starts a number of threads. Each thread run tests for some time 36 * or serveral iterations. See javadoc StressOptions for configuration. 37 * First of all each thread defines what type to check (there are 10 types 38 * totally). As soon as the type is defined, a SoftRefence is created that 39 * refers to an array of tested type and is registered with in a queue. A 40 * SoftRefence for NonbranchyTree class does not refer to an array, but to 41 * instances of the class. 42 * After that a thread performs next checks for the reference: 43 * 1. The reference is in queue after GC is provoked with 44 * Algorithms.eatMemory() method (a single thread eats the memory). 45 * 2. queue.remove() returns reference from the queue. 46 * 3. queue.poll() returns null. 47 * 4. reference.clear() does not throw any exception. 48 * The test extends ThreadedGCTest and implements GarbageProducerAware and 49 * MemoryStrategyAware interfaces. The corresponding javadoc documentation 50 * for additional test configuration. 51 * 52 * @library /vmTestbase 53 * /test/lib 54 * @run main/othervm gc.gctests.SoftReference.soft001.soft001 -ms low 55 */ 56 57 package gc.gctests.SoftReference.soft001; 58 59 import java.lang.ref.Reference; 60 import java.lang.ref.ReferenceQueue; 61 import java.lang.ref.SoftReference; 62 63 import nsk.share.gc.GC; 64 import nsk.share.gc.NonbranchyTree; 65 import nsk.share.gc.ThreadedGCTest; 66 import nsk.share.gc.gp.GarbageProducer; 67 import nsk.share.gc.gp.GarbageProducerAware; 68 import nsk.share.gc.gp.GarbageUtils; 69 import nsk.share.gc.gp.MemoryStrategy; 70 import nsk.share.gc.gp.MemoryStrategyAware; 71 import nsk.share.gc.gp.string.InternedStringProducer; 72 import nsk.share.gc.gp.string.RandomStringProducer; 73 74 public class soft001 extends ThreadedGCTest implements GarbageProducerAware, MemoryStrategyAware { 75 76 private GarbageProducer garbageProducer; 77 private InternedStringProducer internedStringProducer = new InternedStringProducer(new RandomStringProducer(10)); 78 private MemoryStrategy memoryStrategy; 79 // Total number of types to test 80 final static int TYPES_COUNT = 11; 81 // Size of array of each tested type. The constant also specifies the 82 // number of nodes in a NonbranchyTree and size of each node 83 final static int SIZE = 100; 84 createRunnable(int i)85 protected Runnable createRunnable(int i) { 86 return new Test(); 87 } 88 setGarbageProducer(GarbageProducer garbageProducer)89 public void setGarbageProducer(GarbageProducer garbageProducer) { 90 this.garbageProducer = garbageProducer; 91 } 92 setMemoryStrategy(MemoryStrategy memoryStrategy)93 public void setMemoryStrategy(MemoryStrategy memoryStrategy) { 94 this.memoryStrategy = memoryStrategy; 95 } 96 main(String[] args)97 public static void main(String[] args) { 98 GC.runTest(new soft001(), args); 99 } 100 101 // The class implements the logic of the testcase 102 class Test implements Runnable { 103 104 int iteration; 105 run()106 public void run() { 107 // Pre-allocated OOME message to avoid OOME when logging it 108 String oomMsg = "Ignored OOME in run()"; 109 try { 110 111 log.info("iteration " + iteration); 112 ReferenceQueue queue = new ReferenceQueue(); 113 SoftReference reference; 114 int code = iteration % TYPES_COUNT; 115 String type; 116 // Define a specific type for each thread to test 117 switch (code) { 118 case 0: 119 reference = new SoftReference(new byte[SIZE], queue); 120 type = "byte"; 121 break; 122 case 1: 123 reference = new SoftReference(new short[SIZE], queue); 124 type = "short"; 125 break; 126 case 2: 127 reference = new SoftReference(new int[SIZE], queue); 128 type = "int"; 129 break; 130 case 3: 131 reference = new SoftReference(new long[SIZE], queue); 132 type = "long"; 133 break; 134 case 4: 135 reference = new SoftReference(new char[SIZE], queue); 136 type = "char"; 137 break; 138 case 5: 139 reference = new SoftReference(new boolean[SIZE], queue); 140 type = "boolean"; 141 break; 142 case 6: 143 reference = new SoftReference(new double[SIZE], queue); 144 type = "double"; 145 break; 146 case 7: 147 reference = new SoftReference(new float[SIZE], queue); 148 type = "float"; 149 break; 150 case 8: 151 reference = new SoftReference(new Object[SIZE], queue); 152 type = "Object"; 153 break; 154 case 9: 155 reference = new SoftReference(internedStringProducer.create(SIZE), queue); 156 type = "InternedString"; 157 break; 158 default: 159 reference = new SoftReference(new NonbranchyTree(SIZE, 0.3f, SIZE), 160 queue); 161 type = "NonbranchyTree"; 162 break; 163 } 164 int initialFactor = memoryStrategy.equals(MemoryStrategy.HIGH) ? 1 : (memoryStrategy.equals(MemoryStrategy.LOW) ? 10 : 2); 165 GarbageUtils.eatMemory(getExecutionController(), garbageProducer, initialFactor , 10, 0); 166 if (!getExecutionController().continueExecution()) { 167 // we were interrrupted by stresser. just exit... 168 return; 169 } 170 Reference polledReference = null; 171 try { 172 polledReference = queue.remove(); 173 } catch (InterruptedException e) { 174 log.error("Unexpected InterruptedException during queue.remove()."); 175 setFailed(true); 176 } 177 // Check the reference and the queue 178 // The polled reference must be equal to the one enqueued to 179 // the queue 180 181 if (polledReference != reference) { 182 log.error("The original reference is not equal to polled reference."); 183 setFailed(true); 184 } 185 186 // queue.poll() once again must return null now, since there is 187 // only one reference in the queue 188 polledReference = queue.poll(); 189 if (polledReference != null) { 190 log.error("There are more than one references in the queue."); 191 setFailed(true); 192 } 193 reference.clear(); 194 } catch (OutOfMemoryError e) { 195 log.info(oomMsg); 196 } 197 iteration++; 198 } 199 } 200 } 201