1 /*
  2  * Copyright (c) 2020, 2022, 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 package gc.shenandoah;
 25 
 26 /* @test id=satb-100
 27  * @requires vm.gc.Shenandoah
 28  * @library /test/lib
 29  * @build jdk.test.whitebox.WhiteBox
 30  * @modules java.base
 31  * @run main jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 32  * @run main/othervm
 33  *      -Xbootclasspath/a:.
 34  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 35  *      -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=satb -XX:ShenandoahGarbageThreshold=100 -Xmx100m
 36  *      gc.shenandoah.TestReferenceShortcutCycle
 37  */
 38 
 39 /* @test id=iu-100
 40  * @requires vm.gc.Shenandoah
 41  * @library /test/lib
 42  * @build jdk.test.whitebox.WhiteBox
 43  * @modules java.base
 44  * @run main jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 45  * @run main/othervm
 46  *      -Xbootclasspath/a:.
 47  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 48  *      -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGarbageThreshold=100 -Xmx100m
 49  *      gc.shenandoah.TestReferenceShortcutCycle
 50  */
 51 
 52 import java.lang.ref.PhantomReference;
 53 import java.lang.ref.Reference;
 54 import java.lang.ref.ReferenceQueue;
 55 import java.lang.ref.WeakReference;
 56 import jdk.test.whitebox.WhiteBox;
 57 
 58 public class TestReferenceShortcutCycle {
 59     private static final int NUM_ITEMS = 100000;
 60 
 61     private static final WhiteBox WB = WhiteBox.getWhiteBox();
 62 
 63     private static final class TestObject {
 64         public final int value;
 65 
 66         public TestObject(int value) {
 67             this.value = value;
 68         }
 69     }
 70 
 71     private static WeakReference[] refs;
 72 
 73     private static void setup() {
 74         refs = new WeakReference[NUM_ITEMS];
 75         for (int i = 0; i < NUM_ITEMS; i++) {
 76             refs[i] = new WeakReference<>(new TestObject(i));
 77         }
 78     }
 79 
 80     private static void fail(String msg) throws Exception {
 81         throw new RuntimeException(msg);
 82     }
 83 
 84     private static void testConcurrentCollection() throws Exception {
 85         setup();
 86         WB.concurrentGCAcquireControl();
 87         try {
 88             WB.concurrentGCRunToIdle();
 89             WB.concurrentGCRunTo(WB.AFTER_CONCURRENT_REFERENCE_PROCESSING_STARTED);
 90             for (int i = 0; i < NUM_ITEMS; i++) {
 91                 if (refs[i].get() != null) {
 92                     fail("resurrected referent");
 93                 }
 94             }
 95         } finally {
 96             WB.concurrentGCReleaseControl();
 97         }
 98     }
 99     public static void main(String[] args) throws Exception {
100         testConcurrentCollection();
101     }
102 }