8353273: Reduce number of oop map entries in instances

Reviewed-by: lmesnik, fparain, jsjolen
This commit is contained in:
Thomas Stuefe 2025-04-09 05:08:51 +00:00
parent 2a0cf8353a
commit 743d1c64c2
4 changed files with 297 additions and 15 deletions

View File

@ -126,17 +126,19 @@ void FieldLayout::initialize_static_layout() {
}
}
void FieldLayout::initialize_instance_layout(const InstanceKlass* super_klass) {
void FieldLayout::initialize_instance_layout(const InstanceKlass* super_klass, bool& super_ends_with_oop) {
if (super_klass == nullptr) {
super_ends_with_oop = false;
_blocks = new LayoutRawBlock(LayoutRawBlock::EMPTY, INT_MAX);
_blocks->set_offset(0);
_last = _blocks;
_start = _blocks;
insert(first_empty_block(), new LayoutRawBlock(LayoutRawBlock::RESERVED, instanceOopDesc::base_offset_in_bytes()));
} else {
bool has_fields = reconstruct_layout(super_klass);
bool super_has_instance_fields = false;
reconstruct_layout(super_klass, super_has_instance_fields, super_ends_with_oop);
fill_holes(super_klass);
if (!super_klass->has_contended_annotations() || !has_fields) {
if (!super_klass->has_contended_annotations() || !super_has_instance_fields) {
_start = _blocks; // start allocating fields from the first empty block
} else {
_start = _last; // append fields at the end of the reconstructed layout
@ -293,15 +295,21 @@ LayoutRawBlock* FieldLayout::insert_field_block(LayoutRawBlock* slot, LayoutRawB
return block;
}
bool FieldLayout::reconstruct_layout(const InstanceKlass* ik) {
bool has_instance_fields = false;
void FieldLayout::reconstruct_layout(const InstanceKlass* ik, bool& has_instance_fields, bool& ends_with_oop) {
has_instance_fields = ends_with_oop = false;
GrowableArray<LayoutRawBlock*>* all_fields = new GrowableArray<LayoutRawBlock*>(32);
BasicType last_type;
int last_offset = -1;
while (ik != nullptr) {
for (AllFieldStream fs(ik->fieldinfo_stream(), ik->constants()); !fs.done(); fs.next()) {
BasicType type = Signature::basic_type(fs.signature());
// distinction between static and non-static fields is missing
if (fs.access_flags().is_static()) continue;
has_instance_fields = true;
if (fs.offset() > last_offset) {
last_offset = fs.offset();
last_type = type;
}
int size = type2aelembytes(type);
// INHERITED blocks are marked as non-reference because oop_maps are handled by their holder class
LayoutRawBlock* block = new LayoutRawBlock(fs.index(), LayoutRawBlock::INHERITED, size, size, false);
@ -310,6 +318,11 @@ bool FieldLayout::reconstruct_layout(const InstanceKlass* ik) {
}
ik = ik->super() == nullptr ? nullptr : InstanceKlass::cast(ik->super());
}
assert(last_offset == -1 || last_offset > 0, "Sanity");
if (last_offset > 0 &&
(last_type == BasicType::T_ARRAY || last_type == BasicType::T_OBJECT)) {
ends_with_oop = true;
}
all_fields->sort(LayoutRawBlock::compare_offset);
_blocks = new LayoutRawBlock(LayoutRawBlock::RESERVED, instanceOopDesc::base_offset_in_bytes());
@ -323,7 +336,6 @@ bool FieldLayout::reconstruct_layout(const InstanceKlass* ik) {
_last = b;
}
_start = _blocks;
return has_instance_fields;
}
// Called during the reconstruction of a layout, after fields from super
@ -516,7 +528,7 @@ FieldGroup* FieldLayoutBuilder::get_or_create_contended_group(int g) {
void FieldLayoutBuilder::prologue() {
_layout = new FieldLayout(_field_info, _constant_pool);
const InstanceKlass* super_klass = _super_klass;
_layout->initialize_instance_layout(super_klass);
_layout->initialize_instance_layout(super_klass, _super_ends_with_oop);
if (super_klass != nullptr) {
_has_nonstatic_fields = super_klass->has_nonstatic_fields();
}
@ -594,8 +606,14 @@ void FieldLayoutBuilder::insert_contended_padding(LayoutRawBlock* slot) {
// Computation of regular classes layout is an evolution of the previous default layout
// (FieldAllocationStyle 1):
// - primitive fields are allocated first (from the biggest to the smallest)
// - then oop fields are allocated, either in existing gaps or at the end of
// the layout
// - oop fields are allocated, either in existing gaps or at the end of
// the layout. We allocate oops in a single block to have a single oop map entry.
// - if the super class ended with an oop, we lead with oops. That will cause the
// trailing oop map entry of the super class and the oop map entry of this class
// to be folded into a single entry later. Correspondingly, if the super class
// ends with a primitive field, we gain nothing by leading with oops; therefore
// we let oop fields trail, thus giving future derived classes the chance to apply
// the same trick.
void FieldLayoutBuilder::compute_regular_layout() {
bool need_tail_padding = false;
prologue();
@ -608,8 +626,14 @@ void FieldLayoutBuilder::compute_regular_layout() {
insert_contended_padding(_layout->start());
need_tail_padding = true;
}
_layout->add(_root_group->primitive_fields());
_layout->add(_root_group->oop_fields());
if (_super_ends_with_oop) {
_layout->add(_root_group->oop_fields());
_layout->add(_root_group->primitive_fields());
} else {
_layout->add(_root_group->primitive_fields());
_layout->add(_root_group->oop_fields());
}
if (!_contended_groups.is_empty()) {
for (int i = 0; i < _contended_groups.length(); i++) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -169,7 +169,7 @@ class FieldLayout : public ResourceObj {
public:
FieldLayout(GrowableArray<FieldInfo>* field_info, ConstantPool* cp);
void initialize_static_layout();
void initialize_instance_layout(const InstanceKlass* ik);
void initialize_instance_layout(const InstanceKlass* ik, bool& super_ends_with_oop);
LayoutRawBlock* first_empty_block() {
LayoutRawBlock* block = _start;
@ -188,7 +188,7 @@ class FieldLayout : public ResourceObj {
void add_field_at_offset(LayoutRawBlock* blocks, int offset, LayoutRawBlock* start = nullptr);
void add_contiguously(GrowableArray<LayoutRawBlock*>* list, LayoutRawBlock* start = nullptr);
LayoutRawBlock* insert_field_block(LayoutRawBlock* slot, LayoutRawBlock* block);
bool reconstruct_layout(const InstanceKlass* ik);
void reconstruct_layout(const InstanceKlass* ik, bool& has_instance_fields, bool& ends_with_oop);
void fill_holes(const InstanceKlass* ik);
LayoutRawBlock* insert(LayoutRawBlock* slot, LayoutRawBlock* block);
void remove(LayoutRawBlock* block);
@ -237,6 +237,7 @@ class FieldLayoutBuilder : public ResourceObj {
int _alignment;
bool _has_nonstatic_fields;
bool _is_contended; // is a contended class?
bool _super_ends_with_oop;
public:
FieldLayoutBuilder(const Symbol* classname, const InstanceKlass* super_klass, ConstantPool* constant_pool,

View File

@ -3763,7 +3763,7 @@ void InstanceKlass::print_on(outputStream* st) const {
InstanceKlass* ik = const_cast<InstanceKlass*>(this);
ik->print_nonstatic_fields(&print_nonstatic_field);
st->print(BULLET"non-static oop maps: ");
st->print(BULLET"non-static oop maps (%d entries): ", nonstatic_oop_map_count());
OopMapBlock* map = start_of_nonstatic_oop_maps();
OopMapBlock* end_map = map + nonstatic_oop_map_count();
while (map < end_map) {

View File

@ -0,0 +1,257 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2025, Red Hat, Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import jdk.internal.misc.Unsafe;
import jdk.test.whitebox.WhiteBox;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/*
* @test id=no_coops_no_ccptr_no_coh
* @library /test/lib
* @modules java.base/jdk.internal.misc
* java.management
* @build jdk.test.whitebox.WhiteBox
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UnlockExperimentalVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:-UseCompactObjectHeaders TestOopMapSizeMinimal
*/
/*
* @test id=coops_no_ccptr_no_coh
* @library /test/lib
* @modules java.base/jdk.internal.misc
* java.management
* @build jdk.test.whitebox.WhiteBox
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UnlockExperimentalVMOptions -XX:+UseCompressedOops -XX:-UseCompressedClassPointers -XX:-UseCompactObjectHeaders TestOopMapSizeMinimal
*/
/*
* @test id=no_coops_ccptr_no_coh
* @library /test/lib
* @modules java.base/jdk.internal.misc
* java.management
* @build jdk.test.whitebox.WhiteBox
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UnlockExperimentalVMOptions -XX:-UseCompressedOops -XX:+UseCompressedClassPointers -XX:-UseCompactObjectHeaders TestOopMapSizeMinimal
*/
/*
* @test id=coops_ccptr_no_coh
* @library /test/lib
* @modules java.base/jdk.internal.misc
* java.management
* @build jdk.test.whitebox.WhiteBox
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UnlockExperimentalVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:-UseCompactObjectHeaders TestOopMapSizeMinimal
*/
/*
* @test id=no_coops_ccptr_coh
* @library /test/lib
* @modules java.base/jdk.internal.misc
* java.management
* @build jdk.test.whitebox.WhiteBox
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:+UnlockExperimentalVMOptions -XX:-UseCompressedOops -XX:+UseCompactObjectHeaders TestOopMapSizeMinimal
*/
public class TestOopMapSizeMinimal {
public static int OOP_SIZE_IN_BYTES = -1;
public static int HEADER_SIZE_IN_BYTES = -1;
static {
WhiteBox WB = WhiteBox.getWhiteBox();
boolean is_64_bit = System.getProperty("sun.arch.data.model").equals("64");
if (is_64_bit) {
if (System.getProperty("java.vm.compressedOopsMode") == null) {
OOP_SIZE_IN_BYTES = 8;
} else {
OOP_SIZE_IN_BYTES = 4;
}
} else {
OOP_SIZE_IN_BYTES = 4;
}
if (is_64_bit) {
if (WB.getBooleanVMFlag("UseCompactObjectHeaders")) {
HEADER_SIZE_IN_BYTES = 8;
} else if (WB.getBooleanVMFlag("UseCompressedClassPointers")) {
HEADER_SIZE_IN_BYTES = 12;
} else {
HEADER_SIZE_IN_BYTES = 16;
}
} else {
HEADER_SIZE_IN_BYTES = 8;
}
}
public static long alignUp(long value, long alignment) {
return (value + alignment - 1) & ~(alignment - 1);
}
public static long alignForOop(long position) {
return alignUp(position, OOP_SIZE_IN_BYTES);
}
private static final Unsafe U = Unsafe.getUnsafe();
public static class BASE {
int i1;
Object o1;
}
public static class DERIVED1 extends BASE {
int i2;
Object o2;
}
public static class DERIVED2 extends DERIVED1 {
int i3;
Object o3;
}
public static class DERIVED3 extends DERIVED2 {
int i4;
Object o4;
}
static boolean mismatch = false;
private static void checkOffset(Field f, long expectedOffset) {
long realOffset = U.objectFieldOffset(f);
System.out.println("Offset for field " + f.getName() +
": expected " + expectedOffset + ", got " + realOffset + ".");
if (U.objectFieldOffset(f) != expectedOffset) {
mismatch = true;
System.out.println(" ... mimatch");
}
}
private static List<Field> allFieldsOf(Class c) {
ArrayList<Field> l = new ArrayList<>();
while (c != null) {
for (Field f : c.getDeclaredFields()) {
l.add(f);
}
c = c.getSuperclass();
}
return l;
}
public static void main(String[] args) throws Exception {
System.out.println("HEADER_SIZE_IN_BYTES " + HEADER_SIZE_IN_BYTES + ", OOP_SIZE_IN_BYTES " + OOP_SIZE_IN_BYTES);
long i1_loc_expected;
long o1_loc_expected;
long o2_loc_expected;
long i2_loc_expected;
long i3_loc_expected;
long o3_loc_expected;
long o4_loc_expected;
long i4_loc_expected;
// We expect the layouter to reverse order of oop- and non-oop fields
// when it is useful to minimize the number of oop map entries.
//
// If we have no gaps, this should be the layout:
// BASE i1
// o1 oopmap entry 1
// DERIVED1 o2 oopmap entry 1 (reversed order)
// i2
// DERIVED3 i3
// o3 oopmap entry 2
// DERIVED4 o4 oopmap entry 2 (reversed order)
// i4
// There are two combinations that have gaps:
// -UseCompressedOops + +COH, and -UseCompressedOops + -UseCompressedClassPointers.
// In both cases there is a gap following i1, and i2 will therefore nestle into that gap.
// Otherwise the same logic applies.
if (OOP_SIZE_IN_BYTES == 4 || // oop size == int size
(OOP_SIZE_IN_BYTES == 8 && HEADER_SIZE_IN_BYTES == 12)
) {
// No gaps
// Expected layout for BASE: int, object
i1_loc_expected = HEADER_SIZE_IN_BYTES;
o1_loc_expected = i1_loc_expected + 4;
// Expected layout for DERIVED1: object, int (to make o2 border o1)
o2_loc_expected = o1_loc_expected + OOP_SIZE_IN_BYTES;
i2_loc_expected = o2_loc_expected + OOP_SIZE_IN_BYTES;
// Expected layout for DERIVED2: int, object (to trail with oops, for derived classes to nestle against)
i3_loc_expected = i2_loc_expected + 4;
o3_loc_expected = i3_loc_expected + 4;
// Expected layout for DERIVED3: object, int (to make o4 border o3)
o4_loc_expected = o3_loc_expected + OOP_SIZE_IN_BYTES;
i4_loc_expected = o4_loc_expected + OOP_SIZE_IN_BYTES;
} else if (OOP_SIZE_IN_BYTES == 8) {
// gap after i1
i1_loc_expected = HEADER_SIZE_IN_BYTES;
o1_loc_expected = i1_loc_expected + 4 + 4; // + alignment gap
o2_loc_expected = o1_loc_expected + OOP_SIZE_IN_BYTES;
i2_loc_expected = i1_loc_expected + 4; // into gap following i1
o3_loc_expected = o2_loc_expected + OOP_SIZE_IN_BYTES;
i3_loc_expected = o3_loc_expected + OOP_SIZE_IN_BYTES;
i4_loc_expected = i3_loc_expected + 4;
o4_loc_expected = i4_loc_expected + 4;
} else {
throw new RuntimeException("Unexpected");
}
List<Field> l = allFieldsOf(DERIVED3.class);
for (Field f : l) {
switch (f.getName()) {
case "i1" : checkOffset(f, i1_loc_expected); break;
case "o1" : checkOffset(f, o1_loc_expected); break;
case "i2" : checkOffset(f, i2_loc_expected); break;
case "o2" : checkOffset(f, o2_loc_expected); break;
case "i3" : checkOffset(f, i3_loc_expected); break;
case "o3" : checkOffset(f, o3_loc_expected); break;
case "i4" : checkOffset(f, i4_loc_expected); break;
case "o4" : checkOffset(f, o4_loc_expected); break;
default: throw new RuntimeException("Unexpected");
}
}
if (mismatch) {
throw new RuntimeException("Mismatch!");
}
System.out.println("All good.");
}
}