Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/PhantomReference/PhantomReferenceTest/PhantomReferenceTest.java
41161 views
/*1* Copyright (c) 2004, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key stress randomness26*27* @summary converted from VM Testbase gc/gctests/PhantomReference/PhantomReferenceTest.28* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, jrockit]29* VM Testbase readme:30* DESCRIPTION31* Test that verifies the PhantomReference handling in JRockit.32*33* COMMENTS34* This test was ported from JRockit test suite.35*36* @library /vmTestbase37* /test/lib38* @run main/othervm39* -XX:-UseGCOverheadLimit40* gc.gctests.PhantomReference.PhantomReferenceTest.PhantomReferenceTest41*/4243package gc.gctests.PhantomReference.PhantomReferenceTest;4445import gc.gctests.PhantomReference.PRHelper;46import gc.gctests.PhantomReference.PhantomHelper;47import java.lang.ref.ReferenceQueue;48import java.util.ArrayList;49import java.util.HashMap;50import java.util.Random;51import nsk.share.TestFailure;52import nsk.share.gc.GC;53import nsk.share.gc.GCTestBase;54import nsk.share.gc.gp.GarbageUtils;55import nsk.share.test.Stresser;5657/**58* Tests for the PhantomReference handling in JRockit.59*/60public class PhantomReferenceTest extends GCTestBase {6162/**63* Test that verifies the PhantomReference handling in JRockit.64*65* @return success if all phantom references were enqueued66*/67public final void run() {68long seed;69int minSize;70int maxSize;71int nrOfObjs;72int sleepTime;73long maxWaitTime;747576seed = runParams.getSeed();77minSize = 2048;78maxSize = 32768;79nrOfObjs = 1000;80sleepTime = 10000;81maxWaitTime = 30000;8283Random rndGenerator = new Random(seed);84long multiplier = maxSize - minSize;85ReferenceQueue rq = new ReferenceQueue();86HashMap hmHelper = new HashMap();87ArrayList alPhantomRefs = new ArrayList();8889for (int i = 0; i < nrOfObjs; i++) {90int allocationSize = ((int) (rndGenerator.nextDouble()91* multiplier)) + minSize;92byte[] tmp = new byte[allocationSize];93Integer ik = Integer.valueOf(tmp.hashCode());94if (hmHelper.containsKey(ik)) {95PhantomHelper ph = (PhantomHelper) hmHelper.get(ik);96ph.increaseHashCounter();97hmHelper.put(ik, ph);98} else {99hmHelper.put(ik, new PhantomHelper(tmp.hashCode()));100}101102PRHelper prh = new PRHelper(tmp, rq);103prh.setReferentHashCode(tmp.hashCode());104alPhantomRefs.add(prh);105prh = null;106tmp = null;107}108109Stresser stresser = new Stresser(runParams.getStressOptions());110stresser.start(0);111GarbageUtils.eatMemory(stresser);112if (!stresser.continueExecution()) {113return; //we couldn't be sure that FullGC is triggered114}115String retInfo = PhantomHelper.checkAllHashCodes(116rq, hmHelper, maxWaitTime);117if (retInfo != null) {118alPhantomRefs.clear();119hmHelper.clear();120throw new TestFailure(retInfo);121}122123// Make sure the ArrayList:s are live at the end of the test124// to make sure that the references gets enqueued.125alPhantomRefs.clear();126hmHelper.clear();127128alPhantomRefs = null;129hmHelper = null;130}131132public static void main(String[] args) {133GC.runTest(new PhantomReferenceTest(), args);134}135}136137138