1 /*
   2  * Copyright (c) 2016, 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 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 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 /*
  25  * Run standalone with: --add-exports java.base/jdk.internal.misc=ALL-UNNAMED --add-opens java.base/jdk.internal.misc=ALL-UNNAMED
  26  */
  27 
  28 /*
  29  * @test Shenandoah reference CAS test
  30  * @modules java.base/jdk.internal.misc:+open
  31  *
  32  * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC                                                 TestReferenceCAS
  33  * @run main/othervm -Diters=100   -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -Xint                                           TestReferenceCAS
  34  * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-TieredCompilation                          TestReferenceCAS
  35  * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:TieredStopAtLevel=1                         TestReferenceCAS
  36  * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:TieredStopAtLevel=4                         TestReferenceCAS
  37  *
  38  * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops                          TestReferenceCAS
  39  * @run main/othervm -Diters=100   -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops -Xint                    TestReferenceCAS
  40  * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops -XX:-TieredCompilation   TestReferenceCAS
  41  * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops -XX:TieredStopAtLevel=1  TestReferenceCAS
  42  * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops -XX:TieredStopAtLevel=4  TestReferenceCAS
  43  */
  44 
  45 import java.lang.reflect.Field;
  46 
  47 public class TestReferenceCAS {
  48 
  49     static final int ITERS = Integer.getInteger("iters", 1);
  50     static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
  51 
  52     static final jdk.internal.misc.Unsafe UNSAFE;
  53     static final long V_OFFSET;
  54 
  55     static {
  56         try {
  57             Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
  58             f.setAccessible(true);
  59             UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);
  60         } catch (Exception e) {
  61             throw new RuntimeException("Unable to get Unsafe instance.", e);
  62         }
  63 
  64         try {
  65             Field vField = TestReferenceCAS.class.getDeclaredField("v");
  66             V_OFFSET = UNSAFE.objectFieldOffset(vField);
  67         } catch (Exception e) {
  68             throw new RuntimeException(e);
  69         }
  70     }
  71 
  72     Object v;
  73 
  74     private static void assertEquals(boolean a, boolean b, String msg) {
  75         if (a != b) {
  76             throw new RuntimeException("a (" + a + ") != b (" + b + "): " + msg);
  77         }
  78     }
  79 
  80     private static void assertEquals(Object a, Object b, String msg) {
  81         if (!a.equals(b)) {
  82             throw new RuntimeException("a (" + a.toString() + ") != b (" + b.toString() + "): " + msg);
  83         }
  84     }
  85 
  86     public static void main(String[] args) {
  87         TestReferenceCAS t = new TestReferenceCAS();
  88         for (int c = 0; c < ITERS; c++) {
  89             testAccess(t, V_OFFSET);
  90         }
  91     }
  92 
  93     static void testAccess(Object base, long offset) {
  94         String foo = new String("foo");
  95         String bar = new String("bar");
  96         String baz = new String("baz");
  97         UNSAFE.putReference(base, offset, "foo");
  98         {
  99             String newval = bar;
 100             boolean r = UNSAFE.compareAndSetReference(base, offset, "foo", newval);
 101             assertEquals(r, true, "success compareAndSet Object");
 102             assertEquals(newval, "bar", "must not destroy newval");
 103             Object x = UNSAFE.getReference(base, offset);
 104             assertEquals(x, "bar", "success compareAndSet Object value");
 105         }
 106 
 107         {
 108             String newval = baz;
 109             boolean r = UNSAFE.compareAndSetReference(base, offset, "foo", newval);
 110             assertEquals(r, false, "failing compareAndSet Object");
 111             assertEquals(newval, "baz", "must not destroy newval");
 112             Object x = UNSAFE.getReference(base, offset);
 113             assertEquals(x, "bar", "failing compareAndSet Object value");
 114         }
 115 
 116         UNSAFE.putReference(base, offset, "bar");
 117         {
 118             String newval = foo;
 119             Object r = UNSAFE.compareAndExchangeReference(base, offset, "bar", newval);
 120             assertEquals(r, "bar", "success compareAndExchange Object");
 121             assertEquals(newval, "foo", "must not destroy newval");
 122             Object x = UNSAFE.getReference(base, offset);
 123             assertEquals(x, "foo", "success compareAndExchange Object value");
 124         }
 125 
 126         {
 127             String newval = baz;
 128             Object r = UNSAFE.compareAndExchangeReference(base, offset, "bar", newval);
 129             assertEquals(r, "foo", "failing compareAndExchange Object");
 130             assertEquals(newval, "baz", "must not destroy newval");
 131             Object x = UNSAFE.getReference(base, offset);
 132             assertEquals(x, "foo", "failing compareAndExchange Object value");
 133         }
 134 
 135         UNSAFE.putReference(base, offset, "bar");
 136         {
 137             String newval = foo;
 138             boolean success = false;
 139             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 140                 success = UNSAFE.weakCompareAndSetReference(base, offset, "bar", newval);
 141                 assertEquals(newval, "foo", "must not destroy newval");
 142             }
 143             assertEquals(success, true, "weakCompareAndSet Object");
 144             Object x = UNSAFE.getReference(base, offset);
 145             assertEquals(x, "foo", "weakCompareAndSet Object");
 146         }
 147     }
 148 
 149 }