< prev index next >

test/hotspot/jtreg/gc/shenandoah/TestReferenceRefersToShenandoah.java

Print this page

  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  */

 31  * @run main/othervm
 32  *      -Xbootclasspath/a:.
 33  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 34  *      -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=satb
 35  *      gc.shenandoah.TestReferenceRefersToShenandoah
 36  */
 37 
 38 /* @test id=satb-100
 39  * @requires vm.gc.Shenandoah
 40  * @library /test/lib
 41  * @build jdk.test.whitebox.WhiteBox
 42  * @modules java.base
 43  * @run main jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 44  * @run main/othervm
 45  *      -Xbootclasspath/a:.
 46  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 47  *      -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=satb -XX:ShenandoahGarbageThreshold=100 -Xmx100m
 48  *      gc.shenandoah.TestReferenceRefersToShenandoah
 49  */
 50 

























 51 import java.lang.ref.PhantomReference;
 52 import java.lang.ref.Reference;
 53 import java.lang.ref.ReferenceQueue;
 54 import java.lang.ref.WeakReference;
 55 import jdk.test.whitebox.WhiteBox;
 56 
 57 public class TestReferenceRefersToShenandoah {
 58     private static final WhiteBox WB = WhiteBox.getWhiteBox();
 59 
 60     private static final class TestObject {
 61         public final int value;
 62 
 63         public TestObject(int value) {
 64             this.value = value;
 65         }
 66     }
 67 
 68     private static volatile TestObject testObjectNone = null;
 69     private static volatile TestObject testObject1 = null;
 70     private static volatile TestObject testObject2 = null;

 82     private static void setup() {
 83         testObjectNone = new TestObject(0);
 84         testObject1 = new TestObject(1);
 85         testObject2 = new TestObject(2);
 86         testObject3 = new TestObject(3);
 87         testObject4 = new TestObject(4);
 88 
 89         queue = new ReferenceQueue<TestObject>();
 90 
 91         testPhantom1 = new PhantomReference<TestObject>(testObject1, queue);
 92 
 93         testWeak2 = new WeakReference<TestObject>(testObject2, queue);
 94         testWeak3 = new WeakReference<TestObject>(testObject3, queue);
 95         testWeak4 = new WeakReference<TestObject>(testObject4, queue);
 96     }
 97 
 98     private static void gcUntilOld(Object o) throws Exception {
 99         if (!WB.isObjectInOldGen(o)) {
100             WB.fullGC();
101             if (!WB.isObjectInOldGen(o)) {
102                 fail("object not promoted by full gc");




103             }
104         }
105     }
106 
107     private static void gcUntilOld() throws Exception {
108         gcUntilOld(testObjectNone);
109         gcUntilOld(testObject1);
110         gcUntilOld(testObject2);
111         gcUntilOld(testObject3);
112         gcUntilOld(testObject4);
113 
114         gcUntilOld(testPhantom1);
115 
116         gcUntilOld(testWeak2);
117         gcUntilOld(testWeak3);
118         gcUntilOld(testWeak4);
119     }
120 
121     private static void progress(String msg) {
122         System.out.println(msg);
123     }
124 
125     private static void fail(String msg) throws Exception {
126         throw new RuntimeException(msg);
127     }
128 




129     private static void expectCleared(Reference<TestObject> ref,
130                                       String which) throws Exception {
131         expectNotValue(ref, testObjectNone, which);
132         if (!ref.refersTo(null)) {
133             fail("expected " + which + " to be cleared");
134         }
135     }
136 
137     private static void expectNotCleared(Reference<TestObject> ref,
138                                          String which) throws Exception {
139         expectNotValue(ref, testObjectNone, which);
140         if (ref.refersTo(null)) {
141             fail("expected " + which + " to not be cleared");
142         }
143     }
144 
145     private static void expectValue(Reference<TestObject> ref,
146                                     TestObject value,
147                                     String which) throws Exception {
148         expectNotValue(ref, testObjectNone, which);

  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  */

 31  * @run main/othervm
 32  *      -Xbootclasspath/a:.
 33  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 34  *      -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=satb
 35  *      gc.shenandoah.TestReferenceRefersToShenandoah
 36  */
 37 
 38 /* @test id=satb-100
 39  * @requires vm.gc.Shenandoah
 40  * @library /test/lib
 41  * @build jdk.test.whitebox.WhiteBox
 42  * @modules java.base
 43  * @run main jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 44  * @run main/othervm
 45  *      -Xbootclasspath/a:.
 46  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 47  *      -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=satb -XX:ShenandoahGarbageThreshold=100 -Xmx100m
 48  *      gc.shenandoah.TestReferenceRefersToShenandoah
 49  */
 50 
 51 /* @test id=generational
 52  * @requires vm.gc.Shenandoah
 53  * @library /test/lib
 54  * @build jdk.test.whitebox.WhiteBox
 55  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 56  * @run main/othervm
 57  *      -Xbootclasspath/a:.
 58  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 59  *      -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational
 60  *      gc.shenandoah.TestReferenceRefersToShenandoah
 61  */
 62 
 63 /* @test id=generational-100
 64  * @requires vm.gc.Shenandoah
 65  * @library /test/lib
 66  * @build jdk.test.whitebox.WhiteBox
 67  * @modules java.base
 68  * @run main jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 69  * @run main/othervm
 70  *      -Xbootclasspath/a:.
 71  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 72  *      -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational -XX:ShenandoahGarbageThreshold=100 -Xmx100m
 73  *      gc.shenandoah.TestReferenceRefersToShenandoah
 74  */
 75 
 76 import java.lang.ref.PhantomReference;
 77 import java.lang.ref.Reference;
 78 import java.lang.ref.ReferenceQueue;
 79 import java.lang.ref.WeakReference;
 80 import jdk.test.whitebox.WhiteBox;
 81 
 82 public class TestReferenceRefersToShenandoah {
 83     private static final WhiteBox WB = WhiteBox.getWhiteBox();
 84 
 85     private static final class TestObject {
 86         public final int value;
 87 
 88         public TestObject(int value) {
 89             this.value = value;
 90         }
 91     }
 92 
 93     private static volatile TestObject testObjectNone = null;
 94     private static volatile TestObject testObject1 = null;
 95     private static volatile TestObject testObject2 = null;

107     private static void setup() {
108         testObjectNone = new TestObject(0);
109         testObject1 = new TestObject(1);
110         testObject2 = new TestObject(2);
111         testObject3 = new TestObject(3);
112         testObject4 = new TestObject(4);
113 
114         queue = new ReferenceQueue<TestObject>();
115 
116         testPhantom1 = new PhantomReference<TestObject>(testObject1, queue);
117 
118         testWeak2 = new WeakReference<TestObject>(testObject2, queue);
119         testWeak3 = new WeakReference<TestObject>(testObject3, queue);
120         testWeak4 = new WeakReference<TestObject>(testObject4, queue);
121     }
122 
123     private static void gcUntilOld(Object o) throws Exception {
124         if (!WB.isObjectInOldGen(o)) {
125             WB.fullGC();
126             if (!WB.isObjectInOldGen(o)) {
127                 // This is just a warning, because failing would
128                 // be overspecifying for generational shenandoah,
129                 // which need not necessarily promote objects upon
130                 // a full GC.
131                 warn("object not promoted by full gc");
132             }
133         }
134     }
135 
136     private static void gcUntilOld() throws Exception {
137         gcUntilOld(testObjectNone);
138         gcUntilOld(testObject1);
139         gcUntilOld(testObject2);
140         gcUntilOld(testObject3);
141         gcUntilOld(testObject4);
142 
143         gcUntilOld(testPhantom1);
144 
145         gcUntilOld(testWeak2);
146         gcUntilOld(testWeak3);
147         gcUntilOld(testWeak4);
148     }
149 
150     private static void progress(String msg) {
151         System.out.println(msg);
152     }
153 
154     private static void fail(String msg) throws Exception {
155         throw new RuntimeException(msg);
156     }
157 
158     private static void warn(String msg) {
159         System.out.println("Warning: " + msg);
160     }
161 
162     private static void expectCleared(Reference<TestObject> ref,
163                                       String which) throws Exception {
164         expectNotValue(ref, testObjectNone, which);
165         if (!ref.refersTo(null)) {
166             fail("expected " + which + " to be cleared");
167         }
168     }
169 
170     private static void expectNotCleared(Reference<TestObject> ref,
171                                          String which) throws Exception {
172         expectNotValue(ref, testObjectNone, which);
173         if (ref.refersTo(null)) {
174             fail("expected " + which + " to not be cleared");
175         }
176     }
177 
178     private static void expectValue(Reference<TestObject> ref,
179                                     TestObject value,
180                                     String which) throws Exception {
181         expectNotValue(ref, testObjectNone, which);
< prev index next >