Path: blob/master/test/hotspot/jtreg/compiler/profiling/UnsafeAccess.java
41149 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. 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/*23* @test24* @bug 813491825* @modules java.base/jdk.internal.misc26*27* @run main/bootclasspath/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:TypeProfileLevel=222 -XX:+UseTypeSpeculation -Xbatch28* -XX:CompileCommand=dontinline,compiler.profiling.UnsafeAccess::test*29* compiler.profiling.UnsafeAccess30*/3132package compiler.profiling;3334import jdk.internal.misc.Unsafe;3536public class UnsafeAccess {37private static final Unsafe U = Unsafe.getUnsafe();3839static Class cls = Object.class;40static long off = U.ARRAY_OBJECT_BASE_OFFSET;4142static Object testUnsafeAccess(Object o, boolean isObjArray) {43if (o != null && cls.isInstance(o)) { // speculates "o" type to int[]44return helperUnsafeAccess(o, isObjArray);45}46return null;47}4849static Object helperUnsafeAccess(Object o, boolean isObjArray) {50if (isObjArray) {51U.putReference(o, off, new Object());52}53return o;54}5556static Object testUnsafeLoadStore(Object o, boolean isObjArray) {57if (o != null && cls.isInstance(o)) { // speculates "o" type to int[]58return helperUnsafeLoadStore(o, isObjArray);59}60return null;61}6263static Object helperUnsafeLoadStore(Object o, boolean isObjArray) {64if (isObjArray) {65Object o1 = U.getReference(o, off);66U.compareAndSetReference(o, off, o1, new Object());67}68return o;69}7071public static void main(String[] args) {72Object[] objArray = new Object[10];73int[] intArray = new int[10];7475for (int i = 0; i < 20_000; i++) {76helperUnsafeAccess(objArray, true);77}78for (int i = 0; i < 20_000; i++) {79testUnsafeAccess(intArray, false);80}8182for (int i = 0; i < 20_000; i++) {83helperUnsafeLoadStore(objArray, true);84}85for (int i = 0; i < 20_000; i++) {86testUnsafeLoadStore(intArray, false);87}8889System.out.println("TEST PASSED");90}91}929394