1 /* 2 * Copyright (c) 2020, 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 "precompiled.hpp" 26 #include "jvm.h" 27 #include "classfile/classFileParser.hpp" 28 #include "classfile/fieldLayoutBuilder.hpp" 29 #include "memory/resourceArea.hpp" 30 #include "oops/array.hpp" 31 #include "oops/fieldStreams.inline.hpp" 32 #include "oops/instanceMirrorKlass.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(NULL), 39 _prev_block(NULL), 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(NULL), 54 _prev_block(NULL), 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(NULL), 77 _primitive_fields(NULL), 78 _oop_fields(NULL), 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(AllFieldStream fs, BasicType type) { 83 int size = type2aelembytes(type); 84 LayoutRawBlock* block = new LayoutRawBlock(fs.index(), LayoutRawBlock::REGULAR, size, size /* alignment == size for primitive types */, false); 85 if (_primitive_fields == NULL) { 86 _primitive_fields = new(ResourceObj::RESOURCE_AREA, mtInternal) GrowableArray<LayoutRawBlock*>(INITIAL_LIST_SIZE); 87 } 88 _primitive_fields->append(block); 89 } 90 91 void FieldGroup::add_oop_field(AllFieldStream fs) { 92 int size = type2aelembytes(T_OBJECT); 93 LayoutRawBlock* block = new LayoutRawBlock(fs.index(), LayoutRawBlock::REGULAR, size, size /* alignment == size for oops */, true); 94 if (_oop_fields == NULL) { 95 _oop_fields = new(ResourceObj::RESOURCE_AREA, mtInternal) 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 != NULL) { 103 _primitive_fields->sort(LayoutRawBlock::compare_size_inverted); 104 } 105 } 106 107 FieldLayout::FieldLayout(Array<u2>* fields, ConstantPool* cp) : 108 _fields(fields), 109 _cp(cp), 110 _blocks(NULL), 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 == NULL) { 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 ((UseEmptySlotsInSupers && !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 == NULL) return; 164 if (start == NULL) 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 = NULL; 171 LayoutRawBlock* candidate = NULL; 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 != NULL, "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 == NULL || cursor->size() < candidate->size()) { 193 candidate = cursor; 194 } 195 } 196 cursor = cursor->prev_block(); 197 } 198 if (candidate == NULL) { 199 candidate = last_block(); 200 last_search_success = false; 201 } 202 assert(candidate != NULL, "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 != NULL, "Sanity check"); 214 block->set_offset(offset); 215 if (start == NULL) { 216 start = this->_start; 217 } 218 LayoutRawBlock* slot = start; 219 while (slot != NULL) { 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 FieldInfo::from_field_array(_fields, 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 == NULL) return; 246 if (start == NULL) { 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 = NULL; 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 != NULL, "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 FieldInfo::from_field_array(_fields, 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 != NULL) { 300 for (AllFieldStream fs(ik->fields(), 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() == NULL ? NULL : 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 != NULL, "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() != NULL) { 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() == NULL, "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 if (!UseEmptySlotsInSupers) { 367 // Add an empty slots to align fields of the subclass on a heapOopSize boundary 368 // in order to emulate the behavior of the previous algorithm 369 int align = (b->offset() + b->size()) % heapOopSize; 370 if (align != 0) { 371 int sz = heapOopSize - align; 372 LayoutRawBlock* p = new LayoutRawBlock(LayoutRawBlock::EMPTY, sz); 373 p->set_offset(b->offset() + b->size()); 374 b->set_next_block(p); 375 p->set_prev_block(b); 376 b = p; 377 } 378 } 379 380 LayoutRawBlock* last = new LayoutRawBlock(LayoutRawBlock::EMPTY, INT_MAX); 381 last->set_offset(b->offset() + b->size()); 382 assert(last->offset() > 0, "Sanity check"); 383 b->set_next_block(last); 384 last->set_prev_block(b); 385 _last = last; 386 } 387 388 LayoutRawBlock* FieldLayout::insert(LayoutRawBlock* slot, LayoutRawBlock* block) { 389 assert(slot->kind() == LayoutRawBlock::EMPTY, "Blocks can only be inserted in empty blocks"); 390 assert(slot->offset() % block->alignment() == 0, "Incompatible alignment"); 391 block->set_offset(slot->offset()); 392 slot->set_offset(slot->offset() + block->size()); 393 assert((slot->size() - block->size()) < slot->size(), "underflow checking"); 394 assert(slot->size() - block->size() >= 0, "no negative size allowed"); 395 slot->set_size(slot->size() - block->size()); 396 block->set_prev_block(slot->prev_block()); 397 block->set_next_block(slot); 398 slot->set_prev_block(block); 399 if (block->prev_block() != NULL) { 400 block->prev_block()->set_next_block(block); 401 } 402 if (_blocks == slot) { 403 _blocks = block; 404 } 405 return block; 406 } 407 408 void FieldLayout::remove(LayoutRawBlock* block) { 409 assert(block != NULL, "Sanity check"); 410 assert(block != _last, "Sanity check"); 411 if (_blocks == block) { 412 _blocks = block->next_block(); 413 if (_blocks != NULL) { 414 _blocks->set_prev_block(NULL); 415 } 416 } else { 417 assert(block->prev_block() != NULL, "_prev should be set for non-head blocks"); 418 block->prev_block()->set_next_block(block->next_block()); 419 block->next_block()->set_prev_block(block->prev_block()); 420 } 421 if (block == _start) { 422 _start = block->prev_block(); 423 } 424 } 425 426 void FieldLayout::print(outputStream* output, bool is_static, const InstanceKlass* super) { 427 ResourceMark rm; 428 LayoutRawBlock* b = _blocks; 429 while(b != _last) { 430 switch(b->kind()) { 431 case LayoutRawBlock::REGULAR: { 432 FieldInfo* fi = FieldInfo::from_field_array(_fields, b->field_index()); 433 output->print_cr(" @%d \"%s\" %s %d/%d %s", 434 b->offset(), 435 fi->name(_cp)->as_C_string(), 436 fi->signature(_cp)->as_C_string(), 437 b->size(), 438 b->alignment(), 439 "REGULAR"); 440 break; 441 } 442 case LayoutRawBlock::FLATTENED: { 443 FieldInfo* fi = FieldInfo::from_field_array(_fields, b->field_index()); 444 output->print_cr(" @%d \"%s\" %s %d/%d %s", 445 b->offset(), 446 fi->name(_cp)->as_C_string(), 447 fi->signature(_cp)->as_C_string(), 448 b->size(), 449 b->alignment(), 450 "FLATTENED"); 451 break; 452 } 453 case LayoutRawBlock::RESERVED: { 454 output->print_cr(" @%d %d/- %s", 455 b->offset(), 456 b->size(), 457 "RESERVED"); 458 break; 459 } 460 case LayoutRawBlock::INHERITED: { 461 assert(!is_static, "Static fields are not inherited in layouts"); 462 assert(super != NULL, "super klass must be provided to retrieve inherited fields info"); 463 bool found = false; 464 const InstanceKlass* ik = super; 465 while (!found && ik != NULL) { 466 for (AllFieldStream fs(ik->fields(), ik->constants()); !fs.done(); fs.next()) { 467 if (fs.offset() == b->offset()) { 468 output->print_cr(" @%d \"%s\" %s %d/%d %s", 469 b->offset(), 470 fs.name()->as_C_string(), 471 fs.signature()->as_C_string(), 472 b->size(), 473 b->size(), // so far, alignment constraint == size, will change with Valhalla 474 "INHERITED"); 475 found = true; 476 break; 477 } 478 } 479 ik = ik->java_super(); 480 } 481 break; 482 } 483 case LayoutRawBlock::EMPTY: 484 output->print_cr(" @%d %d/1 %s", 485 b->offset(), 486 b->size(), 487 "EMPTY"); 488 break; 489 case LayoutRawBlock::PADDING: 490 output->print_cr(" @%d %d/1 %s", 491 b->offset(), 492 b->size(), 493 "PADDING"); 494 break; 495 } 496 b = b->next_block(); 497 } 498 } 499 500 FieldLayoutBuilder::FieldLayoutBuilder(const Symbol* classname, const InstanceKlass* super_klass, ConstantPool* constant_pool, 501 Array<u2>* fields, bool is_contended, FieldLayoutInfo* info) : 502 _classname(classname), 503 _super_klass(super_klass), 504 _constant_pool(constant_pool), 505 _fields(fields), 506 _info(info), 507 _root_group(NULL), 508 _contended_groups(GrowableArray<FieldGroup*>(8)), 509 _static_fields(NULL), 510 _layout(NULL), 511 _static_layout(NULL), 512 _nonstatic_oopmap_count(0), 513 _alignment(-1), 514 _has_nonstatic_fields(false), 515 _is_contended(is_contended) {} 516 517 518 FieldGroup* FieldLayoutBuilder::get_or_create_contended_group(int g) { 519 assert(g > 0, "must only be called for named contended groups"); 520 FieldGroup* fg = NULL; 521 for (int i = 0; i < _contended_groups.length(); i++) { 522 fg = _contended_groups.at(i); 523 if (fg->contended_group() == g) return fg; 524 } 525 fg = new FieldGroup(g); 526 _contended_groups.append(fg); 527 return fg; 528 } 529 530 void FieldLayoutBuilder::prologue() { 531 _layout = new FieldLayout(_fields, _constant_pool); 532 const InstanceKlass* super_klass = _super_klass; 533 _layout->initialize_instance_layout(super_klass); 534 if (super_klass != NULL) { 535 _has_nonstatic_fields = super_klass->has_nonstatic_fields(); 536 } 537 _static_layout = new FieldLayout(_fields, _constant_pool); 538 _static_layout->initialize_static_layout(); 539 _static_fields = new FieldGroup(); 540 _root_group = new FieldGroup(); 541 } 542 543 // Field sorting for regular classes: 544 // - fields are sorted in static and non-static fields 545 // - non-static fields are also sorted according to their contention group 546 // (support of the @Contended annotation) 547 // - @Contended annotation is ignored for static fields 548 void FieldLayoutBuilder::regular_field_sorting() { 549 for (AllFieldStream fs(_fields, _constant_pool); !fs.done(); fs.next()) { 550 FieldGroup* group = NULL; 551 if (fs.access_flags().is_static()) { 552 group = _static_fields; 553 } else { 554 _has_nonstatic_fields = true; 555 if (fs.is_contended()) { 556 int g = fs.contended_group(); 557 if (g == 0) { 558 group = new FieldGroup(true); 559 _contended_groups.append(group); 560 } else { 561 group = get_or_create_contended_group(g); 562 } 563 } else { 564 group = _root_group; 565 } 566 } 567 assert(group != NULL, "invariant"); 568 BasicType type = Signature::basic_type(fs.signature()); 569 switch(type) { 570 case T_BYTE: 571 case T_CHAR: 572 case T_DOUBLE: 573 case T_FLOAT: 574 case T_INT: 575 case T_LONG: 576 case T_SHORT: 577 case T_BOOLEAN: 578 group->add_primitive_field(fs, type); 579 break; 580 case T_OBJECT: 581 case T_ARRAY: 582 if (group != _static_fields) _nonstatic_oopmap_count++; 583 group->add_oop_field(fs); 584 break; 585 default: 586 fatal("Something wrong?"); 587 } 588 } 589 _root_group->sort_by_size(); 590 _static_fields->sort_by_size(); 591 if (!_contended_groups.is_empty()) { 592 for (int i = 0; i < _contended_groups.length(); i++) { 593 _contended_groups.at(i)->sort_by_size(); 594 } 595 } 596 } 597 598 void FieldLayoutBuilder::insert_contended_padding(LayoutRawBlock* slot) { 599 if (ContendedPaddingWidth > 0) { 600 LayoutRawBlock* padding = new LayoutRawBlock(LayoutRawBlock::PADDING, ContendedPaddingWidth); 601 _layout->insert(slot, padding); 602 } 603 } 604 605 // Computation of regular classes layout is an evolution of the previous default layout 606 // (FieldAllocationStyle 1): 607 // - primitive fields are allocated first (from the biggest to the smallest) 608 // - then oop fields are allocated, either in existing gaps or at the end of 609 // the layout 610 void FieldLayoutBuilder::compute_regular_layout() { 611 bool need_tail_padding = false; 612 prologue(); 613 regular_field_sorting(); 614 615 if (_is_contended) { 616 _layout->set_start(_layout->last_block()); 617 // insertion is currently easy because the current strategy doesn't try to fill holes 618 // in super classes layouts => the _start block is by consequence the _last_block 619 insert_contended_padding(_layout->start()); 620 need_tail_padding = true; 621 } 622 _layout->add(_root_group->primitive_fields()); 623 _layout->add(_root_group->oop_fields()); 624 625 if (!_contended_groups.is_empty()) { 626 for (int i = 0; i < _contended_groups.length(); i++) { 627 FieldGroup* cg = _contended_groups.at(i); 628 LayoutRawBlock* start = _layout->last_block(); 629 insert_contended_padding(start); 630 _layout->add(cg->primitive_fields(), start); 631 _layout->add(cg->oop_fields(), start); 632 need_tail_padding = true; 633 } 634 } 635 636 if (need_tail_padding) { 637 insert_contended_padding(_layout->last_block()); 638 } 639 640 _static_layout->add_contiguously(this->_static_fields->oop_fields()); 641 _static_layout->add(this->_static_fields->primitive_fields()); 642 643 epilogue(); 644 } 645 646 // Compute layout of the java/lang/ref/Reference class according 647 // to the hard coded offsets of its fields 648 void FieldLayoutBuilder::compute_java_lang_ref_Reference_layout() { 649 prologue(); 650 regular_field_sorting(); 651 652 assert(_contended_groups.is_empty(), "java.lang.Reference has no @Contended annotations"); 653 assert(_root_group->primitive_fields() == NULL, "java.lang.Reference has no nonstatic primitive fields"); 654 int field_count = 0; 655 int offset = -1; 656 for (int i = 0; i < _root_group->oop_fields()->length(); i++) { 657 LayoutRawBlock* b = _root_group->oop_fields()->at(i); 658 FieldInfo* fi = FieldInfo::from_field_array(_fields, b->field_index()); 659 if (fi->name(_constant_pool)->equals("referent")) { 660 offset = java_lang_ref_Reference::referent_offset; 661 } else if (fi->name(_constant_pool)->equals("queue")) { 662 offset = java_lang_ref_Reference::queue_offset; 663 } else if (fi->name(_constant_pool)->equals("next")) { 664 offset = java_lang_ref_Reference::next_offset; 665 } else if (fi->name(_constant_pool)->equals("discovered")) { 666 offset = java_lang_ref_Reference::discovered_offset; 667 } 668 assert(offset != -1, "Unknown field"); 669 _layout->add_field_at_offset(b, offset); 670 field_count++; 671 } 672 assert(field_count == 4, "Wrong number of fields in java.lang.ref.Reference"); 673 674 _static_layout->add_contiguously(this->_static_fields->oop_fields()); 675 _static_layout->add(this->_static_fields->primitive_fields()); 676 677 epilogue(); 678 } 679 680 // Compute layout of the boxing class according 681 // to the hard coded offsets of their fields 682 void FieldLayoutBuilder::compute_boxing_class_layout() { 683 prologue(); 684 regular_field_sorting(); 685 686 assert(_contended_groups.is_empty(), "Boxing classes have no @Contended annotations"); 687 assert(_root_group->oop_fields() == NULL, "Boxing classes have no nonstatic oops fields"); 688 int field_count = 0; 689 int offset = -1; 690 691 for (int i = 0; i < _root_group->primitive_fields()->length(); i++) { 692 LayoutRawBlock* b = _root_group->primitive_fields()->at(i); 693 FieldInfo* fi = FieldInfo::from_field_array(_fields, b->field_index()); 694 assert(fi->name(_constant_pool)->equals("value"), "Boxing classes have a single nonstatic field named 'value'"); 695 BasicType type = Signature::basic_type(fi->signature(_constant_pool)); 696 offset = java_lang_boxing_object::value_offset_in_bytes(type); 697 assert(offset != -1, "Unknown field"); 698 _layout->add_field_at_offset(b, offset); 699 field_count++; 700 } 701 assert(field_count == 1, "Wrong number of fields for a boxing class"); 702 703 _static_layout->add_contiguously(this->_static_fields->oop_fields()); 704 _static_layout->add(this->_static_fields->primitive_fields()); 705 706 epilogue(); 707 } 708 709 void FieldLayoutBuilder::epilogue() { 710 // Computing oopmaps 711 int super_oop_map_count = (_super_klass == NULL) ? 0 :_super_klass->nonstatic_oop_map_count(); 712 int max_oop_map_count = super_oop_map_count + _nonstatic_oopmap_count; 713 714 OopMapBlocksBuilder* nonstatic_oop_maps = 715 new OopMapBlocksBuilder(max_oop_map_count); 716 if (super_oop_map_count > 0) { 717 nonstatic_oop_maps->initialize_inherited_blocks(_super_klass->start_of_nonstatic_oop_maps(), 718 _super_klass->nonstatic_oop_map_count()); 719 } 720 721 if (_root_group->oop_fields() != NULL) { 722 for (int i = 0; i < _root_group->oop_fields()->length(); i++) { 723 LayoutRawBlock* b = _root_group->oop_fields()->at(i); 724 nonstatic_oop_maps->add(b->offset(), 1); 725 } 726 } 727 728 if (!_contended_groups.is_empty()) { 729 for (int i = 0; i < _contended_groups.length(); i++) { 730 FieldGroup* cg = _contended_groups.at(i); 731 if (cg->oop_count() > 0) { 732 assert(cg->oop_fields() != NULL && cg->oop_fields()->at(0) != NULL, "oop_count > 0 but no oop fields found"); 733 nonstatic_oop_maps->add(cg->oop_fields()->at(0)->offset(), cg->oop_count()); 734 } 735 } 736 } 737 738 nonstatic_oop_maps->compact(); 739 740 int instance_end = align_up(_layout->last_block()->offset(), wordSize); 741 int static_fields_end = align_up(_static_layout->last_block()->offset(), wordSize); 742 int static_fields_size = (static_fields_end - 743 InstanceMirrorKlass::offset_of_static_fields()) / wordSize; 744 int nonstatic_field_end = align_up(_layout->last_block()->offset(), heapOopSize); 745 746 // Pass back information needed for InstanceKlass creation 747 748 _info->oop_map_blocks = nonstatic_oop_maps; 749 _info->_instance_size = align_object_size(instance_end / wordSize); 750 _info->_static_field_size = static_fields_size; 751 _info->_nonstatic_field_size = (nonstatic_field_end - instanceOopDesc::base_offset_in_bytes()) / heapOopSize; 752 _info->_has_nonstatic_fields = _has_nonstatic_fields; 753 754 #ifndef PRODUCT 755 if (PrintFieldLayout) { 756 ResourceMark rm; 757 tty->print_cr("Layout of class %s", _classname->as_C_string()); 758 tty->print_cr("Instance fields:"); 759 _layout->print(tty, false, _super_klass); 760 tty->print_cr("Static fields:"); 761 _static_layout->print(tty, true, NULL); 762 tty->print_cr("Instance size = %d bytes", _info->_instance_size * wordSize); 763 tty->print_cr("---"); 764 } 765 #endif 766 } 767 768 void FieldLayoutBuilder::build_layout() { 769 if (_classname == vmSymbols::java_lang_ref_Reference()) { 770 compute_java_lang_ref_Reference_layout(); 771 } else if (_classname == vmSymbols::java_lang_Boolean() || 772 _classname == vmSymbols::java_lang_Character() || 773 _classname == vmSymbols::java_lang_Float() || 774 _classname == vmSymbols::java_lang_Double() || 775 _classname == vmSymbols::java_lang_Byte() || 776 _classname == vmSymbols::java_lang_Short() || 777 _classname == vmSymbols::java_lang_Integer() || 778 _classname == vmSymbols::java_lang_Long()) { 779 compute_boxing_class_layout(); 780 } else { 781 compute_regular_layout(); 782 } 783 } 784