1 /* 2 * Copyright (c) 2020, 2025, 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 25 #include "classfile/classFileParser.hpp" 26 #include "classfile/fieldLayoutBuilder.hpp" 27 #include "jvm.h" 28 #include "memory/resourceArea.hpp" 29 #include "oops/array.hpp" 30 #include "oops/fieldStreams.inline.hpp" 31 #include "oops/instanceMirrorKlass.hpp" 32 #include "oops/instanceKlass.inline.hpp" 33 #include "oops/klass.inline.hpp" 34 #include "runtime/fieldDescriptor.inline.hpp" 35 36 37 LayoutRawBlock::LayoutRawBlock(Kind kind, int size) : 38 _next_block(nullptr), 39 _prev_block(nullptr), 40 _kind(kind), 41 _offset(-1), 42 _alignment(1), 43 _size(size), 44 _field_index(-1), 45 _is_reference(false) { 46 assert(kind == EMPTY || kind == RESERVED || kind == PADDING || kind == INHERITED, 47 "Otherwise, should use the constructor with a field index argument"); 48 assert(size > 0, "Sanity check"); 49 } 50 51 52 LayoutRawBlock::LayoutRawBlock(int index, Kind kind, int size, int alignment, bool is_reference) : 53 _next_block(nullptr), 54 _prev_block(nullptr), 55 _kind(kind), 56 _offset(-1), 57 _alignment(alignment), 58 _size(size), 59 _field_index(index), 60 _is_reference(is_reference) { 61 assert(kind == REGULAR || kind == FLATTENED || kind == INHERITED, 62 "Other kind do not have a field index"); 63 assert(size > 0, "Sanity check"); 64 assert(alignment > 0, "Sanity check"); 65 } 66 67 bool LayoutRawBlock::fit(int size, int alignment) { 68 int adjustment = 0; 69 if ((_offset % alignment) != 0) { 70 adjustment = alignment - (_offset % alignment); 71 } 72 return _size >= size + adjustment; 73 } 74 75 FieldGroup::FieldGroup(int contended_group) : 76 _next(nullptr), 77 _primitive_fields(nullptr), 78 _oop_fields(nullptr), 79 _contended_group(contended_group), // -1 means no contended group, 0 means default contended group 80 _oop_count(0) {} 81 82 void FieldGroup::add_primitive_field(int idx, BasicType type) { 83 int size = type2aelembytes(type); 84 LayoutRawBlock* block = new LayoutRawBlock(idx, LayoutRawBlock::REGULAR, size, size /* alignment == size for primitive types */, false); 85 if (_primitive_fields == nullptr) { 86 _primitive_fields = new GrowableArray<LayoutRawBlock*>(INITIAL_LIST_SIZE); 87 } 88 _primitive_fields->append(block); 89 } 90 91 void FieldGroup::add_oop_field(int idx) { 92 int size = type2aelembytes(T_OBJECT); 93 LayoutRawBlock* block = new LayoutRawBlock(idx, LayoutRawBlock::REGULAR, size, size /* alignment == size for oops */, true); 94 if (_oop_fields == nullptr) { 95 _oop_fields = new GrowableArray<LayoutRawBlock*>(INITIAL_LIST_SIZE); 96 } 97 _oop_fields->append(block); 98 _oop_count++; 99 } 100 101 void FieldGroup::sort_by_size() { 102 if (_primitive_fields != nullptr) { 103 _primitive_fields->sort(LayoutRawBlock::compare_size_inverted); 104 } 105 } 106 107 FieldLayout::FieldLayout(GrowableArray<FieldInfo>* field_info, ConstantPool* cp) : 108 _field_info(field_info), 109 _cp(cp), 110 _blocks(nullptr), 111 _start(_blocks), 112 _last(_blocks) {} 113 114 void FieldLayout::initialize_static_layout() { 115 _blocks = new LayoutRawBlock(LayoutRawBlock::EMPTY, INT_MAX); 116 _blocks->set_offset(0); 117 _last = _blocks; 118 _start = _blocks; 119 // Note: at this stage, InstanceMirrorKlass::offset_of_static_fields() could be zero, because 120 // during bootstrapping, the size of the java.lang.Class is still not known when layout 121 // of static field is computed. Field offsets are fixed later when the size is known 122 // (see java_lang_Class::fixup_mirror()) 123 if (InstanceMirrorKlass::offset_of_static_fields() > 0) { 124 insert(first_empty_block(), new LayoutRawBlock(LayoutRawBlock::RESERVED, InstanceMirrorKlass::offset_of_static_fields())); 125 _blocks->set_offset(0); 126 } 127 } 128 129 void FieldLayout::initialize_instance_layout(const InstanceKlass* super_klass, bool& super_ends_with_oop) { 130 if (super_klass == nullptr) { 131 super_ends_with_oop = false; 132 _blocks = new LayoutRawBlock(LayoutRawBlock::EMPTY, INT_MAX); 133 _blocks->set_offset(0); 134 _last = _blocks; 135 _start = _blocks; 136 insert(first_empty_block(), new LayoutRawBlock(LayoutRawBlock::RESERVED, instanceOopDesc::base_offset_in_bytes())); 137 } else { 138 bool super_has_instance_fields = false; 139 reconstruct_layout(super_klass, super_has_instance_fields, super_ends_with_oop); 140 fill_holes(super_klass); 141 if (!super_klass->has_contended_annotations() || !super_has_instance_fields) { 142 _start = _blocks; // start allocating fields from the first empty block 143 } else { 144 _start = _last; // append fields at the end of the reconstructed layout 145 } 146 } 147 } 148 149 LayoutRawBlock* FieldLayout::first_field_block() { 150 LayoutRawBlock* block = _start; 151 while (block->kind() != LayoutRawBlock::INHERITED && block->kind() != LayoutRawBlock::REGULAR 152 && block->kind() != LayoutRawBlock::FLATTENED && block->kind() != LayoutRawBlock::PADDING) { 153 block = block->next_block(); 154 } 155 return block; 156 } 157 158 159 // Insert a set of fields into a layout using a best-fit strategy. 160 // For each field, search for the smallest empty slot able to fit the field 161 // (satisfying both size and alignment requirements), if none is found, 162 // add the field at the end of the layout. 163 // Fields cannot be inserted before the block specified in the "start" argument 164 void FieldLayout::add(GrowableArray<LayoutRawBlock*>* list, LayoutRawBlock* start) { 165 if (list == nullptr) return; 166 if (start == nullptr) start = this->_start; 167 bool last_search_success = false; 168 int last_size = 0; 169 int last_alignment = 0; 170 for (int i = 0; i < list->length(); i ++) { 171 LayoutRawBlock* b = list->at(i); 172 LayoutRawBlock* cursor = nullptr; 173 LayoutRawBlock* candidate = nullptr; 174 175 // if start is the last block, just append the field 176 if (start == last_block()) { 177 candidate = last_block(); 178 } 179 // Before iterating over the layout to find an empty slot fitting the field's requirements, 180 // check if the previous field had the same requirements and if the search for a fitting slot 181 // was successful. If the requirements were the same but the search failed, a new search will 182 // fail the same way, so just append the field at the of the layout. 183 else if (b->size() == last_size && b->alignment() == last_alignment && !last_search_success) { 184 candidate = last_block(); 185 } else { 186 // Iterate over the layout to find an empty slot fitting the field's requirements 187 last_size = b->size(); 188 last_alignment = b->alignment(); 189 cursor = last_block()->prev_block(); 190 assert(cursor != nullptr, "Sanity check"); 191 last_search_success = true; 192 while (cursor != start) { 193 if (cursor->kind() == LayoutRawBlock::EMPTY && cursor->fit(b->size(), b->alignment())) { 194 if (candidate == nullptr || cursor->size() < candidate->size()) { 195 candidate = cursor; 196 } 197 } 198 cursor = cursor->prev_block(); 199 } 200 if (candidate == nullptr) { 201 candidate = last_block(); 202 last_search_success = false; 203 } 204 assert(candidate != nullptr, "Candidate must not be null"); 205 assert(candidate->kind() == LayoutRawBlock::EMPTY, "Candidate must be an empty block"); 206 assert(candidate->fit(b->size(), b->alignment()), "Candidate must be able to store the block"); 207 } 208 209 insert_field_block(candidate, b); 210 } 211 } 212 213 // Used for classes with hard coded field offsets, insert a field at the specified offset */ 214 void FieldLayout::add_field_at_offset(LayoutRawBlock* block, int offset, LayoutRawBlock* start) { 215 assert(block != nullptr, "Sanity check"); 216 block->set_offset(offset); 217 if (start == nullptr) { 218 start = this->_start; 219 } 220 LayoutRawBlock* slot = start; 221 while (slot != nullptr) { 222 if ((slot->offset() <= block->offset() && (slot->offset() + slot->size()) > block->offset()) || 223 slot == _last){ 224 assert(slot->kind() == LayoutRawBlock::EMPTY, "Matching slot must be an empty slot"); 225 assert(slot->size() >= block->offset() + block->size() ,"Matching slot must be big enough"); 226 if (slot->offset() < block->offset()) { 227 int adjustment = block->offset() - slot->offset(); 228 LayoutRawBlock* adj = new LayoutRawBlock(LayoutRawBlock::EMPTY, adjustment); 229 insert(slot, adj); 230 } 231 insert(slot, block); 232 if (slot->size() == 0) { 233 remove(slot); 234 } 235 _field_info->adr_at(block->field_index())->set_offset(block->offset()); 236 return; 237 } 238 slot = slot->next_block(); 239 } 240 fatal("Should have found a matching slot above, corrupted layout or invalid offset"); 241 } 242 243 // The allocation logic uses a best fit strategy: the set of fields is allocated 244 // in the first empty slot big enough to contain the whole set ((including padding 245 // to fit alignment constraints). 246 void FieldLayout::add_contiguously(GrowableArray<LayoutRawBlock*>* list, LayoutRawBlock* start) { 247 if (list == nullptr) return; 248 if (start == nullptr) { 249 start = _start; 250 } 251 // This code assumes that if the first block is well aligned, the following 252 // blocks would naturally be well aligned (no need for adjustment) 253 int size = 0; 254 for (int i = 0; i < list->length(); i++) { 255 size += list->at(i)->size(); 256 } 257 258 LayoutRawBlock* candidate = nullptr; 259 if (start == last_block()) { 260 candidate = last_block(); 261 } else { 262 LayoutRawBlock* first = list->at(0); 263 candidate = last_block()->prev_block(); 264 while (candidate->kind() != LayoutRawBlock::EMPTY || !candidate->fit(size, first->alignment())) { 265 if (candidate == start) { 266 candidate = last_block(); 267 break; 268 } 269 candidate = candidate->prev_block(); 270 } 271 assert(candidate != nullptr, "Candidate must not be null"); 272 assert(candidate->kind() == LayoutRawBlock::EMPTY, "Candidate must be an empty block"); 273 assert(candidate->fit(size, first->alignment()), "Candidate must be able to store the whole contiguous block"); 274 } 275 276 for (int i = 0; i < list->length(); i++) { 277 LayoutRawBlock* b = list->at(i); 278 insert_field_block(candidate, b); 279 assert((candidate->offset() % b->alignment() == 0), "Contiguous blocks must be naturally well aligned"); 280 } 281 } 282 283 LayoutRawBlock* FieldLayout::insert_field_block(LayoutRawBlock* slot, LayoutRawBlock* block) { 284 assert(slot->kind() == LayoutRawBlock::EMPTY, "Blocks can only be inserted in empty blocks"); 285 if (slot->offset() % block->alignment() != 0) { 286 int adjustment = block->alignment() - (slot->offset() % block->alignment()); 287 LayoutRawBlock* adj = new LayoutRawBlock(LayoutRawBlock::EMPTY, adjustment); 288 insert(slot, adj); 289 } 290 insert(slot, block); 291 if (slot->size() == 0) { 292 remove(slot); 293 } 294 _field_info->adr_at(block->field_index())->set_offset(block->offset()); 295 return block; 296 } 297 298 void FieldLayout::reconstruct_layout(const InstanceKlass* ik, bool& has_instance_fields, bool& ends_with_oop) { 299 has_instance_fields = ends_with_oop = false; 300 GrowableArray<LayoutRawBlock*>* all_fields = new GrowableArray<LayoutRawBlock*>(32); 301 BasicType last_type; 302 int last_offset = -1; 303 while (ik != nullptr) { 304 for (AllFieldStream fs(ik->fieldinfo_stream(), ik->constants()); !fs.done(); fs.next()) { 305 BasicType type = Signature::basic_type(fs.signature()); 306 // distinction between static and non-static fields is missing 307 if (fs.access_flags().is_static()) continue; 308 has_instance_fields = true; 309 if (fs.offset() > last_offset) { 310 last_offset = fs.offset(); 311 last_type = type; 312 } 313 int size = type2aelembytes(type); 314 // INHERITED blocks are marked as non-reference because oop_maps are handled by their holder class 315 LayoutRawBlock* block = new LayoutRawBlock(fs.index(), LayoutRawBlock::INHERITED, size, size, false); 316 block->set_offset(fs.offset()); 317 all_fields->append(block); 318 } 319 ik = ik->super() == nullptr ? nullptr : InstanceKlass::cast(ik->super()); 320 } 321 assert(last_offset == -1 || last_offset > 0, "Sanity"); 322 if (last_offset > 0 && 323 (last_type == BasicType::T_ARRAY || last_type == BasicType::T_OBJECT)) { 324 ends_with_oop = true; 325 } 326 327 all_fields->sort(LayoutRawBlock::compare_offset); 328 _blocks = new LayoutRawBlock(LayoutRawBlock::RESERVED, instanceOopDesc::base_offset_in_bytes()); 329 _blocks->set_offset(0); 330 _last = _blocks; 331 332 for(int i = 0; i < all_fields->length(); i++) { 333 LayoutRawBlock* b = all_fields->at(i); 334 _last->set_next_block(b); 335 b->set_prev_block(_last); 336 _last = b; 337 } 338 _start = _blocks; 339 } 340 341 // Called during the reconstruction of a layout, after fields from super 342 // classes have been inserted. It fills unused slots between inserted fields 343 // with EMPTY blocks, so the regular field insertion methods would work. 344 // This method handles classes with @Contended annotations differently 345 // by inserting PADDING blocks instead of EMPTY block to prevent subclasses' 346 // fields to interfere with contended fields/classes. 347 void FieldLayout::fill_holes(const InstanceKlass* super_klass) { 348 assert(_blocks != nullptr, "Sanity check"); 349 assert(_blocks->offset() == 0, "first block must be at offset zero"); 350 LayoutRawBlock::Kind filling_type = super_klass->has_contended_annotations() ? LayoutRawBlock::PADDING: LayoutRawBlock::EMPTY; 351 LayoutRawBlock* b = _blocks; 352 while (b->next_block() != nullptr) { 353 if (b->next_block()->offset() > (b->offset() + b->size())) { 354 int size = b->next_block()->offset() - (b->offset() + b->size()); 355 LayoutRawBlock* empty = new LayoutRawBlock(filling_type, size); 356 empty->set_offset(b->offset() + b->size()); 357 empty->set_next_block(b->next_block()); 358 b->next_block()->set_prev_block(empty); 359 b->set_next_block(empty); 360 empty->set_prev_block(b); 361 } 362 b = b->next_block(); 363 } 364 assert(b->next_block() == nullptr, "Invariant at this point"); 365 assert(b->kind() != LayoutRawBlock::EMPTY, "Sanity check"); 366 367 // If the super class has @Contended annotation, a padding block is 368 // inserted at the end to ensure that fields from the subclasses won't share 369 // the cache line of the last field of the contended class 370 if (super_klass->has_contended_annotations() && ContendedPaddingWidth > 0) { 371 LayoutRawBlock* p = new LayoutRawBlock(LayoutRawBlock::PADDING, ContendedPaddingWidth); 372 p->set_offset(b->offset() + b->size()); 373 b->set_next_block(p); 374 p->set_prev_block(b); 375 b = p; 376 } 377 378 LayoutRawBlock* last = new LayoutRawBlock(LayoutRawBlock::EMPTY, INT_MAX); 379 last->set_offset(b->offset() + b->size()); 380 assert(last->offset() > 0, "Sanity check"); 381 b->set_next_block(last); 382 last->set_prev_block(b); 383 _last = last; 384 } 385 386 LayoutRawBlock* FieldLayout::insert(LayoutRawBlock* slot, LayoutRawBlock* block) { 387 assert(slot->kind() == LayoutRawBlock::EMPTY, "Blocks can only be inserted in empty blocks"); 388 assert(slot->offset() % block->alignment() == 0, "Incompatible alignment"); 389 block->set_offset(slot->offset()); 390 slot->set_offset(slot->offset() + block->size()); 391 assert((slot->size() - block->size()) < slot->size(), "underflow checking"); 392 assert(slot->size() - block->size() >= 0, "no negative size allowed"); 393 slot->set_size(slot->size() - block->size()); 394 block->set_prev_block(slot->prev_block()); 395 block->set_next_block(slot); 396 slot->set_prev_block(block); 397 if (block->prev_block() != nullptr) { 398 block->prev_block()->set_next_block(block); 399 } 400 if (_blocks == slot) { 401 _blocks = block; 402 } 403 return block; 404 } 405 406 void FieldLayout::remove(LayoutRawBlock* block) { 407 assert(block != nullptr, "Sanity check"); 408 assert(block != _last, "Sanity check"); 409 if (_blocks == block) { 410 _blocks = block->next_block(); 411 if (_blocks != nullptr) { 412 _blocks->set_prev_block(nullptr); 413 } 414 } else { 415 assert(block->prev_block() != nullptr, "_prev should be set for non-head blocks"); 416 block->prev_block()->set_next_block(block->next_block()); 417 block->next_block()->set_prev_block(block->prev_block()); 418 } 419 if (block == _start) { 420 _start = block->prev_block(); 421 } 422 } 423 424 void FieldLayout::print(outputStream* output, bool is_static, const InstanceKlass* super) { 425 ResourceMark rm; 426 LayoutRawBlock* b = _blocks; 427 while(b != _last) { 428 switch(b->kind()) { 429 case LayoutRawBlock::REGULAR: { 430 FieldInfo* fi = _field_info->adr_at(b->field_index()); 431 output->print_cr(" @%d \"%s\" %s %d/%d %s", 432 b->offset(), 433 fi->name(_cp)->as_C_string(), 434 fi->signature(_cp)->as_C_string(), 435 b->size(), 436 b->alignment(), 437 "REGULAR"); 438 break; 439 } 440 case LayoutRawBlock::FLATTENED: { 441 FieldInfo* fi = _field_info->adr_at(b->field_index()); 442 output->print_cr(" @%d \"%s\" %s %d/%d %s", 443 b->offset(), 444 fi->name(_cp)->as_C_string(), 445 fi->signature(_cp)->as_C_string(), 446 b->size(), 447 b->alignment(), 448 "FLATTENED"); 449 break; 450 } 451 case LayoutRawBlock::RESERVED: { 452 output->print_cr(" @%d %d/- %s", 453 b->offset(), 454 b->size(), 455 "RESERVED"); 456 break; 457 } 458 case LayoutRawBlock::INHERITED: { 459 assert(!is_static, "Static fields are not inherited in layouts"); 460 assert(super != nullptr, "super klass must be provided to retrieve inherited fields info"); 461 bool found = false; 462 const InstanceKlass* ik = super; 463 while (!found && ik != nullptr) { 464 for (AllFieldStream fs(ik->fieldinfo_stream(), ik->constants()); !fs.done(); fs.next()) { 465 if (fs.offset() == b->offset()) { 466 output->print_cr(" @%d \"%s\" %s %d/%d %s", 467 b->offset(), 468 fs.name()->as_C_string(), 469 fs.signature()->as_C_string(), 470 b->size(), 471 b->size(), // so far, alignment constraint == size, will change with Valhalla 472 "INHERITED"); 473 found = true; 474 break; 475 } 476 } 477 ik = ik->java_super(); 478 } 479 break; 480 } 481 case LayoutRawBlock::EMPTY: 482 output->print_cr(" @%d %d/1 %s", 483 b->offset(), 484 b->size(), 485 "EMPTY"); 486 break; 487 case LayoutRawBlock::PADDING: 488 output->print_cr(" @%d %d/1 %s", 489 b->offset(), 490 b->size(), 491 "PADDING"); 492 break; 493 } 494 b = b->next_block(); 495 } 496 } 497 498 FieldLayoutBuilder::FieldLayoutBuilder(const Symbol* classname, const InstanceKlass* super_klass, ConstantPool* constant_pool, 499 GrowableArray<FieldInfo>* field_info, bool is_contended, FieldLayoutInfo* info) : 500 _classname(classname), 501 _super_klass(super_klass), 502 _constant_pool(constant_pool), 503 _field_info(field_info), 504 _info(info), 505 _root_group(nullptr), 506 _contended_groups(GrowableArray<FieldGroup*>(8)), 507 _static_fields(nullptr), 508 _layout(nullptr), 509 _static_layout(nullptr), 510 _nonstatic_oopmap_count(0), 511 _alignment(-1), 512 _has_nonstatic_fields(false), 513 _is_contended(is_contended) {} 514 515 516 FieldGroup* FieldLayoutBuilder::get_or_create_contended_group(int g) { 517 assert(g > 0, "must only be called for named contended groups"); 518 FieldGroup* fg = nullptr; 519 for (int i = 0; i < _contended_groups.length(); i++) { 520 fg = _contended_groups.at(i); 521 if (fg->contended_group() == g) return fg; 522 } 523 fg = new FieldGroup(g); 524 _contended_groups.append(fg); 525 return fg; 526 } 527 528 void FieldLayoutBuilder::prologue() { 529 _layout = new FieldLayout(_field_info, _constant_pool); 530 const InstanceKlass* super_klass = _super_klass; 531 _layout->initialize_instance_layout(super_klass, _super_ends_with_oop); 532 if (super_klass != nullptr) { 533 _has_nonstatic_fields = super_klass->has_nonstatic_fields(); 534 } 535 _static_layout = new FieldLayout(_field_info, _constant_pool); 536 _static_layout->initialize_static_layout(); 537 _static_fields = new FieldGroup(); 538 _root_group = new FieldGroup(); 539 } 540 541 // Field sorting for regular classes: 542 // - fields are sorted in static and non-static fields 543 // - non-static fields are also sorted according to their contention group 544 // (support of the @Contended annotation) 545 // - @Contended annotation is ignored for static fields 546 void FieldLayoutBuilder::regular_field_sorting() { 547 int idx = 0; 548 for (GrowableArrayIterator<FieldInfo> it = _field_info->begin(); it != _field_info->end(); ++it, ++idx) { 549 FieldInfo ctrl = _field_info->at(0); 550 FieldGroup* group = nullptr; 551 FieldInfo fieldinfo = *it; 552 if (fieldinfo.access_flags().is_static()) { 553 group = _static_fields; 554 } else { 555 _has_nonstatic_fields = true; 556 if (fieldinfo.field_flags().is_contended()) { 557 int g = fieldinfo.contended_group(); 558 if (g == 0) { 559 group = new FieldGroup(true); 560 _contended_groups.append(group); 561 } else { 562 group = get_or_create_contended_group(g); 563 } 564 } else { 565 group = _root_group; 566 } 567 } 568 assert(group != nullptr, "invariant"); 569 BasicType type = Signature::basic_type(fieldinfo.signature(_constant_pool)); 570 switch(type) { 571 case T_BYTE: 572 case T_CHAR: 573 case T_DOUBLE: 574 case T_FLOAT: 575 case T_INT: 576 case T_LONG: 577 case T_SHORT: 578 case T_BOOLEAN: 579 group->add_primitive_field(idx, type); 580 break; 581 case T_OBJECT: 582 case T_ARRAY: 583 if (group != _static_fields) _nonstatic_oopmap_count++; 584 group->add_oop_field(idx); 585 break; 586 default: 587 fatal("Something wrong?"); 588 } 589 } 590 _root_group->sort_by_size(); 591 _static_fields->sort_by_size(); 592 if (!_contended_groups.is_empty()) { 593 for (int i = 0; i < _contended_groups.length(); i++) { 594 _contended_groups.at(i)->sort_by_size(); 595 } 596 } 597 } 598 599 void FieldLayoutBuilder::insert_contended_padding(LayoutRawBlock* slot) { 600 if (ContendedPaddingWidth > 0) { 601 LayoutRawBlock* padding = new LayoutRawBlock(LayoutRawBlock::PADDING, ContendedPaddingWidth); 602 _layout->insert(slot, padding); 603 } 604 } 605 606 // Computation of regular classes layout is an evolution of the previous default layout 607 // (FieldAllocationStyle 1): 608 // - primitive fields are allocated first (from the biggest to the smallest) 609 // - oop fields are allocated, either in existing gaps or at the end of 610 // the layout. We allocate oops in a single block to have a single oop map entry. 611 // - if the super class ended with an oop, we lead with oops. That will cause the 612 // trailing oop map entry of the super class and the oop map entry of this class 613 // to be folded into a single entry later. Correspondingly, if the super class 614 // ends with a primitive field, we gain nothing by leading with oops; therefore 615 // we let oop fields trail, thus giving future derived classes the chance to apply 616 // the same trick. 617 void FieldLayoutBuilder::compute_regular_layout() { 618 bool need_tail_padding = false; 619 prologue(); 620 regular_field_sorting(); 621 622 if (_is_contended) { 623 _layout->set_start(_layout->last_block()); 624 // insertion is currently easy because the current strategy doesn't try to fill holes 625 // in super classes layouts => the _start block is by consequence the _last_block 626 insert_contended_padding(_layout->start()); 627 need_tail_padding = true; 628 } 629 630 if (_super_ends_with_oop) { 631 _layout->add(_root_group->oop_fields()); 632 _layout->add(_root_group->primitive_fields()); 633 } else { 634 _layout->add(_root_group->primitive_fields()); 635 _layout->add(_root_group->oop_fields()); 636 } 637 638 if (!_contended_groups.is_empty()) { 639 for (int i = 0; i < _contended_groups.length(); i++) { 640 FieldGroup* cg = _contended_groups.at(i); 641 LayoutRawBlock* start = _layout->last_block(); 642 insert_contended_padding(start); 643 _layout->add(cg->primitive_fields(), start); 644 _layout->add(cg->oop_fields(), start); 645 need_tail_padding = true; 646 } 647 } 648 649 if (need_tail_padding) { 650 insert_contended_padding(_layout->last_block()); 651 } 652 653 _static_layout->add_contiguously(this->_static_fields->oop_fields()); 654 _static_layout->add(this->_static_fields->primitive_fields()); 655 656 epilogue(); 657 } 658 659 void FieldLayoutBuilder::epilogue() { 660 // Computing oopmaps 661 int super_oop_map_count = (_super_klass == nullptr) ? 0 :_super_klass->nonstatic_oop_map_count(); 662 int max_oop_map_count = super_oop_map_count + _nonstatic_oopmap_count; 663 664 OopMapBlocksBuilder* nonstatic_oop_maps = 665 new OopMapBlocksBuilder(max_oop_map_count); 666 if (super_oop_map_count > 0) { 667 nonstatic_oop_maps->initialize_inherited_blocks(_super_klass->start_of_nonstatic_oop_maps(), 668 _super_klass->nonstatic_oop_map_count()); 669 } 670 671 if (_root_group->oop_fields() != nullptr) { 672 for (int i = 0; i < _root_group->oop_fields()->length(); i++) { 673 LayoutRawBlock* b = _root_group->oop_fields()->at(i); 674 nonstatic_oop_maps->add(b->offset(), 1); 675 } 676 } 677 678 if (!_contended_groups.is_empty()) { 679 for (int i = 0; i < _contended_groups.length(); i++) { 680 FieldGroup* cg = _contended_groups.at(i); 681 if (cg->oop_count() > 0) { 682 assert(cg->oop_fields() != nullptr && cg->oop_fields()->at(0) != nullptr, "oop_count > 0 but no oop fields found"); 683 nonstatic_oop_maps->add(cg->oop_fields()->at(0)->offset(), cg->oop_count()); 684 } 685 } 686 } 687 688 nonstatic_oop_maps->compact(); 689 690 int instance_end = align_up(_layout->last_block()->offset(), wordSize); 691 int static_fields_end = align_up(_static_layout->last_block()->offset(), wordSize); 692 int static_fields_size = (static_fields_end - 693 InstanceMirrorKlass::offset_of_static_fields()) / wordSize; 694 int nonstatic_field_end = align_up(_layout->last_block()->offset(), heapOopSize); 695 696 // Pass back information needed for InstanceKlass creation 697 698 _info->oop_map_blocks = nonstatic_oop_maps; 699 _info->_instance_size = align_object_size(instance_end / wordSize); 700 _info->_static_field_size = static_fields_size; 701 _info->_nonstatic_field_size = (nonstatic_field_end - instanceOopDesc::base_offset_in_bytes()) / heapOopSize; 702 _info->_has_nonstatic_fields = _has_nonstatic_fields; 703 704 if (PrintFieldLayout) { 705 ResourceMark rm; 706 tty->print_cr("Layout of class %s", _classname->as_C_string()); 707 tty->print_cr("Instance fields:"); 708 _layout->print(tty, false, _super_klass); 709 tty->print_cr("Static fields:"); 710 _static_layout->print(tty, true, nullptr); 711 tty->print_cr("Instance size = %d bytes", _info->_instance_size * wordSize); 712 tty->print_cr("---"); 713 } 714 } 715 716 void FieldLayoutBuilder::build_layout() { 717 compute_regular_layout(); 718 }