Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/PhantomReference/PhantomReferenceEvilTest/PhantomReferenceEvilTest.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/PhantomReferenceEvilTest.28* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, jrockit]29* VM Testbase readme:30* DESCRIPTION31* Test that verifies the PhantomReference handling in a more evil way.32* In this test, it will only keep every Xth object, thus causing more33* fragmentation and fill the heap with unused objects. This test should34* not throw any OOME during the test execution.35*36* COMMENTS37* This test was ported from JRockit test suite.38*39* @library /vmTestbase40* /test/lib41* @run main/othervm42* -XX:-UseGCOverheadLimit43* gc.gctests.PhantomReference.PhantomReferenceEvilTest.PhantomReferenceEvilTest44*/4546package gc.gctests.PhantomReference.PhantomReferenceEvilTest;4748import gc.gctests.PhantomReference.PhantomHelper;49import gc.gctests.PhantomReference.PRHelper;50import java.lang.ref.ReferenceQueue;51import java.util.ArrayList;52import java.util.Random;53import java.util.HashMap;54import nsk.share.TestFailure;55import nsk.share.gc.GC;56import nsk.share.gc.GCTestBase;57import nsk.share.gc.Memory;58import nsk.share.gc.gp.GarbageUtils;59import nsk.share.test.Stresser;6061/**62* Tests for the PhantomReference handling in a more evil way.63*64* This test must be run with a mx value set to ensure65* Runtime.maxMemory() doesn't return 0.66*/67public class PhantomReferenceEvilTest extends GCTestBase {6869/**70* Test that verifies the PhantomReference handling in a more evil way.71* In this test, it will only keep every Xth object, thus causing more72* fragmentation and fill the heap with unused objects. This test should73* not throw any OOME during the test execution.74*75* @return success if all phantom references were enqueued76*/77public final void run() {78long seed;79int minSize;80int maxSize;81int keepEveryXthObject;82double memPercentToFill;83long maxWaitTime;84long counter = 0;85long totalMemAlloced = 0;86long memToAlloc = 0;87long nrOfPrs = 0;88Runtime r = Runtime.getRuntime();899091seed = runParams.getSeed();92minSize = 2048;93maxSize = 32768;94keepEveryXthObject = 5;95memPercentToFill = 0.45;96maxWaitTime = 30000;97memToAlloc = (long) (r.maxMemory() * memPercentToFill);98Random rndGenerator = new Random(seed);99long multiplier = maxSize - minSize;100ReferenceQueue rq = new ReferenceQueue();101HashMap hmHelper = new HashMap();102ArrayList alPhantomRefs = new ArrayList();103104try {105try {106while (totalMemAlloced + Memory.getReferenceSize()107* hmHelper.size() < memToAlloc) {108int allocationSize = ((int) (rndGenerator.nextDouble()109* multiplier)) + minSize;110byte[] tmp = new byte[allocationSize];111112if (counter % keepEveryXthObject == 0) {113Integer ik = Integer.valueOf(tmp.hashCode());114if (hmHelper.containsKey(ik)) {115PhantomHelper ph = (PhantomHelper) hmHelper.get(ik);116ph.increaseHashCounter();117hmHelper.put(ik, ph);118} else {119hmHelper.put(ik, new PhantomHelper(tmp.hashCode()));120}121122PRHelper prh = new PRHelper(tmp, rq);123prh.setReferentHashCode(tmp.hashCode());124alPhantomRefs.add(prh);125totalMemAlloced +=126Memory.getArraySize(allocationSize, Memory.getByteSize())127+ Memory.getReferenceSize()128+ Memory.getReferenceObjectSize();129130//Make sure the temporary object is dereferenced131prh = null;132nrOfPrs++;133}134135// Make sure the temporary object is dereferenced136tmp = null;137counter++;138if (counter == Long.MAX_VALUE) {139counter = 0;140}141}142} catch (OutOfMemoryError oome) {143alPhantomRefs.clear();144hmHelper.clear();145log.info(nrOfPrs + " phantom refs had been allocated when "146+ "OOME occured");147throw new TestFailure("OutOfMemoryException was thrown. This should "148+ "not happen during the execution of this test.");149}150151152Stresser stresser = new Stresser(runParams.getStressOptions());153stresser.start(0);154GarbageUtils.eatMemory(stresser);155if (!stresser.continueExecution()) {156return; //we couldn't be sure that FullGC is triggered157}158159String retInfo = PhantomHelper.checkAllHashCodes(160rq, hmHelper, maxWaitTime);161if (retInfo != null) {162alPhantomRefs.clear();163hmHelper.clear();164throw new TestFailure(retInfo);165}166167log.info(nrOfPrs + " phantom refs were allocated during the test");168} finally {169// Make sure the ArrayList:s are live at the end of the test170// to make sure that the references gets enqueued.171alPhantomRefs.clear();172hmHelper.clear();173alPhantomRefs = null;174hmHelper = null;175}176}177178public static void main(String[] args) {179GC.runTest(new PhantomReferenceEvilTest(), args);180}181}182183184