Path: blob/master/test/hotspot/jtreg/runtime/FieldLayout/FieldDensityTest.java
40942 views
/*1* Copyright (c) 2020, 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*/2223/*24* @test25* @bug 823776726* @summary Verify behaviour of field layout algorithm27* @library /test/lib28* @modules java.base/jdk.internal.misc29* java.management30* @run main/othervm FieldDensityTest31*/3233/*34* @test35* @requires vm.bits == "64"36* @library /test/lib37* @modules java.base/jdk.internal.misc38* java.management39* @run main/othervm -XX:+UseCompressedOops -XX:+UseCompressedClassPointers FieldDensityTest40* @run main/othervm -XX:+UseCompressedOops -XX:-UseCompressedClassPointers FieldDensityTest41*/4243import java.lang.reflect.Field;44import java.util.Arrays;45import java.util.Comparator;46import jdk.internal.misc.Unsafe;4748import jdk.test.lib.Asserts;4950public class FieldDensityTest {5152static int OOP_SIZE_IN_BYTES = 0;5354static {55if (System.getProperty("sun.arch.data.model").equals("64")) {56if (System.getProperty("java.vm.compressedOopsMode") == null) {57OOP_SIZE_IN_BYTES = 8;58} else {59OOP_SIZE_IN_BYTES = 4;60}61} else {62OOP_SIZE_IN_BYTES = 4;63}64}6566static class FieldInfo {67public Field field;68public long offset;6970FieldInfo(Field field, long offset) {71this.field = field;72this.offset = offset;73}7475static void checkFieldsContiguity(FieldInfo[] fieldInfo) {76Arrays.sort(fieldInfo, new SortByOffset());77for (int i = 0 ; i < fieldInfo.length - 2; i++) {78int size = sizeInBytesFromType(fieldInfo[i].field.getType());79Asserts.assertEquals((int)(fieldInfo[i].offset + size), (int)fieldInfo[i+1].offset,80"Empty slot between fields, should not happen");81}82}83}8485static int sizeInBytesFromType(Class type) {86if (!type.isPrimitive()) {87return OOP_SIZE_IN_BYTES;88}89switch(type.getTypeName()) {90case "boolean":91case "byte": return 1;92case "char":93case "short": return 2;94case "int":95case "float": return 4;96case "long":97case "double": return 8;98default:99throw new RuntimeException("Unrecognized signature");100}101}102103static class SortByOffset implements Comparator<FieldInfo> {104public int compare(FieldInfo a, FieldInfo b)105{106return (int)(a.offset - b.offset);107}108}109110static class E {111public byte b0;112}113114static class F extends E {115public byte b1;116}117118static class G extends F {119public byte b2;120}121122static class H extends G {123public byte b3;124}125126public static class A {127public int i;128public byte b;129public long l;130public Object o;131}132133public static class B extends A {134public byte b0, b1, b2;135}136137static void testFieldsContiguity(Class c) {138Unsafe unsafe = Unsafe.getUnsafe();139Field[] fields = c.getFields();140FieldInfo[] fieldsInfo = new FieldInfo[fields.length];141int i = 0;142for (Field f : fields) {143long offset = unsafe.objectFieldOffset(f);144fieldsInfo[i] = new FieldInfo(f, offset);145i++;146}147FieldInfo.checkFieldsContiguity(fieldsInfo);148}149150public static void main(String[] args) {151H h = new H();152testFieldsContiguity(h.getClass());153B b = new B();154testFieldsContiguity(b.getClass());155}156}157158159