Path: blob/master/test/hotspot/jtreg/gc/shenandoah/TestRefprocSanity.java
40942 views
/*1* Copyright (c) 2018, Red Hat, Inc. 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*22*/2324/*25* @test TestRefprocSanity26* @summary Test that null references/referents work fine27* @requires vm.gc.Shenandoah28*29* @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions30* -XX:+UseShenandoahGC31* -XX:+ShenandoahVerify32* TestRefprocSanity33*34* @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions35* -XX:+UseShenandoahGC36* TestRefprocSanity37*38* @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions39* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive40* TestRefprocSanity41*/4243/*44* @test TestRefprocSanity45* @summary Test that null references/referents work fine46* @requires vm.gc.Shenandoah47*48* @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions49* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu50* -XX:+ShenandoahVerify51* TestRefprocSanity52*53* @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions54* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu55* TestRefprocSanity56*57* @run main/othervm -Xmx1g -Xms1g -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions58* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive59* TestRefprocSanity60*/6162import java.lang.ref.*;6364public class TestRefprocSanity {6566static final long TARGET_MB = Long.getLong("target", 10_000); // 10 Gb allocation67static final int WINDOW = 10_000;6869static final Reference<MyObject>[] refs = new Reference[WINDOW];7071public static void main(String[] args) throws Exception {72long count = TARGET_MB * 1024 * 1024 / 32;73int rIdx = 0;7475ReferenceQueue rq = new ReferenceQueue();7677for (int c = 0; c < WINDOW; c++) {78refs[c] = select(c, new MyObject(c), rq);79}8081for (int c = 0; c < count; c++) {82verifyRefAt(rIdx);83refs[rIdx] = select(c, new MyObject(rIdx), rq);8485rIdx++;86if (rIdx >= WINDOW) {87rIdx = 0;88}89while (rq.poll() != null); // drain90}91}9293static Reference<MyObject> select(int v, MyObject ext, ReferenceQueue rq) {94switch (v % 10) {95case 0: return new SoftReference<MyObject>(null);96case 1: return new SoftReference<MyObject>(null, rq);97case 2: return new SoftReference<MyObject>(ext);98case 3: return new SoftReference<MyObject>(ext, rq);99case 4: return new WeakReference<MyObject>(null);100case 5: return new WeakReference<MyObject>(null, rq);101case 6: return new WeakReference<MyObject>(ext);102case 7: return new WeakReference<MyObject>(ext, rq);103case 8: return new PhantomReference<MyObject>(null, rq);104case 9: return new PhantomReference<MyObject>(ext, rq);105default: throw new IllegalStateException();106}107}108109static void verifyRefAt(int idx) {110Reference<MyObject> ref = refs[idx];111MyObject mo = ref.get();112if (mo != null && mo.x != idx) {113throw new IllegalStateException("Referent tag is incorrect: " + mo.x + ", should be " + idx);114}115}116117static class MyObject {118final int x;119120public MyObject(int x) {121this.x = x;122}123}124125}126127128