Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/shenandoah/TestRefprocSanity.java
32284 views
/*1* Copyright (c) 2018, Red Hat, Inc. All rights reserved.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation.6*7* This code is distributed in the hope that it will be useful, but WITHOUT8* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or9* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License10* version 2 for more details (a copy is included in the LICENSE file that11* accompanied this code).12*13* You should have received a copy of the GNU General Public License version14* 2 along with this work; if not, write to the Free Software Foundation,15* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.16*17* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA18* or visit www.oracle.com if you need additional information or have any19* questions.20*21*/2223/*24* @test TestRefprocSanity25* @summary Test that null references/referents work fine26* @key gc27*28* @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions29* -XX:+UseShenandoahGC30* -XX:+ShenandoahVerify31* TestRefprocSanity32*33* @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions34* -XX:+UseShenandoahGC35* TestRefprocSanity36*37* @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions38* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive39* TestRefprocSanity40*/4142/*43* @test TestRefprocSanity44* @summary Test that null references/referents work fine45* @key gc46*47* @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions48* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu49* -XX:+ShenandoahVerify50* TestRefprocSanity51*52* @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions53* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu54* TestRefprocSanity55*56* @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions57* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive58* TestRefprocSanity59*/6061import java.lang.ref.*;6263public class TestRefprocSanity {6465static final long TARGET_MB = Long.getLong("target", 10_000); // 10 Gb allocation66static final int WINDOW = 10_000;6768static final Reference<MyObject>[] refs = new Reference[WINDOW];6970public static void main(String[] args) throws Exception {71long count = TARGET_MB * 1024 * 1024 / 32;72int rIdx = 0;7374ReferenceQueue rq = new ReferenceQueue();7576for (int c = 0; c < WINDOW; c++) {77refs[c] = select(c, new MyObject(c), rq);78}7980for (int c = 0; c < count; c++) {81verifyRefAt(rIdx);82refs[rIdx] = select(c, new MyObject(rIdx), rq);8384rIdx++;85if (rIdx >= WINDOW) {86rIdx = 0;87}88while (rq.poll() != null); // drain89}90}9192static Reference<MyObject> select(int v, MyObject ext, ReferenceQueue rq) {93switch (v % 10) {94case 0: return new SoftReference<MyObject>(null);95case 1: return new SoftReference<MyObject>(null, rq);96case 2: return new SoftReference<MyObject>(ext);97case 3: return new SoftReference<MyObject>(ext, rq);98case 4: return new WeakReference<MyObject>(null);99case 5: return new WeakReference<MyObject>(null, rq);100case 6: return new WeakReference<MyObject>(ext);101case 7: return new WeakReference<MyObject>(ext, rq);102case 8: return new PhantomReference<MyObject>(null, rq);103case 9: return new PhantomReference<MyObject>(ext, rq);104default: throw new IllegalStateException();105}106}107108static void verifyRefAt(int idx) {109Reference<MyObject> ref = refs[idx];110MyObject mo = ref.get();111if (mo != null && mo.x != idx) {112throw new IllegalStateException("Referent tag is incorrect: " + mo.x + ", should be " + idx);113}114}115116static class MyObject {117final int x;118119public MyObject(int x) {120this.x = x;121}122}123124}125126127