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 8003985 aoqi@0: * @summary Support Contended Annotation - JEP 142 aoqi@0: * aoqi@0: * @run main/othervm -XX:-RestrictContended Basic aoqi@0: */ aoqi@0: public class Basic { 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.getDeclaredField(field1); aoqi@0: Field f2 = klass.getDeclaredField(field2); aoqi@0: aoqi@0: if (isStatic(f1) != isStatic(f2)) { aoqi@0: return true; // these guys are in naturally disjoint locations aoqi@0: } 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 isPadded(Class klass, String field1) throws Exception { aoqi@0: Field f1 = klass.getDeclaredField(field1); aoqi@0: aoqi@0: if (isStatic(f1)) { aoqi@0: return offset(f1) > 128 + 64; aoqi@0: } aoqi@0: aoqi@0: return offset(f1) > 64; 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(Test1.class, "int1", "int2") || aoqi@0: isPadded(Test1.class, "int1") || aoqi@0: isPadded(Test1.class, "int2")) { aoqi@0: System.err.println("Test1 failed"); aoqi@0: endResult &= false; aoqi@0: } aoqi@0: aoqi@0: if (!arePaddedPairwise(Test2.class, "int1", "int2") || aoqi@0: !isPadded(Test2.class, "int1") || aoqi@0: isPadded(Test2.class, "int2")) { aoqi@0: System.err.println("Test2 failed"); aoqi@0: endResult &= false; aoqi@0: } aoqi@0: aoqi@0: if (!arePaddedPairwise(Test3.class, "int1", "int2") || aoqi@0: !isPadded(Test3.class, "int1") || aoqi@0: !isPadded(Test3.class, "int2")) { aoqi@0: System.err.println("Test3 failed"); aoqi@0: endResult &= false; aoqi@0: } aoqi@0: aoqi@0: if (arePaddedPairwise(Test4.class, "int1", "int2") || aoqi@0: !isPadded(Test4.class, "int1") || aoqi@0: !isPadded(Test4.class, "int2")) { aoqi@0: System.err.println("Test4 failed"); aoqi@0: endResult &= false; aoqi@0: } aoqi@0: aoqi@0: if (!arePaddedPairwise(Test5.class, "int1", "int2") || aoqi@0: !isPadded(Test5.class, "int1") || aoqi@0: !isPadded(Test5.class, "int2")) { aoqi@0: System.err.println("Test5 failed"); aoqi@0: endResult &= false; aoqi@0: } aoqi@0: aoqi@0: if (!arePaddedPairwise(Test6.class, "int1", "int2") || aoqi@0: !isPadded(Test6.class, "int1") || aoqi@0: !isPadded(Test6.class, "int2")) { aoqi@0: System.err.println("Test6 failed"); aoqi@0: endResult &= false; aoqi@0: } aoqi@0: aoqi@0: if (arePaddedPairwise(Test7.class, "int1", "int2") || aoqi@0: !isPadded(Test7.class, "int1") || aoqi@0: !isPadded(Test7.class, "int2")) { aoqi@0: System.err.println("Test7 failed"); aoqi@0: endResult &= false; aoqi@0: } aoqi@0: aoqi@0: if (!arePaddedPairwise(Test8.class, "int1", "int2") || aoqi@0: !isPadded(Test8.class, "int1") || aoqi@0: !isPadded(Test8.class, "int2")) { aoqi@0: System.err.println("Test8 failed"); aoqi@0: endResult &= false; aoqi@0: } aoqi@0: aoqi@0: if (!arePaddedPairwise(Test9.class, "int1", "int2") || aoqi@0: !isPadded(Test9.class, "int1") || aoqi@0: !isPadded(Test9.class, "int2")) { aoqi@0: System.err.println("Test9 failed"); aoqi@0: endResult &= false; aoqi@0: } aoqi@0: aoqi@0: if (!sameLayout(Test4.class, Test7.class)) { aoqi@0: System.err.println("Test4 and Test7 have different layouts"); aoqi@0: endResult &= false; aoqi@0: } aoqi@0: aoqi@0: if (!sameLayout(Test5.class, Test6.class)) { aoqi@0: System.err.println("Test5 and Test6 have different layouts"); aoqi@0: endResult &= false; aoqi@0: } aoqi@0: aoqi@0: if (!sameLayout(Test8.class, Test9.class)) { aoqi@0: System.err.println("Test8 and Test9 have different layouts"); 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: // ----------------------------------- INSTANCE FIELDS ----------------------------------------- aoqi@0: aoqi@0: // naturally packed aoqi@0: public static class Test1 { aoqi@0: private int int1; aoqi@0: private int int2; aoqi@0: } aoqi@0: aoqi@0: // int1 is padded aoqi@0: public static class Test2 { aoqi@0: @Contended private int int1; aoqi@0: private int int2; aoqi@0: } aoqi@0: aoqi@0: // both fields are padded aoqi@0: public static class Test3 { aoqi@0: @Contended private int int1; aoqi@0: @Contended private int int2; aoqi@0: } aoqi@0: aoqi@0: // fields are padded in the singular group aoqi@0: public static class Test4 { aoqi@0: @Contended("sameGroup") private int int1; aoqi@0: @Contended("sameGroup") private int int2; aoqi@0: } aoqi@0: aoqi@0: // fields are padded in disjoint groups aoqi@0: public static class Test5 { aoqi@0: @Contended("diffGroup1") private int int1; aoqi@0: @Contended("diffGroup2") private int int2; aoqi@0: } aoqi@0: aoqi@0: // fields are padded in disjoint groups aoqi@0: public static class Test6 { aoqi@0: @Contended private int int1; aoqi@0: @Contended("diffGroup2") private int int2; aoqi@0: } aoqi@0: aoqi@0: // fields are padded in the singular group aoqi@0: @Contended aoqi@0: public static class Test7 { aoqi@0: private int int1; aoqi@0: private int int2; aoqi@0: } aoqi@0: aoqi@0: // all fields are padded as the group, and one field is padded specifically aoqi@0: @Contended aoqi@0: public static class Test8 { aoqi@0: @Contended private int int1; aoqi@0: private int int2; aoqi@0: } aoqi@0: aoqi@0: // all fields are padded as the group, and one field is padded specifically aoqi@0: @Contended aoqi@0: public static class Test9 { aoqi@0: @Contended("group") private int int1; aoqi@0: private int int2; aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: