Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/contended/DefaultValue.java
32284 views
/*1* Copyright (c) 2013, 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*/2223import java.io.BufferedReader;24import java.io.InputStreamReader;25import java.lang.Class;26import java.lang.String;27import java.lang.System;28import java.lang.management.ManagementFactory;29import java.lang.management.RuntimeMXBean;30import java.util.ArrayList;31import java.util.List;32import java.util.concurrent.CyclicBarrier;33import java.util.regex.Matcher;34import java.util.regex.Pattern;35import java.lang.reflect.Field;36import java.lang.reflect.Modifier;37import sun.misc.Unsafe;38import sun.misc.Contended;3940/*41* @test42* @bug 801450943* @summary \@Contended: explicit default value behaves differently from the implicit value44*45* @run main/othervm -XX:-RestrictContended DefaultValue46*/47public class DefaultValue {4849private static final Unsafe U;50private static int ADDRESS_SIZE;51private static int HEADER_SIZE;5253static {54// steal Unsafe55try {56Field unsafe = Unsafe.class.getDeclaredField("theUnsafe");57unsafe.setAccessible(true);58U = (Unsafe) unsafe.get(null);59} catch (NoSuchFieldException | IllegalAccessException e) {60throw new IllegalStateException(e);61}6263// When running with CompressedOops on 64-bit platform, the address size64// reported by Unsafe is still 8, while the real reference fields are 4 bytes long.65// Try to guess the reference field size with this naive trick.66try {67long off1 = U.objectFieldOffset(CompressedOopsClass.class.getField("obj1"));68long off2 = U.objectFieldOffset(CompressedOopsClass.class.getField("obj2"));69ADDRESS_SIZE = (int) Math.abs(off2 - off1);70HEADER_SIZE = (int) Math.min(off1, off2);71} catch (NoSuchFieldException e) {72ADDRESS_SIZE = -1;73}74}7576static class CompressedOopsClass {77public Object obj1;78public Object obj2;79}8081public static boolean arePaddedPairwise(Class klass, String field1, String field2) throws Exception {82Field f1 = klass.getField(field1);83Field f2 = klass.getField(field2);8485int diff = offset(f1) - offset(f2);86if (diff < 0) {87// f1 is first88return (offset(f2) - (offset(f1) + getSize(f1))) > 64;89} else {90// f2 is first91return (offset(f1) - (offset(f2) + getSize(f2))) > 64;92}93}9495public static boolean sameLayout(Class klass1, Class klass2) throws Exception {96for (Field f1 : klass1.getDeclaredFields()) {97Field f2 = klass2.getDeclaredField(f1.getName());98if (offset(f1) != offset(f2)) {99return false;100}101}102103for (Field f2 : klass1.getDeclaredFields()) {104Field f1 = klass2.getDeclaredField(f2.getName());105if (offset(f1) != offset(f2)) {106return false;107}108}109110return true;111}112113public static boolean isStatic(Field field) {114return Modifier.isStatic(field.getModifiers());115}116117public static int offset(Field field) {118if (isStatic(field)) {119return (int) U.staticFieldOffset(field);120} else {121return (int) U.objectFieldOffset(field);122}123}124125public static int getSize(Field field) {126Class type = field.getType();127if (type == byte.class) { return 1; }128if (type == boolean.class) { return 1; }129if (type == short.class) { return 2; }130if (type == char.class) { return 2; }131if (type == int.class) { return 4; }132if (type == float.class) { return 4; }133if (type == long.class) { return 8; }134if (type == double.class) { return 8; }135return ADDRESS_SIZE;136}137138public static void main(String[] args) throws Exception {139boolean endResult = true;140141if (!arePaddedPairwise(R1.class, "int1", "int2")) {142System.err.println("R1 failed");143endResult &= false;144}145146if (!arePaddedPairwise(R2.class, "int1", "int2")) {147System.err.println("R2 failed");148endResult &= false;149}150151if (!arePaddedPairwise(R3.class, "int1", "int2")) {152System.err.println("R3 failed");153endResult &= false;154}155156System.out.println(endResult ? "Test PASSES" : "Test FAILS");157if (!endResult) {158throw new Error("Test failed");159}160}161162public static class R1 {163@Contended164public int int1;165@Contended166public int int2;167}168169public static class R2 {170@Contended("")171public int int1;172@Contended("")173public int int2;174}175176public static class R3 {177@Contended()178public int int1;179@Contended()180public int int2;181}182183}184185186187