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  * @test TestReferenceCAS
  26  * @summary Shenandoah reference CAS test
  27  * @key gc
  28  *
  29  * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC                                                 TestReferenceCAS
  30  * @run main/othervm -Diters=100   -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -Xint                                           TestReferenceCAS
  31  * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-TieredCompilation                          TestReferenceCAS
  32  * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:TieredStopAtLevel=1                         TestReferenceCAS
  33  * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:TieredStopAtLevel=4                         TestReferenceCAS
  34  */
  35 
  36 /*
  37  * @test TestReferenceCAS
  38  * @summary Shenandoah reference CAS test
  39  * @key gc
  40  * @requires (vm.bits == "64")
  41  * @modules java.base/jdk.internal.misc:+open
  42  *
  43  * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops                          TestReferenceCAS
  44  * @run main/othervm -Diters=100   -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops -Xint                    TestReferenceCAS
  45  * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops -XX:-TieredCompilation   TestReferenceCAS
  46  * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops -XX:TieredStopAtLevel=1  TestReferenceCAS
  47  * @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops -XX:TieredStopAtLevel=4  TestReferenceCAS
  48  */
  49 
  50 import java.lang.reflect.Field;
  51 
  52 public class TestReferenceCAS {
  53 
  54     static final int ITERS = Integer.getInteger("iters", 1);
  55     static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
  56 
  57     static final sun.misc.Unsafe UNSAFE;
  58     static final long V_OFFSET;
  59 
  60     static {
  61         try {
  62             Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
  63             f.setAccessible(true);
  64             UNSAFE = (sun.misc.Unsafe) f.get(null);
  65         } catch (Exception e) {
  66             throw new RuntimeException("Unable to get Unsafe instance.", e);
  67         }
  68 
  69         try {
  70             Field vField = TestReferenceCAS.class.getDeclaredField("v");
  71             V_OFFSET = UNSAFE.objectFieldOffset(vField);
  72         } catch (Exception e) {
  73             throw new RuntimeException(e);
  74         }
  75     }
  76 
  77     Object v;
  78 
  79     private static void assertEquals(boolean a, boolean b, String msg) {
  80         if (a != b) {
  81             throw new RuntimeException("a (" + a + ") != b (" + b + "): " + msg);
  82         }
  83     }
  84 
  85     private static void assertEquals(Object a, Object b, String msg) {
  86         if (!a.equals(b)) {
  87             throw new RuntimeException("a (" + a.toString() + ") != b (" + b.toString() + "): " + msg);
  88         }
  89     }
  90 
  91     public static void main(String[] args) {
  92         TestReferenceCAS t = new TestReferenceCAS();
  93         for (int c = 0; c < ITERS; c++) {
  94             testAccess(t, V_OFFSET);
  95         }
  96     }
  97 
  98     static void testAccess(Object base, long offset) {
  99         String foo = new String("foo");
 100         String bar = new String("bar");
 101         String baz = new String("baz");
 102         UNSAFE.putObject(base, offset, "foo");
 103         {
 104             String newval = bar;
 105             boolean r = UNSAFE.compareAndSwapObject(base, offset, "foo", newval);
 106             assertEquals(r, true, "success compareAndSwap Object");
 107             assertEquals(newval, "bar", "must not destroy newval");
 108             Object x = UNSAFE.getObject(base, offset);
 109             assertEquals(x, "bar", "success compareAndSwap Object value");
 110         }
 111 
 112         {
 113             String newval = baz;
 114             boolean r = UNSAFE.compareAndSwapObject(base, offset, "foo", newval);
 115             assertEquals(r, false, "failing compareAndSwap Object");
 116             assertEquals(newval, "baz", "must not destroy newval");
 117             Object x = UNSAFE.getObject(base, offset);
 118             assertEquals(x, "bar", "failing compareAndSwap Object value");
 119         }
 120     }
 121 
 122 }