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