1 /*
   2  * Copyright (c) 2000, 2010, 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 "incls/_precompiled.incl"
  26 # include "incls/_methodDataOop.cpp.incl"
  27 
  28 // ==================================================================
  29 // DataLayout
  30 //
  31 // Overlay for generic profiling data.
  32 
  33 // Some types of data layouts need a length field.
  34 bool DataLayout::needs_array_len(u1 tag) {
  35   return (tag == multi_branch_data_tag) || (tag == arg_info_data_tag);
  36 }
  37 
  38 // Perform generic initialization of the data.  More specific
  39 // initialization occurs in overrides of ProfileData::post_initialize.
  40 void DataLayout::initialize(u1 tag, u2 bci, int cell_count) {
  41   _header._bits = (intptr_t)0;
  42   _header._struct._tag = tag;
  43   _header._struct._bci = bci;
  44   for (int i = 0; i < cell_count; i++) {
  45     set_cell_at(i, (intptr_t)0);
  46   }
  47   if (needs_array_len(tag)) {
  48     set_cell_at(ArrayData::array_len_off_set, cell_count - 1); // -1 for header.
  49   }
  50 }
  51 
  52 void DataLayout::follow_weak_refs(BoolObjectClosure* cl) {
  53   ResourceMark m;
  54   data_in()->follow_weak_refs(cl);
  55 }
  56 
  57 
  58 // ==================================================================
  59 // ProfileData
  60 //
  61 // A ProfileData object is created to refer to a section of profiling
  62 // data in a structured way.
  63 
  64 // Constructor for invalid ProfileData.
  65 ProfileData::ProfileData() {
  66   _data = NULL;
  67 }
  68 
  69 #ifndef PRODUCT
  70 void ProfileData::print_shared(outputStream* st, const char* name) {
  71   st->print("bci: %d", bci());
  72   st->fill_to(tab_width_one);
  73   st->print("%s", name);
  74   tab(st);
  75   int trap = trap_state();
  76   if (trap != 0) {
  77     char buf[100];
  78     st->print("trap(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap));
  79   }
  80   int flags = data()->flags();
  81   if (flags != 0)
  82     st->print("flags(%d) ", flags);
  83 }
  84 
  85 void ProfileData::tab(outputStream* st) {
  86   st->fill_to(tab_width_two);
  87 }
  88 #endif // !PRODUCT
  89 
  90 // ==================================================================
  91 // BitData
  92 //
  93 // A BitData corresponds to a one-bit flag.  This is used to indicate
  94 // whether a checkcast bytecode has seen a null value.
  95 
  96 
  97 #ifndef PRODUCT
  98 void BitData::print_data_on(outputStream* st) {
  99   print_shared(st, "BitData");
 100 }
 101 #endif // !PRODUCT
 102 
 103 // ==================================================================
 104 // CounterData
 105 //
 106 // A CounterData corresponds to a simple counter.
 107 
 108 #ifndef PRODUCT
 109 void CounterData::print_data_on(outputStream* st) {
 110   print_shared(st, "CounterData");
 111   st->print_cr("count(%u)", count());
 112 }
 113 #endif // !PRODUCT
 114 
 115 // ==================================================================
 116 // JumpData
 117 //
 118 // A JumpData is used to access profiling information for a direct
 119 // branch.  It is a counter, used for counting the number of branches,
 120 // plus a data displacement, used for realigning the data pointer to
 121 // the corresponding target bci.
 122 
 123 void JumpData::post_initialize(BytecodeStream* stream, methodDataOop mdo) {
 124   assert(stream->bci() == bci(), "wrong pos");
 125   int target;
 126   Bytecodes::Code c = stream->code();
 127   if (c == Bytecodes::_goto_w || c == Bytecodes::_jsr_w) {
 128     target = stream->dest_w();
 129   } else {
 130     target = stream->dest();
 131   }
 132   int my_di = mdo->dp_to_di(dp());
 133   int target_di = mdo->bci_to_di(target);
 134   int offset = target_di - my_di;
 135   set_displacement(offset);
 136 }
 137 
 138 #ifndef PRODUCT
 139 void JumpData::print_data_on(outputStream* st) {
 140   print_shared(st, "JumpData");
 141   st->print_cr("taken(%u) displacement(%d)", taken(), displacement());
 142 }
 143 #endif // !PRODUCT
 144 
 145 // ==================================================================
 146 // ReceiverTypeData
 147 //
 148 // A ReceiverTypeData is used to access profiling information about a
 149 // dynamic type check.  It consists of a counter which counts the total times
 150 // that the check is reached, and a series of (klassOop, count) pairs
 151 // which are used to store a type profile for the receiver of the check.
 152 
 153 void ReceiverTypeData::follow_contents() {
 154   // This is a set of weak references that need
 155   // to be followed at the end of the strong marking
 156   // phase. Memoize this object so it can be visited
 157   // in the weak roots processing phase.
 158   MarkSweep::revisit_mdo(data());
 159 }
 160 
 161 #ifndef SERIALGC
 162 void ReceiverTypeData::follow_contents(ParCompactionManager* cm) {
 163   // This is a set of weak references that need
 164   // to be followed at the end of the strong marking
 165   // phase. Memoize this object so it can be visited
 166   // in the weak roots processing phase.
 167   PSParallelCompact::revisit_mdo(cm, data());
 168 }
 169 #endif // SERIALGC
 170 
 171 void ReceiverTypeData::oop_iterate(OopClosure* blk) {
 172   if (blk->should_remember_mdo()) {
 173     // This is a set of weak references that need
 174     // to be followed at the end of the strong marking
 175     // phase. Memoize this object so it can be visited
 176     // in the weak roots processing phase.
 177     blk->remember_mdo(data());
 178   } else { // normal scan
 179     for (uint row = 0; row < row_limit(); row++) {
 180       if (receiver(row) != NULL) {
 181         oop* adr = adr_receiver(row);
 182         blk->do_oop(adr);
 183       }
 184     }
 185   }
 186 }
 187 
 188 void ReceiverTypeData::oop_iterate_m(OopClosure* blk, MemRegion mr) {
 189   // Currently, this interface is called only during card-scanning for
 190   // a young gen gc, in which case this object cannot contribute anything,
 191   // since it does not contain any references that cross out of
 192   // the perm gen. However, for future more general use we allow
 193   // the possibility of calling for instance from more general
 194   // iterators (for example, a future regionalized perm gen for G1,
 195   // or the possibility of moving some references out of perm in
 196   // the case of other collectors). In that case, you will need
 197   // to relax or remove some of the assertions below.
 198 #ifdef ASSERT
 199   // Verify that none of the embedded oop references cross out of
 200   // this generation.
 201   for (uint row = 0; row < row_limit(); row++) {
 202     if (receiver(row) != NULL) {
 203       oop* adr = adr_receiver(row);
 204       CollectedHeap* h = Universe::heap();
 205       assert(h->is_permanent(adr) && h->is_permanent_or_null(*adr), "Not intra-perm");
 206     }
 207   }
 208 #endif // ASSERT
 209   assert(!blk->should_remember_mdo(), "Not expected to remember MDO");
 210   return;   // Nothing to do, see comment above
 211 #if 0
 212   if (blk->should_remember_mdo()) {
 213     // This is a set of weak references that need
 214     // to be followed at the end of the strong marking
 215     // phase. Memoize this object so it can be visited
 216     // in the weak roots processing phase.
 217     blk->remember_mdo(data());
 218   } else { // normal scan
 219     for (uint row = 0; row < row_limit(); row++) {
 220       if (receiver(row) != NULL) {
 221         oop* adr = adr_receiver(row);
 222         if (mr.contains(adr)) {
 223           blk->do_oop(adr);
 224         } else if ((HeapWord*)adr >= mr.end()) {
 225           // Test that the current cursor and the two ends of the range
 226           // that we may have skipped iterating over are monotonically ordered;
 227           // this is just a paranoid assertion, just in case represetations
 228           // should change in the future rendering the short-circuit return
 229           // here invalid.
 230           assert((row+1 >= row_limit() || adr_receiver(row+1) > adr) &&
 231                  (row+2 >= row_limit() || adr_receiver(row_limit()-1) > adr_receiver(row+1)), "Reducing?");
 232           break; // remaining should be outside this mr too
 233         }
 234       }
 235     }
 236   }
 237 #endif
 238 }
 239 
 240 void ReceiverTypeData::adjust_pointers() {
 241   for (uint row = 0; row < row_limit(); row++) {
 242     if (receiver(row) != NULL) {
 243       MarkSweep::adjust_pointer(adr_receiver(row));
 244     }
 245   }
 246 }
 247 
 248 void ReceiverTypeData::follow_weak_refs(BoolObjectClosure* is_alive_cl) {
 249   for (uint row = 0; row < row_limit(); row++) {
 250     klassOop p = receiver(row);
 251     if (p != NULL && !is_alive_cl->do_object_b(p)) {
 252       clear_row(row);
 253     }
 254   }
 255 }
 256 
 257 #ifndef SERIALGC
 258 void ReceiverTypeData::update_pointers() {
 259   for (uint row = 0; row < row_limit(); row++) {
 260     if (receiver_unchecked(row) != NULL) {
 261       PSParallelCompact::adjust_pointer(adr_receiver(row));
 262     }
 263   }
 264 }
 265 
 266 void ReceiverTypeData::update_pointers(HeapWord* beg_addr, HeapWord* end_addr) {
 267   // The loop bounds could be computed based on beg_addr/end_addr and the
 268   // boundary test hoisted outside the loop (see klassVTable for an example);
 269   // however, row_limit() is small enough (2) to make that less efficient.
 270   for (uint row = 0; row < row_limit(); row++) {
 271     if (receiver_unchecked(row) != NULL) {
 272       PSParallelCompact::adjust_pointer(adr_receiver(row), beg_addr, end_addr);
 273     }
 274   }
 275 }
 276 #endif // SERIALGC
 277 
 278 #ifndef PRODUCT
 279 void ReceiverTypeData::print_receiver_data_on(outputStream* st) {
 280   uint row;
 281   int entries = 0;
 282   for (row = 0; row < row_limit(); row++) {
 283     if (receiver(row) != NULL)  entries++;
 284   }
 285   st->print_cr("count(%u) entries(%u)", count(), entries);
 286   int total = count();
 287   for (row = 0; row < row_limit(); row++) {
 288     if (receiver(row) != NULL) {
 289       total += receiver_count(row);
 290     }
 291   }
 292   for (row = 0; row < row_limit(); row++) {
 293     if (receiver(row) != NULL) {
 294       tab(st);
 295       receiver(row)->print_value_on(st);
 296       st->print_cr("(%u %4.2f)", receiver_count(row), (float) receiver_count(row) / (float) total);
 297     }
 298   }
 299 }
 300 void ReceiverTypeData::print_data_on(outputStream* st) {
 301   print_shared(st, "ReceiverTypeData");
 302   print_receiver_data_on(st);
 303 }
 304 void VirtualCallData::print_data_on(outputStream* st) {
 305   print_shared(st, "VirtualCallData");
 306   print_receiver_data_on(st);
 307 }
 308 #endif // !PRODUCT
 309 
 310 // ==================================================================
 311 // RetData
 312 //
 313 // A RetData is used to access profiling information for a ret bytecode.
 314 // It is composed of a count of the number of times that the ret has
 315 // been executed, followed by a series of triples of the form
 316 // (bci, count, di) which count the number of times that some bci was the
 317 // target of the ret and cache a corresponding displacement.
 318 
 319 void RetData::post_initialize(BytecodeStream* stream, methodDataOop mdo) {
 320   for (uint row = 0; row < row_limit(); row++) {
 321     set_bci_displacement(row, -1);
 322     set_bci(row, no_bci);
 323   }
 324   // release so other threads see a consistent state.  bci is used as
 325   // a valid flag for bci_displacement.
 326   OrderAccess::release();
 327 }
 328 
 329 // This routine needs to atomically update the RetData structure, so the
 330 // caller needs to hold the RetData_lock before it gets here.  Since taking
 331 // the lock can block (and allow GC) and since RetData is a ProfileData is a
 332 // wrapper around a derived oop, taking the lock in _this_ method will
 333 // basically cause the 'this' pointer's _data field to contain junk after the
 334 // lock.  We require the caller to take the lock before making the ProfileData
 335 // structure.  Currently the only caller is InterpreterRuntime::update_mdp_for_ret
 336 address RetData::fixup_ret(int return_bci, methodDataHandle h_mdo) {
 337   // First find the mdp which corresponds to the return bci.
 338   address mdp = h_mdo->bci_to_dp(return_bci);
 339 
 340   // Now check to see if any of the cache slots are open.
 341   for (uint row = 0; row < row_limit(); row++) {
 342     if (bci(row) == no_bci) {
 343       set_bci_displacement(row, mdp - dp());
 344       set_bci_count(row, DataLayout::counter_increment);
 345       // Barrier to ensure displacement is written before the bci; allows
 346       // the interpreter to read displacement without fear of race condition.
 347       release_set_bci(row, return_bci);
 348       break;
 349     }
 350   }
 351   return mdp;
 352 }
 353 
 354 
 355 #ifndef PRODUCT
 356 void RetData::print_data_on(outputStream* st) {
 357   print_shared(st, "RetData");
 358   uint row;
 359   int entries = 0;
 360   for (row = 0; row < row_limit(); row++) {
 361     if (bci(row) != no_bci)  entries++;
 362   }
 363   st->print_cr("count(%u) entries(%u)", count(), entries);
 364   for (row = 0; row < row_limit(); row++) {
 365     if (bci(row) != no_bci) {
 366       tab(st);
 367       st->print_cr("bci(%d: count(%u) displacement(%d))",
 368                    bci(row), bci_count(row), bci_displacement(row));
 369     }
 370   }
 371 }
 372 #endif // !PRODUCT
 373 
 374 // ==================================================================
 375 // BranchData
 376 //
 377 // A BranchData is used to access profiling data for a two-way branch.
 378 // It consists of taken and not_taken counts as well as a data displacement
 379 // for the taken case.
 380 
 381 void BranchData::post_initialize(BytecodeStream* stream, methodDataOop mdo) {
 382   assert(stream->bci() == bci(), "wrong pos");
 383   int target = stream->dest();
 384   int my_di = mdo->dp_to_di(dp());
 385   int target_di = mdo->bci_to_di(target);
 386   int offset = target_di - my_di;
 387   set_displacement(offset);
 388 }
 389 
 390 #ifndef PRODUCT
 391 void BranchData::print_data_on(outputStream* st) {
 392   print_shared(st, "BranchData");
 393   st->print_cr("taken(%u) displacement(%d)",
 394                taken(), displacement());
 395   tab(st);
 396   st->print_cr("not taken(%u)", not_taken());
 397 }
 398 #endif
 399 
 400 // ==================================================================
 401 // MultiBranchData
 402 //
 403 // A MultiBranchData is used to access profiling information for
 404 // a multi-way branch (*switch bytecodes).  It consists of a series
 405 // of (count, displacement) pairs, which count the number of times each
 406 // case was taken and specify the data displacment for each branch target.
 407 
 408 int MultiBranchData::compute_cell_count(BytecodeStream* stream) {
 409   int cell_count = 0;
 410   if (stream->code() == Bytecodes::_tableswitch) {
 411     Bytecode_tableswitch* sw = Bytecode_tableswitch_at(stream->bcp());
 412     cell_count = 1 + per_case_cell_count * (1 + sw->length()); // 1 for default
 413   } else {
 414     Bytecode_lookupswitch* sw = Bytecode_lookupswitch_at(stream->bcp());
 415     cell_count = 1 + per_case_cell_count * (sw->number_of_pairs() + 1); // 1 for default
 416   }
 417   return cell_count;
 418 }
 419 
 420 void MultiBranchData::post_initialize(BytecodeStream* stream,
 421                                       methodDataOop mdo) {
 422   assert(stream->bci() == bci(), "wrong pos");
 423   int target;
 424   int my_di;
 425   int target_di;
 426   int offset;
 427   if (stream->code() == Bytecodes::_tableswitch) {
 428     Bytecode_tableswitch* sw = Bytecode_tableswitch_at(stream->bcp());
 429     int len = sw->length();
 430     assert(array_len() == per_case_cell_count * (len + 1), "wrong len");
 431     for (int count = 0; count < len; count++) {
 432       target = sw->dest_offset_at(count) + bci();
 433       my_di = mdo->dp_to_di(dp());
 434       target_di = mdo->bci_to_di(target);
 435       offset = target_di - my_di;
 436       set_displacement_at(count, offset);
 437     }
 438     target = sw->default_offset() + bci();
 439     my_di = mdo->dp_to_di(dp());
 440     target_di = mdo->bci_to_di(target);
 441     offset = target_di - my_di;
 442     set_default_displacement(offset);
 443 
 444   } else {
 445     Bytecode_lookupswitch* sw = Bytecode_lookupswitch_at(stream->bcp());
 446     int npairs = sw->number_of_pairs();
 447     assert(array_len() == per_case_cell_count * (npairs + 1), "wrong len");
 448     for (int count = 0; count < npairs; count++) {
 449       LookupswitchPair *pair = sw->pair_at(count);
 450       target = pair->offset() + bci();
 451       my_di = mdo->dp_to_di(dp());
 452       target_di = mdo->bci_to_di(target);
 453       offset = target_di - my_di;
 454       set_displacement_at(count, offset);
 455     }
 456     target = sw->default_offset() + bci();
 457     my_di = mdo->dp_to_di(dp());
 458     target_di = mdo->bci_to_di(target);
 459     offset = target_di - my_di;
 460     set_default_displacement(offset);
 461   }
 462 }
 463 
 464 #ifndef PRODUCT
 465 void MultiBranchData::print_data_on(outputStream* st) {
 466   print_shared(st, "MultiBranchData");
 467   st->print_cr("default_count(%u) displacement(%d)",
 468                default_count(), default_displacement());
 469   int cases = number_of_cases();
 470   for (int i = 0; i < cases; i++) {
 471     tab(st);
 472     st->print_cr("count(%u) displacement(%d)",
 473                  count_at(i), displacement_at(i));
 474   }
 475 }
 476 #endif
 477 
 478 #ifndef PRODUCT
 479 void ArgInfoData::print_data_on(outputStream* st) {
 480   print_shared(st, "ArgInfoData");
 481   int nargs = number_of_args();
 482   for (int i = 0; i < nargs; i++) {
 483     st->print("  0x%x", arg_modified(i));
 484   }
 485   st->cr();
 486 }
 487 
 488 #endif
 489 // ==================================================================
 490 // methodDataOop
 491 //
 492 // A methodDataOop holds information which has been collected about
 493 // a method.
 494 
 495 int methodDataOopDesc::bytecode_cell_count(Bytecodes::Code code) {
 496   switch (code) {
 497   case Bytecodes::_checkcast:
 498   case Bytecodes::_instanceof:
 499   case Bytecodes::_aastore:
 500     if (TypeProfileCasts) {
 501       return ReceiverTypeData::static_cell_count();
 502     } else {
 503       return BitData::static_cell_count();
 504     }
 505   case Bytecodes::_invokespecial:
 506   case Bytecodes::_invokestatic:
 507     return CounterData::static_cell_count();
 508   case Bytecodes::_goto:
 509   case Bytecodes::_goto_w:
 510   case Bytecodes::_jsr:
 511   case Bytecodes::_jsr_w:
 512     return JumpData::static_cell_count();
 513   case Bytecodes::_invokevirtual:
 514   case Bytecodes::_invokeinterface:
 515     return VirtualCallData::static_cell_count();
 516   case Bytecodes::_invokedynamic:
 517     return CounterData::static_cell_count();
 518   case Bytecodes::_ret:
 519     return RetData::static_cell_count();
 520   case Bytecodes::_ifeq:
 521   case Bytecodes::_ifne:
 522   case Bytecodes::_iflt:
 523   case Bytecodes::_ifge:
 524   case Bytecodes::_ifgt:
 525   case Bytecodes::_ifle:
 526   case Bytecodes::_if_icmpeq:
 527   case Bytecodes::_if_icmpne:
 528   case Bytecodes::_if_icmplt:
 529   case Bytecodes::_if_icmpge:
 530   case Bytecodes::_if_icmpgt:
 531   case Bytecodes::_if_icmple:
 532   case Bytecodes::_if_acmpeq:
 533   case Bytecodes::_if_acmpne:
 534   case Bytecodes::_ifnull:
 535   case Bytecodes::_ifnonnull:
 536     return BranchData::static_cell_count();
 537   case Bytecodes::_lookupswitch:
 538   case Bytecodes::_tableswitch:
 539     return variable_cell_count;
 540   }
 541   return no_profile_data;
 542 }
 543 
 544 // Compute the size of the profiling information corresponding to
 545 // the current bytecode.
 546 int methodDataOopDesc::compute_data_size(BytecodeStream* stream) {
 547   int cell_count = bytecode_cell_count(stream->code());
 548   if (cell_count == no_profile_data) {
 549     return 0;
 550   }
 551   if (cell_count == variable_cell_count) {
 552     cell_count = MultiBranchData::compute_cell_count(stream);
 553   }
 554   // Note:  cell_count might be zero, meaning that there is just
 555   //        a DataLayout header, with no extra cells.
 556   assert(cell_count >= 0, "sanity");
 557   return DataLayout::compute_size_in_bytes(cell_count);
 558 }
 559 
 560 int methodDataOopDesc::compute_extra_data_count(int data_size, int empty_bc_count) {
 561   if (ProfileTraps) {
 562     // Assume that up to 3% of BCIs with no MDP will need to allocate one.
 563     int extra_data_count = (uint)(empty_bc_count * 3) / 128 + 1;
 564     // If the method is large, let the extra BCIs grow numerous (to ~1%).
 565     int one_percent_of_data
 566       = (uint)data_size / (DataLayout::header_size_in_bytes()*128);
 567     if (extra_data_count < one_percent_of_data)
 568       extra_data_count = one_percent_of_data;
 569     if (extra_data_count > empty_bc_count)
 570       extra_data_count = empty_bc_count;  // no need for more
 571     return extra_data_count;
 572   } else {
 573     return 0;
 574   }
 575 }
 576 
 577 // Compute the size of the methodDataOop necessary to store
 578 // profiling information about a given method.  Size is in bytes.
 579 int methodDataOopDesc::compute_allocation_size_in_bytes(methodHandle method) {
 580   int data_size = 0;
 581   BytecodeStream stream(method);
 582   Bytecodes::Code c;
 583   int empty_bc_count = 0;  // number of bytecodes lacking data
 584   while ((c = stream.next()) >= 0) {
 585     int size_in_bytes = compute_data_size(&stream);
 586     data_size += size_in_bytes;
 587     if (size_in_bytes == 0)  empty_bc_count += 1;
 588   }
 589   int object_size = in_bytes(data_offset()) + data_size;
 590 
 591   // Add some extra DataLayout cells (at least one) to track stray traps.
 592   int extra_data_count = compute_extra_data_count(data_size, empty_bc_count);
 593   object_size += extra_data_count * DataLayout::compute_size_in_bytes(0);
 594 
 595   // Add a cell to record information about modified arguments.
 596   int arg_size = method->size_of_parameters();
 597   object_size += DataLayout::compute_size_in_bytes(arg_size+1);
 598   return object_size;
 599 }
 600 
 601 // Compute the size of the methodDataOop necessary to store
 602 // profiling information about a given method.  Size is in words
 603 int methodDataOopDesc::compute_allocation_size_in_words(methodHandle method) {
 604   int byte_size = compute_allocation_size_in_bytes(method);
 605   int word_size = align_size_up(byte_size, BytesPerWord) / BytesPerWord;
 606   return align_object_size(word_size);
 607 }
 608 
 609 // Initialize an individual data segment.  Returns the size of
 610 // the segment in bytes.
 611 int methodDataOopDesc::initialize_data(BytecodeStream* stream,
 612                                        int data_index) {
 613   int cell_count = -1;
 614   int tag = DataLayout::no_tag;
 615   DataLayout* data_layout = data_layout_at(data_index);
 616   Bytecodes::Code c = stream->code();
 617   switch (c) {
 618   case Bytecodes::_checkcast:
 619   case Bytecodes::_instanceof:
 620   case Bytecodes::_aastore:
 621     if (TypeProfileCasts) {
 622       cell_count = ReceiverTypeData::static_cell_count();
 623       tag = DataLayout::receiver_type_data_tag;
 624     } else {
 625       cell_count = BitData::static_cell_count();
 626       tag = DataLayout::bit_data_tag;
 627     }
 628     break;
 629   case Bytecodes::_invokespecial:
 630   case Bytecodes::_invokestatic:
 631     cell_count = CounterData::static_cell_count();
 632     tag = DataLayout::counter_data_tag;
 633     break;
 634   case Bytecodes::_goto:
 635   case Bytecodes::_goto_w:
 636   case Bytecodes::_jsr:
 637   case Bytecodes::_jsr_w:
 638     cell_count = JumpData::static_cell_count();
 639     tag = DataLayout::jump_data_tag;
 640     break;
 641   case Bytecodes::_invokevirtual:
 642   case Bytecodes::_invokeinterface:
 643     cell_count = VirtualCallData::static_cell_count();
 644     tag = DataLayout::virtual_call_data_tag;
 645     break;
 646   case Bytecodes::_invokedynamic:
 647     // %%% should make a type profile for any invokedynamic that takes a ref argument
 648     cell_count = CounterData::static_cell_count();
 649     tag = DataLayout::counter_data_tag;
 650     break;
 651   case Bytecodes::_ret:
 652     cell_count = RetData::static_cell_count();
 653     tag = DataLayout::ret_data_tag;
 654     break;
 655   case Bytecodes::_ifeq:
 656   case Bytecodes::_ifne:
 657   case Bytecodes::_iflt:
 658   case Bytecodes::_ifge:
 659   case Bytecodes::_ifgt:
 660   case Bytecodes::_ifle:
 661   case Bytecodes::_if_icmpeq:
 662   case Bytecodes::_if_icmpne:
 663   case Bytecodes::_if_icmplt:
 664   case Bytecodes::_if_icmpge:
 665   case Bytecodes::_if_icmpgt:
 666   case Bytecodes::_if_icmple:
 667   case Bytecodes::_if_acmpeq:
 668   case Bytecodes::_if_acmpne:
 669   case Bytecodes::_ifnull:
 670   case Bytecodes::_ifnonnull:
 671     cell_count = BranchData::static_cell_count();
 672     tag = DataLayout::branch_data_tag;
 673     break;
 674   case Bytecodes::_lookupswitch:
 675   case Bytecodes::_tableswitch:
 676     cell_count = MultiBranchData::compute_cell_count(stream);
 677     tag = DataLayout::multi_branch_data_tag;
 678     break;
 679   }
 680   assert(tag == DataLayout::multi_branch_data_tag ||
 681          cell_count == bytecode_cell_count(c), "cell counts must agree");
 682   if (cell_count >= 0) {
 683     assert(tag != DataLayout::no_tag, "bad tag");
 684     assert(bytecode_has_profile(c), "agree w/ BHP");
 685     data_layout->initialize(tag, stream->bci(), cell_count);
 686     return DataLayout::compute_size_in_bytes(cell_count);
 687   } else {
 688     assert(!bytecode_has_profile(c), "agree w/ !BHP");
 689     return 0;
 690   }
 691 }
 692 
 693 // Get the data at an arbitrary (sort of) data index.
 694 ProfileData* methodDataOopDesc::data_at(int data_index) {
 695   if (out_of_bounds(data_index)) {
 696     return NULL;
 697   }
 698   DataLayout* data_layout = data_layout_at(data_index);
 699   return data_layout->data_in();
 700 }
 701 
 702 ProfileData* DataLayout::data_in() {
 703   switch (tag()) {
 704   case DataLayout::no_tag:
 705   default:
 706     ShouldNotReachHere();
 707     return NULL;
 708   case DataLayout::bit_data_tag:
 709     return new BitData(this);
 710   case DataLayout::counter_data_tag:
 711     return new CounterData(this);
 712   case DataLayout::jump_data_tag:
 713     return new JumpData(this);
 714   case DataLayout::receiver_type_data_tag:
 715     return new ReceiverTypeData(this);
 716   case DataLayout::virtual_call_data_tag:
 717     return new VirtualCallData(this);
 718   case DataLayout::ret_data_tag:
 719     return new RetData(this);
 720   case DataLayout::branch_data_tag:
 721     return new BranchData(this);
 722   case DataLayout::multi_branch_data_tag:
 723     return new MultiBranchData(this);
 724   case DataLayout::arg_info_data_tag:
 725     return new ArgInfoData(this);
 726   };
 727 }
 728 
 729 // Iteration over data.
 730 ProfileData* methodDataOopDesc::next_data(ProfileData* current) {
 731   int current_index = dp_to_di(current->dp());
 732   int next_index = current_index + current->size_in_bytes();
 733   ProfileData* next = data_at(next_index);
 734   return next;
 735 }
 736 
 737 // Give each of the data entries a chance to perform specific
 738 // data initialization.
 739 void methodDataOopDesc::post_initialize(BytecodeStream* stream) {
 740   ResourceMark rm;
 741   ProfileData* data;
 742   for (data = first_data(); is_valid(data); data = next_data(data)) {
 743     stream->set_start(data->bci());
 744     stream->next();
 745     data->post_initialize(stream, this);
 746   }
 747 }
 748 
 749 // Initialize the methodDataOop corresponding to a given method.
 750 void methodDataOopDesc::initialize(methodHandle method) {
 751   ResourceMark rm;
 752   // Set the method back-pointer.
 753   _method = method();
 754 
 755   if (TieredCompilation) {
 756     _invocation_counter.init();
 757     _backedge_counter.init();
 758     _num_loops = 0;
 759     _num_blocks = 0;
 760     _highest_comp_level = 0;
 761     _highest_osr_comp_level = 0;
 762     _would_profile = false;
 763   }
 764   set_creation_mileage(mileage_of(method()));
 765 
 766   // Initialize flags and trap history.
 767   _nof_decompiles = 0;
 768   _nof_overflow_recompiles = 0;
 769   _nof_overflow_traps = 0;
 770   assert(sizeof(_trap_hist) % sizeof(HeapWord) == 0, "align");
 771   Copy::zero_to_words((HeapWord*) &_trap_hist,
 772                       sizeof(_trap_hist) / sizeof(HeapWord));
 773 
 774   // Go through the bytecodes and allocate and initialize the
 775   // corresponding data cells.
 776   int data_size = 0;
 777   int empty_bc_count = 0;  // number of bytecodes lacking data
 778   BytecodeStream stream(method);
 779   Bytecodes::Code c;
 780   while ((c = stream.next()) >= 0) {
 781     int size_in_bytes = initialize_data(&stream, data_size);
 782     data_size += size_in_bytes;
 783     if (size_in_bytes == 0)  empty_bc_count += 1;
 784   }
 785   _data_size = data_size;
 786   int object_size = in_bytes(data_offset()) + data_size;
 787 
 788   // Add some extra DataLayout cells (at least one) to track stray traps.
 789   int extra_data_count = compute_extra_data_count(data_size, empty_bc_count);
 790   int extra_size = extra_data_count * DataLayout::compute_size_in_bytes(0);
 791 
 792   // Add a cell to record information about modified arguments.
 793   // Set up _args_modified array after traps cells so that
 794   // the code for traps cells works.
 795   DataLayout *dp = data_layout_at(data_size + extra_size);
 796 
 797   int arg_size = method->size_of_parameters();
 798   dp->initialize(DataLayout::arg_info_data_tag, 0, arg_size+1);
 799 
 800   object_size += extra_size + DataLayout::compute_size_in_bytes(arg_size+1);
 801 
 802   // Set an initial hint. Don't use set_hint_di() because
 803   // first_di() may be out of bounds if data_size is 0.
 804   // In that situation, _hint_di is never used, but at
 805   // least well-defined.
 806   _hint_di = first_di();
 807 
 808   post_initialize(&stream);
 809 
 810   set_object_is_parsable(object_size);
 811 }
 812 
 813 // Get a measure of how much mileage the method has on it.
 814 int methodDataOopDesc::mileage_of(methodOop method) {
 815   int mileage = 0;
 816   if (TieredCompilation) {
 817     mileage = MAX2(method->invocation_count(), method->backedge_count());
 818   } else {
 819     int iic = method->interpreter_invocation_count();
 820     if (mileage < iic)  mileage = iic;
 821     InvocationCounter* ic = method->invocation_counter();
 822     InvocationCounter* bc = method->backedge_counter();
 823     int icval = ic->count();
 824     if (ic->carry()) icval += CompileThreshold;
 825     if (mileage < icval)  mileage = icval;
 826     int bcval = bc->count();
 827     if (bc->carry()) bcval += CompileThreshold;
 828     if (mileage < bcval)  mileage = bcval;
 829   }
 830   return mileage;
 831 }
 832 
 833 bool methodDataOopDesc::is_mature() const {
 834   return CompilationPolicy::policy()->is_mature(_method);
 835 }
 836 
 837 // Translate a bci to its corresponding data index (di).
 838 address methodDataOopDesc::bci_to_dp(int bci) {
 839   ResourceMark rm;
 840   ProfileData* data = data_before(bci);
 841   ProfileData* prev = NULL;
 842   for ( ; is_valid(data); data = next_data(data)) {
 843     if (data->bci() >= bci) {
 844       if (data->bci() == bci)  set_hint_di(dp_to_di(data->dp()));
 845       else if (prev != NULL)   set_hint_di(dp_to_di(prev->dp()));
 846       return data->dp();
 847     }
 848     prev = data;
 849   }
 850   return (address)limit_data_position();
 851 }
 852 
 853 // Translate a bci to its corresponding data, or NULL.
 854 ProfileData* methodDataOopDesc::bci_to_data(int bci) {
 855   ProfileData* data = data_before(bci);
 856   for ( ; is_valid(data); data = next_data(data)) {
 857     if (data->bci() == bci) {
 858       set_hint_di(dp_to_di(data->dp()));
 859       return data;
 860     } else if (data->bci() > bci) {
 861       break;
 862     }
 863   }
 864   return bci_to_extra_data(bci, false);
 865 }
 866 
 867 // Translate a bci to its corresponding extra data, or NULL.
 868 ProfileData* methodDataOopDesc::bci_to_extra_data(int bci, bool create_if_missing) {
 869   DataLayout* dp    = extra_data_base();
 870   DataLayout* end   = extra_data_limit();
 871   DataLayout* avail = NULL;
 872   for (; dp < end; dp = next_extra(dp)) {
 873     // No need for "OrderAccess::load_acquire" ops,
 874     // since the data structure is monotonic.
 875     if (dp->tag() == DataLayout::no_tag)  break;
 876     if (dp->tag() == DataLayout::arg_info_data_tag) {
 877       dp = end; // ArgInfoData is at the end of extra data section.
 878       break;
 879     }
 880     if (dp->bci() == bci) {
 881       assert(dp->tag() == DataLayout::bit_data_tag, "sane");
 882       return new BitData(dp);
 883     }
 884   }
 885   if (create_if_missing && dp < end) {
 886     // Allocate this one.  There is no mutual exclusion,
 887     // so two threads could allocate different BCIs to the
 888     // same data layout.  This means these extra data
 889     // records, like most other MDO contents, must not be
 890     // trusted too much.
 891     DataLayout temp;
 892     temp.initialize(DataLayout::bit_data_tag, bci, 0);
 893     dp->release_set_header(temp.header());
 894     assert(dp->tag() == DataLayout::bit_data_tag, "sane");
 895     //NO: assert(dp->bci() == bci, "no concurrent allocation");
 896     return new BitData(dp);
 897   }
 898   return NULL;
 899 }
 900 
 901 ArgInfoData *methodDataOopDesc::arg_info() {
 902   DataLayout* dp    = extra_data_base();
 903   DataLayout* end   = extra_data_limit();
 904   for (; dp < end; dp = next_extra(dp)) {
 905     if (dp->tag() == DataLayout::arg_info_data_tag)
 906       return new ArgInfoData(dp);
 907   }
 908   return NULL;
 909 }
 910 
 911 #ifndef PRODUCT
 912 void methodDataOopDesc::print_data_on(outputStream* st) {
 913   ResourceMark rm;
 914   ProfileData* data = first_data();
 915   for ( ; is_valid(data); data = next_data(data)) {
 916     st->print("%d", dp_to_di(data->dp()));
 917     st->fill_to(6);
 918     data->print_data_on(st);
 919   }
 920   st->print_cr("--- Extra data:");
 921   DataLayout* dp    = extra_data_base();
 922   DataLayout* end   = extra_data_limit();
 923   for (; dp < end; dp = next_extra(dp)) {
 924     // No need for "OrderAccess::load_acquire" ops,
 925     // since the data structure is monotonic.
 926     if (dp->tag() == DataLayout::no_tag)  continue;
 927     if (dp->tag() == DataLayout::bit_data_tag) {
 928       data = new BitData(dp);
 929     } else {
 930       assert(dp->tag() == DataLayout::arg_info_data_tag, "must be BitData or ArgInfo");
 931       data = new ArgInfoData(dp);
 932       dp = end; // ArgInfoData is at the end of extra data section.
 933     }
 934     st->print("%d", dp_to_di(data->dp()));
 935     st->fill_to(6);
 936     data->print_data_on(st);
 937   }
 938 }
 939 #endif
 940 
 941 void methodDataOopDesc::verify_data_on(outputStream* st) {
 942   NEEDS_CLEANUP;
 943   // not yet implemented.
 944 }