Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/contended/Basic.java
32284 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.BufferedReader;24import java.io.InputStreamReader;25import java.lang.Class;26import java.lang.String;27import java.lang.System;28import java.lang.management.ManagementFactory;29import java.lang.management.RuntimeMXBean;30import java.util.ArrayList;31import java.util.List;32import java.util.concurrent.CyclicBarrier;33import java.util.regex.Matcher;34import java.util.regex.Pattern;35import java.lang.reflect.Field;36import java.lang.reflect.Modifier;37import sun.misc.Unsafe;38import sun.misc.Contended;3940/*41* @test42* @bug 800398543* @summary Support Contended Annotation - JEP 14244*45* @run main/othervm -XX:-RestrictContended Basic46*/47public class Basic {4849private static final Unsafe U;50private static int ADDRESS_SIZE;51private static int HEADER_SIZE;5253static {54// steal Unsafe55try {56Field unsafe = Unsafe.class.getDeclaredField("theUnsafe");57unsafe.setAccessible(true);58U = (Unsafe) unsafe.get(null);59} catch (NoSuchFieldException | IllegalAccessException e) {60throw new IllegalStateException(e);61}6263// When running with CompressedOops on 64-bit platform, the address size64// reported by Unsafe is still 8, while the real reference fields are 4 bytes long.65// Try to guess the reference field size with this naive trick.66try {67long off1 = U.objectFieldOffset(CompressedOopsClass.class.getField("obj1"));68long off2 = U.objectFieldOffset(CompressedOopsClass.class.getField("obj2"));69ADDRESS_SIZE = (int) Math.abs(off2 - off1);70HEADER_SIZE = (int) Math.min(off1, off2);71} catch (NoSuchFieldException e) {72ADDRESS_SIZE = -1;73}74}7576static class CompressedOopsClass {77public Object obj1;78public Object obj2;79}8081public static boolean arePaddedPairwise(Class klass, String field1, String field2) throws Exception {82Field f1 = klass.getDeclaredField(field1);83Field f2 = klass.getDeclaredField(field2);8485if (isStatic(f1) != isStatic(f2)) {86return true; // these guys are in naturally disjoint locations87}8889int diff = offset(f1) - offset(f2);90if (diff < 0) {91// f1 is first92return (offset(f2) - (offset(f1) + getSize(f1))) > 64;93} else {94// f2 is first95return (offset(f1) - (offset(f2) + getSize(f2))) > 64;96}97}9899public static boolean isPadded(Class klass, String field1) throws Exception {100Field f1 = klass.getDeclaredField(field1);101102if (isStatic(f1)) {103return offset(f1) > 128 + 64;104}105106return offset(f1) > 64;107}108109public static boolean sameLayout(Class klass1, Class klass2) throws Exception {110for (Field f1 : klass1.getDeclaredFields()) {111Field f2 = klass2.getDeclaredField(f1.getName());112if (offset(f1) != offset(f2)) {113return false;114}115}116117for (Field f2 : klass1.getDeclaredFields()) {118Field f1 = klass2.getDeclaredField(f2.getName());119if (offset(f1) != offset(f2)) {120return false;121}122}123124return true;125}126127public static boolean isStatic(Field field) {128return Modifier.isStatic(field.getModifiers());129}130131public static int offset(Field field) {132if (isStatic(field)) {133return (int) U.staticFieldOffset(field);134} else {135return (int) U.objectFieldOffset(field);136}137}138139public static int getSize(Field field) {140Class type = field.getType();141if (type == byte.class) { return 1; }142if (type == boolean.class) { return 1; }143if (type == short.class) { return 2; }144if (type == char.class) { return 2; }145if (type == int.class) { return 4; }146if (type == float.class) { return 4; }147if (type == long.class) { return 8; }148if (type == double.class) { return 8; }149return ADDRESS_SIZE;150}151152public static void main(String[] args) throws Exception {153boolean endResult = true;154155// --------------- INSTANCE FIELDS ---------------------156157if (arePaddedPairwise(Test1.class, "int1", "int2") ||158isPadded(Test1.class, "int1") ||159isPadded(Test1.class, "int2")) {160System.err.println("Test1 failed");161endResult &= false;162}163164if (!arePaddedPairwise(Test2.class, "int1", "int2") ||165!isPadded(Test2.class, "int1") ||166isPadded(Test2.class, "int2")) {167System.err.println("Test2 failed");168endResult &= false;169}170171if (!arePaddedPairwise(Test3.class, "int1", "int2") ||172!isPadded(Test3.class, "int1") ||173!isPadded(Test3.class, "int2")) {174System.err.println("Test3 failed");175endResult &= false;176}177178if (arePaddedPairwise(Test4.class, "int1", "int2") ||179!isPadded(Test4.class, "int1") ||180!isPadded(Test4.class, "int2")) {181System.err.println("Test4 failed");182endResult &= false;183}184185if (!arePaddedPairwise(Test5.class, "int1", "int2") ||186!isPadded(Test5.class, "int1") ||187!isPadded(Test5.class, "int2")) {188System.err.println("Test5 failed");189endResult &= false;190}191192if (!arePaddedPairwise(Test6.class, "int1", "int2") ||193!isPadded(Test6.class, "int1") ||194!isPadded(Test6.class, "int2")) {195System.err.println("Test6 failed");196endResult &= false;197}198199if (arePaddedPairwise(Test7.class, "int1", "int2") ||200!isPadded(Test7.class, "int1") ||201!isPadded(Test7.class, "int2")) {202System.err.println("Test7 failed");203endResult &= false;204}205206if (!arePaddedPairwise(Test8.class, "int1", "int2") ||207!isPadded(Test8.class, "int1") ||208!isPadded(Test8.class, "int2")) {209System.err.println("Test8 failed");210endResult &= false;211}212213if (!arePaddedPairwise(Test9.class, "int1", "int2") ||214!isPadded(Test9.class, "int1") ||215!isPadded(Test9.class, "int2")) {216System.err.println("Test9 failed");217endResult &= false;218}219220if (!sameLayout(Test4.class, Test7.class)) {221System.err.println("Test4 and Test7 have different layouts");222endResult &= false;223}224225if (!sameLayout(Test5.class, Test6.class)) {226System.err.println("Test5 and Test6 have different layouts");227endResult &= false;228}229230if (!sameLayout(Test8.class, Test9.class)) {231System.err.println("Test8 and Test9 have different layouts");232endResult &= false;233}234235System.out.println(endResult ? "Test PASSES" : "Test FAILS");236if (!endResult) {237throw new Error("Test failed");238}239}240241// ----------------------------------- INSTANCE FIELDS -----------------------------------------242243// naturally packed244public static class Test1 {245private int int1;246private int int2;247}248249// int1 is padded250public static class Test2 {251@Contended private int int1;252private int int2;253}254255// both fields are padded256public static class Test3 {257@Contended private int int1;258@Contended private int int2;259}260261// fields are padded in the singular group262public static class Test4 {263@Contended("sameGroup") private int int1;264@Contended("sameGroup") private int int2;265}266267// fields are padded in disjoint groups268public static class Test5 {269@Contended("diffGroup1") private int int1;270@Contended("diffGroup2") private int int2;271}272273// fields are padded in disjoint groups274public static class Test6 {275@Contended private int int1;276@Contended("diffGroup2") private int int2;277}278279// fields are padded in the singular group280@Contended281public static class Test7 {282private int int1;283private int int2;284}285286// all fields are padded as the group, and one field is padded specifically287@Contended288public static class Test8 {289@Contended private int int1;290private int int2;291}292293// all fields are padded as the group, and one field is padded specifically294@Contended295public static class Test9 {296@Contended("group") private int int1;297private int int2;298}299300}301302303304