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 #ifndef SHARE_VM_OOPS_METHODDATAOOP_HPP 26 #define SHARE_VM_OOPS_METHODDATAOOP_HPP 27 28 #include "interpreter/bytecodes.hpp" 29 #include "memory/universe.hpp" 30 #include "oops/method.hpp" 31 #include "oops/oop.hpp" 32 #include "runtime/orderAccess.hpp" 33 34 class BytecodeStream; 35 class KlassSizeStats; 36 37 // The MethodData object collects counts and other profile information 38 // during zeroth-tier (interpretive) and first-tier execution. 39 // The profile is used later by compilation heuristics. Some heuristics 40 // enable use of aggressive (or "heroic") optimizations. An aggressive 41 // optimization often has a down-side, a corner case that it handles 42 // poorly, but which is thought to be rare. The profile provides 43 // evidence of this rarity for a given method or even BCI. It allows 44 // the compiler to back out of the optimization at places where it 45 // has historically been a poor choice. Other heuristics try to use 46 // specific information gathered about types observed at a given site. 47 // 48 // All data in the profile is approximate. It is expected to be accurate 49 // on the whole, but the system expects occasional inaccuraces, due to 50 // counter overflow, multiprocessor races during data collection, space 51 // limitations, missing MDO blocks, etc. Bad or missing data will degrade 52 // optimization quality but will not affect correctness. Also, each MDO 53 // is marked with its birth-date ("creation_mileage") which can be used 54 // to assess the quality ("maturity") of its data. 55 // 56 // Short (<32-bit) counters are designed to overflow to a known "saturated" 57 // state. Also, certain recorded per-BCI events are given one-bit counters 58 // which overflow to a saturated state which applied to all counters at 59 // that BCI. In other words, there is a small lattice which approximates 60 // the ideal of an infinite-precision counter for each event at each BCI, 61 // and the lattice quickly "bottoms out" in a state where all counters 62 // are taken to be indefinitely large. 63 // 64 // The reader will find many data races in profile gathering code, starting 65 // with invocation counter incrementation. None of these races harm correct 66 // execution of the compiled code. 67 68 // forward decl 69 class ProfileData; 70 71 // DataLayout 72 // 73 // Overlay for generic profiling data. 74 class DataLayout VALUE_OBJ_CLASS_SPEC { 75 friend class VMStructs; 76 77 private: 78 // Every data layout begins with a header. This header 79 // contains a tag, which is used to indicate the size/layout 80 // of the data, 4 bits of flags, which can be used in any way, 81 // 4 bits of trap history (none/one reason/many reasons), 82 // and a bci, which is used to tie this piece of data to a 83 // specific bci in the bytecodes. 84 union { 85 intptr_t _bits; 86 struct { 87 u1 _tag; 88 u1 _flags; 89 u2 _bci; 90 } _struct; 91 } _header; 92 93 // The data layout has an arbitrary number of cells, each sized 94 // to accomodate a pointer or an integer. 95 intptr_t _cells[1]; 96 97 // Some types of data layouts need a length field. 98 static bool needs_array_len(u1 tag); 99 100 public: 101 enum { 102 counter_increment = 1 103 }; 104 105 enum { 106 cell_size = sizeof(intptr_t) 107 }; 108 109 // Tag values 110 enum { 111 no_tag, 112 bit_data_tag, 113 counter_data_tag, 114 jump_data_tag, 115 receiver_type_data_tag, 116 virtual_call_data_tag, 117 ret_data_tag, 118 branch_data_tag, 119 multi_branch_data_tag, 120 arg_info_data_tag, 121 call_type_data_tag, 122 virtual_call_type_data_tag 123 }; 124 125 enum { 126 // The _struct._flags word is formatted as [trap_state:4 | flags:4]. 127 // The trap state breaks down further as [recompile:1 | reason:3]. 128 // This further breakdown is defined in deoptimization.cpp. 129 // See Deoptimization::trap_state_reason for an assert that 130 // trap_bits is big enough to hold reasons < Reason_RECORDED_LIMIT. 131 // 132 // The trap_state is collected only if ProfileTraps is true. 133 trap_bits = 1+3, // 3: enough to distinguish [0..Reason_RECORDED_LIMIT]. 134 trap_shift = BitsPerByte - trap_bits, 135 trap_mask = right_n_bits(trap_bits), 136 trap_mask_in_place = (trap_mask << trap_shift), 137 flag_limit = trap_shift, 138 flag_mask = right_n_bits(flag_limit), 139 first_flag = 0 140 }; 141 142 // Size computation 143 static int header_size_in_bytes() { 144 return cell_size; 145 } 146 static int header_size_in_cells() { 147 return 1; 148 } 149 150 static int compute_size_in_bytes(int cell_count) { 151 return header_size_in_bytes() + cell_count * cell_size; 152 } 153 154 // Initialization 155 void initialize(u1 tag, u2 bci, int cell_count); 156 157 // Accessors 158 u1 tag() { 159 return _header._struct._tag; 160 } 161 162 // Return a few bits of trap state. Range is [0..trap_mask]. 163 // The state tells if traps with zero, one, or many reasons have occurred. 164 // It also tells whether zero or many recompilations have occurred. 165 // The associated trap histogram in the MDO itself tells whether 166 // traps are common or not. If a BCI shows that a trap X has 167 // occurred, and the MDO shows N occurrences of X, we make the 168 // simplifying assumption that all N occurrences can be blamed 169 // on that BCI. 170 int trap_state() const { 171 return ((_header._struct._flags >> trap_shift) & trap_mask); 172 } 173 174 void set_trap_state(int new_state) { 175 assert(ProfileTraps, "used only under +ProfileTraps"); 176 uint old_flags = (_header._struct._flags & flag_mask); 177 _header._struct._flags = (new_state << trap_shift) | old_flags; 178 } 179 180 u1 flags() const { 181 return _header._struct._flags; 182 } 183 184 u2 bci() const { 185 return _header._struct._bci; 186 } 187 188 void set_header(intptr_t value) { 189 _header._bits = value; 190 } 191 void release_set_header(intptr_t value) { 192 OrderAccess::release_store_ptr(&_header._bits, value); 193 } 194 intptr_t header() { 195 return _header._bits; 196 } 197 void set_cell_at(int index, intptr_t value) { 198 _cells[index] = value; 199 } 200 void release_set_cell_at(int index, intptr_t value) { 201 OrderAccess::release_store_ptr(&_cells[index], value); 202 } 203 intptr_t cell_at(int index) const { 204 return _cells[index]; 205 } 206 207 void set_flag_at(int flag_number) { 208 assert(flag_number < flag_limit, "oob"); 209 _header._struct._flags |= (0x1 << flag_number); 210 } 211 bool flag_at(int flag_number) const { 212 assert(flag_number < flag_limit, "oob"); 213 return (_header._struct._flags & (0x1 << flag_number)) != 0; 214 } 215 216 // Low-level support for code generation. 217 static ByteSize header_offset() { 218 return byte_offset_of(DataLayout, _header); 219 } 220 static ByteSize tag_offset() { 221 return byte_offset_of(DataLayout, _header._struct._tag); 222 } 223 static ByteSize flags_offset() { 224 return byte_offset_of(DataLayout, _header._struct._flags); 225 } 226 static ByteSize bci_offset() { 227 return byte_offset_of(DataLayout, _header._struct._bci); 228 } 229 static ByteSize cell_offset(int index) { 230 return byte_offset_of(DataLayout, _cells) + in_ByteSize(index * cell_size); 231 } 232 // Return a value which, when or-ed as a byte into _flags, sets the flag. 233 static int flag_number_to_byte_constant(int flag_number) { 234 assert(0 <= flag_number && flag_number < flag_limit, "oob"); 235 DataLayout temp; temp.set_header(0); 236 temp.set_flag_at(flag_number); 237 return temp._header._struct._flags; 238 } 239 // Return a value which, when or-ed as a word into _header, sets the flag. 240 static intptr_t flag_mask_to_header_mask(int byte_constant) { 241 DataLayout temp; temp.set_header(0); 242 temp._header._struct._flags = byte_constant; 243 return temp._header._bits; 244 } 245 246 ProfileData* data_in(); 247 248 // GC support 249 void clean_weak_klass_links(BoolObjectClosure* cl); 250 }; 251 252 253 // ProfileData class hierarchy 254 class ProfileData; 255 class BitData; 256 class CounterData; 257 class ReceiverTypeData; 258 class VirtualCallData; 259 class VirtualCallTypeData; 260 class RetData; 261 class CallTypeData; 262 class JumpData; 263 class BranchData; 264 class ArrayData; 265 class MultiBranchData; 266 class ArgInfoData; 267 268 // ProfileData 269 // 270 // A ProfileData object is created to refer to a section of profiling 271 // data in a structured way. 272 class ProfileData : public ResourceObj { 273 friend class TypeEntries; 274 friend class TypeEntriesAtCall; 275 friend class ReturnTypeEntry; 276 friend class TypeStackSlotEntries; 277 private: 278 #ifndef PRODUCT 279 enum { 280 tab_width_one = 16, 281 tab_width_two = 36 282 }; 283 #endif // !PRODUCT 284 285 // This is a pointer to a section of profiling data. 286 DataLayout* _data; 287 288 protected: 289 DataLayout* data() { return _data; } 290 const DataLayout* data() const { return _data; } 291 292 enum { 293 cell_size = DataLayout::cell_size 294 }; 295 296 public: 297 // How many cells are in this? 298 virtual int cell_count() const { 299 ShouldNotReachHere(); 300 return -1; 301 } 302 303 // Return the size of this data. 304 int size_in_bytes() { 305 return DataLayout::compute_size_in_bytes(cell_count()); 306 } 307 308 protected: 309 // Low-level accessors for underlying data 310 void set_intptr_at(int index, intptr_t value) { 311 assert(0 <= index && index < cell_count(), "oob"); 312 data()->set_cell_at(index, value); 313 } 314 void release_set_intptr_at(int index, intptr_t value) { 315 assert(0 <= index && index < cell_count(), "oob"); 316 data()->release_set_cell_at(index, value); 317 } 318 intptr_t intptr_at(int index) const { 319 assert(0 <= index && index < cell_count(), "oob"); 320 return data()->cell_at(index); 321 } 322 void set_uint_at(int index, uint value) { 323 set_intptr_at(index, (intptr_t) value); 324 } 325 void release_set_uint_at(int index, uint value) { 326 release_set_intptr_at(index, (intptr_t) value); 327 } 328 uint uint_at(int index) const { 329 return (uint)intptr_at(index); 330 } 331 void set_int_at(int index, int value) { 332 set_intptr_at(index, (intptr_t) value); 333 } 334 void release_set_int_at(int index, int value) { 335 release_set_intptr_at(index, (intptr_t) value); 336 } 337 int int_at(int index) const { 338 return (int)intptr_at(index); 339 } 340 int int_at_unchecked(int index) const { 341 return (int)data()->cell_at(index); 342 } 343 void set_oop_at(int index, oop value) { 344 set_intptr_at(index, (intptr_t) value); 345 } 346 oop oop_at(int index) const { 347 return (oop)intptr_at(index); 348 } 349 350 void set_flag_at(int flag_number) { 351 data()->set_flag_at(flag_number); 352 } 353 bool flag_at(int flag_number) const { 354 return data()->flag_at(flag_number); 355 } 356 357 // two convenient imports for use by subclasses: 358 static ByteSize cell_offset(int index) { 359 return DataLayout::cell_offset(index); 360 } 361 static int flag_number_to_byte_constant(int flag_number) { 362 return DataLayout::flag_number_to_byte_constant(flag_number); 363 } 364 365 ProfileData(DataLayout* data) { 366 _data = data; 367 } 368 369 public: 370 // Constructor for invalid ProfileData. 371 ProfileData(); 372 373 u2 bci() const { 374 return data()->bci(); 375 } 376 377 address dp() { 378 return (address)_data; 379 } 380 381 int trap_state() const { 382 return data()->trap_state(); 383 } 384 void set_trap_state(int new_state) { 385 data()->set_trap_state(new_state); 386 } 387 388 // Type checking 389 virtual bool is_BitData() const { return false; } 390 virtual bool is_CounterData() const { return false; } 391 virtual bool is_JumpData() const { return false; } 392 virtual bool is_ReceiverTypeData()const { return false; } 393 virtual bool is_VirtualCallData() const { return false; } 394 virtual bool is_RetData() const { return false; } 395 virtual bool is_BranchData() const { return false; } 396 virtual bool is_ArrayData() const { return false; } 397 virtual bool is_MultiBranchData() const { return false; } 398 virtual bool is_ArgInfoData() const { return false; } 399 virtual bool is_CallTypeData() const { return false; } 400 virtual bool is_VirtualCallTypeData()const { return false; } 401 402 403 BitData* as_BitData() const { 404 assert(is_BitData(), "wrong type"); 405 return is_BitData() ? (BitData*) this : NULL; 406 } 407 CounterData* as_CounterData() const { 408 assert(is_CounterData(), "wrong type"); 409 return is_CounterData() ? (CounterData*) this : NULL; 410 } 411 JumpData* as_JumpData() const { 412 assert(is_JumpData(), "wrong type"); 413 return is_JumpData() ? (JumpData*) this : NULL; 414 } 415 ReceiverTypeData* as_ReceiverTypeData() const { 416 assert(is_ReceiverTypeData(), "wrong type"); 417 return is_ReceiverTypeData() ? (ReceiverTypeData*)this : NULL; 418 } 419 VirtualCallData* as_VirtualCallData() const { 420 assert(is_VirtualCallData(), "wrong type"); 421 return is_VirtualCallData() ? (VirtualCallData*)this : NULL; 422 } 423 RetData* as_RetData() const { 424 assert(is_RetData(), "wrong type"); 425 return is_RetData() ? (RetData*) this : NULL; 426 } 427 BranchData* as_BranchData() const { 428 assert(is_BranchData(), "wrong type"); 429 return is_BranchData() ? (BranchData*) this : NULL; 430 } 431 ArrayData* as_ArrayData() const { 432 assert(is_ArrayData(), "wrong type"); 433 return is_ArrayData() ? (ArrayData*) this : NULL; 434 } 435 MultiBranchData* as_MultiBranchData() const { 436 assert(is_MultiBranchData(), "wrong type"); 437 return is_MultiBranchData() ? (MultiBranchData*)this : NULL; 438 } 439 ArgInfoData* as_ArgInfoData() const { 440 assert(is_ArgInfoData(), "wrong type"); 441 return is_ArgInfoData() ? (ArgInfoData*)this : NULL; 442 } 443 CallTypeData* as_CallTypeData() const { 444 assert(is_CallTypeData(), "wrong type"); 445 return is_CallTypeData() ? (CallTypeData*)this : NULL; 446 } 447 VirtualCallTypeData* as_VirtualCallTypeData() const { 448 assert(is_VirtualCallTypeData(), "wrong type"); 449 return is_VirtualCallTypeData() ? (VirtualCallTypeData*)this : NULL; 450 } 451 452 453 // Subclass specific initialization 454 virtual void post_initialize(BytecodeStream* stream, MethodData* mdo) {} 455 456 // GC support 457 virtual void clean_weak_klass_links(BoolObjectClosure* is_alive_closure) {} 458 459 // CI translation: ProfileData can represent both MethodDataOop data 460 // as well as CIMethodData data. This function is provided for translating 461 // an oop in a ProfileData to the ci equivalent. Generally speaking, 462 // most ProfileData don't require any translation, so we provide the null 463 // translation here, and the required translators are in the ci subclasses. 464 virtual void translate_from(const ProfileData* data) {} 465 466 virtual void print_data_on(outputStream* st) const { 467 ShouldNotReachHere(); 468 } 469 470 #ifndef PRODUCT 471 void print_shared(outputStream* st, const char* name) const; 472 void tab(outputStream* st, bool first = false) const; 473 #endif 474 }; 475 476 // BitData 477 // 478 // A BitData holds a flag or two in its header. 479 class BitData : public ProfileData { 480 protected: 481 enum { 482 // null_seen: 483 // saw a null operand (cast/aastore/instanceof) 484 null_seen_flag = DataLayout::first_flag + 0 485 }; 486 enum { bit_cell_count = 0 }; // no additional data fields needed. 487 public: 488 BitData(DataLayout* layout) : ProfileData(layout) { 489 } 490 491 virtual bool is_BitData() const { return true; } 492 493 static int static_cell_count() { 494 return bit_cell_count; 495 } 496 497 virtual int cell_count() const { 498 return static_cell_count(); 499 } 500 501 // Accessor 502 503 // The null_seen flag bit is specially known to the interpreter. 504 // Consulting it allows the compiler to avoid setting up null_check traps. 505 bool null_seen() { return flag_at(null_seen_flag); } 506 void set_null_seen() { set_flag_at(null_seen_flag); } 507 508 509 // Code generation support 510 static int null_seen_byte_constant() { 511 return flag_number_to_byte_constant(null_seen_flag); 512 } 513 514 static ByteSize bit_data_size() { 515 return cell_offset(bit_cell_count); 516 } 517 518 #ifndef PRODUCT 519 void print_data_on(outputStream* st) const; 520 #endif 521 }; 522 523 // CounterData 524 // 525 // A CounterData corresponds to a simple counter. 526 class CounterData : public BitData { 527 protected: 528 enum { 529 count_off, 530 counter_cell_count 531 }; 532 public: 533 CounterData(DataLayout* layout) : BitData(layout) {} 534 535 virtual bool is_CounterData() const { return true; } 536 537 static int static_cell_count() { 538 return counter_cell_count; 539 } 540 541 virtual int cell_count() const { 542 return static_cell_count(); 543 } 544 545 // Direct accessor 546 uint count() const { 547 return uint_at(count_off); 548 } 549 550 // Code generation support 551 static ByteSize count_offset() { 552 return cell_offset(count_off); 553 } 554 static ByteSize counter_data_size() { 555 return cell_offset(counter_cell_count); 556 } 557 558 void set_count(uint count) { 559 set_uint_at(count_off, count); 560 } 561 562 #ifndef PRODUCT 563 void print_data_on(outputStream* st) const; 564 #endif 565 }; 566 567 // JumpData 568 // 569 // A JumpData is used to access profiling information for a direct 570 // branch. It is a counter, used for counting the number of branches, 571 // plus a data displacement, used for realigning the data pointer to 572 // the corresponding target bci. 573 class JumpData : public ProfileData { 574 protected: 575 enum { 576 taken_off_set, 577 displacement_off_set, 578 jump_cell_count 579 }; 580 581 void set_displacement(int displacement) { 582 set_int_at(displacement_off_set, displacement); 583 } 584 585 public: 586 JumpData(DataLayout* layout) : ProfileData(layout) { 587 assert(layout->tag() == DataLayout::jump_data_tag || 588 layout->tag() == DataLayout::branch_data_tag, "wrong type"); 589 } 590 591 virtual bool is_JumpData() const { return true; } 592 593 static int static_cell_count() { 594 return jump_cell_count; 595 } 596 597 virtual int cell_count() const { 598 return static_cell_count(); 599 } 600 601 // Direct accessor 602 uint taken() const { 603 return uint_at(taken_off_set); 604 } 605 606 void set_taken(uint cnt) { 607 set_uint_at(taken_off_set, cnt); 608 } 609 610 // Saturating counter 611 uint inc_taken() { 612 uint cnt = taken() + 1; 613 // Did we wrap? Will compiler screw us?? 614 if (cnt == 0) cnt--; 615 set_uint_at(taken_off_set, cnt); 616 return cnt; 617 } 618 619 int displacement() const { 620 return int_at(displacement_off_set); 621 } 622 623 // Code generation support 624 static ByteSize taken_offset() { 625 return cell_offset(taken_off_set); 626 } 627 628 static ByteSize displacement_offset() { 629 return cell_offset(displacement_off_set); 630 } 631 632 // Specific initialization. 633 void post_initialize(BytecodeStream* stream, MethodData* mdo); 634 635 #ifndef PRODUCT 636 void print_data_on(outputStream* st) const; 637 #endif 638 }; 639 640 // Entries in a ProfileData object to record types: it can either be 641 // none (no profile), unknown (conflicting profile data) or a klass if 642 // a single one is seen. Whether a null reference was seen is also 643 // recorded. No counter is associated with the type and a single type 644 // is tracked (unlike VirtualCallData). 645 class TypeEntries { 646 647 public: 648 649 // A single cell is used to record information for a type: 650 // - the cell is initialized to 0 651 // - when a type is discovered it is stored in the cell 652 // - bit zero of the cell is used to record whether a null reference 653 // was encountered or not 654 // - bit 1 is set to record a conflict in the type information 655 656 enum { 657 null_seen = 1, 658 type_mask = ~null_seen, 659 type_unknown = 2, 660 status_bits = null_seen | type_unknown, 661 type_klass_mask = ~status_bits 662 }; 663 664 // what to initialize a cell to 665 static intptr_t type_none() { 666 return NULL; 667 } 668 669 // null seen = bit 0 set? 670 static bool was_null_seen(intptr_t v) { 671 return v & null_seen; 672 } 673 674 // conflicting type information = bit 1 set? 675 static bool is_type_unknown(intptr_t v) { 676 return v & type_unknown; 677 } 678 679 // not type information yet = all bits cleared, ignoring bit 0? 680 static bool is_type_none(intptr_t v) { 681 return (v & type_mask) == 0; 682 } 683 684 // recorded type: cell without bit 0 and 1 685 static intptr_t klass_part(intptr_t v) { 686 intptr_t r = v & type_klass_mask; 687 assert (r != NULL, "invalid"); 688 return r; 689 } 690 691 // type recorded 692 static Klass* valid_klass(intptr_t k) { 693 if (!is_type_none(k) && 694 !is_type_unknown(k)) { 695 return (Klass*)klass_part(k); 696 } else { 697 return NULL; 698 } 699 } 700 701 static intptr_t with_status(intptr_t k, intptr_t in) { 702 return k | (in & status_bits); 703 } 704 705 static intptr_t with_status(Klass* k, intptr_t in) { 706 return with_status((intptr_t)k, in); 707 } 708 709 #ifndef PRODUCT 710 static void print_klass(outputStream* st, intptr_t k); 711 #endif 712 713 // GC support 714 static bool is_loader_alive(BoolObjectClosure* is_alive_cl, intptr_t p); 715 716 protected: 717 // ProfileData object these entries are part of 718 ProfileData* _pd; 719 // offset within the ProfileData object where the entries start 720 const int _base_off; 721 722 TypeEntries(int base_off, ProfileData* pd) 723 : _base_off(base_off), _pd(pd) {} 724 725 void set_intptr_at(int index, intptr_t value) { 726 _pd->set_intptr_at(index, value); 727 } 728 729 intptr_t intptr_at(int index) const { 730 return _pd->intptr_at(index); 731 } 732 }; 733 734 // Type entries used for arguments passed at a call and parameters on 735 // method entry. 2 cells per entry: one for the type encoded as in 736 // TypeEntries and one initialized with the stack slot where the 737 // profiled object is to be found so that the interpreter can locate 738 // it quickly. 739 class TypeStackSlotEntries : public TypeEntries { 740 741 private: 742 enum { 743 stack_slot_entry, 744 type_entry, 745 per_arg_cell_count 746 }; 747 748 // offset of cell for stack slot for entry i within ProfileData object 749 int stack_slot_offset(int i) const { 750 return _base_off + stack_slot_local_offset(i); 751 } 752 753 protected: 754 const int _nb_entries; 755 756 // offset of cell for type for entry i within ProfileData object 757 int type_offset(int i) const { 758 return _base_off + type_local_offset(i); 759 } 760 761 public: 762 763 TypeStackSlotEntries(int base_off, ProfileData* pd, int nb_entries) 764 : TypeEntries(base_off, pd), _nb_entries(nb_entries) {} 765 766 static int compute_cell_count(Symbol* signature, int max); 767 768 void post_initialize(Symbol* signature, bool has_receiver); 769 770 // offset of cell for stack slot for entry i within this block of cells for a TypeStackSlotEntries 771 static int stack_slot_local_offset(int i) { 772 return i * per_arg_cell_count + stack_slot_entry; 773 } 774 775 // offset of cell for type for entry i within this block of cells for a TypeStackSlotEntries 776 static int type_local_offset(int i) { 777 return i * per_arg_cell_count + type_entry; 778 } 779 780 // stack slot for entry i 781 uint stack_slot(int i) const { 782 assert(i >= 0 && i < _nb_entries, "oob"); 783 return _pd->uint_at(stack_slot_offset(i)); 784 } 785 786 // set stack slot for entry i 787 void set_stack_slot(int i, uint num) { 788 assert(i >= 0 && i < _nb_entries, "oob"); 789 _pd->set_uint_at(stack_slot_offset(i), num); 790 } 791 792 // type for entry i 793 intptr_t type(int i) const { 794 assert(i >= 0 && i < _nb_entries, "oob"); 795 return _pd->intptr_at(type_offset(i)); 796 } 797 798 // set type for entry i 799 void set_type(int i, intptr_t k) { 800 assert(i >= 0 && i < _nb_entries, "oob"); 801 _pd->set_intptr_at(type_offset(i), k); 802 } 803 804 static ByteSize per_arg_size() { 805 return in_ByteSize(per_arg_cell_count * DataLayout::cell_size); 806 } 807 808 static int per_arg_count() { 809 return per_arg_cell_count ; 810 } 811 812 // GC support 813 void clean_weak_klass_links(BoolObjectClosure* is_alive_closure); 814 815 #ifndef PRODUCT 816 void print_data_on(outputStream* st) const; 817 #endif 818 }; 819 820 // Type entry used for return from a call. A single cell to record the 821 // type. 822 class ReturnTypeEntry : public TypeEntries { 823 824 private: 825 enum { 826 cell_count = 1 827 }; 828 829 public: 830 ReturnTypeEntry(int base_off, ProfileData* pd) 831 : TypeEntries(base_off, pd) {} 832 833 void post_initialize() { 834 set_type(type_none()); 835 } 836 837 intptr_t type() const { 838 assert_profiling_enabled(); 839 return _pd->intptr_at(_base_off); 840 } 841 842 void set_type(intptr_t k) { 843 assert_profiling_enabled(); 844 _pd->set_intptr_at(_base_off, k); 845 } 846 847 static int static_cell_count() { 848 assert_profiling_enabled(); 849 return cell_count; 850 } 851 852 static ByteSize size() { 853 assert_profiling_enabled(); 854 return in_ByteSize(cell_count * DataLayout::cell_size); 855 } 856 857 ByteSize type_offset() { 858 assert_profiling_enabled(); 859 return DataLayout::cell_offset(_base_off); 860 } 861 862 static bool profiling_enabled(); 863 864 static void assert_profiling_enabled() { 865 assert(profiling_enabled(), "args profiling should be on"); 866 } 867 868 // GC support 869 void clean_weak_klass_links(BoolObjectClosure* is_alive_closure); 870 871 #ifndef PRODUCT 872 void print_data_on(outputStream* st) const; 873 #endif 874 }; 875 876 // Entries to collect type information at a call: contains arguments 877 // (TypeStackSlotEntries), a return type (ReturnTypeEntry) and a 878 // number of cells. Because the number of cells for the return type is 879 // smaller than the number of cells for the type of an arguments, the 880 // number of cells is used to tell how many arguments are profiled and 881 // whether a return value is profiled. See has_arguments() and 882 // has_return(). 883 class TypeEntriesAtCall { 884 885 private: 886 887 // offset within the ProfileData object where the entries start 888 const int _base_off; 889 // entries for arguments if any 890 TypeStackSlotEntries _args; 891 // entry for return type if any 892 ReturnTypeEntry _ret; 893 894 // Start with a header if needed. It stores the number of cells used 895 // for this call type information. Unless we collect only return 896 // type profiling or only profiling for a single argument the number 897 // of cells is unknown statically. 898 static int header_cell_count() { 899 return (arguments_profiling_enabled() && (TypeProfileArgsLimit > 1 || return_profiling_enabled())) ? 1 : 0; 900 } 901 902 static int cell_count_local_offset() { 903 assert(arguments_profiling_enabled() && (TypeProfileArgsLimit > 1 || return_profiling_enabled()), "no cell count"); 904 return 0; 905 } 906 907 int cell_count_global_offset() const { 908 return _base_off + cell_count_local_offset(); 909 } 910 911 static int stack_slot_local_offset(int i) { 912 assert_arguments_profiling_enabled(); 913 return header_cell_count() + TypeStackSlotEntries::stack_slot_local_offset(i); 914 } 915 916 static int argument_type_local_offset(int i) { 917 assert_arguments_profiling_enabled(); 918 return header_cell_count() + TypeStackSlotEntries::type_local_offset(i);; 919 } 920 921 void check_number_of_arguments(uint total) { 922 assert(number_of_arguments() == total, "should be set in DataLayout::initialize"); 923 } 924 925 // number of cells not counting the header 926 int cell_count_no_header() const { 927 return _pd->uint_at(cell_count_global_offset()); 928 } 929 930 static bool arguments_profiling_enabled(); 931 static void assert_arguments_profiling_enabled() { 932 assert(arguments_profiling_enabled(), "args profiling should be on"); 933 } 934 static bool return_profiling_enabled() { 935 return ReturnTypeEntry::profiling_enabled(); 936 } 937 static void assert_return_profiling_enabled() { 938 ReturnTypeEntry::assert_profiling_enabled(); 939 } 940 941 protected: 942 943 // ProfileData object these entries are part of 944 ProfileData* _pd; 945 946 // An entry for a return value takes less space than an entry for an 947 // argument so if the number of cells exceeds the number of cells 948 // needed for an argument, this object contains type information for 949 // at least one argument. 950 bool has_arguments() const { 951 assert_arguments_profiling_enabled(); 952 return return_profiling_enabled() ? (cell_count_no_header() >= TypeStackSlotEntries::per_arg_count()) : true; 953 } 954 955 public: 956 TypeEntriesAtCall(int base_off, ProfileData* pd) 957 : _base_off(base_off), _pd(pd), 958 _args(base_off + header_cell_count(), pd, arguments_profiling_enabled() ? number_of_arguments() : 0), 959 _ret((arguments_profiling_enabled() && return_profiling_enabled()) ? cell_count() - ReturnTypeEntry::static_cell_count() : _base_off, pd) 960 {} 961 962 static int compute_cell_count(BytecodeStream* stream); 963 964 static void initialize(DataLayout* dl, int base, int cell_count) { 965 if (arguments_profiling_enabled() && (TypeProfileArgsLimit > 1 || return_profiling_enabled())) { 966 int off = base + cell_count_local_offset(); 967 dl->set_cell_at(off, cell_count - base - header_cell_count()); 968 } 969 } 970 971 void post_initialize(BytecodeStream* stream); 972 973 uint number_of_arguments() const { 974 assert_arguments_profiling_enabled(); 975 if (TypeProfileArgsLimit > 1) { 976 // An entry for a return value takes less space than an entry 977 // for an argument, so number of cells divided by the number of 978 // cells for an argument is the number of arguments being 979 // profiled in this object. 980 int cell_count = cell_count_no_header(); 981 assert(!return_profiling_enabled() || TypeStackSlotEntries::per_arg_count() > ReturnTypeEntry::static_cell_count(), "need each per arg entry to be bigger than ret entry"); 982 int nb = cell_count / TypeStackSlotEntries::per_arg_count(); 983 assert((!has_arguments() && nb == 0) || (nb > 0 && nb <= TypeProfileArgsLimit) , "only when we profile args"); 984 return nb; 985 } else { 986 assert(TypeProfileArgsLimit == 1, "at least one arg"); 987 return 1; 988 } 989 } 990 991 int cell_count() const { 992 if (arguments_profiling_enabled() && (TypeProfileArgsLimit > 1 || return_profiling_enabled())) { 993 return _base_off + header_cell_count() + _pd->int_at_unchecked(cell_count_global_offset()); 994 } else if (arguments_profiling_enabled()) { 995 return _base_off + TypeStackSlotEntries::per_arg_count(); 996 } else { 997 assert_return_profiling_enabled(); 998 return _base_off + ReturnTypeEntry::static_cell_count(); 999 } 1000 } 1001 1002 const TypeStackSlotEntries* args_type_data() const { return &_args; } 1003 const ReturnTypeEntry* ret_type_data() const { return &_ret; } 1004 1005 // An entry for a return value takes less space than an entry for an 1006 // argument, so if the remainder of the number of cells divided by 1007 // the number of cells for an argument is not null, a return value 1008 // is profiled in this object. 1009 bool has_return() const { 1010 assert_return_profiling_enabled(); 1011 return arguments_profiling_enabled() ? (cell_count_no_header() % TypeStackSlotEntries::per_arg_count()) != 0 : true; 1012 } 1013 1014 void set_argument_type(int i, Klass* k) { 1015 intptr_t current = _args.type(i); 1016 _args.set_type(i, TypeEntries::with_status(k, current)); 1017 } 1018 1019 void set_return_type(Klass* k) { 1020 intptr_t current = _ret.type(); 1021 _ret.set_type(TypeEntries::with_status(k, current)); 1022 } 1023 1024 // Code generation support 1025 static ByteSize cell_count_offset() { 1026 return in_ByteSize(cell_count_local_offset() * DataLayout::cell_size); 1027 } 1028 1029 static ByteSize args_data_offset() { 1030 return in_ByteSize(header_cell_count() * DataLayout::cell_size); 1031 } 1032 1033 static ByteSize stack_slot_offset(int i) { 1034 return in_ByteSize(stack_slot_local_offset(i) * DataLayout::cell_size); 1035 } 1036 1037 static ByteSize argument_type_offset(int i) { 1038 return in_ByteSize(argument_type_local_offset(i) * DataLayout::cell_size); 1039 } 1040 1041 ByteSize return_type_offset() { 1042 return _ret.type_offset(); 1043 } 1044 1045 // GC support 1046 void clean_weak_klass_links(BoolObjectClosure* is_alive_closure); 1047 1048 #ifndef PRODUCT 1049 void print_data_on(outputStream* st) const; 1050 #endif 1051 }; 1052 1053 // CallTypeData 1054 // 1055 // A CallTypeData is used to access profiling information about a non 1056 // virtual call for which we collect type information about arguments 1057 // and return value. 1058 class CallTypeData : public CounterData { 1059 private: 1060 TypeEntriesAtCall _args_and_ret; 1061 1062 public: 1063 CallTypeData(DataLayout* layout) : 1064 CounterData(layout), _args_and_ret(CounterData::static_cell_count(), this) { 1065 assert(layout->tag() == DataLayout::call_type_data_tag, "wrong type"); 1066 } 1067 1068 const TypeEntriesAtCall* args_and_ret() const { return &_args_and_ret; } 1069 1070 virtual bool is_CallTypeData() const { return true; } 1071 1072 static int static_cell_count() { 1073 return -1; 1074 } 1075 1076 static int compute_cell_count(BytecodeStream* stream) { 1077 return CounterData::static_cell_count() + TypeEntriesAtCall::compute_cell_count(stream); 1078 } 1079 1080 static void initialize(DataLayout* dl, int cell_count) { 1081 TypeEntriesAtCall::initialize(dl, CounterData::static_cell_count(), cell_count); 1082 } 1083 1084 virtual void post_initialize(BytecodeStream* stream, MethodData* mdo) { 1085 _args_and_ret.post_initialize(stream); 1086 } 1087 1088 virtual int cell_count() const { 1089 return _args_and_ret.cell_count(); 1090 } 1091 1092 uint number_of_arguments() const { 1093 return args_and_ret()->number_of_arguments(); 1094 } 1095 1096 void set_argument_type(int i, Klass* k) { 1097 _args_and_ret.set_argument_type(i, k); 1098 } 1099 1100 void set_return_type(Klass* k) { 1101 _args_and_ret.set_return_type(k); 1102 } 1103 1104 // Code generation support 1105 static ByteSize args_data_offset() { 1106 return cell_offset(CounterData::static_cell_count()) + TypeEntriesAtCall::args_data_offset(); 1107 } 1108 1109 // GC support 1110 virtual void clean_weak_klass_links(BoolObjectClosure* is_alive_closure) { 1111 _args_and_ret.clean_weak_klass_links(is_alive_closure); 1112 } 1113 1114 #ifndef PRODUCT 1115 virtual void print_data_on(outputStream* st) const; 1116 #endif 1117 }; 1118 1119 // ReceiverTypeData 1120 // 1121 // A ReceiverTypeData is used to access profiling information about a 1122 // dynamic type check. It consists of a counter which counts the total times 1123 // that the check is reached, and a series of (Klass*, count) pairs 1124 // which are used to store a type profile for the receiver of the check. 1125 class ReceiverTypeData : public CounterData { 1126 protected: 1127 enum { 1128 receiver0_offset = counter_cell_count, 1129 count0_offset, 1130 receiver_type_row_cell_count = (count0_offset + 1) - receiver0_offset 1131 }; 1132 1133 public: 1134 ReceiverTypeData(DataLayout* layout) : CounterData(layout) { 1135 assert(layout->tag() == DataLayout::receiver_type_data_tag || 1136 layout->tag() == DataLayout::virtual_call_data_tag || 1137 layout->tag() == DataLayout::virtual_call_type_data_tag, "wrong type"); 1138 } 1139 1140 virtual bool is_ReceiverTypeData() const { return true; } 1141 1142 static int static_cell_count() { 1143 return counter_cell_count + (uint) TypeProfileWidth * receiver_type_row_cell_count; 1144 } 1145 1146 virtual int cell_count() const { 1147 return static_cell_count(); 1148 } 1149 1150 // Direct accessors 1151 static uint row_limit() { 1152 return TypeProfileWidth; 1153 } 1154 static int receiver_cell_index(uint row) { 1155 return receiver0_offset + row * receiver_type_row_cell_count; 1156 } 1157 static int receiver_count_cell_index(uint row) { 1158 return count0_offset + row * receiver_type_row_cell_count; 1159 } 1160 1161 Klass* receiver(uint row) const { 1162 assert(row < row_limit(), "oob"); 1163 1164 Klass* recv = (Klass*)intptr_at(receiver_cell_index(row)); 1165 assert(recv == NULL || recv->is_klass(), "wrong type"); 1166 return recv; 1167 } 1168 1169 void set_receiver(uint row, Klass* k) { 1170 assert((uint)row < row_limit(), "oob"); 1171 set_intptr_at(receiver_cell_index(row), (uintptr_t)k); 1172 } 1173 1174 uint receiver_count(uint row) const { 1175 assert(row < row_limit(), "oob"); 1176 return uint_at(receiver_count_cell_index(row)); 1177 } 1178 1179 void set_receiver_count(uint row, uint count) { 1180 assert(row < row_limit(), "oob"); 1181 set_uint_at(receiver_count_cell_index(row), count); 1182 } 1183 1184 void clear_row(uint row) { 1185 assert(row < row_limit(), "oob"); 1186 // Clear total count - indicator of polymorphic call site. 1187 // The site may look like as monomorphic after that but 1188 // it allow to have more accurate profiling information because 1189 // there was execution phase change since klasses were unloaded. 1190 // If the site is still polymorphic then MDO will be updated 1191 // to reflect it. But it could be the case that the site becomes 1192 // only bimorphic. Then keeping total count not 0 will be wrong. 1193 // Even if we use monomorphic (when it is not) for compilation 1194 // we will only have trap, deoptimization and recompile again 1195 // with updated MDO after executing method in Interpreter. 1196 // An additional receiver will be recorded in the cleaned row 1197 // during next call execution. 1198 // 1199 // Note: our profiling logic works with empty rows in any slot. 1200 // We do sorting a profiling info (ciCallProfile) for compilation. 1201 // 1202 set_count(0); 1203 set_receiver(row, NULL); 1204 set_receiver_count(row, 0); 1205 } 1206 1207 // Code generation support 1208 static ByteSize receiver_offset(uint row) { 1209 return cell_offset(receiver_cell_index(row)); 1210 } 1211 static ByteSize receiver_count_offset(uint row) { 1212 return cell_offset(receiver_count_cell_index(row)); 1213 } 1214 static ByteSize receiver_type_data_size() { 1215 return cell_offset(static_cell_count()); 1216 } 1217 1218 // GC support 1219 virtual void clean_weak_klass_links(BoolObjectClosure* is_alive_closure); 1220 1221 #ifndef PRODUCT 1222 void print_receiver_data_on(outputStream* st) const; 1223 void print_data_on(outputStream* st) const; 1224 #endif 1225 }; 1226 1227 // VirtualCallData 1228 // 1229 // A VirtualCallData is used to access profiling information about a 1230 // virtual call. For now, it has nothing more than a ReceiverTypeData. 1231 class VirtualCallData : public ReceiverTypeData { 1232 public: 1233 VirtualCallData(DataLayout* layout) : ReceiverTypeData(layout) { 1234 assert(layout->tag() == DataLayout::virtual_call_data_tag || 1235 layout->tag() == DataLayout::virtual_call_type_data_tag, "wrong type"); 1236 } 1237 1238 virtual bool is_VirtualCallData() const { return true; } 1239 1240 static int static_cell_count() { 1241 // At this point we could add more profile state, e.g., for arguments. 1242 // But for now it's the same size as the base record type. 1243 return ReceiverTypeData::static_cell_count(); 1244 } 1245 1246 virtual int cell_count() const { 1247 return static_cell_count(); 1248 } 1249 1250 // Direct accessors 1251 static ByteSize virtual_call_data_size() { 1252 return cell_offset(static_cell_count()); 1253 } 1254 1255 #ifndef PRODUCT 1256 void print_data_on(outputStream* st) const; 1257 #endif 1258 }; 1259 1260 // VirtualCallTypeData 1261 // 1262 // A VirtualCallTypeData is used to access profiling information about 1263 // a virtual call for which we collect type information about 1264 // arguments and return value. 1265 class VirtualCallTypeData : public VirtualCallData { 1266 private: 1267 TypeEntriesAtCall _args_and_ret; 1268 1269 public: 1270 VirtualCallTypeData(DataLayout* layout) : 1271 VirtualCallData(layout), _args_and_ret(VirtualCallData::static_cell_count(), this) { 1272 assert(layout->tag() == DataLayout::virtual_call_type_data_tag, "wrong type"); 1273 } 1274 1275 const TypeEntriesAtCall* args_and_ret() const { return &_args_and_ret; } 1276 1277 virtual bool is_VirtualCallTypeData() const { return true; } 1278 1279 static int static_cell_count() { 1280 return -1; 1281 } 1282 1283 static int compute_cell_count(BytecodeStream* stream) { 1284 return VirtualCallData::static_cell_count() + TypeEntriesAtCall::compute_cell_count(stream); 1285 } 1286 1287 static void initialize(DataLayout* dl, int cell_count) { 1288 TypeEntriesAtCall::initialize(dl, VirtualCallData::static_cell_count(), cell_count); 1289 } 1290 1291 virtual void post_initialize(BytecodeStream* stream, MethodData* mdo) { 1292 _args_and_ret.post_initialize(stream); 1293 } 1294 1295 virtual int cell_count() const { 1296 return _args_and_ret.cell_count(); 1297 } 1298 1299 uint number_of_arguments() const { 1300 return args_and_ret()->number_of_arguments(); 1301 } 1302 1303 void set_argument_type(int i, Klass* k) { 1304 _args_and_ret.set_argument_type(i, k); 1305 } 1306 1307 void set_return_type(Klass* k) { 1308 _args_and_ret.set_return_type(k); 1309 } 1310 1311 // Code generation support 1312 static ByteSize args_data_offset() { 1313 return cell_offset(VirtualCallData::static_cell_count()) + TypeEntriesAtCall::args_data_offset(); 1314 } 1315 1316 // GC support 1317 virtual void clean_weak_klass_links(BoolObjectClosure* is_alive_closure) { 1318 ReceiverTypeData::clean_weak_klass_links(is_alive_closure); 1319 _args_and_ret.clean_weak_klass_links(is_alive_closure); 1320 } 1321 1322 #ifndef PRODUCT 1323 virtual void print_data_on(outputStream* st) const; 1324 #endif 1325 }; 1326 1327 // RetData 1328 // 1329 // A RetData is used to access profiling information for a ret bytecode. 1330 // It is composed of a count of the number of times that the ret has 1331 // been executed, followed by a series of triples of the form 1332 // (bci, count, di) which count the number of times that some bci was the 1333 // target of the ret and cache a corresponding data displacement. 1334 class RetData : public CounterData { 1335 protected: 1336 enum { 1337 bci0_offset = counter_cell_count, 1338 count0_offset, 1339 displacement0_offset, 1340 ret_row_cell_count = (displacement0_offset + 1) - bci0_offset 1341 }; 1342 1343 void set_bci(uint row, int bci) { 1344 assert((uint)row < row_limit(), "oob"); 1345 set_int_at(bci0_offset + row * ret_row_cell_count, bci); 1346 } 1347 void release_set_bci(uint row, int bci) { 1348 assert((uint)row < row_limit(), "oob"); 1349 // 'release' when setting the bci acts as a valid flag for other 1350 // threads wrt bci_count and bci_displacement. 1351 release_set_int_at(bci0_offset + row * ret_row_cell_count, bci); 1352 } 1353 void set_bci_count(uint row, uint count) { 1354 assert((uint)row < row_limit(), "oob"); 1355 set_uint_at(count0_offset + row * ret_row_cell_count, count); 1356 } 1357 void set_bci_displacement(uint row, int disp) { 1358 set_int_at(displacement0_offset + row * ret_row_cell_count, disp); 1359 } 1360 1361 public: 1362 RetData(DataLayout* layout) : CounterData(layout) { 1363 assert(layout->tag() == DataLayout::ret_data_tag, "wrong type"); 1364 } 1365 1366 virtual bool is_RetData() const { return true; } 1367 1368 enum { 1369 no_bci = -1 // value of bci when bci1/2 are not in use. 1370 }; 1371 1372 static int static_cell_count() { 1373 return counter_cell_count + (uint) BciProfileWidth * ret_row_cell_count; 1374 } 1375 1376 virtual int cell_count() const { 1377 return static_cell_count(); 1378 } 1379 1380 static uint row_limit() { 1381 return BciProfileWidth; 1382 } 1383 static int bci_cell_index(uint row) { 1384 return bci0_offset + row * ret_row_cell_count; 1385 } 1386 static int bci_count_cell_index(uint row) { 1387 return count0_offset + row * ret_row_cell_count; 1388 } 1389 static int bci_displacement_cell_index(uint row) { 1390 return displacement0_offset + row * ret_row_cell_count; 1391 } 1392 1393 // Direct accessors 1394 int bci(uint row) const { 1395 return int_at(bci_cell_index(row)); 1396 } 1397 uint bci_count(uint row) const { 1398 return uint_at(bci_count_cell_index(row)); 1399 } 1400 int bci_displacement(uint row) const { 1401 return int_at(bci_displacement_cell_index(row)); 1402 } 1403 1404 // Interpreter Runtime support 1405 address fixup_ret(int return_bci, MethodData* mdo); 1406 1407 // Code generation support 1408 static ByteSize bci_offset(uint row) { 1409 return cell_offset(bci_cell_index(row)); 1410 } 1411 static ByteSize bci_count_offset(uint row) { 1412 return cell_offset(bci_count_cell_index(row)); 1413 } 1414 static ByteSize bci_displacement_offset(uint row) { 1415 return cell_offset(bci_displacement_cell_index(row)); 1416 } 1417 1418 // Specific initialization. 1419 void post_initialize(BytecodeStream* stream, MethodData* mdo); 1420 1421 #ifndef PRODUCT 1422 void print_data_on(outputStream* st) const; 1423 #endif 1424 }; 1425 1426 // BranchData 1427 // 1428 // A BranchData is used to access profiling data for a two-way branch. 1429 // It consists of taken and not_taken counts as well as a data displacement 1430 // for the taken case. 1431 class BranchData : public JumpData { 1432 protected: 1433 enum { 1434 not_taken_off_set = jump_cell_count, 1435 branch_cell_count 1436 }; 1437 1438 void set_displacement(int displacement) { 1439 set_int_at(displacement_off_set, displacement); 1440 } 1441 1442 public: 1443 BranchData(DataLayout* layout) : JumpData(layout) { 1444 assert(layout->tag() == DataLayout::branch_data_tag, "wrong type"); 1445 } 1446 1447 virtual bool is_BranchData() const { return true; } 1448 1449 static int static_cell_count() { 1450 return branch_cell_count; 1451 } 1452 1453 virtual int cell_count() const { 1454 return static_cell_count(); 1455 } 1456 1457 // Direct accessor 1458 uint not_taken() const { 1459 return uint_at(not_taken_off_set); 1460 } 1461 1462 void set_not_taken(uint cnt) { 1463 set_uint_at(not_taken_off_set, cnt); 1464 } 1465 1466 uint inc_not_taken() { 1467 uint cnt = not_taken() + 1; 1468 // Did we wrap? Will compiler screw us?? 1469 if (cnt == 0) cnt--; 1470 set_uint_at(not_taken_off_set, cnt); 1471 return cnt; 1472 } 1473 1474 // Code generation support 1475 static ByteSize not_taken_offset() { 1476 return cell_offset(not_taken_off_set); 1477 } 1478 static ByteSize branch_data_size() { 1479 return cell_offset(branch_cell_count); 1480 } 1481 1482 // Specific initialization. 1483 void post_initialize(BytecodeStream* stream, MethodData* mdo); 1484 1485 #ifndef PRODUCT 1486 void print_data_on(outputStream* st) const; 1487 #endif 1488 }; 1489 1490 // ArrayData 1491 // 1492 // A ArrayData is a base class for accessing profiling data which does 1493 // not have a statically known size. It consists of an array length 1494 // and an array start. 1495 class ArrayData : public ProfileData { 1496 protected: 1497 friend class DataLayout; 1498 1499 enum { 1500 array_len_off_set, 1501 array_start_off_set 1502 }; 1503 1504 uint array_uint_at(int index) const { 1505 int aindex = index + array_start_off_set; 1506 return uint_at(aindex); 1507 } 1508 int array_int_at(int index) const { 1509 int aindex = index + array_start_off_set; 1510 return int_at(aindex); 1511 } 1512 oop array_oop_at(int index) const { 1513 int aindex = index + array_start_off_set; 1514 return oop_at(aindex); 1515 } 1516 void array_set_int_at(int index, int value) { 1517 int aindex = index + array_start_off_set; 1518 set_int_at(aindex, value); 1519 } 1520 1521 // Code generation support for subclasses. 1522 static ByteSize array_element_offset(int index) { 1523 return cell_offset(array_start_off_set + index); 1524 } 1525 1526 public: 1527 ArrayData(DataLayout* layout) : ProfileData(layout) {} 1528 1529 virtual bool is_ArrayData() const { return true; } 1530 1531 static int static_cell_count() { 1532 return -1; 1533 } 1534 1535 int array_len() const { 1536 return int_at_unchecked(array_len_off_set); 1537 } 1538 1539 virtual int cell_count() const { 1540 return array_len() + 1; 1541 } 1542 1543 // Code generation support 1544 static ByteSize array_len_offset() { 1545 return cell_offset(array_len_off_set); 1546 } 1547 static ByteSize array_start_offset() { 1548 return cell_offset(array_start_off_set); 1549 } 1550 }; 1551 1552 // MultiBranchData 1553 // 1554 // A MultiBranchData is used to access profiling information for 1555 // a multi-way branch (*switch bytecodes). It consists of a series 1556 // of (count, displacement) pairs, which count the number of times each 1557 // case was taken and specify the data displacment for each branch target. 1558 class MultiBranchData : public ArrayData { 1559 protected: 1560 enum { 1561 default_count_off_set, 1562 default_disaplacement_off_set, 1563 case_array_start 1564 }; 1565 enum { 1566 relative_count_off_set, 1567 relative_displacement_off_set, 1568 per_case_cell_count 1569 }; 1570 1571 void set_default_displacement(int displacement) { 1572 array_set_int_at(default_disaplacement_off_set, displacement); 1573 } 1574 void set_displacement_at(int index, int displacement) { 1575 array_set_int_at(case_array_start + 1576 index * per_case_cell_count + 1577 relative_displacement_off_set, 1578 displacement); 1579 } 1580 1581 public: 1582 MultiBranchData(DataLayout* layout) : ArrayData(layout) { 1583 assert(layout->tag() == DataLayout::multi_branch_data_tag, "wrong type"); 1584 } 1585 1586 virtual bool is_MultiBranchData() const { return true; } 1587 1588 static int compute_cell_count(BytecodeStream* stream); 1589 1590 int number_of_cases() const { 1591 int alen = array_len() - 2; // get rid of default case here. 1592 assert(alen % per_case_cell_count == 0, "must be even"); 1593 return (alen / per_case_cell_count); 1594 } 1595 1596 uint default_count() const { 1597 return array_uint_at(default_count_off_set); 1598 } 1599 int default_displacement() const { 1600 return array_int_at(default_disaplacement_off_set); 1601 } 1602 1603 uint count_at(int index) const { 1604 return array_uint_at(case_array_start + 1605 index * per_case_cell_count + 1606 relative_count_off_set); 1607 } 1608 int displacement_at(int index) const { 1609 return array_int_at(case_array_start + 1610 index * per_case_cell_count + 1611 relative_displacement_off_set); 1612 } 1613 1614 // Code generation support 1615 static ByteSize default_count_offset() { 1616 return array_element_offset(default_count_off_set); 1617 } 1618 static ByteSize default_displacement_offset() { 1619 return array_element_offset(default_disaplacement_off_set); 1620 } 1621 static ByteSize case_count_offset(int index) { 1622 return case_array_offset() + 1623 (per_case_size() * index) + 1624 relative_count_offset(); 1625 } 1626 static ByteSize case_array_offset() { 1627 return array_element_offset(case_array_start); 1628 } 1629 static ByteSize per_case_size() { 1630 return in_ByteSize(per_case_cell_count) * cell_size; 1631 } 1632 static ByteSize relative_count_offset() { 1633 return in_ByteSize(relative_count_off_set) * cell_size; 1634 } 1635 static ByteSize relative_displacement_offset() { 1636 return in_ByteSize(relative_displacement_off_set) * cell_size; 1637 } 1638 1639 // Specific initialization. 1640 void post_initialize(BytecodeStream* stream, MethodData* mdo); 1641 1642 #ifndef PRODUCT 1643 void print_data_on(outputStream* st) const; 1644 #endif 1645 }; 1646 1647 class ArgInfoData : public ArrayData { 1648 1649 public: 1650 ArgInfoData(DataLayout* layout) : ArrayData(layout) { 1651 assert(layout->tag() == DataLayout::arg_info_data_tag, "wrong type"); 1652 } 1653 1654 virtual bool is_ArgInfoData() const { return true; } 1655 1656 1657 int number_of_args() const { 1658 return array_len(); 1659 } 1660 1661 uint arg_modified(int arg) const { 1662 return array_uint_at(arg); 1663 } 1664 1665 void set_arg_modified(int arg, uint val) { 1666 array_set_int_at(arg, val); 1667 } 1668 1669 #ifndef PRODUCT 1670 void print_data_on(outputStream* st) const; 1671 #endif 1672 }; 1673 1674 // MethodData* 1675 // 1676 // A MethodData* holds information which has been collected about 1677 // a method. Its layout looks like this: 1678 // 1679 // ----------------------------- 1680 // | header | 1681 // | klass | 1682 // ----------------------------- 1683 // | method | 1684 // | size of the MethodData* | 1685 // ----------------------------- 1686 // | Data entries... | 1687 // | (variable size) | 1688 // | | 1689 // . . 1690 // . . 1691 // . . 1692 // | | 1693 // ----------------------------- 1694 // 1695 // The data entry area is a heterogeneous array of DataLayouts. Each 1696 // DataLayout in the array corresponds to a specific bytecode in the 1697 // method. The entries in the array are sorted by the corresponding 1698 // bytecode. Access to the data is via resource-allocated ProfileData, 1699 // which point to the underlying blocks of DataLayout structures. 1700 // 1701 // During interpretation, if profiling in enabled, the interpreter 1702 // maintains a method data pointer (mdp), which points at the entry 1703 // in the array corresponding to the current bci. In the course of 1704 // intepretation, when a bytecode is encountered that has profile data 1705 // associated with it, the entry pointed to by mdp is updated, then the 1706 // mdp is adjusted to point to the next appropriate DataLayout. If mdp 1707 // is NULL to begin with, the interpreter assumes that the current method 1708 // is not (yet) being profiled. 1709 // 1710 // In MethodData* parlance, "dp" is a "data pointer", the actual address 1711 // of a DataLayout element. A "di" is a "data index", the offset in bytes 1712 // from the base of the data entry array. A "displacement" is the byte offset 1713 // in certain ProfileData objects that indicate the amount the mdp must be 1714 // adjusted in the event of a change in control flow. 1715 // 1716 1717 class MethodData : public Metadata { 1718 friend class VMStructs; 1719 private: 1720 friend class ProfileData; 1721 1722 // Back pointer to the Method* 1723 Method* _method; 1724 1725 // Size of this oop in bytes 1726 int _size; 1727 1728 // Cached hint for bci_to_dp and bci_to_data 1729 int _hint_di; 1730 1731 MethodData(methodHandle method, int size, TRAPS); 1732 public: 1733 static MethodData* allocate(ClassLoaderData* loader_data, methodHandle method, TRAPS); 1734 MethodData() {}; // For ciMethodData 1735 1736 bool is_methodData() const volatile { return true; } 1737 1738 // Whole-method sticky bits and flags 1739 enum { 1740 _trap_hist_limit = 17, // decoupled from Deoptimization::Reason_LIMIT 1741 _trap_hist_mask = max_jubyte, 1742 _extra_data_count = 4 // extra DataLayout headers, for trap history 1743 }; // Public flag values 1744 private: 1745 uint _nof_decompiles; // count of all nmethod removals 1746 uint _nof_overflow_recompiles; // recompile count, excluding recomp. bits 1747 uint _nof_overflow_traps; // trap count, excluding _trap_hist 1748 union { 1749 intptr_t _align; 1750 u1 _array[_trap_hist_limit]; 1751 } _trap_hist; 1752 1753 // Support for interprocedural escape analysis, from Thomas Kotzmann. 1754 intx _eflags; // flags on escape information 1755 intx _arg_local; // bit set of non-escaping arguments 1756 intx _arg_stack; // bit set of stack-allocatable arguments 1757 intx _arg_returned; // bit set of returned arguments 1758 1759 int _creation_mileage; // method mileage at MDO creation 1760 1761 // How many invocations has this MDO seen? 1762 // These counters are used to determine the exact age of MDO. 1763 // We need those because in tiered a method can be concurrently 1764 // executed at different levels. 1765 InvocationCounter _invocation_counter; 1766 // Same for backedges. 1767 InvocationCounter _backedge_counter; 1768 // Counter values at the time profiling started. 1769 int _invocation_counter_start; 1770 int _backedge_counter_start; 1771 // Number of loops and blocks is computed when compiling the first 1772 // time with C1. It is used to determine if method is trivial. 1773 short _num_loops; 1774 short _num_blocks; 1775 // Highest compile level this method has ever seen. 1776 u1 _highest_comp_level; 1777 // Same for OSR level 1778 u1 _highest_osr_comp_level; 1779 // Does this method contain anything worth profiling? 1780 bool _would_profile; 1781 1782 // Size of _data array in bytes. (Excludes header and extra_data fields.) 1783 int _data_size; 1784 1785 // Beginning of the data entries 1786 intptr_t _data[1]; 1787 1788 // Helper for size computation 1789 static int compute_data_size(BytecodeStream* stream); 1790 static int bytecode_cell_count(Bytecodes::Code code); 1791 enum { no_profile_data = -1, variable_cell_count = -2 }; 1792 1793 // Helper for initialization 1794 DataLayout* data_layout_at(int data_index) const { 1795 assert(data_index % sizeof(intptr_t) == 0, "unaligned"); 1796 return (DataLayout*) (((address)_data) + data_index); 1797 } 1798 1799 // Initialize an individual data segment. Returns the size of 1800 // the segment in bytes. 1801 int initialize_data(BytecodeStream* stream, int data_index); 1802 1803 // Helper for data_at 1804 DataLayout* limit_data_position() const { 1805 return (DataLayout*)((address)data_base() + _data_size); 1806 } 1807 bool out_of_bounds(int data_index) const { 1808 return data_index >= data_size(); 1809 } 1810 1811 // Give each of the data entries a chance to perform specific 1812 // data initialization. 1813 void post_initialize(BytecodeStream* stream); 1814 1815 // hint accessors 1816 int hint_di() const { return _hint_di; } 1817 void set_hint_di(int di) { 1818 assert(!out_of_bounds(di), "hint_di out of bounds"); 1819 _hint_di = di; 1820 } 1821 ProfileData* data_before(int bci) { 1822 // avoid SEGV on this edge case 1823 if (data_size() == 0) 1824 return NULL; 1825 int hint = hint_di(); 1826 if (data_layout_at(hint)->bci() <= bci) 1827 return data_at(hint); 1828 return first_data(); 1829 } 1830 1831 // What is the index of the first data entry? 1832 int first_di() const { return 0; } 1833 1834 // Find or create an extra ProfileData: 1835 ProfileData* bci_to_extra_data(int bci, bool create_if_missing); 1836 1837 // return the argument info cell 1838 ArgInfoData *arg_info(); 1839 1840 enum { 1841 no_type_profile = 0, 1842 type_profile_jsr292 = 1, 1843 type_profile_all = 2 1844 }; 1845 1846 static bool profile_jsr292(methodHandle m, int bci); 1847 static int profile_arguments_flag(); 1848 static bool profile_arguments_jsr292_only(); 1849 static bool profile_all_arguments(); 1850 static bool profile_arguments_for_invoke(methodHandle m, int bci); 1851 static int profile_return_flag(); 1852 static bool profile_return_jsr292_only(); 1853 static bool profile_all_return(); 1854 static bool profile_return_for_invoke(methodHandle m, int bci); 1855 1856 public: 1857 static int header_size() { 1858 return sizeof(MethodData)/wordSize; 1859 } 1860 1861 // Compute the size of a MethodData* before it is created. 1862 static int compute_allocation_size_in_bytes(methodHandle method); 1863 static int compute_allocation_size_in_words(methodHandle method); 1864 static int compute_extra_data_count(int data_size, int empty_bc_count); 1865 1866 // Determine if a given bytecode can have profile information. 1867 static bool bytecode_has_profile(Bytecodes::Code code) { 1868 return bytecode_cell_count(code) != no_profile_data; 1869 } 1870 1871 // reset into original state 1872 void init(); 1873 1874 // My size 1875 int size_in_bytes() const { return _size; } 1876 int size() const { return align_object_size(align_size_up(_size, BytesPerWord)/BytesPerWord); } 1877 #if INCLUDE_SERVICES 1878 void collect_statistics(KlassSizeStats *sz) const; 1879 #endif 1880 1881 int creation_mileage() const { return _creation_mileage; } 1882 void set_creation_mileage(int x) { _creation_mileage = x; } 1883 1884 int invocation_count() { 1885 if (invocation_counter()->carry()) { 1886 return InvocationCounter::count_limit; 1887 } 1888 return invocation_counter()->count(); 1889 } 1890 int backedge_count() { 1891 if (backedge_counter()->carry()) { 1892 return InvocationCounter::count_limit; 1893 } 1894 return backedge_counter()->count(); 1895 } 1896 1897 int invocation_count_start() { 1898 if (invocation_counter()->carry()) { 1899 return 0; 1900 } 1901 return _invocation_counter_start; 1902 } 1903 1904 int backedge_count_start() { 1905 if (backedge_counter()->carry()) { 1906 return 0; 1907 } 1908 return _backedge_counter_start; 1909 } 1910 1911 int invocation_count_delta() { return invocation_count() - invocation_count_start(); } 1912 int backedge_count_delta() { return backedge_count() - backedge_count_start(); } 1913 1914 void reset_start_counters() { 1915 _invocation_counter_start = invocation_count(); 1916 _backedge_counter_start = backedge_count(); 1917 } 1918 1919 InvocationCounter* invocation_counter() { return &_invocation_counter; } 1920 InvocationCounter* backedge_counter() { return &_backedge_counter; } 1921 1922 void set_would_profile(bool p) { _would_profile = p; } 1923 bool would_profile() const { return _would_profile; } 1924 1925 int highest_comp_level() const { return _highest_comp_level; } 1926 void set_highest_comp_level(int level) { _highest_comp_level = level; } 1927 int highest_osr_comp_level() const { return _highest_osr_comp_level; } 1928 void set_highest_osr_comp_level(int level) { _highest_osr_comp_level = level; } 1929 1930 int num_loops() const { return _num_loops; } 1931 void set_num_loops(int n) { _num_loops = n; } 1932 int num_blocks() const { return _num_blocks; } 1933 void set_num_blocks(int n) { _num_blocks = n; } 1934 1935 bool is_mature() const; // consult mileage and ProfileMaturityPercentage 1936 static int mileage_of(Method* m); 1937 1938 // Support for interprocedural escape analysis, from Thomas Kotzmann. 1939 enum EscapeFlag { 1940 estimated = 1 << 0, 1941 return_local = 1 << 1, 1942 return_allocated = 1 << 2, 1943 allocated_escapes = 1 << 3, 1944 unknown_modified = 1 << 4 1945 }; 1946 1947 intx eflags() { return _eflags; } 1948 intx arg_local() { return _arg_local; } 1949 intx arg_stack() { return _arg_stack; } 1950 intx arg_returned() { return _arg_returned; } 1951 uint arg_modified(int a) { ArgInfoData *aid = arg_info(); 1952 assert(aid != NULL, "arg_info must be not null"); 1953 assert(a >= 0 && a < aid->number_of_args(), "valid argument number"); 1954 return aid->arg_modified(a); } 1955 1956 void set_eflags(intx v) { _eflags = v; } 1957 void set_arg_local(intx v) { _arg_local = v; } 1958 void set_arg_stack(intx v) { _arg_stack = v; } 1959 void set_arg_returned(intx v) { _arg_returned = v; } 1960 void set_arg_modified(int a, uint v) { ArgInfoData *aid = arg_info(); 1961 assert(aid != NULL, "arg_info must be not null"); 1962 assert(a >= 0 && a < aid->number_of_args(), "valid argument number"); 1963 aid->set_arg_modified(a, v); } 1964 1965 void clear_escape_info() { _eflags = _arg_local = _arg_stack = _arg_returned = 0; } 1966 1967 // Location and size of data area 1968 address data_base() const { 1969 return (address) _data; 1970 } 1971 int data_size() const { 1972 return _data_size; 1973 } 1974 1975 // Accessors 1976 Method* method() const { return _method; } 1977 1978 // Get the data at an arbitrary (sort of) data index. 1979 ProfileData* data_at(int data_index) const; 1980 1981 // Walk through the data in order. 1982 ProfileData* first_data() const { return data_at(first_di()); } 1983 ProfileData* next_data(ProfileData* current) const; 1984 bool is_valid(ProfileData* current) const { return current != NULL; } 1985 1986 // Convert a dp (data pointer) to a di (data index). 1987 int dp_to_di(address dp) const { 1988 return dp - ((address)_data); 1989 } 1990 1991 address di_to_dp(int di) { 1992 return (address)data_layout_at(di); 1993 } 1994 1995 // bci to di/dp conversion. 1996 address bci_to_dp(int bci); 1997 int bci_to_di(int bci) { 1998 return dp_to_di(bci_to_dp(bci)); 1999 } 2000 2001 // Get the data at an arbitrary bci, or NULL if there is none. 2002 ProfileData* bci_to_data(int bci); 2003 2004 // Same, but try to create an extra_data record if one is needed: 2005 ProfileData* allocate_bci_to_data(int bci) { 2006 ProfileData* data = bci_to_data(bci); 2007 return (data != NULL) ? data : bci_to_extra_data(bci, true); 2008 } 2009 2010 // Add a handful of extra data records, for trap tracking. 2011 DataLayout* extra_data_base() const { return limit_data_position(); } 2012 DataLayout* extra_data_limit() const { return (DataLayout*)((address)this + size_in_bytes()); } 2013 int extra_data_size() const { return (address)extra_data_limit() 2014 - (address)extra_data_base(); } 2015 static DataLayout* next_extra(DataLayout* dp) { return (DataLayout*)((address)dp + in_bytes(DataLayout::cell_offset(0))); } 2016 2017 // Return (uint)-1 for overflow. 2018 uint trap_count(int reason) const { 2019 assert((uint)reason < _trap_hist_limit, "oob"); 2020 return (int)((_trap_hist._array[reason]+1) & _trap_hist_mask) - 1; 2021 } 2022 // For loops: 2023 static uint trap_reason_limit() { return _trap_hist_limit; } 2024 static uint trap_count_limit() { return _trap_hist_mask; } 2025 uint inc_trap_count(int reason) { 2026 // Count another trap, anywhere in this method. 2027 assert(reason >= 0, "must be single trap"); 2028 if ((uint)reason < _trap_hist_limit) { 2029 uint cnt1 = 1 + _trap_hist._array[reason]; 2030 if ((cnt1 & _trap_hist_mask) != 0) { // if no counter overflow... 2031 _trap_hist._array[reason] = cnt1; 2032 return cnt1; 2033 } else { 2034 return _trap_hist_mask + (++_nof_overflow_traps); 2035 } 2036 } else { 2037 // Could not represent the count in the histogram. 2038 return (++_nof_overflow_traps); 2039 } 2040 } 2041 2042 uint overflow_trap_count() const { 2043 return _nof_overflow_traps; 2044 } 2045 uint overflow_recompile_count() const { 2046 return _nof_overflow_recompiles; 2047 } 2048 void inc_overflow_recompile_count() { 2049 _nof_overflow_recompiles += 1; 2050 } 2051 uint decompile_count() const { 2052 return _nof_decompiles; 2053 } 2054 void inc_decompile_count() { 2055 _nof_decompiles += 1; 2056 if (decompile_count() > (uint)PerMethodRecompilationCutoff) { 2057 method()->set_not_compilable(CompLevel_full_optimization, true, "decompile_count > PerMethodRecompilationCutoff"); 2058 } 2059 } 2060 2061 // Support for code generation 2062 static ByteSize data_offset() { 2063 return byte_offset_of(MethodData, _data[0]); 2064 } 2065 2066 static ByteSize invocation_counter_offset() { 2067 return byte_offset_of(MethodData, _invocation_counter); 2068 } 2069 static ByteSize backedge_counter_offset() { 2070 return byte_offset_of(MethodData, _backedge_counter); 2071 } 2072 2073 // Deallocation support - no pointer fields to deallocate 2074 void deallocate_contents(ClassLoaderData* loader_data) {} 2075 2076 // GC support 2077 void set_size(int object_size_in_bytes) { _size = object_size_in_bytes; } 2078 2079 // Printing 2080 #ifndef PRODUCT 2081 void print_on (outputStream* st) const; 2082 #endif 2083 void print_value_on(outputStream* st) const; 2084 2085 #ifndef PRODUCT 2086 // printing support for method data 2087 void print_data_on(outputStream* st) const; 2088 #endif 2089 2090 const char* internal_name() const { return "{method data}"; } 2091 2092 // verification 2093 void verify_on(outputStream* st); 2094 void verify_data_on(outputStream* st); 2095 2096 static bool profile_arguments(); 2097 static bool profile_return(); 2098 }; 2099 2100 #endif // SHARE_VM_OOPS_METHODDATAOOP_HPP