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/PhantomReferenceTest.
29  * VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, jrockit]
30  * VM Testbase readme:
31  * DESCRIPTION
32  * Test that verifies the PhantomReference handling in JRockit.
33  *
34  * COMMENTS
35  * This test was ported from JRockit test suite.
36  *
37  * @library /vmTestbase
38  *          /test/lib
39  * @run driver jdk.test.lib.FileInstaller . .
40  * @run main/othervm
41  *      -XX:-UseGCOverheadLimit
42  *      gc.gctests.PhantomReference.PhantomReferenceTest.PhantomReferenceTest
43  */
44 
45 package gc.gctests.PhantomReference.PhantomReferenceTest;
46 
47 import gc.gctests.PhantomReference.PRHelper;
48 import gc.gctests.PhantomReference.PhantomHelper;
49 import java.lang.ref.ReferenceQueue;
50 import java.util.ArrayList;
51 import java.util.HashMap;
52 import java.util.Random;
53 import nsk.share.TestFailure;
54 import nsk.share.gc.GC;
55 import nsk.share.gc.GCTestBase;
56 import nsk.share.gc.gp.GarbageUtils;
57 import nsk.share.test.Stresser;
58 
59 /**
60  * Tests for the PhantomReference handling in JRockit.
61  */
62 public class PhantomReferenceTest extends GCTestBase {
63 
64     /**
65      * Test that verifies the PhantomReference handling in JRockit.
66      *
67      * @return success if all phantom references were enqueued
68      */
run()69     public final void run() {
70         long seed;
71         int minSize;
72         int maxSize;
73         int nrOfObjs;
74         int sleepTime;
75         long maxWaitTime;
76 
77 
78         seed = runParams.getSeed();
79         minSize = 2048;
80         maxSize = 32768;
81         nrOfObjs = 1000;
82         sleepTime = 10000;
83         maxWaitTime = 30000;
84 
85         Random rndGenerator = new Random(seed);
86         long multiplier = maxSize - minSize;
87         ReferenceQueue rq = new ReferenceQueue();
88         HashMap hmHelper = new HashMap();
89         ArrayList alPhantomRefs = new ArrayList();
90 
91         for (int i = 0; i < nrOfObjs; i++) {
92             int allocationSize = ((int) (rndGenerator.nextDouble()
93                     * multiplier)) + minSize;
94             byte[] tmp = new byte[allocationSize];
95             Integer ik = new Integer(tmp.hashCode());
96             if (hmHelper.containsKey(ik)) {
97                 PhantomHelper ph = (PhantomHelper) hmHelper.get(ik);
98                 ph.increaseHashCounter();
99                 hmHelper.put(ik, ph);
100             } else {
101                 hmHelper.put(ik, new PhantomHelper(tmp.hashCode()));
102             }
103 
104             PRHelper prh = new PRHelper(tmp, rq);
105             prh.setReferentHashCode(tmp.hashCode());
106             alPhantomRefs.add(prh);
107             prh = null;
108             tmp = null;
109         }
110 
111         Stresser stresser = new Stresser(runParams.getStressOptions());
112         stresser.start(0);
113         GarbageUtils.eatMemory(stresser);
114         if (!stresser.continueExecution()) {
115             return; //we couldn't be sure that FullGC is triggered
116         }
117         String retInfo = PhantomHelper.checkAllHashCodes(
118                 rq, hmHelper, maxWaitTime);
119         if (retInfo != null) {
120             alPhantomRefs.clear();
121             hmHelper.clear();
122             throw new TestFailure(retInfo);
123         }
124 
125         // Make sure the ArrayList:s are live at the end of the test
126         // to make sure that the references gets enqueued.
127         alPhantomRefs.clear();
128         hmHelper.clear();
129 
130         alPhantomRefs = null;
131         hmHelper = null;
132     }
133 
main(String[] args)134     public static void main(String[] args) {
135         GC.runTest(new PhantomReferenceTest(), args);
136     }
137 }
138