1 /* 2 * Copyright (c) 2000, 2023, 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 "ci/ciMethodData.hpp" 27 #include "classfile/vmSymbols.hpp" 28 #include "compiler/compilationPolicy.hpp" 29 #include "compiler/compilerDefinitions.inline.hpp" 30 #include "compiler/compilerOracle.hpp" 31 #include "interpreter/bytecode.hpp" 32 #include "interpreter/bytecodeStream.hpp" 33 #include "interpreter/linkResolver.hpp" 34 #include "memory/metaspaceClosure.hpp" 35 #include "memory/resourceArea.hpp" 36 #include "oops/klass.inline.hpp" 37 #include "oops/methodData.inline.hpp" 38 #include "prims/jvmtiRedefineClasses.hpp" 39 #include "runtime/atomic.hpp" 40 #include "runtime/deoptimization.hpp" 41 #include "runtime/handles.inline.hpp" 42 #include "runtime/orderAccess.hpp" 43 #include "runtime/safepointVerifiers.hpp" 44 #include "runtime/signature.hpp" 45 #include "utilities/align.hpp" 46 #include "utilities/checkedCast.hpp" 47 #include "utilities/copy.hpp" 48 49 // ================================================================== 50 // DataLayout 51 // 52 // Overlay for generic profiling data. 53 54 // Some types of data layouts need a length field. 55 bool DataLayout::needs_array_len(u1 tag) { 56 return (tag == multi_branch_data_tag) || (tag == arg_info_data_tag) || (tag == parameters_type_data_tag); 57 } 58 59 // Perform generic initialization of the data. More specific 60 // initialization occurs in overrides of ProfileData::post_initialize. 61 void DataLayout::initialize(u1 tag, u2 bci, int cell_count) { 62 _header._bits = (intptr_t)0; 63 _header._struct._tag = tag; 64 _header._struct._bci = bci; 65 for (int i = 0; i < cell_count; i++) { 66 set_cell_at(i, (intptr_t)0); 67 } 68 if (needs_array_len(tag)) { 69 set_cell_at(ArrayData::array_len_off_set, cell_count - 1); // -1 for header. 70 } 71 if (tag == call_type_data_tag) { 72 CallTypeData::initialize(this, cell_count); 73 } else if (tag == virtual_call_type_data_tag) { 74 VirtualCallTypeData::initialize(this, cell_count); 75 } 76 } 77 78 void DataLayout::clean_weak_klass_links(bool always_clean) { 79 ResourceMark m; 80 data_in()->clean_weak_klass_links(always_clean); 81 } 82 83 84 // ================================================================== 85 // ProfileData 86 // 87 // A ProfileData object is created to refer to a section of profiling 88 // data in a structured way. 89 90 // Constructor for invalid ProfileData. 91 ProfileData::ProfileData() { 92 _data = nullptr; 93 } 94 95 char* ProfileData::print_data_on_helper(const MethodData* md) const { 96 DataLayout* dp = md->extra_data_base(); 97 DataLayout* end = md->args_data_limit(); 98 stringStream ss; 99 for (;; dp = MethodData::next_extra(dp)) { 100 assert(dp < end, "moved past end of extra data"); 101 switch(dp->tag()) { 102 case DataLayout::speculative_trap_data_tag: 103 if (dp->bci() == bci()) { 104 SpeculativeTrapData* data = new SpeculativeTrapData(dp); 105 int trap = data->trap_state(); 106 char buf[100]; 107 ss.print("trap/"); 108 data->method()->print_short_name(&ss); 109 ss.print("(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap)); 110 } 111 break; 112 case DataLayout::bit_data_tag: 113 break; 114 case DataLayout::no_tag: 115 case DataLayout::arg_info_data_tag: 116 return ss.as_string(); 117 break; 118 default: 119 fatal("unexpected tag %d", dp->tag()); 120 } 121 } 122 return nullptr; 123 } 124 125 void ProfileData::print_data_on(outputStream* st, const MethodData* md) const { 126 print_data_on(st, print_data_on_helper(md)); 127 } 128 129 void ProfileData::print_shared(outputStream* st, const char* name, const char* extra) const { 130 st->print("bci: %d ", bci()); 131 st->fill_to(tab_width_one + 1); 132 st->print("%s", name); 133 tab(st); 134 int trap = trap_state(); 135 if (trap != 0) { 136 char buf[100]; 137 st->print("trap(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap)); 138 } 139 if (extra != nullptr) { 140 st->print("%s", extra); 141 } 142 int flags = data()->flags(); 143 if (flags != 0) { 144 st->print("flags(%d) ", flags); 145 } 146 } 147 148 void ProfileData::tab(outputStream* st, bool first) const { 149 st->fill_to(first ? tab_width_one : tab_width_two); 150 } 151 152 // ================================================================== 153 // BitData 154 // 155 // A BitData corresponds to a one-bit flag. This is used to indicate 156 // whether a checkcast bytecode has seen a null value. 157 158 159 void BitData::print_data_on(outputStream* st, const char* extra) const { 160 print_shared(st, "BitData", extra); 161 st->cr(); 162 } 163 164 // ================================================================== 165 // CounterData 166 // 167 // A CounterData corresponds to a simple counter. 168 169 void CounterData::print_data_on(outputStream* st, const char* extra) const { 170 print_shared(st, "CounterData", extra); 171 st->print_cr("count(%u)", count()); 172 } 173 174 // ================================================================== 175 // JumpData 176 // 177 // A JumpData is used to access profiling information for a direct 178 // branch. It is a counter, used for counting the number of branches, 179 // plus a data displacement, used for realigning the data pointer to 180 // the corresponding target bci. 181 182 void JumpData::post_initialize(BytecodeStream* stream, MethodData* mdo) { 183 assert(stream->bci() == bci(), "wrong pos"); 184 int target; 185 Bytecodes::Code c = stream->code(); 186 if (c == Bytecodes::_goto_w || c == Bytecodes::_jsr_w) { 187 target = stream->dest_w(); 188 } else { 189 target = stream->dest(); 190 } 191 int my_di = mdo->dp_to_di(dp()); 192 int target_di = mdo->bci_to_di(target); 193 int offset = target_di - my_di; 194 set_displacement(offset); 195 } 196 197 void JumpData::print_data_on(outputStream* st, const char* extra) const { 198 print_shared(st, "JumpData", extra); 199 st->print_cr("taken(%u) displacement(%d)", taken(), displacement()); 200 } 201 202 int TypeStackSlotEntries::compute_cell_count(Symbol* signature, bool include_receiver, int max) { 203 // Parameter profiling include the receiver 204 int args_count = include_receiver ? 1 : 0; 205 ResourceMark rm; 206 ReferenceArgumentCount rac(signature); 207 args_count += rac.count(); 208 args_count = MIN2(args_count, max); 209 return args_count * per_arg_cell_count; 210 } 211 212 int TypeEntriesAtCall::compute_cell_count(BytecodeStream* stream) { 213 assert(Bytecodes::is_invoke(stream->code()), "should be invoke"); 214 assert(TypeStackSlotEntries::per_arg_count() > ReturnTypeEntry::static_cell_count(), "code to test for arguments/results broken"); 215 const methodHandle m = stream->method(); 216 int bci = stream->bci(); 217 Bytecode_invoke inv(m, bci); 218 int args_cell = 0; 219 if (MethodData::profile_arguments_for_invoke(m, bci)) { 220 args_cell = TypeStackSlotEntries::compute_cell_count(inv.signature(), false, TypeProfileArgsLimit); 221 } 222 int ret_cell = 0; 223 if (MethodData::profile_return_for_invoke(m, bci) && is_reference_type(inv.result_type())) { 224 ret_cell = ReturnTypeEntry::static_cell_count(); 225 } 226 int header_cell = 0; 227 if (args_cell + ret_cell > 0) { 228 header_cell = header_cell_count(); 229 } 230 231 return header_cell + args_cell + ret_cell; 232 } 233 234 class ArgumentOffsetComputer : public SignatureIterator { 235 private: 236 int _max; 237 int _offset; 238 GrowableArray<int> _offsets; 239 240 friend class SignatureIterator; // so do_parameters_on can call do_type 241 void do_type(BasicType type) { 242 if (is_reference_type(type) && _offsets.length() < _max) { 243 _offsets.push(_offset); 244 } 245 _offset += parameter_type_word_count(type); 246 } 247 248 public: 249 ArgumentOffsetComputer(Symbol* signature, int max) 250 : SignatureIterator(signature), 251 _max(max), _offset(0), 252 _offsets(max) { 253 do_parameters_on(this); // non-virtual template execution 254 } 255 256 int off_at(int i) const { return _offsets.at(i); } 257 }; 258 259 void TypeStackSlotEntries::post_initialize(Symbol* signature, bool has_receiver, bool include_receiver) { 260 ResourceMark rm; 261 int start = 0; 262 // Parameter profiling include the receiver 263 if (include_receiver && has_receiver) { 264 set_stack_slot(0, 0); 265 set_type(0, type_none()); 266 start += 1; 267 } 268 ArgumentOffsetComputer aos(signature, _number_of_entries-start); 269 for (int i = start; i < _number_of_entries; i++) { 270 set_stack_slot(i, aos.off_at(i-start) + (has_receiver ? 1 : 0)); 271 set_type(i, type_none()); 272 } 273 } 274 275 void CallTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) { 276 assert(Bytecodes::is_invoke(stream->code()), "should be invoke"); 277 Bytecode_invoke inv(stream->method(), stream->bci()); 278 279 if (has_arguments()) { 280 #ifdef ASSERT 281 ResourceMark rm; 282 ReferenceArgumentCount rac(inv.signature()); 283 int count = MIN2(rac.count(), (int)TypeProfileArgsLimit); 284 assert(count > 0, "room for args type but none found?"); 285 check_number_of_arguments(count); 286 #endif 287 _args.post_initialize(inv.signature(), inv.has_receiver(), false); 288 } 289 290 if (has_return()) { 291 assert(is_reference_type(inv.result_type()), "room for a ret type but doesn't return obj?"); 292 _ret.post_initialize(); 293 } 294 } 295 296 void VirtualCallTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) { 297 assert(Bytecodes::is_invoke(stream->code()), "should be invoke"); 298 Bytecode_invoke inv(stream->method(), stream->bci()); 299 300 if (has_arguments()) { 301 #ifdef ASSERT 302 ResourceMark rm; 303 ReferenceArgumentCount rac(inv.signature()); 304 int count = MIN2(rac.count(), (int)TypeProfileArgsLimit); 305 assert(count > 0, "room for args type but none found?"); 306 check_number_of_arguments(count); 307 #endif 308 _args.post_initialize(inv.signature(), inv.has_receiver(), false); 309 } 310 311 if (has_return()) { 312 assert(is_reference_type(inv.result_type()), "room for a ret type but doesn't return obj?"); 313 _ret.post_initialize(); 314 } 315 } 316 317 void TypeStackSlotEntries::clean_weak_klass_links(bool always_clean) { 318 for (int i = 0; i < _number_of_entries; i++) { 319 intptr_t p = type(i); 320 Klass* k = (Klass*)klass_part(p); 321 if (k != nullptr && (always_clean || !k->is_loader_alive())) { 322 set_type(i, with_status((Klass*)nullptr, p)); 323 } 324 } 325 } 326 327 void ReturnTypeEntry::clean_weak_klass_links(bool always_clean) { 328 intptr_t p = type(); 329 Klass* k = (Klass*)klass_part(p); 330 if (k != nullptr && (always_clean || !k->is_loader_alive())) { 331 set_type(with_status((Klass*)nullptr, p)); 332 } 333 } 334 335 bool TypeEntriesAtCall::return_profiling_enabled() { 336 return MethodData::profile_return(); 337 } 338 339 bool TypeEntriesAtCall::arguments_profiling_enabled() { 340 return MethodData::profile_arguments(); 341 } 342 343 void TypeEntries::print_klass(outputStream* st, intptr_t k) { 344 if (is_type_none(k)) { 345 st->print("none"); 346 } else if (is_type_unknown(k)) { 347 st->print("unknown"); 348 } else { 349 valid_klass(k)->print_value_on(st); 350 } 351 if (was_null_seen(k)) { 352 st->print(" (null seen)"); 353 } 354 } 355 356 void TypeStackSlotEntries::print_data_on(outputStream* st) const { 357 for (int i = 0; i < _number_of_entries; i++) { 358 _pd->tab(st); 359 st->print("%d: stack(%u) ", i, stack_slot(i)); 360 print_klass(st, type(i)); 361 st->cr(); 362 } 363 } 364 365 void ReturnTypeEntry::print_data_on(outputStream* st) const { 366 _pd->tab(st); 367 print_klass(st, type()); 368 st->cr(); 369 } 370 371 void CallTypeData::print_data_on(outputStream* st, const char* extra) const { 372 CounterData::print_data_on(st, extra); 373 if (has_arguments()) { 374 tab(st, true); 375 st->print("argument types"); 376 _args.print_data_on(st); 377 } 378 if (has_return()) { 379 tab(st, true); 380 st->print("return type"); 381 _ret.print_data_on(st); 382 } 383 } 384 385 void VirtualCallTypeData::print_data_on(outputStream* st, const char* extra) const { 386 VirtualCallData::print_data_on(st, extra); 387 if (has_arguments()) { 388 tab(st, true); 389 st->print("argument types"); 390 _args.print_data_on(st); 391 } 392 if (has_return()) { 393 tab(st, true); 394 st->print("return type"); 395 _ret.print_data_on(st); 396 } 397 } 398 399 // ================================================================== 400 // ReceiverTypeData 401 // 402 // A ReceiverTypeData is used to access profiling information about a 403 // dynamic type check. It consists of a counter which counts the total times 404 // that the check is reached, and a series of (Klass*, count) pairs 405 // which are used to store a type profile for the receiver of the check. 406 407 void ReceiverTypeData::clean_weak_klass_links(bool always_clean) { 408 for (uint row = 0; row < row_limit(); row++) { 409 Klass* p = receiver(row); 410 if (p != nullptr && (always_clean || !p->is_loader_alive())) { 411 clear_row(row); 412 } 413 } 414 } 415 416 void ReceiverTypeData::print_receiver_data_on(outputStream* st) const { 417 uint row; 418 int entries = 0; 419 for (row = 0; row < row_limit(); row++) { 420 if (receiver(row) != nullptr) entries++; 421 } 422 st->print_cr("count(%u) entries(%u)", count(), entries); 423 int total = count(); 424 for (row = 0; row < row_limit(); row++) { 425 if (receiver(row) != nullptr) { 426 total += receiver_count(row); 427 } 428 } 429 for (row = 0; row < row_limit(); row++) { 430 if (receiver(row) != nullptr) { 431 tab(st); 432 receiver(row)->print_value_on(st); 433 st->print_cr("(%u %4.2f)", receiver_count(row), (float) receiver_count(row) / (float) total); 434 } 435 } 436 } 437 void ReceiverTypeData::print_data_on(outputStream* st, const char* extra) const { 438 print_shared(st, "ReceiverTypeData", extra); 439 print_receiver_data_on(st); 440 } 441 442 void VirtualCallData::print_data_on(outputStream* st, const char* extra) const { 443 print_shared(st, "VirtualCallData", extra); 444 print_receiver_data_on(st); 445 } 446 447 // ================================================================== 448 // RetData 449 // 450 // A RetData is used to access profiling information for a ret bytecode. 451 // It is composed of a count of the number of times that the ret has 452 // been executed, followed by a series of triples of the form 453 // (bci, count, di) which count the number of times that some bci was the 454 // target of the ret and cache a corresponding displacement. 455 456 void RetData::post_initialize(BytecodeStream* stream, MethodData* mdo) { 457 for (uint row = 0; row < row_limit(); row++) { 458 set_bci_displacement(row, -1); 459 set_bci(row, no_bci); 460 } 461 // release so other threads see a consistent state. bci is used as 462 // a valid flag for bci_displacement. 463 OrderAccess::release(); 464 } 465 466 // This routine needs to atomically update the RetData structure, so the 467 // caller needs to hold the RetData_lock before it gets here. Since taking 468 // the lock can block (and allow GC) and since RetData is a ProfileData is a 469 // wrapper around a derived oop, taking the lock in _this_ method will 470 // basically cause the 'this' pointer's _data field to contain junk after the 471 // lock. We require the caller to take the lock before making the ProfileData 472 // structure. Currently the only caller is InterpreterRuntime::update_mdp_for_ret 473 address RetData::fixup_ret(int return_bci, MethodData* h_mdo) { 474 // First find the mdp which corresponds to the return bci. 475 address mdp = h_mdo->bci_to_dp(return_bci); 476 477 // Now check to see if any of the cache slots are open. 478 for (uint row = 0; row < row_limit(); row++) { 479 if (bci(row) == no_bci) { 480 set_bci_displacement(row, checked_cast<int>(mdp - dp())); 481 set_bci_count(row, DataLayout::counter_increment); 482 // Barrier to ensure displacement is written before the bci; allows 483 // the interpreter to read displacement without fear of race condition. 484 release_set_bci(row, return_bci); 485 break; 486 } 487 } 488 return mdp; 489 } 490 491 void RetData::print_data_on(outputStream* st, const char* extra) const { 492 print_shared(st, "RetData", extra); 493 uint row; 494 int entries = 0; 495 for (row = 0; row < row_limit(); row++) { 496 if (bci(row) != no_bci) entries++; 497 } 498 st->print_cr("count(%u) entries(%u)", count(), entries); 499 for (row = 0; row < row_limit(); row++) { 500 if (bci(row) != no_bci) { 501 tab(st); 502 st->print_cr("bci(%d: count(%u) displacement(%d))", 503 bci(row), bci_count(row), bci_displacement(row)); 504 } 505 } 506 } 507 508 // ================================================================== 509 // BranchData 510 // 511 // A BranchData is used to access profiling data for a two-way branch. 512 // It consists of taken and not_taken counts as well as a data displacement 513 // for the taken case. 514 515 void BranchData::post_initialize(BytecodeStream* stream, MethodData* mdo) { 516 assert(stream->bci() == bci(), "wrong pos"); 517 int target = stream->dest(); 518 int my_di = mdo->dp_to_di(dp()); 519 int target_di = mdo->bci_to_di(target); 520 int offset = target_di - my_di; 521 set_displacement(offset); 522 } 523 524 void BranchData::print_data_on(outputStream* st, const char* extra) const { 525 print_shared(st, "BranchData", extra); 526 st->print_cr("taken(%u) displacement(%d)", 527 taken(), displacement()); 528 tab(st); 529 st->print_cr("not taken(%u)", not_taken()); 530 } 531 532 // ================================================================== 533 // MultiBranchData 534 // 535 // A MultiBranchData is used to access profiling information for 536 // a multi-way branch (*switch bytecodes). It consists of a series 537 // of (count, displacement) pairs, which count the number of times each 538 // case was taken and specify the data displacement for each branch target. 539 540 int MultiBranchData::compute_cell_count(BytecodeStream* stream) { 541 int cell_count = 0; 542 if (stream->code() == Bytecodes::_tableswitch) { 543 Bytecode_tableswitch sw(stream->method()(), stream->bcp()); 544 cell_count = 1 + per_case_cell_count * (1 + sw.length()); // 1 for default 545 } else { 546 Bytecode_lookupswitch sw(stream->method()(), stream->bcp()); 547 cell_count = 1 + per_case_cell_count * (sw.number_of_pairs() + 1); // 1 for default 548 } 549 return cell_count; 550 } 551 552 void MultiBranchData::post_initialize(BytecodeStream* stream, 553 MethodData* mdo) { 554 assert(stream->bci() == bci(), "wrong pos"); 555 int target; 556 int my_di; 557 int target_di; 558 int offset; 559 if (stream->code() == Bytecodes::_tableswitch) { 560 Bytecode_tableswitch sw(stream->method()(), stream->bcp()); 561 int len = sw.length(); 562 assert(array_len() == per_case_cell_count * (len + 1), "wrong len"); 563 for (int count = 0; count < len; count++) { 564 target = sw.dest_offset_at(count) + bci(); 565 my_di = mdo->dp_to_di(dp()); 566 target_di = mdo->bci_to_di(target); 567 offset = target_di - my_di; 568 set_displacement_at(count, offset); 569 } 570 target = sw.default_offset() + bci(); 571 my_di = mdo->dp_to_di(dp()); 572 target_di = mdo->bci_to_di(target); 573 offset = target_di - my_di; 574 set_default_displacement(offset); 575 576 } else { 577 Bytecode_lookupswitch sw(stream->method()(), stream->bcp()); 578 int npairs = sw.number_of_pairs(); 579 assert(array_len() == per_case_cell_count * (npairs + 1), "wrong len"); 580 for (int count = 0; count < npairs; count++) { 581 LookupswitchPair pair = sw.pair_at(count); 582 target = pair.offset() + bci(); 583 my_di = mdo->dp_to_di(dp()); 584 target_di = mdo->bci_to_di(target); 585 offset = target_di - my_di; 586 set_displacement_at(count, offset); 587 } 588 target = sw.default_offset() + bci(); 589 my_di = mdo->dp_to_di(dp()); 590 target_di = mdo->bci_to_di(target); 591 offset = target_di - my_di; 592 set_default_displacement(offset); 593 } 594 } 595 596 void MultiBranchData::print_data_on(outputStream* st, const char* extra) const { 597 print_shared(st, "MultiBranchData", extra); 598 st->print_cr("default_count(%u) displacement(%d)", 599 default_count(), default_displacement()); 600 int cases = number_of_cases(); 601 for (int i = 0; i < cases; i++) { 602 tab(st); 603 st->print_cr("count(%u) displacement(%d)", 604 count_at(i), displacement_at(i)); 605 } 606 } 607 608 void ArgInfoData::print_data_on(outputStream* st, const char* extra) const { 609 print_shared(st, "ArgInfoData", extra); 610 int nargs = number_of_args(); 611 for (int i = 0; i < nargs; i++) { 612 st->print(" 0x%x", arg_modified(i)); 613 } 614 st->cr(); 615 } 616 617 int ParametersTypeData::compute_cell_count(Method* m) { 618 if (!MethodData::profile_parameters_for_method(methodHandle(Thread::current(), m))) { 619 return 0; 620 } 621 int max = TypeProfileParmsLimit == -1 ? INT_MAX : TypeProfileParmsLimit; 622 int obj_args = TypeStackSlotEntries::compute_cell_count(m->signature(), !m->is_static(), max); 623 if (obj_args > 0) { 624 return obj_args + 1; // 1 cell for array len 625 } 626 return 0; 627 } 628 629 void ParametersTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) { 630 _parameters.post_initialize(mdo->method()->signature(), !mdo->method()->is_static(), true); 631 } 632 633 bool ParametersTypeData::profiling_enabled() { 634 return MethodData::profile_parameters(); 635 } 636 637 void ParametersTypeData::print_data_on(outputStream* st, const char* extra) const { 638 print_shared(st, "ParametersTypeData", extra); 639 tab(st); 640 _parameters.print_data_on(st); 641 st->cr(); 642 } 643 644 void SpeculativeTrapData::print_data_on(outputStream* st, const char* extra) const { 645 print_shared(st, "SpeculativeTrapData", extra); 646 tab(st); 647 method()->print_short_name(st); 648 st->cr(); 649 } 650 651 // ================================================================== 652 // MethodData* 653 // 654 // A MethodData* holds information which has been collected about 655 // a method. 656 657 MethodData* MethodData::allocate(ClassLoaderData* loader_data, const methodHandle& method, TRAPS) { 658 assert(!THREAD->owns_locks(), "Should not own any locks"); 659 int size = MethodData::compute_allocation_size_in_words(method); 660 661 return new (loader_data, size, MetaspaceObj::MethodDataType, THREAD) 662 MethodData(method); 663 } 664 665 int MethodData::bytecode_cell_count(Bytecodes::Code code) { 666 if (CompilerConfig::is_c1_simple_only() && !ProfileInterpreter) { 667 return no_profile_data; 668 } 669 switch (code) { 670 case Bytecodes::_checkcast: 671 case Bytecodes::_instanceof: 672 case Bytecodes::_aastore: 673 if (TypeProfileCasts) { 674 return ReceiverTypeData::static_cell_count(); 675 } else { 676 return BitData::static_cell_count(); 677 } 678 case Bytecodes::_invokespecial: 679 case Bytecodes::_invokestatic: 680 if (MethodData::profile_arguments() || MethodData::profile_return()) { 681 return variable_cell_count; 682 } else { 683 return CounterData::static_cell_count(); 684 } 685 case Bytecodes::_goto: 686 case Bytecodes::_goto_w: 687 case Bytecodes::_jsr: 688 case Bytecodes::_jsr_w: 689 return JumpData::static_cell_count(); 690 case Bytecodes::_invokevirtual: 691 case Bytecodes::_invokeinterface: 692 if (MethodData::profile_arguments() || MethodData::profile_return()) { 693 return variable_cell_count; 694 } else { 695 return VirtualCallData::static_cell_count(); 696 } 697 case Bytecodes::_invokedynamic: 698 if (MethodData::profile_arguments() || MethodData::profile_return()) { 699 return variable_cell_count; 700 } else { 701 return CounterData::static_cell_count(); 702 } 703 case Bytecodes::_ret: 704 return RetData::static_cell_count(); 705 case Bytecodes::_ifeq: 706 case Bytecodes::_ifne: 707 case Bytecodes::_iflt: 708 case Bytecodes::_ifge: 709 case Bytecodes::_ifgt: 710 case Bytecodes::_ifle: 711 case Bytecodes::_if_icmpeq: 712 case Bytecodes::_if_icmpne: 713 case Bytecodes::_if_icmplt: 714 case Bytecodes::_if_icmpge: 715 case Bytecodes::_if_icmpgt: 716 case Bytecodes::_if_icmple: 717 case Bytecodes::_if_acmpeq: 718 case Bytecodes::_if_acmpne: 719 case Bytecodes::_ifnull: 720 case Bytecodes::_ifnonnull: 721 return BranchData::static_cell_count(); 722 case Bytecodes::_lookupswitch: 723 case Bytecodes::_tableswitch: 724 return variable_cell_count; 725 default: 726 return no_profile_data; 727 } 728 } 729 730 // Compute the size of the profiling information corresponding to 731 // the current bytecode. 732 int MethodData::compute_data_size(BytecodeStream* stream) { 733 int cell_count = bytecode_cell_count(stream->code()); 734 if (cell_count == no_profile_data) { 735 return 0; 736 } 737 if (cell_count == variable_cell_count) { 738 switch (stream->code()) { 739 case Bytecodes::_lookupswitch: 740 case Bytecodes::_tableswitch: 741 cell_count = MultiBranchData::compute_cell_count(stream); 742 break; 743 case Bytecodes::_invokespecial: 744 case Bytecodes::_invokestatic: 745 case Bytecodes::_invokedynamic: 746 assert(MethodData::profile_arguments() || MethodData::profile_return(), "should be collecting args profile"); 747 if (profile_arguments_for_invoke(stream->method(), stream->bci()) || 748 profile_return_for_invoke(stream->method(), stream->bci())) { 749 cell_count = CallTypeData::compute_cell_count(stream); 750 } else { 751 cell_count = CounterData::static_cell_count(); 752 } 753 break; 754 case Bytecodes::_invokevirtual: 755 case Bytecodes::_invokeinterface: { 756 assert(MethodData::profile_arguments() || MethodData::profile_return(), "should be collecting args profile"); 757 if (profile_arguments_for_invoke(stream->method(), stream->bci()) || 758 profile_return_for_invoke(stream->method(), stream->bci())) { 759 cell_count = VirtualCallTypeData::compute_cell_count(stream); 760 } else { 761 cell_count = VirtualCallData::static_cell_count(); 762 } 763 break; 764 } 765 default: 766 fatal("unexpected bytecode for var length profile data"); 767 } 768 } 769 // Note: cell_count might be zero, meaning that there is just 770 // a DataLayout header, with no extra cells. 771 assert(cell_count >= 0, "sanity"); 772 return DataLayout::compute_size_in_bytes(cell_count); 773 } 774 775 bool MethodData::is_speculative_trap_bytecode(Bytecodes::Code code) { 776 // Bytecodes for which we may use speculation 777 switch (code) { 778 case Bytecodes::_checkcast: 779 case Bytecodes::_instanceof: 780 case Bytecodes::_aastore: 781 case Bytecodes::_invokevirtual: 782 case Bytecodes::_invokeinterface: 783 case Bytecodes::_if_acmpeq: 784 case Bytecodes::_if_acmpne: 785 case Bytecodes::_ifnull: 786 case Bytecodes::_ifnonnull: 787 case Bytecodes::_invokestatic: 788 #ifdef COMPILER2 789 if (CompilerConfig::is_c2_enabled()) { 790 return UseTypeSpeculation; 791 } 792 #endif 793 default: 794 return false; 795 } 796 return false; 797 } 798 799 #if INCLUDE_JVMCI 800 801 void* FailedSpeculation::operator new(size_t size, size_t fs_size) throw() { 802 return CHeapObj<mtCompiler>::operator new(fs_size, std::nothrow); 803 } 804 805 FailedSpeculation::FailedSpeculation(address speculation, int speculation_len) : _data_len(speculation_len), _next(nullptr) { 806 memcpy(data(), speculation, speculation_len); 807 } 808 809 // A heuristic check to detect nmethods that outlive a failed speculations list. 810 static void guarantee_failed_speculations_alive(nmethod* nm, FailedSpeculation** failed_speculations_address) { 811 jlong head = (jlong)(address) *failed_speculations_address; 812 if ((head & 0x1) == 0x1) { 813 stringStream st; 814 if (nm != nullptr) { 815 st.print("%d", nm->compile_id()); 816 Method* method = nm->method(); 817 st.print_raw("{"); 818 if (method != nullptr) { 819 method->print_name(&st); 820 } else { 821 const char* jvmci_name = nm->jvmci_name(); 822 if (jvmci_name != nullptr) { 823 st.print_raw(jvmci_name); 824 } 825 } 826 st.print_raw("}"); 827 } else { 828 st.print("<unknown>"); 829 } 830 fatal("Adding to failed speculations list that appears to have been freed. Source: %s", st.as_string()); 831 } 832 } 833 834 bool FailedSpeculation::add_failed_speculation(nmethod* nm, FailedSpeculation** failed_speculations_address, address speculation, int speculation_len) { 835 assert(failed_speculations_address != nullptr, "must be"); 836 size_t fs_size = sizeof(FailedSpeculation) + speculation_len; 837 838 guarantee_failed_speculations_alive(nm, failed_speculations_address); 839 840 FailedSpeculation** cursor = failed_speculations_address; 841 FailedSpeculation* fs = nullptr; 842 do { 843 if (*cursor == nullptr) { 844 if (fs == nullptr) { 845 // lazily allocate FailedSpeculation 846 fs = new (fs_size) FailedSpeculation(speculation, speculation_len); 847 if (fs == nullptr) { 848 // no memory -> ignore failed speculation 849 return false; 850 } 851 guarantee(is_aligned(fs, sizeof(FailedSpeculation*)), "FailedSpeculation objects must be pointer aligned"); 852 } 853 FailedSpeculation* old_fs = Atomic::cmpxchg(cursor, (FailedSpeculation*) nullptr, fs); 854 if (old_fs == nullptr) { 855 // Successfully appended fs to end of the list 856 return true; 857 } 858 } 859 guarantee(*cursor != nullptr, "cursor must point to non-null FailedSpeculation"); 860 // check if the current entry matches this thread's failed speculation 861 if ((*cursor)->data_len() == speculation_len && memcmp(speculation, (*cursor)->data(), speculation_len) == 0) { 862 if (fs != nullptr) { 863 delete fs; 864 } 865 return false; 866 } 867 cursor = (*cursor)->next_adr(); 868 } while (true); 869 } 870 871 void FailedSpeculation::free_failed_speculations(FailedSpeculation** failed_speculations_address) { 872 assert(failed_speculations_address != nullptr, "must be"); 873 FailedSpeculation* fs = *failed_speculations_address; 874 while (fs != nullptr) { 875 FailedSpeculation* next = fs->next(); 876 delete fs; 877 fs = next; 878 } 879 880 // Write an unaligned value to failed_speculations_address to denote 881 // that it is no longer a valid pointer. This is allows for the check 882 // in add_failed_speculation against adding to a freed failed 883 // speculations list. 884 long* head = (long*) failed_speculations_address; 885 (*head) = (*head) | 0x1; 886 } 887 #endif // INCLUDE_JVMCI 888 889 int MethodData::compute_extra_data_count(int data_size, int empty_bc_count, bool needs_speculative_traps) { 890 #if INCLUDE_JVMCI 891 if (ProfileTraps) { 892 // Assume that up to 30% of the possibly trapping BCIs with no MDP will need to allocate one. 893 int extra_data_count = MIN2(empty_bc_count, MAX2(4, (empty_bc_count * 30) / 100)); 894 895 // Make sure we have a minimum number of extra data slots to 896 // allocate SpeculativeTrapData entries. We would want to have one 897 // entry per compilation that inlines this method and for which 898 // some type speculation assumption fails. So the room we need for 899 // the SpeculativeTrapData entries doesn't directly depend on the 900 // size of the method. Because it's hard to estimate, we reserve 901 // space for an arbitrary number of entries. 902 int spec_data_count = (needs_speculative_traps ? SpecTrapLimitExtraEntries : 0) * 903 (SpeculativeTrapData::static_cell_count() + DataLayout::header_size_in_cells()); 904 905 return MAX2(extra_data_count, spec_data_count); 906 } else { 907 return 0; 908 } 909 #else // INCLUDE_JVMCI 910 if (ProfileTraps) { 911 // Assume that up to 3% of BCIs with no MDP will need to allocate one. 912 int extra_data_count = (uint)(empty_bc_count * 3) / 128 + 1; 913 // If the method is large, let the extra BCIs grow numerous (to ~1%). 914 int one_percent_of_data 915 = (uint)data_size / (DataLayout::header_size_in_bytes()*128); 916 if (extra_data_count < one_percent_of_data) 917 extra_data_count = one_percent_of_data; 918 if (extra_data_count > empty_bc_count) 919 extra_data_count = empty_bc_count; // no need for more 920 921 // Make sure we have a minimum number of extra data slots to 922 // allocate SpeculativeTrapData entries. We would want to have one 923 // entry per compilation that inlines this method and for which 924 // some type speculation assumption fails. So the room we need for 925 // the SpeculativeTrapData entries doesn't directly depend on the 926 // size of the method. Because it's hard to estimate, we reserve 927 // space for an arbitrary number of entries. 928 int spec_data_count = (needs_speculative_traps ? SpecTrapLimitExtraEntries : 0) * 929 (SpeculativeTrapData::static_cell_count() + DataLayout::header_size_in_cells()); 930 931 return MAX2(extra_data_count, spec_data_count); 932 } else { 933 return 0; 934 } 935 #endif // INCLUDE_JVMCI 936 } 937 938 // Compute the size of the MethodData* necessary to store 939 // profiling information about a given method. Size is in bytes. 940 int MethodData::compute_allocation_size_in_bytes(const methodHandle& method) { 941 int data_size = 0; 942 BytecodeStream stream(method); 943 Bytecodes::Code c; 944 int empty_bc_count = 0; // number of bytecodes lacking data 945 bool needs_speculative_traps = false; 946 while ((c = stream.next()) >= 0) { 947 int size_in_bytes = compute_data_size(&stream); 948 data_size += size_in_bytes; 949 if (size_in_bytes == 0 JVMCI_ONLY(&& Bytecodes::can_trap(c))) empty_bc_count += 1; 950 needs_speculative_traps = needs_speculative_traps || is_speculative_trap_bytecode(c); 951 } 952 int object_size = in_bytes(data_offset()) + data_size; 953 954 // Add some extra DataLayout cells (at least one) to track stray traps. 955 int extra_data_count = compute_extra_data_count(data_size, empty_bc_count, needs_speculative_traps); 956 object_size += extra_data_count * DataLayout::compute_size_in_bytes(0); 957 958 // Add a cell to record information about modified arguments. 959 int arg_size = method->size_of_parameters(); 960 object_size += DataLayout::compute_size_in_bytes(arg_size+1); 961 962 // Reserve room for an area of the MDO dedicated to profiling of 963 // parameters 964 int args_cell = ParametersTypeData::compute_cell_count(method()); 965 if (args_cell > 0) { 966 object_size += DataLayout::compute_size_in_bytes(args_cell); 967 } 968 return object_size; 969 } 970 971 // Compute the size of the MethodData* necessary to store 972 // profiling information about a given method. Size is in words 973 int MethodData::compute_allocation_size_in_words(const methodHandle& method) { 974 int byte_size = compute_allocation_size_in_bytes(method); 975 int word_size = align_up(byte_size, BytesPerWord) / BytesPerWord; 976 return align_metadata_size(word_size); 977 } 978 979 // Initialize an individual data segment. Returns the size of 980 // the segment in bytes. 981 int MethodData::initialize_data(BytecodeStream* stream, 982 int data_index) { 983 if (CompilerConfig::is_c1_simple_only() && !ProfileInterpreter) { 984 return 0; 985 } 986 int cell_count = -1; 987 u1 tag = DataLayout::no_tag; 988 DataLayout* data_layout = data_layout_at(data_index); 989 Bytecodes::Code c = stream->code(); 990 switch (c) { 991 case Bytecodes::_checkcast: 992 case Bytecodes::_instanceof: 993 case Bytecodes::_aastore: 994 if (TypeProfileCasts) { 995 cell_count = ReceiverTypeData::static_cell_count(); 996 tag = DataLayout::receiver_type_data_tag; 997 } else { 998 cell_count = BitData::static_cell_count(); 999 tag = DataLayout::bit_data_tag; 1000 } 1001 break; 1002 case Bytecodes::_invokespecial: 1003 case Bytecodes::_invokestatic: { 1004 int counter_data_cell_count = CounterData::static_cell_count(); 1005 if (profile_arguments_for_invoke(stream->method(), stream->bci()) || 1006 profile_return_for_invoke(stream->method(), stream->bci())) { 1007 cell_count = CallTypeData::compute_cell_count(stream); 1008 } else { 1009 cell_count = counter_data_cell_count; 1010 } 1011 if (cell_count > counter_data_cell_count) { 1012 tag = DataLayout::call_type_data_tag; 1013 } else { 1014 tag = DataLayout::counter_data_tag; 1015 } 1016 break; 1017 } 1018 case Bytecodes::_goto: 1019 case Bytecodes::_goto_w: 1020 case Bytecodes::_jsr: 1021 case Bytecodes::_jsr_w: 1022 cell_count = JumpData::static_cell_count(); 1023 tag = DataLayout::jump_data_tag; 1024 break; 1025 case Bytecodes::_invokevirtual: 1026 case Bytecodes::_invokeinterface: { 1027 int virtual_call_data_cell_count = VirtualCallData::static_cell_count(); 1028 if (profile_arguments_for_invoke(stream->method(), stream->bci()) || 1029 profile_return_for_invoke(stream->method(), stream->bci())) { 1030 cell_count = VirtualCallTypeData::compute_cell_count(stream); 1031 } else { 1032 cell_count = virtual_call_data_cell_count; 1033 } 1034 if (cell_count > virtual_call_data_cell_count) { 1035 tag = DataLayout::virtual_call_type_data_tag; 1036 } else { 1037 tag = DataLayout::virtual_call_data_tag; 1038 } 1039 break; 1040 } 1041 case Bytecodes::_invokedynamic: { 1042 // %%% should make a type profile for any invokedynamic that takes a ref argument 1043 int counter_data_cell_count = CounterData::static_cell_count(); 1044 if (profile_arguments_for_invoke(stream->method(), stream->bci()) || 1045 profile_return_for_invoke(stream->method(), stream->bci())) { 1046 cell_count = CallTypeData::compute_cell_count(stream); 1047 } else { 1048 cell_count = counter_data_cell_count; 1049 } 1050 if (cell_count > counter_data_cell_count) { 1051 tag = DataLayout::call_type_data_tag; 1052 } else { 1053 tag = DataLayout::counter_data_tag; 1054 } 1055 break; 1056 } 1057 case Bytecodes::_ret: 1058 cell_count = RetData::static_cell_count(); 1059 tag = DataLayout::ret_data_tag; 1060 break; 1061 case Bytecodes::_ifeq: 1062 case Bytecodes::_ifne: 1063 case Bytecodes::_iflt: 1064 case Bytecodes::_ifge: 1065 case Bytecodes::_ifgt: 1066 case Bytecodes::_ifle: 1067 case Bytecodes::_if_icmpeq: 1068 case Bytecodes::_if_icmpne: 1069 case Bytecodes::_if_icmplt: 1070 case Bytecodes::_if_icmpge: 1071 case Bytecodes::_if_icmpgt: 1072 case Bytecodes::_if_icmple: 1073 case Bytecodes::_if_acmpeq: 1074 case Bytecodes::_if_acmpne: 1075 case Bytecodes::_ifnull: 1076 case Bytecodes::_ifnonnull: 1077 cell_count = BranchData::static_cell_count(); 1078 tag = DataLayout::branch_data_tag; 1079 break; 1080 case Bytecodes::_lookupswitch: 1081 case Bytecodes::_tableswitch: 1082 cell_count = MultiBranchData::compute_cell_count(stream); 1083 tag = DataLayout::multi_branch_data_tag; 1084 break; 1085 default: 1086 break; 1087 } 1088 assert(tag == DataLayout::multi_branch_data_tag || 1089 ((MethodData::profile_arguments() || MethodData::profile_return()) && 1090 (tag == DataLayout::call_type_data_tag || 1091 tag == DataLayout::counter_data_tag || 1092 tag == DataLayout::virtual_call_type_data_tag || 1093 tag == DataLayout::virtual_call_data_tag)) || 1094 cell_count == bytecode_cell_count(c), "cell counts must agree"); 1095 if (cell_count >= 0) { 1096 assert(tag != DataLayout::no_tag, "bad tag"); 1097 assert(bytecode_has_profile(c), "agree w/ BHP"); 1098 data_layout->initialize(tag, checked_cast<u2>(stream->bci()), cell_count); 1099 return DataLayout::compute_size_in_bytes(cell_count); 1100 } else { 1101 assert(!bytecode_has_profile(c), "agree w/ !BHP"); 1102 return 0; 1103 } 1104 } 1105 1106 // Get the data at an arbitrary (sort of) data index. 1107 ProfileData* MethodData::data_at(int data_index) const { 1108 if (out_of_bounds(data_index)) { 1109 return nullptr; 1110 } 1111 DataLayout* data_layout = data_layout_at(data_index); 1112 return data_layout->data_in(); 1113 } 1114 1115 int DataLayout::cell_count() { 1116 switch (tag()) { 1117 case DataLayout::no_tag: 1118 default: 1119 ShouldNotReachHere(); 1120 return 0; 1121 case DataLayout::bit_data_tag: 1122 return BitData::static_cell_count(); 1123 case DataLayout::counter_data_tag: 1124 return CounterData::static_cell_count(); 1125 case DataLayout::jump_data_tag: 1126 return JumpData::static_cell_count(); 1127 case DataLayout::receiver_type_data_tag: 1128 return ReceiverTypeData::static_cell_count(); 1129 case DataLayout::virtual_call_data_tag: 1130 return VirtualCallData::static_cell_count(); 1131 case DataLayout::ret_data_tag: 1132 return RetData::static_cell_count(); 1133 case DataLayout::branch_data_tag: 1134 return BranchData::static_cell_count(); 1135 case DataLayout::multi_branch_data_tag: 1136 return ((new MultiBranchData(this))->cell_count()); 1137 case DataLayout::arg_info_data_tag: 1138 return ((new ArgInfoData(this))->cell_count()); 1139 case DataLayout::call_type_data_tag: 1140 return ((new CallTypeData(this))->cell_count()); 1141 case DataLayout::virtual_call_type_data_tag: 1142 return ((new VirtualCallTypeData(this))->cell_count()); 1143 case DataLayout::parameters_type_data_tag: 1144 return ((new ParametersTypeData(this))->cell_count()); 1145 case DataLayout::speculative_trap_data_tag: 1146 return SpeculativeTrapData::static_cell_count(); 1147 } 1148 } 1149 ProfileData* DataLayout::data_in() { 1150 switch (tag()) { 1151 case DataLayout::no_tag: 1152 default: 1153 ShouldNotReachHere(); 1154 return nullptr; 1155 case DataLayout::bit_data_tag: 1156 return new BitData(this); 1157 case DataLayout::counter_data_tag: 1158 return new CounterData(this); 1159 case DataLayout::jump_data_tag: 1160 return new JumpData(this); 1161 case DataLayout::receiver_type_data_tag: 1162 return new ReceiverTypeData(this); 1163 case DataLayout::virtual_call_data_tag: 1164 return new VirtualCallData(this); 1165 case DataLayout::ret_data_tag: 1166 return new RetData(this); 1167 case DataLayout::branch_data_tag: 1168 return new BranchData(this); 1169 case DataLayout::multi_branch_data_tag: 1170 return new MultiBranchData(this); 1171 case DataLayout::arg_info_data_tag: 1172 return new ArgInfoData(this); 1173 case DataLayout::call_type_data_tag: 1174 return new CallTypeData(this); 1175 case DataLayout::virtual_call_type_data_tag: 1176 return new VirtualCallTypeData(this); 1177 case DataLayout::parameters_type_data_tag: 1178 return new ParametersTypeData(this); 1179 case DataLayout::speculative_trap_data_tag: 1180 return new SpeculativeTrapData(this); 1181 } 1182 } 1183 1184 // Iteration over data. 1185 ProfileData* MethodData::next_data(ProfileData* current) const { 1186 int current_index = dp_to_di(current->dp()); 1187 int next_index = current_index + current->size_in_bytes(); 1188 ProfileData* next = data_at(next_index); 1189 return next; 1190 } 1191 1192 DataLayout* MethodData::next_data_layout(DataLayout* current) const { 1193 int current_index = dp_to_di((address)current); 1194 int next_index = current_index + current->size_in_bytes(); 1195 if (out_of_bounds(next_index)) { 1196 return nullptr; 1197 } 1198 DataLayout* next = data_layout_at(next_index); 1199 return next; 1200 } 1201 1202 // Give each of the data entries a chance to perform specific 1203 // data initialization. 1204 void MethodData::post_initialize(BytecodeStream* stream) { 1205 ResourceMark rm; 1206 ProfileData* data; 1207 for (data = first_data(); is_valid(data); data = next_data(data)) { 1208 stream->set_start(data->bci()); 1209 stream->next(); 1210 data->post_initialize(stream, this); 1211 } 1212 if (_parameters_type_data_di != no_parameters) { 1213 parameters_type_data()->post_initialize(nullptr, this); 1214 } 1215 } 1216 1217 // Initialize the MethodData* corresponding to a given method. 1218 MethodData::MethodData(const methodHandle& method) 1219 : _method(method()), 1220 // Holds Compile_lock 1221 _extra_data_lock(Mutex::safepoint-2, "MDOExtraData_lock"), 1222 _compiler_counters(), 1223 _parameters_type_data_di(parameters_uninitialized) { 1224 initialize(); 1225 } 1226 1227 void MethodData::initialize() { 1228 Thread* thread = Thread::current(); 1229 NoSafepointVerifier no_safepoint; // init function atomic wrt GC 1230 ResourceMark rm(thread); 1231 1232 init(); 1233 set_creation_mileage(mileage_of(method())); 1234 1235 // Go through the bytecodes and allocate and initialize the 1236 // corresponding data cells. 1237 int data_size = 0; 1238 int empty_bc_count = 0; // number of bytecodes lacking data 1239 _data[0] = 0; // apparently not set below. 1240 BytecodeStream stream(methodHandle(thread, method())); 1241 Bytecodes::Code c; 1242 bool needs_speculative_traps = false; 1243 while ((c = stream.next()) >= 0) { 1244 int size_in_bytes = initialize_data(&stream, data_size); 1245 data_size += size_in_bytes; 1246 if (size_in_bytes == 0 JVMCI_ONLY(&& Bytecodes::can_trap(c))) empty_bc_count += 1; 1247 needs_speculative_traps = needs_speculative_traps || is_speculative_trap_bytecode(c); 1248 } 1249 _data_size = data_size; 1250 int object_size = in_bytes(data_offset()) + data_size; 1251 1252 // Add some extra DataLayout cells (at least one) to track stray traps. 1253 int extra_data_count = compute_extra_data_count(data_size, empty_bc_count, needs_speculative_traps); 1254 int extra_size = extra_data_count * DataLayout::compute_size_in_bytes(0); 1255 1256 // Let's zero the space for the extra data 1257 if (extra_size > 0) { 1258 Copy::zero_to_bytes(((address)_data) + data_size, extra_size); 1259 } 1260 1261 // Add a cell to record information about modified arguments. 1262 // Set up _args_modified array after traps cells so that 1263 // the code for traps cells works. 1264 DataLayout *dp = data_layout_at(data_size + extra_size); 1265 1266 int arg_size = method()->size_of_parameters(); 1267 dp->initialize(DataLayout::arg_info_data_tag, 0, arg_size+1); 1268 1269 int arg_data_size = DataLayout::compute_size_in_bytes(arg_size+1); 1270 object_size += extra_size + arg_data_size; 1271 1272 int parms_cell = ParametersTypeData::compute_cell_count(method()); 1273 // If we are profiling parameters, we reserved an area near the end 1274 // of the MDO after the slots for bytecodes (because there's no bci 1275 // for method entry so they don't fit with the framework for the 1276 // profiling of bytecodes). We store the offset within the MDO of 1277 // this area (or -1 if no parameter is profiled) 1278 if (parms_cell > 0) { 1279 object_size += DataLayout::compute_size_in_bytes(parms_cell); 1280 _parameters_type_data_di = data_size + extra_size + arg_data_size; 1281 DataLayout *dp = data_layout_at(data_size + extra_size + arg_data_size); 1282 dp->initialize(DataLayout::parameters_type_data_tag, 0, parms_cell); 1283 } else { 1284 _parameters_type_data_di = no_parameters; 1285 } 1286 1287 // Set an initial hint. Don't use set_hint_di() because 1288 // first_di() may be out of bounds if data_size is 0. 1289 // In that situation, _hint_di is never used, but at 1290 // least well-defined. 1291 _hint_di = first_di(); 1292 1293 post_initialize(&stream); 1294 1295 assert(object_size == compute_allocation_size_in_bytes(methodHandle(thread, _method)), "MethodData: computed size != initialized size"); 1296 set_size(object_size); 1297 } 1298 1299 void MethodData::init() { 1300 _compiler_counters = CompilerCounters(); // reset compiler counters 1301 _invocation_counter.init(); 1302 _backedge_counter.init(); 1303 _invocation_counter_start = 0; 1304 _backedge_counter_start = 0; 1305 1306 // Set per-method invoke- and backedge mask. 1307 double scale = 1.0; 1308 methodHandle mh(Thread::current(), _method); 1309 CompilerOracle::has_option_value(mh, CompileCommand::CompileThresholdScaling, scale); 1310 _invoke_mask = (int)right_n_bits(CompilerConfig::scaled_freq_log(Tier0InvokeNotifyFreqLog, scale)) << InvocationCounter::count_shift; 1311 _backedge_mask = (int)right_n_bits(CompilerConfig::scaled_freq_log(Tier0BackedgeNotifyFreqLog, scale)) << InvocationCounter::count_shift; 1312 1313 _tenure_traps = 0; 1314 _num_loops = 0; 1315 _num_blocks = 0; 1316 _would_profile = unknown; 1317 1318 #if INCLUDE_JVMCI 1319 _jvmci_ir_size = 0; 1320 _failed_speculations = nullptr; 1321 #endif 1322 1323 #if INCLUDE_RTM_OPT 1324 _rtm_state = NoRTM; // No RTM lock eliding by default 1325 if (UseRTMLocking && 1326 !CompilerOracle::has_option(mh, CompileCommand::NoRTMLockEliding)) { 1327 if (CompilerOracle::has_option(mh, CompileCommand::UseRTMLockEliding) || !UseRTMDeopt) { 1328 // Generate RTM lock eliding code without abort ratio calculation code. 1329 _rtm_state = UseRTM; 1330 } else if (UseRTMDeopt) { 1331 // Generate RTM lock eliding code and include abort ratio calculation 1332 // code if UseRTMDeopt is on. 1333 _rtm_state = ProfileRTM; 1334 } 1335 } 1336 #endif 1337 1338 // Initialize escape flags. 1339 clear_escape_info(); 1340 } 1341 1342 // Get a measure of how much mileage the method has on it. 1343 int MethodData::mileage_of(Method* method) { 1344 return MAX2(method->invocation_count(), method->backedge_count()); 1345 } 1346 1347 bool MethodData::is_mature() const { 1348 return CompilationPolicy::is_mature(_method); 1349 } 1350 1351 // Translate a bci to its corresponding data index (di). 1352 address MethodData::bci_to_dp(int bci) { 1353 ResourceMark rm; 1354 DataLayout* data = data_layout_before(bci); 1355 DataLayout* prev = nullptr; 1356 for ( ; is_valid(data); data = next_data_layout(data)) { 1357 if (data->bci() >= bci) { 1358 if (data->bci() == bci) set_hint_di(dp_to_di((address)data)); 1359 else if (prev != nullptr) set_hint_di(dp_to_di((address)prev)); 1360 return (address)data; 1361 } 1362 prev = data; 1363 } 1364 return (address)limit_data_position(); 1365 } 1366 1367 // Translate a bci to its corresponding data, or null. 1368 ProfileData* MethodData::bci_to_data(int bci) { 1369 DataLayout* data = data_layout_before(bci); 1370 for ( ; is_valid(data); data = next_data_layout(data)) { 1371 if (data->bci() == bci) { 1372 set_hint_di(dp_to_di((address)data)); 1373 return data->data_in(); 1374 } else if (data->bci() > bci) { 1375 break; 1376 } 1377 } 1378 return bci_to_extra_data(bci, nullptr, false); 1379 } 1380 1381 DataLayout* MethodData::next_extra(DataLayout* dp) { 1382 int nb_cells = 0; 1383 switch(dp->tag()) { 1384 case DataLayout::bit_data_tag: 1385 case DataLayout::no_tag: 1386 nb_cells = BitData::static_cell_count(); 1387 break; 1388 case DataLayout::speculative_trap_data_tag: 1389 nb_cells = SpeculativeTrapData::static_cell_count(); 1390 break; 1391 default: 1392 fatal("unexpected tag %d", dp->tag()); 1393 } 1394 return (DataLayout*)((address)dp + DataLayout::compute_size_in_bytes(nb_cells)); 1395 } 1396 1397 ProfileData* MethodData::bci_to_extra_data_helper(int bci, Method* m, DataLayout*& dp, bool concurrent) { 1398 DataLayout* end = args_data_limit(); 1399 1400 for (;; dp = next_extra(dp)) { 1401 assert(dp < end, "moved past end of extra data"); 1402 // No need for "Atomic::load_acquire" ops, 1403 // since the data structure is monotonic. 1404 switch(dp->tag()) { 1405 case DataLayout::no_tag: 1406 return nullptr; 1407 case DataLayout::arg_info_data_tag: 1408 dp = end; 1409 return nullptr; // ArgInfoData is at the end of extra data section. 1410 case DataLayout::bit_data_tag: 1411 if (m == nullptr && dp->bci() == bci) { 1412 return new BitData(dp); 1413 } 1414 break; 1415 case DataLayout::speculative_trap_data_tag: 1416 if (m != nullptr) { 1417 SpeculativeTrapData* data = new SpeculativeTrapData(dp); 1418 // data->method() may be null in case of a concurrent 1419 // allocation. Maybe it's for the same method. Try to use that 1420 // entry in that case. 1421 if (dp->bci() == bci) { 1422 if (data->method() == nullptr) { 1423 assert(concurrent, "impossible because no concurrent allocation"); 1424 return nullptr; 1425 } else if (data->method() == m) { 1426 return data; 1427 } 1428 } 1429 } 1430 break; 1431 default: 1432 fatal("unexpected tag %d", dp->tag()); 1433 } 1434 } 1435 return nullptr; 1436 } 1437 1438 1439 // Translate a bci to its corresponding extra data, or null. 1440 ProfileData* MethodData::bci_to_extra_data(int bci, Method* m, bool create_if_missing) { 1441 // This code assumes an entry for a SpeculativeTrapData is 2 cells 1442 assert(2*DataLayout::compute_size_in_bytes(BitData::static_cell_count()) == 1443 DataLayout::compute_size_in_bytes(SpeculativeTrapData::static_cell_count()), 1444 "code needs to be adjusted"); 1445 1446 // Do not create one of these if method has been redefined. 1447 if (m != nullptr && m->is_old()) { 1448 return nullptr; 1449 } 1450 1451 DataLayout* dp = extra_data_base(); 1452 DataLayout* end = args_data_limit(); 1453 1454 // Allocation in the extra data space has to be atomic because not 1455 // all entries have the same size and non atomic concurrent 1456 // allocation would result in a corrupted extra data space. 1457 ProfileData* result = bci_to_extra_data_helper(bci, m, dp, true); 1458 if (result != nullptr) { 1459 return result; 1460 } 1461 1462 if (create_if_missing && dp < end) { 1463 MutexLocker ml(&_extra_data_lock); 1464 // Check again now that we have the lock. Another thread may 1465 // have added extra data entries. 1466 ProfileData* result = bci_to_extra_data_helper(bci, m, dp, false); 1467 if (result != nullptr || dp >= end) { 1468 return result; 1469 } 1470 1471 assert(dp->tag() == DataLayout::no_tag || (dp->tag() == DataLayout::speculative_trap_data_tag && m != nullptr), "should be free"); 1472 assert(next_extra(dp)->tag() == DataLayout::no_tag || next_extra(dp)->tag() == DataLayout::arg_info_data_tag, "should be free or arg info"); 1473 u1 tag = m == nullptr ? DataLayout::bit_data_tag : DataLayout::speculative_trap_data_tag; 1474 // SpeculativeTrapData is 2 slots. Make sure we have room. 1475 if (m != nullptr && next_extra(dp)->tag() != DataLayout::no_tag) { 1476 return nullptr; 1477 } 1478 DataLayout temp; 1479 temp.initialize(tag, checked_cast<u2>(bci), 0); 1480 1481 dp->set_header(temp.header()); 1482 assert(dp->tag() == tag, "sane"); 1483 assert(dp->bci() == bci, "no concurrent allocation"); 1484 if (tag == DataLayout::bit_data_tag) { 1485 return new BitData(dp); 1486 } else { 1487 SpeculativeTrapData* data = new SpeculativeTrapData(dp); 1488 data->set_method(m); 1489 return data; 1490 } 1491 } 1492 return nullptr; 1493 } 1494 1495 ArgInfoData *MethodData::arg_info() { 1496 DataLayout* dp = extra_data_base(); 1497 DataLayout* end = args_data_limit(); 1498 for (; dp < end; dp = next_extra(dp)) { 1499 if (dp->tag() == DataLayout::arg_info_data_tag) 1500 return new ArgInfoData(dp); 1501 } 1502 return nullptr; 1503 } 1504 1505 // Printing 1506 1507 void MethodData::print_on(outputStream* st) const { 1508 assert(is_methodData(), "should be method data"); 1509 st->print("method data for "); 1510 method()->print_value_on(st); 1511 st->cr(); 1512 print_data_on(st); 1513 } 1514 1515 void MethodData::print_value_on(outputStream* st) const { 1516 assert(is_methodData(), "should be method data"); 1517 st->print("method data for "); 1518 method()->print_value_on(st); 1519 } 1520 1521 void MethodData::print_data_on(outputStream* st) const { 1522 ResourceMark rm; 1523 ProfileData* data = first_data(); 1524 if (_parameters_type_data_di != no_parameters) { 1525 parameters_type_data()->print_data_on(st); 1526 } 1527 for ( ; is_valid(data); data = next_data(data)) { 1528 st->print("%d", dp_to_di(data->dp())); 1529 st->fill_to(6); 1530 data->print_data_on(st, this); 1531 } 1532 st->print_cr("--- Extra data:"); 1533 DataLayout* dp = extra_data_base(); 1534 DataLayout* end = args_data_limit(); 1535 for (;; dp = next_extra(dp)) { 1536 assert(dp < end, "moved past end of extra data"); 1537 // No need for "Atomic::load_acquire" ops, 1538 // since the data structure is monotonic. 1539 switch(dp->tag()) { 1540 case DataLayout::no_tag: 1541 continue; 1542 case DataLayout::bit_data_tag: 1543 data = new BitData(dp); 1544 break; 1545 case DataLayout::speculative_trap_data_tag: 1546 data = new SpeculativeTrapData(dp); 1547 break; 1548 case DataLayout::arg_info_data_tag: 1549 data = new ArgInfoData(dp); 1550 dp = end; // ArgInfoData is at the end of extra data section. 1551 break; 1552 default: 1553 fatal("unexpected tag %d", dp->tag()); 1554 } 1555 st->print("%d", dp_to_di(data->dp())); 1556 st->fill_to(6); 1557 data->print_data_on(st); 1558 if (dp >= end) return; 1559 } 1560 } 1561 1562 // Verification 1563 1564 void MethodData::verify_on(outputStream* st) { 1565 guarantee(is_methodData(), "object must be method data"); 1566 // guarantee(m->is_perm(), "should be in permspace"); 1567 this->verify_data_on(st); 1568 } 1569 1570 void MethodData::verify_data_on(outputStream* st) { 1571 NEEDS_CLEANUP; 1572 // not yet implemented. 1573 } 1574 1575 bool MethodData::profile_jsr292(const methodHandle& m, int bci) { 1576 if (m->is_compiled_lambda_form()) { 1577 return true; 1578 } 1579 1580 Bytecode_invoke inv(m , bci); 1581 return inv.is_invokedynamic() || inv.is_invokehandle(); 1582 } 1583 1584 bool MethodData::profile_unsafe(const methodHandle& m, int bci) { 1585 Bytecode_invoke inv(m , bci); 1586 if (inv.is_invokevirtual()) { 1587 Symbol* klass = inv.klass(); 1588 if (klass == vmSymbols::jdk_internal_misc_Unsafe() || 1589 klass == vmSymbols::sun_misc_Unsafe() || 1590 klass == vmSymbols::jdk_internal_misc_ScopedMemoryAccess()) { 1591 Symbol* name = inv.name(); 1592 if (name->starts_with("get") || name->starts_with("put")) { 1593 return true; 1594 } 1595 } 1596 } 1597 return false; 1598 } 1599 1600 int MethodData::profile_arguments_flag() { 1601 return TypeProfileLevel % 10; 1602 } 1603 1604 bool MethodData::profile_arguments() { 1605 return profile_arguments_flag() > no_type_profile && profile_arguments_flag() <= type_profile_all && TypeProfileArgsLimit > 0; 1606 } 1607 1608 bool MethodData::profile_arguments_jsr292_only() { 1609 return profile_arguments_flag() == type_profile_jsr292; 1610 } 1611 1612 bool MethodData::profile_all_arguments() { 1613 return profile_arguments_flag() == type_profile_all; 1614 } 1615 1616 bool MethodData::profile_arguments_for_invoke(const methodHandle& m, int bci) { 1617 if (!profile_arguments()) { 1618 return false; 1619 } 1620 1621 if (profile_all_arguments()) { 1622 return true; 1623 } 1624 1625 if (profile_unsafe(m, bci)) { 1626 return true; 1627 } 1628 1629 assert(profile_arguments_jsr292_only(), "inconsistent"); 1630 return profile_jsr292(m, bci); 1631 } 1632 1633 int MethodData::profile_return_flag() { 1634 return (TypeProfileLevel % 100) / 10; 1635 } 1636 1637 bool MethodData::profile_return() { 1638 return profile_return_flag() > no_type_profile && profile_return_flag() <= type_profile_all; 1639 } 1640 1641 bool MethodData::profile_return_jsr292_only() { 1642 return profile_return_flag() == type_profile_jsr292; 1643 } 1644 1645 bool MethodData::profile_all_return() { 1646 return profile_return_flag() == type_profile_all; 1647 } 1648 1649 bool MethodData::profile_return_for_invoke(const methodHandle& m, int bci) { 1650 if (!profile_return()) { 1651 return false; 1652 } 1653 1654 if (profile_all_return()) { 1655 return true; 1656 } 1657 1658 assert(profile_return_jsr292_only(), "inconsistent"); 1659 return profile_jsr292(m, bci); 1660 } 1661 1662 int MethodData::profile_parameters_flag() { 1663 return TypeProfileLevel / 100; 1664 } 1665 1666 bool MethodData::profile_parameters() { 1667 return profile_parameters_flag() > no_type_profile && profile_parameters_flag() <= type_profile_all; 1668 } 1669 1670 bool MethodData::profile_parameters_jsr292_only() { 1671 return profile_parameters_flag() == type_profile_jsr292; 1672 } 1673 1674 bool MethodData::profile_all_parameters() { 1675 return profile_parameters_flag() == type_profile_all; 1676 } 1677 1678 bool MethodData::profile_parameters_for_method(const methodHandle& m) { 1679 if (!profile_parameters()) { 1680 return false; 1681 } 1682 1683 if (profile_all_parameters()) { 1684 return true; 1685 } 1686 1687 assert(profile_parameters_jsr292_only(), "inconsistent"); 1688 return m->is_compiled_lambda_form(); 1689 } 1690 1691 void MethodData::metaspace_pointers_do(MetaspaceClosure* it) { 1692 log_trace(cds)("Iter(MethodData): %p", this); 1693 it->push(&_method); 1694 } 1695 1696 void MethodData::clean_extra_data_helper(DataLayout* dp, int shift, bool reset) { 1697 if (shift == 0) { 1698 return; 1699 } 1700 if (!reset) { 1701 // Move all cells of trap entry at dp left by "shift" cells 1702 intptr_t* start = (intptr_t*)dp; 1703 intptr_t* end = (intptr_t*)next_extra(dp); 1704 for (intptr_t* ptr = start; ptr < end; ptr++) { 1705 *(ptr-shift) = *ptr; 1706 } 1707 } else { 1708 // Reset "shift" cells stopping at dp 1709 intptr_t* start = ((intptr_t*)dp) - shift; 1710 intptr_t* end = (intptr_t*)dp; 1711 for (intptr_t* ptr = start; ptr < end; ptr++) { 1712 *ptr = 0; 1713 } 1714 } 1715 } 1716 1717 // Check for entries that reference an unloaded method 1718 class CleanExtraDataKlassClosure : public CleanExtraDataClosure { 1719 bool _always_clean; 1720 public: 1721 CleanExtraDataKlassClosure(bool always_clean) : _always_clean(always_clean) {} 1722 bool is_live(Method* m) { 1723 return !(_always_clean) && m->method_holder()->is_loader_alive(); 1724 } 1725 }; 1726 1727 // Check for entries that reference a redefined method 1728 class CleanExtraDataMethodClosure : public CleanExtraDataClosure { 1729 public: 1730 CleanExtraDataMethodClosure() {} 1731 bool is_live(Method* m) { return !m->is_old(); } 1732 }; 1733 1734 1735 // Remove SpeculativeTrapData entries that reference an unloaded or 1736 // redefined method 1737 void MethodData::clean_extra_data(CleanExtraDataClosure* cl) { 1738 DataLayout* dp = extra_data_base(); 1739 DataLayout* end = args_data_limit(); 1740 1741 int shift = 0; 1742 for (; dp < end; dp = next_extra(dp)) { 1743 switch(dp->tag()) { 1744 case DataLayout::speculative_trap_data_tag: { 1745 SpeculativeTrapData* data = new SpeculativeTrapData(dp); 1746 Method* m = data->method(); 1747 assert(m != nullptr, "should have a method"); 1748 if (!cl->is_live(m)) { 1749 // "shift" accumulates the number of cells for dead 1750 // SpeculativeTrapData entries that have been seen so 1751 // far. Following entries must be shifted left by that many 1752 // cells to remove the dead SpeculativeTrapData entries. 1753 shift += (int)((intptr_t*)next_extra(dp) - (intptr_t*)dp); 1754 } else { 1755 // Shift this entry left if it follows dead 1756 // SpeculativeTrapData entries 1757 clean_extra_data_helper(dp, shift); 1758 } 1759 break; 1760 } 1761 case DataLayout::bit_data_tag: 1762 // Shift this entry left if it follows dead SpeculativeTrapData 1763 // entries 1764 clean_extra_data_helper(dp, shift); 1765 continue; 1766 case DataLayout::no_tag: 1767 case DataLayout::arg_info_data_tag: 1768 // We are at end of the live trap entries. The previous "shift" 1769 // cells contain entries that are either dead or were shifted 1770 // left. They need to be reset to no_tag 1771 clean_extra_data_helper(dp, shift, true); 1772 return; 1773 default: 1774 fatal("unexpected tag %d", dp->tag()); 1775 } 1776 } 1777 } 1778 1779 // Verify there's no unloaded or redefined method referenced by a 1780 // SpeculativeTrapData entry 1781 void MethodData::verify_extra_data_clean(CleanExtraDataClosure* cl) { 1782 #ifdef ASSERT 1783 DataLayout* dp = extra_data_base(); 1784 DataLayout* end = args_data_limit(); 1785 1786 for (; dp < end; dp = next_extra(dp)) { 1787 switch(dp->tag()) { 1788 case DataLayout::speculative_trap_data_tag: { 1789 SpeculativeTrapData* data = new SpeculativeTrapData(dp); 1790 Method* m = data->method(); 1791 assert(m != nullptr && cl->is_live(m), "Method should exist"); 1792 break; 1793 } 1794 case DataLayout::bit_data_tag: 1795 continue; 1796 case DataLayout::no_tag: 1797 case DataLayout::arg_info_data_tag: 1798 return; 1799 default: 1800 fatal("unexpected tag %d", dp->tag()); 1801 } 1802 } 1803 #endif 1804 } 1805 1806 void MethodData::clean_method_data(bool always_clean) { 1807 ResourceMark rm; 1808 for (ProfileData* data = first_data(); 1809 is_valid(data); 1810 data = next_data(data)) { 1811 data->clean_weak_klass_links(always_clean); 1812 } 1813 ParametersTypeData* parameters = parameters_type_data(); 1814 if (parameters != nullptr) { 1815 parameters->clean_weak_klass_links(always_clean); 1816 } 1817 1818 CleanExtraDataKlassClosure cl(always_clean); 1819 clean_extra_data(&cl); 1820 verify_extra_data_clean(&cl); 1821 } 1822 1823 // This is called during redefinition to clean all "old" redefined 1824 // methods out of MethodData for all methods. 1825 void MethodData::clean_weak_method_links() { 1826 ResourceMark rm; 1827 CleanExtraDataMethodClosure cl; 1828 clean_extra_data(&cl); 1829 verify_extra_data_clean(&cl); 1830 } 1831 1832 void MethodData::deallocate_contents(ClassLoaderData* loader_data) { 1833 release_C_heap_structures(); 1834 } 1835 1836 void MethodData::release_C_heap_structures() { 1837 #if INCLUDE_JVMCI 1838 FailedSpeculation::free_failed_speculations(get_failed_speculations_address()); 1839 #endif 1840 }