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/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 driver jdk.test.lib.FileInstaller . .
55  * @run main/othervm gc.gctests.SoftReference.soft001.soft001 -ms low
56  */
57 
58 package gc.gctests.SoftReference.soft001;
59 
60 import java.lang.ref.Reference;
61 import java.lang.ref.ReferenceQueue;
62 import java.lang.ref.SoftReference;
63 
64 import nsk.share.gc.GC;
65 import nsk.share.gc.NonbranchyTree;
66 import nsk.share.gc.ThreadedGCTest;
67 import nsk.share.gc.gp.GarbageProducer;
68 import nsk.share.gc.gp.GarbageProducerAware;
69 import nsk.share.gc.gp.GarbageUtils;
70 import nsk.share.gc.gp.MemoryStrategy;
71 import nsk.share.gc.gp.MemoryStrategyAware;
72 import nsk.share.gc.gp.string.InternedStringProducer;
73 import nsk.share.gc.gp.string.RandomStringProducer;
74 
75 public class soft001 extends ThreadedGCTest implements GarbageProducerAware, MemoryStrategyAware {
76 
77     private GarbageProducer garbageProducer;
78     private InternedStringProducer internedStringProducer = new InternedStringProducer(new RandomStringProducer(10));
79     private MemoryStrategy memoryStrategy;
80     // Total number of types to test
81     final static int TYPES_COUNT = 11;
82     // Size of array of each tested type. The constant also specifies the
83     // number of nodes in a NonbranchyTree and size of each node
84     final static int SIZE = 100;
85 
createRunnable(int i)86     protected Runnable createRunnable(int i) {
87         return new Test();
88     }
89 
setGarbageProducer(GarbageProducer garbageProducer)90     public void setGarbageProducer(GarbageProducer garbageProducer) {
91         this.garbageProducer = garbageProducer;
92     }
93 
setMemoryStrategy(MemoryStrategy memoryStrategy)94     public void setMemoryStrategy(MemoryStrategy memoryStrategy) {
95         this.memoryStrategy = memoryStrategy;
96     }
97 
main(String[] args)98     public static void main(String[] args) {
99         GC.runTest(new soft001(), args);
100     }
101 
102     // The class implements the logic of the testcase
103     class Test implements Runnable {
104 
105         int iteration;
106 
run()107         public void run() {
108             // Pre-allocated OOME message to avoid OOME when logging it
109             String oomMsg = "Ignored OOME in run()";
110             try {
111 
112                 log.info("iteration " + iteration);
113                 ReferenceQueue queue = new ReferenceQueue();
114                 SoftReference reference;
115                 int code = iteration % TYPES_COUNT;
116                 String type;
117                 // Define a specific type for each thread to test
118                 switch (code) {
119                     case 0:
120                         reference = new SoftReference(new byte[SIZE], queue);
121                         type = "byte";
122                         break;
123                     case 1:
124                         reference = new SoftReference(new short[SIZE], queue);
125                         type = "short";
126                         break;
127                     case 2:
128                         reference = new SoftReference(new int[SIZE], queue);
129                         type = "int";
130                         break;
131                     case 3:
132                         reference = new SoftReference(new long[SIZE], queue);
133                         type = "long";
134                         break;
135                     case 4:
136                         reference = new SoftReference(new char[SIZE], queue);
137                         type = "char";
138                         break;
139                     case 5:
140                         reference = new SoftReference(new boolean[SIZE], queue);
141                         type = "boolean";
142                         break;
143                     case 6:
144                         reference = new SoftReference(new double[SIZE], queue);
145                         type = "double";
146                         break;
147                     case 7:
148                         reference = new SoftReference(new float[SIZE], queue);
149                         type = "float";
150                         break;
151                     case 8:
152                         reference = new SoftReference(new Object[SIZE], queue);
153                         type = "Object";
154                         break;
155                     case 9:
156                         reference = new SoftReference(internedStringProducer.create(SIZE), queue);
157                         type = "InternedString";
158                         break;
159                     default:
160                         reference = new SoftReference(new NonbranchyTree(SIZE, 0.3f, SIZE),
161                                 queue);
162                         type = "NonbranchyTree";
163                         break;
164                 }
165                 int initialFactor = memoryStrategy.equals(MemoryStrategy.HIGH) ? 1 : (memoryStrategy.equals(MemoryStrategy.LOW) ? 10 : 2);
166                 GarbageUtils.eatMemory(getExecutionController(), garbageProducer, initialFactor , 10, 0);
167                 if (!getExecutionController().continueExecution()) {
168                     // we were interrrupted by stresser. just exit...
169                     return;
170                 }
171                 Reference polledReference = null;
172                 try {
173                     polledReference = queue.remove();
174                 } catch (InterruptedException e) {
175                     log.error("Unexpected InterruptedException during queue.remove().");
176                     setFailed(true);
177                 }
178                 // Check the reference and the queue
179                 // The polled reference must be equal to the one enqueued to
180                 // the queue
181 
182                 if (polledReference != reference) {
183                     log.error("The original reference is not equal to polled reference.");
184                     setFailed(true);
185                 }
186 
187                 // queue.poll() once again must return null now, since there is
188                 // only one reference in the queue
189                 polledReference = queue.poll();
190                 if (polledReference != null) {
191                     log.error("There are more  than one references in the queue.");
192                     setFailed(true);
193                 }
194                 reference.clear();
195             } catch (OutOfMemoryError e) {
196                 log.info(oomMsg);
197             }
198             iteration++;
199         }
200     }
201 }
202