Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/ReferencesGC/ReferencesGC.java
41159 views
/*1* Copyright (c) 2002, 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/ReferencesGC.28* VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, quick]29*30* @library /vmTestbase31* /test/lib32* @run main/othervm33* -XX:-UseGCOverheadLimit34* gc.gctests.ReferencesGC.ReferencesGC35* -range 20036* -ratio 0.937* -t 138*/3940package gc.gctests.ReferencesGC;4142import java.lang.ref.*;43import nsk.share.TestFailure;44import nsk.share.gc.Algorithms;45import nsk.share.gc.GC;46import nsk.share.gc.ThreadedGCTest;47import nsk.share.gc.gp.GarbageProducer;48import nsk.share.gc.gp.GarbageUtils;49import nsk.share.test.ExecutionController;5051public class ReferencesGC extends ThreadedGCTest {5253static int RANGE = 256;54static float RATIO = (float) 1.0;55static int REMOVE; // Initialized in parseArgs.56static int RETAIN; // Initialized in parseArgs.5758public static void main(String[] args) {59parseArgs(args);60GC.runTest(new ReferencesGC(), args);61}6263public static void parseArgs(String[] args) {64for (int i = 0; i < args.length; i++) {65if (args[i].compareTo("-range") == 0) {66RANGE = Integer.valueOf(args[++i]).intValue();67} else if (args[i].compareTo("-ratio") == 0) {68RATIO = Float.valueOf(args[++i]).floatValue();69}70}71REMOVE = (int) (RANGE * RATIO);72RETAIN = RANGE - REMOVE;73}7475private class Worker implements Runnable {7677static final int WEAK = 0;78static final int SOFT = 1;79static final int PHANTOM = 2;80private ExecutionController stresser;81int finalizationMaxTime = 1000 * 60 * runParams.getNumberOfThreads();82ReferenceQueue refq = null; // Reinitialized each time through loop83int[] alive = null; // Reinitialized each time through loop84int[] wrong = null; // Reinitialized each time through loop85CircularLinkedList holder[] = new CircularLinkedList[RANGE];86WeakReference wr[] = new WeakReference[RANGE];87SoftReference sr[] = new SoftReference[RANGE];88PhantomReference phr[] = new PhantomReference[RANGE];89GarbageProducer gp = GarbageUtils.getArrayProducers().get(0);90int iter = 0;9192@Override93public void run() {94if (stresser == null) {95stresser = getExecutionController();96}9798while (stresser.continueExecution()) {99int totalLive = 0;100try {101refq = new ReferenceQueue();102alive = new int[3];103wrong = new int[3];104for (int j = 0; j < RANGE; j++) {105holder[j] = new CircularLinkedList();106holder[j].addNelements(300);107wr[j] = new WeakReference(holder[j], refq);108sr[j] = new SoftReference(holder[j], refq);109phr[j] = new PhantomReference(holder[j], refq);110}111} catch (OutOfMemoryError oome) {112// we should just skip the test113// the other thread could eat all memory114continue;115}116117for (int i = 0; i < RANGE; i++) {118if (wr[i].refersTo(holder[i])) {119++totalLive;120}121if (sr[i].refersTo(holder[i])) {122++totalLive;123}124if (phr[i].refersTo(holder[i])) {125++totalLive;126}127}128if (totalLive != 3 * RANGE) {129throw new TestFailure("There are " + (3 * RANGE - totalLive) + " references cleared before null-assigment.");130}131132for (int i = 0; i < REMOVE; i++) {133holder[i] = null;134}135136Algorithms.eatMemory(stresser);137if (!stresser.continueExecution()) {138break;139}140// At this point OOME was thrown and accordingly to spec141// all weak refs should be processed142143long waitTime = System.currentTimeMillis() + finalizationMaxTime;144int totalQ = 0;145while ((totalQ < (3 * REMOVE)) && (System.currentTimeMillis() < waitTime)) {146alive[WEAK] = alive[SOFT] = alive[PHANTOM] = 0;147wrong[WEAK] = wrong[SOFT] = wrong[PHANTOM] = 0;148for (int i = 0; i < RANGE; i++) {149if (!wr[i].refersTo(holder[i])) {150++wrong[WEAK];151} else if (holder[i] != null) {152++alive[WEAK];153}154155if (!sr[i].refersTo(holder[i])) {156++wrong[SOFT];157} else if (holder[i] != null) {158++alive[SOFT];159}160161if (!phr[i].refersTo(holder[i])) {162++wrong[PHANTOM];163} else if (holder[i] != null) {164++alive[PHANTOM];165}166}167168try {169while (refq.remove(100) != null) {170++totalQ;171}172} catch (InterruptedException ie) {173}174if (totalQ < (3 * REMOVE)) {175log.debug("After null-assignment to " + REMOVE +176" referent values and provoking gc found:\n\t" +177totalQ + " queued refs.");178try {179log.debug("sleeping to give reference processing more time ...");180Thread.sleep(1000);181} catch (InterruptedException ie) {182}183}184}185log.debug("iteration.... " + iter++);186if (wrong[WEAK] != 0) {187throw new TestFailure("Expected " + RETAIN + " weak references still alive: " + alive[WEAK]);188} else if (wrong[SOFT] != 0) {189throw new TestFailure("Expected " + RETAIN + " soft references still alive: " + alive[SOFT]);190} else if (wrong[PHANTOM] != 0) {191throw new TestFailure("Expected " + RETAIN + " phantom references still alive: " + alive[PHANTOM]);192} else if (totalQ != (3 * REMOVE)) {193throw new TestFailure("Expected " + (3 * REMOVE) + " references enqueued: " + totalQ);194}195}196}197}198199@Override200protected Runnable createRunnable(int i) {201return new Worker();202}203}204205206