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