Path: blob/master/test/hotspot/jtreg/gc/shenandoah/compiler/TestReferenceCAS.java
41153 views
/*1* Copyright (c) 2016, 2018, Red Hat, Inc. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 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 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/*25* Run standalone with: --add-exports java.base/jdk.internal.misc=ALL-UNNAMED --add-opens java.base/jdk.internal.misc=ALL-UNNAMED26*/2728/*29* @test TestReferenceCAS30* @summary Shenandoah reference CAS test31* @requires vm.gc.Shenandoah32* @modules java.base/jdk.internal.misc:+open33*34* @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC TestReferenceCAS35* @run main/othervm -Diters=100 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -Xint TestReferenceCAS36* @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-TieredCompilation TestReferenceCAS37* @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:TieredStopAtLevel=1 TestReferenceCAS38* @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:TieredStopAtLevel=4 TestReferenceCAS39*/4041/*42* @test TestReferenceCAS43* @summary Shenandoah reference CAS test44* @requires vm.gc.Shenandoah45* @requires vm.bits == "64"46* @modules java.base/jdk.internal.misc:+open47*48* @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops TestReferenceCAS49* @run main/othervm -Diters=100 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops -Xint TestReferenceCAS50* @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops -XX:-TieredCompilation TestReferenceCAS51* @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops -XX:TieredStopAtLevel=1 TestReferenceCAS52* @run main/othervm -Diters=20000 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive -XX:+UseShenandoahGC -XX:-UseCompressedOops -XX:TieredStopAtLevel=4 TestReferenceCAS53*/5455import java.lang.reflect.Field;5657public class TestReferenceCAS {5859static final int ITERS = Integer.getInteger("iters", 1);60static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);6162static final jdk.internal.misc.Unsafe UNSAFE;63static final long V_OFFSET;6465static {66try {67Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");68f.setAccessible(true);69UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);70} catch (Exception e) {71throw new RuntimeException("Unable to get Unsafe instance.", e);72}7374try {75Field vField = TestReferenceCAS.class.getDeclaredField("v");76V_OFFSET = UNSAFE.objectFieldOffset(vField);77} catch (Exception e) {78throw new RuntimeException(e);79}80}8182Object v;8384private static void assertEquals(boolean a, boolean b, String msg) {85if (a != b) {86throw new RuntimeException("a (" + a + ") != b (" + b + "): " + msg);87}88}8990private static void assertEquals(Object a, Object b, String msg) {91if (!a.equals(b)) {92throw new RuntimeException("a (" + a.toString() + ") != b (" + b.toString() + "): " + msg);93}94}9596public static void main(String[] args) {97TestReferenceCAS t = new TestReferenceCAS();98for (int c = 0; c < ITERS; c++) {99testAccess(t, V_OFFSET);100}101}102103static void testAccess(Object base, long offset) {104String foo = new String("foo");105String bar = new String("bar");106String baz = new String("baz");107UNSAFE.putReference(base, offset, "foo");108{109String newval = bar;110boolean r = UNSAFE.compareAndSetReference(base, offset, "foo", newval);111assertEquals(r, true, "success compareAndSet Object");112assertEquals(newval, "bar", "must not destroy newval");113Object x = UNSAFE.getReference(base, offset);114assertEquals(x, "bar", "success compareAndSet Object value");115}116117{118String newval = baz;119boolean r = UNSAFE.compareAndSetReference(base, offset, "foo", newval);120assertEquals(r, false, "failing compareAndSet Object");121assertEquals(newval, "baz", "must not destroy newval");122Object x = UNSAFE.getReference(base, offset);123assertEquals(x, "bar", "failing compareAndSet Object value");124}125126UNSAFE.putReference(base, offset, "bar");127{128String newval = foo;129Object r = UNSAFE.compareAndExchangeReference(base, offset, "bar", newval);130assertEquals(r, "bar", "success compareAndExchange Object");131assertEquals(newval, "foo", "must not destroy newval");132Object x = UNSAFE.getReference(base, offset);133assertEquals(x, "foo", "success compareAndExchange Object value");134}135136{137String newval = baz;138Object r = UNSAFE.compareAndExchangeReference(base, offset, "bar", newval);139assertEquals(r, "foo", "failing compareAndExchange Object");140assertEquals(newval, "baz", "must not destroy newval");141Object x = UNSAFE.getReference(base, offset);142assertEquals(x, "foo", "failing compareAndExchange Object value");143}144145UNSAFE.putReference(base, offset, "bar");146{147String newval = foo;148boolean success = false;149for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {150success = UNSAFE.weakCompareAndSetReference(base, offset, "bar", newval);151assertEquals(newval, "foo", "must not destroy newval");152}153assertEquals(success, true, "weakCompareAndSet Object");154Object x = UNSAFE.getReference(base, offset);155assertEquals(x, "foo", "weakCompareAndSet Object");156}157}158159}160161162