Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/WeakReferenceGC/WeakReferenceGC.java
41155 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*/222324/*25* @test26* @key randomness27*28* @summary converted from VM Testbase gc/gctests/WeakReferenceGC.29* VM Testbase keywords: [gc, nonconcurrent]30* VM Testbase readme:31* *******************************************32* set timeout = 25 when running this test33* *******************************************34* This tests checks to see if the garbage collector enqueues35* a weak reference when referrent has been turned to garbage.36* Weak references are enqueued in a non-deterministic way37* by the garbage collector, so the test uses a heuristic to38* determine whether the references are being enqueued in a timely39* manner which in turn determines whether outcome of the test.40* IF the user invokes the test with the following command line41* parameters :42* java WeakReferenceGC -qFactor 0.9 -gcCount 543* the test expects that 90% of all objects with only weak references44* pointing to them will be enqueued within 5 calls to the garbage collector.45* When I ran the test, I consistently got figures of 98% enqueueing46* with just 1 call to the garbage collector. So if this test fails,47* at its current settings, the garbage collector is not performing as well48* as it used to.49* The test creates circular linked lists of size 0.1Meg each. The number50* of lists created can be set via the -numLists flag. The default51* value is 50.52* The circular linked lists have both strong and weak references pointing53* to them. The strong and weak references are kept in arrays.54* The strong references are all nulled out and System.gc() is called55* explicitly and the heuristic is applied. If the test does not56* satisfy the heuristic or an OutOfMemory exception is thrown,57* the test fails.58* Array containing Each circular linked list Array containing59* weak references is 0.1 Meg each and has strong references60* to linked lists. a weak reference, W<n> and a to linked lists.61* strong reference, x<n>62* pointing to it.63* ---- ---------------------------- -----64* | | | ---- ---- <-| | |65* | W1 |--> -->| |---.......>| | <---- | x1 |66* | | -->| |<---.......| |<-- | |67* ---- | ---- 1000 --- | -----68* ---------------------------69* . .70* . .71* . .72* . .73* . . 1074* . .75* ---- ---------------------------- -----76* | | | ---- ---- <-| | |77* | Wn |--> -->| |---.......>| | <---- | xn |78* | | -->| |<---.......| |<-- | |79* ---- | ---- 1000 --- | -----80* ---------------------------81*82* @library /vmTestbase83* /test/lib84* @run main/othervm85* gc.gctests.WeakReferenceGC.WeakReferenceGC86* -numList 5087* -qFactor 0.988* -gcCount 589* -iter 10090*/9192package gc.gctests.WeakReferenceGC;9394import java.util.*;95import java.lang.ref.*;96import nsk.share.TestFailure;97import nsk.share.gc.GC;98import nsk.share.gc.ThreadedGCTest;99import nsk.share.gc.gp.GarbageUtils;100101public class WeakReferenceGC extends ThreadedGCTest {102103// A heuristic is used to determine the outcome(pass/fail104// status) of a test. If 90% of all objects that have105// _only_ weak references pointing to them are garbage106// collected with 5 explicit calls to the garbage collector107// the test is deemed a pass. The following two variables108// are used to define this heuristic: gcCount, qFactor109110static String args[];111112CircularLinkedList holder[];113114int loopCount = 100; // # of times test is performed115116int memory_reserve[];117int gcCount = 5;118int numLists = 50; // number of linked lists119float qFactor = (float) 0.9;120ReferenceQueue refQueue;121Vector results;122WeakReference wholder[];123124public static void main(String[] args) {125WeakReferenceGC.args = args;126GC.runTest(new WeakReferenceGC(), args);127}128129WeakReferenceGC() {130holder = new CircularLinkedList[numLists];131wholder = new WeakReference[numLists];132refQueue = new ReferenceQueue();133results = new Vector();134}135136protected Runnable createRunnable(int i) {137return i > 0 ? null : new Worker();138}139140private void dumpTestResults() {141double objectsRecovered;142143System.out.println("Percentage of Objects" + " # of GC's");144System.out.println(" Recovered " + " Required");145for (int i = 0; i < results.size(); i++) {146Statistic s = (Statistic) results.elementAt(i);147objectsRecovered = Math.rint((float) (s.numEnqueued)148/ (float) (numLists) * 100.0);149System.out.println(" " + objectsRecovered150+ " % " + s.iterations);151}152}153154private boolean hasPassed() {155boolean passed;156passed = true; // assume passed till proven otherwise157158for (int i = 0; i < results.size(); i++) {159Statistic s = (Statistic) results.elementAt(i);160if ((s.iterations > gcCount)161|| (s.numEnqueued < (int) (numLists * qFactor))) {162passed = false;163break; // test failed164}165}166return passed;167}168169private void parseTestParams(String args[]) {170for (int i = 0; i < args.length; i++) {171if (args[i].compareTo("-numList") == 0) {172numLists = Integer.valueOf(args[++i]).intValue();173} else if (args[i].compareTo("-qFactor") == 0) {174qFactor = Float.valueOf(args[++i]).floatValue();175} else if (args[i].compareTo("-gcCount") == 0) {176gcCount = Integer.valueOf(args[++i]).intValue();177} else if (args[i].compareTo("-iter") == 0) {178loopCount = Integer.valueOf(args[++i]).intValue();179// } else {180// System.err.println("usage : " +181// "java WeakReferenceGC [-numList <n>] " +182// "[-qFactor <0.x>] [-gcCount <n>] [-iter <n>]");183// throw new TestBug("Invalid arguments");184}185}186}187188private void persistentGC() {189int numEnqueued, iter, qCriterion;190191numEnqueued = 0; // number of weakReference enqueued192iter = 0;193qCriterion = (int) (numLists * qFactor);194195while ((numEnqueued < qCriterion) && (iter <= gcCount)) {196iter++;197if (!getExecutionController().continueExecution()) {198return;199}200if (GarbageUtils.eatMemory(getExecutionController()) == 0) {201return; // We were unable to provoke OOME before timeout is over202}203try {204while ((numEnqueued < numLists) &&205(refQueue.remove(1000) != null)) {206numEnqueued++;207}208} catch (InterruptedException ie) {209}210}211results.addElement((new Statistic(iter, numEnqueued)));212}213214private void runTest() {215int iter;216iter = 1;217try {218do {219for (int i = 0; i < numLists; i++) {220holder[i] = new CircularLinkedList();221holder[i].addNelements(1000);222wholder[i] = new WeakReference(holder[i], refQueue);223}224225for (int i = 0; i < numLists; i++) {226holder[i] = null;227}228229if (!getExecutionController().continueExecution()) {230return;231}232persistentGC();233234iter++;235} while (iter <= loopCount);236} catch (OutOfMemoryError e) {237memory_reserve = null;238System.gc();239throw new TestFailure("Failed at iteration=" + loopCount);240}241}242243//We can't override run() method in WeakReferenceGC, so we are carrying out it to Worker244private class Worker implements Runnable {245@Override246public void run() {247parseTestParams(args);248runTest();249dumpTestResults();250boolean passed = hasPassed();251if (passed == true) {252log.info("Test passed.");253} else {254log.error("Test failed.");255setFailed(true);256}257}258}259}260261class Statistic {262int iterations;263int numEnqueued;264265Statistic(int i, int num) {266iterations = i;267numEnqueued = num;268}269270}271272273