test/runtime/contended/Inheritance1.java

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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 8012939
aoqi@0 44 * @summary \@Contended doesn't work correctly with inheritance
aoqi@0 45 *
aoqi@0 46 * @run main/othervm -XX:-RestrictContended Inheritance1
aoqi@0 47 */
aoqi@0 48 public class Inheritance1 {
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 // --------------- INSTANCE FIELDS ---------------------
aoqi@0 143
aoqi@0 144 if (!arePaddedPairwise(A2_R1.class, "int1", "int2")) {
aoqi@0 145 System.err.println("A2_R1 failed");
aoqi@0 146 endResult &= false;
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 if (!arePaddedPairwise(A3_R1.class, "int1", "int2")) {
aoqi@0 150 System.err.println("A3_R1 failed");
aoqi@0 151 endResult &= false;
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 if (!arePaddedPairwise(A1_R2.class, "int1", "int2")) {
aoqi@0 155 System.err.println("A1_R2 failed");
aoqi@0 156 endResult &= false;
aoqi@0 157 }
aoqi@0 158
aoqi@0 159 if (!arePaddedPairwise(A2_R2.class, "int1", "int2")) {
aoqi@0 160 System.err.println("A2_R2 failed");
aoqi@0 161 endResult &= false;
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 if (!arePaddedPairwise(A3_R2.class, "int1", "int2")) {
aoqi@0 165 System.err.println("A3_R2 failed");
aoqi@0 166 endResult &= false;
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 if (!arePaddedPairwise(A1_R3.class, "int1", "int2")) {
aoqi@0 170 System.err.println("A1_R3 failed");
aoqi@0 171 endResult &= false;
aoqi@0 172 }
aoqi@0 173
aoqi@0 174 if (!arePaddedPairwise(A2_R3.class, "int1", "int2")) {
aoqi@0 175 System.err.println("A2_R3 failed");
aoqi@0 176 endResult &= false;
aoqi@0 177 }
aoqi@0 178
aoqi@0 179 if (!arePaddedPairwise(A3_R3.class, "int1", "int2")) {
aoqi@0 180 System.err.println("A3_R3 failed");
aoqi@0 181 endResult &= false;
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 System.out.println(endResult ? "Test PASSES" : "Test FAILS");
aoqi@0 185 if (!endResult) {
aoqi@0 186 throw new Error("Test failed");
aoqi@0 187 }
aoqi@0 188 }
aoqi@0 189
aoqi@0 190 public static class R1 {
aoqi@0 191 public int int1;
aoqi@0 192 }
aoqi@0 193
aoqi@0 194 public static class R2 {
aoqi@0 195 @Contended
aoqi@0 196 public int int1;
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 @Contended
aoqi@0 200 public static class R3 {
aoqi@0 201 public int int1;
aoqi@0 202 }
aoqi@0 203
aoqi@0 204 public static class A1_R1 extends R1 {
aoqi@0 205 public int int2;
aoqi@0 206 }
aoqi@0 207
aoqi@0 208 public static class A2_R1 extends R1 {
aoqi@0 209 @Contended
aoqi@0 210 public int int2;
aoqi@0 211 }
aoqi@0 212
aoqi@0 213 @Contended
aoqi@0 214 public static class A3_R1 extends R1 {
aoqi@0 215 public int int2;
aoqi@0 216 }
aoqi@0 217
aoqi@0 218 public static class A1_R2 extends R2 {
aoqi@0 219 public int int2;
aoqi@0 220 }
aoqi@0 221
aoqi@0 222 public static class A2_R2 extends R2 {
aoqi@0 223 @Contended
aoqi@0 224 public int int2;
aoqi@0 225 }
aoqi@0 226
aoqi@0 227 @Contended
aoqi@0 228 public static class A3_R2 extends R2 {
aoqi@0 229 public int int2;
aoqi@0 230 }
aoqi@0 231
aoqi@0 232 public static class A1_R3 extends R3 {
aoqi@0 233 public int int2;
aoqi@0 234 }
aoqi@0 235
aoqi@0 236 public static class A2_R3 extends R3 {
aoqi@0 237 @Contended
aoqi@0 238 public int int2;
aoqi@0 239 }
aoqi@0 240
aoqi@0 241 @Contended
aoqi@0 242 public static class A3_R3 extends R3 {
aoqi@0 243 public int int2;
aoqi@0 244 }
aoqi@0 245
aoqi@0 246
aoqi@0 247 }
aoqi@0 248

mercurial