1 /* 2 * Copyright (c) 2000, 2013, 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 "classfile/systemDictionary.hpp" 27 #include "interpreter/bytecode.hpp" 28 #include "interpreter/bytecodeStream.hpp" 29 #include "interpreter/linkResolver.hpp" 30 #include "memory/heapInspection.hpp" 31 #include "oops/methodData.hpp" 32 #include "prims/jvmtiRedefineClasses.hpp" 33 #include "runtime/compilationPolicy.hpp" 34 #include "runtime/deoptimization.hpp" 35 #include "runtime/handles.inline.hpp" 36 37 // ================================================================== 38 // DataLayout 39 // 40 // Overlay for generic profiling data. 41 42 // Some types of data layouts need a length field. 43 bool DataLayout::needs_array_len(u1 tag) { 44 return (tag == multi_branch_data_tag) || (tag == arg_info_data_tag); 45 } 46 47 // Perform generic initialization of the data. More specific 48 // initialization occurs in overrides of ProfileData::post_initialize. 49 void DataLayout::initialize(u1 tag, u2 bci, int cell_count) { 50 _header._bits = (intptr_t)0; 51 _header._struct._tag = tag; 52 _header._struct._bci = bci; 53 for (int i = 0; i < cell_count; i++) { 54 set_cell_at(i, (intptr_t)0); 55 } 56 if (needs_array_len(tag)) { 57 set_cell_at(ArrayData::array_len_off_set, cell_count - 1); // -1 for header. 58 } 59 if (tag == call_type_data_tag) { 60 CallTypeData::initialize(this, cell_count); 61 } else if (tag == virtual_call_type_data_tag) { 62 VirtualCallTypeData::initialize(this, cell_count); 63 } 64 } 65 66 void DataLayout::clean_weak_klass_links(BoolObjectClosure* cl) { 67 ResourceMark m; 68 data_in()->clean_weak_klass_links(cl); 69 } 70 71 72 // ================================================================== 73 // ProfileData 74 // 75 // A ProfileData object is created to refer to a section of profiling 76 // data in a structured way. 77 78 // Constructor for invalid ProfileData. 79 ProfileData::ProfileData() { 80 _data = NULL; 81 } 82 83 #ifndef PRODUCT 84 void ProfileData::print_shared(outputStream* st, const char* name) const { 85 st->print("bci: %d", bci()); 86 st->fill_to(tab_width_one); 87 st->print("%s", name); 88 tab(st); 89 int trap = trap_state(); 90 if (trap != 0) { 91 char buf[100]; 92 st->print("trap(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap)); 93 } 94 int flags = data()->flags(); 95 if (flags != 0) 96 st->print("flags(%d) ", flags); 97 } 98 99 void ProfileData::tab(outputStream* st, bool first) const { 100 st->fill_to(first ? tab_width_one : tab_width_two); 101 } 102 #endif // !PRODUCT 103 104 // ================================================================== 105 // BitData 106 // 107 // A BitData corresponds to a one-bit flag. This is used to indicate 108 // whether a checkcast bytecode has seen a null value. 109 110 111 #ifndef PRODUCT 112 void BitData::print_data_on(outputStream* st) const { 113 print_shared(st, "BitData"); 114 } 115 #endif // !PRODUCT 116 117 // ================================================================== 118 // CounterData 119 // 120 // A CounterData corresponds to a simple counter. 121 122 #ifndef PRODUCT 123 void CounterData::print_data_on(outputStream* st) const { 124 print_shared(st, "CounterData"); 125 st->print_cr("count(%u)", count()); 126 } 127 #endif // !PRODUCT 128 129 // ================================================================== 130 // JumpData 131 // 132 // A JumpData is used to access profiling information for a direct 133 // branch. It is a counter, used for counting the number of branches, 134 // plus a data displacement, used for realigning the data pointer to 135 // the corresponding target bci. 136 137 void JumpData::post_initialize(BytecodeStream* stream, MethodData* mdo) { 138 assert(stream->bci() == bci(), "wrong pos"); 139 int target; 140 Bytecodes::Code c = stream->code(); 141 if (c == Bytecodes::_goto_w || c == Bytecodes::_jsr_w) { 142 target = stream->dest_w(); 143 } else { 144 target = stream->dest(); 145 } 146 int my_di = mdo->dp_to_di(dp()); 147 int target_di = mdo->bci_to_di(target); 148 int offset = target_di - my_di; 149 set_displacement(offset); 150 } 151 152 #ifndef PRODUCT 153 void JumpData::print_data_on(outputStream* st) const { 154 print_shared(st, "JumpData"); 155 st->print_cr("taken(%u) displacement(%d)", taken(), displacement()); 156 } 157 #endif // !PRODUCT 158 159 int TypeStackSlotEntries::compute_cell_count(BytecodeStream* stream) { 160 int max = TypeProfileArgsLimit; 161 assert(Bytecodes::is_invoke(stream->code()), "should be invoke"); 162 Bytecode_invoke inv(stream->method(), stream->bci()); 163 164 ResourceMark rm; 165 SignatureStream ss(inv.signature()); 166 int args_count = MIN2(ss.reference_parameter_count(), max); 167 168 return args_count * per_arg_cell_count + (args_count > 0 ? header_cell_count() : 0); 169 } 170 171 class ArgumentOffsetComputer : public SignatureInfo { 172 private: 173 int _max; 174 GrowableArray<int> _offsets; 175 176 void set(int size, BasicType type) { _size += size; } 177 void do_object(int begin, int end) { 178 if (_offsets.length() < _max) { 179 _offsets.push(_size); 180 } 181 SignatureInfo::do_object(begin, end); 182 } 183 void do_array (int begin, int end) { 184 if (_offsets.length() < _max) { 185 _offsets.push(_size); 186 } 187 SignatureInfo::do_array(begin, end); 188 } 189 190 public: 191 ArgumentOffsetComputer(Symbol* signature, int max) 192 : SignatureInfo(signature), _max(max), _offsets(Thread::current(), max) { 193 } 194 195 int total() { lazy_iterate_parameters(); return _size; } 196 197 int off_at(int i) const { return _offsets.at(i); } 198 }; 199 200 void TypeStackSlotEntries::post_initialize(BytecodeStream* stream) { 201 ResourceMark rm; 202 203 assert(Bytecodes::is_invoke(stream->code()), "should be invoke"); 204 Bytecode_invoke inv(stream->method(), stream->bci()); 205 206 #ifdef ASSERT 207 SignatureStream ss(inv.signature()); 208 int count = MIN2(ss.reference_parameter_count(), TypeProfileArgsLimit); 209 assert(count > 0, "room for args type but none found?"); 210 check_number_of_arguments(count); 211 #endif 212 213 int start = 0; 214 ArgumentOffsetComputer aos(inv.signature(), number_of_arguments()-start); 215 aos.total(); 216 bool has_receiver = inv.has_receiver(); 217 for (int i = start; i < number_of_arguments(); i++) { 218 set_stack_slot(i, aos.off_at(i-start) + (has_receiver ? 1 : 0)); 219 set_type(i, type_none()); 220 } 221 } 222 223 bool TypeEntries::is_loader_alive(BoolObjectClosure* is_alive_cl, intptr_t p) { 224 return !is_type_none(p) && 225 !((Klass*)klass_part(p))->is_loader_alive(is_alive_cl); 226 } 227 228 void TypeStackSlotEntries::clean_weak_klass_links(BoolObjectClosure* is_alive_cl) { 229 for (int i = 0; i < number_of_arguments(); i++) { 230 intptr_t p = type(i); 231 if (is_loader_alive(is_alive_cl, p)) { 232 set_type(i, type_none()); 233 } 234 } 235 } 236 237 bool TypeStackSlotEntries::arguments_profiling_enabled() { 238 return MethodData::profile_arguments(); 239 } 240 241 #ifndef PRODUCT 242 void TypeEntries::print_klass(outputStream* st, intptr_t k) { 243 if (is_type_none(k)) { 244 st->print("none"); 245 } else if (is_type_unknown(k)) { 246 st->print("unknown"); 247 } else { 248 valid_klass(k)->print_value_on(st); 249 } 250 if (was_null_seen(k)) { 251 st->print(" (null seen)"); 252 } 253 } 254 255 void TypeStackSlotEntries::print_data_on(outputStream* st) const { 256 _pd->tab(st, true); 257 st->print("argument types"); 258 for (int i = 0; i < number_of_arguments(); i++) { 259 _pd->tab(st); 260 st->print("%d: stack(%u) ", i, stack_slot(i)); 261 print_klass(st, type(i)); 262 st->cr(); 263 } 264 } 265 266 void CallTypeData::print_data_on(outputStream* st) const { 267 CounterData::print_data_on(st); 268 _args.print_data_on(st); 269 } 270 271 void VirtualCallTypeData::print_data_on(outputStream* st) const { 272 VirtualCallData::print_data_on(st); 273 _args.print_data_on(st); 274 } 275 #endif 276 277 // ================================================================== 278 // ReceiverTypeData 279 // 280 // A ReceiverTypeData is used to access profiling information about a 281 // dynamic type check. It consists of a counter which counts the total times 282 // that the check is reached, and a series of (Klass*, count) pairs 283 // which are used to store a type profile for the receiver of the check. 284 285 void ReceiverTypeData::clean_weak_klass_links(BoolObjectClosure* is_alive_cl) { 286 for (uint row = 0; row < row_limit(); row++) { 287 Klass* p = receiver(row); 288 if (p != NULL && !p->is_loader_alive(is_alive_cl)) { 289 clear_row(row); 290 } 291 } 292 } 293 294 #ifndef PRODUCT 295 void ReceiverTypeData::print_receiver_data_on(outputStream* st) const { 296 uint row; 297 int entries = 0; 298 for (row = 0; row < row_limit(); row++) { 299 if (receiver(row) != NULL) entries++; 300 } 301 st->print_cr("count(%u) entries(%u)", count(), entries); 302 int total = count(); 303 for (row = 0; row < row_limit(); row++) { 304 if (receiver(row) != NULL) { 305 total += receiver_count(row); 306 } 307 } 308 for (row = 0; row < row_limit(); row++) { 309 if (receiver(row) != NULL) { 310 tab(st); 311 receiver(row)->print_value_on(st); 312 st->print_cr("(%u %4.2f)", receiver_count(row), (float) receiver_count(row) / (float) total); 313 } 314 } 315 } 316 void ReceiverTypeData::print_data_on(outputStream* st) const { 317 print_shared(st, "ReceiverTypeData"); 318 print_receiver_data_on(st); 319 } 320 void VirtualCallData::print_data_on(outputStream* st) const { 321 print_shared(st, "VirtualCallData"); 322 print_receiver_data_on(st); 323 } 324 #endif // !PRODUCT 325 326 // ================================================================== 327 // RetData 328 // 329 // A RetData is used to access profiling information for a ret bytecode. 330 // It is composed of a count of the number of times that the ret has 331 // been executed, followed by a series of triples of the form 332 // (bci, count, di) which count the number of times that some bci was the 333 // target of the ret and cache a corresponding displacement. 334 335 void RetData::post_initialize(BytecodeStream* stream, MethodData* mdo) { 336 for (uint row = 0; row < row_limit(); row++) { 337 set_bci_displacement(row, -1); 338 set_bci(row, no_bci); 339 } 340 // release so other threads see a consistent state. bci is used as 341 // a valid flag for bci_displacement. 342 OrderAccess::release(); 343 } 344 345 // This routine needs to atomically update the RetData structure, so the 346 // caller needs to hold the RetData_lock before it gets here. Since taking 347 // the lock can block (and allow GC) and since RetData is a ProfileData is a 348 // wrapper around a derived oop, taking the lock in _this_ method will 349 // basically cause the 'this' pointer's _data field to contain junk after the 350 // lock. We require the caller to take the lock before making the ProfileData 351 // structure. Currently the only caller is InterpreterRuntime::update_mdp_for_ret 352 address RetData::fixup_ret(int return_bci, MethodData* h_mdo) { 353 // First find the mdp which corresponds to the return bci. 354 address mdp = h_mdo->bci_to_dp(return_bci); 355 356 // Now check to see if any of the cache slots are open. 357 for (uint row = 0; row < row_limit(); row++) { 358 if (bci(row) == no_bci) { 359 set_bci_displacement(row, mdp - dp()); 360 set_bci_count(row, DataLayout::counter_increment); 361 // Barrier to ensure displacement is written before the bci; allows 362 // the interpreter to read displacement without fear of race condition. 363 release_set_bci(row, return_bci); 364 break; 365 } 366 } 367 return mdp; 368 } 369 370 371 #ifndef PRODUCT 372 void RetData::print_data_on(outputStream* st) const { 373 print_shared(st, "RetData"); 374 uint row; 375 int entries = 0; 376 for (row = 0; row < row_limit(); row++) { 377 if (bci(row) != no_bci) entries++; 378 } 379 st->print_cr("count(%u) entries(%u)", count(), entries); 380 for (row = 0; row < row_limit(); row++) { 381 if (bci(row) != no_bci) { 382 tab(st); 383 st->print_cr("bci(%d: count(%u) displacement(%d))", 384 bci(row), bci_count(row), bci_displacement(row)); 385 } 386 } 387 } 388 #endif // !PRODUCT 389 390 // ================================================================== 391 // BranchData 392 // 393 // A BranchData is used to access profiling data for a two-way branch. 394 // It consists of taken and not_taken counts as well as a data displacement 395 // for the taken case. 396 397 void BranchData::post_initialize(BytecodeStream* stream, MethodData* mdo) { 398 assert(stream->bci() == bci(), "wrong pos"); 399 int target = stream->dest(); 400 int my_di = mdo->dp_to_di(dp()); 401 int target_di = mdo->bci_to_di(target); 402 int offset = target_di - my_di; 403 set_displacement(offset); 404 } 405 406 #ifndef PRODUCT 407 void BranchData::print_data_on(outputStream* st) const { 408 print_shared(st, "BranchData"); 409 st->print_cr("taken(%u) displacement(%d)", 410 taken(), displacement()); 411 tab(st); 412 st->print_cr("not taken(%u)", not_taken()); 413 } 414 #endif 415 416 // ================================================================== 417 // MultiBranchData 418 // 419 // A MultiBranchData is used to access profiling information for 420 // a multi-way branch (*switch bytecodes). It consists of a series 421 // of (count, displacement) pairs, which count the number of times each 422 // case was taken and specify the data displacment for each branch target. 423 424 int MultiBranchData::compute_cell_count(BytecodeStream* stream) { 425 int cell_count = 0; 426 if (stream->code() == Bytecodes::_tableswitch) { 427 Bytecode_tableswitch sw(stream->method()(), stream->bcp()); 428 cell_count = 1 + per_case_cell_count * (1 + sw.length()); // 1 for default 429 } else { 430 Bytecode_lookupswitch sw(stream->method()(), stream->bcp()); 431 cell_count = 1 + per_case_cell_count * (sw.number_of_pairs() + 1); // 1 for default 432 } 433 return cell_count; 434 } 435 436 void MultiBranchData::post_initialize(BytecodeStream* stream, 437 MethodData* mdo) { 438 assert(stream->bci() == bci(), "wrong pos"); 439 int target; 440 int my_di; 441 int target_di; 442 int offset; 443 if (stream->code() == Bytecodes::_tableswitch) { 444 Bytecode_tableswitch sw(stream->method()(), stream->bcp()); 445 int len = sw.length(); 446 assert(array_len() == per_case_cell_count * (len + 1), "wrong len"); 447 for (int count = 0; count < len; count++) { 448 target = sw.dest_offset_at(count) + bci(); 449 my_di = mdo->dp_to_di(dp()); 450 target_di = mdo->bci_to_di(target); 451 offset = target_di - my_di; 452 set_displacement_at(count, offset); 453 } 454 target = sw.default_offset() + bci(); 455 my_di = mdo->dp_to_di(dp()); 456 target_di = mdo->bci_to_di(target); 457 offset = target_di - my_di; 458 set_default_displacement(offset); 459 460 } else { 461 Bytecode_lookupswitch sw(stream->method()(), stream->bcp()); 462 int npairs = sw.number_of_pairs(); 463 assert(array_len() == per_case_cell_count * (npairs + 1), "wrong len"); 464 for (int count = 0; count < npairs; count++) { 465 LookupswitchPair pair = sw.pair_at(count); 466 target = pair.offset() + bci(); 467 my_di = mdo->dp_to_di(dp()); 468 target_di = mdo->bci_to_di(target); 469 offset = target_di - my_di; 470 set_displacement_at(count, offset); 471 } 472 target = sw.default_offset() + bci(); 473 my_di = mdo->dp_to_di(dp()); 474 target_di = mdo->bci_to_di(target); 475 offset = target_di - my_di; 476 set_default_displacement(offset); 477 } 478 } 479 480 #ifndef PRODUCT 481 void MultiBranchData::print_data_on(outputStream* st) const { 482 print_shared(st, "MultiBranchData"); 483 st->print_cr("default_count(%u) displacement(%d)", 484 default_count(), default_displacement()); 485 int cases = number_of_cases(); 486 for (int i = 0; i < cases; i++) { 487 tab(st); 488 st->print_cr("count(%u) displacement(%d)", 489 count_at(i), displacement_at(i)); 490 } 491 } 492 #endif 493 494 #ifndef PRODUCT 495 void ArgInfoData::print_data_on(outputStream* st) const { 496 print_shared(st, "ArgInfoData"); 497 int nargs = number_of_args(); 498 for (int i = 0; i < nargs; i++) { 499 st->print(" 0x%x", arg_modified(i)); 500 } 501 st->cr(); 502 } 503 504 #endif 505 // ================================================================== 506 // MethodData* 507 // 508 // A MethodData* holds information which has been collected about 509 // a method. 510 511 MethodData* MethodData::allocate(ClassLoaderData* loader_data, methodHandle method, TRAPS) { 512 int size = MethodData::compute_allocation_size_in_words(method); 513 514 return new (loader_data, size, false, MetaspaceObj::MethodDataType, THREAD) 515 MethodData(method(), size, CHECK_NULL); 516 } 517 518 int MethodData::bytecode_cell_count(Bytecodes::Code code) { 519 #if defined(COMPILER1) && !defined(COMPILER2) 520 return no_profile_data; 521 #else 522 switch (code) { 523 case Bytecodes::_checkcast: 524 case Bytecodes::_instanceof: 525 case Bytecodes::_aastore: 526 if (TypeProfileCasts) { 527 return ReceiverTypeData::static_cell_count(); 528 } else { 529 return BitData::static_cell_count(); 530 } 531 case Bytecodes::_invokespecial: 532 case Bytecodes::_invokestatic: 533 if (MethodData::profile_arguments()) { 534 return variable_cell_count; 535 } else { 536 return CounterData::static_cell_count(); 537 } 538 case Bytecodes::_goto: 539 case Bytecodes::_goto_w: 540 case Bytecodes::_jsr: 541 case Bytecodes::_jsr_w: 542 return JumpData::static_cell_count(); 543 case Bytecodes::_invokevirtual: 544 case Bytecodes::_invokeinterface: 545 if (MethodData::profile_arguments()) { 546 return variable_cell_count; 547 } else { 548 return VirtualCallData::static_cell_count(); 549 } 550 case Bytecodes::_invokedynamic: 551 if (MethodData::profile_arguments()) { 552 return variable_cell_count; 553 } else { 554 return CounterData::static_cell_count(); 555 } 556 case Bytecodes::_ret: 557 return RetData::static_cell_count(); 558 case Bytecodes::_ifeq: 559 case Bytecodes::_ifne: 560 case Bytecodes::_iflt: 561 case Bytecodes::_ifge: 562 case Bytecodes::_ifgt: 563 case Bytecodes::_ifle: 564 case Bytecodes::_if_icmpeq: 565 case Bytecodes::_if_icmpne: 566 case Bytecodes::_if_icmplt: 567 case Bytecodes::_if_icmpge: 568 case Bytecodes::_if_icmpgt: 569 case Bytecodes::_if_icmple: 570 case Bytecodes::_if_acmpeq: 571 case Bytecodes::_if_acmpne: 572 case Bytecodes::_ifnull: 573 case Bytecodes::_ifnonnull: 574 return BranchData::static_cell_count(); 575 case Bytecodes::_lookupswitch: 576 case Bytecodes::_tableswitch: 577 return variable_cell_count; 578 } 579 return no_profile_data; 580 #endif 581 } 582 583 // Compute the size of the profiling information corresponding to 584 // the current bytecode. 585 int MethodData::compute_data_size(BytecodeStream* stream) { 586 int cell_count = bytecode_cell_count(stream->code()); 587 if (cell_count == no_profile_data) { 588 return 0; 589 } 590 if (cell_count == variable_cell_count) { 591 switch (stream->code()) { 592 case Bytecodes::_lookupswitch: 593 case Bytecodes::_tableswitch: 594 cell_count = MultiBranchData::compute_cell_count(stream); 595 break; 596 case Bytecodes::_invokespecial: 597 case Bytecodes::_invokestatic: 598 case Bytecodes::_invokedynamic: 599 assert(MethodData::profile_arguments(), "should be collecting args profile"); 600 if (profile_arguments_for_invoke(stream->method(), stream->bci())) { 601 cell_count = CallTypeData::compute_cell_count(stream); 602 } else { 603 cell_count = CounterData::static_cell_count(); 604 } 605 break; 606 case Bytecodes::_invokevirtual: 607 case Bytecodes::_invokeinterface: { 608 assert(MethodData::profile_arguments(), "should be collecting args profile"); 609 if (profile_arguments_for_invoke(stream->method(), stream->bci())) { 610 cell_count = VirtualCallTypeData::compute_cell_count(stream); 611 } else { 612 cell_count = VirtualCallData::static_cell_count(); 613 } 614 break; 615 } 616 default: 617 fatal("unexpected bytecode for var length profile data"); 618 } 619 } 620 // Note: cell_count might be zero, meaning that there is just 621 // a DataLayout header, with no extra cells. 622 assert(cell_count >= 0, "sanity"); 623 return DataLayout::compute_size_in_bytes(cell_count); 624 } 625 626 int MethodData::compute_extra_data_count(int data_size, int empty_bc_count) { 627 if (ProfileTraps) { 628 // Assume that up to 3% of BCIs with no MDP will need to allocate one. 629 int extra_data_count = (uint)(empty_bc_count * 3) / 128 + 1; 630 // If the method is large, let the extra BCIs grow numerous (to ~1%). 631 int one_percent_of_data 632 = (uint)data_size / (DataLayout::header_size_in_bytes()*128); 633 if (extra_data_count < one_percent_of_data) 634 extra_data_count = one_percent_of_data; 635 if (extra_data_count > empty_bc_count) 636 extra_data_count = empty_bc_count; // no need for more 637 return extra_data_count; 638 } else { 639 return 0; 640 } 641 } 642 643 // Compute the size of the MethodData* necessary to store 644 // profiling information about a given method. Size is in bytes. 645 int MethodData::compute_allocation_size_in_bytes(methodHandle method) { 646 int data_size = 0; 647 BytecodeStream stream(method); 648 Bytecodes::Code c; 649 int empty_bc_count = 0; // number of bytecodes lacking data 650 while ((c = stream.next()) >= 0) { 651 int size_in_bytes = compute_data_size(&stream); 652 data_size += size_in_bytes; 653 if (size_in_bytes == 0) empty_bc_count += 1; 654 } 655 int object_size = in_bytes(data_offset()) + data_size; 656 657 // Add some extra DataLayout cells (at least one) to track stray traps. 658 int extra_data_count = compute_extra_data_count(data_size, empty_bc_count); 659 object_size += extra_data_count * DataLayout::compute_size_in_bytes(0); 660 661 // Add a cell to record information about modified arguments. 662 int arg_size = method->size_of_parameters(); 663 object_size += DataLayout::compute_size_in_bytes(arg_size+1); 664 665 return object_size; 666 } 667 668 // Compute the size of the MethodData* necessary to store 669 // profiling information about a given method. Size is in words 670 int MethodData::compute_allocation_size_in_words(methodHandle method) { 671 int byte_size = compute_allocation_size_in_bytes(method); 672 int word_size = align_size_up(byte_size, BytesPerWord) / BytesPerWord; 673 return align_object_size(word_size); 674 } 675 676 // Initialize an individual data segment. Returns the size of 677 // the segment in bytes. 678 int MethodData::initialize_data(BytecodeStream* stream, 679 int data_index) { 680 #if defined(COMPILER1) && !defined(COMPILER2) 681 return 0; 682 #else 683 int cell_count = -1; 684 int tag = DataLayout::no_tag; 685 DataLayout* data_layout = data_layout_at(data_index); 686 Bytecodes::Code c = stream->code(); 687 switch (c) { 688 case Bytecodes::_checkcast: 689 case Bytecodes::_instanceof: 690 case Bytecodes::_aastore: 691 if (TypeProfileCasts) { 692 cell_count = ReceiverTypeData::static_cell_count(); 693 tag = DataLayout::receiver_type_data_tag; 694 } else { 695 cell_count = BitData::static_cell_count(); 696 tag = DataLayout::bit_data_tag; 697 } 698 break; 699 case Bytecodes::_invokespecial: 700 case Bytecodes::_invokestatic: { 701 int counter_data_cell_count = CounterData::static_cell_count(); 702 if (profile_arguments_for_invoke(stream->method(), stream->bci())) { 703 cell_count = CallTypeData::compute_cell_count(stream); 704 } else { 705 cell_count = counter_data_cell_count; 706 } 707 if (cell_count > counter_data_cell_count) { 708 tag = DataLayout::call_type_data_tag; 709 } else { 710 tag = DataLayout::counter_data_tag; 711 } 712 break; 713 } 714 case Bytecodes::_goto: 715 case Bytecodes::_goto_w: 716 case Bytecodes::_jsr: 717 case Bytecodes::_jsr_w: 718 cell_count = JumpData::static_cell_count(); 719 tag = DataLayout::jump_data_tag; 720 break; 721 case Bytecodes::_invokevirtual: 722 case Bytecodes::_invokeinterface: { 723 int virtual_call_data_cell_count = VirtualCallData::static_cell_count(); 724 if (profile_arguments_for_invoke(stream->method(), stream->bci())) { 725 cell_count = VirtualCallTypeData::compute_cell_count(stream); 726 } else { 727 cell_count = virtual_call_data_cell_count; 728 } 729 if (cell_count > virtual_call_data_cell_count) { 730 tag = DataLayout::virtual_call_type_data_tag; 731 } else { 732 tag = DataLayout::virtual_call_data_tag; 733 } 734 break; 735 } 736 case Bytecodes::_invokedynamic: { 737 // %%% should make a type profile for any invokedynamic that takes a ref argument 738 int counter_data_cell_count = CounterData::static_cell_count(); 739 if (profile_arguments_for_invoke(stream->method(), stream->bci())) { 740 cell_count = CallTypeData::compute_cell_count(stream); 741 } else { 742 cell_count = counter_data_cell_count; 743 } 744 if (cell_count > counter_data_cell_count) { 745 tag = DataLayout::call_type_data_tag; 746 } else { 747 tag = DataLayout::counter_data_tag; 748 } 749 break; 750 } 751 case Bytecodes::_ret: 752 cell_count = RetData::static_cell_count(); 753 tag = DataLayout::ret_data_tag; 754 break; 755 case Bytecodes::_ifeq: 756 case Bytecodes::_ifne: 757 case Bytecodes::_iflt: 758 case Bytecodes::_ifge: 759 case Bytecodes::_ifgt: 760 case Bytecodes::_ifle: 761 case Bytecodes::_if_icmpeq: 762 case Bytecodes::_if_icmpne: 763 case Bytecodes::_if_icmplt: 764 case Bytecodes::_if_icmpge: 765 case Bytecodes::_if_icmpgt: 766 case Bytecodes::_if_icmple: 767 case Bytecodes::_if_acmpeq: 768 case Bytecodes::_if_acmpne: 769 case Bytecodes::_ifnull: 770 case Bytecodes::_ifnonnull: 771 cell_count = BranchData::static_cell_count(); 772 tag = DataLayout::branch_data_tag; 773 break; 774 case Bytecodes::_lookupswitch: 775 case Bytecodes::_tableswitch: 776 cell_count = MultiBranchData::compute_cell_count(stream); 777 tag = DataLayout::multi_branch_data_tag; 778 break; 779 } 780 assert(tag == DataLayout::multi_branch_data_tag || 781 (MethodData::profile_arguments() && 782 (tag == DataLayout::call_type_data_tag || 783 tag == DataLayout::counter_data_tag || 784 tag == DataLayout::virtual_call_type_data_tag || 785 tag == DataLayout::virtual_call_data_tag)) || 786 cell_count == bytecode_cell_count(c), "cell counts must agree"); 787 if (cell_count >= 0) { 788 assert(tag != DataLayout::no_tag, "bad tag"); 789 assert(bytecode_has_profile(c), "agree w/ BHP"); 790 data_layout->initialize(tag, stream->bci(), cell_count); 791 return DataLayout::compute_size_in_bytes(cell_count); 792 } else { 793 assert(!bytecode_has_profile(c), "agree w/ !BHP"); 794 return 0; 795 } 796 #endif 797 } 798 799 // Get the data at an arbitrary (sort of) data index. 800 ProfileData* MethodData::data_at(int data_index) const { 801 if (out_of_bounds(data_index)) { 802 return NULL; 803 } 804 DataLayout* data_layout = data_layout_at(data_index); 805 return data_layout->data_in(); 806 } 807 808 ProfileData* DataLayout::data_in() { 809 switch (tag()) { 810 case DataLayout::no_tag: 811 default: 812 ShouldNotReachHere(); 813 return NULL; 814 case DataLayout::bit_data_tag: 815 return new BitData(this); 816 case DataLayout::counter_data_tag: 817 return new CounterData(this); 818 case DataLayout::jump_data_tag: 819 return new JumpData(this); 820 case DataLayout::receiver_type_data_tag: 821 return new ReceiverTypeData(this); 822 case DataLayout::virtual_call_data_tag: 823 return new VirtualCallData(this); 824 case DataLayout::ret_data_tag: 825 return new RetData(this); 826 case DataLayout::branch_data_tag: 827 return new BranchData(this); 828 case DataLayout::multi_branch_data_tag: 829 return new MultiBranchData(this); 830 case DataLayout::arg_info_data_tag: 831 return new ArgInfoData(this); 832 case DataLayout::call_type_data_tag: 833 return new CallTypeData(this); 834 case DataLayout::virtual_call_type_data_tag: 835 return new VirtualCallTypeData(this); 836 }; 837 } 838 839 // Iteration over data. 840 ProfileData* MethodData::next_data(ProfileData* current) const { 841 int current_index = dp_to_di(current->dp()); 842 int next_index = current_index + current->size_in_bytes(); 843 ProfileData* next = data_at(next_index); 844 return next; 845 } 846 847 // Give each of the data entries a chance to perform specific 848 // data initialization. 849 void MethodData::post_initialize(BytecodeStream* stream) { 850 ResourceMark rm; 851 ProfileData* data; 852 for (data = first_data(); is_valid(data); data = next_data(data)) { 853 stream->set_start(data->bci()); 854 stream->next(); 855 data->post_initialize(stream, this); 856 } 857 } 858 859 // Initialize the MethodData* corresponding to a given method. 860 MethodData::MethodData(methodHandle method, int size, TRAPS) { 861 No_Safepoint_Verifier no_safepoint; // init function atomic wrt GC 862 ResourceMark rm; 863 // Set the method back-pointer. 864 _method = method(); 865 866 init(); 867 set_creation_mileage(mileage_of(method())); 868 869 // Go through the bytecodes and allocate and initialize the 870 // corresponding data cells. 871 int data_size = 0; 872 int empty_bc_count = 0; // number of bytecodes lacking data 873 _data[0] = 0; // apparently not set below. 874 BytecodeStream stream(method); 875 Bytecodes::Code c; 876 while ((c = stream.next()) >= 0) { 877 int size_in_bytes = initialize_data(&stream, data_size); 878 data_size += size_in_bytes; 879 if (size_in_bytes == 0) empty_bc_count += 1; 880 } 881 _data_size = data_size; 882 int object_size = in_bytes(data_offset()) + data_size; 883 884 // Add some extra DataLayout cells (at least one) to track stray traps. 885 int extra_data_count = compute_extra_data_count(data_size, empty_bc_count); 886 int extra_size = extra_data_count * DataLayout::compute_size_in_bytes(0); 887 888 // Add a cell to record information about modified arguments. 889 // Set up _args_modified array after traps cells so that 890 // the code for traps cells works. 891 DataLayout *dp = data_layout_at(data_size + extra_size); 892 893 int arg_size = method->size_of_parameters(); 894 dp->initialize(DataLayout::arg_info_data_tag, 0, arg_size+1); 895 896 object_size += extra_size + DataLayout::compute_size_in_bytes(arg_size+1); 897 898 // Set an initial hint. Don't use set_hint_di() because 899 // first_di() may be out of bounds if data_size is 0. 900 // In that situation, _hint_di is never used, but at 901 // least well-defined. 902 _hint_di = first_di(); 903 904 post_initialize(&stream); 905 906 set_size(object_size); 907 } 908 909 void MethodData::init() { 910 _invocation_counter.init(); 911 _backedge_counter.init(); 912 _invocation_counter_start = 0; 913 _backedge_counter_start = 0; 914 _num_loops = 0; 915 _num_blocks = 0; 916 _highest_comp_level = 0; 917 _highest_osr_comp_level = 0; 918 _would_profile = true; 919 920 // Initialize flags and trap history. 921 _nof_decompiles = 0; 922 _nof_overflow_recompiles = 0; 923 _nof_overflow_traps = 0; 924 clear_escape_info(); 925 assert(sizeof(_trap_hist) % sizeof(HeapWord) == 0, "align"); 926 Copy::zero_to_words((HeapWord*) &_trap_hist, 927 sizeof(_trap_hist) / sizeof(HeapWord)); 928 } 929 930 // Get a measure of how much mileage the method has on it. 931 int MethodData::mileage_of(Method* method) { 932 int mileage = 0; 933 if (TieredCompilation) { 934 mileage = MAX2(method->invocation_count(), method->backedge_count()); 935 } else { 936 int iic = method->interpreter_invocation_count(); 937 if (mileage < iic) mileage = iic; 938 MethodCounters* mcs = method->method_counters(); 939 if (mcs != NULL) { 940 InvocationCounter* ic = mcs->invocation_counter(); 941 InvocationCounter* bc = mcs->backedge_counter(); 942 int icval = ic->count(); 943 if (ic->carry()) icval += CompileThreshold; 944 if (mileage < icval) mileage = icval; 945 int bcval = bc->count(); 946 if (bc->carry()) bcval += CompileThreshold; 947 if (mileage < bcval) mileage = bcval; 948 } 949 } 950 return mileage; 951 } 952 953 bool MethodData::is_mature() const { 954 return CompilationPolicy::policy()->is_mature(_method); 955 } 956 957 // Translate a bci to its corresponding data index (di). 958 address MethodData::bci_to_dp(int bci) { 959 ResourceMark rm; 960 ProfileData* data = data_before(bci); 961 ProfileData* prev = NULL; 962 for ( ; is_valid(data); data = next_data(data)) { 963 if (data->bci() >= bci) { 964 if (data->bci() == bci) set_hint_di(dp_to_di(data->dp())); 965 else if (prev != NULL) set_hint_di(dp_to_di(prev->dp())); 966 return data->dp(); 967 } 968 prev = data; 969 } 970 return (address)limit_data_position(); 971 } 972 973 // Translate a bci to its corresponding data, or NULL. 974 ProfileData* MethodData::bci_to_data(int bci) { 975 ProfileData* data = data_before(bci); 976 for ( ; is_valid(data); data = next_data(data)) { 977 if (data->bci() == bci) { 978 set_hint_di(dp_to_di(data->dp())); 979 return data; 980 } else if (data->bci() > bci) { 981 break; 982 } 983 } 984 return bci_to_extra_data(bci, false); 985 } 986 987 // Translate a bci to its corresponding extra data, or NULL. 988 ProfileData* MethodData::bci_to_extra_data(int bci, bool create_if_missing) { 989 DataLayout* dp = extra_data_base(); 990 DataLayout* end = extra_data_limit(); 991 DataLayout* avail = NULL; 992 for (; dp < end; dp = next_extra(dp)) { 993 // No need for "OrderAccess::load_acquire" ops, 994 // since the data structure is monotonic. 995 if (dp->tag() == DataLayout::no_tag) break; 996 if (dp->tag() == DataLayout::arg_info_data_tag) { 997 dp = end; // ArgInfoData is at the end of extra data section. 998 break; 999 } 1000 if (dp->bci() == bci) { 1001 assert(dp->tag() == DataLayout::bit_data_tag, "sane"); 1002 return new BitData(dp); 1003 } 1004 } 1005 if (create_if_missing && dp < end) { 1006 // Allocate this one. There is no mutual exclusion, 1007 // so two threads could allocate different BCIs to the 1008 // same data layout. This means these extra data 1009 // records, like most other MDO contents, must not be 1010 // trusted too much. 1011 DataLayout temp; 1012 temp.initialize(DataLayout::bit_data_tag, bci, 0); 1013 dp->release_set_header(temp.header()); 1014 assert(dp->tag() == DataLayout::bit_data_tag, "sane"); 1015 //NO: assert(dp->bci() == bci, "no concurrent allocation"); 1016 return new BitData(dp); 1017 } 1018 return NULL; 1019 } 1020 1021 ArgInfoData *MethodData::arg_info() { 1022 DataLayout* dp = extra_data_base(); 1023 DataLayout* end = extra_data_limit(); 1024 for (; dp < end; dp = next_extra(dp)) { 1025 if (dp->tag() == DataLayout::arg_info_data_tag) 1026 return new ArgInfoData(dp); 1027 } 1028 return NULL; 1029 } 1030 1031 // Printing 1032 1033 #ifndef PRODUCT 1034 1035 void MethodData::print_on(outputStream* st) const { 1036 assert(is_methodData(), "should be method data"); 1037 st->print("method data for "); 1038 method()->print_value_on(st); 1039 st->cr(); 1040 print_data_on(st); 1041 } 1042 1043 #endif //PRODUCT 1044 1045 void MethodData::print_value_on(outputStream* st) const { 1046 assert(is_methodData(), "should be method data"); 1047 st->print("method data for "); 1048 method()->print_value_on(st); 1049 } 1050 1051 #ifndef PRODUCT 1052 void MethodData::print_data_on(outputStream* st) const { 1053 ResourceMark rm; 1054 ProfileData* data = first_data(); 1055 for ( ; is_valid(data); data = next_data(data)) { 1056 st->print("%d", dp_to_di(data->dp())); 1057 st->fill_to(6); 1058 data->print_data_on(st); 1059 } 1060 st->print_cr("--- Extra data:"); 1061 DataLayout* dp = extra_data_base(); 1062 DataLayout* end = extra_data_limit(); 1063 for (; dp < end; dp = next_extra(dp)) { 1064 // No need for "OrderAccess::load_acquire" ops, 1065 // since the data structure is monotonic. 1066 if (dp->tag() == DataLayout::no_tag) continue; 1067 if (dp->tag() == DataLayout::bit_data_tag) { 1068 data = new BitData(dp); 1069 } else { 1070 assert(dp->tag() == DataLayout::arg_info_data_tag, "must be BitData or ArgInfo"); 1071 data = new ArgInfoData(dp); 1072 dp = end; // ArgInfoData is at the end of extra data section. 1073 } 1074 st->print("%d", dp_to_di(data->dp())); 1075 st->fill_to(6); 1076 data->print_data_on(st); 1077 } 1078 } 1079 #endif 1080 1081 #if INCLUDE_SERVICES 1082 // Size Statistics 1083 void MethodData::collect_statistics(KlassSizeStats *sz) const { 1084 int n = sz->count(this); 1085 sz->_method_data_bytes += n; 1086 sz->_method_all_bytes += n; 1087 sz->_rw_bytes += n; 1088 } 1089 #endif // INCLUDE_SERVICES 1090 1091 // Verification 1092 1093 void MethodData::verify_on(outputStream* st) { 1094 guarantee(is_methodData(), "object must be method data"); 1095 // guarantee(m->is_perm(), "should be in permspace"); 1096 this->verify_data_on(st); 1097 } 1098 1099 void MethodData::verify_data_on(outputStream* st) { 1100 NEEDS_CLEANUP; 1101 // not yet implemented. 1102 } 1103 1104 bool MethodData::profile_jsr292(methodHandle m, int bci) { 1105 if (m->is_compiled_lambda_form()) { 1106 return true; 1107 } 1108 1109 Bytecode_invoke inv(m , bci); 1110 return inv.is_invokedynamic() || inv.is_invokehandle(); 1111 } 1112 1113 int MethodData::profile_arguments_flag() { 1114 return TypeProfileLevel; 1115 } 1116 1117 bool MethodData::profile_arguments() { 1118 return profile_arguments_flag() > no_type_profile && profile_arguments_flag() <= type_profile_all; 1119 } 1120 1121 bool MethodData::profile_arguments_jsr292_only() { 1122 return profile_arguments_flag() == type_profile_jsr292; 1123 } 1124 1125 bool MethodData::profile_all_arguments() { 1126 return profile_arguments_flag() == type_profile_all; 1127 } 1128 1129 bool MethodData::profile_arguments_for_invoke(methodHandle m, int bci) { 1130 if (!profile_arguments()) { 1131 return false; 1132 } 1133 1134 if (profile_all_arguments()) { 1135 return true; 1136 } 1137 1138 assert(profile_arguments_jsr292_only(), "inconsistent"); 1139 return profile_jsr292(m, bci); 1140 } 1141