test/runtime/contended/Inheritance1.java

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

mercurial