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 8003985 |
aoqi@0 | 44 | * @summary Support Contended Annotation - JEP 142 |
aoqi@0 | 45 | * |
aoqi@0 | 46 | * @run main/othervm -XX:-RestrictContended Basic |
aoqi@0 | 47 | */ |
aoqi@0 | 48 | public class Basic { |
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.getDeclaredField(field1); |
aoqi@0 | 84 | Field f2 = klass.getDeclaredField(field2); |
aoqi@0 | 85 | |
aoqi@0 | 86 | if (isStatic(f1) != isStatic(f2)) { |
aoqi@0 | 87 | return true; // these guys are in naturally disjoint locations |
aoqi@0 | 88 | } |
aoqi@0 | 89 | |
aoqi@0 | 90 | int diff = offset(f1) - offset(f2); |
aoqi@0 | 91 | if (diff < 0) { |
aoqi@0 | 92 | // f1 is first |
aoqi@0 | 93 | return (offset(f2) - (offset(f1) + getSize(f1))) > 64; |
aoqi@0 | 94 | } else { |
aoqi@0 | 95 | // f2 is first |
aoqi@0 | 96 | return (offset(f1) - (offset(f2) + getSize(f2))) > 64; |
aoqi@0 | 97 | } |
aoqi@0 | 98 | } |
aoqi@0 | 99 | |
aoqi@0 | 100 | public static boolean isPadded(Class klass, String field1) throws Exception { |
aoqi@0 | 101 | Field f1 = klass.getDeclaredField(field1); |
aoqi@0 | 102 | |
aoqi@0 | 103 | if (isStatic(f1)) { |
aoqi@0 | 104 | return offset(f1) > 128 + 64; |
aoqi@0 | 105 | } |
aoqi@0 | 106 | |
aoqi@0 | 107 | return offset(f1) > 64; |
aoqi@0 | 108 | } |
aoqi@0 | 109 | |
aoqi@0 | 110 | public static boolean sameLayout(Class klass1, Class klass2) throws Exception { |
aoqi@0 | 111 | for (Field f1 : klass1.getDeclaredFields()) { |
aoqi@0 | 112 | Field f2 = klass2.getDeclaredField(f1.getName()); |
aoqi@0 | 113 | if (offset(f1) != offset(f2)) { |
aoqi@0 | 114 | return false; |
aoqi@0 | 115 | } |
aoqi@0 | 116 | } |
aoqi@0 | 117 | |
aoqi@0 | 118 | for (Field f2 : klass1.getDeclaredFields()) { |
aoqi@0 | 119 | Field f1 = klass2.getDeclaredField(f2.getName()); |
aoqi@0 | 120 | if (offset(f1) != offset(f2)) { |
aoqi@0 | 121 | return false; |
aoqi@0 | 122 | } |
aoqi@0 | 123 | } |
aoqi@0 | 124 | |
aoqi@0 | 125 | return true; |
aoqi@0 | 126 | } |
aoqi@0 | 127 | |
aoqi@0 | 128 | public static boolean isStatic(Field field) { |
aoqi@0 | 129 | return Modifier.isStatic(field.getModifiers()); |
aoqi@0 | 130 | } |
aoqi@0 | 131 | |
aoqi@0 | 132 | public static int offset(Field field) { |
aoqi@0 | 133 | if (isStatic(field)) { |
aoqi@0 | 134 | return (int) U.staticFieldOffset(field); |
aoqi@0 | 135 | } else { |
aoqi@0 | 136 | return (int) U.objectFieldOffset(field); |
aoqi@0 | 137 | } |
aoqi@0 | 138 | } |
aoqi@0 | 139 | |
aoqi@0 | 140 | public static int getSize(Field field) { |
aoqi@0 | 141 | Class type = field.getType(); |
aoqi@0 | 142 | if (type == byte.class) { return 1; } |
aoqi@0 | 143 | if (type == boolean.class) { return 1; } |
aoqi@0 | 144 | if (type == short.class) { return 2; } |
aoqi@0 | 145 | if (type == char.class) { return 2; } |
aoqi@0 | 146 | if (type == int.class) { return 4; } |
aoqi@0 | 147 | if (type == float.class) { return 4; } |
aoqi@0 | 148 | if (type == long.class) { return 8; } |
aoqi@0 | 149 | if (type == double.class) { return 8; } |
aoqi@0 | 150 | return ADDRESS_SIZE; |
aoqi@0 | 151 | } |
aoqi@0 | 152 | |
aoqi@0 | 153 | public static void main(String[] args) throws Exception { |
aoqi@0 | 154 | boolean endResult = true; |
aoqi@0 | 155 | |
aoqi@0 | 156 | // --------------- INSTANCE FIELDS --------------------- |
aoqi@0 | 157 | |
aoqi@0 | 158 | if (arePaddedPairwise(Test1.class, "int1", "int2") || |
aoqi@0 | 159 | isPadded(Test1.class, "int1") || |
aoqi@0 | 160 | isPadded(Test1.class, "int2")) { |
aoqi@0 | 161 | System.err.println("Test1 failed"); |
aoqi@0 | 162 | endResult &= false; |
aoqi@0 | 163 | } |
aoqi@0 | 164 | |
aoqi@0 | 165 | if (!arePaddedPairwise(Test2.class, "int1", "int2") || |
aoqi@0 | 166 | !isPadded(Test2.class, "int1") || |
aoqi@0 | 167 | isPadded(Test2.class, "int2")) { |
aoqi@0 | 168 | System.err.println("Test2 failed"); |
aoqi@0 | 169 | endResult &= false; |
aoqi@0 | 170 | } |
aoqi@0 | 171 | |
aoqi@0 | 172 | if (!arePaddedPairwise(Test3.class, "int1", "int2") || |
aoqi@0 | 173 | !isPadded(Test3.class, "int1") || |
aoqi@0 | 174 | !isPadded(Test3.class, "int2")) { |
aoqi@0 | 175 | System.err.println("Test3 failed"); |
aoqi@0 | 176 | endResult &= false; |
aoqi@0 | 177 | } |
aoqi@0 | 178 | |
aoqi@0 | 179 | if (arePaddedPairwise(Test4.class, "int1", "int2") || |
aoqi@0 | 180 | !isPadded(Test4.class, "int1") || |
aoqi@0 | 181 | !isPadded(Test4.class, "int2")) { |
aoqi@0 | 182 | System.err.println("Test4 failed"); |
aoqi@0 | 183 | endResult &= false; |
aoqi@0 | 184 | } |
aoqi@0 | 185 | |
aoqi@0 | 186 | if (!arePaddedPairwise(Test5.class, "int1", "int2") || |
aoqi@0 | 187 | !isPadded(Test5.class, "int1") || |
aoqi@0 | 188 | !isPadded(Test5.class, "int2")) { |
aoqi@0 | 189 | System.err.println("Test5 failed"); |
aoqi@0 | 190 | endResult &= false; |
aoqi@0 | 191 | } |
aoqi@0 | 192 | |
aoqi@0 | 193 | if (!arePaddedPairwise(Test6.class, "int1", "int2") || |
aoqi@0 | 194 | !isPadded(Test6.class, "int1") || |
aoqi@0 | 195 | !isPadded(Test6.class, "int2")) { |
aoqi@0 | 196 | System.err.println("Test6 failed"); |
aoqi@0 | 197 | endResult &= false; |
aoqi@0 | 198 | } |
aoqi@0 | 199 | |
aoqi@0 | 200 | if (arePaddedPairwise(Test7.class, "int1", "int2") || |
aoqi@0 | 201 | !isPadded(Test7.class, "int1") || |
aoqi@0 | 202 | !isPadded(Test7.class, "int2")) { |
aoqi@0 | 203 | System.err.println("Test7 failed"); |
aoqi@0 | 204 | endResult &= false; |
aoqi@0 | 205 | } |
aoqi@0 | 206 | |
aoqi@0 | 207 | if (!arePaddedPairwise(Test8.class, "int1", "int2") || |
aoqi@0 | 208 | !isPadded(Test8.class, "int1") || |
aoqi@0 | 209 | !isPadded(Test8.class, "int2")) { |
aoqi@0 | 210 | System.err.println("Test8 failed"); |
aoqi@0 | 211 | endResult &= false; |
aoqi@0 | 212 | } |
aoqi@0 | 213 | |
aoqi@0 | 214 | if (!arePaddedPairwise(Test9.class, "int1", "int2") || |
aoqi@0 | 215 | !isPadded(Test9.class, "int1") || |
aoqi@0 | 216 | !isPadded(Test9.class, "int2")) { |
aoqi@0 | 217 | System.err.println("Test9 failed"); |
aoqi@0 | 218 | endResult &= false; |
aoqi@0 | 219 | } |
aoqi@0 | 220 | |
aoqi@0 | 221 | if (!sameLayout(Test4.class, Test7.class)) { |
aoqi@0 | 222 | System.err.println("Test4 and Test7 have different layouts"); |
aoqi@0 | 223 | endResult &= false; |
aoqi@0 | 224 | } |
aoqi@0 | 225 | |
aoqi@0 | 226 | if (!sameLayout(Test5.class, Test6.class)) { |
aoqi@0 | 227 | System.err.println("Test5 and Test6 have different layouts"); |
aoqi@0 | 228 | endResult &= false; |
aoqi@0 | 229 | } |
aoqi@0 | 230 | |
aoqi@0 | 231 | if (!sameLayout(Test8.class, Test9.class)) { |
aoqi@0 | 232 | System.err.println("Test8 and Test9 have different layouts"); |
aoqi@0 | 233 | endResult &= false; |
aoqi@0 | 234 | } |
aoqi@0 | 235 | |
aoqi@0 | 236 | System.out.println(endResult ? "Test PASSES" : "Test FAILS"); |
aoqi@0 | 237 | if (!endResult) { |
aoqi@0 | 238 | throw new Error("Test failed"); |
aoqi@0 | 239 | } |
aoqi@0 | 240 | } |
aoqi@0 | 241 | |
aoqi@0 | 242 | // ----------------------------------- INSTANCE FIELDS ----------------------------------------- |
aoqi@0 | 243 | |
aoqi@0 | 244 | // naturally packed |
aoqi@0 | 245 | public static class Test1 { |
aoqi@0 | 246 | private int int1; |
aoqi@0 | 247 | private int int2; |
aoqi@0 | 248 | } |
aoqi@0 | 249 | |
aoqi@0 | 250 | // int1 is padded |
aoqi@0 | 251 | public static class Test2 { |
aoqi@0 | 252 | @Contended private int int1; |
aoqi@0 | 253 | private int int2; |
aoqi@0 | 254 | } |
aoqi@0 | 255 | |
aoqi@0 | 256 | // both fields are padded |
aoqi@0 | 257 | public static class Test3 { |
aoqi@0 | 258 | @Contended private int int1; |
aoqi@0 | 259 | @Contended private int int2; |
aoqi@0 | 260 | } |
aoqi@0 | 261 | |
aoqi@0 | 262 | // fields are padded in the singular group |
aoqi@0 | 263 | public static class Test4 { |
aoqi@0 | 264 | @Contended("sameGroup") private int int1; |
aoqi@0 | 265 | @Contended("sameGroup") private int int2; |
aoqi@0 | 266 | } |
aoqi@0 | 267 | |
aoqi@0 | 268 | // fields are padded in disjoint groups |
aoqi@0 | 269 | public static class Test5 { |
aoqi@0 | 270 | @Contended("diffGroup1") private int int1; |
aoqi@0 | 271 | @Contended("diffGroup2") private int int2; |
aoqi@0 | 272 | } |
aoqi@0 | 273 | |
aoqi@0 | 274 | // fields are padded in disjoint groups |
aoqi@0 | 275 | public static class Test6 { |
aoqi@0 | 276 | @Contended private int int1; |
aoqi@0 | 277 | @Contended("diffGroup2") private int int2; |
aoqi@0 | 278 | } |
aoqi@0 | 279 | |
aoqi@0 | 280 | // fields are padded in the singular group |
aoqi@0 | 281 | @Contended |
aoqi@0 | 282 | public static class Test7 { |
aoqi@0 | 283 | private int int1; |
aoqi@0 | 284 | private int int2; |
aoqi@0 | 285 | } |
aoqi@0 | 286 | |
aoqi@0 | 287 | // all fields are padded as the group, and one field is padded specifically |
aoqi@0 | 288 | @Contended |
aoqi@0 | 289 | public static class Test8 { |
aoqi@0 | 290 | @Contended private int int1; |
aoqi@0 | 291 | private int int2; |
aoqi@0 | 292 | } |
aoqi@0 | 293 | |
aoqi@0 | 294 | // all fields are padded as the group, and one field is padded specifically |
aoqi@0 | 295 | @Contended |
aoqi@0 | 296 | public static class Test9 { |
aoqi@0 | 297 | @Contended("group") private int int1; |
aoqi@0 | 298 | private int int2; |
aoqi@0 | 299 | } |
aoqi@0 | 300 | |
aoqi@0 | 301 | } |
aoqi@0 | 302 |