1 /* 2 * Copyright (c) 2018, Red Hat, Inc. All rights reserved. 3 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 /* 27 * @test id=default 28 * @summary Test that null references/referents work fine 29 * @requires vm.gc.Shenandoah 30 * 31 * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions 32 * -XX:+UseShenandoahGC 33 * -XX:+ShenandoahVerify 34 * TestRefprocSanity 35 * 36 * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions 37 * -XX:+UseShenandoahGC 38 * TestRefprocSanity 39 * 40 * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions 41 * -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive 42 * TestRefprocSanity 43 */ 44 45 /* 46 * @test id=generational 47 * @summary Test that null references/referents work fine 48 * @requires vm.gc.Shenandoah 49 * 50 * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions 51 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational 52 * -XX:+ShenandoahVerify 53 * TestRefprocSanity 54 * 55 * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions 56 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational 57 * TestRefprocSanity 58 */ 59 60 /* 61 * @test id=iu 62 * @summary Test that null references/referents work fine 63 * @requires vm.gc.Shenandoah 64 * 65 * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions 66 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu 67 * -XX:+ShenandoahVerify 68 * TestRefprocSanity 69 * 70 * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions 71 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu 72 * TestRefprocSanity 73 * 74 * @run main/othervm -Xmx128m -Xms128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions 75 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive 76 * TestRefprocSanity 77 */ 78 79 import java.lang.ref.*; 80 81 public class TestRefprocSanity { 82 83 static final long TARGET_MB = Long.getLong("target", 1_000); // 1 Gb allocation 84 static final int WINDOW = 1_000; 85 86 static final Reference<MyObject>[] refs = new Reference[WINDOW]; 87 88 static Object sink; 89 90 public static void main(String[] args) throws Exception { 91 long count = TARGET_MB * 1024 * 1024 / 128; 92 int rIdx = 0; 93 94 ReferenceQueue rq = new ReferenceQueue(); 95 96 for (int c = 0; c < WINDOW; c++) { 97 refs[c] = select(c, new MyObject(c), rq); 98 } 99 100 for (int c = 0; c < count; c++) { 101 verifyRefAt(rIdx); 102 refs[rIdx] = select(c, new MyObject(rIdx), rq); 103 104 rIdx++; 105 if (rIdx >= WINDOW) { 106 rIdx = 0; 107 } 108 109 // Do allocations to force GCs 110 sink = new byte[100]; 111 112 // Drain the refqueue 113 while (rq.poll() != null); 114 } 115 } 116 117 static Reference<MyObject> select(int v, MyObject ext, ReferenceQueue rq) { 118 switch (v % 10) { 119 case 0: return new SoftReference<MyObject>(null); 120 case 1: return new SoftReference<MyObject>(null, rq); 121 case 2: return new SoftReference<MyObject>(ext); 122 case 3: return new SoftReference<MyObject>(ext, rq); 123 case 4: return new WeakReference<MyObject>(null); 124 case 5: return new WeakReference<MyObject>(null, rq); 125 case 6: return new WeakReference<MyObject>(ext); 126 case 7: return new WeakReference<MyObject>(ext, rq); 127 case 8: return new PhantomReference<MyObject>(null, rq); 128 case 9: return new PhantomReference<MyObject>(ext, rq); 129 default: throw new IllegalStateException(); 130 } 131 } 132 133 static void verifyRefAt(int idx) { 134 Reference<MyObject> ref = refs[idx]; 135 MyObject mo = ref.get(); 136 if (mo != null && mo.x != idx) { 137 throw new IllegalStateException("Referent tag is incorrect: " + mo.x + ", should be " + idx); 138 } 139 } 140 141 static class MyObject { 142 final int x; 143 144 public MyObject(int x) { 145 this.x = x; 146 } 147 } 148 149 }