1 /* 2 * Copyright (c) 2016, 2018, Red Hat, Inc. 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 25 /* 26 * Run standalone with: --add-exports java.base/jdk.internal.misc=ALL-UNNAMED --add-opens java.base/jdk.internal.misc=ALL-UNNAMED 27 */ 28 29 /* 30 * @test TestReferenceCAS 31 * @summary Shenandoah reference CAS test 32 * @requires vm.gc.Shenandoah & !vm.graal.enabled 33 * @modules java.base/jdk.internal.misc:+open 34 * 35 * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC TestReferenceCAS 36 * @run main/othervm -Diters=100 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -Xint TestReferenceCAS 37 * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-TieredCompilation TestReferenceCAS 38 * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:TieredStopAtLevel=1 TestReferenceCAS 39 * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:TieredStopAtLevel=4 TestReferenceCAS 40 */ 41 42 /* 43 * @test TestReferenceCAS 44 * @summary Shenandoah reference CAS test 45 * @requires vm.gc.Shenandoah & !vm.graal.enabled & (vm.bits == "64") 46 * @modules java.base/jdk.internal.misc:+open 47 * 48 * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops TestReferenceCAS 49 * @run main/othervm -Diters=100 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops -Xint TestReferenceCAS 50 * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops -XX:-TieredCompilation TestReferenceCAS 51 * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops -XX:TieredStopAtLevel=1 TestReferenceCAS 52 * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops -XX:TieredStopAtLevel=4 TestReferenceCAS 53 */ 54 55 import java.lang.reflect.Field; 56 57 public class TestReferenceCAS { 58 59 static final int ITERS = Integer.getInteger("iters", 1); 60 static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10); 61 62 static final jdk.internal.misc.Unsafe UNSAFE; 63 static final long V_OFFSET; 64 65 static { 66 try { 67 Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe"); 68 f.setAccessible(true); 69 UNSAFE = (jdk.internal.misc.Unsafe) f.get(null); 70 } catch (Exception e) { 71 throw new RuntimeException("Unable to get Unsafe instance.", e); 72 } 73 74 try { 75 Field vField = TestReferenceCAS.class.getDeclaredField("v"); 76 V_OFFSET = UNSAFE.objectFieldOffset(vField); 77 } catch (Exception e) { 78 throw new RuntimeException(e); 79 } 80 } 81 82 Object v; 83 84 private static void assertEquals(boolean a, boolean b, String msg) { 85 if (a != b) { 86 throw new RuntimeException("a (" + a + ") != b (" + b + "): " + msg); 87 } 88 } 89 90 private static void assertEquals(Object a, Object b, String msg) { 91 if (!a.equals(b)) { 92 throw new RuntimeException("a (" + a.toString() + ") != b (" + b.toString() + "): " + msg); 93 } 94 } 95 96 public static void main(String[] args) { 97 TestReferenceCAS t = new TestReferenceCAS(); 98 for (int c = 0; c < ITERS; c++) { 99 testAccess(t, V_OFFSET); 100 } 101 } 102 103 static void testAccess(Object base, long offset) { 104 String foo = new String("foo"); 105 String bar = new String("bar"); 106 String baz = new String("baz"); 107 UNSAFE.putReference(base, offset, "foo"); 108 { 109 String newval = bar; 110 boolean r = UNSAFE.compareAndSetReference(base, offset, "foo", newval); 111 assertEquals(r, true, "success compareAndSet Object"); 112 assertEquals(newval, "bar", "must not destroy newval"); 113 Object x = UNSAFE.getReference(base, offset); 114 assertEquals(x, "bar", "success compareAndSet Object value"); 115 } 116 117 { 118 String newval = baz; 119 boolean r = UNSAFE.compareAndSetReference(base, offset, "foo", newval); 120 assertEquals(r, false, "failing compareAndSet Object"); 121 assertEquals(newval, "baz", "must not destroy newval"); 122 Object x = UNSAFE.getReference(base, offset); 123 assertEquals(x, "bar", "failing compareAndSet Object value"); 124 } 125 126 UNSAFE.putReference(base, offset, "bar"); 127 { 128 String newval = foo; 129 Object r = UNSAFE.compareAndExchangeReference(base, offset, "bar", newval); 130 assertEquals(r, "bar", "success compareAndExchange Object"); 131 assertEquals(newval, "foo", "must not destroy newval"); 132 Object x = UNSAFE.getReference(base, offset); 133 assertEquals(x, "foo", "success compareAndExchange Object value"); 134 } 135 136 { 137 String newval = baz; 138 Object r = UNSAFE.compareAndExchangeReference(base, offset, "bar", newval); 139 assertEquals(r, "foo", "failing compareAndExchange Object"); 140 assertEquals(newval, "baz", "must not destroy newval"); 141 Object x = UNSAFE.getReference(base, offset); 142 assertEquals(x, "foo", "failing compareAndExchange Object value"); 143 } 144 145 UNSAFE.putReference(base, offset, "bar"); 146 { 147 String newval = foo; 148 boolean success = false; 149 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 150 success = UNSAFE.weakCompareAndSetReference(base, offset, "bar", newval); 151 assertEquals(newval, "foo", "must not destroy newval"); 152 } 153 assertEquals(success, true, "weakCompareAndSet Object"); 154 Object x = UNSAFE.getReference(base, offset); 155 assertEquals(x, "foo", "weakCompareAndSet Object"); 156 } 157 } 158 159 }