1 /*
  2  * Copyright (c) 2020, 2023, 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=generational-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=generational -XX:ShenandoahGarbageThreshold=100 -Xmx100m
 49  *      gc.shenandoah.TestReferenceShortcutCycle
 50  */
 51 
 52 /* @test id=iu-100
 53  * @requires vm.gc.Shenandoah
 54  * @library /test/lib
 55  * @build jdk.test.whitebox.WhiteBox
 56  * @modules java.base
 57  * @run main jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 58  * @run main/othervm
 59  *      -Xbootclasspath/a:.
 60  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 61  *      -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGarbageThreshold=100 -Xmx100m
 62  *      gc.shenandoah.TestReferenceShortcutCycle
 63  */
 64 
 65 import java.lang.ref.PhantomReference;
 66 import java.lang.ref.Reference;
 67 import java.lang.ref.ReferenceQueue;
 68 import java.lang.ref.WeakReference;
 69 import jdk.test.whitebox.WhiteBox;
 70 
 71 public class TestReferenceShortcutCycle {
 72     private static final int NUM_ITEMS = 100000;
 73 
 74     private static final WhiteBox WB = WhiteBox.getWhiteBox();
 75 
 76     private static final class TestObject {
 77         public final int value;
 78 
 79         public TestObject(int value) {
 80             this.value = value;
 81         }
 82     }
 83 
 84     private static WeakReference[] refs;
 85 
 86     private static void setup() {
 87         refs = new WeakReference[NUM_ITEMS];
 88         for (int i = 0; i < NUM_ITEMS; i++) {
 89             refs[i] = new WeakReference<>(new TestObject(i));
 90         }
 91     }
 92 
 93     private static void fail(String msg) throws Exception {
 94         throw new RuntimeException(msg);
 95     }
 96 
 97     private static void testConcurrentCollection() throws Exception {
 98         setup();
 99         WB.concurrentGCAcquireControl();
100         try {
101             WB.concurrentGCRunToIdle();
102             WB.concurrentGCRunTo(WB.AFTER_CONCURRENT_REFERENCE_PROCESSING_STARTED);
103             for (int i = 0; i < NUM_ITEMS; i++) {
104                 if (refs[i].get() != null) {
105                     fail("resurrected referent");
106                 }
107             }
108         } finally {
109             WB.concurrentGCReleaseControl();
110         }
111     }
112     public static void main(String[] args) throws Exception {
113         testConcurrentCollection();
114     }
115 }