Mon, 12 May 2014 06:17:05 -0700
8042804: Support invoking Hotspot tests from top level
Reviewed-by: sla, erikj
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | import java.io.BufferedReader; |
aoqi@0 | 25 | import java.io.InputStreamReader; |
aoqi@0 | 26 | import java.lang.Class; |
aoqi@0 | 27 | import java.lang.String; |
aoqi@0 | 28 | import java.lang.System; |
aoqi@0 | 29 | import java.lang.management.ManagementFactory; |
aoqi@0 | 30 | import java.lang.management.RuntimeMXBean; |
aoqi@0 | 31 | import java.util.ArrayList; |
aoqi@0 | 32 | import java.util.List; |
aoqi@0 | 33 | import java.util.concurrent.CyclicBarrier; |
aoqi@0 | 34 | import java.util.regex.Matcher; |
aoqi@0 | 35 | import java.util.regex.Pattern; |
aoqi@0 | 36 | import java.lang.reflect.Field; |
aoqi@0 | 37 | import java.lang.reflect.Modifier; |
aoqi@0 | 38 | import sun.misc.Unsafe; |
aoqi@0 | 39 | import sun.misc.Contended; |
aoqi@0 | 40 | |
aoqi@0 | 41 | /* |
aoqi@0 | 42 | * @test |
aoqi@0 | 43 | * @bug 8014509 |
aoqi@0 | 44 | * @summary \@Contended: explicit default value behaves differently from the implicit value |
aoqi@0 | 45 | * |
aoqi@0 | 46 | * @run main/othervm -XX:-RestrictContended DefaultValue |
aoqi@0 | 47 | */ |
aoqi@0 | 48 | public class DefaultValue { |
aoqi@0 | 49 | |
aoqi@0 | 50 | private static final Unsafe U; |
aoqi@0 | 51 | private static int ADDRESS_SIZE; |
aoqi@0 | 52 | private static int HEADER_SIZE; |
aoqi@0 | 53 | |
aoqi@0 | 54 | static { |
aoqi@0 | 55 | // steal Unsafe |
aoqi@0 | 56 | try { |
aoqi@0 | 57 | Field unsafe = Unsafe.class.getDeclaredField("theUnsafe"); |
aoqi@0 | 58 | unsafe.setAccessible(true); |
aoqi@0 | 59 | U = (Unsafe) unsafe.get(null); |
aoqi@0 | 60 | } catch (NoSuchFieldException | IllegalAccessException e) { |
aoqi@0 | 61 | throw new IllegalStateException(e); |
aoqi@0 | 62 | } |
aoqi@0 | 63 | |
aoqi@0 | 64 | // When running with CompressedOops on 64-bit platform, the address size |
aoqi@0 | 65 | // reported by Unsafe is still 8, while the real reference fields are 4 bytes long. |
aoqi@0 | 66 | // Try to guess the reference field size with this naive trick. |
aoqi@0 | 67 | try { |
aoqi@0 | 68 | long off1 = U.objectFieldOffset(CompressedOopsClass.class.getField("obj1")); |
aoqi@0 | 69 | long off2 = U.objectFieldOffset(CompressedOopsClass.class.getField("obj2")); |
aoqi@0 | 70 | ADDRESS_SIZE = (int) Math.abs(off2 - off1); |
aoqi@0 | 71 | HEADER_SIZE = (int) Math.min(off1, off2); |
aoqi@0 | 72 | } catch (NoSuchFieldException e) { |
aoqi@0 | 73 | ADDRESS_SIZE = -1; |
aoqi@0 | 74 | } |
aoqi@0 | 75 | } |
aoqi@0 | 76 | |
aoqi@0 | 77 | static class CompressedOopsClass { |
aoqi@0 | 78 | public Object obj1; |
aoqi@0 | 79 | public Object obj2; |
aoqi@0 | 80 | } |
aoqi@0 | 81 | |
aoqi@0 | 82 | public static boolean arePaddedPairwise(Class klass, String field1, String field2) throws Exception { |
aoqi@0 | 83 | Field f1 = klass.getField(field1); |
aoqi@0 | 84 | Field f2 = klass.getField(field2); |
aoqi@0 | 85 | |
aoqi@0 | 86 | int diff = offset(f1) - offset(f2); |
aoqi@0 | 87 | if (diff < 0) { |
aoqi@0 | 88 | // f1 is first |
aoqi@0 | 89 | return (offset(f2) - (offset(f1) + getSize(f1))) > 64; |
aoqi@0 | 90 | } else { |
aoqi@0 | 91 | // f2 is first |
aoqi@0 | 92 | return (offset(f1) - (offset(f2) + getSize(f2))) > 64; |
aoqi@0 | 93 | } |
aoqi@0 | 94 | } |
aoqi@0 | 95 | |
aoqi@0 | 96 | public static boolean sameLayout(Class klass1, Class klass2) throws Exception { |
aoqi@0 | 97 | for (Field f1 : klass1.getDeclaredFields()) { |
aoqi@0 | 98 | Field f2 = klass2.getDeclaredField(f1.getName()); |
aoqi@0 | 99 | if (offset(f1) != offset(f2)) { |
aoqi@0 | 100 | return false; |
aoqi@0 | 101 | } |
aoqi@0 | 102 | } |
aoqi@0 | 103 | |
aoqi@0 | 104 | for (Field f2 : klass1.getDeclaredFields()) { |
aoqi@0 | 105 | Field f1 = klass2.getDeclaredField(f2.getName()); |
aoqi@0 | 106 | if (offset(f1) != offset(f2)) { |
aoqi@0 | 107 | return false; |
aoqi@0 | 108 | } |
aoqi@0 | 109 | } |
aoqi@0 | 110 | |
aoqi@0 | 111 | return true; |
aoqi@0 | 112 | } |
aoqi@0 | 113 | |
aoqi@0 | 114 | public static boolean isStatic(Field field) { |
aoqi@0 | 115 | return Modifier.isStatic(field.getModifiers()); |
aoqi@0 | 116 | } |
aoqi@0 | 117 | |
aoqi@0 | 118 | public static int offset(Field field) { |
aoqi@0 | 119 | if (isStatic(field)) { |
aoqi@0 | 120 | return (int) U.staticFieldOffset(field); |
aoqi@0 | 121 | } else { |
aoqi@0 | 122 | return (int) U.objectFieldOffset(field); |
aoqi@0 | 123 | } |
aoqi@0 | 124 | } |
aoqi@0 | 125 | |
aoqi@0 | 126 | public static int getSize(Field field) { |
aoqi@0 | 127 | Class type = field.getType(); |
aoqi@0 | 128 | if (type == byte.class) { return 1; } |
aoqi@0 | 129 | if (type == boolean.class) { return 1; } |
aoqi@0 | 130 | if (type == short.class) { return 2; } |
aoqi@0 | 131 | if (type == char.class) { return 2; } |
aoqi@0 | 132 | if (type == int.class) { return 4; } |
aoqi@0 | 133 | if (type == float.class) { return 4; } |
aoqi@0 | 134 | if (type == long.class) { return 8; } |
aoqi@0 | 135 | if (type == double.class) { return 8; } |
aoqi@0 | 136 | return ADDRESS_SIZE; |
aoqi@0 | 137 | } |
aoqi@0 | 138 | |
aoqi@0 | 139 | public static void main(String[] args) throws Exception { |
aoqi@0 | 140 | boolean endResult = true; |
aoqi@0 | 141 | |
aoqi@0 | 142 | if (!arePaddedPairwise(R1.class, "int1", "int2")) { |
aoqi@0 | 143 | System.err.println("R1 failed"); |
aoqi@0 | 144 | endResult &= false; |
aoqi@0 | 145 | } |
aoqi@0 | 146 | |
aoqi@0 | 147 | if (!arePaddedPairwise(R2.class, "int1", "int2")) { |
aoqi@0 | 148 | System.err.println("R2 failed"); |
aoqi@0 | 149 | endResult &= false; |
aoqi@0 | 150 | } |
aoqi@0 | 151 | |
aoqi@0 | 152 | if (!arePaddedPairwise(R3.class, "int1", "int2")) { |
aoqi@0 | 153 | System.err.println("R3 failed"); |
aoqi@0 | 154 | endResult &= false; |
aoqi@0 | 155 | } |
aoqi@0 | 156 | |
aoqi@0 | 157 | System.out.println(endResult ? "Test PASSES" : "Test FAILS"); |
aoqi@0 | 158 | if (!endResult) { |
aoqi@0 | 159 | throw new Error("Test failed"); |
aoqi@0 | 160 | } |
aoqi@0 | 161 | } |
aoqi@0 | 162 | |
aoqi@0 | 163 | public static class R1 { |
aoqi@0 | 164 | @Contended |
aoqi@0 | 165 | public int int1; |
aoqi@0 | 166 | @Contended |
aoqi@0 | 167 | public int int2; |
aoqi@0 | 168 | } |
aoqi@0 | 169 | |
aoqi@0 | 170 | public static class R2 { |
aoqi@0 | 171 | @Contended("") |
aoqi@0 | 172 | public int int1; |
aoqi@0 | 173 | @Contended("") |
aoqi@0 | 174 | public int int2; |
aoqi@0 | 175 | } |
aoqi@0 | 176 | |
aoqi@0 | 177 | public static class R3 { |
aoqi@0 | 178 | @Contended() |
aoqi@0 | 179 | public int int1; |
aoqi@0 | 180 | @Contended() |
aoqi@0 | 181 | public int int2; |
aoqi@0 | 182 | } |
aoqi@0 | 183 | |
aoqi@0 | 184 | } |
aoqi@0 | 185 |