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