aoqi@0: /* aoqi@0: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: import java.io.BufferedReader; aoqi@0: import java.io.InputStreamReader; aoqi@0: import java.lang.Class; aoqi@0: import java.lang.String; aoqi@0: import java.lang.System; aoqi@0: import java.lang.management.ManagementFactory; aoqi@0: import java.lang.management.RuntimeMXBean; aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.List; aoqi@0: import java.util.concurrent.CyclicBarrier; aoqi@0: import java.util.regex.Matcher; aoqi@0: import java.util.regex.Pattern; aoqi@0: import java.lang.reflect.Field; aoqi@0: import java.lang.reflect.Modifier; aoqi@0: import sun.misc.Unsafe; aoqi@0: import sun.misc.Contended; aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 8012939 aoqi@0: * @summary \@Contended doesn't work correctly with inheritance aoqi@0: * aoqi@0: * @run main/othervm -XX:-RestrictContended Inheritance1 aoqi@0: */ aoqi@0: public class Inheritance1 { aoqi@0: aoqi@0: private static final Unsafe U; aoqi@0: private static int ADDRESS_SIZE; aoqi@0: private static int HEADER_SIZE; aoqi@0: aoqi@0: static { aoqi@0: // steal Unsafe aoqi@0: try { aoqi@0: Field unsafe = Unsafe.class.getDeclaredField("theUnsafe"); aoqi@0: unsafe.setAccessible(true); aoqi@0: U = (Unsafe) unsafe.get(null); aoqi@0: } catch (NoSuchFieldException | IllegalAccessException e) { aoqi@0: throw new IllegalStateException(e); aoqi@0: } aoqi@0: aoqi@0: // When running with CompressedOops on 64-bit platform, the address size aoqi@0: // reported by Unsafe is still 8, while the real reference fields are 4 bytes long. aoqi@0: // Try to guess the reference field size with this naive trick. aoqi@0: try { aoqi@0: long off1 = U.objectFieldOffset(CompressedOopsClass.class.getField("obj1")); aoqi@0: long off2 = U.objectFieldOffset(CompressedOopsClass.class.getField("obj2")); aoqi@0: ADDRESS_SIZE = (int) Math.abs(off2 - off1); aoqi@0: HEADER_SIZE = (int) Math.min(off1, off2); aoqi@0: } catch (NoSuchFieldException e) { aoqi@0: ADDRESS_SIZE = -1; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static class CompressedOopsClass { aoqi@0: public Object obj1; aoqi@0: public Object obj2; aoqi@0: } aoqi@0: aoqi@0: public static boolean arePaddedPairwise(Class klass, String field1, String field2) throws Exception { aoqi@0: Field f1 = klass.getField(field1); aoqi@0: Field f2 = klass.getField(field2); aoqi@0: aoqi@0: int diff = offset(f1) - offset(f2); aoqi@0: if (diff < 0) { aoqi@0: // f1 is first aoqi@0: return (offset(f2) - (offset(f1) + getSize(f1))) > 64; aoqi@0: } else { aoqi@0: // f2 is first aoqi@0: return (offset(f1) - (offset(f2) + getSize(f2))) > 64; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public static boolean sameLayout(Class klass1, Class klass2) throws Exception { aoqi@0: for (Field f1 : klass1.getDeclaredFields()) { aoqi@0: Field f2 = klass2.getDeclaredField(f1.getName()); aoqi@0: if (offset(f1) != offset(f2)) { aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: for (Field f2 : klass1.getDeclaredFields()) { aoqi@0: Field f1 = klass2.getDeclaredField(f2.getName()); aoqi@0: if (offset(f1) != offset(f2)) { aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: public static boolean isStatic(Field field) { aoqi@0: return Modifier.isStatic(field.getModifiers()); aoqi@0: } aoqi@0: aoqi@0: public static int offset(Field field) { aoqi@0: if (isStatic(field)) { aoqi@0: return (int) U.staticFieldOffset(field); aoqi@0: } else { aoqi@0: return (int) U.objectFieldOffset(field); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public static int getSize(Field field) { aoqi@0: Class type = field.getType(); aoqi@0: if (type == byte.class) { return 1; } aoqi@0: if (type == boolean.class) { return 1; } aoqi@0: if (type == short.class) { return 2; } aoqi@0: if (type == char.class) { return 2; } aoqi@0: if (type == int.class) { return 4; } aoqi@0: if (type == float.class) { return 4; } aoqi@0: if (type == long.class) { return 8; } aoqi@0: if (type == double.class) { return 8; } aoqi@0: return ADDRESS_SIZE; aoqi@0: } aoqi@0: aoqi@0: public static void main(String[] args) throws Exception { aoqi@0: boolean endResult = true; aoqi@0: aoqi@0: // --------------- INSTANCE FIELDS --------------------- aoqi@0: aoqi@0: if (!arePaddedPairwise(A2_R1.class, "int1", "int2")) { aoqi@0: System.err.println("A2_R1 failed"); aoqi@0: endResult &= false; aoqi@0: } aoqi@0: aoqi@0: if (!arePaddedPairwise(A3_R1.class, "int1", "int2")) { aoqi@0: System.err.println("A3_R1 failed"); aoqi@0: endResult &= false; aoqi@0: } aoqi@0: aoqi@0: if (!arePaddedPairwise(A1_R2.class, "int1", "int2")) { aoqi@0: System.err.println("A1_R2 failed"); aoqi@0: endResult &= false; aoqi@0: } aoqi@0: aoqi@0: if (!arePaddedPairwise(A2_R2.class, "int1", "int2")) { aoqi@0: System.err.println("A2_R2 failed"); aoqi@0: endResult &= false; aoqi@0: } aoqi@0: aoqi@0: if (!arePaddedPairwise(A3_R2.class, "int1", "int2")) { aoqi@0: System.err.println("A3_R2 failed"); aoqi@0: endResult &= false; aoqi@0: } aoqi@0: aoqi@0: if (!arePaddedPairwise(A1_R3.class, "int1", "int2")) { aoqi@0: System.err.println("A1_R3 failed"); aoqi@0: endResult &= false; aoqi@0: } aoqi@0: aoqi@0: if (!arePaddedPairwise(A2_R3.class, "int1", "int2")) { aoqi@0: System.err.println("A2_R3 failed"); aoqi@0: endResult &= false; aoqi@0: } aoqi@0: aoqi@0: if (!arePaddedPairwise(A3_R3.class, "int1", "int2")) { aoqi@0: System.err.println("A3_R3 failed"); aoqi@0: endResult &= false; aoqi@0: } aoqi@0: aoqi@0: System.out.println(endResult ? "Test PASSES" : "Test FAILS"); aoqi@0: if (!endResult) { aoqi@0: throw new Error("Test failed"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public static class R1 { aoqi@0: public int int1; aoqi@0: } aoqi@0: aoqi@0: public static class R2 { aoqi@0: @Contended aoqi@0: public int int1; aoqi@0: } aoqi@0: aoqi@0: @Contended aoqi@0: public static class R3 { aoqi@0: public int int1; aoqi@0: } aoqi@0: aoqi@0: public static class A1_R1 extends R1 { aoqi@0: public int int2; aoqi@0: } aoqi@0: aoqi@0: public static class A2_R1 extends R1 { aoqi@0: @Contended aoqi@0: public int int2; aoqi@0: } aoqi@0: aoqi@0: @Contended aoqi@0: public static class A3_R1 extends R1 { aoqi@0: public int int2; aoqi@0: } aoqi@0: aoqi@0: public static class A1_R2 extends R2 { aoqi@0: public int int2; aoqi@0: } aoqi@0: aoqi@0: public static class A2_R2 extends R2 { aoqi@0: @Contended aoqi@0: public int int2; aoqi@0: } aoqi@0: aoqi@0: @Contended aoqi@0: public static class A3_R2 extends R2 { aoqi@0: public int int2; aoqi@0: } aoqi@0: aoqi@0: public static class A1_R3 extends R3 { aoqi@0: public int int2; aoqi@0: } aoqi@0: aoqi@0: public static class A2_R3 extends R3 { aoqi@0: @Contended aoqi@0: public int int2; aoqi@0: } aoqi@0: aoqi@0: @Contended aoqi@0: public static class A3_R3 extends R3 { aoqi@0: public int int2; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: } aoqi@0: